| # | Time | Username | Problem | Language | Result | Execution time | Memory | 
|---|---|---|---|---|---|---|---|
| 1165117 | sitablechair | Group Photo (JOI21_ho_t3) | C++20 | 0 ms | 0 KiB | 
#include <bits/stdc++.h>
#define ll long long
#define ldb long double
#define endl '\n'
#define For(i,l,r) for(int i=l;i<=r;i++)
#define ForD(i,r,l) for(int i=r;i>=l;i--)
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define sz(x) (signed)x.size()
#define unq(x) x.resize(unique(all(x))-x.begin())
#define F "TASK"
#define fio freopen(F".INP","r",stdin);freopen(F".OUT","w",stdout);
#ifdef NCGM
#include"debug.h"
#else 
#define debug(...) "fr";
#endif
using namespace std;
const int N=5003;
int n,pf[N],a[N];
int idx[N];
vector<int> v;
ll dp[N];
int query(int p) {
    int idx=p;
    int ans=0;
    while(idx>0) {
        ans+=pf[idx];
        idx-=(idx&(-idx));
    }
    return ans;
}
void update(int u,int v) {
    int idx=u;
    while(idx<=n) {
        pf[idx]+=v;
        idx+=(idx&(-idx));
    }
}
inline cal(int l,int r) {
    return 1LL*r*(r+1)/2-1LL*l*(l+1)/2;
}
int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin >> n;
    For(i,1,n) cin >> a[i];
    For(i,1,n) dp[i]=1e18;
    For(i,0,n) {
        v.clear();
        For(j,1,n) 
            if (a[j]>i) v.pb(a[j]);
        For(j,1,n) pf[j]=0;
        For(j,0,sz(v)-1) idx[v[j]]=j+1+i;
        ll cur=0;
        For(j,1,n-i) {
            cur+=query(idx[i+j])+idx[i+j];
            dp[i+j]=min(dp[i+j],dp[i]+cur-cal(i,i+j));
            update(idx[i+j],1);
        }
    }
    cout << dp[n] << endl;
    return 0;
}
