N=int(input())
Alist=list(map(int,input().split()))
sum=0
if N==1:
  print(0)
else:
  for i in range(1,N):
    if Alist[i]<max(Alist[:i]):
      sum=sum+max(Alist[:i])-Alist[i]
      Alist[i]=max(Alist)
print(sum)