#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
const int oo = 0x3f3f3f3f;
const int maxn = 100000, max0 = 300, max1 = (maxn + max0 - 1) / max0;
int n, m;
long long a[maxn + 5];
int pre[maxn + 5];
int p[max0 + 5];
long long labelx[max0 + 5], labely[max0 + 5];
vector<int> all[max0 + 5];
long long first[maxn + 5], second[maxn + 5];
int st[max0 + 5], ed[max0 + 5];
bool pkd[maxn + 5];
inline long long cross(const int &a, const int &b, const int &c) {
  return (first[b] - first[a]) * (second[c] - second[a]) -
         (first[c] - first[a]) * (second[b] - second[a]);
}
inline long long calc(const int &a, const int &b) {
  return first[a] * labelx[b] + second[a] + labely[b];
}
inline void update(const int &first) {
  while (p[first] != -1 && pre[p[first]] != -1 &&
         calc(pre[p[first]], first) > calc(p[first], first))
    p[first] = pre[p[first]];
}
inline void build(const int &first) {
  labelx[first] = labely[first] = 0;
  p[first] = -1;
  for (auto i : all[first])
    if (!pkd[i]) {
      while (p[first] != -1 && pre[p[first]] != -1 &&
             cross(pre[p[first]], p[first], i) <= 0)
        p[first] = pre[p[first]];
      pre[i] = p[first], p[first] = i;
    }
  int u = p[first];
  while (u != -1) u = pre[u];
  update(first);
}
inline void addx(const int &first, const long long &second) {
  labelx[first] += second;
  update(first);
}
inline void addy(const int &first, const long long &second) {
  labely[first] += second;
}
inline bool cmp(const int &first, const int &second) {
  return ::first[first] > ::first[second];
}
int main() {
  scanf("%d", &n);
  for (int i = (0), _end_ = (n); i < _end_; ++i) scanf("%I64d", a + i);
  long long sum = 0;
  for (int i = n - 1; i >= 0; --i) {
    first[i] = a[i];
    second[i] = -a[i] * (i + 1) - sum;
    sum += a[i];
  }
  m = (n + max1 - 1) / max1;
  for (int i = (0), _end_ = (m); i < _end_; ++i) {
    st[i] = i * max1, ed[i] = min(n, (i + 1) * max1);
    all[i].clear();
    for (int j = (st[i]), _end_ = (ed[i]); j < _end_; ++j) all[i].push_back(j);
    sort((all[i]).begin(), (all[i]).end(), cmp);
    build(i);
  }
  long long now = 0;
  for (int i = (0), _end_ = (n); i < _end_; ++i) now += (i + 1) * a[i];
  long long ans = now;
  for (int i = (0), _end_ = (n); i < _end_; ++i) {
    long long Max = LLONG_MIN;
    int Maxp = -1, Maxp0 = -1;
    for (int j = (0), _end_ = (m); j < _end_; ++j)
      if (p[j] != -1)
        if (chkmax(Max, calc(p[j], j))) Maxp = p[j], Maxp0 = j;
    now += Max;
    chkmax(ans, now);
    pkd[Maxp] = 1;
    for (int j = (st[Maxp0]), _end_ = (ed[Maxp0]); j < _end_; ++j)
      if (!pkd[j]) {
        second[j] += labely[Maxp0] + labelx[Maxp0] * first[j];
        if (j < Maxp)
          second[j] += a[Maxp];
        else
          second[j] += a[j];
      }
    build(Maxp0);
    for (int j = (Maxp0 + 1), _end_ = (m); j < _end_; ++j) addx(j, 1);
    for (int j = (0), _end_ = (Maxp0); j < _end_; ++j) addy(j, a[Maxp]);
  }
  printf("%I64d\n", ans);
  return 0;
}
