{-# LANGUAGE BangPatterns #-}
module Main where
import qualified Data.HashSet as HS
import qualified Data.Vector.Mutable as MV
import qualified Data.Vector as V
import Control.Monad

readL :: Read a => IO [a]
readL = map read . words <$> getLine

main = do
  [n, duration] <- readL
  jobs <- replicateM n $ (\[a,b] -> (a,b)) <$> readL

  result <- MV.replicate (duration+1) (0 :: Int, HS.empty :: HS.HashSet Int)
  forM_ [0..duration] $ \day -> do
    current <- V.freeze result
    forM_ (zip [0..] jobs) $ \(jobId, (delay, income)) -> do
      let day' = day + delay
      unless (day' > duration) $ do
        forM_ [day..duration] $ \baseDay -> do
          let (total, done) = current V.! baseDay
          unless (HS.member jobId done) $ do
            let !new = total + income
            let !done' = HS.insert jobId done
            (old, _) <- MV.read result day'
            when (new > old) $ do
              MV.write result day' (new, done')

  V.unsafeFreeze result >>= print . V.maximum . V.map fst
