Bob has joined a part-time job as a delivery man. He needs to deliver ‘N’ packages with ‘A[i]’ value within ‘K’ days.
In one day, Bob can deliver as many packages as he wants, but the ‘i’th package cannot be delivered until ‘(i - 1)’th package is not delivered.
Each day, Bob earns the maximum value of the packages Bob delivered during that day.
Can you help Bob to find a way to deliver his packages to maximize his earning?
Note: If Bob does not deliver any packages someday, his earning will be 0 on that day.
The first line contains ‘T’, denoting the number of test cases.
The first line of each test case contains two space-separated integers, ‘N’ & ‘K’.
The following line contains ‘N’ space-separated integers denoting the array ‘A’.
Output Format:
For each test case, print an integer denoting the maximum amount Bob can earn after all deliveries.
Note:
You are not required to print the expected output. It has already been taken care of. Just implement the function.
1 <= T <= 5
1 <= K <= N <= 10^5
1 <= A[i] <= 10^9
Time Limit: 1 sec
2
5 1
1 2 3 4 5
5 5
1 2 3 4 5
5
15
In the first test case, Bob will deliver all packages on the first day, so his earning will be max(1, 2, 3, 4, 5) = 5.
In the second test case, Bob will deliver one package each day, so his earning will be 1 + 2 + 3 + 4 + 5 = 15.
2
6 3
7 7 7 7 7 7
5 5
1 2 3 7 8
21
21
Can we check the earnings for all possible configurations?
Iterate through every possible configuration and calculate the earning at each configuration. The maximum of those earnings will be our answer.
Algorithm:
O((N + K - 1)CN), where ‘N’ is the number of packages, and ‘K’ is the number of days.
There are (N + K - 1)CN possible configurations, and we are iterating through all of them.
O(K), where K is the number of days.
There will be at most K function calls in the stack space for recursion at a given point of time.