#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
const ll INF = 1e18;
const ll MAX = 1e5;
const ll MOD = 1e9 + 7;
const ll LOG = 20;
ll arr[MAX + 5] = {-INF};
struct node {
  ll val[4] = {};
  node(ll v = 0, ll i = 0) {
    if (v > 0) val[2] = val[3] = v;
    val[0] = v;
    val[1] = i;
  }
  friend node operator+(node& l, node& r) {
    node ret;
    ret.val[0] = l.val[0] + r.val[0];
    ret.val[1] = arr[l.val[1]] > arr[r.val[1]] ? l.val[1] : r.val[1];
    ret.val[2] = max(l.val[2] + r.val[0], r.val[2]);
    ret.val[3] = max(l.val[3], l.val[0] + r.val[3]);
    return ret;
  }
};
struct seg {
  ll base;
  vector<node> v;
  seg(ll n, ll* arr = NULL) {
    base = 1;
    while (base < n) base <<= 1;
    v.resize(base << 1);
    if (arr == NULL) return;
    for (ll i = 1; i <= n; ++i) v[base + i - 1] = node(arr[i], i);
    for (ll i = base - 1; i; --i) {
      v[i] = v[i << 1] + v[i << 1 | 1];
    }
  }
  node query(ll l, ll r) { return query(l, r, 1, base, 1); }
  node query(ll l, ll r, ll s, ll e, ll vi) {
    if (l <= s && e <= r) return v[vi];
    if (r < s || e < l) return 0;
    ll m = s + e >> 1;
    node lval = query(l, r, s, m, vi << 1);
    node rval = query(l, r, m + 1, e, vi << 1 | 1);
    return lval + rval;
  }
};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll n;
  cin >> n;
  for (ll i = 1; i <= n; ++i) cin >> arr[i];
  seg tree(n, arr);
  queue<pll> q;
  q.emplace(1, n);
  ll ans = 0;
  while (!q.empty()) {
    ll a, b;
    tie(a, b) = q.front();
    q.pop();
    if (a >= b) continue;
    ll idx = tree.query(a, b).val[1];
    q.emplace(a, idx - 1);
    q.emplace(idx + 1, b);
    ans =
        max(ans, tree.query(a, idx - 1).val[2] + tree.query(idx + 1, b).val[3]);
  }
  cout << ans;
  return 0;
}
