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)

#pragma warning disable

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Text.RegularExpressions;


static class MainClass
{
    public static void Main(string[] args)
    {
        //    var nm = Console.ReadLine().SplittedStringToInt32List();
        //    var n = nm[0];
        //    var m = nm[1];
        //    var ls = new List<KeyValuePair<int, int>>();
        //    for (int i = 0; i < n; i++)
        //    {
        //        var ab = Console.ReadLine().SplittedStringToInt32List();
        //        var a = ab[0];
        //        var b = ab[1];

        //        ls.Add(new KeyValuePair<int, int>(a, b));

        //    }
        //    var items = new int[m];
        //    var queued = ls.OrderByDescending(x => x.Value).ToList();
        //    foreach (var item in queued)
        //    {
        //        if (m - item.Key < 0)
        //        {
        //            continue;
        //        }
        //        if (items[m - item.Key] != 0)
        //        {
        //            var i = 0;
        //            while (true)
        //            {
        //                if (m - item.Key - i < 0)
        //                {
        //                    break;
        //                }
        //                if (items[m - item.Key - i] == 0)
        //                {
        //                    items[m - item.Key - i] = item.Value;
        //                    break;
        //                }
        //                i++;
        //            }
        //        }
        //        else
        //        {
        //            items[m - item.Key] = item.Value;
        //        }
        //    }
        //    var sum = 0;
        //    foreach (var item in items)
        //    {
        //        sum += item;
        //    }
        //    Console.WriteLine(sum);

        //    Console.ReadLine();
        var nm = Console.ReadLine().SplittedStringToInt32List();
        var n = nm[0];
        var m = nm[1];
        var ls = new List<KeyValuePair<int, int>>();
        for (int i = 0; i < n; i++)
        {
            var ab = Console.ReadLine().SplittedStringToInt32List();
            var a = ab[0];
            var b = ab[1];

            ls.Add(new KeyValuePair<int, int>(a, b));

        }
        var items = new int[m];
        var queued = ls.OrderByDescending(x => x.Value).ToList();
        var umattenaimono = Enumerable.Range(0, m).ToList();
        foreach (var item in queued)
        {
            if (m - item.Key < 0)
            {
                continue;
            }
            if (items[m - item.Key] != 0)
            {
                var i = 1;
                var nowind = umattenaimono.LowerBound(m - item.Key);
                // ここでおそらくTLE
                while (true)
                {
                    if (nowind - i < 0 || nowind - i > umattenaimono.Count - 1 || umattenaimono[(nowind - i)] < 0)
                    {
                        break;
                    }
                    if (items[umattenaimono[(nowind - i)]] == 0)
                    {
                        var ind = umattenaimono.BinarySearch(umattenaimono[(nowind - i)]);
                        items[umattenaimono[(nowind - i)]] = item.Value;
                        umattenaimono.RemoveAt(ind);

                        break;
                    }
                    i++;
                }
            }
            else
            {
                var ind = umattenaimono.BinarySearch(m - item.Key);
                items[m - item.Key] = item.Value;
                umattenaimono.RemoveAt(ind);

            }
        }
        var sum = 0;
        foreach (var item in items)
        {
            sum += item;
        }
        Console.WriteLine(sum);

        Console.ReadLine();
    }



    public static int LowerBound<T>(this List<T> arr, int start, int end, T value, IComparer<T> comparer)
    {
        int low = start;
        int high = end;
        int mid;
        while (low < high)
        {
            mid = ((high - low) >> 1) + low;
            if (comparer.Compare(arr[mid], value) < 0)
                low = mid + 1;
            else
                high = mid;
        }
        return low;
    }

    public static int LowerBound<T>(this List<T> arr, T value) where T : IComparable
    {
        return LowerBound(arr, 0, arr.Count(), value, Comparer<T>.Default);
    }

    public static int UpperBound<T>(this List<T> arr, int start, int end, T value, IComparer<T> comparer)
    {
        int low = start;
        int high = end;
        int mid;
        while (low < high)
        {
            mid = ((high - low) >> 1) + low;
            if (comparer.Compare(arr[mid], value) <= 0)
                low = mid + 1;
            else
                high = mid;
        }
        return low;
    }

    public static int UpperBound<T>(this List<T> arr, T value)
    {
        return UpperBound(arr, 0, arr.Count(), value, Comparer<T>.Default);
    }


    #region
    public static int ParentCount;

    public static int[] Parents;
    public static int[] IfParentsChildrenCount;

    public static int[] Ranks;

    public static void InitializeUnionFind(int N)
    {
        Parents = new int[N];
        Ranks = new int[N];
        IfParentsChildrenCount = new int[N];
        for (int i = 0; i < N; i++)
        {
            Parents[i] = i;
            Ranks[i] = 0;
            IfParentsChildrenCount[i] = 1;
        }
        ParentCount = N + 1;
    }

    public static int Root(int a)
    {
        if (Parents[a] == a)
        {
            return a;
        }
        else
        {
            Parents[a] = Root(Parents[a]);
            return Parents[a];
        }
    }

    public static bool Same(int a, int b)
    {
        return Root(a) == Root(b);
    }

    public static void Unite(int a, int b)
    {
        var parentA = Root(a);
        var parentB = Root(b);
        if (parentA == parentB)
            return;
        if (Ranks[parentA] < Ranks[parentB])
        {
            Parents[parentA] = parentB;
            IfParentsChildrenCount[parentB] += IfParentsChildrenCount[parentA];
            ParentCount -= IfParentsChildrenCount[parentA];

            IfParentsChildrenCount[parentA] = 0;
        }
        else
        {
            Parents[parentB] = parentA;
            if (Ranks[parentA] == Ranks[parentB])
            {
                Ranks[parentA]++;
            }
            IfParentsChildrenCount[parentA] += IfParentsChildrenCount[parentB];
            ParentCount -= IfParentsChildrenCount[parentB];
            IfParentsChildrenCount[parentB] = 0;
        }
    }
    #endregion
    #region ライブラリ

    public static long ToInt64(this string str) => long.Parse(str);
    public static int ToInt32(this string str) => str == "" ? 0 : int.Parse(str);
    public static BigInteger ToBigInteger(this string str) => BigInteger.Parse(str);
    public static List<string> SplittedStringToList(this string str) => str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
    public static List<int> SplittedStringToInt32List(this string str) => str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)).ToList();
    public static List<long> SplittedStringToInt64List(this string str) => str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(x => long.Parse(x)).ToList();
    public static List<BigInteger> SplittedStringToBigInteger(this string str) => str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(x => BigInteger.Parse(x)).ToList();
    public const int INF = int.MaxValue / 4;
    public const long LONGINF = long.MaxValue / 2;
    public const int Mod1e9 = 1000000007;

    public static void PrintArray(char[,] array)
    {
        for (int i = 0; i < array.GetLength(0); i++)
        {
            var sb = new StringBuilder();
            for (int j = 0; j < array.GetLength(1); j++)
            {
                sb.Append(array[i, j]);
            }
            Console.WriteLine(sb.ToString());
        }
    }
    public static int Manhattan(int x1, int x2, int y1, int y2)
    {
        return Math.Abs(x1 - x2) + Math.Abs(y1 - y2);
    }

    public static int Manhattan(KeyValuePair<int, int> keyValuePair1, KeyValuePair<int, int> keyValuePair2)
    {
        return Manhattan(keyValuePair1.Key, keyValuePair2.Key, keyValuePair1.Value, keyValuePair2.Value);
    }

    public static Dictionary<T, int> ToCountedDictionary<T>(this IEnumerable<T> items)
    {
        var dic = new Dictionary<T, int>();
        foreach (var item in items)
        {
            if (dic.ContainsKey(item))
                dic[item]++;
            else
                dic.Add(item, 1);
        }
        return dic;
    }
    public static Dictionary<TKey, TValue> KeyValuePairListToDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> pairs)
    {
        var dic = new Dictionary<TKey, TValue>();
        foreach (var item in pairs)
        {
            dic.Add(item.Key, item.Value);
        }
        return dic;
    }

    public static long Sums(IEnumerable<int> list)
    {
        var sum = 0l;
        foreach (var item in list)
        {
            sum += Math.Abs(item);
        }
        return sum;
        #endregion
    }

    public static int[] dx = { -1, 1, 0, 0 };
    public static int[] dy = { 0, 0, 1, -1 };

    public static int[] dxx = { 0, 1, 1, 1, 0, -1, -1, -1 };
    public static int[] dyy = { 1, 1, 0, -1, -1, -1, 0, 1 };
}