n = int(input()) blst = list(map(int, input().split())) ans = 0 b = 0 for num in blst: if num == 0: b += 1 else: b -= (num - 1) ans += abs(b) print(ans)