N = int(input()) stack = [] for i, x in enumerate(map(int, input().split())): if x > 0: stack.append([i, x]) stack = stack[::-1] ans = 0 for i in range(N): ans += abs(i - stack[-1][0]) stack[-1][1] -= 1 if stack[-1][1] == 0: stack.pop() print(ans)