#include <bits/stdc++.h>
#pragma GCC optimze("Ofast")
using namespace std;
const long long infl = 0x3f3f3f3f3f3f3f3fLL;
const long long infi = 0x3f3f3f3f;
const long long mod = 998244353;
const long long N = 5e5 + 5;
void solve() {
  long long n;
  cin >> n;
  vector<long long> a(n + 1, 0);
  vector<vector<long long> > f(n + 1, vector<long long>(61, 0));
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    for (long long j = 0; j <= 60; j++) f[i][j] = f[i - 1][j];
    f[i][a[i] + 30]++;
  }
  long long res = 0;
  for (long long i = 1; i <= 30; i++) {
    vector<long long> b(n + 1);
    for (long long j = 1; j <= n; j++) {
      if (a[j] <= i)
        b[j] = a[j];
      else
        b[j] = -infi;
    }
    long long ans = 0, sum = 0, min_sum = 0, maxi = 0;
    for (long long r = 1; r <= n; ++r) {
      if (b[r] == -infi) {
        min_sum = 0;
        sum = 0;
        maxi = 0;
        continue;
      }
      maxi = max(maxi, b[r]);
      sum += b[r];
      if (maxi == i) ans = max(ans, sum - min_sum - i);
      if (sum < min_sum) {
        min_sum = sum;
        maxi = 0;
      }
    }
    res = max(res, ans);
  }
  cout << res << '\n';
}
signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cout << fixed << setprecision(15);
  long long t = 1;
  while (t--) solve();
  return 0;
}
long long norm(long long a) {
  while (a >= mod) a -= mod;
  while (a < 0) a += mod;
  return a;
}
long long powm(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
long long divide(long long a, long long b) {
  return (a % mod) * powm(b, mod - 2) % mod;
}
long long mul(long long a, long long b) {
  return ((a % mod) * (b % mod)) % mod;
}
long long add(long long a, long long b) { return (a % mod + b % mod) % mod; }
