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: n, m = map(int, input().split())
  li = [list(map(int, input().split())) for i in range(n)]
  day = [0 for i in range(m)]
  
  li.sort(key=lambda x: x[0])
  li.sort(key=lambda x: x[1], reverse=True)
  
  count = 0
  
  li2 = []
  
  for i in range(n):
      x = li[i][0] - 1
      if x < m and len(li2) == 0:
          li2.append([x, x])
          count += li[i][1]
          continue
      elif x >= m:
          continue
  
      if li2[0][0] > x:
          li2[0:0] = [[x, x]]
          count += li[i][1]
          if li2[0][1] + 1 == li2[1][0]:
              li2[0][1] = li2[1][0]
              del li2[1:2]
          continue
      elif li2[len(li2) - 1][1] < x:
          li2[len(li2) : len(li2)] = [[x, x]]
          count += li[i][1]
          if li2[len(li2) - 1][1] + 1 == li2[len(li2)][0]:
              li2[len(li2) - 1][1] = li2[len(li2)][0]
              li2.pop()
          continue
  
      a = 0
      b = len(li2) - 1
      while a != b:
          c = int((a + b) / 2)
          if li2[c][1] >= x:
              b = c
          else:
              a = c + 1
      if li2[a][0] <= x and li2[a][1] == m - 1:
          continue
      elif li2[a][0] <= x:
          li2[a][1] += 1
          count += li[i][1]
          if a != len(li2) - 1:
              if li2[a][1] + 1 == li2[a + 1][0]:
                  li2[a][1] = li2[a + 1][0]
                  del li2[a + 1 : a + 2]
      else:
          li2[a:a] = [[x, x]]
          count += li[i][1]
          if li2[a - 1][1] + 1 == li2[a][0] and li2[a][1] + 1 == li2[a + 1][0]:
              li2[a - 1][1] = li2[a + 1][1]
              del li2[a : a + 1]
              del li2[a : a + 1]
          elif li2[a - 1][1] + 1 == li2[a][0]:
              del li2[a : a + 1]
          elif li2[a][1] + 1 == li2[a + 1][0]:
              del li2[a + 1 : a + 2]
  
  
  print(count)
  
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"}]