Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter

N, M = map(int, input().split())
pairs = [tuple(map(int, input().split())) for _ in range(N)]
pairs.sort(key = itemgetter(1), reverse = True)

A_max = M + 1
schedule = [0] * (M+1)
for A, B in pairs:
    if A_max == 1:
        break
    elif A < A_max:
        for i in range(A, A_max):
            if schedule[i] == 0:
                schedule[i] = B
                break
        else:
            A_max = A
    
print(sum(schedule))            


Fixed submission:
from operator import itemgetter
import heapq

N, M = map(int, input().split())
pairs = [tuple(map(int, input().split())) for _ in range(N)]
pairs.sort(key = itemgetter(0))

heap = []
reward = 0
index_of_pairs = 0   
for i in range(1, M+1):
    while index_of_pairs < N and pairs[index_of_pairs][0] <= i:
        heapq.heappush(heap, -pairs[index_of_pairs][1])
        index_of_pairs += 1
    if heap:
        reward -= heapq.heappop(heap)

print(reward)

Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter

N, M = map(int, input().split())
pairs = [tuple(map(int, input().split())) for _ in range(N)]
pairs.sort(key = itemgetter(1), reverse = True)

A_max = M + 1
schedule = [0] * (M+1)
for A, B in pairs:
    if A_max == 1:
        break
    elif A < A_max:
        for i in range(A, A_max):
            if schedule[i] == 0:
                schedule[i] = B
                break
        else:
            A_max = A
    


print(sum(schedule))            


Fixed submission:
from operator import itemgetter
import heapq

N, M = map(int, input().split())
pairs = [tuple(map(int, input().split())) for _ in range(N)]
pairs.sort(key = itemgetter(0))

heap = []
reward = 0
index_of_pairs = 0   
for i in range(1, M+1):
    while index_of_pairs < N and pairs[index_of_pairs][0] <= i:
        heapq.heappush(heap, -pairs[index_of_pairs][1])
        index_of_pairs += 1
    if heap:
        reward -= heapq.heappop(heap)

print(reward)

Buggy python submission (verdict: Runtime Error):
from operator import itemgetter

N, M = map(int, input().split())
pairs = [tuple(map(int, input().split())) for _ in range(N)]
pairs.sort(key = itemgetter(1), reverse = True)

A_max = M
schedule = [0] * (M+1)
for A, B in pairs:
    for i in range(A, A_max+1):
        if schedule[i] == 0:
            schedule[i] = B
            break
    else:
        A_max = A
    


print(sum(schedule))            


Fixed submission:
from operator import itemgetter
import heapq

N, M = map(int, input().split())
pairs = [tuple(map(int, input().split())) for _ in range(N)]
pairs.sort(key = itemgetter(0))

heap = []
reward = 0
index_of_pairs = 0   
for i in range(1, M+1):
    while index_of_pairs < N and pairs[index_of_pairs][0] <= i:
        heapq.heappush(heap, -pairs[index_of_pairs][1])
        index_of_pairs += 1
    if heap:
        reward -= heapq.heappop(heap)

print(reward)

Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter

N, M = map(int, input().split())
pairs = [tuple(map(int, input().split())) for _ in range(N)]

pairs.sort(key = itemgetter(1), reverse = True)
schedule = [0] * (M+1)
for A, B in pairs:
    for i in range(A, M+1):
        if schedule[i] == 0:
            schedule[i] = B
            break

print(sum(schedule))            


Fixed submission:
from operator import itemgetter
import heapq

N, M = map(int, input().split())
pairs = [tuple(map(int, input().split())) for _ in range(N)]
pairs.sort(key = itemgetter(0))

heap = []
reward = 0
index_of_pairs = 0   
for i in range(1, M+1):
    while index_of_pairs < N and pairs[index_of_pairs][0] <= i:
        heapq.heappush(heap, -pairs[index_of_pairs][1])
        index_of_pairs += 1
    if heap:
        reward -= heapq.heappop(heap)

print(reward)