output_description: Print the maximum total reward that you can earn no later than M days from
  today.
  
  * * *
input_description: 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
attempt: import os
  import sys
  
  if os.getenv("LOCAL"):
      sys.stdin = open("_in.txt", "r")
  
  sys.setrecursionlimit(2147483647)
  INF = float("inf")
  IINF = 10**18
  MOD = 10**9 + 7
  
  
  class BinaryIndexedTree:
      # http://hos.ac/slides/20140319_bit.pdf
      def __init__(self, size):
          """
          :param int size:
          """
          self._bit = [0] * size
          self._size = size
  
      def add(self, i, w):
          """
          i 番目に w を加える
          :param int i:
          :param int w:
          :return:
          """
          x = i + 1
          while x <= self._size:
              self._bit[x - 1] += w
              x += x & -x
  
      def sum(self, i):
          """
          0 番目から i 番目までの合計
          :param int i:
          :return:
          """
          ret = 0
          x = i + 1
          while x > 0:
              ret += self._bit[x - 1]
              x -= x & -x
          return ret
  
      def __len__(self):
          return self._size
  
  
  N, M = list(map(int, sys.stdin.readline().split()))
  AB = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
  
  AB.sort(key=lambda ab: (-ab[1], ab[0]))
  bit = BinaryIndexedTree(size=M)
  
  ans = 0
  for a, b in AB:
      if M - a < 0:
          continue
      # 入れられる？
      cap = M - a - bit.sum(M - a) + 1
      if cap > 0:
          ans += b
          # どこに入れよう？
          ok = -1
          ng = M - a
          while abs(ok - ng) > 1:
              mid = (ok + ng) // 2
              if mid - bit.sum(mid) + 1 != cap:
                  ok = mid
              else:
                  ng = mid
          bit.add(ok + 1, 1)
  print(ans)
  
problem_description: 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.
samples: [{"input": "3 4\n    4 3\n    4 1\n    2 2", "output": "5\n    \n\nYou can earn the total reward of 5 by taking the jobs as follows:\n\n  * Take and complete the first job today. You will earn the reward of 3 after four days from today.\n  * 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.\n\n* * *"}, {"input": "5 3\n    1 2\n    1 3\n    1 4\n    2 1\n    2 3", "output": "10\n    \n\n* * *"}, {"input": "1 1\n    2 1", "output": "0"}]