#include <bits/stdc++.h>
using namespace std;
int n, a[100005], cnt, tag;
long long S[100005], ans;
bool op[100005];
int main() {
  int i;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%d", &a[i]), op[i] = 1;
  while (1) {
    cnt = tag = 0;
    for (i = n; i >= 1; i--) S[i] = S[i + 1] + op[i] * a[i];
    for (i = 1; i <= n; i++) {
      if (op[i] && S[i + 1] + 1LL * (cnt + 1) * a[i] < 0) op[i] = 0, tag = 1;
      if (!op[i] && S[i + 1] + 1LL * (cnt + 1) * a[i] > 0) op[i] = 1, tag = 1;
      cnt += op[i];
    }
    if (!tag) break;
  }
  cnt = 0;
  for (i = 1; i <= n; i++)
    if (op[i]) cnt++, ans += 1LL * cnt * a[i];
  cout << ans;
  return 0;
}
