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.Linq;

namespace ABC137D
{
    class Program
    {
        static void Main(string[] args)
        {
            long[] NM = CIN.LongArray();
            long N = NM[0];
            long M = NM[1];
            Work[] AB = new Work[N];
            for (int i = 0; i < N; i++)
            {
                long[] ab = CIN.LongArray();
                AB[i] = new Work(ab[0], ab[1]);
            }

            Array.Sort(AB);

            int rest = 1;
            long ans = 0;
            long[] free = { };
            long[] work = new long[N];
            int index = 0;
            int newindex = 0;
            for (int i = 0; i < M; i++)
            {
                for (int j = index; j < N; j++)
                {
                    if (AB[j].A > rest)
                    {
                        newindex = j;
                        break;
                    }

                    if (j == AB.Length - 1)
                    {
                        newindex = j;
                    }
                }

                int oldLength = free.Length - 1;
                for (int j = 1; j < free.Length; j++)
                {
                    work[j - 1] = free[j];
                }

                free = new long[free.Length + newindex - index];

                int ABi = index;
                int wi = 0;
                for (int j = 0; j < free.Length; j++)
                {
                    if (ABi > newindex || (work[wi] >= AB[ABi].B && wi < oldLength))
                    {
                        free[j] = work[wi];
                        wi++;
                        continue;
                    }

                    if (ABi < newindex)
                    {
                        free[j] = AB[ABi].B;
                        ABi++;
                    }
                }

                index = newindex;

                rest++;
                if (free.Length == 0)
                {
                    continue;
                }

                ans += free[0];
            }

            Console.WriteLine(ans);
        }

        class Work : IComparable
        {
            public long A;
            public long B;

            public Work(long a, long b)
            {
                A = a;
                B = b;
            }

            public int CompareTo(object obj)
            {
                Work work = (Work) obj;
                if (A > work.A)
                {
                    return 1;
                }

                if (A < work.A)
                {
                    return -1;
                }

                if (B > work.B)
                {
                    return -1;
                }

                if (B == work.B)
                {
                    return 0;
                }

                return 1;
            }
        }

        internal static class CIN
        {
            public static string String()
            {
                return Console.ReadLine();
            }

            public static int Int()
            {
                return int.Parse(Console.ReadLine());
            }

            public static int[] IntArray()
            {
                return Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            }

            public static long Long()
            {
                return long.Parse(Console.ReadLine());
            }

            public static long[] LongArray()
            {
                return Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
            }

            public static double[] DoubleArray()
            {
                return Console.ReadLine().Split(' ').Select(double.Parse).ToArray();
            }
        }
    }
}