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

ans = 0
a = A.index(max(A))
for i in range(1,N):
    if a < i:
        ans += max(A)-A[i]
    elif A[i-1] > A[i]:
        ans += A[i-1]-A[i]

print(ans)