import qualified Data.ByteString.Char8 as BS
import Data.Maybe
import Data.List
import Data.Function
import qualified Data.Set as S
import Data.Array.IArray

solve m xs =
    let set = f m S.empty $ zip [0..] xs
        arr = listArray (0,length xs - 1) xs :: Array Int (Int,Int)
     in S.foldr (\i a -> a + snd (arr!i)) 0 set
  where
    f :: Int -> S.Set Int -> [(Int,(Int,Int))] -> S.Set Int
    f 0 s xs = s
    f m s xs =
        let xs' = [ x | x <- xs,
                      S.notMember (fst x) s && m >= (fst . snd) x ]
            maxOf = maximumBy (compare `on` (snd . snd))
         in case xs' of
                [] -> s
                _  -> let (i,_) = maxOf xs'
                       in f (m-1) (S.insert i s) xs

main :: IO ()
main = do
    [n,m] <- map read . words <$> getLine :: IO [Int]
    xs <- sortOn fst 
        . map ((\[a,b] -> (a,b))
        . map (fst . fromJust . BS.readInt) . BS.words)
        . BS.lines <$> BS.getContents
    print $ solve m xs
