#include <bits/stdc++.h>
int n, cnt = 0;
int a[100054];
long long ans = 0;
inline void up(long long &x, const long long y) { x < y ? x = y : 0; }
namespace S {
struct node {
  int sz, p, c[2];
  long long v, tag_k, tag_b;
} nd[100054];
inline int dir(int x) { return x == nd[x].p[nd].c[1]; }
inline void cover(int x, long long k, long long b) {
  nd[x].tag_k += k, nd[x].tag_b += b, nd[x].v += k * nd[x].c[0][nd].sz + b;
}
inline void push_down(int x) {
  long long &k = nd[x].tag_k, &b = nd[x].tag_b;
  if (!(k || b)) return;
  int l = nd[x].c[0], r = nd[x].c[1];
  if (l) cover(l, k, b);
  if (r) cover(r, k, b + (nd[l].sz + 1) * k);
  k = b = 0;
}
void pull_down(int x) {
  if (nd[x].p != 100054 - 1) pull_down(nd[x].p);
  push_down(x);
}
inline void update(int x) {
  nd[x].sz = nd[x].c[0][nd].sz + nd[x].c[1][nd].sz + 1;
}
void rotate(int x) {
  int y = nd[x].p, d = !dir(x);
  nd[nd[y].c[!d] = nd[x].c[d]].p = y;
  nd[x].p = nd[y].p;
  nd[y].p[nd].c[dir(y)] = x;
  nd[nd[x].c[d] = y].p = x;
  update(y);
}
void splay(int x, int g = 100054 - 1) {
  for (; nd[x].p != g; rotate(x))
    if (nd[x].p[nd].p != g) rotate(dir(x) ^ dir(nd[x].p) ? x : nd[x].p);
  update(x);
}
int kth(int x, int k) {
  assert(k <= nd[x].sz);
  for (int j;; x = nd[x].c[k > j], k -= j - k >> 31 & j + 1)
    if (push_down(x), k == (j = nd[x].c[0][nd].sz) + 1) return splay(x), x;
}
void push_back(int &r, long long value, int p = 100054 - 1) {
  if (!r) {
    nd[r = ++cnt].p = p, nd[cnt].v = value, nd[cnt].sz = 1;
    return;
  }
  int x = r;
  for (; push_down(x), nd[x].c[1]; x = nd[x].c[1])
    ;
  splay(x, p);
  nd[nd[x].c[1] = ++cnt].p = x, nd[cnt].v = value, nd[cnt].sz = 1, update(x);
}
inline void insert_before(int x, long long value) {
  splay(x), push_back(nd[x].c[0], value, x), update(x);
}
void traversal(int x) {
  push_down(x), up(ans, nd[x].v);
  if (nd[x].c[0]) traversal(nd[x].c[0]);
  if (nd[x].c[1]) traversal(nd[x].c[1]);
}
void debug(int x) {
  push_down(x);
  if (nd[x].c[0]) debug(nd[x].c[0]);
  fprintf(stderr, "    #%d: val %lld, size %d, p %d, lc %d, rc %d\n", x,
          nd[x].v, nd[x].sz, nd[x].p, nd[x].c[0], nd[x].c[1]);
  if (nd[x].c[1]) debug(nd[x].c[1]);
}
}  // namespace S
inline long long f(int x) {
  return x ? S::nd[S::kth(S::nd[100054 - 1].c[0], x)].v : 0;
}
int main() {
  int i, L, R, M, g;
  long long t;
  scanf("%d", &n);
  for (i = 1; i <= n; ++i) scanf("%d", a + i);
  S::nd[100054 - 1].sz = 1;
  for (i = 1; i <= n; ++i) {
    for (L = 1, R = i; L < R;
         f(M - 1) + (long long)M * a[i] <= f(M) ? (L = M + 1) : (R = M))
      M = (L + R) / 2;
    t = f(L - 1);
    if (L == i)
      S::push_back(S::nd[100054 - 1].c[0], t + (long long)L * a[i]);
    else {
      L == 1 ? S::insert_before(g = S::kth(S::nd[100054 - 1].c[0], 1), a[i])
             : S::insert_before(g = S::kth(S::nd[100054 - 1].c[0], L - 1), t);
      S::splay(cnt);
      assert(g == S::nd[cnt].c[1]);
      S::cover(g, a[i], (L == 1 ? 2ll : (long long)L) * a[i]);
    }
  }
  assert(cnt == n), S::traversal(S::nd[100054 - 1].c[0]);
  printf("%lld\n", ans);
}
