#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
int i, j, k, n, m, Fg, ch, ff, num;
int a[N], fg[N];
long long ans, sum, s[N];
void R(int &x) {
  ff = x = 0;
  ch = getchar();
  while (ch < '0' || '9' < ch) {
    if (ch == '-') ff = 1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  if (ff) x = -x;
}
void W(long long x) {
  if (x >= 10) W(x / 10);
  putchar(x % 10 + '0');
}
int main() {
  R(n);
  for (i = 1; i <= n; i++) R(a[i]), sum += (long long)i * a[i];
  ans = sum;
  Fg = 1;
  while (Fg) {
    Fg = 0;
    for (i = 1; i <= n; i++) {
      s[i] = s[i - 1];
      if (!fg[i]) s[i] += a[i];
    }
    num = 0;
    for (i = 1; i <= n; i++) {
      long long t = (long long)(num + 1) * a[i] + (s[n] - s[i]);
      if (fg[i] && t > 0) {
        fg[i] = 0;
        ans += t;
        Fg = 1;
      }
      if (!fg[i] && t < 0) {
        fg[i] = 1;
        ans -= t;
        Fg = 1;
      }
      if (!fg[i]) num++;
    }
  }
  W(ans);
  puts("");
}
