ran = int(input())
Ns = list(map(int, input().split()))
ans = 0
base = Ns[0]
for i in range(1, ran):
    if Ns[i] < Ns[i-1]:
        preans = base - Ns[i]
        ans += preans
    else:
        base = Ns[i]
print(ans)