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>
using namespace std;
const long long MAX = (long long)1e18;
const long long INF = (long long)1e18;
const long long MIN = (long long)-1e18;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? ", " : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
struct segminidx {
  long long sz;
  vector<array<long long, 4>> v;
  segminidx(long long n) {
    sz = 1;
    while (sz < n) sz *= 2;
    v.assign(2 * sz, {0, 0, 0, 0});
  }
  array<long long, 4> merge(array<long long, 4> a1, array<long long, 4> a2) {
    if (a1[0] < a2[0]) return a1;
    if (a2[0] < a1[0]) return a2;
    return {a1[0], a1[1] + a2[1], min(a1[2], a2[2]), max(a1[3], a2[3])};
  }
  void build(vector<long long> &a, long long x, long long lx, long long rx) {
    if (rx - lx == 1) {
      if (lx < a.size()) {
        v[x] = {a[lx], 1, lx, lx};
      }
      return;
    }
    long long m = (lx + rx) / 2;
    build(a, 2 * x + 1, lx, m);
    build(a, 2 * x + 2, m, rx);
    v[x] = merge(v[2 * x + 1], v[2 * x + 2]);
  }
  void build(vector<long long> &a) { build(a, 0, 0, sz); }
  void set(long long i, long long va, long long x, long long lx, long long rx) {
    if (rx - lx == 1) {
      v[x] = {va, 1, i, i};
      return;
    }
    long long m = (lx + rx) / 2;
    if (i < m)
      set(i, va, 2 * x + 1, lx, m);
    else
      set(i, va, 2 * x + 2, m, rx);
    v[x] = merge(v[2 * x + 1], v[2 * x + 2]);
  }
  void set(long long i, long long va) { set(i, va, 0, 0, sz); }
  array<long long, 4> findmin(long long l, long long r, long long x,
                              long long lx, long long rx) {
    if (lx >= r || rx <= l) {
      return {INF, 0, INF, -INF};
    }
    if (lx >= l && rx <= r) {
      return v[x];
    }
    array<long long, 4> a1, a2;
    long long m = (lx + rx) / 2;
    a1 = findmin(l, r, 2 * x + 1, lx, m);
    a2 = findmin(l, r, 2 * x + 2, m, rx);
    return merge(a1, a2);
  }
  array<long long, 4> findmin(long long l, long long r) {
    return findmin(l, r, 0, 0, sz);
  }
};
struct segmaxidx {
  long long sz;
  vector<array<long long, 4>> v;
  segmaxidx(long long n) {
    sz = 1;
    while (sz < n) sz *= 2;
    v.assign(2 * sz, {0, 0, 0, 0});
  }
  array<long long, 4> merge(array<long long, 4> a1, array<long long, 4> a2) {
    if (a1[0] < a2[0]) return a2;
    if (a2[0] < a1[0]) return a1;
    return {a1[0], a1[1] + a2[1], min(a1[2], a2[2]), max(a1[3], a2[3])};
  }
  void build(vector<long long> &a, long long x, long long lx, long long rx) {
    if (rx - lx == 1) {
      if (lx < a.size()) {
        v[x] = {a[lx], 1, lx, lx};
      }
      return;
    }
    long long m = (lx + rx) / 2;
    build(a, 2 * x + 1, lx, m);
    build(a, 2 * x + 2, m, rx);
    v[x] = merge(v[2 * x + 1], v[2 * x + 2]);
  }
  void build(vector<long long> &a) { build(a, 0, 0, sz); }
  void set(long long i, long long va, long long x, long long lx, long long rx) {
    if (rx - lx == 1) {
      v[x] = {va, 1, i, i};
      return;
    }
    long long m = (lx + rx) / 2;
    if (i < m)
      set(i, va, 2 * x + 1, lx, m);
    else
      set(i, va, 2 * x + 2, m, rx);
    v[x] = merge(v[2 * x + 1], v[2 * x + 2]);
  }
  void set(long long i, long long va) { set(i, va, 0, 0, sz); }
  array<long long, 4> findmax(long long l, long long r, long long x,
                              long long lx, long long rx) {
    if (lx >= r || rx <= l) {
      return {-INF, 0, INF, -INF};
    }
    if (lx >= l && rx <= r) {
      return v[x];
    }
    array<long long, 4> a1, a2;
    long long m = (lx + rx) / 2;
    a1 = findmax(l, r, 2 * x + 1, lx, m);
    a2 = findmax(l, r, 2 * x + 2, m, rx);
    return merge(a1, a2);
  }
  array<long long, 4> findmax(long long l, long long r) {
    return findmax(l, r, 0, 0, sz);
  }
};
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long n;
  cin >> n;
  vector<long long> a(n);
  for (long long i = 0; i < n; ++i) {
    cin >> a[i];
  }
  vector<long long> pref(n + 1);
  pref[0] = 0;
  for (long long i = 1; i <= n; ++i) {
    pref[i] = pref[i - 1] + a[i - 1];
  }
  segminidx smin(n + 1);
  segmaxidx smax(n + 1);
  smin.build(pref);
  smax.build(pref);
  vector<long long> r(n), l(n);
  r[n - 1] = n;
  l[0] = -1;
  for (long long i = 1; i < n; ++i) {
    long long j = i - 1;
    while (j >= 0 && a[j] <= a[i]) {
      j = l[j];
    }
    l[i] = j;
  }
  for (long long i = n - 2; i >= 0; --i) {
    long long j = i + 1;
    while (j < n && a[j] <= a[i]) {
      j = r[j];
    }
    r[i] = j;
  }
  long long ans = MIN;
  for (long long i = 0; i < n; ++i) {
    long long res = 0;
    array<long long, 4> q1, q2;
    if (r[i] != n)
      q1 = smax.findmax(i + 1, r[i] + 1);
    else
      q1 = smax.findmax(i + 1, n + 1);
    if (l[i] != -1)
      q2 = smin.findmin(l[i] + 2, i + 2);
    else
      q2 = smin.findmin(0, i + 2);
    if (q1[3] == q2[2]) {
      res = 0;
    } else {
      res = q1[0] - q2[0] - a[i];
    }
    ans = max(ans, res);
  }
  cout << ans << endl;
  return 0;
}
