let () = Scanf.scanf "%d %d\n" @@ fun n m ->
  let abs = Array.init n @@ fun _ -> Scanf.scanf "%d %d\n" @@ fun a b -> a, b in
  Array.sort (fun (a, b) (a', b') -> compare (-b, -a) (-b', -a')) abs;
  Printf.printf "%d\n" @@ fst @@ Array.fold_left (fun (acc, c) (a, b) ->
    if a <= c then (acc + b, c - 1) else (acc, c)) (0, m) abs

