Imagine you're a treasure hunter who has discovered n piles of ancient coins on a mysterious table. Each pile contains coins of different denominations stacked on top of each other, but here's the catch: you can only take coins from the top of any pile!
You have exactly k moves to collect coins. In each move, you can:
- Choose any pile that still has coins
- Take the topmost coin from that pile
- Add its value to your treasure collection
Given a list piles where piles[i] represents the i-th pile from top to bottom, and a positive integer k, your goal is to maximize the total value of coins you can collect in exactly k moves.
Example: If you have piles [[1,100,3], [7,8,9]] and k=2, you could take the top coin from pile 1 (value 1) and top coin from pile 2 (value 7) for a total of 8. But optimally, you'd take both coins from pile 2 (7+8=15) for maximum value!
Input & Output
Constraints
- n == piles.length
- 1 โค n โค 1000
- 1 โค piles[i][j] โค 105
- 1 โค k โค sum of all pile lengths
- Important: You can only take coins from the top of piles