#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 50;
const int B = 350;
const int INF = 0x3f3f3f3f;
int n, A[N];
long long off_k[N], off_o[N];
deque<long long> D[N];
inline long long get(int i) {
  if (i == -1 || (int)D[i / B].size() <= i % B) return -(long long)INF * INF;
  return D[i / B][i % B] + off_k[i / B] * i + off_o[i / B];
}
void dod(long long x, int i) {
  off_o[i] -= off_k[i];
  x -= off_o[i];
  x -= off_k[i] * i * B;
  D[i].push_front(x);
}
void rebalans() {
  for (int i = 0; !D[i].empty(); i++) {
    if ((int)D[i].size() > B) {
      long long nw = D[i].back() + off_o[i] + off_k[i] * (i + 1) * B;
      D[i].pop_back();
      dod(nw, i + 1);
    }
  }
}
inline bool dobar(int i, long long k) { return get(i) < get(i - 1) + k * i; }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", A + i);
  D[0].push_back(0);
  for (int i = 1; i <= n; i++) {
    bool ubac = false;
    for (int j = (i - 1) / B; j >= 0; j--) {
      int ch = min(i - 1, (j + 1) * B - 1);
      if (dobar(j * B, A[i])) {
        off_k[j] += A[i];
        off_o[j] += A[i];
        ubac = true;
        continue;
      }
      ubac = true;
      deque<long long> nw;
      for (int k = j * B; k <= (j + 1) * B && k <= i; k++) {
        nw.push_back(
            max((k < (j + 1) * B && k < i ? get(k) : -(long long)INF * INF),
                get(k - 1) + (long long)A[i] * k));
      }
      off_k[j] = 0;
      off_o[j] = 0;
      D[j] = nw;
      rebalans();
      break;
    }
    if (!ubac) {
      D[i / B].push_back(get(i - 1) + (long long)A[i] * i);
    }
  }
  long long sol = 0;
  for (int i = 1; i <= n; i++)
    if (get(i) > sol) sol = get(i);
  printf("%lld\n", sol);
  return 0;
}
