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 C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

// RMQを求めるセグ木, 完全二分木, 蟻本p.155
struct segTree{
    int size; // 2べき, 葉の数
    vector<int> cbt; // complete binary tree
    segTree(int N){
        size = 1;
        while (size < N){
            size *= 2;
        }
        cbt.resize(size * 2 - 1, INT_MAX);
    }

    // v.size <= size の vector を入力
    void input(vector<int> v){
        for (int i = 0; i < v.size(); ++i) {
            cbt[size+i-1] = v[i];
        }
        for (int i = size - 2; i >= 0; --i) {
            cbt[i] = min(cbt[i*2+1], cbt[i*2+2]);
        }
    }

    // 葉のi番目(0-indexed)をxに更新
    void update(int i, int x){
        i += size - 1;
        cbt[i] = x;
        while (i > 0){
            i = (i - 1) / 2;
            cbt[i] = min(cbt[i*2+1], cbt[i*2+2]);
        }
    }

    // 区間[a, b)の最小値(0-indexed)を返す関数
    // 外から呼ぶときは query(a, b, 0, 0, n)
    int query(int a, int b, int k, int l, int r){
        if(r <= a || b <= l) return INT_MAX;
        if(a <= l && r <= b) return cbt[k];
        int vl = query(a, b, k*2+1, l, (l+r)/2);
        int vr = query(a, b, k*2+2, (l+r)/2, r);
        return min(vl, vr);
    }
};

#define MAX_M 100005

int main() {
    int N, M;
    cin >> N >> M;
    vector<pair<int, int>> jobs(N);
    for (int i = 0; i < N; ++i) {
        int A, B;
        cin >> A >> B;
        jobs[i] = make_pair(B, A); // 順番に注意
    }
    sort(jobs.begin(), jobs.end(), greater<pair<int, int>>());

    // セグ木でスケジュール管理しながら報酬で貪欲
    // 貪欲で棄却される仕事は完全下位互換(または同等)でそれを入れても報酬が上がらないことが保証される
    vector<int>sc(M); // 何日後までに報酬を受け取ればいいか
    for (int i = 0; i < M; ++i) {
        sc[i] = i+1;
    }
    segTree tree(MAX_M);
    tree.input(sc);
    int ans = 0;
    for (int i = 0; i < N; ++i) {
        int A = jobs[i].second;
        int B = jobs[i].first;
        int day = tree.query(A-1, tree.size, 0, 0, tree.size);
        if(day == INT_MAX) continue;
        ans += B;
        tree.update(day-1, INT_MAX);
    }
    cout << ans;
    return 0;
}