#include<bits/stdc++.h>
using namespace std;
typedef long long ll; typedef double db;
typedef pair<int, int> pii; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vl;
typedef vector<pii> vpii; typedef vector<pll> vpll;
#define FOR(n) for(int i=0;i<n;++i)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll gcd(ll a, ll b) { return a % b ? gcd(b, a % b) : b; }
const ll mod = 1e9 + 7; const ll inf = 0x3f3f3f3f;
ll qpow(ll a, ll b) { ll t = 1; while (b != 0) { if (b & 1)t = (t * a) % mod; a = (a * a) % mod; b >>= 1; }return t; }
int n;
ll a[212345];
ll b[212345];
ll c[212345];
signed main() {
	cin >> n;
	FOR(n)cin >> a[i];
	b[0] = a[0];
	for (int i = 1; i < n; ++i)b[i] = b[i - 1] + a[i];
	c[0] = max(0ll, b[0]);
	for (int i = 1; i < n; ++i)c[i] = max(c[i - 1], b[i]);
	/*FOR(n)cout << a[i] << " "; cout << endl;
	FOR(n)cout << b[i] << " "; cout << endl;
	FOR(n)cout << c[i] << " "; cout << endl;*/
	ll mx = 0, ans = 0;
	FOR(n) {
		mx = max(ans + c[i], mx);
		ans += b[i];
	}
	mx = max(mx, ans);
	cout << mx << endl;
	return 0;
}
//Iamskk//
#include <iostream>
#include <iosfwd>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cassert>
#include <cctype>
#include <climits>
#include <vector>
#include <bitset>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <deque>
#include <string>
#include <list>
#include <iterator>
#include <sstream>
#include <complex>
#include <fstream>
#include <functional>
#include <numeric>
#include <utility>
#include <algorithm>
#include <assert.h>
#include <unordered_map>
using namespace std;
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0);
#define endl '\n'
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define ai(arr,n) for(int i=0;i<n;i++)cin>>arr[i];
#define ao(arr) for(auto asdfasdfasdf:arr) cout<<asdfasdfasdf<<" ";
#define mi(arr,m,n) for(int i=0;i<m;i++){ for(int j=0;j<n;j++) cin>>arr[i][j];}
#define mo(arr,m,n) for(int i=0;i<m;i++){ for(int j=0;j<n;j++) cout<<arr[i][j]<<" "; cout<<endl;}
#define countsetbits(x) __builtin_popcount(x)
#define ll long long
#define debug cout<<"I AM EXECUTING"<<endl
#define testcases int asdf; cin>>asdf; while(asdf--)
#define vi vector<int>
#define si set<int>
#define vc vector<char>
#define sc set<char>
#define vll vector<long long int>
#define vs vector<string>
#define vpp vector<pair<int,int>> 
#define vppo(prs) for(auto x:prs){cout<<x.first<<" "<<x.second<<endl;}
#define For(__,hajmola,adfdf) for(int __ = hajmola; __<adfdf;__++)

string sconvert(ll int n)
{
	stringstream ss;
	ss<<n;
	string str = ss.str();
	return str;
}
bool sortbysec(const pair<int,int> &a, 
              const pair<int,int> &b) 
{ 
    return (a.second > b.second); 
}


void  single()
{
	ll int n;
	cin>>n;
	vector<ll int> arr(n);
	ai(arr,n);
	vector<ll int> mtn(n,0);
	ll int sum=arr[0];
	mtn[0] = max(mtn[0],sum);
	For(i,1,n){
        sum+=arr[i];
        if(sum>mtn[i-1]){
        	mtn[i]=sum;
        }
        else{
        	mtn[i]=mtn[i-1];
        }
	}
	ll int answer = 0;
	sum=0;
	ll int lp = 0;
	For(i,0,n){
       sum+=arr[i];
       answer = max(answer,lp+mtn[i]);
       lp=lp+sum;
	}
	cout<<answer<<endl;
	return;
}
void multiple()
{
	testcases
    {
		single();
	}
}

int main()
{
  IOS;
 // freopen("input.txt","r",stdin);
  //freopen("output.txt","w",stdout);
  single();
}

















