#55048: 火星文


bradyou0726 (加州多米多羅)


#include<bits/stdc++.h>
using namespace std;
const int MaxN=1e6;
const int MaxA=1e9;

int N;
int A[MaxN];
int ord[MaxN];
set<int> pos;
set<int> :: iterator it;
int main(){
    cin>>N;
    for(int n=0;n<N;n++){
        cin>>A[n];
        ord[n]=n;
        pos.insert(n);
    }
    sort(ord,ord+N,[](int lhs, int rhs){
        return A[lhs]<A[rhs];
    });
    long ans=0;
    for(int n=0;n<N-1;n++){
        int now=ord[n];
        pos.erase(now);
        it=pos.upper_bound(now);
        int nxt=it==pos.end()?-1: *it;
        int pre=it==pos.begin()? -1:*prev(it);
        if(pre==-1){
            ans+=A[nxt];
        }
        else if(nxt==-1){
            ans+=A[pre];
        }
        else{
            ans+=min(A[pre],A[nxt]);
        }
    }
    cout<<ans;
}