#P15817. [JOI 2015 Final] ケーキの切り分け2
[JOI 2015 Final] ケーキの切り分け2
Problem Description
JOI-kun and IOI-chan are twin siblings. JOI-kun has recently become obsessed with making desserts. Today he baked a cake to eat by himself, but as soon as it came out of the oven, IOI-chan smelled it and came over, so the two decided to share the cake.
The cake is circular. Starting from some point, radial cuts are made to divide the cake into pieces, and these pieces are numbered to in counterclockwise order. That is, for , piece is adjacent to pieces and (where piece is considered to be piece , and piece is considered to be piece ). The size of piece is , but because the cutting skill is poor, all values are different from each other.
:::align{center}

Figure 1: Example cake ($N = 5, A_1 = 2, A_2 = 8, A_3 = 1, A_4 = 10, A_5 = 9$). :::
They decide to distribute the pieces of cake according to the following rules:
- First, JOI-kun chooses any one piece from the pieces and takes it.
- Then, starting with IOI-chan, IOI-chan and JOI-kun take turns taking one piece at a time from the remaining pieces. However, they can only take a piece such that at least one of its adjacent pieces has already been taken. When there are multiple pieces that can be taken, IOI-chan must choose the largest one among them, while JOI-kun may choose any one among them.
JOI-kun wants to maximize the sum of the sizes of all pieces he finally takes.
Task
Given the number of pieces and the sizes of the pieces, write a program to compute the maximum possible sum of the sizes of the pieces that JOI-kun can obtain.
Input Format
Read the following input from standard input.
- The first line contains an integer , meaning the cake is cut into pieces.
- In the next lines, line () contains an integer , meaning the size of piece is .
Output Format
Output one line to standard output containing an integer, meaning the maximum possible sum of the sizes of the pieces that JOI-kun can obtain.
5
2
8
1
10
9
18
8
1
10
4
5
6
2
9
3
26
15
182243672
10074562
977552215
122668426
685444213
3784162
463324752
560071245
134465220
21447865
654556327
183481051
20041805
405079805
564327789
3600242976
Hint
Sample Explanation 1
It is optimal for JOI-kun to take the cake in the following way:
- JOI-kun takes piece . Its size is .
- IOI-chan takes piece . Its size is .
- JOI-kun takes piece . Its size is .
- IOI-chan takes piece . Its size is .
- JOI-kun takes piece . Its size is .
In the end, the sum of the sizes of the pieces JOI-kun takes is .
Constraints
All input data satisfy the following conditions:
- .
- .
- All are distinct.
Subtasks
Subtask 1 [15 points]
- Satisfies .
Subtask 2 [45 points]
- Satisfies .
Subtask 3 [40 points]
There are no additional constraints.
Translated by DeepSeek V3.2.
Translated by ChatGPT 5