n=int(input())
a=list(map(int, input().split()))
list=[0]
hl=[]

for i in range(1,n):
  for j in range(i):
    hl.append(a[j])
  if a[i]>=a[i-1]:
    list.append(0)
  else:
    mn=max(hl)
    tmp=mn-a[i]
    list.append(tmp)
    
print(sum(list))