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.Generic;
using System.IO;

namespace Sandbox
{
    internal class ProgramD
    {
        public struct DayTask : IComparable<DayTask>
        {
            public DayTask(int a, int b)
            {
                this.Day = a;
                this.Money = b;
            }

            public int Day { get; }
            public int Money { get; }

            public int CompareTo(DayTask other)
            {
                var aComparison = -this.Money.CompareTo(other.Money);
                if (aComparison != 0)
                {
                    return aComparison;
                }

                return -this.Day.CompareTo(other.Day);
            }
        }

        private static void Main(string[] args)
        {
            var scanner = new Scanner();
            var n = scanner.GetNextAsInt();
            var m = scanner.GetNextAsInt();

            var dayTasks = new List<DayTask>();

            for (var i = 0; i < n; i++)
            {
                var a = scanner.GetNextAsInt();
                var b = scanner.GetNextAsInt();
                dayTasks.Add(new DayTask(a, b));
            }

            dayTasks.Sort();

            var money = 0L;
            var day = 0;

            foreach (var dayTask in dayTasks)
            {
                if (dayTask.Day + day > m)
                {
                    continue;
                }

                money += dayTask.Money;
                day++;

                if (day > m)
                {
                    break;
                }
            }

            Console.WriteLine(money);
        }

        #region String Scanner

        public class Scanner
        {
            private const char Separator = ' ';
            private readonly StreamReader _Stream = new StreamReader(Console.OpenStandardInput());
            private int _Pos;
            private string[] _Line = new string[0];

            #region get a element

            public string GetNext()
            {
                // ReSharper disable once InvertIf
                if (this._Pos >= this._Line.Length)
                {
                    // ReSharper disable once PossibleNullReferenceException
                    this._Line = this._Stream.ReadLine().Split(Separator);
                    this._Pos = 0;
                }

                return this._Line[this._Pos++];
            }

            public int GetNextAsInt()
            {
                return int.Parse(this.GetNext());
            }

            public long GetNextAsLong()
            {
                return long.Parse(this.GetNext());
            }

            public double GetNextAsDouble()
            {
                return double.Parse(this.GetNext());
            }

            #endregion

            #region get array

            public string[] ToArrayFromNextLine()
            {
                if (this._Pos >= this._Line.Length)
                {
                    // ReSharper disable once PossibleNullReferenceException
                    this._Line = this._Stream.ReadLine().Split(Separator);
                }

                this._Pos = this._Line.Length;
                return this._Line;
            }

            public int[] ToIntArrayFromNextLine()
            {
                return ToIntArray(this.ToArrayFromNextLine());
            }

            public long[] ToLongArrayFromNextLine()
            {
                return ToLongArray(this.ToArrayFromNextLine());
            }

            public double[] ToDoubleArrayFromNextLine()
            {
                return ToDoubleArray(this.ToArrayFromNextLine());
            }

            #endregion

            #region convert array

            private static int[] ToIntArray(string[] array)
            {
                var result = new int[array.Length];
                for (var i = 0; i < array.Length; i++)
                {
                    result[i] = int.Parse(array[i]);
                }

                return result;
            }

            private static long[] ToLongArray(string[] array)
            {
                var result = new long[array.Length];
                for (var i = 0; i < array.Length; i++)
                {
                    result[i] = long.Parse(array[i]);
                }

                return result;
            }

            private static double[] ToDoubleArray(string[] array)
            {
                var result = new double[array.Length];
                for (var i = 0; i < array.Length; i++)
                {
                    result[i] = double.Parse(array[i]);
                }

                return result;
            }

            #endregion
        }

        #endregion
    }
}