///nhatnama4k68nq
///---------------------------------------------------------------------
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define N 1000005
#define NN 100005
#define MOD 1000000007
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define FORd(i,a,b) for (int i = (a) - 1; i >= (b); i--)
///---------------------------------------------------------------------
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pair<int,int>> vii;
///---------------------------------------------------------------------
const int maxint = 1e9+ 1608;
const long long maxll = 1e18+ 16082009;
///---------------------------------------------------------------------
/**
------------------------------------------------------------------------
NHAT NAM A4K68NQ
------------------------------------------------------------------------
*/
int n;
bool an = true;
ll res = 0;
deque<int> w;
void Read(){
cin >> n;
FOR (i,0,n){
int u; cin >> u;
w.pb(u);
}
while (!w.empty()){
if (an){
if (w.front() > w.back()){
int nho = w.back();
res += nho;
w.pop_back();
}
else{
int nho = w.front();
res += nho;
w.pop_front();
}
an = false;
}
else{
an = true;
if (w.front() > w.back()) w.pop_back();
else w.pop_front();
}
}
cout << res;
}
void Solve(){
}
int main(){
// #define NAME "main"
// freopen(NAME".INP", "r", stdin);
// freopen(NAME".OUT", "w",stdout);
int t = 1;
while (t--){
Read();
Solve();
}
return 0;
}
Ly8vbmhhdG5hbWE0azY4bnEKCi8vLy0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLQojaW5jbHVkZSA8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCiNkZWZpbmUgZW5kbCAiXG4iCiNkZWZpbmUgTiAxMDAwMDA1CiNkZWZpbmUgTk4gMTAwMDA1CiNkZWZpbmUgTU9EIDEwMDAwMDAwMDcKI2RlZmluZSBwYiBwdXNoX2JhY2sKI2RlZmluZSBwZiBwdXNoX2Zyb250CiNkZWZpbmUgZmkg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gPSAwOwpkZXF1ZTxpbnQ+IHc7CnZvaWQgUmVhZCgpewogICAgY2luID4+IG47CiAgICBGT1IgKGksMCxuKXsKICAgICAgICBpbnQgdTsgY2luID4+IHU7CiAgICAgICAgdy5wYih1KTsKICAgIH0KICAgIHdoaWxlICghdy5lbXB0eSgpKXsKICAgICAgICBpZiAoYW4pewogICAgICAgICAgICBpZiAody5mcm9udCgpID4gdy5iYWNrKCkpewogICAgICAgICAgICAgICAgaW50IG5obyA9IHcuYmFjaygpOwogICAgICAgICAgICAgICAgcmVzICs9IG5obzsKICAgICAgICAgICAgICAgIHcucG9wX2JhY2soKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlewogICAgICAgICAgICAgICAgaW50IG5obyA9IHcuZnJvbnQoKTsKICAgICAgICAgICAgICAgIHJlcyArPSBuaG87CiAgICAgICAgICAgICAgICB3LnBvcF9mcm9udCgpOwogICAgICAgICAgICB9CgogICAgICAgICAgICBhbiA9IGZhbHNlOwogICAgICAgIH0KICAgICAgICBlbHNlewogICAgICAgICAgICBhbiA9IHRydWU7CiAgICAgICAgaWYgKHcuZnJvbnQoKSA+IHcuYmFjaygpKSB3LnBvcF9iYWNrKCk7CiAgICAgICAgICAgIGVsc2Ugdy5wb3BfZnJvbnQoKTsKICAgICAgICB9CiAgICB9CiAgICBjb3V0IDw8IHJlczsKfQoKCnZvaWQgU29sdmUoKXsKCn0KCmludCBtYWluKCl7Ci8vICAgICNkZWZpbmUgTkFNRSAibWFpbiIKLy8gICAgZnJlb3BlbihOQU1FIi5JTlAiLCAiciIsIHN0ZGluKTsKLy8gICAgZnJlb3BlbihOQU1FIi5PVVQiLCAidyIsc3Rkb3V0KTsKICAgIGludCB0ID0gMTsKICAgIHdoaWxlICh0LS0pewogICAgICAgIFJlYWQoKTsKICAgICAgICBTb2x2ZSgpOwogICAgfQpyZXR1cm4gMDsKfQo=