cswork / OAOpen practice workspace

Publicis SapientOA #1

Get Minimum Time

Estimated MediumJudge-ready

Topics (AI-labelled)GreedyPrefix & Ranges

Carry the boxes of each floor to the top floor with separate round trips from that floor; each box requires one round trip plus 1 minute of loading and unloading.

The standard input/output format and supplementary bounds of this site are as stated in this problem.

Input

The first line contains n (1≤n≤10^5); the second line contains n non-negative integers boxes[i] (≤10^9), where index i corresponds to floor i and the top floor is n.

Output

Output the minimum total number of minutes (the integer may exceed 32 bits).

Example 1

Input:

4
2 1 0 1

Output:

28

Example 2

Input:

1
0

Output:

0

Example 3

Input:

3
0 0 2

Output:

6

Editorial

Loading solution…