Problem:
Score : 400 points

Problem Statement

There are N one-off jobs available. If you take the i-th job and complete it, you will earn the reward of B_i after A_i days from the day you do it.

You can take and complete at most one of these jobs in a day.

However, you cannot retake a job that you have already done.

Find the maximum total reward that you can earn no later than M days from today.

You can already start working today.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i \leq 10^5

1 \leq B_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the maximum total reward that you can earn no later than M days from today.

Sample Input 1

3 4
4 3
4 1
2 2

Sample Output 1

5

You can earn the total reward of 5 by taking the jobs as follows:

Take and complete the first job today. You will earn the reward of 3 after four days from today.

Take and complete the third job tomorrow. You will earn the reward of 2 after two days from tomorrow, that is, after three days from today.

Sample Input 2

5 3
1 2
1 3
1 4
2 1
2 3

Sample Output 2

10

Sample Input 3

1 1
2 1

Sample Output 3

0

Solution in Haskell (GHC 7.10.3) (judge verdict: Time Limit Exceeded)

{-# LANGUAGE BangPatterns #-}
import Control.Monad
import Data.Maybe
import Debug.Trace
import Data.List
import qualified Data.Map.Strict as Map
import qualified Data.IntMap.Strict as IntMap
import qualified Data.Set as Set
import qualified Data.IntSet as IntSet
import Data.Functor
import Data.Array
-- import Data.Array.Unboxed
import Control.Monad.ST
import Data.Array.ST
import qualified Data.ByteString.Char8 as B
import Data.Maybe (fromJust)
readInt = fst . fromJust . B.readInt
readInts = map readInt . B.words <$> B.getLine :: IO [Int]
readIntegers = map (fst . fromJust . B.readInteger) . B.words <$> B.getLine :: IO [Integer]
read2dInts = map (map (fst . fromJust . B.readInt) . B.words) . B.lines <$> B.getContents
readVInts = map (fst . fromJust . B.readInt) . B.lines <$> B.getContents


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

solve n m abs = go (0, 0) initial wvs
  where
    initial = empty (\x y -> compare (snd y) (snd x))
    wvs = sortOn fst $ abs
    go (_, mv) heap [] = mv
    go (mm, mv) heap wvs@((ha, hb):wvss)
      | mm > m = mv
      | isEmpty heap = go (mm+1, mv) (foldr insertHeap heap next) newWvs
      | otherwise = go (mm+1, mv + maxb) (foldr insertHeap (removeRoot heap) next) newWvs
      where
        next = filter (\x -> fst x == mm + 1) wvs
        newHeap = foldr insertHeap heap next 
        newWvs = dropWhile (\x -> fst x == m + 1) wvs
        maxb = snd $ root heap


data Heap a = Heap (a -> a -> Ordering) (Tree a)
data Tree a = Null | Fork !a (Tree a) (Tree a) deriving (Show)

instance (Show a) => Show (Heap a) where
  show (Heap _ a) = show a

empty :: (a -> a -> Ordering) -> Heap a
empty pred = Heap pred Null

isEmpty :: Heap a -> Bool
isEmpty (Heap _ Null) = True
isEmpty _ = False

root :: Heap a -> a
root (Heap _ (Fork x _ _)) = x

rootNode :: Tree a -> a
rootNode (Fork x _ _) = x

removeRoot :: Heap a -> Heap a
removeRoot (Heap c (Fork _ a b)) = Heap c $ merge c a b

insertHeap :: a -> Heap a -> Heap a
insertHeap x (Heap c t) = Heap c $ merge c (singleton x) t

merge :: (a -> a -> Ordering) -> Tree a -> Tree a -> Tree a
merge _ a Null = a
merge _ Null b = b
merge c a b
  | c (rootNode a) (rootNode b) == LT = joinHeap c a b
  | otherwise = joinHeap c b a

joinHeap :: (a -> a -> Ordering) -> Tree a -> Tree a -> Tree a
joinHeap c (Fork x l r) a = Fork x r (merge c l a)

singleton :: a -> Tree a
singleton a = Fork a Null Null

fromList :: (a -> a -> Ordering) -> [a] -> Heap a
fromList c = foldl' (flip insertHeap) (Heap c Null)