program main

 implicit none
 integer::n,i
 real(8)::ans,maxx
 real(8),allocatable::a(:)
 
 read*,n
 allocate(a(n))
 read*,a
 !print*,a

ans=0
maxx=a(1)
 do i=2,n
   if(maxx>a(i)) then
    ans=ans+(maxx-a(i))
    !print*,ans
   end if
   if(maxx<a(i)) then
    maxx=a(i)
   end if
!print*,maxx
 end do


 print*,ans

end program main