Alice and Bob play with a row of piles, Alice first, starting with M = 1. On a turn, a player takes all stones from the first X remaining piles, where 1 <= X <= 2M, and then M becomes max(M, X). The game ends when no piles remain. Both play optimally. Return the maximum number of stones Alice can get.
Alice and Bob play with a row of piles, Alice first, starting with M = 1. On a turn, a player takes all stones from the first X remaining piles, where 1 <= X <= 2M, and then M becomes max(M, X). The game ends when no piles remain. Both play optimally. Return the maximum number of stones Alice can get.
Example 1:
Input: {"piles":[2,7,9,4,4]}
Output: 10
Constraints:
1≤piles.length≤100
1≤piles[i]≤104
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"piles":[2,7,9,4,4]}
suffix[i] = stones in piles i..end (total 26). Whatever the player to move does not get from that, the opponent gets.
2
0
26
7
1
24
9
2
17
4
3
8
4
4
4
small number under each pile = suffix sum from there
dp[i][M] for the reachable states (blank = never reached)