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

class Solution {
public:

    // int solve(vector<vector<int>>& piles, int n ,int k){
    //         if(k<=0) return 0;
    //         int maxi=INT_MAX;
    //         for(int i=0;i<n;i++){
                 
    //         }
    // }
    // int solve(vector<vector<int>>& piles,int n ,int k , int j,int i){
    //     cout<<"i:"<<i<<endl;
    //     if(i==piles[i].size()-1 && k==1) return piles[i][j];
    //     if(i>=n) return 0;
    //     // if(i>=n) return 0;
    //     if(k<=0) return 0;
    //     if(j+1>=piles[i].size()) return 0;
       
    //     // cout<<"i="<<i<<endl;
    //     int tk,ntk=0;
    //     tk=piles[i][j]+solve(piles,n,k-1,j+1,i);
    //     ntk=ntk+solve(piles,n,k,j+1,i);

    //     return max(tk,ntk);

          
    // }

    // int sol(vector<vector<int>>& piles,int n, int k,int i){
    //     int res=0;
    //     for(int j=0;j<k;j++){
    //           res+=piles[i][j];
    //     }

    //     return res;
    // }

    int t[1001][2002];

    int solve(int i,vector<vector<int>>& piles, int k,int n){
           if(i>=n) return 0;
           if(t[i][k] != -1) return t[i][k];

           int ntk=solve(i+1,piles,k,n);

           int sum=0; int take=0;
           for(int j=0;j<min((int)k,(int)piles[i].size());j++){

               sum+=piles[i][j];
               take=max(take,sum+solve(i+1,piles,k-(j+1),n));
           }

           return t[i][k]= max(take,ntk);
    }




    int maxValueOfCoins(vector<vector<int>>& piles, int k ) {
        int n=piles.size();
        memset(t,-1,sizeof(t));

        int res=solve(0,piles,k,n);
        
        return res;

    }
};
22186