#include <bits/stdc++.h>
using namespace std;
//make_tuple emplace_back next_permutation push_back make_pair second first setprecision

#if MYDEBUG
#include "lib/cp_debug.h"
#else
#define DBG(...) ;
#endif

#define int long long 

using LL = long long;
constexpr LL LINF=334ll<<53;
constexpr int INF=15<<26;
constexpr LL  MOD=1E9+7;
int counter;
template<typename Val, typename Key, typename Comp>
struct HeapItem;

class Phash{
    public:
    size_t operator () ( const pair<LL,int> &p ) const{ return hash<LL>()(p.first)^hash<int>()(p.second);};
};
template<typename V, typename K=int, typename compare = less<K>>
class HollowHeap{
public:
    struct Node{
        K key;
        HeapItem<V,K,compare> *item;
        int rank;
        Node *child,*next,*second_parent;
        HollowHeap *hh;//root to HH
        //bool is_hollow = second_parent==null?T:F
        //bool is_root;
        Node(int k, HeapItem<V,K,compare> *item_):
            key(k),
            item(item_),
            rank(0),
            child(nullptr),
            next(nullptr),
            second_parent(nullptr),
            hh(nullptr){};

    };

    struct DisjointSet{//Union-Find
        int size;
        DisjointSet *par;
        HeapItem<V,K,compare> *item;
        HollowHeap<V,K,compare> *heap;
        DisjointSet():size(1),par(nullptr),item(nullptr),heap(nullptr){};
        DisjointSet(HeapItem<V,K,compare> *it):size(1),par(nullptr),item(it),heap(nullptr){};
        DisjointSet* UFroot(){
            return par==nullptr?this:par=par->UFroot();
        }
        void unite(DisjointSet *ds){
            auto root_this = UFroot(); //DisjointSet*
            auto root_ds = ds->UFroot();
            if(root_this==root_ds) return;
            if(root_this->size < root_ds->size)swap(root_this,root_ds);
            root_ds->par = root_this;
            root_this->size += root_ds->size;
        }
    };

    HollowHeap():count_item(0),count_node(0),root(nullptr),rankmap(3){};
    bool empty(){return root==nullptr;}
    int size(){return count_item;}

    HeapItem<V,K,compare>* emplace(K k,V v){
        auto pr = new HeapItem<V,K,compare>(v);
        emplace(k,*pr);
        return pr;
    }
    HeapItem<V,K,compare>* emplace(K k,HeapItem<V,K,compare> &e){
        ++count_item; ++count_node;
        e.node = new Node(k,&e);
        e.uf = new DisjointSet(&e);
        root = meld_(e.node);
        return &e;
    }
    void add_child(Node *v,Node *w){//make v child of w
        v->next=w->child;
        w->child=v;
    }
    Node* link(Node *v,Node *w){
        if(compare()(v->key,w->key)){
            add_child(w,v);
            return v;
        }else{
            add_child(v,w);
            return w;
        }
    }
    Node* meld_(Node *u){
        if(u == nullptr) {root->item->uf->UFroot()->heap = this;return root;}
        if(root == nullptr) {u->item->uf->UFroot()->heap = this;return u;}
        root->item->uf->unite(u->item->uf);
        root->item->uf->UFroot()->heap = this;
        return link(root,u);
    }
    Node* meld(HollowHeap &g){
        count_item+=g.count_item;
        count_node+=g.count_node;
        g.count_item=g.count_node=0;
        root = meld_(g.root);
        g.root = nullptr;
        return root;
    }
    K top(){//do not use when the heap is empty
        return root->key;
    }
    pair<K, HeapItem<V,K,compare>*> top_item(){
        return make_pair(root->key,root->item);
    }
    Node* pop(){
        return delete_node(root);
    }
    static Node* delete_node(){}//to do
    Node* delete_node(Node *u){
        count_item--;count_node--;
        u->item->node=nullptr;
        u->item = nullptr;
        if(root->item!=nullptr) return root; //if u!=root, deletion is completed
        int max_rank = 0;
        while(root!=nullptr){
            auto w = root->child;
            auto x = root;
            root = root->next; //root : listing all hollow roots
            while(w!=nullptr){
                counter ++;
                auto u = w;
                w = w->next;
                if(u->item == nullptr){//if the child node is hollow
                    if(u->second_parent== nullptr){ //u became hollow by delete op
                        //insert u to list of hollow nodes
                        u->next = root;
                        root = u;
                    }else{//u became hollow by decrease-key
                        if(u->second_parent== x) w =nullptr;//unnecessary?
                        else u->next = nullptr; //when x is deleted, u is the last child of u->second_parent
                        u->second_parent= nullptr; // u no longer have two parents
                    }
                }else{//ranked link
                    u->next = nullptr;//?
                    while(rankmap[u->rank]!=nullptr){
                        u = link(u,rankmap[u->rank]);
                        rankmap[u->rank] = nullptr;
                        ++u->rank;
                    }
                    rankmap[u->rank] = u;
                    max_rank=max(max_rank,u->rank);
                    if(max_rank>=(int)rankmap.size()-2)rankmap.resize((int)rankmap.size()*2);
                }

            }
            --count_node;
            if(x!= nullptr)delete x;//
        }
        //unranked link
        for(int i=0; i<=max_rank; ++i){
            if(rankmap[i]!=nullptr){
                if(root==nullptr)root = rankmap[i];
                else root = link(root,rankmap[i]);
                rankmap[i]=nullptr;
            }
        }
        return root;
    }
    static Node* decrease_key(){}//to do
    Node* decrease_key(HeapItem<V,K,compare> &i, K k){
        return decrease_key(i.node,k);
    }
    Node* decrease_key(Node *u, K k){
        if (u == root){
            u->key = k;
            return u;
        }
        Node *v = new Node(k,move(u->item));
        count_node++;
        u -> item = nullptr;
        v->rank = max(0,u->rank-2);

        v->child = u;
        u->second_parent= v;
        return root = link(v,root);
    }
    //swap
    void swap_hh(HollowHeap &a){
        swap(count_item,a.count_item);
        swap(count_node,a.count_node);
        swap(root,a.root);
    }
//private:
    //static multimap<V*,Node*> table;
    static unordered_multimap<pair<K,int>,HeapItem<V,K,compare>*,Phash> table; //hash function needed
    int count_item,count_node;
    Node *root;
    vector<Node*> rankmap;
};
template<typename V, typename K, typename compare>
unordered_multimap<pair<K,int>,HeapItem<V,K,compare>*,Phash> HollowHeap<V,K,compare>::table;


template<typename Val, typename Key, typename Comp>
struct HeapItem{
    Val val;
    typename HollowHeap<Val,Key,Comp>::Node *node;
    typename HollowHeap<Val,Key,Comp>::DisjointSet *uf;
    HeapItem(Val v):val(v),node(nullptr){};
    HeapItem():val(),node(nullptr){};
};

struct UnionFind{
    vector<int> par; //par: -1*elements(root) number of par(not root)
    UnionFind(int n){par = vector<int>(n,-1);}
    int root(int x){return par[x]<0?x:(par[x]=root(par[x]));}
    void unite(int x,int y){
        x=root(x);
        y=root(y);
        if(x!=y){
            if(par[x]<par[y]) swap(x,y);
            par[y] += par[x];
            par[x] = y;
        }
    }
    bool is_root(int x){return root(x)==x;}
    bool same(int x,int y){return root(x) == root(y);}
    int size(int x){return -par[root(x)];}
};
struct Problem{
    int n;
    UnionFind uf;
    vector<LL> w;
    priority_queue<pair<LL,int>,vector<pair<LL,int>>,greater<pair<LL,int>>> pq;
    Problem(LL n):n(n),uf(n-1){};
    void solve(){
        vector<HollowHeap<int,LL>> hh(n-1);

        for(int i=0; i<n; ++i){
            LL tmp;
            scanf("%lld",&tmp);
            w.push_back(tmp);
        }
        for(int i=0; i<n-1; ++i){
            HollowHeap<int,LL>::table.insert({{w[i],i-n},hh[i].emplace(w[i],i-n)});
            HollowHeap<int,LL>::table.insert({{w[i+1],(i+1)-n},hh[i].emplace(w[i+1],i+1-n)});
            pq.push({w[i]+w[i+1],i});
        }
        LL ans = 0,cnt=n;
        for(;cnt>1;){
            auto t =pq.top();
            pq.pop();
            auto heapid = t.second;
            if(!uf.is_root(heapid))continue;
            auto x = hh[heapid].top_item();
            hh[heapid].pop();
            auto y = hh[heapid].top_item();
            hh[heapid].pop();
            if(x.first+y.first!=t.first){
                hh[heapid].emplace(x.first,*x.second);
                hh[heapid].emplace(y.first,*y.second);
                continue;
            }
            ans += t.first;
            cnt--;
            if(cnt==1)break;
            int nxt_heapid = heapid;
            if(-n<x.second->val and x.second->val<-1 ){
                int xx = x.second->val + n;
                hh[uf.root(xx-1)].meld(hh[uf.root(xx)]);
                int tmp = uf.root(xx-1);
                uf.unite(xx-1, xx);
                if(!uf.is_root(tmp))hh[tmp].swap_hh(hh[uf.root(xx-1)]);
                nxt_heapid=uf.root(xx-1);
                auto range = HollowHeap<int,LL>::table.equal_range({x.first,xx-n});
                if(range.first->second->node!=nullptr)hh[nxt_heapid].delete_node(range.first->second->node);
                else range.first++,hh[nxt_heapid].delete_node(range.first->second->node);
            }
            if(-n<y.second->val and y.second->val<-1 ){
                int yy = y.second->val + n;
                hh[uf.root(yy-1)].meld(hh[uf.root(yy)]);
                int tmp = uf.root(yy-1);
                uf.unite(yy-1, yy);
                if(!uf.is_root(tmp))hh[tmp].swap_hh(hh[uf.root(yy-1)]);
                nxt_heapid=uf.root(yy-1);
                auto range = HollowHeap<int,LL>::table.equal_range({y.first,yy-n});
                if(range.first->second->node!=nullptr)hh[nxt_heapid].delete_node(range.first->second->node);
                else range.first++,hh[nxt_heapid].delete_node(range.first->second->node);
            }
            hh[nxt_heapid].emplace(t.first,nxt_heapid);
            auto p = hh[nxt_heapid].top_item();
            hh[nxt_heapid].pop();
            auto q = hh[nxt_heapid].top_item();
            hh[nxt_heapid].emplace(p.first,*p.second);
            pq.push({p.first+q.first,nxt_heapid});
            //hh[nxt_heapid].emplace(p.first+q.first,-1);
        }
        cout << ans << "\n";
    }
};

signed main(){
    //cin.tie(0);
    //ios_base::sync_with_stdio(false);
    long long n=0;
    cin >> n;
    Problem p(n);
    p.solve();
    return 0;
}



Measured memory_bytes: 512