n = int(input())
x = list(map(int, input().split()))
left = 0
cur = x[-1]
left_cur = 0
ans = 0
for i in range(n - 1, -1, -1):
    if x[i] == cur:
        if x[i] - left_cur <= 0:
            break
        ans += x[i] - left_cur
        left += 1
    else:
        left_cur = left
        cur = x[i]
        if x[i] - left_cur <= 0:
            break
        ans += x[i] - left_cur
        left += 1
print(ans)

