lw = gets.to_i n = gets.to_i w = gets.split(" ").map(&:to_i).sort sum = 0 w.each_with_index do |val, i| tmp = sum + val if i == n - 1 then puts i + 1 break elsif tmp < lw then sum = tmp elsif tmp > lw then puts i break else puts i + 1 break end end