#include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long y, long long m);
long long modInverse(long long n, long long m);
long long nCr(long long n, long long r);
long long ceiling(long long x, long long y);
bool sortbyth(const tuple<long long, int, int>& a,
              const tuple<long long, int, int>& b) {
  if (get<0>(a) != get<0>(b))
    return get<0>(a) > get<0>(b);
  else
    return get<1>(a) < get<1>(b);
}
void aksayushx() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  vector<vector<int>> fwd(n, vector<int>(31, 0)), bwd(n, vector<int>(31, 0));
  for (int i = 0; i < 31; i++) {
    fwd[0][i] = a[0] <= i ? max(0, a[0]) : 0;
    bwd[n - 1][i] = a[n - 1] <= i ? max(0, a[n - 1]) : 0;
  }
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 31; j++) {
      fwd[i][j] = fwd[i - 1][j];
      if (a[i] <= j)
        fwd[i][j] = max(fwd[i][j] + a[i], 0);
      else
        fwd[i][j] = 0;
    }
  }
  for (int i = n - 2; i >= 0; i--) {
    for (int j = 0; j < 31; j++) {
      bwd[i][j] = bwd[i + 1][j];
      if (a[i] <= j)
        bwd[i][j] = max(bwd[i][j] + a[i], 0);
      else
        bwd[i][j] = 0;
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int maxx = 0; maxx < 31; maxx++) {
      int candidate = fwd[i][maxx] + (i + 1 < n ? bwd[i + 1][maxx] : 0) - maxx;
      ans = max(ans, candidate);
    }
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  while (t--) aksayushx();
  return 0;
}
long long power(long long x, long long y, long long m) {
  long long res = 1;
  x = x % m;
  if (x == 0) return 0;
  while (y > 0) {
    if (y & 1) res = (res * x) % m;
    y = y >> 1;
    x = (x * x) % m;
  }
  return res;
}
long long modInverse(long long n, long long m) { return power(n, m - 2, m); }
long long ceiling(long long x, long long y) { return (x + y - 1) / y; }
