from collections import deque N = int(input()) B = [int(b) for b in input().split()] ans = 0 q = deque() for i in range(N): if B[i] == 0: q.append(i) for i in range(N): while B[i] > 1: B[i] -= 1 t = q.popleft() ans += abs(t-i) print(ans)