Artem has an array of n positive integers. Artem decided to play with it. The game consists of n moves. Each move goes like this. Artem chooses some element of the array and removes it. For that, he gets min(a, b) points, where a and b are numbers that were adjacent with the removed number. If the number doesn't have an adjacent number to the left or right, Artem doesn't get any points. 

After the element is removed, the two parts of the array glue together resulting in the new array that Artem continues playing with. Borya wondered what maximum total number of points Artem can get as he plays this game.

Input

The first line contains a single integer n (1 ≤ n ≤ 5·105) — the number of elements in the array. The next line contains n integers ai (1 ≤ ai ≤ 106) — the values of the array elements.

Output

In a single line print a single integer — the maximum number of points Artem can get.

Examples

Input

5
3 1 5 2 6


Output

11


Input

5
1 2 3 4 5


Output

6


Input

5
1 100 101 100 1


Output

102
9
72 49 39 50 68 35 75 94 56
 4
2 3 1 2
 8
3 4 3 1 1 3 4 1
 1
4
 7
2 1 2 2 2 2 2
 6
1 7 3 1 6 2
 8
77 84 26 34 17 56 76 3
 2
93 51
 1
87
 4
86 21 58 60
 10
96 66 8 18 30 48 34 11 37 42
 6
46 30 38 9 65 23
 5
21 6 54 69 32
 3
1 2 1
 3
31 19 5
 10
4 2 2 4 1 2 2 4 2 1
 7
82 60 92 4 2 13 15
 9
4 5 2 2 3 1 3 3 5
 5
2 6 2 1 2
 2
3 1

435 4 15 0 10 12 279 0 0 118 299 145 74 1 5 21 129 23 6 0
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long int LL;
typedef pair<int,int> PII;
typedef pair<LL , LL> PLL;
typedef pair< LL , LL > PLL;
typedef vector< int > VI;
typedef vector< LL > VLL;
typedef vector< PII > VPII;
#define REP(i,j)       for(int i = 0 ; i < j ; i++)
#define REPNM(i,j,k)   for(int i = j ; i < k ; i++)
#define RREP(i,j,k)    for(int i = j ; i >=k ; i--)
#define LREP(i,j)      for(LL  i = 0 ; i < j ; i++)
#define LREPNM(i,j,k)  for(LL  i = j ; i < k ; i++)
#define LRREP(i,j,k)   for(LL  i = j ; i >=k ; i--)
#define pb   push_back
#define mp   make_pair
#define A    first
#define B    second
#define MEM(i,j)   memset(i,j,sizeof i)
#define ALL(i)     i.begin(),i.end()
#define DBGG(i,j)     cout << i << " " << j << endl
#define DB4(i,j,k,l)  cout << i << " " << j << " " << k << " " << l << endl
#define RI(i)       scanf("%d" , &i)
#define RII(i,j)    scanf("%d%d" , &i , &j)
#define RIII(i,j,k) scanf("%d%d%d" , &i , &j , &k)
#define RL(i)       scanf("%lld" , &i)
#define RLL(i,j)    scanf("%lld%lld" , &i , &j)
#define RLLL(i,j,k) scanf("%lld%lld%lld" , &i , &j , &k)
#define AS assert
#define IOS cin.tie() , cout.sync_with_stdio(0)
///------------------------------------------------------------
#define MAX 500900
#define INF 0x3f3f3f3f
#define N 50000000LL

LL n , x[MAX] , pr[MAX] , ba[MAX] , d[MAX];
__gnu_pbds::priority_queue<PLL>::point_iterator po[MAX];
__gnu_pbds::priority_queue<PLL> pq;
LL TR(LL now) { return now >= N ? now - N : now; }
int main(){
    RL(n);
    REPNM(i , 1 , n + 1) RL(x[i]);
    REPNM(i , 2 , n) pr[i] = i - 1 , ba[i] = i + 1;
    REPNM(i , 2 , n){
    	LL tmp = min(x[pr[i]] , x[ba[i]]);
    	d[i] = tmp;
    	// DBGG(i , tmp);
    	if(x[i] < tmp) po[i] = pq.push(mp(tmp + N , i));
    	else po[i] = pq.push(mp(tmp , i));
    }
    LL ans = 0;
    while(pq.size()){
    	PLL tmp = pq.top() ; pq.pop();
    	ans += TR(tmp.A);
    	// DB4(55 , tmp.A , tmp.B , "");
    	int now = tmp.B;
    	if(ba[now] != n){
    		d[ba[now]] = min(x[ba[ba[now]]] , x[pr[now]]);
    		pr[ba[now]] = pr[now];
    		if(x[ba[now]] < d[ba[now]]) pq.modify(po[ba[now]] , mp(d[ba[now]] + N , ba[now]));
    		else pq.modify(po[ba[now]] , mp(d[ba[now]] , ba[now]));
    	}
    	if(pr[now] != 1){
    		d[pr[now]] = min(x[pr[pr[now]]] , x[ba[now]]);
    		ba[pr[now]] = ba[now];
    		// DB4("modify = " , d[pr[now]] , pr[now] , "");
    		if(x[pr[now]] < d[pr[now]]) pq.modify(po[pr[now]] , mp(d[pr[now]] + N , pr[now]));
    		else pq.modify(po[pr[now]] , mp(d[pr[now]] , pr[now]));
    	}
    }
    printf("%lld\n", ans);
    return 0;
}