program step
	implicit none
    integer:: N
    integer:: step_size(200000), height(200000)
    integer:: i, h1, h2

	read(*,*) N    
    read(*,*) (height(i),i=1,N)
    step_size(1:N)=0

	do i = 1, N-1
    	h1 = height(i)+step_size(i)
        h2 = height(i+1)+step_size(i+1)
    	if (h1>h2) then
        	step_size(i+1)=step_size(i+1)+h1-h2
        end if
    end do
    write(*,'(i0)') sum(step_size(1:N))
end program
