#Lutece1571. Deal W1th The Number

Deal W1th The Number

Migrated from Lutece 1571 Deal W1th The Number

All parts of this problem, including description, images, samples, data and checker, might be broken. If you find bugs in this problem, please contact the admins.

Description

There is a sequence which contains NN numbers x1,x2,,xNx_1, x_2, \dots, x_N.

Every time you can choose a number xix_i, delete it from the sequence and you will gain min{xi1,xi+1}\min\{x_{i - 1}, x_{i + 1}\} scores. If xix_i is the first or last number in the sequence, you will gain 00 scores.

After that, merge the left part and the right part of the rest of the numbers, they will form a new sequence with new indexes, and you can do the same operation on it.

What's the maximum sum scores you can gain at last?

Input

The first line contains an integer NN.

The second line contains NN integers x1,x2,,xNx_1, x_2, \dots, x_N.

1N1051 \leq N \leq 10^5, 0xi1090 \leq x_i \leq 10^9.

Output

Print the maximum sum scores you can gain.

Samples

4
1 4 3 2
3

Note

In the sample, scores 33 will be gained if number 33 is deleted first and number 44 is deleted then; scores 22 will be gained if number 44 is deleted first and number 33 is deleted then. So the answer is 33.

Resources

The 15th UESTC Programming Contest Final