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

using System;
using System.Collections;
using System.Collections.Generic;
namespace Collections
{
	class program
	{
		static void Main(string[] args)
		{
			// 入力
			string[] input = Console.ReadLine().Split(' '); // N,M
			int N = Int32.Parse(input[0]);
			int M = Int32.Parse(input[1]);

			//N件のバイト情報を読む
			Dictionary<int, PriorityQueue<int>> bite
			= new Dictionary<int, PriorityQueue<int>>();

			// ？？日後にもらえる、のリストを作る
			List<int> days = new 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))
				{
					//Console.WriteLine("debug1 " + a + " " + b);
					PriorityQueue<int> c = bite[a];
					c.Push(b);
				}
				else
				{
					//Console.WriteLine("debug2 " + a + " " + b);
					PriorityQueue<int> c = new PriorityQueue<int>();
					c.Push(b);
					bite.Add(a, c);

					// a日後にもらえる
					days.Add(a);
				}
			}
			days.Sort();

			// M-1日後は、1日後に報酬を得られる仕事をできる
			// M-2日後は、1日後と2日後に報酬を得られる仕事をできる...
			int ret = 0; // 最終的にもらえる報酬
			for(int i=1; i<=M; i++)
			{
				// M-i日目にできる仕事のうち、最大の報酬を探す
				int maxGain = 0, d = 0; // d:d日後に報酬をもらえる仕事
				for(int j=0; j<days.Count; j++)
				{
						int day = days[j];
						//Console.WriteLine("debug3 " + j + " " + day);
						if(day > i)
						{
							// M-i日目にはi日後までに報酬をもらえる仕事じゃないと意味ない
							break;
						}
						if(bite[day].Count > 0 && maxGain < bite[day].Top)
						{
							maxGain = bite[day].Top;
							d = day;
							//bite[day].Pop();
						}
				}
				//Console.WriteLine("debug4 " + maxGain + " " + d);
				// M-i日目の稼ぎ
				if(maxGain > 0 && bite.ContainsKey(d))
				{
					ret += maxGain;
					bite[d].Pop();
				}

				//Console.WriteLine("debug5");
			}

			Console.WriteLine(ret + "");
		}
	}
	// 優先度付きキュー
	class PriorityQueue<T>
		where T : IComparable<T>
		{
			ArrayList buffer;

			// コンストラクタ
			public PriorityQueue()
			{
				this.buffer = new ArrayList();
			}
			public PriorityQueue(int capacity)
			{
				this.buffer = new ArrayList(capacity);
			}

			static int Compare(T a, T b)
			{
				return a.CompareTo(b);
			}

			// 配列リストに新しい要素を追加する
			static void PushHeap(ArrayList array, T elem)
			{
				int n = array.Count;
				array.Add(elem);

				while(n != 0)
				{
					int i = (n-1)/2; //i:親ノード
					//if(array[n].CompareTo(array[i]) > 0)
					T a = (T)array[n];
					T b = (T)array[i];
					if(Compare(a,b) > 0)
					{
						// n番目のノード > その親ノード だった場合スワップする
						T tmp = (T)array[n];
						array[n] = array[i];
						array[i] = tmp;
					}
					n = i; // 親ノードに進む
				}
			}

			// 最大値を削除する
			static void PopHeap(ArrayList array)
			{
				if(array == null || array.Count == 0)
					return;

				int n = array.Count - 1;
				array[0] = array[n]; // 最後の要素（一番ちいさいかも）を、一番上に持ってくる
				array.RemoveAt(n);

				//Console.WriteLine("debug6");
				for(int i = 0; i*2+1 < n;)
				{
					// 子ノードの番号
					int j = i*2+1;// 左側の子ノード、右側の子ノードはj+1

					//Console.WriteLine("debug7 " + i + " " + j);
					// 子ノードが二つある場合、双子のうち大きい方と比較する
					T a,b;
					if(j+1<n)
					{
						a = (T)array[j];
						b = (T)array[j+1];
						if(Compare(a,b) > 0)
							j++; // j+1の要素の方が大きい
					}

					// 子ノード > 親ノードを比較して だった場合スワップする
					a = (T)array[j];
					b = (T)array[i];
					if(Compare(a,b) > 0)
					//if(array[j].CompareTo(array[i]) > 0)
					{
						T tmp = (T)array[i];
						array[i] = array[j];
						array[j] = tmp;
					}
					i = j; // 子ノードに進む
				}
			}

			// プッシュ
			public void Push(T elem)
			{
				PushHeap(this.buffer, elem);
			}

			// ポップ
			public void Pop()
			{
				PopHeap(this.buffer);
			}

			public T Top
			{
				get
				{
					//return this.buffer[0];
					T ret = (T)this.buffer[0];
					return ret;
				}
			}

			public int Count
			{
				get {return this.buffer.Count;}
			}
		}
}