INF = 1 << 25 n = gets.to_i ITEM = n.times.map{gets.split.map(&:to_i)} V = gets.to_i init = Array.new(V+2, INF) init[0] = 0 dp = ITEM.inject(init) do |s, (v,w)| dp = Array.new(V+2, INF) (0 .. V+1).each do |i| if i < v dp[i] = [ w, s[i] ].min else dp[i] = [ s[i - v] + w, s[i] ].min end end dp end if dp[V] == 0 puts 1 else puts dp[V] end if dp[V + 1] < 10 ** 5 puts dp[V + 1] - 1 else puts "inf" end