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 integer::box_width,block_num,total_width integer,allocatable::input(:), sorted(:) read *,box_width,block_num allocate(input(block_num)) allocate(sorted(block_num)) read *,input sorted = qsort(input) total_width = 0 do i=1,block_num total_width=total_width+sorted(i) if ( total_width.gt.box_width ) then exit endif end do print '(i0)', i-1 end program main