import Control.Monad
import qualified Data.IntMap as M
import Data.IntMap ((!))
type Ints = M.IntMap Int

main :: IO ()
main = do
    [n, c] <- (map read . words) <$> getLine
    wvs <- map readLine2 <$> replicateM n getLine
    print $ solve (n, c) wvs

-- |
--
-- >>> solve (4,6) [(2,1),(3,4),(4,10),(3,4)]
-- 11
solve :: (Int, Int) -> [(Int, Int)] -> Int
solve (n, c) wvs = rec 0 c
  where
    (ws, vs) = toMap2 wvs

    rec :: Int -> Int -> Int
    rec i j
      | i >= n      = 0
      | ws ! i > j  = no
      | otherwise   = max yes no
      where
        yes = (vs ! i) + rec (i + 1) (j - (ws ! i))
        no = rec (i + 1) j

toMap2 :: [(Int, Int)] -> (Ints, Ints)
toMap2 x2s = (x0map, x1map)
  where
    x0map = M.fromList $ zip ixs x0s
    x1map = M.fromList $ zip ixs x1s
    ixs = [0 .. (length x2s - 1)]
    (x0s, x1s) = unzip x2s

readLine2 :: String -> (Int,Int)
readLine2 = tuplify2 . map read . words
    where
      tuplify2 [] = (0, 0)
      tuplify2 [x] = (x, 0)
      tuplify2 (x:y:_) = (x, y)
