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.*;
import java.lang.reflect.Array;
import java.util.*;
import java.math.BigInteger;
import java.util.Collections.*;
import java.util.function.Consumer;

import static java.lang.Math.*;
import static java.lang.Math.min;
import static java.util.Arrays.*;
import static java.math.BigInteger.*;


public class Main{

    void run(){
        Locale.setDefault(Locale.US);
        boolean my;
        try {
            my = System.getProperty("MY_LOCAL") != null;
        } catch (Exception e) {
            my = false;
        }
        try{
            err = System.err;
            if( my ){
                sc  = new FastScanner(new BufferedReader(new FileReader("input.txt")));
//                sc  = new FastScanner(new BufferedReader(new FileReader("C:\\myTest.txt")));
                out = new PrintWriter   (new FileWriter("output.txt"));
            }
            else {
                sc = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
                out = new PrintWriter(new OutputStreamWriter(System.out));
            }
//            out = new PrintWriter(new OutputStreamWriter(System.out));
        }catch(Exception e){
            MLE();
        }
        if( my )
            tBeg = System.currentTimeMillis();
        solve();
        if( my )
            err.println( "TIME: " + (System.currentTimeMillis() - tBeg ) / 1e3 );
        exit(0);
    }

    void exit( int val ){
        err.flush();
        out.flush();
        System.exit(val);
    }

    double tBeg;
    FastScanner sc;
    PrintWriter out;
    PrintStream err;

    class FastScanner{

        StringTokenizer st;
        BufferedReader br;

        FastScanner( BufferedReader _br ){
            br = _br;
        }

        String readLine(){
            try {
                return br.readLine();
            } catch (IOException e) {
                return null;
            }
        }

        String next(){
            while( st==null || !st.hasMoreElements() )
                st = new StringTokenizer(readLine());
            return st.nextToken();
        }

        int nextInt(){ return Integer.parseInt(next()); }
        long nextLong(){ return Long.parseLong(next()); }
        double nextDouble(){ return Double.parseDouble(next()); }
    }

    void MLE(){
        int[][] arr = new int[1024*1024][]; for( int i = 0; i < 1024*1024; ++i ) arr[i] = new int[1024*1024];
    }

    void MLE( boolean doNotMLE ){ if( !doNotMLE ) MLE(); }

    void TLE(){
        for(;;);
    }

    public static void main(String[] args) {
        new Main().run();
//        new Thread( null, new Runnable() {
//            @Override
//            public void run() {
//                new Main().run();
//            }
//        }, "Lolka", 256_000_000L ).run();
    }

    ////////////////////////////////////////////////////////////////

    int n;
    int[] a, pr, nx;
    ArrayList<Integer> Q;
    boolean[] used;

    void solve(){
        n = sc.nextInt();
        a = new int[n];
        pr = new int[n];
        nx = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
            pr[i] = i - 1;
            nx[i] = i + 1;
        }

        Q = new ArrayList<>();
        used = new boolean[n];
        for (int i = 1; i <= n-2; i++) {
            try_add(i);
        }
        long ans = 0;

        while( !Q.isEmpty() ){
            int i = Q.get(Q.size()-1);
            Q.remove( Q.size() - 1 );
            nx[pr[i]] = nx[i];
            pr[nx[i]] = pr[i];
            ans += min( a[pr[i]], a[nx[i]] );
            try_add( pr[i] );
            try_add( nx[i] );
        }

        if( 3 <= n ){
            int i = nx[0];
            while( nx[i] != n ){
                ans += min( a[pr[i]], a[nx[i]] );
                i = nx[i];
            }
        }

        out.println( ans );
    }

    private void try_add(int i) {
        if( used[i] )return;

        if( 1<=i && i <=n-2 && a[pr[i]] >= a[i] && a[i] <= a[nx[i]] ){
            Q.add( i );
            used[i] = true;
        }
    }


}