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.15.1) (judge verdict: Wrong Answer)

use std::collections::HashMap;

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 $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
    };
    // 3
    // 1 2
    // 1 3
    // 1 4
    // のようなタイプ
    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    }};
    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
  }

fn solve() {
    input! {
        N: usize,
        M: usize,
        bite: [(usize,usize);N]
    }

    // まずbiteを振り分ける
    let mut reward: Vec<Vec<usize>> = vec![Vec::new(); M + 1];
    let mut valid_days: Vec<usize> = Vec::new();
    for i in 0..N {
        if bite[i].0 < M + 1 {
            reward[bite[i].0].push(bite[i].1);
        }
    }
    for i in 0..(M + 1) {
        if reward[i].len() > 0 {
            valid_days.push(i);
            reward[i].sort();
        }
    }
    valid_days.sort();
    println!("{:?}", valid_days);

    //println!("{:?}", reward);

    let mut ans = 0;
    for j in 0..M {
        let target_day = j + 1;

        let mut max = 0;
        let mut use_day: i64 = -1;
        let mut valid_day_index = 0;
        for k in 0..valid_days.len() {
            let valid_day = valid_days[k];
            //println!("{} {} {}", day, target_day, valid_day);
            if valid_day <= target_day {
                if reward[valid_day][reward[valid_day].len() - 1] > max {
                    max = reward[valid_day][reward[valid_day].len() - 1];
                    use_day = valid_day as i64;
                    valid_day_index = k;
                }
            } else {
                break;
            }
        }
        if use_day != -1 {
            ans += max;
            reward[use_day as usize].pop();

            if reward[use_day as usize].len() == 0 {
                valid_days.remove(valid_day_index);
            }
        }
    }

    //println!("{:?}", reward);

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

fn main() {
    let thd = std::thread::Builder::new().stack_size(104_857_600);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}