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: Wrong Answer)

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

//
// utility
//
void swap(int* p, int* q) {
  int swp = *q;
  *q = *p;
  *p = swp;
}

//
// data structure
//
enum pqueue_order {
  PQUEUE_ORDER_DESC = -1,
  PQUEUE_ORDER_ASC = 1
};

typedef struct pqueue {
  int* heap;
  int reserved;                     // reserved memory size
  int size;                         // heap size
  int (*push)(struct pqueue*, int);
  int (*pop)(struct pqueue*);
  int (*empty)(struct pqueue*);
  enum pqueue_order ord;
} pqueue_t;

//
// methods
//
int pqueue_push(pqueue_t* self, int n) {
  int node;
  int *heap;

  // ordering
  n *= self->ord;

  // expand heap size if stervation occured
  if (self->size >= self->reserved) {
    self->reserved = self->reserved * 2;
    self->heap = realloc(self->heap, self->reserved * sizeof(int));
  }

  heap = self->heap - 1; // index start from 1
  heap[++self->size] = n;

  // up-heap
  for (node = self->size; heap[node] < heap[node / 2] && node != 1; node/=2)
    swap(heap + node, heap + node/2);

  return 0;
}

int pqueue_pop(pqueue_t* self) {
  int ret;
  int left, rght;
  int node, downto;
  int *heap = self->heap - 1; // index start from 1

  if (self->size == 0) return -1;

  // de-ordering
  ret = heap[1] * self->ord;

  // down-heap
  heap[1] = heap[(self->size)--];
  for (node = 1, downto = -1; node != downto; swap(heap + node, heap + downto), node = downto) {
    int left = node*2    ;
    int rght = node*2 + 1;

    downto = node;
    // downto = min(parent, left child, right child)
    if (left <= self->size && heap[left] < heap[downto])
      downto = left;
    if (rght <= self->size && heap[rght] < heap[downto])
      downto = rght;
  }

  return ret;
}

int pqueue_empty(pqueue_t* self) {
  return self->size == 0;
}

//
// constructor
//
pqueue_t* create_pqueue(enum pqueue_order ord) {
  pqueue_t *p = malloc(sizeof(pqueue_t));

  p->size = 0;
  p->reserved = 2;
  p->heap = malloc(p->reserved * sizeof(int));

  p->ord = ord;

  p->push = pqueue_push;
  p->pop = pqueue_pop;
  p->empty = pqueue_empty;

  return p;
}

//
// destructor
//
void dispose_pqueue(pqueue_t* q) {
  free(q->heap);
  free(q);
}

int test() {
  pqueue_t *q;
  q = create_pqueue(PQUEUE_ORDER_DESC);

  q->push(q, 5);
  q->push(q, 1);
  q->push(q, 8);
  q->push(q, 9);
  q->push(q, -1);

  while (!q->empty(q))
    printf("%d%c", q->pop(q), 10);

  dispose_pqueue(q);

  return 0;
}

int max(int a,int b){
  if(a > b){
    return a;
  }
  return b;
}

typedef struct pair{
  int x,y;
}point;

int compare_pair(const void *a,const void *b){
  if((*(point*)a).x > (*(point*)b).x) return 1;
  if((*(point*)a).x == (*(point*)b).x)
    if((*(point*)a).y < (*(point*)b).y) return 1;
  return -1;
}


int main(void){
  int N, M;
  scanf("%d%d", &N, &M);

  point A[N];
  for (int i = 0; i < N; i++) {
    scanf("%d%d", &A[i].x, &A[i].y);
  }

  qsort(A, N, sizeof(point), compare_pair);

  long long ans = 0;
  int num = 0;
  pqueue_t *q;
  q = create_pqueue(PQUEUE_ORDER_DESC);
  for (int i = 1; i <= 10000; i++) {
    while(A[num].x == i){
      q->push(q, A[num].y);
      num++;
    }
    if (!q->empty(q)) ans += q->pop(q);
  }

  printf("%lld\n", ans);

  /* printf("\n"); */
  /* for (int i = 0; i < N; i++) { */
  /*   printf("%d %d\n", A[i].x, A[i].y); */
  /* } */

  /*   printf("\n"); */
  /*   for (int i = 0; i < M; i++) { */
  /*     for (int j = 0; j < N; j++) { */
  /*       printf("%d ", arr[i][j]); */
  /*     } */
  /*     printf("\n"); */
  /*   } */

  return 0;
}