<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 maxValueOfCoins(vector<vector<int>>& piles, int k) 
    {
        int m = piles.size();

        vector<vector<long long>> sums(m+1, vector<long long>(k+1, 0));
        for (int i=1; i<=m; i++)
        {
            for (int j=1; j <= min((int)piles[i-1].size(), k); j++)
                sums[i][j] = sums[i][j-1] + piles[i-1][j-1];
        }

        vector<vector<long long>> dp(m+1, vector<long long>(k+1, 0));       // dp[i][j] = max value with [1...i] row and j coins

        for (int i=1; i<=m; i++)
        {
            int n = piles[i-1].size();

            for (int j=1; j<=k; j++)
            {
                dp[i][j] = dp[i-1][j];  // when currentCoins = 0

                for (int currentCoins = 1; currentCoins <= min(n, j); currentCoins++)
                    dp[i][j] = max(dp[i][j], sums[i][currentCoins] + dp[i-1][j - currentCoins]);
            }
        }

        return dp[m][k];
    }

/*  // Recursion
    int maxValueOfCoins(vector<vector<int>>& piles, int k) 
    {
        int m = piles.size();
        vector dp(m + 1, vector<int>(k + 1, -1));
        
        function<int(int, int)> f = [&](int i, int coins) {
            if (i == 0)
                return 0;

            if (dp[i][coins] != -1)
                return dp[i][coins];

            int csum = 0;
            for (int currentCoins = 0; currentCoins <= min((int)piles[i - 1].size(), coins); currentCoins++) 
            {
                if (currentCoins > 0)
                    csum += piles[i - 1][currentCoins - 1];

                dp[i][coins] = max(dp[i][coins], f(i - 1, coins - currentCoins) + csum);
            }
            return dp[i][coins];
        };
        
        return f(m, k);
    }                                                                                   */
};
57598