Ctrl K

The Growing Grab Limit

Medium
Chests of coins stand in a row; chests[i] is the number of coins in chest i. You and an opponent take them in turns, and you go first. Both players share a parameter M, which starts at M = 1. On a turn, a player takes some of the first remaining chests in a row, anywhere from 1 to 2·M of them, say X, and then M becomes max(M, X). The game goes on until no chests are left. Both play optimally, each trying to collect as many coins as possible. How many coins do you end up with?
Example 1
chests=[3, 1, 4, 1, 5] → 8

It pays to start with one chest (3). If the opponent then takes two (1 and 4), both remaining chests go to you for 9 in total. So the opponent takes one (1), you take the next two (4 and 1), and the opponent gets the 5. You end with 3 + 4 + 1 = 8. Taking two chests at the start would let the opponent take all three remaining ones, leaving you with only 4.

Example 2
chests=[5, 1, 1, 8] → 13
Example 3
chests=[12] → 12

The only chest is yours.

Constraints
  • 1 ≤ len(chests) ≤ 100
  • 1 ≤ chests[i] ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMerge the Front Tiles HardDuel for the End Cards MediumDuel for the End Heaps MediumSegments With an Odd Sum MediumScoring What Remains Medium
loading Python…
Input
chests=[3, 1, 4, 1, 5]
Expected
8

Run checks the first 3 tests in your browser. Submit checks all 98 tests on the server, hidden ones included.