#include <bits/stdc++.h>
bool FLAG = 0;
using namespace std;
const long long MOD = 998244353, N = (long long)1e6 + 222;
const long long INF = (long long)1e9;
long long dx[] = {0, -1, 0, 1, 0};
long long dy[] = {0, 0, 1, 0, -1};
long long a[N], tmx[N], tmn[N], pref[N], l[N], r[N], n;
void build(long long v, long long l, long long r) {
  if (l == r) {
    tmn[v] = pref[l];
    tmx[v] = pref[l];
    return;
  }
  long long mid = (l + r) / 2;
  build(v * 2, l, mid);
  build(v * 2 + 1, mid + 1, r);
  tmn[v] = min(tmn[v * 2], tmn[v * 2 + 1]);
  tmx[v] = max(tmx[v * 2], tmx[v * 2 + 1]);
}
long long getmn(long long v, long long l, long long r, long long L,
                long long R) {
  if (l > R || L > r) return INF;
  if (L <= l && r <= R) return tmn[v];
  long long mid = (l + r) / 2;
  return min(getmn(v * 2, l, mid, L, R), getmn(v * 2 + 1, mid + 1, r, L, R));
}
long long getmx(long long v, long long l, long long r, long long L,
                long long R) {
  if (l > R || L > r) return -INF;
  if (L <= l && r <= R) return tmx[v];
  long long mid = (l + r) / 2;
  return max(getmx(v * 2, l, mid, L, R), getmx(v * 2 + 1, mid + 1, r, L, R));
}
inline void solve() {
  cin >> n;
  for (long long i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  a[0] = INF;
  a[n + 1] = INF;
  vector<long long> v;
  for (long long i = 1; i <= n + 1; ++i) {
    while ((long long)(v.size()) && a[v[(long long)(v.size()) - 1]] < a[i]) {
      r[v[(long long)(v.size()) - 1]] = i;
      v.pop_back();
    }
    v.push_back(i);
  }
  v.clear();
  for (long long i = n; i >= 0; --i) {
    while ((long long)(v.size()) && a[v[(long long)(v.size()) - 1]] < a[i]) {
      l[v[(long long)(v.size()) - 1]] = i;
      v.pop_back();
    }
    v.push_back(i);
  }
  for (long long i = 1; i <= n; ++i) {
    if (l[i] != 0) l[i]++;
    pref[i] = pref[i - 1] + a[i];
  }
  build(1, 0, n);
  long long ans = 0;
  for (long long i = 1; i <= n; ++i) {
    if (a[i] < 0) continue;
    ans = max(ans,
              (getmx(1, 0, n, i, r[i] - 1) - getmn(1, 0, n, l[i], i)) - a[i]);
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
