
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’.
For each test case, print an integer denoting the maximum amount Bob can earn after all deliveries.
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
Iterate through every possible configuration and calculate the earning at each configuration. The maximum of those earnings will be our answer.
Iterate through every possible configuration and calculate the earning at each configuration. The maximum of those earnings will be our answer.
We will reduce the time of execution with the help of memoization as there are overlapping subproblems.
The maximum possible profit you can obtain is the sum of the given array’s ‘K’ largest values. We can always separate these ‘K’ maximums and then extend the segments corresponding to them to the left or the right and cover the entire array.