Coin Game
Time limit1sMemory limit32 MB
Two players alternately take coins from the top of a pile, where each move may take between 1 and twice the previous move's count; find the maximum total value the first player can guarantee with optimal play from both sides.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Game theory, Greedy, Prefix sum
- Solved
- No attempts yet
Problem
Two players take turns in a coin game.
Initially coins are stacked in a single pile. The -th coin from the top has value .
The first player takes one or two coins from the top of the pile. On every turn after that, a player may take at most twice as many coins as the previous player just took, and must take at least one. In other words, if the previous player took coins, the current player may take from up to coins from the top. (If fewer coins remain, they take only what is left.) The game ends when no coins remain.
Both players act optimally to maximize the total value of the coins they collect. Assuming the second player also plays to maximize their own total, find the maximum total value the first player can collect.
Input
The first line contains the number of coins ().
Each of the next lines contains , the value of the -th coin from the top ().
Output
Print the maximum total value the first player can collect.
Hint
Consider coins whose values from top to bottom are .
The first player takes one coin (value ). The second player also takes one coin (value ). The first player then takes two coins (values , total ). Finally the second player takes the remaining coin (value , total ). The first player's collected value of is the maximum.