N = int(input()) A = list(map(int, input().split())) memo = [(a, i) for i, a in enumerate(A)] ans = 0 x = 0 for a, i in memo: if a == 0: continue a -= 1 while a and x < N: if not A[x]: A[x] += 1 ans += abs(x - i) x += 1 else: x += 1 A[i] = 1 print(ans)