#Lutece2969. A very easy problem of sequence

A very easy problem of sequence

Migrated from Lutece 2969 A very easy problem of sequence

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

You have been given an array aa consisting of nn positive integers, you need to construct an array bb with the same length, which satisfy the following conditions:

  1. for each ii: biaib_i\leq a_i;
  2. there exist a position kk that for each ii: bibi+1b_i\leq b_{i+1} when 1i<k1\leq i< k , and bibi+1b_i \ge b_{i+1} when ki<nk\le i < n .

Please output the maximum sum of the array bb.

Input

The first line contains an integer nn. The second line contains nn positive integers a1,a2,......,ana_1,a_2,......,a_n.

Output

One positive integer, the maximum sum of the array bb.

Samples

6
1 1 4 5 1 4
13

Constraints

1n5×1051\le n \le 5\times 10^5; 1ai1091\le a_i\le 10^9.