#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int s = 0, t = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') t = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar();
  return s * t;
}
const int N = 1e5 + 5;
int n, tot, rt;
int c[N][2], prt[N], sz[N];
long long Sum, Ans, a[N], s[N], d[N];
void Pushup(int x) {
  sz[x] = sz[c[x][0]] + sz[c[x][1]] + 1;
  s[x] = s[c[x][0]] + s[c[x][1]] + a[x];
}
void Init(int x, long long t) { a[x] += t, s[x] += t * sz[x], d[x] += t; }
void Pushdown(int x) {
  if (!d[x]) return;
  if (c[x][0]) Init(c[x][0], d[x]);
  if (c[x][1]) Init(c[x][1], d[x]);
  d[x] = 0;
}
void rot(int x, int &f) {
  int y = prt[x], z = prt[y];
  int l = c[y][0] != x, r = l ^ 1;
  if (y == f)
    f = x;
  else
    c[z][c[z][0] != y] = x;
  prt[x] = z, prt[y] = x, prt[c[x][r]] = y;
  c[y][l] = c[x][r], c[x][r] = y;
  Pushup(y), Pushup(x);
}
void Splay(int x, int &f) {
  while (x ^ f) {
    int y = prt[x], z = prt[y];
    if (y ^ f) Pushdown(z);
    Pushdown(y), Pushdown(x);
    if (y ^ f) {
      if ((c[y][0] == x) ^ (c[z][0] == y))
        rot(x, f);
      else
        rot(y, f);
    }
    rot(x, f);
  }
}
int Find(int k) {
  for (int x = rt; x;) {
    Pushdown(x);
    if (sz[c[x][0]] + 1 == k) return x;
    if (k <= sz[c[x][0]])
      x = c[x][0];
    else
      k -= sz[c[x][0]] + 1, x = c[x][1];
  }
}
void Upd(int l, int r, long long v) {
  if (l == 1) {
    Init(rt, v);
    return;
  }
  Splay(Find(l - 1), rt);
  Init(c[rt][1], v), Pushup(rt);
}
int Node(long long v) {
  int x = ++tot;
  return sz[x] = 1, s[x] = a[x] = v, x;
}
void Ins(int k, long long v) {
  if (!rt) {
    rt = Node(v);
    return;
  }
  for (int x = rt, y = x, tp = 0; x;) {
    Pushdown(x), sz[x]++, s[x] += v;
    tp = !(k <= sz[c[x][0]] + 1), y = x;
    if (k <= sz[c[x][0]] + 1)
      x = c[x][0];
    else
      k -= sz[c[x][0]] + 1, x = c[x][1];
    if (!x) prt[c[y][tp] = Node(v)] = y, Splay(tot, rt);
  }
}
void Trans(int i, int v) {
  int res = i;
  for (int x = rt, tp = 0; x;) {
    Pushdown(x);
    int j = tp + sz[c[x][0]] + 1;
    if (1ll * j * v >= a[x])
      res = j, x = c[x][0];
    else
      tp += sz[c[x][0]] + 1, x = c[x][1];
  }
  Ins(res, 1ll * v * res);
  if (res ^ i) Upd(res + 1, i, v);
}
void Solve(int x) {
  Pushdown(x);
  if (c[x][0]) Solve(c[x][0]);
  Sum += a[x], Ans = max(Ans, Sum);
  if (c[x][1]) Solve(c[x][1]);
}
int main() {
  n = read();
  for (int i = 1; i <= n; i++) Trans(i, read());
  Solve(rt);
  printf("%lld", Ans);
  return 0;
}
