program abc44
 implicit none
 integer::N,ia,ib,ans=0,r
 integer,allocatable::A(:)
 read(*,*)N
 allocate(A(N))
 read(*,*)(A(ia),ia=1,N)
 r=A(1)
 do ib=2,N
  if(r>A(ib))then
   ans=ans+r-A(ib)
  else
   r=A(ib)
  end if
 end do
 print'(i0)',ans
 deallocate(A)
end program abc44