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
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;

/*
br = new BufferedReader(new FileReader("input.txt"));
pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
 */

public class C {
	private static BufferedReader br;
	private static StringTokenizer st;
	private static PrintWriter pw;
	public static void main(String[] args) throws IOException	{
		br = new BufferedReader(new InputStreamReader(System.in));
		pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		//int qq = 1;
		int qq = Integer.MAX_VALUE;
		//int qq = readInt();
		for(int casenum = 1; casenum <= qq; casenum++)	{
			int n = readInt();
			int[] list = new int[n];
			for(int i = 0; i < n; i++) {
				list[i] = readInt();
			}
			PriorityQueue<State> q = new PriorityQueue<State>();
			for(int i = 1; i < n-1; i++) {
				q.add(new State(i-1,i,i+1,Math.min(list[i-1],list[i+1])));
			}
			TreeSet<Integer> exists = new TreeSet<Integer>();
			boolean[] used = new boolean[n];
			for(int i = 0; i < n; i++) {
				exists.add(i);
			}
			long ret = 0;
			while(!q.isEmpty()) {
				State curr = q.poll();
				if(used[curr.l] || used[curr.m] || used[curr.r]) {
					continue;
				}
				if(exists.lower(curr.m) == curr.l && exists.higher(curr.m) == curr.r) {
					ret += curr.val;
					exists.remove(curr.m);
					used[curr.m] = true;
					if(exists.first() != curr.l) {
						int lower = exists.lower(curr.l);
						q.add(new State(lower, curr.l, curr.r, Math.min(list[lower], list[curr.r])));
					}
					if(exists.last() != curr.r) {
						int higher = exists.higher(curr.r);
						q.add(new State(curr.l, curr.r, higher, Math.min(list[higher], list[curr.l])));
					}
				}
			}
			pw.println(ret);
		}
		pw.close();
	}
	
	static class State implements Comparable<State> {
		public int l,m,r,val;
		public State(int l, int m, int r, int val) {
			super();
			this.l = l;
			this.m = m;
			this.r = r;
			this.val = val;
		}
		public int compareTo(State s) {
			return s.val - val;
		}
		@Override
		public String toString() {
			return "State [l=" + l + ", m=" + m + ", r=" + r + ", val=" + val
					+ "]";
		}
	}
	
	private static void exitImmediately()	{
		pw.close();
		System.exit(0);
	}

	private static long readLong() throws IOException	{
		return Long.parseLong(nextToken());
	}

	private static double readDouble() throws IOException	{
		return Double.parseDouble(nextToken());
	}

	private static int readInt() throws IOException	{
		return Integer.parseInt(nextToken());
	}

	private static String nextToken() throws IOException	{
		while(st == null || !st.hasMoreTokens())	{
			if(!br.ready())	{
				exitImmediately();
			}
			st = new StringTokenizer(br.readLine().trim());
		}
		return st.nextToken();
	}
}
