#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n;
int ch[N][2], rnd[N], sz[N], tot, root;
long long tag[N], val[N];
int newnode(long long w = 0) {
  ++tot;
  (ch[tot][0]) = (ch[tot][1]) = 0;
  rnd[tot] = rand();
  sz[tot] = 1;
  tag[tot] = 0;
  val[tot] = w;
  return tot;
}
void maketag(int x, long long w) {
  val[x] += w;
  tag[x] += w;
}
void pushdown(int x) {
  if (!tag[x]) return;
  if ((ch[x][0])) maketag((ch[x][0]), tag[x]);
  if ((ch[x][1])) maketag((ch[x][1]), tag[x]);
  tag[x] = 0;
}
void pushup(int x) { sz[x] = 1 + sz[(ch[x][0])] + sz[(ch[x][1])]; }
void split(int x, int k, int &l, int &r) {
  if (!x) {
    l = r = 0;
    return;
  }
  pushdown(x);
  if (sz[(ch[x][0])] >= k) {
    r = x;
    split((ch[x][0]), k, l, (ch[x][0]));
  } else {
    l = x;
    split((ch[x][1]), k - sz[(ch[x][0])] - 1, (ch[x][1]), r);
  }
  pushup(x);
}
int merge(int x, int y) {
  if (!x || !y) return x | y;
  if (rnd[x] > rnd[y]) {
    pushdown(x);
    (ch[x][1]) = merge((ch[x][1]), y);
    pushup(x);
    return x;
  } else {
    pushdown(y);
    (ch[y][0]) = merge(x, (ch[y][0]));
    pushup(y);
    return y;
  }
}
void work(int pos, long long w) {
  int a, b;
  split(root, pos - 1, a, b);
  b = merge(newnode(w * (pos - 1)), b);
  maketag(b, w);
  root = merge(a, b);
}
long long ask(int x, int k) {
  pushdown(x);
  if (sz[(ch[x][0])] >= k)
    return ask((ch[x][0]), k);
  else if (sz[(ch[x][0])] + 1 == k)
    return val[x];
  else
    return ask((ch[x][1]), k - sz[(ch[x][0])] - 1);
}
int find_pos(long long w) {
  int l = 1, r = sz[root], mid;
  if (r == 0 || ask(root, r) >= w * r) return r + 1;
  while (l < r) {
    mid = (l + r) >> 1;
    if (ask(root, mid) >= w * mid)
      l = mid + 1;
    else
      r = mid;
  }
  return l;
}
long long dp[N];
void smash(int x, long long *&w) {
  if (!x) return;
  pushdown(x);
  smash((ch[x][0]), w);
  *(++w) = val[x];
  smash((ch[x][1]), w);
}
inline int read() {
  int x = 0;
  char c = getchar();
  bool f = 0;
  while (c < '0' || c > '9') f |= (c == '-'), c = getchar();
  while (c >= '0' && c <= '9') x = x * 10 + (c ^ 48), c = getchar();
  return f ? -x : x;
}
int main() {
  n = read();
  for (int i = 1; i <= n; ++i) {
    int w = read(), pos = find_pos(w);
    work(pos, w);
  }
  long long *_ = dp, ans = 0;
  smash(root, _);
  for (int i = 1; i <= n; ++i) dp[i] += dp[i - 1], ans = max(ans, dp[i]);
  cout << ans << endl;
  return 0;
}
