#include <bits/stdc++.h>
const int N = 1e5 + 5;
int fa[N], ch[N][2], sz[N];
long long A[N], B[N], val[N];
int n, a, rt, cnt;
inline int ckr(int x, const int p = 1) { return ch[fa[x]][p] == x; }
inline void update(int x) {
  const int L = ch[x][0], R = ch[x][1];
  sz[x] = sz[L] + sz[R] + 1;
}
inline void modify(int x, long long a, long long b) {
  val[x] += a * (sz[ch[x][0]] + 1) + b, A[x] += a, B[x] += b;
}
inline void pushdown(int x) {
  if (A[x] || B[x]) {
    if (ch[x][0]) modify(ch[x][0], A[x], B[x]);
    if (ch[x][1]) modify(ch[x][1], A[x], B[x] + A[x] * (sz[ch[x][0]] + 1));
    A[x] = B[x] = 0;
  }
}
inline void rotate(int x) {
  int y = fa[x], z = fa[y], k = ckr(x);
  if (z) ch[z][ckr(y)] = x;
  fa[x] = z, fa[y] = x, fa[ch[x][!k]] = y, ch[y][k] = ch[x][!k], ch[x][!k] = y,
  update(y);
}
inline void splay(int x) {
  static int sta[N], top;
  sta[top = 1] = x;
  for (int y = x; fa[y]; sta[++top] = y = fa[y])
    ;
  while (top) pushdown(sta[top--]);
  for (; fa[x]; rotate(x))
    if (fa[fa[x]]) rotate((ckr(x) ^ ckr(fa[x])) ? x : fa[x]);
  update(rt = x);
}
inline long long getv(int k) {
  int x = rt;
  while (1) {
    if (sz[ch[x][0]] + 1 == k) {
      splay(x);
      return val[x];
    }
    if (sz[ch[x][0]] >= k)
      x = ch[x][0];
    else
      k -= sz[ch[x][0]] + 1, x = ch[x][1];
  }
}
long long chkmax(int x) {
  if (!x) return -1e18;
  pushdown(x);
  return std::max(val[x], std::max(chkmax(ch[x][0]), chkmax(ch[x][1])));
}
int main() {
  scanf("%d", &n);
  sz[1] = rt = cnt = 1;
  for (int i = 1, a; i <= n; ++i) {
    scanf("%d", &a);
    int l = 0, r = i - 2, ans = i - 1;
    while (l <= r) {
      const int mid = l + r >> 1;
      if (getv(mid + 1) + (mid + 1LL) * a > getv(mid + 2))
        r = (ans = mid) - 1;
      else
        l = mid + 1;
    }
    getv(ans + 1);
    fa[++cnt] = rt, fa[ch[rt][1]] = cnt, ch[cnt][1] = ch[rt][1],
    ch[rt][1] = cnt, val[cnt] = val[rt];
    modify(cnt, a, (long long)a * ans);
  }
  printf("%lld\n", chkmax(rt));
  return 0;
}
