#include <bits/stdc++.h>
using namespace std;
struct Splay {
  int cnt, rt;
  int fa[100005], ch[100005][2], sz[100005], st[100005];
  long long val[100005], tag1[100005], tag2[100005];
  bool get(int x) { return ch[fa[x]][1] == x; }
  void init() {
    val[0] = -1e18;
    rt = cnt = sz[1] = 1;
    return;
  }
  void push_up(int x) {
    sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + 1;
    return;
  }
  void update(int x, long long a, long long b) {
    val[x] += a * (sz[ch[x][0]] + 1) + b;
    tag1[x] += a;
    tag2[x] += b;
    return;
  }
  void push_down(int x) {
    if (!tag1[x] && !tag2[x]) return;
    long long a = tag1[x], b = tag2[x];
    tag1[x] = tag2[x] = 0;
    if (ch[x][0]) update(ch[x][0], a, b);
    if (ch[x][1]) update(ch[x][1], a, b + a * (sz[ch[x][0]] + 1));
    return;
  }
  void newnode() {
    val[++cnt] = val[rt];
    fa[cnt] = rt;
    ch[cnt][1] = ch[rt][1];
    fa[ch[rt][1]] = cnt;
    ch[rt][1] = cnt;
    push_up(cnt);
    push_up(rt);
    return;
  }
  void rotate(int x) {
    int y = fa[x], z = fa[y], tx = get(x), ty = get(y), p = ch[x][tx ^ 1];
    if (z) ch[z][ty] = x;
    fa[x] = z;
    if (p) fa[p] = y;
    ch[y][tx] = p;
    fa[y] = x;
    ch[x][tx ^ 1] = y;
    push_up(y);
    return;
  }
  void splay(int x) {
    int top = 0, u = x;
    st[++top] = u;
    while (u) st[++top] = u = fa[u];
    while (top) push_down(st[top--]);
    while (fa[x]) {
      int y = fa[x], tx = get(x), ty = get(y);
      if (fa[y]) rotate(tx == ty ? y : x);
      rotate(x);
    }
    push_up(x);
    rt = x;
    return;
  }
  int find(int k) {
    int x = rt;
    if (k > sz[rt]) return 0;
    while (true) {
      push_down(x);
      if (sz[ch[x][0]] + 1 == k) return splay(x), x;
      if (sz[ch[x][0]] >= k)
        x = ch[x][0];
      else
        k -= sz[ch[x][0]] + 1, x = ch[x][1];
    }
  }
  long long get_max(int x) {
    if (!x) return -100000000000000000;
    push_down(x);
    return max(val[x], max(get_max(ch[x][0]), get_max(ch[x][1])));
  }
  void dfs(int x) {
    push_down(x);
    if (ch[x][0]) dfs(ch[x][0]);
    cout << val[x] << ' ';
    if (ch[x][1]) dfs(ch[x][1]);
    return;
  }
} T;
int main() {
  int n;
  cin >> n;
  T.init();
  for (int i = 1; i <= n; ++i) {
    int v;
    scanf("%d", &v);
    int l = 1, r = i, res = 0;
    while (l <= r) {
      int mid = (l + r) >> 1, x = T.find(mid + 1), y = T.find(mid);
      if (T.val[x] <= T.val[y] + 1ll * v * mid)
        res = mid, r = mid - 1;
      else
        l = mid + 1;
    }
    T.find(res);
    T.newnode();
    T.update(T.cnt, v, 1ll * v * (res - 1));
  }
  cout << T.get_max(T.rt) << endl;
  return 0;
}
