<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 dp[31][31][31];

//     void helper(vector<int>& stones, int l, int r, int k, int ind, int curr, int total, vector<int>& vec, set<vector<int>>& V)
//     {
//         if(ind == r && vec.size() == k-2 && (curr == 1 || curr >= k))
//         {
//             vec.push_back(ind);
//             V.insert(vec);
//             vec.pop_back();
//             return ;

//         }
//         if(ind > r && vec.size() == k-1) 
//         {
//             V.insert(vec);
//             return ;
//         }
//         if(ind > r) return ;
//         if(dp[ind][curr][total] != -1) return ;
//         cout<<ind<<" "<<curr<<" "<<total<<" "<<l<<" "<<r<<"\n";
//         if(curr == 1 || curr >= k)
//         {
//             helper(stones, l, r, k, ind+1, curr+1, total, vec, V);
//             vec.push_back(ind);
//             helper(stones, l, r, k, ind+1, 1, total+1, vec, V);
//             vec.pop_back();
//         }
//         else
//         {
//             helper(stones, l, r, k, ind+1, curr+1, total, vec, V);
//         }
//         dp[ind][curr][total] = 1;
//         return ;
//     }


//     int dpp[31][31];
//     map<int,int> pre;
//     int func(vector<int>& stones, int l, int r, int k)
//     {
//         if(dpp[l][r] != -1) return dpp[l][r];
//         if(l > r) return 1e9;
//         if(l == r) return stones[l];
//         vector<int> vec;
//         memset(dp, -1, sizeof(dp));
//         set<vector<int>> V;
//         int ans = 1e9;
        
//         helper(stones, l, r, k, l, 0, 1, vec, V);
//         for(auto &it: V)
//         {
//             int temp = 0;
//             cout<<it[0]<<" hmm ";
//             for(int i = 1; i<it.size(); i++)
//             {
//                 cout<<it[i]<<" ";
//                 temp += func(stones, it[i], it[i-1], k);
//             }
//             cout<<"\n";
//             temp += func(stones, l, it[0], k);
//             temp += func(stones, it[it.size()-1], r, k);
//             temp += pre[r] - pre[l-1];
//             ans = min(ans, temp);
//         }
//         return dpp[l][r] = ans;
//     }

//     int mergeStones(vector<int>& stones, int k) {
//         if(stones.size() % (k-1) != 0 && stones.size() != 1) return -1;
//         memset(dpp, -1, sizeof(dpp));
//         int sum = 0;
//         pre[-1] = sum;
//         for(int i=0; i<stones.size(); i++)
//         {
//             sum += stones[i];
//             pre[i] = sum;
//         }
//         int ans = func(stones, 0, stones.size()-1, k);
//         if( ans > 1e8) return -1;
//         return ans;
//     }
// };

class Solution {
public:
    int dp[31][31];
    map<int,int> pre;
    
    int func(vector<int>& stones, int l, int r, int k) {
        if (dp[l][r] != -1) return dp[l][r];
        if (l == r) return 0;
        
        int ans = INT_MAX;
        for (int m = l; m < r; m += k-1) {
            ans = min(ans, func(stones, l, m, k) + func(stones, m+1, r, k));
        }
        
        // After merging the entire segment from l to r, we need to add the total sum of that segment
        if ((r - l) % (k - 1) == 0) {
            ans += pre[r+1] - pre[l];
        }
        
        return dp[l][r] = ans;
    }
    
    int mergeStones(vector<int>& stones, int k) {
        int n = stones.size();
        if ((n - 1) % (k - 1) != 0) return -1;  // Impossible to merge the stones
        
        pre[0] = 0;
        for (int i = 0; i < n; i++) {
            pre[i + 1] = pre[i] + stones[i];
        }
        
        memset(dp, -1, sizeof(dp));
        return func(stones, 0, n - 1, k);
    }
};

11900