#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  return x * f;
}
long long n, cnt = 0, root = 0;
long long a[500050];
struct Splay {
  long long val;
  long long sz, fa, son[2];
  long long k, b;
} t[500050];
inline void Push_Up(register long long x) {
  t[x].sz = t[t[x].son[0]].sz + t[t[x].son[1]].sz + 1;
}
inline void Push_Down(register long long x) {
  if (t[x].k || t[x].b) {
    if (t[x].son[0]) {
      t[t[x].son[0]].val += t[x].k * (t[t[t[x].son[0]].son[0]].sz + 1) + t[x].b;
      t[t[x].son[0]].k += t[x].k;
      t[t[x].son[0]].b += t[x].b;
    }
    if (t[x].son[1]) {
      t[t[x].son[1]].val += t[x].k * (t[t[t[x].son[1]].son[0]].sz + 1) +
                            t[x].b + t[x].k * (t[t[x].son[0]].sz + 1);
      t[t[x].son[1]].k += t[x].k;
      t[t[x].son[1]].b += t[x].b + t[x].k * (t[t[x].son[0]].sz + 1);
    }
    t[x].k = t[x].b = 0;
  }
}
inline void Rotate(register long long x, register long long p) {
  register long long fx = t[x].fa;
  Push_Down(fx);
  Push_Down(x);
  t[fx].son[p ^ 1] = t[x].son[p];
  t[t[x].son[p]].fa = fx;
  t[x].fa = t[fx].fa;
  if (t[x].fa) t[t[x].fa].son[t[t[x].fa].son[1] == fx] = x;
  t[x].son[p] = fx;
  t[fx].fa = x;
  Push_Up(fx);
  Push_Up(x);
}
inline void Splay(register long long x, register long long to) {
  while (t[x].fa != to) {
    if (t[t[x].fa].fa == to)
      Rotate(x, t[t[x].fa].son[0] == x);
    else {
      register long long fa = t[x].fa;
      register long long ffa = t[fa].fa;
      long long p = (t[ffa].son[0] == fa);
      if (t[fa].son[p] == x) {
        Rotate(x, p ^ 1);
        Rotate(x, p);
      } else {
        Rotate(fa, p);
        Rotate(x, p);
      }
    }
  }
  if (to == 0) root = x;
}
inline long long Getval(long long k) {
  if (root == 0) return 0;
  long long x = root;
  while (t[t[x].son[0]].sz != k - 1) {
    if (t[t[x].son[0]].sz >= k)
      x = t[x].son[0];
    else {
      k -= t[t[x].son[0]].sz + 1;
      x = t[x].son[1];
    }
  }
  Splay(x, 0);
  return t[x].val;
}
inline void Newnode() {
  t[++cnt].fa = root;
  t[t[root].son[1]].fa = cnt;
  t[cnt].son[1] = t[root].son[1];
  t[root].son[1] = cnt;
  t[cnt].val = t[root].val;
  Push_Up(cnt);
  Push_Up(root);
}
inline bool check(long long mid, long long i) {
  return (Getval(mid) + (mid)*a[i] <= Getval(mid + 1));
}
inline long long solve(long long x) {
  if (!x) return -1e18;
  Push_Down(x);
  long long ans = max((long long)-1e18, t[x].val);
  if (t[x].son[0]) ans = max(ans, solve(t[x].son[0]));
  if (t[x].son[1]) ans = max(ans, solve(t[x].son[1]));
  return ans;
}
inline long long randint() { return (rand() << 15 | rand()); }
inline long long randint(long long l, long long r) {
  return (randint() & 1 ? -1 : 1) * (randint() % (r + 1) + l);
}
int main() {
  srand(time(0));
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (long long i = 1; i <= n; i++) cin >> a[i];
  t[1].sz = root = cnt = 1;
  for (long long i = 1; i <= n; i++) {
    long long left = 0, right = i - 2, ans = i - 1;
    while (left <= right) {
      long long mid = (left + right) >> 1;
      if (check(mid + 1, i))
        left = mid + 1;
      else {
        ans = mid;
        right = mid - 1;
      }
    }
    Getval(ans + 1);
    Newnode();
    t[cnt].val += a[i] * (t[t[cnt].son[0]].sz + 1) + a[i] * ans;
    t[cnt].k += a[i];
    t[cnt].b += a[i] * ans;
  }
  cout << solve(root) << endl;
  return 0;
}
