# Read k and n from first line input_line = gets.not_nil!.split.map(&.to_i) k = input_line[0] n = input_line[1] # Read the array from second line w = gets.not_nil!.split.map(&.to_i) # Sort the array w.sort! # Iterate through the sorted list result = n w.each_with_index do |value, i| k -= value if k < 0 result = i break end end puts result