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
import java.io.IOException;
import java.util.Arrays;
import java.io.FilterInputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedInputStream;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Zyflair Griffane
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		JoltyScanner in = new JoltyScanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		ArtemAndArray solver = new ArtemAndArray();
		solver.solve(1, in, out);
		out.close();
	}
}

class ArtemAndArray {
    public void solve(int testNumber, JoltyScanner in, PrintWriter out) {
        int n = in.nextInt();
        Node[] arr = new Node[n + 1];
        arr[0] = new Node(0);

        for (int i = 1; i <= n; i++) {
            arr[i] = new Node(in.nextInt());
            arr[i - 1].append(arr[i]);
        }
        arr[n].append(new Node(0));

        long result = 0;
        Arrays.sort(arr);
        for (int i = 1; i < n - 1; i++) {
            int toAdd = Math.min(arr[i].prev.value, arr[i].next.value);
            result += toAdd == 0 ? arr[i].value : toAdd;
            arr[i].delete();
        }
        out.println(result);
    }
    public class Node implements Comparable<Node> {
        int value;
        Node next;
        Node prev;
        public Node(int value) {
            this.value = value;
        }
        public void append(Node o) {
            this.next = o;
            o.prev = this;
        }
        public void delete() {
            this.next.prev = this.prev;
            this.prev.next = this.next;
        }
        public int compareTo(Node o) {
            return value - o.value;
        }
    }
}

class JoltyScanner {
    public static final int BUFFER_SIZE = 1 << 16;
    public static final char NULL_CHAR = (char) -1;

    byte[] buffer = new byte[BUFFER_SIZE];
    boolean EOF_FLAG = false;
    int bufferIdx = 0, size = 0;
    char c = (char) -1;
    BufferedInputStream in = new BufferedInputStream(System.in, BUFFER_SIZE);
    public JoltyScanner(InputStream in) {
        this.in = new BufferedInputStream(in);
    }
    public int nextInt() {
        long x = nextLong();
        if (x > Integer.MAX_VALUE || x < Integer.MIN_VALUE) {
            throw new ArithmeticException("Scanned value overflows integer");
        }
        return (int) x;
    }
    public long nextLong() {
        boolean negative = false;
        if (c == NULL_CHAR) {
            c = nextChar();
        }
        while (!EOF_FLAG && (c < '0' || c > '9')) {
            if (c == '-') {
                negative = true;
            }
            c = nextChar();
        }
        if (EOF_FLAG) {
            throw new EndOfFileException();
        }
        long res = 0;
        while (c >= '0' && c <= '9') {
            res = (res << 3) + (res << 1) + c - '0';
            c = nextChar();
        }
        return negative ? -res : res;
    }

    public char nextChar() {
        if (EOF_FLAG) {
            return NULL_CHAR;
        }
        while (bufferIdx == size) {
            try {
                size = in.read(buffer);
                if (size == -1) {
                    throw new Exception();
                }
            }
            catch (Exception e) {
                EOF_FLAG = true;
                return (char) -1;
            }
            if (size == -1) {
                size = BUFFER_SIZE;
            }
            bufferIdx = 0;
        }
        return (char) buffer[bufferIdx++];
    }

    public class EndOfFileException extends RuntimeException {}
}

