program step
    implicit none
    integer n, i
    integer :: dai = 0
    integer,allocatable :: a(:)
    read *, n
    allocate(a(n))
    read *, a(:n)
    do i = 2, n
        if(a(i) .lt. a(i-1))then
            dai = dai+(a(i-1)-a(i))
            a(i) = a(i) + (a(i-1)-a(i))
        end if
    end do
    print *, dai
end program