length = gets.to_i n = gets.to_i arry = gets.split.map(&:to_i) arry.sort! sum =0 count = 0 (0...n).each do |i| sum += arry[i] if length < sum break end count += 1 end p count