Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
AB_list = [tuple(map(int, input().split())) for i in range(N)]

sorted_AB_list = sorted(AB_list, key=lambda x:x[0], reverse=False)
sorted_AB_list = sorted(sorted_AB_list, key=lambda x:x[1], reverse=True)

money = 0

for i in range(M):
  remain_days = i+1
  
  for (A, B) in sorted_AB_list:
    if A <= remain_days:
      money += B
      sorted_AB_list.remove((A, B))
      break

print(money)

Fixed submission:
from heapq import *
n, m = map(int, input().split())
jobs = [tuple(map(int, input().split())) for _ in range(n)]
jobs.sort(reverse=True)
ans = 0
q = []
for i in range(1, m+1):
    while jobs and jobs[-1][0] <= i:
        heappush(q, -jobs.pop(-1)[1])
    if q:
        ans -= heappop(q)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
AB_list = [tuple(map(int, input().split())) for i in range(N)]

# sorted_AB_list = sorted(AB_list, key=lambda x:x[0], reverse=False)
# sorted_AB_list = sorted(sorted_AB_list, key=lambda x:x[1], reverse=True)

sorted_AB_list = sorted(AB_list, key=lambda x:x[1], reverse=True)

money = 0

# 最終日から逆算
for i in range(M): # 残り日数
  remain_days = i+1
  
  for (A, B) in sorted_AB_list:
    if A <= remain_days:
      money += B
      sorted_AB_list.remove((A, B))
      break
  
print(money)
    
  

Fixed submission:
from heapq import *
n, m = map(int, input().split())
jobs = [tuple(map(int, input().split())) for _ in range(n)]
jobs.sort(reverse=True)
ans = 0
q = []
for i in range(1, m+1):
    while jobs and jobs[-1][0] <= i:
        heappush(q, -jobs.pop(-1)[1])
    if q:
        ans -= heappop(q)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
AB_list = [tuple(map(int, input().split())) for i in range(N)]

sorted_AB_list = sorted(AB_list, key=lambda x:x[0], reverse=False)
sorted_AB_list = sorted(sorted_AB_list, key=lambda x:x[1], reverse=True)

money = 0

# 最終日から逆算
for i in range(M): # 残り日数
  remain_days = i+1
  
  for (A, B) in sorted_AB_list:
    if A <= remain_days:
      money += B
      sorted_AB_list.remove((A, B))
      break
  
print(money)
    
  

Fixed submission:
from heapq import *
n, m = map(int, input().split())
jobs = [tuple(map(int, input().split())) for _ in range(n)]
jobs.sort(reverse=True)
ans = 0
q = []
for i in range(1, m+1):
    while jobs and jobs[-1][0] <= i:
        heappush(q, -jobs.pop(-1)[1])
    if q:
        ans -= heappop(q)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
AB_list = [tuple(map(int, input().split())) for i in range(N)]

sorted_AB_list = sorted(AB_list, key=lambda x:x[0], reverse=True)
sorted_AB_list = sorted(sorted_AB_list, key=lambda x:x[1], reverse=True)

money = 0

# for A, B in sorted_AB_list:
#  print(A, B)

# 最終日から逆算
for i in range(M): # 残り日数
  remain_days = i+1
  
  for A, B in sorted_AB_list:
    if A <= remain_days:
      money += B
      sorted_AB_list.remove((A, B))
      break
      
print(money)
    
  

Fixed submission:
from heapq import *
n, m = map(int, input().split())
jobs = [tuple(map(int, input().split())) for _ in range(n)]
jobs.sort(reverse=True)
ans = 0
q = []
for i in range(1, m+1):
    while jobs and jobs[-1][0] <= i:
        heappush(q, -jobs.pop(-1)[1])
    if q:
        ans -= heappop(q)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
AB_list = [tuple(map(int, input().split())) for i in range(N)]
               
# print(AB_list)
# sorted_AB_list = sorted(AB_list, key=lambda x:x[0], reverse=True)
# sorted_AB_list = sorted(sorted_AB_list, key=lambda x:x[1], reverse=True)

sorted_AB_list = sorted(AB_list, key=lambda x:x[1], reverse=True)

# for A, B in sorted_AB_list:
#   print(A, B)

money = 0

# 最終日から逆算して考える
for i in range(M): # 残り日数
  remain_days = i+1
  
  for A, B in sorted_AB_list:
    if A <= remain_days:
      # print(A, B)
      money += B
      sorted_AB_list.remove((A, B))
      break
      
print(money)
    
  

Fixed submission:
from heapq import *
n, m = map(int, input().split())
jobs = [tuple(map(int, input().split())) for _ in range(n)]
jobs.sort(reverse=True)
ans = 0
q = []
for i in range(1, m+1):
    while jobs and jobs[-1][0] <= i:
        heappush(q, -jobs.pop(-1)[1])
    if q:
        ans -= heappop(q)
print(ans)