442_C. Artem and Array 
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
```python

MAXN = 5 * 10**5 + 100
a = []
ans = 0

n = int(input())

a = list( map ( int, input().split() ) )
a.append(0)

a = [0] + a
n = n + 2 
arr = []
arr.append( a[0] )
arr.append( a[1] ) 


i = 2
while i < n :
	ln = a[i] 
	l1 = arr[-1]
	l0 = arr[-2]
	while l1 <= l0 and l1 <= ln :
		ans = ans + min ( l0 , ln )
		arr.pop()
		l1 = arr[-1]
		l0 = arr[-2]
	arr.append(ln)
	i = i + 1
for i in range ( 1 , len(arr) - 1 ) :
	ans += min ( arr[i - 1] , arr[i + 1] ) 

print (ans)





```