#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  char ch = getchar();
  long long s = 0, w = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    s = s * 10 + ch - '0';
    ch = getchar();
  }
  return s * w;
}
long long Stack[200010], top;
long long n, a[200010], rt, cnt;
long long ch[200010][2], fa[200010], size[200010], A[200010], B[200010],
    val[200010];
inline void pushup(long long x) {
  size[x] = size[ch[x][0]] + size[ch[x][1]] + 1;
}
inline void add(long long x, long long a, long long b) {
  val[x] += a * (size[ch[x][0]] + 1) + b;
  A[x] += a;
  B[x] += b;
}
inline void pushdown(long long x) {
  if (ch[x][0]) add(ch[x][0], A[x], B[x]);
  if (ch[x][1]) add(ch[x][1], A[x], B[x] + A[x] * (size[ch[x][0]] + 1));
  A[x] = B[x] = 0;
}
inline void rotate(long long x) {
  long long y = fa[x], z = fa[y], opt = (ch[y][1] == x);
  if (z) ch[z][ch[z][1] == y] = x;
  fa[x] = z;
  fa[y] = x;
  fa[ch[x][opt ^ 1]] = y;
  ch[y][opt] = ch[x][opt ^ 1];
  ch[x][opt ^ 1] = y;
  pushup(y);
  pushup(x);
}
inline void Splay(long long x) {
  for (register long long i = x; i; i = fa[i]) Stack[++top] = i;
  while (top) pushdown(Stack[top--]);
  while (fa[x]) {
    long long y = fa[x], z = fa[fa[x]];
    if (z) {
      if ((ch[y][0] == x) ^ (ch[z][0] == y))
        rotate(x);
      else
        rotate(y);
    }
    rotate(x);
  }
  rt = x;
}
inline long long qv(long long k) {
  long long x = rt;
  while (true) {
    if (size[ch[x][0]] + 1 == k) {
      Splay(x);
      return val[x];
    }
    if (size[ch[x][0]] >= k)
      x = ch[x][0];
    else
      k -= size[ch[x][0]] + 1, x = ch[x][1];
  }
}
long long qans(long long x) {
  if (!x) return -1e9;
  pushdown(x);
  return max(val[x], max(qans(ch[x][0]), qans(ch[x][1])));
}
int main() {
  n = read();
  rt = cnt = size[1] = 1;
  for (register long long i = 1; i <= n; i++) {
    a[i] = read();
    long long l = 0, r = i - 2, ans = i - 1;
    while (l <= r) {
      long long mid = (l + r) >> 1;
      if (qv(mid + 1) + (mid + 1) * a[i] > qv(mid + 2))
        ans = mid, r = mid - 1;
      else
        l = mid + 1;
    }
    qv(ans + 1);
    fa[++cnt] = rt;
    fa[ch[rt][1]] = cnt;
    ch[cnt][1] = ch[rt][1];
    ch[rt][1] = cnt;
    val[cnt] = val[rt];
    add(cnt, a[i], a[i] * ans);
  }
  printf("%lld\n", qans(rt));
  return 0;
}
