10
-3 4 -4 1 1 -4 -5 3 -4 -2
 10
8 9 -10 -10 -4 -3 5 9 -9 9
 6
5 -4 2 -1 2 -2
 11
3 0 1 -2 5 -5 -1 0 3 2 2
 10
4 -2 -2 -2 2 0 1 0 -4 -1
 4
2 2 -3 4
 11
-11 -7 -2 -24 30 -26 24 9 -8 -9 23
 6
30 -29 5 5 -29 30
 24
-8 -6 6 -11 -15 -7 10 -19 -6 21 14 -4 -21 -10 12 17 1 -1 5 19 2 0 -4 -23
 10
-9 -6 -1 9 -2 0 0 -1 5 0
 6
10 -8 3 3 -8 10
 10
-2 -3 -1 4 -3 2 0 1 -5 4
 5
30 -29 10 -1 10
 31
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -29 30
 7
30 -20 5 1 3 -20 30
 5
30 -29 4 -3 4
 10
-5 28 -18 -10 9 -2 11 -6 -19 30
 8
4 -10 13 -13 2 2 -11 6
 131
30 -29 29 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 27 -26 27
 10
-4 4 0 -2 3 0 1 2 3 -4
 10
-2 -8 5 4 -1 -6 -9 6 1 -6
 7
25 -17 13 13 -25 25 -24
 10
3 -3 2 1 -4 -2 3 -2 -3 0
 10
4 -5 0 4 0 0 -3 1 1 -4
 10
0 4 4 1 -3 4 2 1 4 -1
 17
15 -13 7 8 8 1 -19 12 8 -23 -4 16 -13 -28 10 -30 -14
 4
1 1 -4 10
 11
3 -2 1 1 1 1 1 1 1 -3 4
 7
30 -29 10 10 10 -29 30
 7
25 -13 17 -23 12 12 9
 7
15 -14 10 10 10 -14 15
 10
3 0 0 -4 3 1 -2 -1 -1 4
 6
8 -7 7 -6 5 5
 10
1 -4 -4 4 -2 -3 3 0 -2 3
 15
1 -26 -6 26 7 10 -2 -4 -6 14 -9 25 9 -12 -28
 6
30 -29 10 10 -29 30
 1
6
 2
1 1
 5
0 5 -4 3 -1
 7
5 -4 3 -1 3 -4 4
 10
16 -15 13 -1 10 -2 -20 26 5 -30
 10
-1 2 0 -1 2 -1 4 3 -5 1
 7
5 -4 1 1 1 -4 5
 5
10 -3 -5 3 5
 10
4 -4 3 -2 3 -4 -2 0 1 0
 10
-5 -5 -3 1 -2 2 0 2 -3 4
 10
2 -4 -2 3 1 1 -2 -1 -3 4
 10
-4 -5 4 -3 2 -2 2 1 -1 0
 10
0 -1 4 -2 3 -2 -1 -5 -5 2
 5
19 -6 1 -11 19
 10
1 0 -4 3 -5 3 -1 2 2 -5
 7
5 -5 3 4 3 -5 5
 10
-5 2 -3 -3 -4 3 3 0 2 4
 4
2 3 -10 11
 10
27 -17 15 -22 18 17 -6 2 16 -14
 6
30 -22 6 7 -22 30
 7
30 -25 0 6 9 -2 10
 9
7 2 -1 1 1 -10 6 -1 6
 4
1 -10 1 10
 5
17 -16 12 -11 15
 35
-23 -22 23 4 -22 29 -4 -6 -28 18 -5 21 -7 -8 -11 -7 30 -25 -1 12 19 5 -8 12 -1 10 -24 -19 24 -17 -7 24 20 -22 16
 10
4 -3 1 -2 1 1 0 -4 4 -2
 10
4 -2 1 0 1 -5 -2 1 0 -5
 2
3 9
 9
11 -5 -5 1 2 3 -5 -5 11

1
 8
 1
 4
 1
 2
 15
 5
 36
 2
 3
 1
 9
 28
 4
 1
 7
 2
 1
 7
 4
 13
 1
 1
 13
 16
 1
 6
 20
 21
 20
 1
 5
 1
 44
 10
 0
 1
 0
 2
 9
 5
 2
 3
 1
 2
 2
 1
 1
 3
 3
 6
 8
 2
 29
 6
 13
 5
 1
 1
 30
 1
 1
 3
 3

#define ll long long
#define lld long double
#define cn(n)  ll n;cin >> n;
#define tes(t) ll t;cin>>t;while(t--) 
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define repd(i, a, b) for (int i = a; i >= b; i--)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define al(v) v.begin(), v.end()
#define oset(X) tree<X, null_type, less<X>, rb_tree_tag, tree_order_statistics_node_update>
#define roset(X) tree<X, null_type, greater<X>, rb_tree_tag, tree_order_statistics_node_update>
const ll MOD = 1000 * 1000 * 1000 + 7;
const ll INF = 1ll * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 7;
const ll MOD2 = 998244353;
const long double PI = 3.141592653589793;
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
using namespace __gnu_pbds;
using namespace std;
ll power(ll x, ll y, ll p){ll res = 1;x %= p;while (y > 0){if (y & 1)res = (res * x) % p;y = y >> 1;x = (x * x) % p;}return res;}
ll po(ll base, ll powerRaised){if (powerRaised != 0)return (base * po(base, powerRaised - 1));else return 1;}
ll gcd(ll a, ll b){if (b == 0)return a;return gcd(b, a % b);}
ll lcm(ll a, ll b){return (a * b) / gcd(a, b);}
ll a[100009];
vector<int>v[100];
struct node
{
    ll sum, prefixsum, suffixsum, maxsum;
};
node segt[4*100009];
void build(ll start, ll end, ll pos)
{
    ll mid;
    if (start == end)
    {
        segt[pos].sum = a[start];
        segt[pos].prefixsum = a[start];
        segt[pos].suffixsum = a[start];
        segt[pos].maxsum = a[start];
        return;
    }
    mid = (start + end) / 2;
    build(start, mid, 2 * pos);
    build(mid + 1, end, 2 * pos + 1);
    segt[pos].sum = segt[2 * pos].sum + segt[2 * pos + 1].sum;
    segt[pos].prefixsum = max(segt[2 * pos].prefixsum, segt[2 * pos].sum + segt[2 * pos + 1].prefixsum);
    segt[pos].suffixsum = max(segt[2 * pos + 1].suffixsum, segt[2 * pos + 1].sum + segt[2 * pos].suffixsum);
    segt[pos].maxsum = max(segt[pos].suffixsum, max(segt[pos].prefixsum, max(segt[2 * pos].maxsum, max(segt[2 * pos + 1].maxsum, segt[2 * pos].suffixsum + segt[2 * pos + 1].prefixsum))));
}
node query(ll start, ll end, ll pos, ll x, ll y)
{
    if (start >= x && end <= y)
        return segt[pos];
    ll mid = (start + end) / 2;
    if (y <= mid)
        return query(start, mid, 2 * pos, x, y);
    if (x > mid)
        return query(mid + 1, end, 2 * pos + 1, x, y);

    node left = query(start, mid, 2 * pos, x, y);
    node right = query(mid + 1, end, 2 * pos + 1, x, y);
    node nodal;
    nodal.sum = left.sum + right.sum;
    nodal.prefixsum = max(left.prefixsum, left.sum + right.prefixsum);
    nodal.suffixsum = max(right.suffixsum, right.sum + left.suffixsum);
    nodal.maxsum = max(nodal.prefixsum, max(nodal.suffixsum, max(right.maxsum, max(left.maxsum, left.suffixsum + right.prefixsum))));
    return nodal;
}

int findst(int num,int pos)
{
  int px=-1;
  int res=-1;
  for(int i=60;i>num;i--)
  {
    if(!v[i].empty()){
    auto itr=lower_bound(al(v[i]),pos);
    itr--;
    if(itr==prev(v[i].begin(),1))
    res=-1;
    else res=*itr;
    px=max(px,res);
    // if(num==34)
    // cout<<px<<" ";
    }
  }
  return px+1;
}
int findnd(int num,int pos)
{
  int px=100009;
  int res=res;
  for(int i=60;i>num;i--)
  {
    if(!v[i].empty()){
    auto itr=lower_bound(al(v[i]),pos);
    if(itr==v[i].end())
    res=100009;
    else res=*itr;
    px=min(px,res);
    }
  }
  return px-1;
}
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif //*
  int n;cin>>n;
  
  rep(i,1,n)
  {
    cin>>a[i];
    v[a[i]+30].pb(i);
  }
  build(1,n,1);
  int start,end;
  ll res=0;
  for(int i=60;i>=0;i--)
  {
    if(!v[i].empty())
    {
      for(int j:v[i])
      {
        ll sum=0;
        if(i==60)
        {
          start=1;
          end=j-1;
          if(start<=end)
          sum+=query(1,n,1,start,end).suffixsum;
          start=j+1;
          end=n;
          if(start<=end)
          sum+=query(1,n,1,start,end).prefixsum;
        }
        else
        {
          end=j-1;
          start=max(findst(i,j),1);
          if(start<=end)
          sum+=query(1,n,1,start,end).suffixsum;
          // if(i==34)cout<<start<<" "<<end<<" ";
          start=j+1;
          end=min(findnd(i,j),n);
          if(start<=end)
          sum+=query(1,n,1,start,end).prefixsum;

        }
        res=max(res,sum);
        // cout<<start<<" "<<end<<" "<<i<<" "<<res<<endl;
      }
      
    }
  }
  cout<<res;

}
