#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
const int NINF = -35 * MAXN;
int n;
int a[MAXN];
int seg[4 * MAXN], seg1[4 * MAXN];
int pre[MAXN], suf[MAXN];
int pge[MAXN], nge[MAXN];
void build(int node, int ss, int se) {
  if (ss > se) return;
  if (ss == se) {
    seg[node] = pre[ss];
    return;
  }
  int mid = ss + se >> 1;
  build(node * 2 + 1, ss, mid);
  build(node * 2 + 2, mid + 1, se);
  seg[node] = max(seg[node * 2 + 1], seg[node * 2 + 2]);
}
int query(int node, int ss, int se, int qs, int qe) {
  if (ss > se || qs > se || qe < ss) return NINF;
  if (ss >= qs && se <= qe) {
    return seg[node];
  }
  int mid = ss + se >> 1;
  int q1 = query(node * 2 + 1, ss, mid, qs, qe);
  int q2 = query(node * 2 + 2, mid + 1, se, qs, qe);
  return max(q1, q2);
}
void build1(int node, int ss, int se) {
  if (ss > se) return;
  if (ss == se) {
    seg1[node] = suf[ss];
    return;
  }
  int mid = ss + se >> 1;
  build1(node * 2 + 1, ss, mid);
  build1(node * 2 + 2, mid + 1, se);
  seg1[node] = max(seg1[node * 2 + 1], seg1[node * 2 + 2]);
}
int query1(int node, int ss, int se, int qs, int qe) {
  if (ss > se || qs > se || qe < ss) return NINF;
  if (ss >= qs && se <= qe) {
    return seg1[node];
  }
  int mid = ss + se >> 1;
  int q1 = query1(node * 2 + 1, ss, mid, qs, qe);
  int q2 = query1(node * 2 + 2, mid + 1, se, qs, qe);
  return max(q1, q2);
}
void findGe() {
  stack<int> s;
  for (int i = 1; i <= n; i++) {
    if (s.empty())
      s.push(i);
    else {
      while (!s.empty() && a[s.top()] < a[i]) {
        nge[s.top()] = i;
        s.pop();
      }
      s.push(i);
    }
  }
  while (!s.empty()) {
    nge[s.top()] = n + 1;
    s.pop();
  }
  for (int i = n; i >= 1; i--) {
    if (s.empty())
      s.push(i);
    else {
      while (!s.empty() && a[s.top()] < a[i]) {
        pge[s.top()] = i;
        s.pop();
      }
      s.push(i);
    }
  }
  while (!s.empty()) {
    pge[s.top()] = 0;
    s.pop();
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (auto(i) = (1); (i) <= (n); (i)++) cin >> a[i];
  for (auto(i) = (1); (i) <= (n); (i)++) pre[i] = pre[i - 1] + a[i];
  for (auto(i) = (n); (i) >= (1); (i)--) suf[i] = suf[i + 1] + a[i];
  build(0, 1, n);
  build1(0, 1, n);
  int ans = 0, temp = 0;
  findGe();
  for (auto(i) = (1); (i) <= (n); (i)++) {
    int r = query(0, 1, n, i, nge[i] - 1) - pre[i - 1];
    int l = query1(0, 1, n, pge[i] + 1, i) - suf[i + 1];
    ans = max(ans, l + r - a[i] * 2);
  }
  cout << ans;
}
