L = gets.chomp.to_i N = gets.chomp.to_i W = gets.chomp.split.map(&:to_i).sort count = 0 sum = 0 W.each_with_index do |n, i| if W.length > 1 if (L - sum) - W[i] >= 0 count += 1 sum += n else puts count break end else count += 1 puts count end end