#P3004. [USACO10DEC] Treasure Chest S
[USACO10DEC] Treasure Chest S
Description
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
京公网安备 11011102002149号