N = int(input())
A = input().replace(" ","")
Alist = list(A)
An = list(map(int,Alist))

sum = 0

for i in range(1,N):
  if An[i-1]<=An[i]:
    continue
  else:
    sum += (An[i-1]-An[i])
    An[i] = An[i-1]
    
print(sum)
