Media.net coding challenge question

Help me to solve

This is year 2050 and you plan to make a hi-tech bus with resizable technology.

There are N bus stops, and each bus stop has A[i] people standing on it. Everyone in a group stick together, so either everyone
jumps on the bus or neither person does.

Also, you first have to pick up people of group 1, then group 2 and so on. The passengers don’t travel a lot, so the group 1 leaves before group 2 joins.

The resizable technology, although very useful, produces a lot of smoke, so the government has decided to put a restriction to its use upto B times a day.

Even you don’t like your bus to be empty, so you decide to measure the utility of your bus by the number of empty seats it has, throughout the day.
The lesser seats that are wasted, the happier you will be.

Given the groups of people on each bus stop, find and return the minimum sum of total wasted space using your resizable technology upto B times a day.

Note: It is necessary that the number of passengers in a bus must be less than or equal to the capacity of the bus.

Input Format:

The first argument of input contains an integer array A, representing the number of people in all groups.
The second argument of input contain an integer B, representing the number of times you can use the resizable technology.
Output Format:

Return an integer, representing the minimum sum of total wasted space.

Constraints:

1 <= N <= 400
0 <= A[i] <= 1e6
1 <= B < N
Examples:

Input 1:
A = [10, 20]
B = 0

Output 1:
10

Explanation 1:
You start with a bus of size 20.
Group 1 contains 10 people, so you waste 10 space. (Wasted space + 10).
Group 2 contains 20 people, so you do not waste any space. (Wasted space + 0).

Input 2:
A = [1, 2, 3]
B = 1

Output 2:
1

Explanation 2:
You start with a bus of size 2.
Group 1 contains only 1 person, so you waste a space. (Wasted space + 1).
Group 2 contains 2 people, so you do not waste any space. (Wasted space + 0).
You change the size of the bus to 3.
Group 3 contains 3 people, so you do not waste any space. (Wasted space + 0).
Comments (1)