<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 {
private:
    int maxi(vector<int>& v, int l, int r) {
        int tem = -1;
        int n = v.size();
        n = min(n, r);
        for (int i = l; i < n; i++) {
            tem = max(tem, v[i]);
        }
        return tem;
    }

public:
    int solve(vector<int>& arr, int k, int index,unordered_map<string,int> &dp) {
        int ans = 0;
        if (index >= arr.size())
            return 0;
        string key=to_string(index)+","+to_string(k);
        if(dp.find(key)!=dp.end())
        return dp[key];
        for (int i = 1; i <= k; i++) {
            ans = max(ans,

                      maxi(arr, index, index + i) * min(i, (int)arr.size() - index) +
                          solve(arr, k, index + i,dp)

            );
            // cout << "index: " << index << " k:" << i << " ans:" << ans << "\n";
        }
        dp[key]=ans;
        return ans;
    }
    int maxSumAfterPartitioning(vector<int>& arr, int k) {
        unordered_map<string,int> dp;
        return solve(arr, k, 0,dp);
    }
};

// 1+SOLVE({2,3},2)=1+
// 2*2+SOLVE({3},2)
15300