#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;
const ll mod = 1e9+7;
const ll N = 1e5+2;

//My tools
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define rep(i,s,e)  for(ll i=s;i<e;i++)
#define repe(i,s,e) for(ll i=s;i<=e;i++) 
#define all(v)  (v).begin(),(v).end()

ll merge(int a[],int l,int r,int mid){
    int p,q,k=0;
    ll inv = 0;
    p = l,q = mid+1;
    int temp[r-l+1];
    for(int i=l;i<=r;i++){
        if(p>mid) temp[k++] = a[q++];
        else if(q>r) temp[k++] = a[p++];
        else if(a[p]<=a[q]) temp[k++] = a[p++];
        else if(a[p]>a[q]){
            inv+= (mid - p +1);
            temp[k++] = a[q++];
        }
    }

    for(int i=0;i<k;i++){
        a[l++] = temp[i];
    }
    return inv;
}

ll mergesort(int a[],int l,int r){
    ll inv = 0;
    if(r>l){
        int mid = (l + r)/2;
        inv+= mergesort(a,l,mid);
        inv+= mergesort(a,mid+1,r);
        inv+= merge(a,l,r,mid);
    }
    return inv;
}


main(){
    int n;cin>>n;
    int a[n],b[n];
    rep(i,0,n){
        cin>>a[i];
        b[i] = a[i];
    }
    ll inv = mergesort(a,0,n-1);
    cout<<inv<<"\n";
    rep(i,0,n-1){
        inv+= (n - 2*b[i] - 1);
        cout<<inv<<"\n";
    }
}
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_
void debug_out() {cerr << endl;}
template<typename Head,typename... Tail> void debug_out(Head H,Tail... T){cerr << ' ' << H; debug_out(T...);}
#define debug(...) cerr << 'L' << __LINE__ << " [" << #__VA_ARGS__ << "]:",debug_out(__VA_ARGS__)
#define dump(x) cerr << 'L' << __LINE__ << " " << #x << " = " << (x) << endl;
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
#define rep(i,n) for (int i = 0; i < (int)(n); i ++)
#define irep(i,n) for (int i = (int)(n) - 1;i >= 0;--i)
using ll = long long;
using PL = pair<ll,ll>;
using P = pair<int,int>;
constexpr int INF = 1000000000;
constexpr long long HINF = 1000000000000000;
constexpr long long MOD = 1000000007;// = 998244353;
constexpr double EPS = 1e-4;
constexpr double PI = 3.14159265358979;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int N; cin >> N;
    vector<ll> A(N);
    rep(i,N) cin >> A[i];
    vector<ll> B(N+1,0);
    rep(i,N) B[i+1] = B[i] + A[i];

    ll ans = 0;
    ll ret = 0;
    ll bfmax = 0;
    rep(i,N) {
        bfmax = max(bfmax,B[i+1]);
        ans = max(ans,ret + bfmax);
        ret += B[i+1];
    }
    cout << ans << '\n';
    return 0;
}