#P3004. [USACO10DEC] Treasure Chest S
[USACO10DEC] Treasure Chest S
Problem Description
Bessie and Bonnie have found a treasure chest full of marvelous gold coins! Being cows, though, they can't just walk into a store and buy stuff, so instead they decide to have some fun with the coins.
The N (1 <= N <= 5,000) coins, each with some value C_i (1 <= C_i <= 5,000) are placed in a straight line. Bessie and Bonnie take turns, and for each cow's turn, she takes exactly one coin off of either the left end or the right end of the line. The game ends when there are no coins left.
Bessie and Bonnie are each trying to get as much wealth as possible for themselves. Bessie goes first. Help her figure out the maximum value she can win, assuming that both cows play optimally.
Consider a game in which four coins are lined up with these values:
30 25 10 35
Consider this game sequence:
Bessie Bonnie New Coin
Player Side CoinValue Total Total Line
Bessie Right 35 35 0 30 25 10
Bonnie Left 30 35 30 25 10
Bessie Left 25 60 30 10
Bonnie Right 10 60 40 --
This is the best game Bessie can play.
Two players, A and B, are playing a game.
Initially, there are coins in a line. From left to right, the value of the -th coin is .
A and B take turns, one move per turn. On a player's turn, they must choose the leftmost or the rightmost coin from the current sequence, remove it from the sequence, and add its value to their own total. The game ends when all coins have been taken.
Assuming both players play optimally to maximize their own total value, and player A moves first, determine the maximum total value that A can obtain.
Input Format
- The first line contains an integer , the number of coins.
- Lines to each contain one integer. The integer on line is the value of the -th coin.
Output Format
Output a single integer: the maximum total value that player A can obtain.
4
30
25
10
35
60
Hint
-
Sample explanation:
Initially, the coin sequence is .
- Turn 1: A takes the rightmost coin, the sequence becomes , and A’s total is .
- Turn 2: B takes the leftmost coin, the sequence becomes , and B’s total is .
- Turn 3: A takes the leftmost coin, the sequence becomes , and A’s total is .
- Turn 4: B takes the leftmost coin, the sequence becomes empty, and B’s total is . The game ends.
The maximum total value that A can obtain is .
-
Constraints:
For all testdata, , .
-
Note: The memory limit is MiB.
Translated by ChatGPT 5