program c_stop
    implicit none
    integer n
    integer,allocatable :: a(:)
    integer i, ans, sa

    read *,n
    allocate(a(n))
    read *,a

    ans=0
    sa=0
    do i=1,n
        if(i/=n)then
            if(a(i)>a(i+1)) then
            sa=a(i)-a(i+1)
            a(i+1)=a(i+1)+sa
            ans=ans+sa
            end if
        else 
            exit
        end if
    end do
    print *,ans
end program c_stop