#P3210. [HNOI2010] 取石头游戏
[HNOI2010] 取石头游戏
Description
Company A is hosting a two-player puzzle contest — the stone-taking game. The winner will receive a generous prize provided by Company A, attracting many clever contestants from all over the country.
Compared with the classic stone-taking game, the rules of this contest are much more complex:
- There are piles of stones arranged in a row. The -th pile contains stones.
- Some piles have already been deliberately removed by Company A at the start.
- The two players then take turns. On each turn, a player may take all the stones from exactly one pile, subject to the following constraint: to take a pile, at least one of its adjacent piles must have already been taken (either previously by a player or initially removed by Company A). Note: pile is adjacent only to pile , pile is adjacent only to pile , and any other pile is adjacent to piles and .
- The game ends when all stones have been taken. The player who has collected more stones in total wins.
As one of the contestants, you want to know, for any game, if both the first player and the second player play optimally, what total number of stones each will obtain.
Input Format
The first line contains a positive integer , the number of piles. The second line contains space-separated nonnegative integers , where is the number of stones in the -th pile, and means the -th pile was initially removed by Company A. The input guarantees , and there exists at least one such that .
Output Format
Output a single line containing two integers: the total stones obtained by the first player and by the second player under optimal play, separated by a single space.
8
1 2 0 3 7 4 0 9
17 9
Hint
Sample explanation: when both players play optimally, the piles are taken in the order , so the first player gets stones and the second player gets stones.
Constraints:
- For of the testdata, .
- For of the testdata, .
Translated by ChatGPT 5
京公网安备 11011102002149号