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·10^5) — the number of elements in the array. The next line contains n integers a_{i} (1 ≤ a_{i} ≤ 10^6) — 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

Difficulty: competition

Starter code:

Solutions:
n = input()

s = []

a = 0

for i in map(int, input().split()):

    while len(s) > 1 and min(s[-2], i)>=s[-1]:

        a += min(i, s[-2])

        del(s[-1])

    s.append(i)

s.sort()

print(a + sum(s[0: -2]))



# Made By Mostafa_Khaled


n = int(input())
cards = list(map(int,input().split()))

score = 0
preIndexes = []
nextIndexes = []
isChosen = [0]*n
chosens = []

#first card score initialize
nextIndexes.append(1)
preIndexes.append(-1)

for i in range(1, n-1):
    if cards[i] <= cards[i-1] and cards[i] <= cards[i+1]:
        chosens.append((-cards[i], i))
        isChosen[i] = 1
    preIndexes.append(i-1)
    nextIndexes.append(i+1)

#last card score initialize
preIndexes.append(n-2)
nextIndexes.append(n)

while len(chosens) != 0:
    chosen = chosens.pop()
    preChosen = preIndexes[chosen[1]]
    nextChosen = nextIndexes[chosen[1]]

    #handle score
    score += min(cards[preChosen], cards[nextChosen])

    #handle deleting
    nextIndexes[preChosen] = nextChosen
    preIndexes[nextChosen] = preChosen

    #check if preChosen can be chosen
    if preChosen != 0 and preIndexes[preChosen] != -1 and nextIndexes[preChosen] != n:
        if cards[preChosen] <= cards[preIndexes[preChosen]] and cards[preChosen] <= cards[nextIndexes[preChosen]]:
            if isChosen[preChosen] == 0:
                isChosen[preChosen] = 1
                chosens.append((-cards[preChosen], preChosen))
    #check if nextChosen can be chosen
    if nextChosen != n-1 and preIndexes[nextChosen] != -1 and nextIndexes[nextChosen] != n:
        if cards[nextChosen] <= cards[preIndexes[nextChosen]] and cards[nextChosen] <= cards[nextIndexes[nextChosen]]:
            if isChosen[nextChosen] == 0:
                isChosen[nextChosen] = 1
                chosens.append((-cards[nextChosen], nextChosen))

tempNode = nextIndexes[0]
while tempNode != n and tempNode != n-1:
    score += min(cards[preIndexes[tempNode]], cards[nextIndexes[tempNode]])
    tempNode = nextIndexes[tempNode]

print(score)



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)






input()
list=[int(x) for x in input().split()]
stack=[]
score=0
stack.append(list[0])
for i in range(1,len(list)):
    while(len(stack)>1 and stack[-1]<=min(list[i],stack[-2])):
        score=score+min(list[i],stack[-2])
        stack.pop()
    stack.append(list[i])
for i in range(1,len(stack)-1):
    score=score+min(stack[i-1],stack[i+1])
print(score)






def maxScore(list):
    score=0
    stack=[]
    stack.append(list[0])
    for i in range(1,len(list)):
        while(len(stack)>1 and stack[-1]<=min(list[i],stack[-2])):
            score=score+min(list[i],stack[-2])
            stack.pop()
        stack.append(list[i])
    for i in range(1,len(stack)-1):
        score=score+min(stack[i-1],stack[i+1])
    return score
input()
l=[int(x) for x in input().split()]
print(maxScore(l))