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 Fortran (gfortran v4.8.4) (judge verdict: Time Limit Exceeded)

program main
implicit none
integer :: i, j, k, l, x, y, i1, i2, res
integer :: n, m
integer , allocatable :: a(:), b(:), c(:), bb(:), cc(:), d(:), dd(:)

read(*,*) n, m
allocate( a(n), b(n), c(n), bb(n), cc(n), d(n), dd(n) )
c = 0
do i = 1, n
   d(i) = i
   read(*,*) a(i), b(i)
end do
call quicksort2( n ,a, b, c, 1, n )
if( a(1) .gt. m ) then
   write(*,'(i0)') 0
   stop
end if
i2 = 1
res = 0
do i = m, 1, -1
   x = m+1-i
   i1 = i2
   do j = i1, n
      if( a(j) .le. x ) then
         i2 = j
      end if
      if( a(j) .gt. x ) exit
   end do
   y = 0
   call heapsort2( i2, b(1:i2), bb(1:i2), c(1:i2), cc(1:i2), d(1:i2), dd(1:i2) )
   do j = i2, 1, -1
      if( cc(j) == 0 ) then
         y = bb(j)
         c(dd(j)) = 1
         exit
      end if
   end do
   res = res + y
end do
write(*,'(i0)') res
!do i = 1, n
!   write(*,*) a(i), b(i), c(i)
!end do

stop
contains
recursive subroutine quicksort2(n, a, b, c, first, last)
  implicit none
  integer :: a(n), b(n), c(n)
  integer :: first, last
  integer ::  i, j, n
  integer ::  x, y, t, s, u
  
  x = a( (first+last) / 2 )
  i = first
  j = last
  do
     do while (a(i) < x)
        i=i+1
     end do
     do while (x < a(j))
        j=j-1
     end do
     if (i >= j) exit
     t = a(i)
    a(i) = a(j)
    a(j) = t
    s = b(i)
    b(i) = b(j)
    b(j) = s
    u = c(i)
    c(i) = c(j)
    c(j) = u
    i=i+1
    j=j-1
  end do
  if (first < i - 1) call quicksort2(n, a, b, c, first, i - 1)
  if (j + 1 < last)  call quicksort2(n, a, b, c, j + 1, last)
end subroutine quicksort2
subroutine heapsort2(n, a, aa, b, bb, c, cc )
  implicit none
  integer,intent(in) :: n
  integer,intent(in) :: a(1:n), b(1:n), c(1:n)
  integer,intent(out) :: aa(1:n), bb(1:n), cc(1:n)
  
  integer ::i,k,j,l
  double precision :: t, u, v
  aa = a
  bb = b
  cc = c
  if(n.le.0)then
     write(6,*)"Error, at heapsort"; stop
  endif
  if(n.eq.1)return

  l=n/2+1
  k=n
  do while(k.ne.1)
     if(l.gt.1)then
        l=l-1
        t=aa(L)
        u=bb(L)
        v = cc(l)
     else
        t=aa(k)
        aa(k)=aa(1)
        u = bb(k)
        bb(k) = bb(1)
        v = cc(k)
        cc(k) = cc(1)
        k = k -1
        if(k.eq.1) then
           aa(1) = t
           bb(1) = u
           cc(1) = v
           exit
        endif
     endif
     i=l
     j=l+l
     do while(j.le.k)
        if(j.lt.k)then
           if(aa(j).lt.aa(j+1))j=j+1
        endif
        if (t.lt.aa(j))then
           aa(i)=aa(j)
           bb(i) = bb(j)
           cc(i) = cc(j)
           i=j
           j=j+j
        else
           j=k+1
        endif
     end do
     aa(i)=t
     bb(i) =u
     cc(i) = v
  end do

  return
end subroutine heapsort2
end program main