#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, block = 400;
const long long inf = 1e18;
int n, m;
int a[N], L[N], R[N], bel[N];
long long f[N], ans;
inline bool cmp(int x, int y) { return a[x] < a[y]; }
struct B {
  int l, r, q[block + 5], p[block + 5], cnt;
  long long k, b;
  inline long long calc(int x) { return k * a[x] + f[x] + b; }
  inline double slope(int i, int j) {
    if (a[i] == a[j]) return f[i] > f[j] ? -inf : inf;
    return 1.0 * (f[i] - f[j]) / (a[i] - a[j]);
  }
  inline void init(int x) {
    cnt = 0;
    for (int i = L[x]; i <= R[x]; i++) p[++cnt] = i;
    sort(p + 1, p + cnt + 1, cmp);
  }
  inline void rebuild(int x) {
    for (int i = L[x]; i <= R[x]; i++) f[i] += k * a[i] + b;
    k = b = 0;
    l = 1, r = 0;
    for (int i = 1; i <= cnt; i++) {
      while (l < r && slope(q[r - 1], q[r]) < slope(q[r], p[i])) r--;
      q[++r] = p[i];
    }
  }
  inline int query() {
    while (l < r && calc(q[l]) <= calc(q[l + 1])) l++;
    return q[l];
  }
} az[block];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    f[i] = a[i];
    bel[i] = (i - 1) / block + 1;
    if (!L[bel[i]]) L[bel[i]] = i;
    R[bel[i]] = i;
  }
  m = bel[n];
  for (int i = 1; i <= m; i++) az[i].init(i), az[i].rebuild(i);
  while (1) {
    long long res = 0, pos = 0;
    for (int i = 1; i <= m; i++) {
      int t = az[i].query();
      long long v = az[i].calc(t);
      if (v > res) res = v, pos = t;
    }
    if (!pos) break;
    ans += res;
    f[pos] = -inf;
    for (int i = L[bel[pos]]; i < pos; i++) f[i] += a[pos];
    for (int i = pos + 1; i <= R[bel[pos]]; i++) f[i] += a[i];
    for (int i = 1; i < bel[pos]; i++) az[i].b += a[pos];
    for (int i = bel[pos] + 1; i <= m; i++) az[i].k++;
    az[bel[pos]].rebuild(bel[pos]);
  }
  printf("%lld", ans);
  return 0;
}
