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 Rust (1.42.0) (judge verdict: Accepted)

#![allow(unused_mut)]
#![allow(non_snake_case)]
#![allow(unused_variables)]
#![allow(unused_imports)]
#![allow(unused_macros)]
#![allow(dead_code)]
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

macro_rules! max {
    ($x: expr) => ($x);
    ($x: expr, $($y: expr),+) => {
        cmp::max($x, max!($($y),+))
    };
}

macro_rules! min {
    ($x: expr) => ($x);
    ($x: expr, $($y: expr),+) => {
        cmp::min($x, min!($($y),+))
    };
}

macro_rules! abs {
    ($x:expr, $y:expr) => {
        cmp::max($x, $y) - cmp::min($x, $y)
    };
}

const MOD: usize = 1_000_000_007;

struct BinCoeff {
    size: usize,
    fac: Vec<usize>,
    finv: Vec<usize>,
    inv: Vec<usize>,
}

impl BinCoeff {
    fn new(size: usize) -> BinCoeff {
        let fac = vec![0; size];
        let finv = vec![0; size];
        let inv = vec![0; size];
        let mut bincoeff = BinCoeff {
            size,
            fac: fac,
            finv: finv,
            inv: inv,
        };
        bincoeff.com_init();
        bincoeff
    }

    fn com_init(&mut self) {
        let fac = &mut self.fac;
        let finv = &mut self.finv;
        let inv = &mut self.inv;
        let size = self.size;
        fac[0] = 1;
        fac[1] = 1;
        finv[0] = 1;
        finv[1] = 1;
        inv[1] = 1;
        for i in 2..size {
            fac[i] = fac[i - 1] * i % MOD;
            inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
            finv[i] = finv[i - 1] * inv[i] % MOD;
        }
    }

    fn bin_coeff(&mut self, n: usize, k: usize) -> usize {
        if k > n / 2 {
            self.bin_coeff(n, n - k)
        } else {
            self.fac[n] * (self.finv[k] * self.finv[n - k] % MOD) % MOD
        }
    }
}

struct UnionFind {
    par: Vec<usize>,
}

impl UnionFind {
    fn new(size: usize) -> UnionFind {
        UnionFind {
            par: (0..size).collect::<Vec<usize>>(),
        }
    }

    fn root(&mut self, node: usize) -> usize {
        let parent = self.par[node];
        if parent == node {
            parent
        } else {
            let parent = self.root(parent);
            self.par[node] = parent;
            parent
        }
    }

    fn unite(&mut self, a: usize, b: usize) {
        let root_a = self.root(a);
        let root_b = self.root(b);
        self.par[root_b] = root_a;
    }

    fn is_same(&mut self, a: usize, b: usize) -> bool {
        let root_a = self.root(a);
        let root_b = self.root(b);
        root_a == root_b
    }
}

// must input n <= m
fn gcd(n: usize, m: usize) -> usize {
    if n == 0 {
        m
    } else {
        gcd(m % n, n)
    }
}

fn printer<T, U>(mut array: T)
where
    T: Iterator<Item = U>,
    U: fmt::Display + Copy,
{
    if let Some(a) = array.next() {
        print!("{}", a);
    }
    for a in array {
        print!(" {}", a);
    }
    println!()
}

fn sieve_of_eratosthenes(max: usize) -> Vec<bool> {
    let mut candidates = vec![true; max + 1];
    candidates[0] = false;
    candidates[1] = false;
    let mut num = 2;
    while num * num <= max {
        if candidates[num] {
            let prime = num;
            for n in 2..max / prime + 1 {
                candidates[prime * n] = false;
            }
        }
        num += 1;
    }
    candidates
}

use std::cmp::Ordering;
use std::collections::{BinaryHeap, HashMap, HashSet, VecDeque};
use std::io::{self, BufRead, Read};
use std::{cmp, fmt, iter, usize};

fn main() {
    input! {
        N: usize,
        M: usize,
        work: [(usize, usize); N],
    }
    let mut ans = 0usize;
    let mut pool: BinaryHeap<usize> = BinaryHeap::with_capacity(N);
    work.sort_by_key(|&(a, b)| a);
    let mut work_iter = work.iter().peekable();
    for day in 1..=M {
        while let Some(&&(a, b)) = work_iter.peek() {
            match a.cmp(&day) {
                Ordering::Less => panic!("wrong algorithm!!"),
                Ordering::Equal => {
                    pool.push(b);
                    work_iter.next().unwrap();
                }
                Ordering::Greater => break,
            }
        }
        if let Some(b) = pool.pop() {
            ans += b;
        }
    }

    println!("{}", ans);
}