#include <bits/stdc++.h>
using namespace std;
template <class T>
T gi() {
  T x = 0;
  bool f = 0;
  char c = getchar();
  while (c != '-' && (c < '0' || c > '9')) c = getchar();
  if (c == '-') f = 1, c = getchar();
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return f ? -x : x;
}
const int N = 1e5 + 10, M = 410;
const long long inf = 1e18;
long long f[N];
int a[N], n, T, bel[N], p[N];
struct node {
  long long k, b;
  int q[M], l, r, L, R;
  long long g(int x) { return f[x] + k * a[x] + b; }
  long double slope(int i, int j) {
    if (a[i] == a[j]) return f[i] > f[j] ? -inf : inf;
    return 1.0L * (f[i] - f[j]) / (a[i] - a[j]);
  }
  void build() {
    for (int i = l; i <= r; i++) f[i] += k * a[i] + b;
    k = b = 0;
    L = 1, R = 0;
    for (int i = l; i <= r; i++) {
      while (L < R && slope(q[R - 1], q[R]) < slope(q[R], p[i])) R--;
      q[++R] = p[i];
    }
  }
  pair<long long, int> ask() {
    while (L < R && g(q[L]) <= g(q[L + 1])) L++;
    return make_pair(g(q[L]), q[L]);
  }
} b[M];
int main() {
  n = gi<int>();
  T = sqrt(n);
  for (int i = 1; i <= n; i++) {
    f[i] = a[i] = gi<int>();
    p[i] = i;
    bel[i] = (i - 1) / T + 1;
  }
  long long ans = 0;
  for (int i = 1; i <= bel[n]; i++) {
    b[i].l = b[i - 1].r + 1;
    b[i].r = min(n, i * T);
    sort(p + b[i].l, p + 1 + b[i].r, [](int x, int y) { return a[x] < a[y]; });
    b[i].build();
  }
  while (1) {
    pair<long long, int> t = make_pair(0, 0);
    for (int i = 1; i <= bel[n]; i++) t = max(t, b[i].ask());
    if (!t.first) break;
    ans += t.first;
    f[t.second] = -inf;
    for (int i = 1; i < bel[t.second]; i++) b[i].b += a[t.second];
    for (int i = b[bel[t.second]].l; i < t.second; i++) f[i] += a[t.second];
    for (int i = t.second + 1; i <= b[bel[t.second]].r; i++) f[i] += a[i];
    for (int i = bel[t.second] + 1; i <= bel[n]; i++) b[i].k++;
    b[bel[t.second]].build();
  }
  printf("%lld\n", ans);
  return 0;
}
