#include "bits/stdc++.h" #pragma GCC optimize("Ofast") // Begin {{{ using namespace std; #ifndef DEBUG #define dump(...) #endif template inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); } template inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); } template inline vector make_v(const T &initvalue, size_t sz) { return vector(sz, initvalue); } template inline auto make_v(const T &initvalue, size_t sz, Args... args) { return vector(initvalue, args...))>(sz, make_v(initvalue, args...)); } constexpr int INF = 0x3f3f3f3f; constexpr int64_t LINF = 0x3f3f3f3f3f3f3f3fLL; // }}} End signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); size_t N; cin >> N; vector> exist; vector empty_idx; for (size_t i = 0; i < N; ++i) { intmax_t b; cin >> b; if (b == 0) { empty_idx.emplace_back(i); } else if (b >= 2) { exist.emplace_back(i, b - 1); } } sort(empty_idx.begin(), empty_idx.end()); sort(exist.begin(), exist.end()); size_t pos = 0; intmax_t res = 0; for (size_t i = 0; i < exist.size(); ++i) { while (exist[i].second--) { res += llabs(exist[i].first - empty_idx[pos]); pos++; } } cout << res << "\n"; return 0; }