def max_sub(list):
  max_value = max(list)
  max_index = list.index(max_value)
  return max_value,max_index

N=int(input())
xs=list(int(x) for x in input().split())
max_index=len(xs)
sum=0
while max_index>0:
  xs0=[]
  for i in range(max_index):
    xs0.append(xs[i])
  max_value,max_index0 = (max_sub(xs0))
  for i in range(max_index0,max_index):
    sum+=max_value-xs[i]
  max_index=max_index0
print(sum)