n = int(input())
A = list( int(x) for x  in input().split() )

fumidai = 0

for i in range(n):
    x_list = A[0:i+1]
    max_list = max(x_list)
    if max_list > A[i]:
        fumidai += max_list - A[i]

print(fumidai)