#include<bits/stdc++.h>
#define wl 1500000
using namespace std;
int n;
int a[wl];
int fl[wl],fr[wl];
int l[wl],r[wl];
int ans = -wl;
int main()
{
fl[0] = -wl ,fr[0] = -wl;
scanf("%d",&n);
for(int i = 1; i <= n; i++) scanf("%d",&a[i]);
for(int i = 1; i <= n; i++) fl[i] = max(fl[i-1] + a[i] ,a[i]);
for(int i = 1; i <= n; i++) l[i] = max(l[i-1] ,fl[i]);
for(int i = n; i >= 1; i--) fr[i] = max(fr[i+1] + a[i],a[i]);
for(int i = n; i >= 1; i--) r[i] = max(r[i+1] ,fr[i]);
for(int i = 2; i < n; i++)
{
ans = max( ans , l[i-1] + r[i+1] );
}
printf("%d",ans);
}