<p>Given an integer array <code>arr</code>, partition the array into (contiguous) subarrays of length <strong>at most</strong> <code>k</code>. After partitioning, each subarray has their values changed to become the maximum value of that subarray.</p>

<p>Return <em>the largest sum of the given array after partitioning. Test cases are generated so that the answer fits in a <strong>32-bit</strong> integer.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> arr = [1,15,7,9,2,5,10], k = 3
<strong>Output:</strong> 84
<strong>Explanation:</strong> arr becomes [15,15,15,9,10,10,10]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> arr = [1,4,1,5,7,3,6,1,9,9,3], k = 4
<strong>Output:</strong> 83
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> arr = [1], k = 1
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= arr.length &lt;= 500</code></li>
	<li><code>0 &lt;= arr[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>1 &lt;= k &lt;= arr.length</code></li>
</ul>

class Solution {
public:
    int t[501];
    int solve(vector<int>& ip,int ind,int n,int k){
        if(ind>=n) return 0; 

        if(t[ind]!=-1) return t[ind];

        int ans=INT_MIN;
        int maxi=INT_MIN;

        // jis index pe hai vnha se k size tak ka check kr rhe 
        // you may think that why min ?
        // think about the case that, you have not k element then the group must be less than k na, isiliye ki out of bound na ho jaye.
        for(int i=ind;i<min(ind+k,n);i++){
            maxi=max(ip[i],maxi);
            // partition kar rhe and us range ka maximum nikal rhe
            ans=max(ans,solve(ip,i+1,n,k) +(i-ind+1)*maxi);
        }

        return t[ind]=ans;
    }
    int maxSumAfterPartitioning(vector<int>& arr, int k) {
        int n=arr.size();
        memset(t,-1,sizeof(t));
        return solve(arr,0,n,k);
    }
};
11000