#include <bits/stdc++.h>
using namespace std;
#define fast_io cin.tie(0);ios_base::sync_with_stdio(0);
string to_string(string s) { return '"' + s + '"';}
string to_string(char s) { return string(1, s);}
string to_string(const char* s) { return to_string((string) s);}
string to_string(bool b) { return (b ? "true" : "false");}
template <typename A> string to_string(A);
template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template <typename A> string to_string(A v) {bool f = 1; string r = "{"; for (const auto &x : v) {if (!f)r += ", "; f = 0; r += to_string(x);} return r + "}";}
void debug_out() { cout << endl; }
void show() { cout << endl; }
void pret() { cout << endl; exit(0);}
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {cout << " " << to_string(H); debug_out(T...);}
template <typename Head, typename... Tail> void show(Head H, Tail... T) {cout <<H<<" "; show(T...);}
template <typename Head, typename... Tail> void pret(Head H, Tail... T) {cout <<H<<" "; pret(T...);}
#define pr(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
typedef long long ll;
#define int ll
typedef long double ld;
typedef vector<int> vi;
#define disp(x) cout<<x<<" ";
#define rep(i,a,b) for(int i=a;i<(int)b;i++)
#define fo(i,a,b) for(int i=a;i<=(int)b;i++)
#define rf(i,a,b) for(int i=a;i>=(int)b;i--)
#define mp make_pair
#define pb emplace_back
#define F first
#define S second
#define endl '\n'
//cout.setf(ios::fixed);cout.precision(18)
const int MOD = 1e9+7;
const int maxn = 300000+10;

int a[maxn], prefix[maxn];

int f(){
    int n;
    cin >> n;
    fo(i, 1, n) cin >> a[i];
    fo(i, 1, n) prefix[i] = a[i] + prefix[i - 1];
    int ans = 0, curr = 0, val = -MOD;
    fo(i, 1, n){
        val = max(val, prefix[i]);
        ans = max(ans, curr + val);
        curr += prefix[i];
    }
    return ans;
}

int32_t main(){
    fast_io;
    int t = 1;
    // cin>>t;
    while(t--){
        show(f());

        // f();

        // if(f()){
        //  cout<<"YES"<<endl;
        // }
        // else cout<<"NO"<<endl;
    }
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for(int i=0;i<(int)n;i++)
#define PI acos(-1)
#define fast_io ios_base::sync_with_stdio(false) ; cin.tie(0); cout.tie(0);
ll mod=1e9+7;

int main(){
    fast_io
   
    ll n;
    cin >> n;
    vector<ll> A(n);
    rep(i,n) cin >> A[i];
    ll ans = 0;
    ll s= 0, b = 0;
    ll max_b = 0;
    rep(i,n){
      // フェーズごとの累積和
      b += A[i];
      // これまでの累積和の最大
      max_b = max(max_b, b);
      // 最終的な座標からそれまでの累積和の最大
      ans = max(ans, s + max_b);
      // 各フェーズの最終的な座標
      s += b;
    }
    cout << ans << endl;
    return 0;
}
