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

dailist = [0]*N
Alist.append(0)
dailist.append(0)
max = 0
for i in range(N):
  if Alist[i]>max:
    max=Alist[i]
  
  if Alist[i]>Alist[i+1]:
    dailist[i+1]=max-Alist[i+1]
  
del dailist[-1]
print (sum(dailist))