Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque


N, M = map(int, input().split())
ab_list = [ list(map(int, input().split())) for i in range(N)]

score_list = [[] for i in range(M+1)]


for ab in ab_list:
    if ab[0] <= M:
        score_list[ab[0]].append(ab[1])

scores = [deque() for i in range(M+1)]
for i in range(len(score_list)):
    scores[i] = deque(sorted(score_list[i], reverse=True))

ans = 0
for i in range(0, M+1):

    max_idx = -1
    max_n = -1
    for j in range(i+1):
        if len(scores[j]) == 0:
          continue
        if max_n < scores[j][0]:
            max_n = scores[j][0]
            max_idx = j

    if max_idx > 0:
        ans += scores[max_idx].popleft()

print(ans)

Fixed submission:
import heapq


N, M = map(int, input().split())
ab_list = [ list(map(int, input().split())) for i in range(N)]

score_list = [[] for i in range(M+1)]


for ab in ab_list:
    if ab[0] <= M:
        score_list[ab[0]].append(ab[1])


ans = 0
candidates = []

for i in range(0, M+1):

    for s in score_list[i]:
        heapq.heappush(candidates, -s)
    if len(candidates) == 0:
        continue
    ans -= heapq.heappop(candidates)


print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque


def merge(a_list, b_list):
    res = deque()

    while len(a_list) > 0 and len(b_list) > 0:
        if a_list[0] > b_list[0]:
            res.append(a_list.popleft())
        else:
            res.append(b_list.popleft())


    if len(a_list) > 0:
        res.extend(a_list)
    if len(b_list) > 0:
        res.extend(b_list)

    return res


N, M = map(int, input().split())
ab_list = [ list(map(int, input().split())) for i in range(N)]

score_list = [[] for i in range(M+1)]


for ab in ab_list:
    if ab[0] <= M:
        score_list[ab[0]].append(ab[1])

scores = [deque() for i in range(M+1)]
for i in range(len(score_list)):
    scores[i] = deque(sorted(score_list[i], reverse=True))
ans = 0
for i in range(0, M+1):
    if len(scores[i]) == 0:
        continue
    ans += scores[i].popleft()
    if i < M and len(scores[i]) > 0:
        scores[i+1] = merge(scores[i], scores[i+1])


print(ans)

Fixed submission:
import heapq


N, M = map(int, input().split())
ab_list = [ list(map(int, input().split())) for i in range(N)]

score_list = [[] for i in range(M+1)]


for ab in ab_list:
    if ab[0] <= M:
        score_list[ab[0]].append(ab[1])


ans = 0
candidates = []

for i in range(0, M+1):

    for s in score_list[i]:
        heapq.heappush(candidates, -s)
    if len(candidates) == 0:
        continue
    ans -= heapq.heappop(candidates)


print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def merge(a_list, b_list):
    res = []

    while len(a_list) > 0 and len(b_list) > 0:
        if a_list[0] > b_list[0]:
            res.append(a_list.pop(0))
        else:
            res.append((b_list.pop(0)))

    if len(a_list) > 0:
        res.extend(a_list)
    if len(b_list) > 0:
        res.extend(b_list)

    return res


N, M = map(int, input().split())
ab_list = [ list(map(int, input().split())) for i in range(N)]

score_list = [[] for i in range(M+1)]


for ab in ab_list:
    if ab[0] <= M:
        score_list[ab[0]].append(ab[1])
for i in range(len(score_list)):
    score_list[i].sort(reverse=True)

ans = 0
for i in range(0, M+1):
    if len(score_list[i]) == 0:
        continue
    ans += score_list[i].pop(0)
    if i < M and len(score_list[i]) > 0:
        score_list[i+1] = merge(score_list[i], score_list[i+1])

print(ans)

Fixed submission:
import heapq


N, M = map(int, input().split())
ab_list = [ list(map(int, input().split())) for i in range(N)]

score_list = [[] for i in range(M+1)]


for ab in ab_list:
    if ab[0] <= M:
        score_list[ab[0]].append(ab[1])


ans = 0
candidates = []

for i in range(0, M+1):

    for s in score_list[i]:
        heapq.heappush(candidates, -s)
    if len(candidates) == 0:
        continue
    ans -= heapq.heappop(candidates)


print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
N, M = map(int, input().split())
ab_list = [ list(map(int, input().split())) for i in range(N)]

score_list = [[] for i in range(M+1)]


for ab in ab_list:
    if ab[0] <= M:
        score_list[ab[0]].append(ab[1])

ans = 0
for i in range(0, M+1):
    if len(score_list[i]) == 0:
        continue
    score_list[i].sort(reverse=True)
    ans += score_list[i].pop(0)
    if i < M and len(score_list[i]) > 0:
        score_list[i+1].extend(score_list[i])

print(ans)

Fixed submission:
import heapq


N, M = map(int, input().split())
ab_list = [ list(map(int, input().split())) for i in range(N)]

score_list = [[] for i in range(M+1)]


for ab in ab_list:
    if ab[0] <= M:
        score_list[ab[0]].append(ab[1])


ans = 0
candidates = []

for i in range(0, M+1):

    for s in score_list[i]:
        heapq.heappush(candidates, -s)
    if len(candidates) == 0:
        continue
    ans -= heapq.heappop(candidates)


print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
N, M = map(int, input().split())
ab_list = [ list(map(int, input().split())) for i in range(N)]

score_list = [[] for i in range(M+1)]


for ab in ab_list:
    if ab[0] <= M:
        score_list[ab[0]].append(ab[1])

ans = 0
for i in range(0, M+1):
    if len(score_list[i]) == 0:
        continue
    scores = deque(sorted(score_list[i]))
    ans += scores.pop()
    if i < M and len(scores) > 0:
        score_list[i+1].extend(scores)

print(ans)

Fixed submission:
import heapq


N, M = map(int, input().split())
ab_list = [ list(map(int, input().split())) for i in range(N)]

score_list = [[] for i in range(M+1)]


for ab in ab_list:
    if ab[0] <= M:
        score_list[ab[0]].append(ab[1])


ans = 0
candidates = []

for i in range(0, M+1):

    for s in score_list[i]:
        heapq.heappush(candidates, -s)
    if len(candidates) == 0:
        continue
    ans -= heapq.heappop(candidates)


print(ans)