#include <bits/stdc++.h>
using namespace std;
vector<long long> mint, maxt;
vector<long long> dp;
void build(long long v, long long l, long long r) {
  if (l + 1 == r) {
    mint[v] = maxt[v] = dp[l];
    return;
  }
  long long m = (r + l) / 2;
  build(2 * v + 1, l, m);
  build(2 * v + 2, m, r);
  mint[v] = min(mint[2 * v + 1], mint[2 * v + 2]);
  maxt[v] = max(maxt[2 * v + 1], maxt[2 * v + 2]);
}
long long maxi(long long v, long long l, long long r, long long L,
               long long R) {
  if (l >= L && r <= R) {
    return maxt[v];
  }
  if (l >= R || r <= L) {
    return -1e18;
  }
  long long m = (r + l) / 2;
  return max(maxi(2 * v + 1, l, m, L, R), maxi(2 * v + 2, m, r, L, R));
}
long long mini(long long v, long long l, long long r, long long L,
               long long R) {
  if (l >= L && r <= R) {
    return mint[v];
  }
  if (l >= R || r <= L) {
    return 1e18;
  }
  long long m = (r + l) / 2;
  return min(mini(2 * v + 1, l, m, L, R), mini(2 * v + 2, m, r, L, R));
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n;
  cin >> n;
  vector<long long> p(n);
  for (auto &x : p) {
    cin >> x;
  }
  dp.assign(n + 1, 0);
  for (int i = 0; i < n; i++) {
    dp[i + 1] = dp[i] + p[i];
  }
  mint.resize(4 * (dp.size()));
  maxt.resize(4 * (dp.size()));
  build(0, 0, dp.size());
  vector<map<long long, long long> > left(n + 1), right(n + 1);
  map<long long, long long> last;
  for (int i = -30; i <= 30; i++) {
    last[i] = -1;
  }
  for (int i = 0; i < n; i++) {
    left[i + 1] = last;
    last[p[i]] = i + 1;
  }
  for (int i = -30; i <= 30; i++) {
    last[i] = dp.size();
  }
  for (int i = n - 1; i >= 0; i--) {
    right[i + 1] = last;
    last[p[i]] = i + 1;
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    long long l = -1;
    for (int j = p[i] + 1; j <= 30; j++) {
      l = max(l, left[i + 1][j]);
    }
    long long r = dp.size();
    for (int j = p[i] + 1; j <= 30; j++) {
      r = min(r, right[i + 1][j]);
    }
    long long maxx = maxi(0, 0, dp.size(), i + 1, r);
    long long minn = mini(0, 0, dp.size(), l + 1, i + 1);
    ans = max(ans, maxx - minn - p[i]);
  }
  cout << ans;
  return 0;
}
