program step
	implicit none
    integer:: N, height(200000)
    integer:: stp(200000)
    integer:: i
    
    
    read(*,*) N    
    read(*,*) (height(i),i=1,N)
    stp(1:N)=0

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