#include <bits/stdc++.h>
#define union Union
#define rank rank228
#define tm tm228
#define hash hash228
#define matr vector < vector < int > >
#define int long long
#define pii pair < int, int >
#define papa pair < node*, node* >
#define all(v) v.begin(), v.end()
#define endl "\n"
#define pb push_back
#define PI 3.14159265358979323846
#define ff first
#define ss second
#define mp make_pair

using namespace std;

const int inf = 1e18, maxn = 1e6 + 100, mod1 = 1e9 + 7, mod2 = 998244353;

int mood1(int x)
{
    x %= mod1;
    if (x < 0)
        x += mod1;
    return x;
}

int mood2(int x)
{
    x %= mod2;
    if (x < 0)
        x += mod2;
    return x;
}

int a[maxn];

void solve(){
    int ans = 0, now = 0, pref = 0, mpref = 0;
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i)
    {
        cin >> a[i];
        now += mpref;
        ans = max(ans, now);
        pref += a[i];
        now += pref - mpref;
        mpref = max(pref, mpref);
        ans = max(ans, now);
    }
    cout << ans;
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    srand(100);
    cout << fixed << setprecision(10);
    int t = 1;
    #ifdef LOCAL
        freopen("task.in", "r", stdin);
    #endif
    //cin >> t;
    while (t--)
    {
        solve();
        cout << endl;
    }
}

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

#define asll unsigned long long
#define ll long long
#define ld long double
#define imt int
#define pii pair<int,int>
#define st first
#define nd second
#define psbk(x) push_back(x);
#define For(x,a,b) for(int x=(a);x!=(b+((b-a)/abs(b-a)));x+=(b-a)/abs(b-a))
#define FOR(x,n) for(int x=0;x<(n);x++)
#define m_p make_pair

#define kj <<" "<<
#define kjb <<" ";
#define dg <<","<<
#define ml <<endl;
#define co cout<<
#define ter cout<<endl;
#define sibi cout<<"TES"<<endl;
#define tes(x) cout<<"TES"<<x<<endl;
#define kpr(x) cout<<"("<<x.st dg x.nd <<")" ml
#define kkor(p) cout<<"("<<p.x dg p.y <<")" ml
#define kera(x,m) for( int i = 0;i < m ; i++ ){ cout<<"x["<< i <<"] =" kj x[i] ml}

asll M=1e9+7;
int n,m,k,tc;
ll p,q,r;

ll pms[200005];
ll ps[200005];

int main()
{
	
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);cout.tie(0);
	//freopen("stdin.in", "r", stdin)
 	//freopen("stdout.out", "w", stdout)
	srand(time(0));
	cin>>n;
	pms[0]=0;
	ps[0]=0;
	FOR(i,200005){
		ps[i]=0;
		pms[i]=0;
	}
	For(i,1,n){
		cin>>p;
		ps[i]=ps[i-1]+p;
		pms[i]=max(pms[i-1],ps[i]);
		//co ps[i] kj pms[i] ml
	}

	ll js1=0,js2=0,js3=0;
	//js1=psum
	//js2=psum sebelum + max ini
	//js3 maks akhir
	For(i,1,n){
		js2=js1+pms[i];
		js3=max(js3,js2);
		js1+=ps[i];

	}
	co js3 ml;

}

