#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
long long p[200005], a[200005], n, sum[200005], cnt1, l1[200005], it1[200005];
void in1(long long x) {
  while (cnt1) {
    if (sum[it1[cnt1]] > sum[x]) {
      long long nl = (sum[it1[cnt1]] - sum[x]) / (x - it1[cnt1]) + 1;
      if (nl > l1[cnt1]) {
        l1[++cnt1] = nl;
        it1[cnt1] = x;
        return;
      }
    }
    cnt1--;
  }
  l1[++cnt1] = -1e18;
  it1[cnt1] = x;
}
long long cnt2, l2[200005], it2[200005];
void in2(long long x) {
  while (cnt2) {
    if (sum[it2[cnt2]] < sum[x]) {
      long long nl = (sum[it2[cnt2]] - sum[x]) / (it2[cnt2] - x);
      nl *= -1;
      if (nl > l2[cnt2]) {
        l2[++cnt2] = nl;
        it2[cnt2] = x;
        return;
      }
    } else
      return;
    cnt2--;
  }
  l2[++cnt2] = -1e18;
  it2[cnt2] = x;
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (long long i = 1; i <= n; i++) cin >> a[i];
  for (long long i = 1; i <= n; i++) p[i] = p[i - 1] + a[i];
  for (long long i = n; i >= 1; i--) {
    sum[i] = sum[i + 1] + p[n] - p[i - 1];
  }
  long long ans = 0;
  for (long long i = 1; i <= n; i++) {
    in1(i);
    in2(i);
    if (p[n] >= p[i]) {
      long long iter = upper_bound(l1 + 1, l1 + cnt1 + 1, p[n] - p[i]) - l1 - 1;
      long long s = it1[iter];
      ans = max(ans, sum[s] - sum[i + 1] - (i - s + 1) * (p[n] - p[i]));
    } else {
      long long iter = upper_bound(l2 + 1, l2 + cnt2 + 1, p[n] - p[i]) - l2 - 1;
      long long s = it2[iter];
      ans = max(ans, sum[s] - sum[i + 1] - (i - s + 1) * (p[n] - p[i]));
    }
  }
  cout << ans;
  return 0;
}
