#include <bits/stdc++.h>
using namespace std;
const int MAXM = 316 + 10;
const int MAXN = 100000 + 10;
const long long INF = 0x3ffffff;
long long ans;
int n, m, T;
long long b[MAXN];
int q[MAXN], bz[MAXN];
long long K[MAXM], B[MAXM];
int l[MAXM], r[MAXM], a[MAXN], p[MAXN], ql[MAXM], qr[MAXM];
int getw(int x) { return (x + m - 1) / m; }
double slope(int x, int y) {
  if (a[x] == a[y]) return b[x] > b[y] ? -INF : INF;
  return (double)(b[y] - b[x]) / (a[y] - a[x]);
}
void build(int w) {
  ql[w] = l[w], qr[w] = l[w] - 1;
  for (int i = l[w]; i <= r[w]; i++)
    if (!bz[p[i]]) {
      while (ql[w] < qr[w] &&
             slope(q[qr[w] - 1], q[qr[w]]) < slope(q[qr[w]], p[i]))
        qr[w]--;
      q[++qr[w]] = p[i];
    }
}
pair<long long, int> query(int w) {
  while (ql[w] < qr[w] && slope(q[ql[w]], q[ql[w] + 1]) >= -K[w]) ql[w]++;
  if (ql[w] > qr[w]) return make_pair(-INF, 0);
  return make_pair(K[w] * a[q[ql[w]]] + b[q[ql[w]]] + B[w], q[ql[w]]);
}
bool cmp(int x, int y) { return a[x] < a[y]; }
int main() {
  scanf("%d", &n), m = sqrt(n), T = (n + m - 1) / m;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    p[i] = i, b[i] = a[i];
  }
  for (int i = 1; i <= T; i++) {
    l[i] = (i - 1) * m + 1, r[i] = min(i * m, n);
    sort(p + l[i], p + r[i] + 1, cmp);
    build(i);
  }
  while (1) {
    pair<long long, int> now = make_pair(0, 0);
    for (int i = 1; i <= T; i++) now = max(now, query(i));
    if (now.first <= 0) break;
    ans += now.first;
    int pos = now.second, w = getw(pos);
    for (int i = 1; i < w; i++) B[i] += a[pos];
    for (int i = w + 1; i <= T; i++) K[i]++;
    for (int i = l[w]; i < pos; i++) b[i] += a[pos];
    for (int i = pos + 1; i <= r[w]; i++) b[i] += a[i];
    bz[pos] = 1, build(w);
  }
  printf("%lld\n", ans);
}
