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-mcs 6.8.0.105) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;

class Program
{
	static void Main()
	{
		var nm = Array.ConvertAll(Console.ReadLine().Split(new[] { ' ' }), number => int.Parse(number));
		var n = nm[0];
		var m = nm[1];

		var datas = new Dictionary<int, List<int>>();
		for (var i = 0; i < n; ++i) {
			var ab = Array.ConvertAll(Console.ReadLine().Split(new[] { ' ' }), number => int.Parse(number));
			var a = ab[0];
			var b = ab[1];
			if (datas.ContainsKey(a)) {
				datas[a].Add(b);
			} else {
				datas.Add(a, new List<int>(new[] { b }));
			}
		}

		var queue = new PriorityQueue<int>((item1, item2) => item1.CompareTo(item2));
		var sum = 0;
		for (var i = 1; i <= m; ++i) {
			if (datas.ContainsKey(i)) {
				datas[i].ForEach(data => queue.Enqueue(data));
			}

			if (queue.Any()) {
				sum += queue.Dequeue();
			}
		}

		Console.WriteLine(sum);
	}
}

internal class PriorityQueue<T>
{
	private readonly Comparison<T> comparison_;

	private T[] items_;

	private int count_;

	internal PriorityQueue(Comparison<T> comparison)
	{
		comparison_ = comparison;
		items_ = new T[2];
	}

	internal bool Any()
	{
		return count_ > 0;
	}

	internal void Enqueue(T item)
	{
		if (items_.Length == count_) {
			var tempItems = items_;
			items_ = new T[items_.Length * 2];
			tempItems.CopyTo(items_, 0);
		}

		items_[count_] = item;
		var index = count_;
		while (index > 0) {
			var parentIndex = (index - 1) / 2;
			if (comparison_(items_[index], items_[parentIndex]) > 0) {
				items_[index] = items_[parentIndex];
				index = parentIndex;
			} else {
				break;
			}
		}

		items_[index] = item;
		++count_;
	}

	internal T Dequeue()
	{
		var returnItem = items_[0];
		--count_;

		var lastItem = items_[count_];
		var rootIndex = 0;
		var childIndex = 1;
		while (childIndex < count_) {
			if (childIndex < count_ - 1 && comparison_(items_[childIndex], items_[childIndex + 1]) < 0) {
				++childIndex;
			}

			if (comparison_(lastItem, items_[childIndex]) < 0) {
				items_[rootIndex] = items_[childIndex];
				rootIndex = childIndex;
				childIndex = childIndex * 2 + 1;
			} else {
				break;
			}
		}

		items_[rootIndex] = lastItem;

		return returnItem;
	}
}