program sample
    implicit none
    character(100)::s,t
    integer(8) :: i,j,k,m,n
    integer(8)::a,b,c,d,ans
    integer(8),allocatable::x(:),y(:)
  
    read(*,*) n
    allocate(x(n))
    read(*,*)x
    m=0
    do i=1,n
        if(x(i)>m)then
            m=x(i)
        elseif(x(i)<m)then
            ans=ans+m-x(i)
        endif
    end do
	write(*,*)ans
    stop
end program sample
  

