import Data.List
import qualified Data.ByteString.Char8 as B
import Control.Monad.State (State, evalState, gets, modify)
import Data.Map (Map, lookup, insert, empty)
import Data.Maybe (maybe)
import Control.Monad

main = breadIs >>= main'
    where
        main' [n, w] = replicateM n breadIs >>= print . solve w

solve :: Int -> [[Int]] -> Int
solve w xs = run calc (w, xs)

calc :: Memoizer (Int, [[Int]]) Int -> Memoizer (Int, [[Int]]) Int
calc f (w, [nw, nv] : xs)
    | w < nw = f (w, xs)
    | otherwise = max <$> f (w, xs) <*> ((+ nv) <$> f (w - nw, xs))
calc _ _ = return 0

breadIs :: IO [Int]
breadIs = unfoldr uff <$> B.getLine
    where
        uff b = check <$> B.readInt b
        check (a, b)
            | B.null b = (a, b)
            | otherwise = (a, B.tail b)

type Memoizer a b = a -> State (Map a b) b

run :: (Ord a) => (Memoizer a b -> Memoizer a b) -> a -> b
run f = flip evalState empty . memoizeFix f

memoizeFix :: (Ord a) => (Memoizer a b -> Memoizer a b) -> Memoizer a b
memoizeFix f x = gets (Data.Map.lookup x) >>= maybe (f (memoizeFix f) x >>= ((<$) <*> (modify . Data.Map.insert x))) return
