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: Wrong Answer)

/* ABC137-D
   Takatan */

#include<stdio.h>
#include<stdlib.h>

typedef struct tnode{
    int day;
    int reward;
    struct tnode *left;
    struct tnode *right;
} tnode;

tnode *createTnode(void);
void insertTnode(tnode *new, tnode **first);
void checkTree(tnode *first, int *day, long long int *wage);
void freeTree(tnode *first);
int main(void){
    int number, day, i;
    long long int wage;
    tnode *new, *tree = NULL;
    wage = 0;

    scanf( "%d %d" , &number, &day);

    for(i=0; i < number; i++){
        new = createTnode(); /* 新しいアルバイト先の情報を格納するノードを作成 */
        insertTnode(new, &tree); /* 報酬を基準に二分探索木を作成 */
    }

    checkTree(tree, &day, &wage); /* 働くアルバイトを選択 */
    freeTree(tree); /* メモリ解放 */
    printf( "%lld\n" , wage); /* 出力 */
    return 0;
}

tnode *createTnode(void){
    tnode *new = malloc(sizeof(tnode));

    if(new==NULL){
        exit(2);
    }

    scanf( "%d %d" , &(new->day), &(new->reward));
    new->left = NULL;
    new->right = NULL;
    return(new);
}

void insertTnode(tnode *new, tnode **first){

    if(*first==NULL){
        *first = new; /* 1つ上が先端の時 */
    }else if((new->reward)<((*first)->reward)){
        /* 新しいノードの報酬の方が小さいとき、左を探索 */
        insertTnode(new, &((*first)->left)); /* 再帰的に実行 */
    }else if((new->reward)>((*first)->reward)){
        /* 新しいノードの報酬の方が大きいとき、右を探索 */
        insertTnode(new, &((*first)->right)); /* 再帰的に実行 */
    }else{ /* 新しいノードの報酬の方と同じとき */
        if((new->day)<((*first)->day)){
            /* 新しいノードの日数の方が小さいとき、左を探索 */
            insertTnode(new, &((*first)->left)); /* 再帰的に実行 */
        }else{
            /* 新しいノードの日数の方が大きいまたは同じとき、右を探索 */
            insertTnode(new, &((*first)->right)); /* 再帰的に実行 */
        }
    }
}

void checkTree(tnode *first, int *day, long long int *wage){
    if(first->right!=NULL){ /* 右に子がいる時 */
        checkTree(first->right, day, wage); /* 再帰的に実行 */
    }
    if((*day)>=(first->day)){ /* 報酬を受け取るまでの日数が足りるとき */
        *wage += first->reward; /* 報酬を加算 */
        (*day)--; /* 残り日数を1減らす */
    }
    if(first->left!=NULL){ /* 左に子がいる時 */
        checkTree(first->left, day, wage); /* 再帰的に実行 */
    }
}

void freeTree(tnode *first){

    if(first->left!=NULL){ /* 左に子がいる時 */
        freeTree(first->left); /* 再帰的に実行 */
    }
    if(first->right!=NULL){ /* 右に子がいる時 */
        freeTree(first->right); /* 再帰的に実行 */
    }
    free(first); /* 自分のノードのメモリを解放 */
}