#include <bits/stdc++.h>
using namespace std;
long long read() {
  long long a = 0, fh = 1;
  char c = getchar();
  while (c > '9' || c < '0') {
    if (c == '-') fh = -1;
    c = getchar();
  }
  while ('0' <= c && c <= '9') {
    a = a * 10 + c - 48;
    c = getchar();
  }
  return a * fh;
}
long long T, n, m, blk;
long long loc(long long first) { return (first - 1) / blk + 1; }
long long a[200005], b[200005], tagk[455], tagb[455], id[200005];
long long st[455][455], top[455], pos[455];
bool use[200005];
bool cmp(long long first, long long second) { return a[first] < a[second]; }
double getx(long long i, long long j) {
  return 1.0 * (b[i] - b[j]) / (a[j] - a[i]);
}
void build(long long first) {
  long long l = (first - 1) * blk + 1, r = min(first * blk, n);
  top[first] = 0;
  pos[first] = 1;
  for (long long i = l; i <= r; ++i) {
    if (use[id[i]]) continue;
    b[id[i]] += tagk[first] * a[id[i]] + tagb[first];
    if (top[first] && a[st[first][top[first]]] == a[id[i]] &&
        b[st[first][top[first]]] > b[id[i]])
      continue;
    while (top[first] && a[st[first][top[first]]] == a[id[i]] &&
           b[st[first][top[first]]] <= b[id[i]])
      top[first]--;
    while (top[first] > 1 &&
           getx(st[first][top[first] - 1], st[first][top[first]]) >=
               getx(st[first][top[first] - 1], id[i]))
      top[first]--;
    st[first][++top[first]] = id[i];
  }
  tagk[first] = tagb[first] = 0;
}
long long _f(long long id, long long first) { return a[id] * first + b[id]; }
pair<long long, long long> getmx(long long first) {
  if (!top[first]) return make_pair(0, 0);
  while (pos[first] < top[first] &&
         _f(st[first][pos[first]], tagk[first]) <
             _f(st[first][pos[first] + 1], tagk[first]))
    pos[first]++;
  return make_pair(_f(st[first][pos[first]], tagk[first]) + tagb[first],
                   st[first][pos[first]]);
}
signed main() {
  n = read();
  for (long long i = 1; i <= n; ++i) {
    b[i] = a[i] = read();
  }
  blk = sqrt(n);
  T = (n - 1) / blk + 1;
  for (long long i = 1; i <= n; ++i) id[i] = i;
  for (long long i = 1; i <= T; ++i) {
    long long l = (i - 1) * blk + 1, r = min(i * blk, n);
    sort(id + l, id + r + 1, cmp);
    build(i);
  }
  long long ans = 0;
  while (1) {
    pair<long long, long long> res = make_pair(0, 0);
    for (long long i = 1; i <= T; ++i) {
      pair<long long, long long> w = getmx(i);
      if (res < w) res = w;
    }
    if (!res.second) break;
    ans += res.first;
    use[res.second] = 1;
    long long first = loc(res.second);
    for (long long i = 1; i < first; ++i) tagb[i] += a[res.second];
    for (long long i = first + 1; i <= T; ++i) tagk[i]++;
    for (long long i = (first - 1) * blk + 1; i < res.second; ++i)
      b[i] += a[res.second];
    for (long long i = res.second + 1; i <= first * blk && i <= n; ++i)
      b[i] += a[i];
    build(first);
  }
  printf("%lld\n", ans);
  return 0;
}
