#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int sum = 0;
  char c = getchar();
  bool f = 0;
  while (c < '0' || c > '9') {
    if (c == '-') f = 1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  if (f) return -sum;
  return sum;
}
const int N = 100005;
int n, a[N];
long long sm[N];
bool ch[N];
int main() {
  int i, j;
  long long v, tot, ans = 0;
  bool CH = 1;
  n = read();
  for (i = 1; i <= n; i++) {
    a[i] = read();
    ch[i] = 1;
    sm[i] = sm[i - 1] + a[i];
    ans += (long long)i * a[i];
  }
  while (CH) {
    tot = sm[n];
    CH = 0;
    for (i = 1, j = 1; i <= n; i++) {
      v = tot - sm[i] + (long long)j * a[i];
      if (v < 0 && ch[i]) ans -= v, ch[i] = 0, CH = 1;
      if (v > 0 && !ch[i]) ans += v, ch[i] = 1, CH = 1;
      if (ch[i])
        j++, sm[i] = sm[i - 1] + a[i];
      else
        sm[i] = sm[i - 1];
    }
  }
  cout << ans;
  return 0;
}
