from math import ceil
def mlt(): return map(int, input().split())


x = int(input())
s = list(mlt())

res = 0

for n in range(1, x):
    if s[n] < s[n-1]:
        d = s[n-1]-s[n]
        res += d
        s[n] += 1

print(res)
