n = int(input())
a = [int(_) for _ in input().split()]
ans = 0
h = a[0]
if n == 1:
    print(0)
else:
    for i in range(1, n):
        if a[i] < h:
            ans += h - a[i]
        else:
            h = a[i]
print(ans)