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