# Read first number (k) k = gets.not_nil!.to_i # Read second number (n) n = gets.not_nil!.to_i # Read the array from third 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