program t implicit none integer::N,M integer,allocatable,dimension(:)::C integer::i read*,N,M allocate(C(N)) read*,C call heapsort(N,C) do i=1,N if(M 1)then l=l-1 t=array(L) else t=array(k) array(k)=array(1) k=k-1 if(k == 1) then array(1)=t exit endif endif i=l j=l+l do while(j<=k) if(j < k)then if(array(j) < array(j+1))j=j+1 endif if (t < array(j))then array(i)=array(j) i=j j=j+j else j=k+1 endif enddo array(i)=t enddo return end subroutine heapsort end program t