#include <bits/stdc++.h>
using namespace std;
long long a[100001];
long long psum[100001];
bool can[100001];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    psum[i] = psum[i - 1] + a[i];
    ans += i * a[i];
  }
  bool flg = 1;
  while (flg) {
    flg = 0;
    long long tot = psum[n];
    for (int i = 1, j = 1; i <= n; i++) {
      long long dif = tot - psum[i] + a[i] * j;
      if (dif < 0 && !can[i] || dif > 0 && can[i]) {
        ans += abs(dif);
        can[i] ^= 1;
        flg = 1;
      }
      psum[i] = psum[i - 1];
      if (!can[i]) {
        j++;
        psum[i] += a[i];
      }
    }
  }
  cout << ans;
}
