program main

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

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

 print*,ans

end program main