module mod_segment_tree
  implicit none
  type t_segment_tree
    private
    integer :: deflt
    integer :: n, p
    integer, pointer :: arr(:) => null()
  contains
    procedure :: init => init_segtree
    procedure :: release => release_segtree
    procedure :: default => set_default
    procedure :: update => update
    procedure :: query => query
  end type t_segment_tree
contains
  integer function op(x,y)
    integer, intent(in) :: x, y
    op = min(x,y)
  end
  subroutine init_segtree(this,n,deflt)
    class(t_segment_tree), intent(inout) :: this
    integer, intent(in) :: n
    integer, intent(in) :: deflt
    integer :: p = 1
    do while (p < n)
      p = 2*p
    end do
    this%deflt = deflt
    this%n = n
    this%p = p
    allocate(this%arr(2*p-1))
    this%arr = deflt
  end
  subroutine release_segtree(this)
    class(t_segment_tree), intent(inout) :: this
    if (associated(this%arr)) deallocate(this%arr)
  end
  subroutine set_default(this,i)
    class(t_segment_tree), intent(inout) :: this
    integer, intent(in) :: i
    integer :: x
    x = i+this%p-1
    this%arr(x) = this%deflt
    do while (x > 1)
      x = x/2
      this%arr(x) = op(this%arr(2*x),this%arr(2*x+1))
    end do
  end
  subroutine update(this,i,v)
    class(t_segment_tree), intent(inout) :: this
    integer, intent(in) :: i
    integer, intent(in) :: v
    integer :: x
    x = i+this%p-1
    this%arr(x) = v
    do while (x > 1)
      x = x/2
      this%arr(x) = op(this%arr(2*x),this%arr(2*x+1))
    end do
  end
  integer function query(this,a,b)
    class(t_segment_tree), intent(inout) :: this
    integer, intent(in) :: a, b
    integer :: l, r
    query = this%deflt
    l = a+this%p-1
    r = b+this%p-1
    do while (l <= r)
      if (mod(l,2) == 1) then
        query = op(query,this%arr(l))
        l = l+1
      end if
      if (mod(r,2) == 0) then
        query = op(query,this%arr(r))
        r = r-1
      end if
      l = l/2
      r = r/2
    end do
  end
end module mod_segment_tree
program summer_vacation
  use mod_segment_tree
  implicit none
  integer, parameter :: INF = 10**9
  type(t_segment_tree) :: st
  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))
  call st%init(m,INF)
  do i = 1, m
    call st%update(i,i)
  end do
  do i = 1, n
    k = st%query(a(i),m)
    if (k == INF) cycle
    call st%default(k)
    ans = ans+int(b(i),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 (y1(i1) > y2(i2) .or. (y1(i1) == y2(i2) .and. 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: 2412