Archery Duel
InterviewTime limit1sMemory limit1024 MB
Two players alternately shoot a chained target or resign, and the task is to print the first player's score under optimal play.
- Level
Medium5 of 10
- Topics
- Game theory, Dynamic programming, Prefix sum
- Solved
- No attempts yet
Problem
Even in today's peaceful KSA class, students hold an archery duel. There are targets in total. One target is fixed at the top, and the rest hang one below another in a chain. Targets are numbered from to . Target is at the top, and target () hangs from target . The score of target is .
Two players take turns. On their turn, they may choose one of the following two actions.
- Pick one target and shoot it down.
- Resign.
When a target is hit, it is destroyed and its score is added to the score of the player who hit it. Targets hanging below the destroyed target fall. The scores of fallen targets that were not destroyed are added to the opponent's score. If a player resigns, the scores of all targets that are neither destroyed nor fallen are added to the opponent's score, and the game ends immediately. The game also ends when every target is destroyed or has fallen. Both students are excellent archers, so every chosen target is hit without fail, and each player makes only optimal choices to maximize their own score. Predict the result of the game.
Input
The first line contains the integer .
The second line contains integers , separated by spaces. These are the scores of the targets.
Output
Print the score that the first player takes.