import Control.Monad
import qualified Data.ByteString.Char8 as C
import Data.List
import Data.Ord

main = C.interact $ put . sol . get

get = fmap (unfoldr (C.readInt . C.dropWhile (==' '))) . C.lines

put = C.pack . show

sol ([_,w]:wvs) = maximum $ do 
  (w0,v0) <- f (bnd+0) wvs
  (w1,v1) <- f (bnd+1) wvs
  (w2,v2) <- f (bnd+2) wvs
  (w3,v3) <- f (bnd+3) wvs
  guard (w0+w1+w2+w3<=w)
  return (v0+v1+v2+v3)
  where
  bnd = head $ wvs!!0

f x = zip (iterate (+x) 0) . scanl (+) 0 . sortBy (flip compare) . fmap last . filter ((==x) . head)