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: Accepted)

import qualified Data.ByteString.Char8 as BS
import Data.Maybe
import qualified Data.IntMap as M

data HeapPolicy = MinHeap | MaxHeap

data HeapT p a = Empty p
               | HeapT p Int a (HeapT p a) (HeapT p a)

type Heap a = HeapT HeapPolicy a

empty :: HeapPolicy -> Heap a
empty = Empty

rank :: Heap a -> Int
rank (Empty _)         = 0
rank (HeapT _ r _ _ _) = r

constract :: HeapPolicy -> a -> Heap a -> Heap a -> Heap a
constract p x a b
  | rank a >= rank b = HeapT p (rank b + 1) x a b
  | otherwise        = HeapT p (rank a + 1) x b a

merge :: Ord a => Heap a -> Heap a -> Heap a
merge h (Empty _) = h
merge (Empty _) h = h
merge h1@(HeapT MinHeap _ x l1 r1) h2@(HeapT MinHeap _ y l2 r2)
  | x < y     = constract MinHeap x l1 (merge r1 h2)
  | otherwise = constract MinHeap y l2 (merge r2 h1)
merge h1@(HeapT MaxHeap _ x l1 r1) h2@(HeapT MaxHeap _ y l2 r2)
  | x > y     = constract MaxHeap x l1 (merge r1 h2)
  | otherwise = constract MaxHeap y l2 (merge r2 h1)
merge _ _     = error "invalid pattern match!"

push :: Ord a => a -> Heap a -> Heap a
push x h@(Empty p)         = merge (HeapT p 1 x (Empty p) (Empty p)) h
push x h@(HeapT p _ _ _ _) = merge (HeapT p 1 x (Empty p) (Empty p)) h

pop :: Ord a => Heap a -> Heap a
pop (Empty _)         = error "empty heap!"
pop (HeapT _ _ _ a b) = merge a b

top :: Heap a -> a
top (Empty _)         = error "empty heap!"
top (HeapT _ _ x _ _) = x

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

mapFromList :: [(Int,Int)] -> M.IntMap [Int]
mapFromList [] = M.empty
mapFromList ((k,v):xs) =
    M.insertWith (\[a] b -> a:b) k [v] (mapFromList xs)

solve :: Int -> M.IntMap [Int] -> Int
solve m xs = f 1 (empty MaxHeap)
  where
    f :: Int -> Heap Int -> Int
    f i h
      | i > m = 0
      | otherwise =
          let h' = case M.lookup i xs of
                     Just ls  -> foldr push h ls
                     Nothing -> h
           in if isEmpty h'
                 then f (i+1) h'
                 else top h' + f (i+1) (pop h')

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