A = $<.map { |s| s.split.map &:to_i } N = A.shift[0] VALUE = A.pop[0] V, W = A.transpose dp = [0] + [-1e9.to_i] * 10**5 N.times do |i| 1e5.to_i.downto(W[i]) do |j| dp[j] = [dp[j], V[i] + dp[j - W[i]]].max end end _ = 0 1e5.to_i.times do |i| _ = [_, dp[i]].max dp[i] = [dp[i], _].max end p [1, dp.index(VALUE)].max p dp[-1] == VALUE ? :inf : [1, dp.rindex(VALUE)].max