#include <iostream>
#include <bits/stdc++.h>
typedef long long ll;

using namespace std;

struct Line {
    mutable ll k, m, p;
    bool operator<(const Line& o) const { return k < o.k; }
    bool operator<(ll x) const { return p < x; }
};
 
struct HullDynamic : multiset<Line, less<>> {
    // (for doubles, use inf = 1/.0, div(a,b) = a/b)
    const ll inf = 2e18;
    ll div(ll a, ll b) { // floored division
        return a / b - ((a ^ b) < 0 && a % b); }
    bool isect(iterator x, iterator y) {
        if (y == end()) { x->p = inf; return false; }
        if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
        else x->p = div(y->m - x->m, x->k - y->k);
        return x->p >= y->p;
    }
    void add(ll k, ll m) {
        auto z = insert({k, m, 0}), y = z++, x = y;
        while (isect(y, z)) z = erase(z);
        if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
        while ((y = x) != begin() && (--x)->p >= y->p)
            isect(x, erase(y));
    }
    ll query(ll x) {
        assert(!empty());
        auto l = *lower_bound(x);
        return l.k * x + l.m;
    }
};
const int N = 2e5 + 5;
ll dp[N];
ll n; 
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	cin>>n;
	vector<ll>a(n + 1);
	for(int i = 1;i<=n;i++) cin>>a[i];
	vector<ll>pref(n + 1,0ll);
	for(int i = 1;i<=n;i++) pref[i] = pref[i - 1] + a[i];
	vector<ll>c(n + 1, 0ll); 
	for(ll i = 1ll; i<=n;i++) c[i] = c[i - 1] + (a[i]*i);
	
	HullDynamic cht; 
    dp[0] = 0ll; 
    dp[1] = a[1]; 
    cht.add(0ll, 0ll); 

	for(ll i = 2;i<=n;i++){
		dp[i] = cht.query(pref[i]);
        dp[i] += c[i]; 
        cht.add(1ll - i, (i- 1ll)*pref[i- 1] - c[i-1]); 
		
    }
    ll ans = 0ll; 
    for(int i = 0; i<=n;i++) ans = max(ans, dp[i]); 
    cout<<ans<<'\n'; 
	
	
}