N = int(input())
A = list(map(int, input().split()))

before = 0
d = [0] * N
for i in range(N):
    if i == 0:
        before = i
        continue

    if A[before] > A[i]:
        d[i] = (A[before] + d[before]) - A[i]
        before = i
    else:
        before = i

print(sum(d))
