program C176
    implicit none
    integer(4) :: N
    integer(4), allocatable :: A(:)
    integer(4) :: i, step

    read(5,*) N
    allocate(A(N))
    read(5,*) (A(i), i = 1, N)

    step = 0
    if (N /= 1) then
        do i = 2, N
            if (A(i-1)>A(i)) then 
                step = step + A(i-1) - A(i)
                A(i) = A(i-1)
            end if
        end do 


    end if

    write(6,*) step
    
end program C176