#include <bits/stdc++.h>
using namespace std;
int static dp[100002][31][2];
int fun(int a[], int n, int i, int m, int p) {
  if (i == n - 1) {
    if (p == 0) return 0;
    return a[i] - max(a[i], m);
  }
  if (dp[i][m][p] != -1) return dp[i][m][p];
  if (p == 0) {
    if (a[i] > 0)
      return dp[i][m][p] =
                 max(fun(a, n, i + 1, m, p), a[i] + fun(a, n, i + 1, a[i], 1));
    return dp[i][m][p] = fun(a, n, i + 1, m, p);
  }
  return dp[i][m][p] =
             max(a[i] + fun(a, n, i + 1, max(m, a[i]), p), a[i] - max(a[i], m));
}
int main() {
  int n, ans;
  scanf("%d", &n);
  int a[n];
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  for (int i = 0; i < n + 1; i++) {
    for (int j = 0; j < 31; j++) {
      for (int k = 0; k < 2; k++) {
        dp[i][j][k] = -1;
      }
    }
  }
  ans = fun(a, n, 0, 0, 0);
  printf("%d ", ans);
  printf("\n");
}
