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# (Mono 4.6.2.0) (judge verdict: Runtime Error)

using System;
using System.Linq;
using System.Collections.Generic;
class Program
{
	//static int[] dp;
	//static decimal[,,] dp;
	//static decimal N;
	static void Main(string[] args)
	{
			// 入力フェーズ
			//int N = Int32.Parse(Console.ReadLine());
			string[] input = Console.ReadLine().Split(' '); // N,M
			int N = Int32.Parse(input[0]);
			int M = Int32.Parse(input[1]);

			//N件のバイト情報をよむ
			//int[,] bite = new int[N,2];
			Dictionary<int, List<int>> bite = new Dictionary<int, List<int>>();
			for(int i=0; i<N; i++)
			{
				input = Console.ReadLine().Split(' ');
				int a = Int32.Parse(input[0]);
				int b = Int32.Parse(input[1]);
				if(bite.ContainsKey(a))
				{
					List<int> c = bite[a];
					c.Add(b);
					bite[a] = c;
				}
				else
				{
					List<int> c = new List<int>();
					c.Add(b);
					bite.Add(a, c);
				}
			}
//Console.WriteLine("debug1");
			// バイト情報をソートする
			List<int> biteKeys = new List<int>();
			Dictionary<int, List<int>> sortedBite = new Dictionary<int, List<int>>();
			// foreachの中でディクショナリーを更新できないので新しいのを作る
			foreach(int key in bite.Keys)
			{
				biteKeys.Add(key);
				List<int> value = bite[key];
				value.Sort();
				sortedBite.Add(key, value);
			}
			biteKeys.Sort();
			int num = biteKeys.Count;
//Console.WriteLine("debug2 " + num);
			long ret = 0; //　えられる報酬の合計
			// M日後は仕事をできない
			// M-1日後は、1日後に報酬をえられる仕事だけできる
			// できる仕事の中で最大の報酬のバイトをする、なるべく早く報酬がもらえる仕事からする
			for(int i=1; i<=M; i++)
			{
				// M-i日目にできる仕事は、j日後(j<=i)に報酬をもらえる仕事
				int maxGain = 0, ll = -1, lastnum = -1; // できる仕事の中で最大の報酬、ll日後に報酬をもらえる仕事
				for(int j=0; j<num; j++)
				{
					int mm = biteKeys[j];
					if(mm > i)
					{
						break; // もう選べない
					}
					//Console.WriteLine((M-i) + " 日目にできる仕事は" + mm + "日後に報酬をもらえる仕事");

					List<int> b = sortedBite[mm];
					lastnum = b.Count - 1;
					if(b.Count > 0 && b[lastnum] > maxGain)
					{
						maxGain = b[lastnum];
						ll = mm;
					}
				}
				if(maxGain > 0 && ll >= 0 && lastnum >= 0) 
				{
					ret += maxGain;
					sortedBite[ll].RemoveAt(lastnum);
				}
			}
			Console.WriteLine(ret + "");
    }

}