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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class ArtemAndArray {
    public static void main(String[] args) throws IOException {
        input.init(System.in);
        int n = input.nextInt();
        list l = new list(new node(input.nextInt()));
        for (int i = 0; i < n - 1; i++) {
            l.add(input.nextInt());
        }
        int res = 0;
        while (l.size != 2) {
            res += l.remove();
        }
        System.out.println(res);
    }

    public static class node {
        int value;
        int min;
        node next;
        node prev;
        node minNode;

        public node(int val) {
            this.value = val;
            this.min = -1;
        }

        public String toString() {
            return "(" + value + ")-->";
        }
    }

    public static class list {
        node first;
        node maxNode;
        node temp;
        int size;

        public list(node f) {
            this.first = f;
            this.temp = this.first;
            this.maxNode = new node(Integer.MAX_VALUE);
            this.maxNode.min = -10;

            size++;
        }

        public void add(int n) {
            node nextNode = new node(n);
            temp.next = nextNode;
            if ((temp.prev != null)) {
                if (temp.prev.value < temp.next.value) {
                    temp.minNode = temp.prev;
                    temp.min = temp.prev.value;

                } else if (temp.prev.value >= temp.next.value) {
                    temp.minNode = temp.next;
                    temp.min = temp.next.value;
                }
                if ((temp.min > maxNode.min)) {
                    maxNode = temp;
                } else if ((temp.min == maxNode.min)
                        && (temp.value < maxNode.value)) {
                    maxNode = temp;
                }
            } else {
                temp.minNode = null;
                temp.min = -1;
            }
            nextNode.prev = temp;
            temp = nextNode;
            size++;

        }

        public String toString() {
            String res = "";
            node temp = this.first;
            for (int i = 0; i < size; i++) {
                res += temp.min;
                if (i + 1 == size) {
                    res += "(" + temp.value + ")";
                } else {
                    res += temp.toString();
                }
                temp = temp.next;
            }
            return res;
        }

        public int remove() {
            if (size == 3){
                size--;
                return maxNode.min;
            }
            node temp = maxNode;
            int res = maxNode.min;
            if (temp.next.next != null) {
                temp.next.min = Math.min(temp.prev.value, temp.next.next.value);
            }
            if (temp.prev.prev != null) {
                temp.prev.min = Math.min(temp.next.value, temp.prev.prev.value);
            }
            temp.next.prev = temp.prev;
            temp.prev.next = temp.next;
            if (temp.next.min > temp.prev.min) {
                maxNode = temp.next;
            } else if (temp.next.min < temp.prev.min) {
                maxNode = temp.prev;
            } else if (temp.next.min == temp.prev.min) {
                if (temp.next.value < temp.prev.value) {
                    maxNode = temp.next;
                } else {
                    maxNode = temp.prev;
                }
            }
            size--;
            node temp2 = first.next;
            while (temp2 != null) {
                if (temp2.min > maxNode.min) {
                    maxNode = temp2;
                } else if (temp2.min == maxNode.min) {
                    if (temp2.value < maxNode.value) {
                        maxNode = temp2;
                    }
                }
                temp2 = temp2.next;
            }
            return res;

        }
    }

    // fast input
    public static class input {
        static BufferedReader reader;
        static StringTokenizer tokenizer;

        /** call this method to initialize reader for InputStream */
        static void init(InputStream input) {
            reader = new BufferedReader(new InputStreamReader(input));
            tokenizer = new StringTokenizer("");
        }

        /** get next word */
        static String next() throws IOException {
            while (!tokenizer.hasMoreTokens()) {
                // TODO add check for eof if necessary
                tokenizer = new StringTokenizer(reader.readLine());
            }
            return tokenizer.nextToken();
        }

        static int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        static double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        static long nextLong() throws IOException {
            return Long.parseLong(next());
        }
    }
}
