if __name__=="__main__":
  N=int(input())
  x=[int(i) for i in input().split()]
  ans+=0
  for i in range(1,len(x)):
    if x[i]<x[i-1]:
      ans+=(x[i-1]-x[i])
      x[i]+=ans
  print(ans)
      
    