#include using namespace std; #define int long long //typedef pair P; #define mod 1000000007 #define INF (1LL<<60) #define rep(i,n) for(int i=0, i##_len=(n); i istream& operator>>(istream& is, vector& vec) { for (T& x : vec) is >> x; return is; } // pair template ostream& operator<<(ostream& os, pair& pair_var) { os << "(" << pair_var.first << ", " << pair_var.second << ")"; return os; } // vector template ostream& operator<<(ostream& os, const vector& vec) { os << "{"; for (int i = 0; i < vec.size(); i++) { os << vec[i] << (i + 1 == vec.size() ? "" : ", "); } os << "}"; return os; } // map template ostream& operator<<(ostream& os, map& map_var) { os << "{"; repi(itr, map_var) { os << *itr; itr++; if (itr != map_var.end()) os << ", "; itr--; } os << "}"; return os; } // set template ostream& operator<<(ostream& os, set& set_var) { os << "{"; repi(itr, set_var) { os << *itr; itr++; if (itr != set_var.end()) os << ", "; itr--; } os << "}"; return os; } #define DUMPOUT cerr void dump_func() { DUMPOUT << endl; } template void dump_func(Head&& head, Tail&& ... tail) { DUMPOUT << head; if (sizeof...(Tail) > 0) { DUMPOUT << ", "; } dump_func(std::move(tail)...); } #ifdef DEBUG #define DEB #define dump(...) \ DUMPOUT << " " << string(#__VA_ARGS__) << ": " \ << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" \ << endl \ << " ", \ dump_func(__VA_ARGS__) #else #define DEB if (false) #define dump(...) #endif #endif signed main() { int N; cin >> N; vector B(N); priority_queue< int, vector, greater > pq; rep(i,N){ cin >> B[i]; if(B[i]==0) pq.push(i); } int ans=0; rep(i,N){ if(B[i]>=2){ rep(k,B[i]-1){ int t = pq.top(); pq.pop(); B[i]--; B[t]++; ans += abs(t-i); } } } dump(B); cout << ans << endl; return 0; }