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