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

read(*,*) n, m
allocate( a(n), b(n), c(n) )
c = 0
do i = 1, n
   read(*,*) a(i), b(i)
end do
call quicksort2( n ,a, b, c, 1, n )
!do i = 1, n
!   write(*,*) a(i), b(i)
!end do
!write(*,*) ''
i2 = 0
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
   do j = 1, i2
      if( c(j) == 0 ) then
         if( b(j) .ge. y ) then
            y = b(j)
            k = j
         end if
      end if
   end do
   c(k) = 1
   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
end program main