#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define fi first
#define se second
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep0(i, n) for (ll i = 0; i <= (n); ++i)
#define rep1(i, n) for (ll i = 1; i <= (n); ++i)
#define rrep1(i, n) for (ll i = (n); i > 0; --i)
#define rrep0(i, n) for (ll i = (n); i >= 0; --i)
#define srep(i, s, t) for (ll i = s; i < t; ++i)
#define srep1(i, s, t) for (ll i = s; i <= t; ++i)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)), x.end())
#define snuke srand((unsigned)clock() + (unsigned)time(NULL));
#define show(x) cerr << #x << " = " << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define bn(x) ((1 << x) - 1)
#define dup(x, y) (((x) + (y)-1) / (y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;
constexpr ll MAX = 200002;

int main()
{
    ll n;
    cin >> n;
    vl a(MAX, 0);
    ll b = 0, maxb = 0, sum = 0, ans = 0;
    rep1(i, n) cin >> a[i];
    rep1(i, n)
    {
        b += a[i];
        maxb = max(maxb, b);
        ans = max(ans, sum + maxb);
        sum += b;
    }
    cout << ans << endl;
    return 0;
}
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<math.h>
#include<map>
typedef long long int ll;
using namespace std;
#define maxn 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
const int mm=2e5+100;
ll d[mm],num[mm];
int main()
{
   ll n,m,i,j,t,a,b,c,p,k,kk; 
   cin>>n;
   for(i=1;i<=n;i++) scanf("%lld",&d[i]);
   for(i=1;i<=n;i++)
   {
   	  num[i]=num[i-1]+d[i];
   }
    ll ans=0; p=0; ll sum=0;
   for(i=1;i<=n;i++)
   {
   	   ans+=num[i];
   	   if(ans>=sum)
   	   {
   	    sum=ans; p=i;	
	   }
   }
   if(p==0) 
   {
   	 printf("0\n");
   }
   else
   {
   	  ll sum1=sum; ll o=0; o=max(o,sum);
   	  if(p!=1)
   	  {
   	   for(i=p;i>=1;i--)
   	   {
   	      if(d[i]>=0)
			 {
			    sum1-=d[i];	
			 } 	
			 else sum1-=d[i];
			 o=max(o,sum1);
	   }
	   o=max(o,sum1);
       }
	    sum1=sum;
//	    cout<<sum1<<endl;
       if(p!=n)
       {
	   for(i=1;i<=p;i++)
	   {
   	      if(d[i]<=0)
			 {
			    sum1+=d[i];	
			 } 	
			 else sum1+=d[i];
			 o=max(o,sum1);
			 //cout<<sum1<<endl;
	   }
	   o=max(o,sum1);
       }
	   cout<<o<<endl;
   }
	  
} 