#include <bits/stdc++.h>
inline long long read() {
  long long res = 0, bo = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') bo = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    res = (res << 1) + (res << 3) + ch - '0';
    ch = getchar();
  }
  return bo * res;
}
template <typename T>
void read(T &x) {
  x = read();
}
template <typename T, typename... Argv>
void read(T &a, Argv &...argv) {
  read(a);
  read(argv...);
}
const int N = 2e5 + 100;
int ch[N][2], fa[N], siz[N], root = 1, tot = 1;
long long val[N], tag_a[N], tag_b[N];
void Modify(int o, long long a, long long b) {
  tag_a[o] += a, tag_b[o] += b;
  val[o] += a + b * (siz[ch[o][0]] + 1);
}
void down(int o) {
  if (tag_b[o] || tag_a[o]) {
    Modify(ch[o][0], tag_a[o], tag_b[o]);
    Modify(ch[o][1], tag_a[o] + tag_b[o] * (siz[ch[o][0]] + 1), tag_b[o]);
    tag_a[o] = tag_b[o] = 0;
  }
}
void Down(int o) {
  if (fa[o]) Down(fa[o]);
  down(o);
}
int Get(int o) { return ch[fa[o]][1] == o; }
void up(int o) { siz[o] = siz[ch[o][0]] + siz[ch[o][1]] + 1; }
void rotate(int x) {
  int y = fa[x], z = fa[y], k = Get(x);
  if (z)
    ch[z][Get(y)] = x;
  else
    root = x;
  fa[x] = z;
  ch[y][k] = ch[x][!k];
  ch[x][!k] = y;
  fa[y] = x;
  fa[ch[y][k]] = y;
  up(y), up(x);
}
void Splay(int o) {
  Down(o);
  while (root != o) {
    if (fa[o] != root) rotate(Get(fa[o]) == Get(o) ? fa[o] : o);
    rotate(o);
  }
}
long long Kth(int k) {
  int o = root;
  while (1) {
    if (siz[ch[o][0]] + 1 == k) {
      Splay(o);
      return val[o];
    } else if (siz[ch[o][0]] + 1 > k)
      o = ch[o][0];
    else
      k -= siz[ch[o][0]] + 1, o = ch[o][1];
  }
  return 0;
}
int Add() {
  int o = ++tot, t = root;
  fa[o] = t;
  fa[ch[t][1]] = o;
  ch[o][1] = ch[t][1];
  ch[t][1] = o;
  val[o] = val[t];
  return o;
}
int Ask(int i, int a) {
  int ans = i - 1, l = 0, r = i - 2;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (Kth(mid + 1) + (mid + 1LL) * a > Kth(mid + 2))
      r = mid - 1, ans = mid;
    else
      l = mid + 1;
  }
  return ans;
}
long long Max(int o) {
  if (!o) return -1e18;
  down(o);
  return std::max(val[o], std::max(Max(ch[o][0]), Max(ch[o][1])));
}
int main() {
  int n = read();
  for (int i = 1; i <= n; ++i) {
    int a = read();
    int k = Ask(i, a) + 1;
    Kth(k);
    int o = Add();
    Modify(o, 1LL * (k - 1) * a, a);
  }
  std::cout << Max(root) << std::endl;
  return 0;
}
