l = gets.to_i n = gets.to_i w = gets.split.map(&:to_i) p w.sort.each_with_object({ sum: 0, count:0}) { |v,o| unless o[:sum] + v > l o[:count] += 1 o[:sum] += v else break o end }[:count]