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: Accepted)

using System;
using System.Linq;
using System.Collections.Generic;

class Program
{
  static void Main()
  {
    string[] input = Console.ReadLine().Split(' ');
    int N = int.Parse(input[0]);
    int M = int.Parse(input[1]);
    
    Dictionary<int, List<int>> Jobs = new Dictionary<int, List<int>>();
    for (int i = 0; i < N; i++)
    {
      string[] input_ = Console.ReadLine().Split(' ');
      int A = int.Parse(input_[0]);
      int B = int.Parse(input_[1]);
      
      if (Jobs.ContainsKey(A))
      {
        Jobs[A].Add(B);
      }
      else
      {
        Jobs.Add(A, new List<int>{B});
      }
    }
    
    // 仕事の候補を格納するキュー
    List<Tuple<int, int>> Jobq = new List<Tuple<int, int>>();
    // 報酬の合計
    int sum = 0;
    
    for (int i = 1; i <= M; i++)
    {
      // i 日以内に報酬が獲得できる仕事を追加
      if (Jobs.ContainsKey(i))
      {
  	    foreach (int b in Jobs[i])
    	{
     	   PushHeap(Jobq, new Tuple<int, int>(i, b));
  	    }
      }
      if (Jobq.Count == 0)
        continue;
      
      // 最も報酬の大きい要素の報酬を加算
      sum += Jobq[0].Item2;
      // 最大要素を削除
      PopHeap(Jobq);
    }
    
    Console.WriteLine(sum);
  }
  
  // Heap に要素を追加
  static void PushHeap(List<Tuple<int, int>> array, Tuple<int, int> elem)
  {
    int n = array.Count;
    array.Add(elem);
    
    while (n != 0)
    {
      int i = (n-1) / 2; // parent's index
      if (array[n].Item2 > array[i].Item2) // if child is larger than parent
      {
        // switch the child and the parents
        Tuple<int, int> tmp = array[n];
        array[n] = array[i];
        array[i] = tmp;
      }
      n = i;
    }
  }

  // Heap から最大値を削除
  static void PopHeap(List<Tuple<int, int>> array)
  {
    // 末端の要素で置き換える. n := 新しいリストの大きさ
    int n = array.Count - 1;
    array[0] = array[n];
    array.RemoveAt(n);
    
    // i := 親の番号, j := (左側の)子の番号, j が n 未満の限り繰り返す
    for (int i = 0, j; (j = i*2 + 1) < n; ) 
    {
      // 右側の子の方が大きい場合は, 右側の子と親を入れ替える
      if ((j != n - 1) && (array[j].Item2 < array[j + 1].Item2))
      {
        j++;
      }
      // 親の方が小さければ，子と親の値を入れ替える
      if ((array[i].Item2 < array[j].Item2))
      {
        Tuple<int, int> tmp = array[j];
        array[j] = array[i];
        array[i] = tmp;
      }
      i = j;
    }
  }
}