<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>

typedef long long int  ll;

class Solution {
public:
    vector<int>prefixSum;
    const int INF = 1e8;
    int blockSize;
    int dp[31][31][31];
    int getRangeSum(int l,int r){
        return prefixSum[r] - (l?prefixSum[l-1]:0);
    }
    int solve(int l,int r,int piles,vector<int>& stones){
        if(dp[l][r][piles]!=-1)return dp[l][r][piles];
        if(l==r && piles==1)return dp[l][r][piles] = 0;
        if(l==r)return INF;
        if(piles==1){
            return dp[l][r][piles] = solve(l,r,blockSize,stones) + getRangeSum(l,r);
        }
        int ans = INF;
        for (int i = l; i <r ; ++i) {
            int op1 = solve(l,i,1,stones) + solve(i+1,r,piles-1,stones);
            ans = min(ans,op1);
        }
        return dp[l][r][piles] = ans;
    }
    int mergeStones(vector<int>& stones, int k) {
        int n = (int)stones.size();
        if((n-1)%(k-1))return -1;
        blockSize = k;
        prefixSum.assign(n,0);
        for (int i = 0; i < n; ++i) prefixSum[i] = stones[i] + (i?prefixSum[i-1]:0);
        for (int i = 0; i < 31; ++i)
            for (int j = 0; j < 31; ++j)
                for (int l = 0; l < 31; ++l)dp[i][j][l]=-1;

        int ans = solve(0,n-1,1,stones);
        if(ans==INF)ans=-1;
        return ans;
    }
};

static const auto speedup = []() {
    std::ios::sync_with_stdio(false); std::cin.tie(nullptr); return 0;
}();
9900