module mod_priority_queue
  implicit none
  type t_priority_queue
    private
    integer :: num = 0
    integer, pointer :: heap(:) => null()
  contains
    procedure :: offer => offer
    procedure :: clear => clear
    procedure :: poll => poll
    procedure :: peek => peek
    procedure :: size => size_of
  end type t_priority_queue
contains
  subroutine offer(pq,item)
    class(t_priority_queue), intent(inout) :: pq
    integer, intent(in) :: item
    integer :: n, i, t
    integer, allocatable :: tmp(:)
    if (.not.associated(pq%heap)) allocate(pq%heap(1))
    if (pq%num == size(pq%heap)) then
      allocate(tmp(pq%num))
      tmp = pq%heap
      deallocate(pq%heap)
      allocate(pq%heap(2*pq%num))
      pq%heap(1:pq%num) = tmp
      deallocate(tmp)
    end if
    pq%num = pq%num+1
    pq%heap(pq%num) = item
    n = pq%num
    do while (n > 1)
      i = n/2
      if (pq%heap(n) > pq%heap(i)) then
        t = pq%heap(n)
        pq%heap(n) = pq%heap(i)
        pq%heap(i) = t
      end if
      n = i
    end do
    return
  end
  subroutine clear(pq)
    class(t_priority_queue), intent(inout) :: pq
    if (associated(pq%heap)) deallocate(pq%heap)
    pq%num = 0
    return
  end subroutine clear
  function poll(pq) result(item)
    class(t_priority_queue), intent(inout) :: pq
    integer :: item, n, i, j, tmp
    n = pq%num
    item = pq%heap(1)
    pq%heap(1) = pq%heap(n)
    pq%num = pq%num-1
    i = 1
    do while (2*i < n)
      j = 2*i
      if (j+1 < n .and. pq%heap(j+1) > pq%heap(j)) j = j+1
      if (pq%heap(j) > pq%heap(i)) then
        tmp = pq%heap(j)
        pq%heap(j) = pq%heap(i)
        pq%heap(i) = tmp
      end if
      i = j
    end do
  end
  function peek(pq) result(item)
    class(t_priority_queue), intent(inout) :: pq
    integer :: item
    item = pq%heap(1)
  end
  integer function size_of(pq)
    class(t_priority_queue), intent(in) :: pq
    size_of = pq%num
  end
end module mod_priority_queue
program summer_vacation
  use mod_priority_queue
  implicit none
  type(t_priority_queue) :: pq
  integer :: n, m, a(100000) = 0, b(100000) = 0, i, k
  integer(8) :: ans = 0_8
  read(*,*) n, m
  do i = 1, n
    read(*,*) a(i), b(i)
  end do
  call merge_sort(a(1:n),b(1:n))
  k = 1
  do i = 1, m
    do while (k <= n .and. a(k) <= i)
      call pq%offer(b(k))
      k = k+1
    end do
    if (pq%size() > 0) ans = ans+int(pq%poll(),8)
  end do
  write(*,'(i0)') ans
contains
  subroutine merge_sort(x,y)
    implicit none
    integer, intent(inout) :: x(:), y(:)
    integer :: n, m, l, i, u
    n = size(x)
    m = n
    l = 1
    do while (m.gt.1)
      do i = 1, m/2
        u = min(2*i*l,n)
        call merger(x(2*(i-1)*l+1:(2*i-1)*l),x((2*i-1)*l+1:u), &
        &           y(2*(i-1)*l+1:(2*i-1)*l),y((2*i-1)*l+1:u))
      end do
      l = 2*l
      m = (m+1)/2
    end do
    return
  end subroutine merge_sort
  subroutine merger(x1,x2,y1,y2)
    implicit none
    integer, intent(inout) :: x1(:), x2(:), y1(:), y2(:)
    integer :: x(size(x1)+size(x2)), y(size(y1)+size(y2))
    integer :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(x1)
    n2 = size(x2)
    do while (i1 <= n1 .and. i2 <= n2)
      if (x1(i1) < x2(i2)) then
        x(i1+i2-1) = x1(i1)
        y(i1+i2-1) = y1(i1)
        i1 = i1+1
      else
        x(i1+i2-1) = x2(i2)
        y(i1+i2-1) = y2(i2)
        i2 = i2+1
      end if
    end do
    if (i1 <= n1) then
      x(i1+i2-1:n1+n2) = x1(i1:n1)
      y(i1+i2-1:n1+n2) = y1(i1:n1)
    else if (i2 <= n2) then
      x(i1+i2-1:n1+n2) = x2(i2:n2)
      y(i1+i2-1:n1+n2) = y2(i2:n2)
    end if
    x1 = x(1:n1)
    x2 = x(n1+1:n1+n2)
    y1 = y(1:n1)
    y2 = y(n1+1:n1+n2)
    return
  end subroutine merger
end program summer_vacation

Measured memory_bytes: 2028