#include <bits/stdc++.h>
const int MAX = 500010;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const long long LLINF = 0x3f3f3f3f3f3f3f3f;
const long double EPS = 1e-6;
const long double PI = acosl(-1.0);
using namespace std;
struct no {
  long long pref, suff, total, best;
  no(long long x = 0) : pref(x), suff(x), total(x), best(x) {}
};
no merge(no l, no r) {
  no ans;
  ans.pref = max(0LL, max(l.pref, l.total + r.pref));
  ans.suff = max(0LL, max(r.suff, l.suff + r.total));
  ans.total = l.total + r.total;
  ans.best = max(max(l.best, r.best), l.suff + r.pref);
  return ans;
}
struct Segtree {
  vector<no> t;
  int n;
  Segtree(int n) {
    this->n = n;
    t.assign(2 * n, no(0));
  }
  void build() {
    for (int i = n - 1; i > 0; i--) t[i] = merge(t[i << 1], t[i << 1 | 1]);
  }
  no query(int l, int r) {
    no a(0), b(0);
    for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1) a = merge(a, t[l++]);
      if (r & 1) b = merge(t[--r], b);
    }
    return merge(a, b);
  }
  void update(int pos, int val) {
    for (t[pos += n] = no(val); pos /= 2;)
      t[pos] = merge(t[2 * pos], t[2 * pos + 1]);
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, a[100010];
  cin >> n;
  Segtree st(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    st.t[n + i] = no(a[i]);
  }
  st.build();
  no aux;
  aux = st.query(0, n - 1);
  long long resp = aux.best - 30;
  for (int i = 30; i >= 1; i--) {
    for (int j = 0; j < n; j++) {
      if (a[j] == i) {
        st.update(j, -INF);
      }
    }
    aux = st.query(0, n - 1);
    resp = max(resp, aux.best - (i - 1));
  }
  cout << resp << '\n';
  return 0;
}
