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 (GCC 5.4.1) (judge verdict: Time Limit Exceeded)

#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdlib.h>

typedef unsigned long long int uint64;
typedef long long int          int64;
 
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define ABS(a) ((a)>(0)?(a):-(a))

void get_N (int64 *n)
{
  int cnt;
  
  cnt = scanf("%lld", n);

  return;
}

void get_NK (int64 *n, int64 *k)
{
  int cnt;
  
  cnt = scanf("%lld %lld", n, k);

  return;
}

typedef struct _tmp_t {
  int64    a;
  int64    b;
  int64    done;
} tmp_t;


int compare_a(const void *a, const void *b)
{
  tmp_t *x;
  tmp_t *y;

  x = (tmp_t *)a;
  y = (tmp_t *)b;

  if (x->a > y->a) return -1;
  if (x->a < y->a) return 1;

  return 0;
}

int compare_b(const void *a, const void *b)
{
  tmp_t *x;
  tmp_t *y;

  x = (tmp_t *)a;
  y = (tmp_t *)b;

  if (x->b > y->b) return -1;
  if (x->b < y->b) return 1;

  return 0;
}

int compare_ab(const void *a, const void *b)
{
  tmp_t *x;
  tmp_t *y;

  x = (tmp_t *)a;
  y = (tmp_t *)b;

  if (x->a > y->a) return 1;
  if (x->a < y->a) return -1;

  if (x->b > y->b) return -1;
  if (x->b < y->b) return 1;


  return 0;
}

int compare_ab2(const void *a, const void *b)
{
  tmp_t *x;
  tmp_t *y;

  x = (tmp_t *)a;
  y = (tmp_t *)b;

  if (x->b > y->b) return -1;
  if (x->b < y->b) return 1;

  if (x->a > y->a) return 1;
  if (x->a < y->a) return -1;

  return 0;
}

void qsort_a (tmp_t *array, int size)
{
  qsort(array, size, sizeof(tmp_t), compare_a);
}

void qsort_b (tmp_t *array, int size)
{
  qsort(array, size, sizeof(tmp_t), compare_b);
}

void qsort_ab (tmp_t *array, int size)
{
  qsort(array, size, sizeof(tmp_t), compare_ab);
}

void qsort_ab2 (tmp_t *array, int size)
{
  qsort(array, size, sizeof(tmp_t), compare_ab2);
}

#define SIZE (10*10*10*10*10)
tmp_t J[SIZE];
tmp_t K[SIZE];

int main()
{
  int cnt;
  int64 N, M;
  int64 i,j;
  uint64 r = 0;
  uint64 d = 0;
  int worked = 0;

  cnt = scanf("%lld %lld", &N, &M);

  for (i=0; i<N; i++) {
    cnt = scanf("%lld %lld", &J[i].a, &J[i].b); 
  }

    qsort_ab(J, N);

#if 0
  for (i=0; i<N; i++) {
    printf("i=%d J.a=%d, J.b=%d\n", i, J[i].a, J[i].b);
  }
#endif

  j=0;
  for(d=1; d<=M; d++) {
    for (i=j; i<N; i++) {
      if (J[i].a == d) {
        K[j] = J[i];
        j++;
      }
      if (J[i].a > d) break;
    }
    qsort_ab2(K, j);
    for (i=0; i<j; i++) {
      if (K[i].done) continue;
      if (d >= K[i].a) {
        r += K[i].b;
        K[i].done = 1;
#if 0
        printf("i=%d J.a=%d, J.b=%d r=%d d=%d\n", i, J[i].a, J[i].b, r, d);
#endif
        break;
      }
      if (d < K[i].a) {
        break;
      }
    }
  }
  
  printf("%lld", r);

  return 0;
}