

You are given ‘ARR’ = [3, 4, 6, 7, 2] and ‘K’ = 3. Here you can divide the array into the subarrays [3], [4, 6, 7] and [2]. Here the sum of maximum values of all subarrays is 3 + 7 + 2 = 12.
The first line of input contains a single integer ‘T’ representing the number of test cases.
The first line of each input contains two space-separated integers ‘N’ and ‘K’, representing the size of the array and the given integer ‘K’.
The second line of each test case contains ‘N’ space-separated integers representing the elements of the array.
For each test print a single integer representing the minimum possible sum of maximum values of ‘K’ subarrays of the given array.
Print a separate line for each test case.
1 <= T <= 10
1 <= N <= 1000
1 <= K <= N
1 <= |ARR[i]| <= 10^6
Time Limit: 1 sec
You do not need to print anything. It has already been taken care of. Just implement the given function
In this approach, we iterate over every position in the array and try to make a subarray till that index and recursively call the function on the rest of the array.
At every position, we know where the array starts from the arguments in the recursive function, so we calculate the maximum from the starting position of the current subarray and the position we are making the cut and add it to the recursive call on the rest of the array. We take the minimum of the places of possible sum and return the minimum sum.
We create a recursive function subarraySum(arr, startPos, splitRem), Where arr is the given array, startPos is the starting index of the current subarray, splitRem is the number of remaining splits.
In this approach, we utilise the same technique as the last approach, but there are multiple recursive calls with the same parameters so, we will create a cache to store the results and avoid multiple recursive calls.
We create a recursive function subarraySum(arr, startPos, splitRem, cache), Where arr is the given array, startPos is the starting index of the current subarray, splitRem is the number of remaining splits.cache is the cache that stores the values of recursive calls.
In this approach, we will use the stack to speed up things, because we don’t have to check every single cutoff position The stack keeps the index of monotonically decreasing number in A. When checking ARR[i], assuming that the last number that is larger than ARR[i] is ARR[t] (stack.top() = t after poping), there are two possible situations:
Remember that in 2D Array: dp[k][i] means the min value in k subarrays for i elements, it equals dp[d][i] = dp[d][t] (t < i so dp[d][t] should have already been calculated)