Cutting the Cake 2

No attempts yetTime limit2sMemory limit512 MB

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 1iN1 \le i \le N, piece ii touches piece i1i-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

  • 1N20001 \le N \le 2000
  • 1Ai1091 \le A_i \le 10^9
  • All AiA_i are different.