N = gets.to_i VA, WA = N.times.map{ gets.split.take(2).map(&:to_i)}.sort.transpose V = gets.to_i $dp = {} def f(w, i) $dp[[w,i]] ||= if i <0 0 elsif w < WA[i] f(w, i - 1) else [VA[i] + f(w - WA[i], i - 1), f(w, i - 1)].max end end puts (1..WA.sum).bsearch{|i| f(i, N-1) >= V } puts (1..WA.sum).bsearch{|i| f(i, N-1) > V } || :inf