#include using namespace std; #define REP(i,n) for(int i=0; i<(int)(n); i++) int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector x, y; REP (i, n) { int b; cin >> b; if (!b) x.push_back(i); else if (b > 1) { REP (j, b-1) y.push_back(i); } } int ret = 0; REP (i, x.size()) ret += abs(x[i] - y[i]); cout << ret << endl; return 0; }