<p>There are <code>n</code> <strong>piles</strong> of coins on a table. Each pile consists of a <strong>positive number</strong> of coins of assorted denominations.</p>

<p>In one move, you can choose any coin on <strong>top</strong> of any pile, remove it, and add it to your wallet.</p>

<p>Given a list <code>piles</code>, where <code>piles[i]</code> is a list of integers denoting the composition of the <code>i<sup>th</sup></code> pile from <strong>top to bottom</strong>, and a positive integer <code>k</code>, return <em>the <strong>maximum total value</strong> of coins you can have in your wallet if you choose <strong>exactly</strong></em> <code>k</code> <em>coins optimally</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/11/09/e1.png" style="width: 600px; height: 243px;" />
<pre>
<strong>Input:</strong> piles = [[1,100,3],[7,8,9]], k = 2
<strong>Output:</strong> 101
<strong>Explanation:</strong>
The above diagram shows the different ways we can choose k coins.
The maximum total we can obtain is 101.
</pre>

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

<pre>
<strong>Input:</strong> piles = [[100],[100],[100],[100],[100],[100],[1,1,1,1,1,1,700]], k = 7
<strong>Output:</strong> 706
<strong>Explanation:
</strong>The maximum total can be obtained if we choose all coins from the last pile.
</pre>

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

<ul>
	<li><code>n == piles.length</code></li>
	<li><code>1 &lt;= n &lt;= 1000</code></li>
	<li><code>1 &lt;= piles[i][j] &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= k &lt;= sum(piles[i].length) &lt;= 2000</code></li>
</ul>

int curr[2001][2001];
int maxVal[2001];
class Solution {
public:
    int maxValueOfCoins(vector<vector<int>>& piles, int k) {
        //recursive: each step choose from one of the piles
        // and increment that pile's index by 1 for next iteration
        // need bth coin of ath pile to not be taken yet
        // so calc. for b of pile a take it as the t-th coin for t=1 to k
        // dp[a][b][c]:max val taking bth coin of ath pile as the cth coin
        // dp[a][b][c]=max(dp[a][b-1][c-1],dp[i][j][c-1])

        // take in order so take from pile 0,1,2,... 
        // so if we take any except top coin from pile
        // need to take the coin above it to get to curr coin

        // for top coin keep count of max val for t-1 coins before curr pile
        // if top coin is the t'th coin
        memset(maxVal,0,sizeof maxVal);//[piles.size()] dp[k+1]
        // int curr[2001][2001];
        for(int i=0;i<piles.size();i++){
            memset(curr,0,(sizeof(int))*piles.size()*(k+1));
            for(int t=k;t>=1;t--){
                curr[0][t]=maxVal[t-1]+piles[i][0];
                maxVal[t]=max(maxVal[t],curr[0][t]);
            }
            for(int j=1;j<piles[i].size();j++){
                for(int t=j+1;t<=k;t++){
                    if(curr[j-1][t-1]>0){
                        curr[j][t]=curr[j-1][t-1]+piles[i][j];
                        maxVal[t]=max(maxVal[t],curr[j][t]);
                    }
                }
            }
        }
        return maxVal[k];
    }
};
25914