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 (GCC 5.4.1) (judge verdict: Time Limit Exceeded)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define ll long long
#define rep(i,n) for(ll i=0;i<(n);i++)
#define max(p,q) ((p)>(q)?(p):(q))
#define min(p,q) ((p)<(q)?(p):(q))
#define chmax(a,b) ((a)=(a)>(b)?(a):(b))
#define chmin(a,b) ((a)=(a)<(b)?(a):(b))
#define abs(p) ((p)>=(0)?(p):(-(p)))
#define swap(a,b) do{ll w=(a);(a)=(b);(b)=w;}while(0)
#define swapd(a,b) do{double w=(a);(a)=(b);(b)=w}while(0)
#define in(a) scanf("%lld", &(a))
#define in2(a,b) scanf("%lld %lld",&(a),&(b))
#define in3(a,b,c) scanf("%lld %lld %lld",&(a),&(b),&(c))
#define ind(a) scanf("%lf", &(a))
#define put(a) printf("%lld\n", (a))
#define putd(a) printf("%.15f\n", (a))
//def   puts(a) printf("%s\n", a) 文字はこっち
//your code here!
//残り日数を後ろから優先度付きキューに入れると、最大値がすぐ出る
//優先度付きキュー　　最大値を求める場合
#define MAX_HEAP 100005
ll a[MAX_HEAP];
ll g_idx = -1;

ll PQ_pop(void){//最大値を取り出す ヒープからその最大値は削除される
    ll i, j, ret = a[0];
    if(g_idx<0){
        exit(1);
        puts("Queue has no data");
    }
    a[0] = a[g_idx--];
    i = 0;
    while(i*2+1<=g_idx){
        j = i*2+1;
        if(j+1<=g_idx && a[j]<a[j+1]){//min ならここを変更
            j++;
        }
        if(a[i] >= a[j]){//min ならここを変更
            break;
        }
        swap(a[i],a[j]);
        i = j;
    }
    return ret;
}

void PQ_push(ll elem){//ヒープに要素elemを追加する
    if(g_idx >= MAX_HEAP-1){
        exit(1);
        puts("Queue has no room");
    }
    a[++g_idx] = elem;
    ll x = g_idx;
    while(x>0 && a[(x-1)/2]<a[x]){//min ならここを変更
        swap(a[(x-1)/2], a[x]);
        x = (x-1)/2;
    }
}

ll idx[MAX_HEAP]; //dataの各行の要素数
int main(void){
    ll N, M, A[MAX_HEAP], B[MAX_HEAP], ans=0;
    in2(N, M);
    rep(i, N) in2(A[i], B[i]);
    rep(i, M){ //1日後からはじめて2,3...Mとキューに入れていく
        rep(j, N) if(A[j]-1==i) PQ_push(B[j]);
        if(g_idx!=-1) ans += PQ_pop(); //キューが空でなければ、最大値を取得
    }
    put(ans);
    return 0;
}