#include <bits/stdc++.h>
using namespace std;
long long md = 998244353LL;
long long inf;
int p[100000] = {0};
int nx[100000] = {0};
long long x2[100000][20];
long long x1[100000][20];
long long getmn(int i, int j, int n, bool f) {
  bool f1 = false, f2 = false;
  if (i == -1) i = 0, f1 = true;
  if (j == -1) j = n - 1, f2 = true;
  int l = 1;
  int s = 0;
  while (i + l - 1 <= j) {
    l *= 2;
    s++;
  }
  s--;
  l /= 2;
  long long ret;
  if (f) {
    ret = min(x1[i][s], x1[j - l + 1][s]);
    if (f1) return min(ret, 0LL);
  } else {
    ret = min(x2[i][s], x2[j - l + 1][s]);
    if (f2) return min(ret, 0LL);
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  inf = md * md;
  inf *= 8;
  inf++;
  int test = 1;
  for (int t = 1; t <= test; t++) {
    int n;
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    };
    long long s1[n];
    s1[0] = a[0];
    long long s2[n];
    s2[n - 1] = a[n - 1];
    x1[0][0] = a[0];
    x2[n - 1][0] = a[n - 1];
    int i = 1;
    while (i < n) {
      s1[i] = s1[i - 1] + a[i];
      x1[i][0] = s1[i];
      i++;
    }
    i = n - 2;
    while (i >= 0) {
      s2[i] = s2[i + 1] + a[i];
      x2[i][0] = s2[i];
      i--;
    }
    i = 2;
    int j = 1;
    while (i <= n) {
      int k = i / 2;
      int f = 0;
      while (f < n) {
        if (f + i - 1 >= n) break;
        x1[f][j] = min(x1[f][j - 1], x1[f + k][j - 1]);
        f++;
      }
      f = n - 1;
      while (f >= 0) {
        if (f + i - 1 >= n) {
          f--;
          continue;
        }
        x2[f][j] = min(x2[f][j - 1], x2[f + k][j - 1]);
        f--;
      }
      i *= 2;
      j++;
    }
    p[0] = -1;
    int v[61];
    for (int i = 0; i < 61; i++) v[i] = -1;
    ;
    j = a[0];
    while (j >= (-30)) v[30 + j] = 0, j--;
    i = 1;
    while (i < n) {
      p[i] = -1;
      j = a[i];
      while (j >= (-30)) v[30 + j] = i, j--;
      if (a[i] == 30) {
        i++;
        continue;
      }
      j = a[i] + 1;
      while (j <= 30) {
        if (v[30 + j] >= 0) {
          p[i] = v[30 + j];
          break;
        }
        j++;
      }
      i++;
    }
    i = n - 2;
    for (int i = 0; i < 61; i++) v[i] = -1;
    ;
    v[30 + a[n - 1]] = n - 1;
    j = a[n - 1];
    while (j >= (-30)) v[30 + j] = n - 1, j--;
    nx[n - 1] = -1;
    while (i >= 0) {
      nx[i] = -1;
      j = a[i];
      while (j >= (-30)) v[30 + j] = i, j--;
      if (a[i] == 30) {
        i--;
        continue;
      }
      j = a[i] + 1;
      while (j <= 30) {
        if (v[30 + j] >= 0) {
          nx[i] = v[30 + j];
          break;
        }
        j++;
      }
      i--;
    }
    i = 0;
    long long ans = 0;
    while (i < n) {
      long long sp = 0, sn = 0;
      if (i > 0) {
        j = p[i];
        sp = s1[i - 1] - getmn(j, i - 1, n, 1);
      }
      if (i < n - 1) {
        j = nx[i];
        sn = s2[i + 1] - getmn(i + 1, j, n, 0);
      }
      ans = max(ans, sp + sn);
      i++;
    }
    cout << ans << endl;
  }
  return 0;
}
