import Control.Monad
import Data.Ord
import Data.List

main = do
  (n:w:_) <- getLine >>= return . map read . words :: IO [Int]
  dat <- getContents >>= return . map ((\(w:v:_) -> (w,v)) . map read . words) . lines :: IO [(Int,Int)]

  let
    w' = fst $ head dat
    f i = zip (iterate (+(w'+i)) 0) . scanl (+) 0 . reverse . sort . map snd . filter ((==w'+i) . fst)
    wv0 = f 0 dat
    wv1 = f 1 dat
    wv2 = f 2 dat
    wv3 = f 3 dat

  print $ maximum [v0+v1+v2+v3 | (w0,v0)<-wv0, (w1,v1)<-wv1, (w2,v2)<-wv2, (w3,v3)<-wv3, w0+w1+w2+w3<=w]
