<p>There are <code>n</code> piles of <code>stones</code> arranged in a row. The <code>i<sup>th</sup></code> pile has <code>stones[i]</code> stones.</p>

<p>A move consists of merging exactly <code>k</code> <strong>consecutive</strong> piles into one pile, and the cost of this move is equal to the total number of stones in these <code>k</code> piles.</p>

<p>Return <em>the minimum cost to merge all piles of stones into one pile</em>. If it is impossible, return <code>-1</code>.</p>

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

<pre>
<strong>Input:</strong> stones = [3,2,4,1], k = 2
<strong>Output:</strong> 20
<strong>Explanation:</strong> We start with [3, 2, 4, 1].
We merge [3, 2] for a cost of 5, and we are left with [5, 4, 1].
We merge [4, 1] for a cost of 5, and we are left with [5, 5].
We merge [5, 5] for a cost of 10, and we are left with [10].
The total cost was 20, and this is the minimum possible.
</pre>

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

<pre>
<strong>Input:</strong> stones = [3,2,4,1], k = 3
<strong>Output:</strong> -1
<strong>Explanation:</strong> After any merge operation, there are 2 piles left, and we can&#39;t merge anymore.  So the task is impossible.
</pre>

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

<pre>
<strong>Input:</strong> stones = [3,5,1,2,6], k = 3
<strong>Output:</strong> 25
<strong>Explanation:</strong> We start with [3, 5, 1, 2, 6].
We merge [5, 1, 2] for a cost of 8, and we are left with [3, 8, 6].
We merge [3, 8, 6] for a cost of 17, and we are left with [17].
The total cost was 25, and this is the minimum possible.
</pre>

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

<ul>
	<li><code>n == stones.length</code></li>
	<li><code>1 &lt;= n &lt;= 30</code></li>
	<li><code>1 &lt;= stones[i] &lt;= 100</code></li>
	<li><code>2 &lt;= k &lt;= 30</code></li>
</ul>

class Solution {
public:
    int solve(int i, int j, vector<int>& prefix, int k, vector<vector<int>>&dp) {
        if ((j - i + 1) < k)
            return 0;
            if(dp[i][j]!=-1)
            return dp[i][j];
        int t = k - 1;
        int ans = INT_MAX;
        for (int x = i; x < j; x += t) {
            int left = solve(i, x, prefix, k,dp);
            int right = solve(x + 1, j, prefix, k,dp);
            ans = min({ans, left+right});
        }

        if ((j - i) % (k - 1) == 0) {
            int sum = 0;
            sum += prefix[j];
            if (i > 0)
                sum -= prefix[i-1];
            ans += sum;
        }
     return dp[i][j]= ans;
    }
    int mergeStones(vector<int>& stones, int k) {
        vector<vector<int>>dp(stones.size(),vector<int>(stones.size(),-1));
        if ((stones.size() - 1) % (k - 1) != 0)
            return -1;
        vector<int> prefix(stones.size(), 0);
        prefix[0] = stones[0];

        for (int i = 1; i < stones.size(); i++) {
            prefix[i] = prefix[i - 1] + stones[i];
        }
        

        return solve(0, stones.size() - 1, prefix, k,dp);
    }
};
11300