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

dai=[0, 0, 0, 0, 0]
for i, l in enumerate(B):
  if i==0:
    continue
  if B[i-1]-B[i]>0:
    dai[i]=B[i-1]-B[i]
    #B[i]+=B[i-1]-B[i]
    
print(sum(dai))