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: Time Limit Exceeded)

using System;
using System.Numerics;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace debug
{
    class main 
    {
        static void Main(string[] args)
        {
            //問題クラスを展開
            ProgramD a = new ProgramD();
            a.main();//実行する
            
        }

    }

    class ProgramA
    {
        public void main()
        {
            //入力
            string[] s = Console.ReadLine().Split(' ');
            int a = int.Parse(s[0]);
            int b = int.Parse(s[1]);

            //最大値出力
            Console.WriteLine(Math.Max(Math.Max(a * b, a - b), a + b));

        }


    }

    class ProgramB
    {
        public void main()
        {
            //入力
            string[] s = Console.ReadLine().Split(' ');
            int k = int.Parse(s[0]);
            int x = int.Parse(s[1]);


            for (int i = x - k + 1; i <= x + k -1 ; i++)
            {
                Console.Write(i);
                Console.Write(" ");

            }
            
        }

    }

    class ProgramC
    {
        public void main()
        {
            //入力
            string input = Console.ReadLine();
            int n = int.Parse(input);

            //辞書型で考える
            var dic = new Dictionary<string, int>();
            long sum = 0;
            //ちいさい順に並び変え
            for (int i = 0; i < n; i++)
            {
                string s = Console.ReadLine();
                char[] t = new char[10];
                for (int j = 0; j < 10; j++)
                    t[j] = s[j];

                Array.Sort(t);
                string sa = "";
                for (int j = 0; j < 10; j++)
                    sa += t[j];

                if (dic.ContainsKey(sa))//もしあるなら足す
                {
                    sum += dic[sa];
                    dic[sa]++;
                }
                else
                {
                    dic.Add(sa, 1);//それ以外では加える
                }

            }
            
            //答え出力
            Console.WriteLine(sum);
        }

    }

    class ProgramD
    {
        public void main()
        {
            string[] input = Console.ReadLine().Split(' ');

            int n = int.Parse(input[0]);
            int m = int.Parse(input[1]);
            List<Task> task = new List<Task>();

            //親子を記録しておく
            for (int i = 0; i < n; i++)
            {
                string[] t = Console.ReadLine().Split(' ');
                int a = int.Parse(t[0]);
                int b = int.Parse(t[1]);
                task.Add(new Task(a, b));
            }

            //Aを小さい順に、Bを大きい順に取り出す
            var task_s = task.OrderBy(x => x.a).ThenByDescending(x => x.b);

            //優先度付きキューを使用（これは最大値を常に取り出せる）
            var q = new PriorityQueue<int>(n + 1);

            //
            long sum = 0;
            for (int i = 1; i <= m; i++)
            {
                //a = Mのときのbでリスト化する
                var b = task.Where(x => x.a == i).Select(x => x.b).ToList();


                //それぞれ取り出して優先キューに入れる
                foreach (var s in b)
                    q.Enqueue(s);
                

                //その中で最大を選んで代入
                if(q.Count != 0)
                    sum += q.Dequeue();

            }


            //答代入
            Console.WriteLine(sum);

        }

    }

    public class Task
    {
        public int a;
        public int b;
        public Task(int a, int b)
        {
            this.a = a;
            this.b = b;
        }

    }

    class PriorityQueue<T> where T : IComparable<T>
    {
        public readonly T[] Array;
        public readonly bool Greater;
        public int Count { get; private set; } = 0;


        public PriorityQueue(int capacity, bool greater = true)
        {
            this.Array = new T[capacity];
            this.Greater = greater;
        }

        //キューを追加
        public void Enqueue(T item)
        {
            this.Array[this.Count] = item;
            this.Count += 1;//数を足す
            this.UpHeap();　//最後尾に追加したところを移動させる
        }

        public T Dequeue()
        {
            // 先頭要素を末尾にして再構成
            this.Swap(0, this.Count - 1);
            this.Count -= 1;
            this.DownHeap();

            return this.Array[this.Count];
        }

        //親と比較して入れ替えていく
        private void UpHeap()
        {
            var n = this.Count - 1;
            while (n != 0)
            {
                // 親要素の座標
                var parent = (n - 1) / 2;

                //もし親の方がちいさいなら、交換する。
                if (this.Compare(this.Array[n], this.Array[parent]) > 0)
                {
                    this.Swap(n, parent);
                    n = parent;
                }
                else
                {
                    break;
                }
            }
        }

        //子と比較して入れ替えていく
        private void DownHeap()
        {
            var parent = 0;
            while (true)//無限ループ
            {
                var child = 2 * parent + 1;
                //子供がいないなら終わり
                if (child > this.Count - 1) break;

                //子供は2つあるので大きい方を対象にする
                if (child < this.Count - 1 && this.Compare(this.Array[child], this.Array[child + 1]) < 0)
                {
                    // 左より右の子のほうが大きい値の場合、右を入れ替え対象にする
                    child += 1;
                }

                //親と子を比較して子が大きいなら入れ替える。そうでないなら成立なのでおしまい。
                if (this.Compare(this.Array[parent], this.Array[child]) < 0)
                {
                    this.Swap(parent, child);
                    parent = child;
                }
                else
                {
                    break;
                }
            }
        }

        //aとbを比較する
        private int Compare(T a, T b)
        {
            var c = a.CompareTo(b);//a-b、正のときはaが大きく、負のときはbが大きい（任意もので処理できる）
            if (!this.Greater)
            {
                c = -c;
            }
            return c;
        }

        //aとbを入れ替える
        private void Swap(int a, int b)
        {
            var tmp = this.Array[a];
            this.Array[a] = this.Array[b];
            this.Array[b] = tmp;
        }
    }

}