#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=5010;
int n,a[maxn],dp[maxn][maxn][2],s[maxn];
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<=n;i++) dp[i][i][0]=a[i],s[i]=s[i-1]+a[i];
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
int j=i+k;
if(j>n) continue;
dp[i][j][0]=max(dp[i][j][0],(s[j]-s[i])-dp[i+1][j][0]+a[i]);
dp[i][j][0]=max(dp[i][j][0],(s[j-1]-s[i-1])-dp[i][j-1][0]+a[j]);
//dp[i][j][1]=max(dp[i][j][1],(s[j]-s[i])-dp[i+1][j][0]+a[i]);
//dp[i][j][1]=max(dp[i][j][1],(s[j-1]-s[i-1])-dp[i][j-1][0]+a[j]);
}
}
cout<<dp[1][n][0]<<endl;
return 0;
}
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=5010;
int n,a[maxn],dp[maxn][maxn][2],s[maxn];
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<=n;i++) dp[i][i][0]=a[i],s[i]=s[i-1]+a[i];
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
int j=i+k;
if(j>n) continue;
dp[i][j][0]=max(dp[i][j][0],(s[j]-s[i])-dp[i+1][j][0]+a[i]);
dp[i][j][0]=max(dp[i][j][0],(s[j-1]-s[i-1])-dp[i][j-1][0]+a[j]);
//dp[i][j][1]=max(dp[i][j][1],(s[j]-s[i])-dp[i+1][j][0]+a[i]);
//dp[i][j][1]=max(dp[i][j][1],(s[j-1]-s[i-1])-dp[i][j-1][0]+a[j]);
}
}
cout<<dp[1][n][0]<<endl;
return 0;
}