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

from typing import List

class Solution:
    def maxValueOfCoins(self, piles: List[List[int]], k: int) -> int:
        # Memoization dictionary to store intermediate results
        dp = {}
        
        # Recursive function to calculate the maximum value of coins
        def dfs(i: int, k: int) -> int:
            # If we have considered all piles or have no more coins to pick
            if i == len(piles) or k == 0:
                return 0
            
            # Check if result for this state is already computed
            if (i, k) in dp:
                return dp[(i, k)]
            
            # Option 1: Skip the current pile and move to the next pile
            best = dfs(i + 1, k)
            
            # Option 2: Try taking coins from the current pile
            current_sum = 0
            # Go through all possible numbers of coins we can take from the current pile
            for j in range(min(len(piles[i]), k)):
                current_sum += piles[i][j]
                # Recursively compute the maximum value considering the coins taken from this pile
                best = max(best, current_sum + dfs(i + 1, k - (j + 1)))
            
            # Memoize the result
            dp[(i, k)] = best
            return best
        
        # Start the recursion from the first pile and k coins
        return dfs(0, k)

# Time Complexity: O(n * k^2), where n is the number of piles and k is the maximum number of coins.
# Space Complexity: O(n * k), due to memoization storage.

# Creating an instance of Solution and testing with the provided examples
solution = Solution()

# Test case 1
piles1 = [[1, 100, 3], [7, 8, 9]]
k1 = 2
print(solution.maxValueOfCoins(piles1, k1))  # Output: 101

# Test case 2
piles2 = [[100], [100], [100], [100], [100], [100], [1, 1, 1, 1, 1, 1, 700]]
k2 = 7
print(solution.maxValueOfCoins(piles2, k2))  # Output: 706

        
4698