#include <bits/stdc++.h>
inline int read() {
  char c = getchar();
  int x = 0, r = 1;
  while (c < '0' || c > '9') {
    if (c == '-') r = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - 48, c = getchar();
  return x * r;
}
const int maxn = 1e5 + 5;
int n, tot, rt, cnt, num;
long long rsum, ans;
int ch[maxn][2], fa[maxn], sz[maxn], a[maxn], st[maxn];
long long tag[maxn], v[maxn], bl[maxn];
inline void pushup(int x) {
  sz[x] = 1;
  if (ch[x][0]) sz[x] += sz[ch[x][0]];
  if (ch[x][1]) sz[x] += sz[ch[x][1]];
}
inline void pushr(int x, long long va) {
  if (!x) return;
  v[x] += va;
  tag[x] += va;
}
inline void pushdown(int x) {
  if (!tag[x] || !x) return;
  pushr(ch[x][0], tag[x]);
  pushr(ch[x][1], tag[x]);
  tag[x] = 0;
}
inline void rotate(int x) {
  int y = fa[x], z = fa[y], k = ch[y][1] == x, w = ch[x][k ^ 1];
  ch[x][k ^ 1] = y, ch[y][k] = w, ch[z][ch[z][1] == y] = x;
  fa[w] = y, fa[y] = x, fa[x] = z;
  pushup(y), pushup(x);
}
inline void splay(int x, int goal) {
  int y = x;
  int top = 0;
  while (y) st[++top] = y, y = fa[y];
  while (top) pushdown(st[top--]);
  while (fa[x] != goal) {
    int y = fa[x];
    if (fa[y] != goal) rotate((ch[y][1] == x) ^ (ch[fa[y]][1] == y) ? x : y);
    rotate(x);
  }
  if (!goal) rt = x;
}
inline int kth(int k) {
  int x = rt;
  while (1) {
    pushdown(x);
    if (sz[ch[x][0]] + 1 == k) {
      splay(x, 0);
      return x;
    }
    if (sz[ch[x][0]] >= k)
      x = ch[x][0];
    else if (sz[ch[x][0]] + 1 < k)
      k -= sz[ch[x][0]] + 1, x = ch[x][1];
  }
}
inline void ins(int x, long long va) {
  x++;
  v[++tot] = va, sz[tot] = 1;
  int A = kth(x), B = kth(x - 1);
  splay(A, 0), splay(B, A);
  ch[B][1] = tot, fa[tot] = B;
  splay(tot, 0);
}
inline void add(int l, int r, long long va) {
  l++, r++;
  int A = kth(l - 1), B = kth(r + 1);
  splay(A, 0), splay(B, A);
  pushr(ch[B][0], va);
  splay(ch[B][0], 0);
}
void dfs(int x) {
  if (!x) return;
  pushdown(x);
  dfs(ch[x][0]);
  bl[num++] = v[x];
  dfs(ch[x][1]);
}
inline void doit() {
  num = 0;
  dfs(rt);
  num--;
  for (register int i = 1; i < num; ++i) printf("%lld ", bl[i]);
  puts("");
  puts("");
}
int main() {
  n = read();
  for (register int i = 1; i <= n; i++) a[i] = read();
  for (register int i = n; i; i--)
    if (a[i] >= 0) rsum += a[i], ans += rsum;
  tot = 2;
  fa[2] = 1, ch[1][1] = 2;
  sz[1] = 2, sz[2] = 1;
  rt = 1;
  int nl = 1;
  for (register int i = 1; i <= n; i++) {
    if (a[i] >= 0) {
      ++cnt, rsum -= a[i];
      continue;
    }
    long long va = 1ll * a[i] * (cnt + nl) + rsum;
    int l = 1, r = nl - 1, nw = 0;
    long long K = 1ll * a[i] * cnt + rsum;
    while (l <= r) {
      int mid = l + r >> 1, id = kth(mid + 1);
      if (1ll * mid * a[i] + K > v[id])
        r = mid - 1, nw = mid;
      else
        l = mid + 1;
    }
    if (!nw) {
      ins(nl, va), ++nl;
      continue;
    }
    ins(nw, 1ll * nw * a[i] + K);
    ++nl;
    add(nw + 1, nl - 1, a[i]);
  }
  dfs(rt);
  long long tmp = 0;
  num--;
  for (register int i = 1; i < num; i++)
    bl[i] += bl[i - 1], tmp = ((tmp) > (bl[i]) ? (tmp) : (bl[i]));
  printf("%lld\n", tmp + ans);
  return 0;
}
