#include <bits/stdc++.h>
using namespace std;
int read() {
  int X = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') w = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') X = X * 10 + c - '0', c = getchar();
  return X * w;
}
const int N = 100000 + 10, M = 350;
int n, a[N], p[N];
int B, bl[N], L[M], R[M];
long long w[N], k[M], b[M];
int q[N], qh[M], qt[M], del[N];
double slope(int i, int j) {
  if (a[i] == a[j])
    return w[i] < w[j] ? 1e50 : -1e50;
  else
    return 1.0 * (w[j] - w[i]) / (a[j] - a[i]);
}
long long calc(int x) { return w[x] + k[bl[x]] * a[x] + b[bl[x]]; }
void build(int x) {
  for (int i = L[x]; i <= R[x]; ++i) w[i] += k[x] * a[i] + b[x];
  k[x] = b[x] = 0;
  int h = L[x], t = L[x] - 1;
  for (int i = L[x]; i <= R[x]; ++i) {
    if (del[p[i]]) continue;
    while (h < t && slope(q[t - 1], q[t]) < slope(q[t], p[i])) --t;
    q[++t] = p[i];
  }
  qh[x] = h, qt[x] = t;
}
pair<long long, int> qmax(int x) {
  int &h = qh[x], t = qt[x];
  while (h < t && calc(q[h]) < calc(q[h + 1])) ++h;
  if (h > t)
    return make_pair(0ll, 0);
  else
    return make_pair(calc(q[h]), q[h]);
}
int main() {
  n = read();
  B = sqrt(n);
  for (int i = 1; i <= n; ++i) a[i] = read();
  for (int i = 1; i <= n; ++i) bl[i] = (i - 1) / B + 1;
  for (int i = 1; i <= bl[n]; ++i) {
    L[i] = (i - 1) * B + 1, R[i] = min(i * B, n);
    for (int j = L[i]; j <= R[i]; ++j) p[j] = j, w[j] = a[j];
    sort(p + L[i], p + R[i] + 1, [](int x, int y) { return a[x] < a[y]; });
    build(i);
  }
  long long ans = 0;
  while (1) {
    pair<long long, int> mx = make_pair(0ll, 0);
    for (int i = 1; i <= bl[n]; ++i) mx = max(mx, qmax(i));
    if (!mx.first) break;
    ans += mx.first;
    int p = mx.second;
    for (int i = 1; i < bl[p]; ++i) b[i] += a[p];
    for (int i = L[bl[p]]; i < p; ++i) w[i] += a[p];
    for (int i = p + 1; i <= R[bl[p]]; ++i) w[i] += a[i];
    for (int i = bl[p] + 1; i <= bl[n]; ++i) ++k[i];
    del[p] = 1;
    build(bl[p]);
  }
  printf("%lld\n", ans);
  return 0;
}
