#include <bits/stdc++.h>
using namespace std;
int i, l, r, mid, cnt, rot, n;
int fa[100005], ch[100005][2], s[100005];
long long a[100005], b[100005], val[100005];
long long x, ans;
int isr(int x) { return ch[fa[x]][1] == x; }
void up(int p) { s[p] = s[ch[p][0]] + s[ch[p][1]] + 1; }
void change(int p, long long aa, long long bb) {
  val[p] += aa * (s[ch[p][0]] + 1) + bb, a[p] += aa, b[p] += bb;
}
void down(int p) {
  if (a[p] || b[p]) {
    if (ch[p][0]) change(ch[p][0], a[p], b[p]);
    if (ch[p][1]) change(ch[p][1], a[p], b[p] + a[p] * (s[ch[p][0]] + 1));
    a[p] = b[p] = 0;
  }
}
void pb(int x) {
  if (fa[x]) pb(fa[x]);
  down(x);
}
void ro(int x) {
  int y = fa[x], z = fa[y], k = isr(x);
  if (z) ch[z][isr(y)] = x;
  fa[x] = z, fa[y] = x;
  fa[ch[x][!k]] = y;
  ch[y][k] = ch[x][!k];
  ch[x][!k] = y;
  up(y);
}
void splay(int x) {
  pb(x);
  for (; fa[x]; ro(x))
    if (fa[fa[x]]) ro(isr(x) ^ isr(fa[x]) ? x : fa[x]);
  up(rot = x);
}
long long kth(int k) {
  int p = rot;
  while (1) {
    if (k > s[ch[p][0]] + 1)
      k -= s[ch[p][0]] + 1, p = ch[p][1];
    else if (s[ch[p][0]] >= k)
      p = ch[p][0];
    else {
      splay(p);
      return val[p];
    }
  }
}
long long qry(int p) {
  if (p == 0) return -1 * 1926081719491001;
  down(p);
  return max(val[p], max(qry(ch[p][0]), qry(ch[p][1])));
}
int main() {
  scanf("%d", &n);
  s[1] = rot = cnt = 1;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &x);
    l = 0, r = i - 2, ans = i - 1;
    while (l <= r) {
      mid = l + r >> 1;
      if (kth(mid + 1) + (mid + 1) * x > kth(mid + 2))
        ans = mid, r = mid - 1;
      else
        l = mid + 1;
    }
    kth(ans + 1);
    fa[++cnt] = rot;
    fa[ch[rot][1]] = cnt;
    ch[cnt][1] = ch[rot][1];
    ch[rot][1] = cnt;
    val[cnt] = val[rot];
    change(cnt, x, x * ans);
  }
  printf("%lld\n", qry(rot));
}
