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 OCaml (4.02.3) (judge verdict: Accepted)

type 'a t = Empty | Node of (int * 'a * 'a t * 'a t)

let rec merge q1 q2 =
    match q1, q2 with
    | Empty, Empty            -> Empty
    | Node(_, _, _, _), Empty -> q1
    | Node(n1, v1, l1, r1), Node(n2, v2, l2, r2) ->
        if v1 < v2
        then Node(n1 + n2, v2, q1, merge l2 r2)
        else Node(n1 + n2, v1, q2, merge l1 r1)
    | _, _ -> failwith "merge"

let pop q =
    match q with
    | Empty -> failwith "pop"
    | Node(_, _, l, r) -> merge l r

let rec push x q =
    match q with
    | Empty -> Node(1, x, Empty, Empty)
    | Node(_, v, l, r) ->
        let large = max x v in
        let small = min x v in
        match l, r with
        | Empty, Empty         -> Node(2, large, Node(1, small, Empty, Empty), Empty)
        | Node(n, _, _, _), Empty -> Node(n + 2, large, l, Node(1, small, Empty, Empty))
        | Node(nl, _, _, _), Node(nr, _, _, _) ->
            if nl > nr
            then Node(nl + nr + 2, large, l, push small r)
            else Node(nl + nr + 2, large, push small l, r)
        | _, _ -> failwith "push"

let top q =
    match q with
    | Empty -> failwith "top"
    | Node(_, v, _, _) -> v

let rec insert_while i ws q =
    match ws with
    | w :: ws' when fst w = i ->
        insert_while i ws' (push (snd w) q)
    | _ -> (ws, q)

let rec solve ws m q i sum =
    if i > m then sum
    else
        let (ws', q') = insert_while i ws q in
        if q' = Empty
        then solve ws' m q' (i + 1) sum
        else solve ws' m (pop q') (i + 1) (sum + top q')

let rec input n =
    if n = 0 then []
    else Scanf.scanf "%d %d\n" @@ fun a b ->
        let ws = input (n - 1) in
        (a, b) :: ws

let () =
    Scanf.scanf "%d %d\n" @@ fun n m ->
    let ws = input n in
    let ws' = List.sort (fun a b -> compare (fst a) (fst b)) ws in
    Printf.printf "%d\n" @@ solve ws' m Empty 1 0