#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
struct node {
  int son[2], fa, siz;
  long long sumd, d, laz;
} tr[N];
int root, n, id;
inline void update(int x) {
  tr[x].siz = tr[tr[x].son[0]].siz + tr[tr[x].son[1]].siz + 1;
  tr[x].sumd = tr[tr[x].son[0]].sumd + tr[x].d + tr[tr[x].son[1]].sumd;
  return;
}
void rotate(int x) {
  int f1 = tr[x].fa, f2 = tr[f1].fa;
  int w1 = (tr[f2].son[0] == f1 ? 0 : 1), w2 = (tr[f1].son[0] == x ? 0 : 1);
  tr[f2].son[w1] = x;
  tr[x].fa = tr[f1].fa;
  tr[tr[x].son[w2 ^ 1]].fa = f1;
  tr[f1].son[w2] = tr[x].son[w2 ^ 1];
  tr[x].son[w2 ^ 1] = f1;
  tr[f1].fa = x;
  update(tr[x].son[w2 ^ 1]);
  update(x);
  return;
}
void splay(int x, int aim) {
  while (tr[x].fa != aim) {
    if (tr[tr[x].fa].fa == aim)
      rotate(x);
    else {
      int a1 = tr[tr[x].fa].fa;
      if (tr[tr[a1].son[0]].son[0] == x || tr[tr[a1].son[1]].son[1] == x)
        rotate(tr[x].fa), rotate(x);
      else
        rotate(x), rotate(x);
    }
  }
  if (aim == 0) root = x;
  return;
}
inline void pushdown(int x) {
  if (tr[x].laz == 0) return;
  if (tr[x].son[0]) {
    tr[tr[x].son[0]].laz += tr[x].laz;
    tr[tr[x].son[0]].sumd += 1ll * tr[tr[x].son[0]].siz * tr[x].laz;
    tr[tr[x].son[0]].d += tr[x].laz;
  }
  if (tr[x].son[1]) {
    tr[tr[x].son[1]].laz += tr[x].laz;
    tr[tr[x].son[1]].sumd += 1ll * tr[tr[x].son[1]].siz * tr[x].laz;
    tr[tr[x].son[1]].d += tr[x].laz;
  }
  tr[x].laz = 0;
  return;
}
inline void insert(int aim, long long val) {
  int now = root;
  while (1) {
    pushdown(now);
    if (aim <= tr[tr[now].son[0]].siz)
      now = tr[now].son[0];
    else if (aim <= tr[tr[now].son[0]].siz + 1) {
      int op = ++id;
      tr[op].fa = now;
      tr[op].son[1] = tr[now].son[1];
      tr[op].d = val;
      if (tr[now].son[1]) tr[tr[now].son[1]].fa = op;
      tr[now].son[1] = op;
      update(op);
      update(now);
      splay(op, 0);
      return;
    } else
      aim -= (tr[tr[now].son[0]].siz + 1), now = tr[now].son[1];
  }
  return;
}
inline long long query(int aim) {
  long long res = 0;
  int now = root;
  while (1) {
    pushdown(now);
    if (aim <= tr[tr[now].son[0]].siz)
      now = tr[now].son[0];
    else if (aim <= tr[tr[now].son[0]].siz + 1) {
      res += tr[tr[now].son[0]].sumd + tr[now].d;
      splay(now, 0);
      return res;
    } else
      res += (tr[tr[now].son[0]].sumd + tr[now].d),
          aim -= (tr[tr[now].son[0]].siz + 1), now = tr[now].son[1];
  }
  return -1;
}
inline int get_kth(int aim) {
  int now = root;
  while (now) {
    pushdown(now);
    if (aim <= tr[tr[now].son[0]].siz)
      now = tr[now].son[0];
    else if (aim <= tr[tr[now].son[0]].siz + 1) {
      splay(now, 0);
      return now;
    } else
      aim -= tr[tr[now].son[0]].siz + 1, now = tr[now].son[1];
  }
  return -1;
}
inline void add(int l, int r, long long val) {
  if (r == tr[root].siz) {
    int p1 = get_kth(l - 1);
    splay(p1, 0);
    tr[tr[root].son[1]].d += val;
    tr[tr[root].son[1]].sumd += 1ll * tr[tr[root].son[1]].siz * val;
    tr[tr[root].son[1]].laz += val;
    update(root);
  } else {
    int p1 = get_kth(l - 1), p2 = get_kth(r + 1);
    splay(p1, 0);
    splay(p2, p1);
    int p3 = tr[p2].son[0];
    tr[p3].d += val;
    tr[p3].sumd += 1ll * tr[p3].siz * val;
    tr[p3].laz += val;
    update(p2);
    update(p1);
  }
  return;
}
inline long long getmax(int x) {
  long long res = -1e18;
  pushdown(x);
  if (tr[x].son[0]) res = max(res, getmax(tr[x].son[0]));
  if (tr[x].son[1])
    res = max(res, getmax(tr[x].son[1]) + tr[tr[x].son[0]].sumd + tr[x].d);
  res = max(res, tr[tr[x].son[0]].sumd + tr[x].d);
  return res;
}
int main() {
  scanf("%d", &n);
  root = ++id;
  for (int i = 1; i <= n; i++) {
    int v;
    scanf("%d", &v);
    int l = 0, r = i - 2, res = i - 1;
    while (l <= r) {
      int mid = (l + r) >> 1;
      long long op1 = query(mid + 1), op2 = query(mid + 2);
      if ((op1 + 1ll * (mid + 1) * v) > op2)
        res = mid, r = mid - 1;
      else
        l = mid + 1;
    }
    insert(res + 1, 0);
    add(res + 2, res + 2, 1ll * (res + 1) * v);
    if ((res + 3) <= i + 1) add(res + 3, i + 1, v);
  }
  printf("%lld", getmax(root));
  return 0;
}
