#include <bits/stdc++.h>
using namespace std;
#define ll long long

int n;
/*
ll dp[200005][2];
ll a[200005];
ll solve(int index,int taken,int c)
{
    if(index>=n && c!=n/2) return LLONG_MIN;
    if(index>=n && c==n/2) return  0;
    if(c==n/2) return 0;
    if(n%2==0 && c>=n/2+1) return LLONG_MIN;
    if(n%2!=0 && c>=(n+1)/2) return LLONG_MIN;
    ll &ret = dp[index][taken];
    if(~ret) return ret;
    ret = 0;
    if(taken==1)
    {
        ret = max(ret,solve(index+1,0,c));
    }
    else
    {
        ret = max(ret,solve(index+1,0,c));
        ret = max(ret,solve(index+1,1,c+1)+a[index]);
    }
    return ret;
}
*/
struct lol
{
    ll val;
    int pos;
    bool operator < (lol a)
    {
        return val > a.val;
    }
};
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin>>n;
    vector<lol> a(n);
    for(int i=0;i<n;++i)
    {
        ll x; cin>>x;
        a[i].val = x;
        a[i].pos = i;
    }
    sort(a.begin(),a.end());
    set<int> x;
    //for(auto l : a) cout<<l.val<<" ";
    ll ans = 0; int c = 0;
    for(int i=0;i<n;++i)
    {
        if(x.find(a[i].pos-1)!=x.end() || x.find(a[i].pos+1)!=x.end())
        {
            //cout<<a[i].val<<" ";
        }
        else
        {
            x.insert(a[i].pos);
            ans+=a[i].val;
            c++;
        }
        if(c==n/2) break;
    }
    cout<<ans;
}
