#include <bits/stdc++.h>
int main() {
  long long max = 0, temp;
  int i, j, k, n;
  scanf("%d", &n);
  int buffer[n];
  char used[n];
  long long cur[n];
  for (i = 0; i < n; i++) {
    scanf("%d", &buffer[i]);
    max += 1LL * buffer[i] * i;
    used[i] = 1;
    if (i == 0)
      cur[i] = buffer[i];
    else
      cur[i] = cur[i - 1] + buffer[i];
  }
  for (;;) {
    char change = 0;
    k = 1;
    temp = 0;
    for (j = 0; j < n; j++) {
      if (used[j] && 1LL * buffer[j] * k + cur[n - 1] - cur[j] < 0) {
        used[j] = 0;
        change = 1;
      }
      if (!used[j] && 1LL * buffer[j] * k + cur[n - 1] - cur[j] >= 0) {
        used[j] = 1;
        change = 1;
      }
      if (used[j]) {
        cur[j] = cur[j - 1] + buffer[j];
        temp += 1LL * buffer[j] * k;
        k++;
      } else
        cur[j] = cur[j - 1];
    }
    if (temp > max) max = temp;
    if (change == 0) break;
  }
  printf("%I64d", max);
}
