class A def initialize l = gets.chomp.to_i n = gets.chomp.to_i list = gets.chomp.split(' ').map(&:to_i).sort max_size = 0 count = 0 while list.any? if max_size + list.first <= l max_size += list.shift count += 1 else break end end puts count end end A.new