n = int(input())
A = list(map(int, input().split()))
b = sum(A)
s = 0 
M = 0
for i in range(n-1):
    if A[i] <= A[(i+1)]:
        M = 0
        continue
    else:
        if M == 0:
            s += A[i] - A[(i+1)]
            M = A[(i+1)] + (A[i] - A[(i+1)])
        else:
            s += M - A[(i+1)]
print(s)
