

Input is given such that the answer will fit in a 32-bit integer.
Input: 'k' = 3, 'arr' = [1, 20, 13, 4, 4, 1]
Output: 80
Explanation:
We can divide the array into the following subarrays
[1,20] max of this subarray is 20 so the contribution of
this subarray in the final answer will be 20*2=40.
[13,4,4] max of this subarray is 13 so the contribution of
this subarray in the final answer will be 13*3=39.
[1] max of this subarray is 1 so the contribution of this
subarray in the final answer will be 1*1=1.
So, the answer will be 40 + 39 + 1 = 80.
The first line of input contains two space-separated integers 'n' and 'k', denoting the size of the array and the given integer.
The second line of each test case contains 'n' space-separated integers denoting elements of the array.
Return the maximum sum of elements after partitioning.
You do not need to print anything, it has already been taken care of. Just implement the given function.
The basic idea is to check for every possible valid way of dividing the array and find the max of all valid ways.
For each index ‘i’ we have options of taking a subarray of length [1,2,..,k] from’ i‘ (including ‘i’).
Let’s say we take a subarray starting from ‘i’ and ending to ‘j’ then find the maximum element of the subarray [i,j] let it be ‘max’ so the contribution of this subarray to the final sum will be (j-i+1)*max.
Let the function maxSubarray(i, n, num, k)( ‘i’ is the current index of the array, n is the size of the array, num is the given array, k is the max size of the subarray) give the maximum sum of the subarray [i,n-1],(consider 0-based indexing here) after partitioning. So the final answer will be maxSubarray(0, n, num, k).
The basic idea is to store the results of the recursive calls to avoid repetition. We can memorize the results of our function calls in an array (say, ‘DP’). The definition of the function “maxSubarray” will remain the same. For each index will be checked whether we have already computed answer for ‘i’ or not if we have the answer then we will simply return it otherwise
We have to compute the answer for index ‘i’ as discussed in the brute force method.
For each index ‘i’ we have options of taking a subarray of length [1,2,..,k] from’ i‘ (including ‘i’).
Let say we take a subarray starting from ‘i’ and ending to ‘j’ then find the maximum element of the subarray [i,j] let it be ‘max’ so the contribution of this subarray to the final sum will be (j-i+1)*max.
The idea is similar to the previous approach. In this approach, we use an iterative way to store the count of answers for each index ‘i’ using previously computed values.
Let's define dp[i] as the maximum sum of the subarray [0,i] after partitioning. So our final answer will be dp[n-1].
For each index ‘i’ we can iterate over all the possible starting points of the subarray ending with ‘i’ lets say that index is ‘j’ so
dp[i]=(max element in subarray [j,i])*(i-j+1) + dp[j-1] (if j-1>=0).