#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  vector<int> arr(n);
  for (int i = 0; i < n; i++) cin >> arr[i];
  int ans = 0;
  int max_so_far = 0, max_ending_here = 0, max_val = 0;
  for (int j = 0; j <= 31; j++) {
    max_ending_here = 0;
    max_val = 0;
    for (int i = 0; i < n; i++) {
      max_ending_here += arr[i];
      max_val = max(max_val, arr[i]);
      if (max_val > j || max_ending_here <= 0) {
        max_ending_here = 0;
        max_val = 0;
      }
      if (max_val == j) ans = max(max_ending_here - max_val, ans);
    }
  }
  cout << ans << "\n";
}
int main() {
  int t = 1;
  for (int i = 0; i < t; i++) {
    solve();
  }
}
