Under construction

Still being built. Things may change

Eagle

Time limit2sMemory limit512 MB

Summary
Each day pick one cell, route from an edge, empty every cell passed over, and lose one sheep per cell at night; maximize the sheep eaten.
Level

Medium4 of 10

Topics
Simulation, Greedy
Solved
No attempts yet

Problem

An eagle lives by eating sheep. The place where the sheep live can be represented as a 1×N1 \times N rectangle divided into 1×11 \times 1 cells. The cells are numbered 1,2,…,N1, 2, \dots, N from left to right. The number of sheep living in cell ii is AiA_i.

The eagle goes out to eat sheep every morning. It starts flying from the left of cell 11 or the right of cell NN and flies to the cell whose sheep it wants to eat. The eagle cannot fly outside the cells. If the sheep it wants to eat are in cell xx, it flies to cell xx and then eats all the sheep in cell xx. In one day the eagle can eat the sheep of only one cell.

Sheep are very afraid of the eagle, so they run away when they see it flying. A sheep runs away when it sees the eagle flying above its cell. When sheep run away, the number of sheep in that cell becomes 00. For example, if the eagle starts flying from the left of cell 11 and arrives at cell xx, then all the sheep that were in cells 11 through x−1x-1 run away and become 00. If it starts flying from the right of cell NN, then all the sheep in cells x+1x+1 through NN run away.

Also, because this is a dangerous place, every night the number of sheep in every cell decreases by 11.

How many sheep the eagle can eat depends on which cell's sheep it eats each day and from which side it starts flying.

Given the numbers of sheep, write a program to find the maximum number of sheep the eagle can eat.

Input

The first line gives the number of cells NN. (1≤N≤1,0001 \le N \le 1{,}000)

The second line gives the number of sheep in each cell, A1,A2,…,ANA_1, A_2, \dots, A_N. (0≤Ai≤100,0000 \le A_i \le 100{,}000)

Output

On the first line, print the maximum number of sheep the eagle can eat.

Examples5

  1. Example 1

    Input
    5
    1 10 4 10 1
    
    Expected output
    21
    
  2. Example 2

    Input
    5
    1 1 1 1 1
    
    Expected output
    1
    
  3. Example 3

    Input
    6
    10 1 1 1 1 10
    
    Expected output
    19
    
  4. Example 4

    Input
    7
    1 2 3 4 5 6 7
    
    Expected output
    16
    
  5. Example 5

    Input
    14
    10 1 10 2 10 3 10 4 10 3 10 2 10 1
    
    Expected output
    49