cswork / OAOpen practice workspace

AmazonOA #386

Maximize the Array Value

Estimated MediumJudge-ready

Topics (AI-labelled)GreedyBits & Math

Each operation deletes one element: if it is at an end, it is simply deleted; if it is in the middle, it is deleted and its left and right neighbors are merged into their sum. Exactly one element must remain at the end; maximize the final value.

Input

The first line contains n, and the second line contains n integers. The original n≤100000 and −10^9≤a[i]≤10^9 are kept; this site specifies n≥1, and the final result cannot be an empty array.

Output

Output a single integer answer.

Example 1

Input:

3
-5 -2 -7

Output:

-2

Example 2

Input:

3
1 -10 3

Output:

4

Example 3

Input:

3
0 -1 0

Output:

0

Example notes

The outputs of the three public examples are, in order: -2; 4; 0. Verified by independent enumeration or direct simulation.

Editorial

Loading solution…