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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define u64 unsigned long long
#define u32 unsigned int
#define reg register
#define Raed Read
#define debug(x) cerr<<#x<<" = "<<x<<endl;
#define rep(a,b,c) for(reg int a=(b),a##_end_=(c); a<=a##_end_; ++a)
#define ret(a,b,c) for(reg int a=(b),a##_end_=(c); a<a##_end_; ++a)
#define drep(a,b,c) for(reg int a=(b),a##_end_=(c); a>=a##_end_; --a)
#define erep(i,G,x) for(reg int i=(G).Head[x]; i; i=(G).Nxt[i])

inline int Read() {
	int res = 0, f = 1;
	char c;
	while (c = getchar(), c < 48 || c > 57)if (c == '-')f = 0;
	do res = (res << 3) + (res << 1) + (c ^ 48);
	while (c = getchar(), c >= 48 && c <= 57);
	return f ? res : -res;
}

template<class T>inline bool Min(T &a, T const&b) {
	return a > b ? a = b, 1 : 0;
}
template<class T>inline bool Max(T &a, T const&b) {
	return a < b ? a = b, 1 : 0;
}

const int N = 1e5+5,M = 2e5 + 5,mod = 99999997;

bool MOP1;

struct node {
	int a,b;
	bool operator<(node _)const {
		return b!=_.b?b>_.b:a<_.a;
	}
} A[N];

bool MOP2;

int vis[N],Fa[N];

int find(int x) {
	return Fa[x]==x?Fa[x]:Fa[x]=find(Fa[x]);
}

inline void _main(void) {
	int n=Raed(),m=Raed();
	rep(i,1,n)A[i]=(node)<%Read()-1,Read()%>,Fa[i]=i;
	sort(A+1,A+n+1);
	int Ans=0;
	rep(i,1,n) {
		if(A[i].a>=m)continue;
		int pos=find(m-A[i].a);
		if(vis[pos]) {
			int pre=find(pos-1);
			pre--;
			if(pre>0)vis[pre]=1,Ans+=A[i].b,Fa[pos]=pre;
		} else {
			vis[pos]=1,Ans+=A[i].b;
		}
	}
	cout<<Ans;
}

signed main() {
#define offline1
#ifdef offline
	freopen("truck.in", "r", stdin);
	freopen("truck.out", "w", stdout);
	_main();
	fclose(stdin);
	fclose(stdout);
#else
	_main();
#endif
	return 0;
}