Scanf.scanf "%d %d" (fun n m ->
    let module S = Set.Make (struct type t = int * int let compare = compare end) in
    let ab = Array.init n (fun _ -> Scanf.scanf " %d %d" (fun a b -> a, b)) in
    Array.sort compare ab;

    let rec loop i q acc set =
        if i > m then acc else
            let rec loop2 q set =
                if q = n then q, set else
                    let a, b = ab.(q) in
                    if a > i then q, set else
                        loop2 (q + 1) (S.add (b, q) set)
            in
            let q, set = loop2 q set in
            if S.cardinal set > 0 then (
                let mm = S.max_elt set in
                loop (i + 1) q (acc + fst mm) (S.remove mm set)
            ) else (
                loop (i + 1) q acc set
            )
    in
    loop 1 0 0 S.empty |> Printf.printf "%d\n"
)