#include"bits/stdc++.h" using namespace std; #define REP(k,m,n) for(int (k)=(m);(k)<(n);(k)++) #define rep(i,n) REP((i),0,(n)) using ll = long long; int main() { ll N; cin>>N; vector v(N); rep(i,N)cin>>v[i]; ll res=0; priority_queue,greater> over,less; rep(i,N){ switch(v[i]){ case 0: less.push(i); break; case 1: break; default: rep(j,v[i]-1)over.push(i); } while(!over.empty()&&!less.empty()){ res+=abs(over.top()-less.top()); over.pop(); less.pop(); } } cout<