> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Maximum Value of K Coins From Piles

> Tested Python solution for LeetCode 2218 with 24 pytest cases. Generate a practice environment with lcpy.

LeetCode 2218, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Dynamic Programming](/catalog/topics/dynamic-programming), [Prefix Sum](/catalog/topics/prefix-sum). [View on LeetCode](https://leetcode.com/problems/maximum-value-of-k-coins-from-piles/description/).

Generate this problem as a practice environment: tested reference solution, 24 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2218   # by problem number
lcpy gen -s maximum_value_of_k_coins_from_piles   # by problem name
```

## Problem

There are `n` **piles** of coins on a table. Each pile consists of a **positive number** of coins of assorted denominations.

In one move, you can choose any coin on **top** of any pile, remove it, and add it to your wallet.

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

### Examples

![Example 1](https://assets.leetcode.com/uploads/2019/11/09/e1.png)

```
Input: piles = [[1,100,3],[7,8,9]], k = 2
Output: 101
Explanation: The above diagram shows the different ways we can choose k coins. The maximum total we can obtain is 101.
```

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

### Constraints

* n == piles.length
* 1 \<= piles.length \<= 10^3
* 1 \<= piles\[i]\[j] \<= 10^5
* 1 \<= k \<= sum(piles\[i].length) \<= 2000

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_value_of_k_coins_from_piles/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_value_of_k_coins_from_piles/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(total_coins * k)
    # Space: O(k)
    def max_value_of_coins(self, piles: list[list[int]], k: int) -> int:
        dp = [0] * (k + 1)
        for pile in piles:
            prefix = [0]
            for value in pile:
                prefix.append(prefix[-1] + value)
            new_dp = dp[:]
            for taken in range(1, k + 1):
                best = new_dp[taken]
                for use in range(min(taken, len(prefix) - 1) + 1):
                    candidate = dp[taken - use] + prefix[use]
                    if candidate > best:
                        best = candidate
                new_dp[taken] = best
            dp = new_dp
        return dp[k]
```

## Complexity

| Time | Space |
| - | - |
| O(total\_coins \* k) | O(k) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.