import Data.IntMap ((!)) import qualified Data.IntMap.Strict as M import Data.List import Control.Monad main = do n <- readLn vws <- map (map read . words) <$> replicateM n getLine v <- readLn mapM_ (putStrLn . maybe "inf" show) (knap v vws) knap vmax vws = [max 1 <$> rmin, rmax] where rs = M.keys (M.filter (==vmax) im') rs' = M.keys (M.filter (>vmax) im') im' = foldl' dp (M.singleton 0 0) vws dp im [v,w] = M.filter (<=vmax+1000) $ M.unionWith max im $ M.mapKeys (+w) $ M.map (+v) im rmin = Just (head rs) rmax = if null rs' || (sum (map head vws) <= vmax) then Nothing else Just (head rs' - 1)