import numpy as np

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

sum=0
while 1:
    max_A=max(A)
    max_index=np.argmax(A)
    A_new=A[max_index:]
    for a in A_new:
        sum+=max_A-a
    A=A[:max_index]
    if max_index==0:
        break

print(sum)