{-# LANGUAGE BangPatterns #-}
module Main where

import qualified Data.IntSet as IS
import Data.List (foldl')
import Control.Monad
import Data.List (sortBy)
import Data.Ord (comparing)

greedy
  :: Foldable f
  => s -- ^ empty set
  -> (a -> s -> Maybe s) -- insert an element
  -> f a
  -> s
greedy e ins = foldl' (\s a -> case ins a s of
  Just s' -> s'
  _ -> s) e

assign :: Int -> (Int, Int) -> (Int, IS.IntSet) -> Maybe (Int, IS.IntSet)
assign duration (d, v) (!total, !s) = do
  day <- IS.lookupLE (duration - d) s
  return (total + v, IS.delete day s)

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

main = do
  [n, duration] <- readL
  jobs <- map (\[a, b] -> (a :: Int, b :: Int)) <$> replicateM n readL
  let (total, _) = greedy (0, IS.fromList [0..duration]) (assign duration)
        $ sortBy (comparing (negate . snd)) jobs
  print total
