<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:
    def maxValueOfCoins(self, piles: List[List[int]], k: int) -> int:
        # dp=[[] for i in range(k+1)]
        # themax=[0]*(k+1)
        # for i in piles:
        #     s=0
        #     for j in range(len(i)):
        #         s+=i[j]
        #         dp[j+1].append(s)
        #         if j==k-1:
        #             break
        # for i in range(1,len(dp)):
        #     dp[i].sort(reverse=True)
        #     for j in range(len(dp[i])):
        #         if i+j<len(themax):
        #             themax[j+i]=max(themax[j+i],themax[j]+dp[i][j])
        #         else:
        #             break
        # print(themax)
        # return themax[-1]


        dp=[[0]*(k) for i in range(len(piles))]
        themax=[[0]*(k+1) for i in range(len(piles))]
        for i in range(len(piles)):
            s=0
            for j in range(min(k,len(piles[i]))):
                s+=piles[i][j]
                dp[i][j]=s

        for i in range(k):
            themax[0][i+1]=dp[0][i]

        for i in range(1,len(piles)):
            for j in range(k):
                if dp[i][j]==0:
                    break
                # themax[i][j]=max(themax[i-1][j],dp[i][j])
                for f in range(k+1):
                    if j+f+1>k or (f>0 and themax[i-1][f]==0):
                        break
                    themax[i][j+f+1]=max(themax[i][j+f+1],themax[i-1][f]+dp[i][j])
                #     print(themax[i-1][f]+dp[i][j])
                # print(themax)
            for j in range(k+1):
                themax[i][j]=max(themax[i-1][j],themax[i][j])
        # print(dp)
        # print(themax)
        return themax[-1][-1]
2252