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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.util.Scanner;
import java.util.Arrays;

public class Main {
    public static void main(String args[]){
        Scanner stdIn = new Scanner(System.in);
        int n = stdIn.nextInt(); //バイト件数
        int m = stdIn.nextInt(); //働く日数
        double wage = 0;
        double days[] = new double[n];
        double wages[] = new double[n];
        double dw[] = new double[n]; //1日当たり
        
        for(int i = 0; i < n; i++){
            days[i] = stdIn.nextInt();
            wages[i] = stdIn.nextInt();
            dw[i] = wages[i] / days[i];
        }
        
        Main.sort(dw, days, wages, 0, n - 1);
        
        for(int i = n - 1; i >= 0; i--){
            if(days[i] - m >= 0){
                wage += wages[i];
                m = (int)days[i] - m;
            }
            
            if(m == 0){
                break;
            }
        }
        
        
        System.out.println(wage);
    }
    
    public static void sort(double array[], double array2[], double array3[], int left, int right){
        if(left <= right){
            double p = array[(left + right) >>> 1];
            int l = left;
            int r = right;
            while(l <= r){
                while (array[l] < p){
                    l++;
                }
                while (array[r] > p){
                    r--;
                }
                if (l <= r){
                    double tmp = array[l];
                    array[l] = array[r];
                    array[r] = tmp;
                    
                    double tmp2 = array2[l];
                    array2[l] = array2[r];
                    array2[r] = tmp2;
                    
                    double tmp3 = array3[l];
                    array3[l] = array3[r];
                    array3[r] = tmp3;
                    
                    l++;
                    r--;
                }
            }
            Main.sort(array, array2, array3, left, r);
            Main.sort(array, array2, array3, l, right);
        }
    }
}