#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
static inline void amin(T &x, U y) {
  if (y < x) x = y;
}
template <typename T, typename U>
static inline void amax(T &x, U y) {
  if (y > x) x = y;
}
const long long int mod = 998244353;
const long long inf = (long long int)1e18 + 17;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
long long int tree[500010], a[100010], pre[100010], tree1[500010];
void build(int node, int start, int end) {
  if (start == end) {
    tree[node] = pre[start];
  } else {
    int mid = (start + end) / 2;
    build(2 * node, start, mid);
    build(2 * node + 1, mid + 1, end);
    tree[node] = min(tree[2 * node], tree[2 * node + 1]);
  }
}
void update(int node, int start, int end, int idx, int val) {
  if (start == end) {
    a[idx] += val;
    tree[node] += val;
  } else {
    int mid = (start + end) / 2;
    if (start <= idx and idx <= mid) {
      update(2 * node, start, mid, idx, val);
    } else {
      update(2 * node + 1, mid + 1, end, idx, val);
    }
    tree[node] = max(tree[2 * node], tree[2 * node + 1]);
  }
}
int query(int node, int start, int end, int l, int r) {
  if (r < start or end < l) {
    return 10000000;
  }
  if (l <= start and end <= r) {
    return tree[node];
  }
  int mid = (start + end) / 2;
  int p1 = query(2 * node, start, mid, l, r);
  int p2 = query(2 * node + 1, mid + 1, end, l, r);
  return min(p1, p2);
}
void mbuild(int node, int start, int end) {
  if (start == end) {
    tree1[node] = pre[start];
  } else {
    int mid = (start + end) / 2;
    mbuild(2 * node, start, mid);
    mbuild(2 * node + 1, mid + 1, end);
    tree1[node] = max(tree1[2 * node], tree1[2 * node + 1]);
  }
}
int mquery(int node, int start, int end, int l, int r) {
  if (r < start or end < l) {
    return -10000000;
  }
  if (l <= start and end <= r) {
    return tree1[node];
  }
  int mid = (start + end) / 2;
  int p1 = mquery(2 * node, start, mid, l, r);
  int p2 = mquery(2 * node + 1, mid + 1, end, l, r);
  return max(p1, p2);
}
long long int lef[100010], rgt[100010];
void lefsmall(long long int *arr, int n) {
  stack<pair<int, int>> S;
  int ind = 0;
  for (int i = 0; i < n; i++) {
    while (!S.empty() && S.top().first <= arr[i]) S.pop();
    if (S.empty())
      lef[ind++] = 0;
    else
      lef[ind++] = S.top().second + 1;
    S.push({arr[i], i});
  }
}
void rgtsmall(long long int *arr, int n) {
  stack<pair<int, int>> S;
  int ind = n - 1;
  for (int i = n - 1; i >= 0; i--) {
    while (!S.empty() && S.top().first <= arr[i]) S.pop();
    if (S.empty())
      rgt[ind--] = n - 1;
    else
      rgt[ind--] = S.top().second - 1;
    S.push({arr[i], i});
  }
}
void solve() {
  long long int n;
  cin >> n;
  long long int b[n + 10];
  pre[0] = 0;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) pre[i] = pre[i - 1] + a[i - 1];
  build(1, 0, n);
  mbuild(1, 0, n);
  lefsmall(a, n);
  rgtsmall(a, n);
  long long int ans = -1;
  for (int i = 0; i < n; i++) {
    int aa, b, val1 = pre[i], val2 = pre[i + 1];
    aa = lef[i];
    b = i - 1;
    aa++;
    b++;
    if (b >= aa) {
      amin(val1, query(1, 0, n, aa - 1, b - 1));
    }
    aa = i + 1;
    b = rgt[i];
    aa++;
    b++;
    if (b >= aa) {
      amax(val2, mquery(1, 0, n, aa, b));
    }
    amax(ans, val2 - val1 - a[i]);
  }
  cout << ans << endl;
}
int main() {
  clock_t tt;
  tt = clock();
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  tt = clock() - tt;
  double time_taken = ((double)tt) / CLOCKS_PER_SEC;
  return 0;
}
