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

Step=[0]
maxt=A[0]
i=0
while i<N-1:
  if A[i]<A[i+1]:
    maxt=max(maxt,A[i+1])
    s=0
  else: # A[i]>=A[i+1]
    s=maxt-A[i+1]
  Step+=[s]
  i+=1
print(sum(Step))