<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 mergeStones(vector<int>& stones, int K) {
        int n=stones.size();
        //dp[i][j][k] stores the minimum cost to convert the segment [i..........j] into k piles
        //answer will be dp [0][n-1][1],coz in the end we need only one pile
        vector<int> prefix(n,0);
        prefix[0]=stones[0];
        for(int i=1;i<n;i++){
            prefix[i]=prefix[i-1]+stones[i];
        }
        vector<vector<vector<int>>> dp(n,vector<vector<int>>(n,vector<int> (K+1,INT_MAX)));
        //TIME FOR SOME BASE CASES
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(i==j)
                  dp[i][j][1]=0;//its already a pile,so its minimum cost is 0 obv
            }
        }
        for(int len=2;len<=n;len++){
            for(int i=0;i+len-1<n;i++){
                int j=i+len-1;
                //so we have the segment [i......j]
                //now we have to make k piles out of these [i....j] where it can be 1 pile,2 pile,..K piles
                //for 1 pile its kindof like a base case,see below
                for(int k=2;k<=K;k++){
                    //we will split using m
                    //making [i....m] a pile and [m+1...j] k-1 piles
                    if (k>len) 
                    {
                        continue;
                    }
                    for(int m=i;m<j;m++){  
                        if (dp[i][m][1]==INT_MAX || dp[m+1][j][k-1]==INT_MAX) 
                            continue;   
                        dp[i][j][k]=min(dp[i][j][k],dp[i][m][1]+dp[m+1][j][k-1]);
                    }
                    //ask urself what is dp[i][m][1]
                    //it actually means [i....m] must be made 1 pile but also the cost needs to be minimum
                }
                if(dp[i][j][K]!=INT_MAX)
                    dp[i][j][1]=dp[i][j][K]+prefix[j]-((i-1>=0)?prefix[i-1]:0);
                //making 1 pile equals making K consecutive piles before merging into pile with minimum cost where k=K and the total sum is basically the prefix[j]-prfix[i-1]
            }
        }
        return ((dp[0][n-1][1]==INT_MAX)?-1:dp[0][n-1][1]);
    }
};
14000