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

count = 0

for i, j in enumerate(a):
    try:
        if j < a[i - 1]:
            b = a[i - 1] - j
            count += b
        else:
            pass
    except IndexError:
        pass

print(count)
