Hocolate Hame
Time limit2sMemory limit2048 MB
Two players eat a row of pieces from opposite ends, each move taking k or k+1 pieces, and the task is the optimal sweetness difference between them.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Prefix sum, Game theory
- Solved
- No attempts yet
Problem
Azizkhan and Temirulan love Swiss chocolate. Recently they bought a chocolate bar made of a row of pieces. Each piece has a certain amount of sweetness, and the sweetness can be negative. The pieces are numbered from left to right. Azizkhan eats from the left side and Temirulan eats from the right side, taking turns. Each player eats one or more pieces per turn. Azizkhan goes first and can eat either 1 or 2 pieces. If pieces were eaten on the previous move, the current player must eat either or pieces. They stop when the next move is impossible. Each player wants to eat more sweetness than the other, so each player tries to maximize the difference between the total sweetness of the pieces he ate and the total sweetness of the pieces the opponent ate. Find the difference between the total sweetness consumed by Azizkhan and the total sweetness consumed by Temirulan if both play optimally.
Input
The first line contains an integer (), the number of pieces. The second line contains integers (), the sweetness values of the pieces.
Output
Output a single integer: the difference between the total sweetness consumed by Azizkhan and the total sweetness consumed by Temirulan if both play optimally.