D - Summer Vacation Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

N 件の日雇いアルバイトがあり、i 件目の日雇いアルバイトを請けて働くと、その A_i 日後に報酬 B_i が得られます。

あなたは、これらの中から 1 日に 1 件まで選んで請け、働くことができます。

ただし、請けたことのある日雇いアルバイトは選べません。

今日から M 日後まで(M 日後を含む)に得られる報酬の合計の最大値を求めてください。

なお、日雇いアルバイトは今日から請けて働くことができます。

制約

  • 入力は全て整数である。
  • 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

入力

入力は以下の形式で標準入力から与えられる。

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

M 日後までに得られる報酬の合計の最大値を出力せよ。


入力例 1

3 4
4 3
4 1
2 2

出力例 1

5

以下のように日雇いアルバイトを請けて働くと、報酬の合計は 5 となり、このときが最大です。

  • 今日、1 件目の日雇いアルバイトを請けて働き、今日から 4 日後に報酬 3 を得ます。
  • 明日(今日から 1 日後)、3 件目の日雇いアルバイトを請けて働き、今日から 1+2 = 3 日後に報酬 2 を得ます。

入力例 2

5 3
1 2
1 3
1 4
2 1
2 3

出力例 2

10

入力例 3

1 1
2 1

出力例 3

0

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