#include<bits/stdc++.h>
using namespace std;
void __print(int x) {cout << x;}void __print(long x) {cout << x;}void __print(long long x) {cout << x;}void __print(unsigned x) {cout << x;}void __print(unsigned long x) {cout << x;}void __print(unsigned long long x) {cout << x;}void __print(float x) {cout << x;}void __print(double x) {cout << x;}void __print(long double x) {cout << x;}void __print(char x) {cout << '\'' << x << '\'';}void __print(const char *x) {cout << '\"' << x << '\"';}void __print(const string &x) {cout << '\"' << x << '\"';}void __print(bool x) {cout << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cout << '{'; for (auto &i: x) cout << (f++ ? "," : ""), __print(i); cout << "}";}
void _print() {cout << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cout << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cout << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define IOS             ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define loop(i,a,b)     for (ll i = a; i <= (long long)b; i++)
#define rloop(i,a,b)    for (ll i = a; i >= (long long)b; i--)
#define ms(a,x)         memset(a,x,sizeof a)
#define ll              long long int
#define ff              first
#define ss              second
#define all(a)          a.begin(),a.end()
#define rall(a)         a.rbegin(),a.rend()
#define pb              push_back
#define nl              cout<<"\n"
#define gcd(a,b)        __gcd((a),(b))
#define lcm(a,b)        ((a)*(b))/(gcd(a,b))
#define sq(a)           ((a)*(a))
#define sz(a)           a.size()
#define mod             1000000007
#define PI              (2*acos(0.0))

int main()
{
    IOS
    int T=1;
    //cin>>T;
    while(T--)
    {
      ll n;
      cin>>n;
      std::vector<ll> a(n),b(n),c(n),d(n),e(n);
      loop(i,0,n-1) cin>>a[i];
      b[0]=a[0];
      loop(i,1,n-1) b[i]=a[i]+b[i-1];
      e[0]=b[0];
      loop(i,1,n-1) e[i]=e[i-1]+b[i];
      ll mx=0;
      loop(i,0,n-1) 
      {
      	mx=max(mx,b[i]);
      	c[i]=mx;
      }
      // debug(b);
      // debug(c);
      d[0]=max(0LL,a[0]);
      ll ans=max(0LL,d[0]);
      loop(i,1,n-1)
      {
      	d[i]=e[i-1]+c[i];
      	ans=max(ans,d[i]);
      }
      //debug(d);
      cout<<ans;
    }
    return 0;
}

#include <iostream>
#include <algorithm>       // for next_permutation
#include <vector>
#include <bits/stl_numeric.h>
#include <queue>
#include <regex>

#define DEBUG 0
#define REP(i, n) for (long long i = 0; i < (n); i++) 
typedef long long ll;
static const ll MOD = 1000000007;
static const ll INF = 1000000000000000000LL;
using namespace std;

int main() {
    int N;
    cin >> N;

    vector <ll> a(N);
    vector <ll> ido(N,0);
    vector <ll> syunkan(N,0);


    ll wawa = 0;


    REP(i,N)cin >> a[i];
    ido[0] = a[0];
    syunkan[0] = max(0LL,a[0]);
    for(int i = 1; i < N; ++i)
    {
        ido[i] = ido[i-1] + a[i];
        syunkan[i] = max(syunkan[i-1],ido[i-1] + a[i]);
    }

    ll res = 0;
    ll x = 0;
    x += a[0];
    res = max(res,x);
    for(int i = 1; i < N; ++i)
    {
        res = max(res, x + syunkan[i]);
        x = x + ido[i];
    }

    cout << res << endl;

    return 0;
}
