from collections import deque n = int(input()) B = list(map(int,input().split())) ans = 0 left = deque() need = deque() for i,b in enumerate(B): if b == 0: if left: ans += i-left.popleft() else: need.append(i) else: for j in range(b-1): left.append(i) while left and need: ans += left.popleft()-need.popleft() print(ans)