import qualified Data.IntMap.Strict as M
import Data.List

mapKeysValue p f g = M.fromDistinctAscList . filter p . map (\(k,v) -> (f k, g v)) . M.toList

main = do
 [n,w] <- map read . words <$> getLine
 wvs <- map (map read . words) . lines <$> getContents
 print (knap n w wvs)

knap n w = maximum . M.elems . foldl' dp (M.singleton 0 0)
 where dp im [w',v'] = M.unionWith max im (mapKeysValue ((<=w).fst) (+w') (+v') im)