Buggy python submission (verdict: Time Limit Exceeded):
import heapq

N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
TF = [False for _ in range(N)]
kouho = []
ans = 0

# 後ろから見て優先度付きキューで取り出す
mae = 0
for i in reversed(range(M)):
    mae += 1
    kouho = []
    for n in range(N):
        if AB[n][0] <= mae and TF[n] != True:
            kouho.append([AB[n][1] * (-1), n])
    heapq.heapify(kouho)
    
    if kouho:
        b, n = heapq.heappop(kouho)
        ans += b*(-1)
        TF[n] = True
    
print(ans)

Fixed submission:
import heapq

N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
kouho = []
ans = 0

AB = sorted(AB)

# 後ろから見て優先度付きキューで取り出す
n = 0
heapq.heapify(kouho)
for i in range(1, M+1):
    if n < N:
        while AB[n][0] <= i:
            heapq.heappush(kouho, AB[n][1] * (-1))
            n += 1
            if n >= N:
                break
    
    if kouho:
        b = heapq.heappop(kouho)
        ans += b*(-1)
    
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq

N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
TF = [False for _ in range(N)]
kouho = []
ans = 0

# 後ろから見て優先度付きキューで取り出す
mae = 0
for i in reversed(range(M)):
    mae += 1
    kouho = []
    for n in range(N):
        if AB[n][0] <= mae and TF[n] != True:
            kouho.append([AB[n][1] * (-1), n])
    heapq.heapify(kouho)
    
    if kouho:
        b, n = heapq.heappop(kouho)
        ans += b*(-1)
        TF[n] = True
    
print(ans)

Fixed submission:
import heapq

N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
kouho = []
ans = 0

AB = sorted(AB)

# 後ろから見て優先度付きキューで取り出す
n = 0
heapq.heapify(kouho)
for i in range(1, M+1):
    if n < N:
        while AB[n][0] <= i:
            heapq.heappush(kouho, AB[n][1] * (-1))
            n += 1
            if n >= N:
                break
    
    if kouho:
        b = heapq.heappop(kouho)
        ans += b*(-1)
    
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq

N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
TF = [False for _ in range(N)]
kouho = []
ans = 0

# 後ろから見て優先度付きキューで取り出す
mae = 0
for i in reversed(range(M)):
    mae += 1
    for n in range(N):
        if AB[n][0] <= mae and TF[n] != True:
            kouho.append([n, AB[n][0], AB[n][1]])
    kouho = sorted(kouho, key=lambda x: x[2], reverse=True)
    if len(kouho) != 0:
        n, A, B = kouho[0]
        TF[n] = True
        ans += B
        kouho = []
    
print(ans)

Fixed submission:
import heapq

N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
kouho = []
ans = 0

AB = sorted(AB)

# 後ろから見て優先度付きキューで取り出す
n = 0
heapq.heapify(kouho)
for i in range(1, M+1):
    if n < N:
        while AB[n][0] <= i:
            heapq.heappush(kouho, AB[n][1] * (-1))
            n += 1
            if n >= N:
                break
    
    if kouho:
        b = heapq.heappop(kouho)
        ans += b*(-1)
    
print(ans)

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
box = []
for i in range(N):
    box.append(list(map(int, input().split())))
box = sorted(box, reverse=True, key=lambda x: x[0])
box = sorted(box, reverse=True, key=lambda x: x[1])

ans = 0

for i in range(M):
    if i == 0:
        j = 0
    while box[j][0] > M - i:
        j += 1
        if j >= N-1:
            break
    else:
        ans += box[j][1]
        j += 1
        if j >= N-1:
            break

print(ans)

Fixed submission:
import heapq

N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
kouho = []
ans = 0

AB = sorted(AB)

# 後ろから見て優先度付きキューで取り出す
n = 0
heapq.heapify(kouho)
for i in range(1, M+1):
    if n < N:
        while AB[n][0] <= i:
            heapq.heappush(kouho, AB[n][1] * (-1))
            n += 1
            if n >= N:
                break
    
    if kouho:
        b = heapq.heappop(kouho)
        ans += b*(-1)
    
print(ans)