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.math.BigDecimal;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class ArtemArray {


    static public class IndexMaxPQ<Key extends Comparable<Key>> {
        private int n;           // number of elements on PQ
        private int[] pq;        // binary heap using 1-based indexing
        private int[] qp;        // inverse of pq - qp[pq[i]] = pq[qp[i]] = i
        private Key[] keys;      // keys[i] = priority of i

        public IndexMaxPQ(int maxN) {
            if (maxN < 0) throw new IllegalArgumentException();
            n = 0;
            keys = (Key[]) new Comparable[maxN + 1];    // make this of length maxN??
            pq = new int[maxN + 1];
            qp = new int[maxN + 1];                   // make this of length maxN??
            for (int i = 0; i <= maxN; i++)
                qp[i] = -1;
        }

        public boolean isEmpty() {
            return n == 0;
        }

        public boolean contains(int i) {
            return qp[i] != -1;
        }

        public int size() {
            return n;
        }

        public void insert(int i, Key key) {
            n++;
            qp[i] = n;
            pq[n] = i;
            keys[i] = key;
            swim(n);
        }

        public int maxIndex() {
            if (n == 0) throw new NoSuchElementException("Priority queue underflow");
            return pq[1];
        }

        public Key maxKey() {
            if (n == 0) throw new NoSuchElementException("Priority queue underflow");
            return keys[pq[1]];
        }

        public Key delMax() {
            if (n == 0) throw new NoSuchElementException("Priority queue underflow");
            int min = pq[1];
            exch(1, n--);
            sink(1);

            assert pq[n + 1] == min;
            qp[min] = -1;        // delete
            Key toReturn = keys[min];    // to help with garbage collection
            keys[min] = null;
            pq[n + 1] = -1;        // not needed
            return toReturn;
        }

        public Key keyOf(int i) {
            return keys[i];
        }

        public void changeKey(int i, Key key) {
            keys[i] = key;
            swim(qp[i]);
            sink(qp[i]);
        }

        public void delete(int i) {
            int index = qp[i];
            exch(index, n--);
            swim(index);
            sink(index);
            keys[i] = null;
            qp[i] = -1;
        }

        private boolean less(int i, int j) {
            return keys[pq[i]].compareTo(keys[pq[j]]) < 0;
        }

        private void exch(int i, int j) {
            int swap = pq[i];
            pq[i] = pq[j];
            pq[j] = swap;
            qp[pq[i]] = i;
            qp[pq[j]] = j;
        }

        private void swim(int k) {
            while (k > 1 && less(k / 2, k)) {
                exch(k, k / 2);
                k = k / 2;
            }
        }

        private void sink(int k) {
            while (2 * k <= n) {
                int j = 2 * k;
                if (j < n && less(j, j + 1)) j++;
                if (!less(k, j)) break;
                exch(k, j);
                k = j;
            }
        }
    }

    public static class Element implements Comparable<Element> {
        int left;
        int right;
        Long min;

        public Element(int left, int right, long min) {
            this.left = left;
            this.right = right;
            this.min = min;
        }

        @Override
        public int compareTo(Element o) {
            return min.compareTo(o.min);
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        long arr[] = new long[n];
        IndexMaxPQ<Element> pq = new IndexMaxPQ<>(n);

        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextLong();
        }

        for (int i = 1; i < n - 1; i++) {
            pq.insert(i, new Element(i - 1, i + 1, Math.min(arr[i - 1], arr[i + 1])));
        }

        BigDecimal ans = new BigDecimal(0);
        while (!pq.isEmpty()) {
            Element element = pq.delMax();
            ans = ans.add(new BigDecimal(element.min));

            int left = element.left;
            int right = element.right;

            if (right != n - 1) {
                Element toChange = pq.keyOf(right);
                toChange.left = left;
                toChange.min = Math.min(arr[toChange.left], arr[toChange.right]);
            }

            if (left != 0) {
                Element toChange = pq.keyOf(left);
                toChange.right = right;
                toChange.min = Math.min(arr[toChange.left], arr[toChange.right]);
            }
        }
        System.out.println(ans);
    }
}
