recursive function qsort(x) result(y) integer,intent(in) ::x(:) integer,allocatable::y(:) integer::pivot,total total = size(x) if (total <=1) then y = x else pivot = x(total/2) y = [qsort(pack(x, x .lt. pivot)), & pack(x, x .eq. pivot), & qsort(pack(x, x .gt. pivot))] endif end function qsort program main interface recursive function qsort(x) result(y) integer,intent(in) ::x(:) integer,allocatable::y(:) end function qsort end interface integer::i,N,M integer,allocatable::C(:),INPUT(:) read *, N, M allocate(INPUT(N)) allocate(C(N)) read *, INPUT C = qsort(INPUT) do i=1,N M = M - C(i) if (M.le.0) then if(M.eq.0) then print '(i0)', i else print '(i0)', i-1 end if exit end if end do end program main