10
-3 4 -4 1 1 -4 -5 3 -4 -2
 10
8 9 -10 -10 -4 -3 5 9 -9 9
 6
5 -4 2 -1 2 -2
 11
3 0 1 -2 5 -5 -1 0 3 2 2
 10
4 -2 -2 -2 2 0 1 0 -4 -1
 4
2 2 -3 4
 11
-11 -7 -2 -24 30 -26 24 9 -8 -9 23
 6
30 -29 5 5 -29 30
 24
-8 -6 6 -11 -15 -7 10 -19 -6 21 14 -4 -21 -10 12 17 1 -1 5 19 2 0 -4 -23
 10
-9 -6 -1 9 -2 0 0 -1 5 0
 6
10 -8 3 3 -8 10
 10
-2 -3 -1 4 -3 2 0 1 -5 4
 5
30 -29 10 -1 10
 31
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -29 30
 7
30 -20 5 1 3 -20 30
 5
30 -29 4 -3 4
 10
-5 28 -18 -10 9 -2 11 -6 -19 30
 8
4 -10 13 -13 2 2 -11 6
 131
30 -29 29 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 28 -28 27 -26 27
 10
-4 4 0 -2 3 0 1 2 3 -4
 10
-2 -8 5 4 -1 -6 -9 6 1 -6
 7
25 -17 13 13 -25 25 -24
 10
3 -3 2 1 -4 -2 3 -2 -3 0
 10
4 -5 0 4 0 0 -3 1 1 -4
 10
0 4 4 1 -3 4 2 1 4 -1
 17
15 -13 7 8 8 1 -19 12 8 -23 -4 16 -13 -28 10 -30 -14
 4
1 1 -4 10
 11
3 -2 1 1 1 1 1 1 1 -3 4
 7
30 -29 10 10 10 -29 30
 7
25 -13 17 -23 12 12 9
 7
15 -14 10 10 10 -14 15
 10
3 0 0 -4 3 1 -2 -1 -1 4
 6
8 -7 7 -6 5 5
 10
1 -4 -4 4 -2 -3 3 0 -2 3
 15
1 -26 -6 26 7 10 -2 -4 -6 14 -9 25 9 -12 -28
 6
30 -29 10 10 -29 30
 1
6
 2
1 1
 5
0 5 -4 3 -1
 7
5 -4 3 -1 3 -4 4
 10
16 -15 13 -1 10 -2 -20 26 5 -30
 10
-1 2 0 -1 2 -1 4 3 -5 1
 7
5 -4 1 1 1 -4 5
 5
10 -3 -5 3 5
 10
4 -4 3 -2 3 -4 -2 0 1 0
 10
-5 -5 -3 1 -2 2 0 2 -3 4
 10
2 -4 -2 3 1 1 -2 -1 -3 4
 10
-4 -5 4 -3 2 -2 2 1 -1 0
 10
0 -1 4 -2 3 -2 -1 -5 -5 2
 5
19 -6 1 -11 19
 10
1 0 -4 3 -5 3 -1 2 2 -5
 7
5 -5 3 4 3 -5 5
 10
-5 2 -3 -3 -4 3 3 0 2 4
 4
2 3 -10 11
 10
27 -17 15 -22 18 17 -6 2 16 -14
 6
30 -22 6 7 -22 30
 7
30 -25 0 6 9 -2 10
 9
7 2 -1 1 1 -10 6 -1 6
 4
1 -10 1 10
 5
17 -16 12 -11 15
 35
-23 -22 23 4 -22 29 -4 -6 -28 18 -5 21 -7 -8 -11 -7 30 -25 -1 12 19 5 -8 12 -1 10 -24 -19 24 -17 -7 24 20 -22 16
 10
4 -3 1 -2 1 1 0 -4 4 -2
 10
4 -2 1 0 1 -5 -2 1 0 -5
 2
3 9
 9
11 -5 -5 1 2 3 -5 -5 11

1
 8
 1
 4
 1
 2
 15
 5
 36
 2
 3
 1
 9
 28
 4
 1
 7
 2
 1
 7
 4
 13
 1
 1
 13
 16
 1
 6
 20
 21
 20
 1
 5
 1
 44
 10
 0
 1
 0
 2
 9
 5
 2
 3
 1
 2
 2
 1
 1
 3
 3
 6
 8
 2
 29
 6
 13
 5
 1
 1
 30
 1
 1
 3
 3

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const long long MOD2 = 998244353;
const long long BIG = 1197423052705914509LL;
mt19937 rng(
    (long long)std::chrono::steady_clock::now().time_since_epoch().count());
const long long MAXN = 2e5 + 10;
const long long is_query = -BIG;
struct line {
  long long m, b;
  mutable function<const line*()> succ;
  bool operator<(const line& rhs) const {
    if (rhs.b != is_query) return m < rhs.m;
    const line* s = succ();
    if (!s) return 0;
    long long x = rhs.m;
    return b - s->b < (s->m - m) * x;
  }
};
struct dynamic_hull : public multiset<line> {
  const long long inf = BIG;
  bool bad(iterator y) {
    auto z = next(y);
    if (y == begin()) {
      if (z == end()) return 0;
      return y->m == z->m && y->b <= z->b;
    }
    auto x = prev(y);
    if (z == end()) return y->m == x->m && y->b <= x->b;
    long long v1 = (x->b - y->b);
    if (y->m == x->m)
      v1 = x->b > y->b ? inf : -inf;
    else
      v1 /= (y->m - x->m);
    long long v2 = (y->b - z->b);
    if (z->m == y->m)
      v2 = y->b > z->b ? inf : -inf;
    else
      v2 /= (z->m - y->m);
    return v1 >= v2;
  }
  void insert_line(long long m, long long b) {
    auto y = insert({m, b});
    y->succ = [=] { return next(y) == end() ? 0 : &*next(y); };
    if (bad(y)) {
      erase(y);
      return;
    }
    while (next(y) != end() && bad(next(y))) erase(next(y));
    while (y != begin() && bad(prev(y))) erase(prev(y));
  }
  long long eval(long long x) {
    auto l = *lower_bound((line){x, is_query});
    return l.m * x + l.b;
  }
};
struct auxiliary_tree {
  long long n;
  vector<long long> ori[MAXN];
  vector<long long> storelatest;
  long long tin[MAXN], tout[MAXN], dep[MAXN], cor[MAXN];
  bool imp[MAXN];
  long long run = 0;
  vector<long long> euler;
  vector<vector<pair<long long, long long> > > lca_table;
  long long lef[MAXN], rig[MAXN];
  void dfs(long long node, long long prev, long long de) {
    dep[node] = de;
    tin[node] = ++run;
    cor[tin[node]] = node;
    euler.push_back(node);
    for (long long i = 0; i < ori[node].size(); i++) {
      if (ori[node][i] != prev) {
        dfs(ori[node][i], node, de + 1);
        euler.push_back(node);
      }
    }
    tout[node] = ++run;
  }
  void find_all_lcas() {
    long long k = log2(2 * n - 1);
    lca_table.resize(k + 1);
    for (long long i = 0; i < k + 1; i++) {
      lca_table[i].resize(2 * n - 1);
    }
    for (long long i = 0; i < 2 * n - 1; i++) {
      lca_table[0][i] = make_pair(dep[euler[i]], euler[i]);
    }
    for (long long i = 1; i <= k; i++) {
      for (long long j = 0; j < 2 * n - 1; j++) {
        if (j + (1 << (i - 1)) >= 2 * n - 1) continue;
        if (lca_table[i - 1][j].first <
            lca_table[i - 1][j + (1 << (i - 1))].first) {
          lca_table[i][j].first = lca_table[i - 1][j].first;
          lca_table[i][j].second = lca_table[i - 1][j].second;
        } else {
          lca_table[i][j].first = lca_table[i - 1][j + (1 << (i - 1))].first;
          lca_table[i][j].second = lca_table[i - 1][j + (1 << (i - 1))].second;
        }
      }
    }
    for (long long i = 0; i < MAXN; i++) lef[i] = -1;
    for (long long i = 0; i < 2 * n - 1; i++) {
      if (lef[euler[i]] == -1) {
        lef[euler[i]] = i;
      }
      rig[euler[i]] = i;
    }
  }
  bool isParent(long long u, long long v) {
    return tin[u] < tin[v] && tout[v] < tout[u];
  }

 public:
  vector<long long> adj[MAXN];
  long long root;
  long long lcadep(long long x, long long y) {
    if (lef[x] > rig[y]) {
      swap(x, y);
    }
    long long k = log2(rig[y] - lef[x] + 1);
    return min(lca_table[k][lef[x]].first,
               lca_table[k][rig[y] - (1 << k) + 1].first);
  }
  long long lcaidx(long long x, long long y) {
    if (lef[x] > rig[y]) {
      swap(x, y);
    }
    long long k = log2(rig[y] - lef[x] + 1);
    if (lca_table[k][lef[x]].first <
        lca_table[k][rig[y] - (1 << k) + 1].first) {
      return lca_table[k][lef[x]].second;
    } else {
      return lca_table[k][rig[y] - (1 << k) + 1].second;
    }
  }
  void base(long long x, long long rt, vector<long long> y[]) {
    n = x;
    for (long long i = 1; i <= n; i++) {
      ori[i] = y[i];
    }
    dfs(rt, -1, 0);
    find_all_lcas();
  }
  void build(vector<long long> g) {
    for (long long i = 0; i < g.size(); i++) {
      g[i] = tin[g[i]];
    }
    sort(g.begin(), g.end());
    for (long long i = 0; i < g.size(); i++) {
      g[i] = cor[g[i]];
    }
    long long k = g.size();
    for (long long i = 0; i < k - 1; i++) {
      g.push_back(lcaidx(g[i], g[i + 1]));
    }
    for (long long i = 0; i < g.size(); i++) {
      g[i] = tin[g[i]];
    }
    sort(g.begin(), g.end());
    for (long long i = 0; i < g.size(); i++) {
      g[i] = cor[g[i]];
    }
    g.erase(unique(g.begin(), g.end()), g.end());
    for (long long i = 0; i < g.size(); i++) {
      imp[g[i]] = 1;
      storelatest.push_back(g[i]);
    }
    stack<long long> vert;
    vert.push(g[0]);
    for (long long i = 1; i < g.size(); i++) {
      long long u = g[i];
      while (vert.size() > 1 && isParent(vert.top(), u) == 0) {
        long long sto = vert.top();
        vert.pop();
        adj[vert.top()].push_back(sto);
      }
      vert.push(u);
    }
    while (vert.size() > 1) {
      long long sto = vert.top();
      vert.pop();
      adj[vert.top()].push_back(sto);
    }
    root = vert.top();
  }
  void clear() {
    for (long long i = 0; i < storelatest.size(); i++) {
      imp[storelatest[i]] = 0;
      adj[storelatest[i]].clear();
    }
    storelatest.clear();
  }
};
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t a) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(a + FIXED_RANDOM);
  }
  template <class T>
  size_t operator()(T a) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    hash<T> x;
    return splitmix64(x(a) + FIXED_RANDOM);
  }
  template <class T, class H>
  size_t operator()(pair<T, H> a) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    hash<T> x;
    hash<H> y;
    return splitmix64(x(a.f) * 37 + y(a.s) + FIXED_RANDOM);
  }
};
template <class T, class H>
using umap = unordered_map<T, H, custom_hash>;
void solve(long long test_case) {
  long long n;
  cin >> n;
  long long a[n + 1];
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
  }
  long long ans = 0;
  for (long long i = -30; i <= 30; i++) {
    vector<long long> v;
    for (long long j = 1; j <= n; j++) {
      if (a[j] == i) {
        long long L = j, R = j;
        while (L >= 2 && a[L - 1] <= i) L--;
        while (R <= n - 1 && a[R + 1] <= i) R++;
        v.push_back(0);
        for (long long k = L; k <= R; k++) v.push_back(a[k]);
        j = R;
        long long V = v.size() - 1;
        long long dp[V + 1];
        dp[0] = 0;
        for (long long k = 1; k <= V; k++) {
          dp[k] = max(0LL, dp[k - 1]) + v[k];
          ans = max(ans, dp[k] - i);
        }
        v.clear();
      }
    }
  }
  cout << ans << "\n";
}
int32_t main() {
  time_t t = clock();
  srand(time(NULL));
  long long T = 1;
  long long test_case = 1;
  while (T--) {
    solve(test_case);
    test_case++;
  }
  cerr << "Program terminated successfully\n";
  t = clock() - t;
  cerr << "Time used: " << fixed << setprecision(3)
       << (long double)(t * 1.0 / CLOCKS_PER_SEC) << " seconds\n";
}
