program ABC176C
    implicit none
    integer(8)::N,ans=0,i
    integer(8),allocatable::A(:)
    read*,N
    allocate(A(N))
    read*,A
    do i=2,N
        if(A(i)<A(i-1))then
            ans=ans+A(i-1)-A(i)
            A(i)=A(i-1)
        end if
    end do

    print'(i0)',ans
end program ABC176C