


The first line of input contains an integer ‘T’, denoting the number of test cases. Then the test cases follow.
The first line of each test case contains two space-separated integers, ‘N’ and ‘K’, denoting the size of array ‘ARR’ and the number of friends Ninja has.
The second line of each test case contains ‘N’ space-separated integers denoting the elements of the array ‘ARR’.
For each test case, print the maximum sweetness that Ninja can get under the given condition.
Print the output of each test case in a new line.
You are not required to print the expected output, it has already been taken care of. Just implement the function.
1<= T <= 10
1 <= N <= 50000
0 <= K < N
1 <= ARR[i] <= 10000
Where 'ARR[i]' is the sweetness of the ‘i_th' chunk.
Time Limit: 1 sec
The idea is to figure out the recursive calls and optimize them using dynamic programming.
It breaks down into a problem of dividing the array into ‘K’ + 1 partitions such that the partition which has the lowest sum of sweetness is maximum as compared to all the possible partitions. It’s better to make a prefix array as we would need the prefix sum of the array in every iteration.
Now, we will start from the bottom, and then we will find the final answer. We will find the answer for breaking the subarray from indices from 0 to ‘i’ into ‘j’ parts where ‘i’ lies from 0 to ‘N’ - 1 and ‘j’ lies from 0 to a minimum of ‘M’ and ‘i’ + 1.
There is a base condition: If the number of chunks is less than the number of desired divisions, then Ninja won’t get any chunks. So, the answer will be 0.
The steps are as follows:
The idea is to find the range in which the minimum sweetness can lie and then iterate in the given range and find the maximum sweetness you can have. The minimum value possible of sweetness is the minimum sweetness in the given array ‘ARR’, and the maximum value of sweetness is the sum of the sweetness of the chunks in the array ‘ARR’. The goal is to maximize the minimum sweetness one can have. Then use binary search to maximize the minimum sweetness of the divided chunks. The reason is that the range ‘MINSWEETNESS’ to ‘MAXSWEETNESS’ is a sorted range. So, if we can use binary search, then we can cut down the time complexity from O('SUM') to O(log('SUM')) for searching, which increases the efficiency.
There is a base condition: If the number of chunks is less than the number of desired divisions, then Ninja won’t get any chunks. So, the answer will be ‘0’.
The steps are as follows: