Cutting the Cake 2
Time limit2sMemory limit512 MB
JOI chooses the first slice of a round cake, then both sides take exposed ends in turn against an opponent who always takes the larger end.
- Level
Medium7 of 10
- Topics
- Game theory, Dynamic programming, Intervals
- Solved
- No attempts yet
Problem
JOI and IOI are twin siblings. JOI has been absorbed in baking lately, so he baked a cake today too. The moment it came out of the oven, IOI smelled it and came over, so the two decided to share the cake.
The cake is round. Straight cuts from one point outward split the cake into pieces, and the pieces are numbered to counterclockwise. That is, for every with , piece touches piece and piece , where piece means piece and piece means piece . Piece has size , and because the cutting was clumsy, all are different.
The two split the pieces as follows.
- First, JOI takes any one of the pieces.
- After that, starting with IOI, IOI and JOI alternately take one of the remaining pieces each. A piece may be taken only if at least one of its two neighbors has already been taken by someone. When several pieces can be taken, IOI takes the largest of them, and JOI can take whichever piece he wants.
JOI wants the total size of the pieces he takes to be as large as possible.
Given the number of pieces and the size of each piece, write a program that finds the largest total size JOI can take.
Input
The first line contains the number of pieces .
Each of the next lines contains one integer. Line contains , the size of piece .
Output
Print the largest total size JOI can take on one line.
Constraints
- All are different.