n = int(input())
arr = list(map(int, input().split()))

summary = 0
prev = 0
tmp_offset = 0

for x in range(n):
    tmp = arr[n - x - 1]
    if prev != tmp:
        tmp_offset = x
    if arr[n - x - 1] - tmp_offset > 0:
        summary += arr[n - x - 1] - tmp_offset
    prev = tmp

print(summary)