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: class Work:
      count = 0
  
      def __init__(self, payday, money):
          Work.count += 1
          self.count = count
          self.payday = payday
          self.money = money
  
      # moneyが大きい方、同じなら早くもらえる方、それも同じなら最初に書いてある方が強い方
      def __gt__(self, other):
          if self.money > other.money:
              return True
          if self.money == other.money:
              if self.payday < other.payday:
                  return True
              if self.payday == other.payday:
                  if self.count < other.count:
                      return True
          return False
  
      def __ft__(self, other):
          if self.money < other.money:
              return True
          if self.money == other.money:
              if self.payday > other.payday:
                  return True
              if self.payday == other.payday:
                  if self.count > other.count:
                      return True
          return False
  
  
  #     def __eq__(self,other):
  #         if self.money==other.money and self.payday==other.payday:
  #             return True
  #         return False
  #     def __fe__(self,other):
  #         return self.__ft__(other) or self.__eq__(other)
  #     def __ge__(self,other):
  #         return self.__gt__(other) or self.__eq__(other)
  
  # バイトの数、いつまでに稼ぐのか
  n, limit = [int(i) for i in input().split()]
  
  # バイトのリストを作り、クラスを入れる O(n)
  baitolist = []
  for i in range(n):
      payday, money = [int(i) for i in input().split()]
      work = Work(payday, money)
      baitolist.append(work)
  
  # 日数でランク付け　O(n)
  summoney = 0
  rank = [[] for i in range(limit + 1)]
  for baito in baitolist:
      if baito.payday <= limit:
          rank[baito.payday].append(baito)
  
  accessible = []
  count = 1
  while baitolist and count <= limit:  # 最悪O(limit)
      for baito in rank[count]:
          accessible.append(baito)
      accessible.sort()
      if accessible != []:
          dowork = accessible.pop()
          summoney += dowork.money
      count += 1
  
  
  print(summoney)
  
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"}]