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

#include <bits/stdc++.h>
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
using ll = long long;
using datas = pair<ll, ll>;
using ddatas = pair<long double, long double>;
using tdata = pair<ll, datas>;
using vec = vector<ll>;
using mat = vector<vec>;
using pvec = vector<datas>;
using pmat = vector<pvec>;
const ll mod = 1000000007;
const ll inf = 1LL << 60;
const long double PI = acos(-1);
const long double eps = 1e-9;
template <class T>
inline bool chmax(T& a, T b) {
  bool x = a < b;
  if (x) a = b;
  return x;
}
template <class T>
inline bool chmin(T& a, T b) {
  bool x = a > b;
  if (x) a = b;
  return x;
}
void startupcpp() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}
long double distance(ddatas x, ddatas y) {
  long double a = x.first - y.first, b = x.second - y.second;
  return sqrt(a * a + b * b);
}
ll modinv(ll a) {
  ll b = mod, u = 1, v = 0, t;
  while (b) {
    t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  return (u + mod) % mod;
}
ll moddevide(ll a, ll b) { return (a * modinv(b)) % mod; }
vec modncrlistp, modncrlistm;
ll modncr(ll n, ll r) {
  if (n < r) return 0;
  ll i, size = modncrlistp.size();
  if (size <= n) {
    modncrlistp.resize(n + 1);
    modncrlistm.resize(n + 1);
    if (!size) {
      modncrlistp[0] = modncrlistm[0] = 1;
      size++;
    }
    for (i = size; i < (ll)n + 1; ++i) {
      modncrlistp[i] = modncrlistp[i - 1] * i % mod;
      modncrlistm[i] = modinv(modncrlistp[i]);
    }
  }
  return modncrlistp[n] * modncrlistm[r] % mod * modncrlistm[n - r] % mod;
}
ll modpow(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1) res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
ll gcd(ll a, ll b) {
  if (!b) return abs(a);
  return (a % b == 0) ? abs(b) : gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll countdigits(ll n) {
  ll ans = 0;
  while (n) {
    n /= 10;
    ans++;
  }
  return ans;
}
ll sumdigits(ll n) {
  ll ans = 0;
  while (n) {
    ans += n % 10;
    n /= 10;
  }
  return ans;
}
void topcoder(vector<int>& v) {
  string s;
  while (1) {
    cin >> s;
    int i = s[0] == '{', x = 0;
    while (s[i] >= '0' && s[i] <= '9') {
      x = x * 10 + s[i] - '0';
      ++i;
    }
    v.emplace_back(x);
    if (s[i] == '}') break;
  }
}
void topcoder(string& s) {
  string t;
  cin >> t;
  int i = 1;
  while (t[i] != '"') {
    s += t[i++];
  }
}
template <typename T>
struct segtree {
 private:
  int N = 1;
  T E;
  using func = function<T(T, T)>;
  func f, g;
  vector<T> tree;

 public:
  segtree(vector<T>& v, T e, func Merge, func Act) : E(e), f(Merge), g(Act) {
    int i, K = v.size();
    while (N < K) N <<= 1;
    tree.resize(N << 1);
    for (i = N; i < (ll)N + K; ++i) tree[i] = v[i - N];
    for (i = N + K; i < (ll)N << 1; ++i) tree[i] = E;
    for (i = N, --i; i >= (ll)1; --i)
      tree[i] = f(tree[i << 1], tree[(i << 1) | 1]);
  }
  segtree(vector<T>& v, T e, func Merge) {
    *this = segtree(v, e, Merge, Merge);
  };
  segtree(int N, T e, func F) {
    vector<T> res(N, e);
    *this = segtree(res, e, F);
  }
  void action(int i, T a) {
    if ((i < 0) | (i >= N)) return;
    i += N;
    tree[i] = g(tree[i], a);
    while (i > 1) {
      i >>= 1;
      tree[i] = f(tree[i << 1], tree[(i << 1) | 1]);
    }
  }
  void input(int i, T a) {
    if ((i < 0) | (i >= N)) return;
    tree[i += N] = a;
    while (i > 1) {
      i >>= 1;
      tree[i] = f(tree[i << 1], tree[(i << 1) | 1]);
    }
  }
  T queli(int a, int b) {
    if ((a >= b) | (a < 0) | (b <= 0) | (a >= N) | (b > N)) return E;
    T l(E), r(E);
    for (a += N, b += N; a < b; a >>= 1, b >>= 1) {
      if (a & 1) l = f(l, tree[a++]);
      if (b & 1) r = f(tree[--b], r);
    }
    return f(l, r);
  }
};
int main() {
  startupcpp();
  ll i, j = 0, N, ans = 0;
  cin >> N;
  pvec v(N);
  vec sum(N + 1);
  sum[0] = 0;
  for (i = 0; i < (ll)N; ++i) {
    cin >> v[i].first;
    sum[i + 1] += v[i].first + sum[i];
    v[i].second = i + 1;
  }
  sort((v).rbegin(), (v).rend());
  segtree<ll> mi(sum, inf, [](ll a, ll b) { return min(a, b); });
  segtree<ll> ma(sum, -inf, [](ll a, ll b) { return max(a, b); });
  set<ll> se;
  se.insert(0);
  se.insert(N + 1);
  for (i = 0; i < (ll)N; ++i) {
    while (v[j].first > v[i].first) {
      se.insert(v[j++].second);
    }
    auto itr = se.upper_bound(v[i].second);
    ll a, b;
    b = *itr;
    --itr;
    a = *itr;
    chmax(ans,
          ma.queli(v[i].second, b) - mi.queli(a, v[i].second) - v[i].first);
  }
  cout << ans << "\n";
}
