#include <bits/stdc++.h>   using namespace std;   const int MX = 400000;   long long l[500000], r[500000], k[500000], b[500000], a[500000], pref1[500000],       pref2[500000], s = MX;   long long f(long long b1, long long a1, long long b2, long long a2) {     return (b1 - b2) / (a1 - a2);   }   void add(long long kx, long long bx) {     if (s == MX) {       l[s] = -2e18;       r[s] = 2e18;       b[s] = bx;       k[s] = kx;       s--;       return;     }     if (f(bx, kx, b[s + 1], k[s + 1]) > r[s + 1]) {       s++;       add(kx, bx);       return;     }     l[s] = -2e18;     r[s] = f(bx, kx, b[s + 1], k[s + 1]);     b[s] = bx;     k[s] = kx;     l[s + 1] = r[s] + 1;     s--;     return;   }   int main() {     ios::sync_with_stdio(0);     long long n;     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     pref1[0] = a[0];     pref2[0] = a[0];     for (int i = 1; i < n; i++) {       pref1[i] = pref1[i - 1] + a[i];       pref2[i] = pref2[i - 1] + a[i] * (i + 1);     }     long long ans = 0;     for (long long i = 0; i < n; i++) {       ans = max(ans, pref2[i]);       long long lt = s + 1, rt = MX;       while (lt < rt) {         long long mid = (lt + rt) / 2;         if (lt == rt - 1) mid++;         if (pref1[i] < l[mid])           rt = mid - 1;         else           lt = mid;       }       long long q = 0, t = 0;       if (i > 0) {         q = pref2[i - 1];         t = pref1[i - 1];       }       if (lt <= MX) ans = max(ans, pref2[i] - b[lt] + pref1[i] * k[lt]);       add(-i - 1, -(pref1[i] * (i + 1) - pref2[i]));     }     cout << ans <<   n ;   }   