#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fll;
const int MAX = 1e5 + 10;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> v;
  map<int, vector<int>> mp;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    v.push_back(a);
    mp[a].push_back(i);
  }
  int ans = 0;
  for (int k = 30; k >= 0; k--) {
    int sat = 0, id1 = 0;
    int ct = 0;
    for (int i = 0; i < n; i++) {
      sat += v[i];
      if (v[i] == k) {
        ct++;
        if (ct == 1) sat -= k;
      }
      if (sat > ans and ct) ans = sat;
      if ((!ct and sat < 0) or (ct and sat < -k)) {
        sat = 0;
        ct = 0;
      }
    }
    for (auto x : mp[k]) v[x] = -INF;
  }
  cout << ans << '\n';
  exit(0);
}
