#include <bits/stdc++.h>
using namespace std;
const long long MAGIC = 400;
const long long N = 1e5 + 2;
const long long inf = 1e14 + 2;
long long lzval[MAGIC], lzmul[MAGIC], ar[N], add[N];
bool used[N];
vector<long long> idx[MAGIC];
struct hull {
  long long ptr;
  struct line {
    long long a, b, idx;
    long long y(long long x) { return a * x + b; }
  };
  vector<line> hull;
  bool bad(line x, line y, line z) {
    assert(y.a != x.a);
    if (z.a == y.a) {
      return z.b > y.b;
    }
    return (1.0 * (x.b - z.b)) / (z.a - x.a) <
           (1.0 * (y.b - z.b)) / (z.a - y.a);
  }
  void insert(line a) {
    while (hull.size() > 1) {
      if (bad(hull[hull.size() - 2], hull[hull.size() - 1], a)) {
        hull.pop_back();
      } else {
        break;
      }
    }
    if (hull.size() && hull.back().a == a.a) {
      if (hull.back().b > a.b) {
        return;
      } else {
        hull.pop_back();
      }
    }
    hull.push_back(a);
  }
  void build(vector<long long> &wow) {
    hull.clear();
    ptr = 0;
    for (long long i = 0; i < wow.size(); i++) {
      long long idx = wow[i];
      if (used[idx]) {
        continue;
      }
      line tmp;
      tmp.a = ar[idx];
      tmp.b = add[idx];
      tmp.idx = idx;
      insert(tmp);
    }
  }
  pair<long long, long long> get(long long idx) {
    while (ptr + 1 < hull.size() &&
           hull[ptr].y(lzmul[idx]) < hull[ptr + 1].y(lzmul[idx])) {
      ptr++;
    }
    return pair<long long, long long>(hull[ptr].y(lzmul[idx]) + lzval[idx],
                                      -hull[ptr].idx);
  }
} lis[MAGIC];
long long grp(long long x) { return (x + MAGIC - 1) / MAGIC; }
bool cmp(long long x, long long y) {
  if (ar[x] != ar[y]) {
    return ar[x] > ar[y];
  }
  return add[x] > add[y];
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long n, i, j, k, l, mx, ans = 0, mxans = 0, z, q;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> ar[i];
    add[i] = ar[i];
    idx[grp(i)].push_back(i);
  }
  mx = grp(n);
  for (i = 1; i <= mx; i++) {
    sort(idx[i].begin(), idx[i].end(), cmp);
    lis[i].build(idx[i]);
  }
  for (i = 1; i <= n; i++) {
    pair<long long, long long> res = {-inf, -1};
    for (j = 1; j <= mx; j++) {
      if (lis[j].hull.size()) {
        res = max(res, lis[j].get(j));
        pair<long long, long long> kek = lis[j].get(j);
      }
    }
    ans += res.first;
    mxans = max(mxans, ans);
    q = j = -res.second;
    used[j] = true;
    j = grp(j);
    for (k = j + 1; k <= mx; k++) {
      lzmul[k]++;
    }
    for (k = 1; k < j; k++) {
      lzval[k] += ar[q];
    }
    k = (j - 1) * MAGIC + 1, l = min(j * MAGIC, n);
    for (z = k; z <= l; z++) {
      add[z] += lzval[j] + lzmul[j] * ar[z];
      if (z >= q) {
        add[z] += ar[z];
      } else {
        add[z] += ar[q];
      }
    }
    lzval[j] = lzmul[j] = 0;
    lis[j].build(idx[j]);
  }
  cout << mxans;
}
