#include #include #include using namespace std; using namespace atcoder; using mint = modint998244353; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf32 1000000001 #define Inf64 4000000000000000001 struct absolute_minima{ using ll = long long; priority_queue> pQ; priority_queue,vector>,greater>> sQ; ll pSum=0,sSum=0,pCnt=0,sCnt=0; void add(long long x,long long w){ sQ.emplace(x,w); sSum += x*w; sCnt += w; while(true){ if(pQ.size()>0&&sQ.size()>0){ if(pQ.top().first>sQ.top().first){ move(pCnt > sCnt); continue; } else if(pCnt > sCnt){ move(true); continue; } else if(pCnt + sQ.top().second <= sCnt - sQ.top().second){ move(false); continue; } } else{ if(sQ.size()==0){ move(true); continue; } else{ if(sQ.top().second <= sCnt - sQ.top().second){ move(false); continue; } } } break; } } void add(ll x){ add(x,1LL); } ll median(){ return sQ.top().first; } vector medians(){ vector ret; if((pCnt+sCnt)%2==0){ if(pCnt==sCnt)ret.push_back(pQ.top().first); else ret.push_back(sQ.top().first); } ret.push_back(median()); return ret; } ll distance_sum(){ ll ret = 0LL; ret -= pSum; ret += sSum; ret += pCnt * median(); ret -= sCnt * median(); return ret; } void move(bool f){ if(f){ if(pQ.size()>0){ pair p = pQ.top(); pQ.pop(); pSum -= p.first*p.second; pCnt -= p.second; sQ.push(p); sSum += p.first*p.second; sCnt += p.second; } } else{ if(sQ.size()>0){ pair p = sQ.top(); sQ.pop(); sSum -= p.first*p.second; sCnt -= p.second; pQ.push(p); pSum += p.first*p.second; pCnt += p.second; } } } int size(){ return pQ.size() + sQ.size(); } }; int main(){ int n; cin>>n; vector a(n); rep(i,n)cin>>a[i]; sort(a.begin(),a.end()); vector A; rep(i,n){ absolute_minima t; t.add(a[i],1); A.push_back(t); while(A.size()>=2){ int x = A.size()-2; int y = A.size()-1; int xr = A[x].median() + A[x].sQ.size() - 1; int yl = A[y].median() - A[y].pQ.size(); if(xr0){ A.back().add(t.pQ.top().first,1); t.pQ.pop(); } while(t.sQ.size()>0){ A.back().add(t.sQ.top().first,1); t.sQ.pop(); } } } long long ans = 0; rep(i,A.size()){ int l = A[i].median() - A[i].pQ.size(); //cout< vs; while(A[i].pQ.size()>0){ vs.push_back(A[i].pQ.top().first); A[i].pQ.pop(); } while(A[i].sQ.size()>0){ vs.push_back(A[i].sQ.top().first); A[i].sQ.pop(); } sort(vs.begin(),vs.end()); rep(j,vs.size()){ ans += abs(vs[j] - (l+j)); } } cout<