#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 10;
int n;
int arr[MAX], pref[MAX], suff[MAX], L[MAX], R[MAX];
int tree[4 * MAX][2];
void build(int node, int l, int r) {
  if (l == r) {
    tree[node][0] = pref[l];
    tree[node][1] = suff[l];
    return;
  }
  int mid = (l + r) >> 1;
  build(node << 1, l, mid);
  build(node << 1 | 1, mid + 1, r);
  tree[node][0] = max(tree[node << 1][0], tree[node << 1 | 1][0]);
  tree[node][1] = max(tree[node << 1][1], tree[node << 1 | 1][1]);
}
int query(int node, int l, int r, int from, int to, int t) {
  if (from > r || to < l) return -1e9;
  if (l >= from && r <= to) return tree[node][t];
  int mid = (l + r) >> 1;
  int a = query(node << 1, l, mid, from, to, t);
  int b = query(node << 1 | 1, mid + 1, r, from, to, t);
  return max(a, b);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> arr[i];
  stack<int> s;
  for (int i = 1; i <= n; ++i) {
    pref[i] = pref[i - 1] + arr[i];
    while (s.size() > 0 && arr[s.top()] <= arr[i]) s.pop();
    if (s.size())
      L[i] = s.top() + 1;
    else
      L[i] = 1;
    s.push(i);
  }
  while (s.size() > 0) s.pop();
  for (int i = n; i >= 1; --i) {
    suff[i] = suff[i + 1] + arr[i];
    while (s.size() > 0 && arr[s.top()] <= arr[i]) s.pop();
    if (s.size())
      R[i] = s.top() - 1;
    else
      R[i] = n;
    s.push(i);
  }
  build(1, 1, n);
  int ans = -1e9;
  for (int i = 1; i <= n; ++i) {
    int a = query(1, 1, n, i, R[i], 0) - pref[i];
    int b = query(1, 1, n, L[i], i, 1) - suff[i];
    ans = max(ans, a + b);
  }
  return cout << ans << "\n", 0;
}
