#include<iostream>
using namespace std;
#define ll long long int
int main()
{
     ll n,a;
	 cin>>n;
     ll sum=0,mx=0,ans=0,dis=0;
     for(ll i=0;i<n;i++){
       cin>>a;
       sum+=a;
       mx=max(mx,sum);
       ans=max(ans,dis+mx);  //First check plus the mx value cause after mx value it will decrease
       dis+=sum;
     }
     cout<<ans<<endl;
  return 0;
}
#include<iostream>
#include<vector>
#include<algorithm>
#include <sstream>
#include<string>
using namespace std;

int main()
{
	int n;
	long long a[5][2 * 100000 + 8] = { 0 },c=0,m=0;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[0][i];
		c += a[0][i];
		if (a[0][i] >= 0)
		{
			a[1][i] += a[1][i - 1] + a[0][i];
			a[2][i] = a[2][i - 1];

		}
		else
		{
			a[2][i] += a[2][i - 1] + a[0][i];
			a[1][i] = a[1][i - 1];
		}
		a[3][i] = a[3][i - 1] + a[1][i] + a[2][i];
		if (i == 1)
		{
			a[4][1] = a[0][1];
		}
		else
		{
			a[4][i] = max(c, a[4][i - 1]);
		}
		m = max(m, a[4][i] + a[3][i - 1]);
	}
	cout << m << endl;
}