#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
struct Line {
  long long k, b;
  Line() {}
  Line(long long k, long long b) : k(k), b(b) {}
  bool operator<(const Line& a) const { return k != a.k ? k < a.k : b < a.b; }
  long long f(int x) { return k * x + b; }
};
char Ma[MAXN << 1];
int R[MAXN << 1];
Line a[MAXN];
long long sum[MAXN];
long long val[MAXN];
long long tot[MAXN];
char s[MAXN];
int id[MAXN];
int c[MAXN];
int S[MAXN];
long long ans;
int n;
int check(int i, int j, int k) {
  return (double)(a[i].b - a[j].b) / (a[j].k - a[i].k) >=
         (double)(a[i].b - a[k].b) / (a[k].k - a[i].k);
}
void merge(int a[], int n, int b[], int m) {
  int i = 0, j = 0, k = 0, t = 0;
  while (i < n && j < m) c[k++] = sum[a[i]] < sum[b[j]] ? a[i++] : b[j++];
  while (i < n) c[k++] = a[i++];
  while (j < m) c[k++] = b[j++];
  while (t < n + m) a[t] = c[t], t++;
}
void dfs(int l, int r) {
  if (l == r) return;
  int m = l + r >> 1;
  dfs(l, m);
  dfs(m + 1, r);
  int cnt = 0, t = 0;
  tot[m] = 0;
  for (int i = m + 1; i <= r; ++i) {
    tot[i] = tot[i - 1] + (i - m) * val[i];
  }
  for (int i = m + 1; i <= r; ++i) {
    a[++cnt] = Line(sum[id[i]] - sum[m], tot[id[i]]);
  }
  for (int i = 1; i <= cnt; ++i) {
    if (i > 1 && a[i - 1].k == a[i].k && a[i - 1].b >= a[i].b) continue;
    while (t && a[S[t]].k == a[i].k || t > 1 && check(S[t - 1], S[t], i)) --t;
    S[++t] = i;
  }
  long long res = 0;
  for (int i = m, j = 1, k = 1; i >= l; --i, ++j) {
    res += sum[m] - sum[i - 1];
    while (k < t && a[S[k]].f(j) < a[S[k + 1]].f(j)) ++k;
    ans = max(ans, res + a[S[k]].f(j));
  }
  merge(id + l, m - l + 1, id + m + 1, r - m);
}
void manacher(char s[], int n) {
  int l = 0;
  Ma[l++] = '$';
  Ma[l++] = '#';
  for (int i = 0; i < n; ++i) {
    Ma[l++] = s[i];
    Ma[l++] = '#';
  }
  Ma[l] = 0;
  for (int i = 0, mx = 0, id = 0; i < l; ++i) {
    R[i] = mx > i ? min(R[2 * id - i], mx - i) : 1;
    while (Ma[i + R[i]] == Ma[i - R[i]]) R[i]++;
    if (i + R[i] > mx) mx = i + R[i], id = i;
  }
}
void solve() {
  ans = 0;
  for (int i = 1; i <= n; ++i) {
    scanf("%lld", &val[i]);
    sum[i] = sum[i - 1] + val[i];
    ans = max(ans, val[i]);
    id[i] = i;
  }
  dfs(1, n);
  printf("%lld\n", ans);
}
int main() {
  scanf("%d", &n);
  solve();
  return 0;
}
