This page is still under construction.

Parts of this page are still being built. What you see may change.

Cutting the Cake 2

Time limit2sMemory limit512 MB

Summary
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 NN pieces, and the pieces are numbered 11 to NN counterclockwise. That is, for every ii with 1≤i≤N1 \le i \le N, piece ii touches piece i−1i-1 and piece i+1i+1, where piece 00 means piece NN and piece N+1N+1 means piece 11. Piece ii has size AiA_i, and because the cutting was clumsy, all AiA_i are different.

The two split the pieces as follows.

  1. First, JOI takes any one of the NN pieces.
  2. 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 NN 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 NN.

Each of the next NN lines contains one integer. Line ii contains AiA_i, the size of piece ii.

Output

Print the largest total size JOI can take on one line.

Constraints

  • 1≤N≤20001 \le N \le 2000
  • 1≤Ai≤1091 \le A_i \le 10^9
  • All AiA_i are different.

Examples4

  1. Example 1

    Input
    5
    2
    8
    1
    10
    9
    
    Expected output
    18
  2. Example 2

    Input
    8
    1
    10
    4
    5
    6
    2
    9
    3
    
    Expected output
    26
  3. Example 3

    Input
    15
    182243672
    10074562
    977552215
    122668426
    685444213
    3784162
    463324752
    560071245
    134465220
    21447865
    654556327
    183481051
    20041805
    405079805
    564327789
    
    Expected output
    3600242976
  4. Example 4

    Input
    1
    1000000000
    
    Expected output
    1000000000