#include <bits/stdc++.h>   using namespace std;   struct Line {     long long a, b;     int id;     Line(long long a, long long b, int id) : a(a), b(b), id(id) {}     bool before(const Line &A, const Line B) {       long long p1 = A.b - b, p2 = B.b - b;       long long q1 = a - A.a, q2 = a - B.a;       if (q1 < 0) {         p1 *= -1;         q1 *= -1;       }       if (q2 < 0) {         p2 *= -1;         q2 *= -1;       }       return p1 * q2 <= p2 * q1;     }     long long value(int x) { return a * x + b; }   };   struct Hull {     vector<int> a;     vector<bool> taken;     vector<Line> h;     vector<int> order;     int ptr;     int size = 0;     long long sum = 0;     Hull() {}     Hull(const vector<int> &a) : a(a) {       taken.resize(a.size(), false);       for (int i = 0; i < (int)a.size(); ++i) {         order.push_back(i);       }       sort((order).begin(), (order).end(),            [&](int i, int j) { return a[i] < a[j]; });       Build();     }     void Build() {       const int n = (int)a.size();       ptr = 0;       vector<long long> val(n);       long long suf = sum;       int cnt = 0;       for (int i = 0; i < n; ++i) {         if (taken[i]) {           suf -= a[i];           ++cnt;         } else {           val[i] = suf + a[i] * 1ll * (cnt + 1);         }       }       vector<Line> sorted;       for (int i : order) {         if (!taken[i]) {           sorted.push_back(Line(a[i], val[i], i));         }       }       h.clear();       for (Line &C : sorted) {         if (!h.empty() && h.back().a == C.a) {           if (C.b <= h.back().b)             continue;           else             h.pop_back();         }         while ((int)h.size() >= 2) {           Line &A = h[(int)h.size() - 2];           Line &B = h[(int)h.size() - 1];           if (A.before(B, C)) break;           h.pop_back();         }         h.push_back(C);       }     }     pair<long long, int> best(int before) {       if (h.empty()) return {-1ll, -1};       while (ptr <= (int)h.size() - 2 &&              h[ptr].value(before) < h[ptr + 1].value(before)) {         ++ptr;       }       return make_pair(h[ptr].value(before), h[ptr].id);     }     void Remove(const int i) {       taken[i] = true;       sum += a[i];       ++size;       Build();     }   } h[1010];   const int N = 1e5 + 5;   int a[N];   int main() {     ios_base::sync_with_stdio(false);     int n;     cin >> n;     for (int _n(n), i(0); i < _n; i++) cin >> a[i];     int block = max(1, int(sqrt(n)));     for (int i = 0; i < n; i += block) {       vector<int> b;       for (int j = i; j < min(n, i + block); ++j) {         b.push_back(a[j]);       }       h[i / block] = Hull(b);     }     long long best = 0, score = 0;     while (true) {       pair<long long, int> m = make_pair(-1ll, -1);       long long suf = 0;       int cnt = 0;       for (int i = 0; i < n; i += block) {         suf += h[i / block].sum;       }       for (int i = 0; i < n; i += block) {         suf -= h[i / block].sum;         pair<long long, int> p = h[i / block].best(cnt);         p.first += suf;         if (p.second != -1)           if (m.second == -1 || p.first > m.first)             m = make_pair(p.first, i + p.second);         cnt += h[i / block].size;       }       if (m.second == -1) break;       score += m.first;       best = max(best, score);       int i = m.second;       h[i / block].Remove(i - i / block * block);     }     cout << best << endl;     return 0;   }