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

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

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

knap w = maximum . M.elems . foldl' (dp w) (M.singleton 0 0)

dp w im [w',v'] = foldl' (\ acc (k,v) -> M.insertWith max k v acc) im (mapKeysValue ((<=w).fst) (+w') (+v') im)