program main implicit none integer :: l, n integer, allocatable :: w(:) logical, allocatable :: mask(:) integer :: total_width = 0 integer :: min_index integer :: i read *, l read *, n allocate (w(n)) allocate (mask(n)) read *, w mask = .true. do i = 1, n min_index = minloc(w, 1, mask) total_width = total_width + w(min_index) mask(min_index) = .false. if (total_width > l) exit end do print '(I0)', i-1 end program main