Problem 645
Stone Game II
Medium · 20 pts · Dynamic Programming, Game Theory
Problem
Piles of stones stand in a row. Alice and Bob take turns, Alice first, starting with M = 1. On a turn the player takes all the stones of the first X remaining piles, where 1 <= X <= 2M, and then M becomes max(M, X). The game ends when no piles are left. Both play optimally to collect as many stones as possible. Print the number of stones Alice gets. Let best(i, M) be the most a player can collect from piles i onwards; it equals the stones left minus the opponent's best after the move.
Input
The first line contains an integer N, the size of the array. The second line contains N space-separated integers A[0], A[1], ..., A[N-1].
Output
Print the maximum number of stones Alice can get.
Constraints
1 <= N <= 100; 1 <= A[i] <= 10000
Example input
5 2 7 9 4 4
Example output
10
Explanation
With piles 2 7 9 4 4, Alice takes the first pile (2), Bob takes the next two (7 and 9), and Alice then takes the last two (4 and 4), for 2 + 4 + 4 = 10. No other line of play gives Alice more, so the answer is 10.