#include <bits/stdc++.h>
using namespace std;
vector<long long> val;
vector<long long> pref;
vector<long long> sum;
long long divide(long long a, long long b) {
  long long delta = 0;
  if (a % b != 0) {
    delta = 1;
  }
  if ((a > 0 && b > 0) || (a < 0 && b < 0)) {
    return (a / b) + delta;
  } else {
    return (a / b);
  }
}
long long getAns(int l, int r) {
  if (r - l == 1) {
    return max(0ll, val[l]);
  }
  int m = (l + r) / 2;
  long long ans = max(getAns(l, m), getAns(m, r));
  vector<pair<long long, long long>> av;
  for (int i = m; i < r; i++) {
    long long t = sum[i];
    long long s = pref[i];
    if (m >= 1) {
      s -= pref[m - 1];
      t -= sum[m - 1];
      t -= (long long)(m)*s;
    }
    av.push_back({s, t});
  }
  sort(av.begin(), av.end());
  vector<pair<long long, long long>> v;
  vector<long long> border;
  v.push_back(av[0]);
  for (int i = 1; i < av.size(); i++) {
    while (v.size() >= 2 && (v[v.size() - 1].first == av[i].first ||
                             divide(av[i].second - v[v.size() - 1].second,
                                    v[v.size() - 1].first - av[i].first) <=
                                 border[border.size() - 1])) {
      v.pop_back();
      border.pop_back();
    }
    if (v.size() == 1 && v[0].first == av[i].first) {
      v.pop_back();
      v.push_back(av[i]);
      continue;
    }
    border.push_back(divide(av[i].second - v[v.size() - 1].second,
                            v[v.size() - 1].first - av[i].first));
    v.push_back(av[i]);
  }
  for (int i = l; i < m; i++) {
    int _l = 0;
    int _r = v.size();
    while (_r - _l > 1) {
      int _m = (_l + _r) / 2;
      if ((long long)(m - i) * v[_m].first + v[_m].second >
          (long long)(m - i) * v[_m - 1].first + v[_m - 1].second) {
        _l = _m;
      } else {
        _r = _m;
      }
    }
    long long t = sum[m - 1];
    long long s = pref[m - 1];
    if (i >= 1) {
      t -= sum[i - 1];
      s -= pref[i - 1];
      t -= (long long)(i)*s;
    }
    ans = max(ans, t + (m - i) * v[_l].first + v[_l].second);
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  val.resize(n);
  pref.resize(n);
  sum.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> val[i];
    if (i != 0) {
      pref[i] = pref[i - 1];
      sum[i] = sum[i - 1];
    }
    pref[i] += val[i];
    sum[i] += (long long)(i + 1) * val[i];
  }
  cout << getAns(0, n);
  cin >> n;
  return 0;
}
