Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
AB = []
for i in range(n):
    a, b  = map(int, input().split())
    if a <= m:
        AB.append((a, b))
AB.sort(key=lambda ab: -ab[1])
counts = [0] * m
cands = [0] * m
total = 0
for a, b in AB:
    if a > m:
        continue
    for i in range(a - 1 + counts[a - 1], m):
        if cands[i] > 0:
            continue
        cands[i] = b
        counts[a - 1] += 1
        total += 1
        break
    if total == m:
        break
print(sum(cands))


Fixed submission:
import heapq

n, m = map(int, input().split())

cands = [[] for _ in range(m)]
for _ in range(n):
    a, b = map(int, input().split())
    if a <= m:
        cands[a - 1].append(-b)

heapq.heapify(cands[0])
total = 0
for i in range(m):
    if len(cands[0]) > 0:
        total -= heapq.heappop(cands[0])
    if i < m - 1:
        for c in cands[i + 1]:
            heapq.heappush(cands[0], c)

print(total)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(n)]
AB.sort(key=lambda ab: -ab[1])
counts = [0] * m
used = [False] * m
total = 0
score = 0
for a, b in AB:
    if a > m:
        continue
    for i in range(a - 1 + counts[a - 1], m):
        if used[i]:
            continue
        used[i] = True
        counts[a - 1] += 1
        total += 1
        score += b
        break
    if total == m:
        break
print(score)


Fixed submission:
import heapq

n, m = map(int, input().split())

cands = [[] for _ in range(m)]
for _ in range(n):
    a, b = map(int, input().split())
    if a <= m:
        cands[a - 1].append(-b)

heapq.heapify(cands[0])
total = 0
for i in range(m):
    if len(cands[0]) > 0:
        total -= heapq.heappop(cands[0])
    if i < m - 1:
        for c in cands[i + 1]:
            heapq.heappush(cands[0], c)

print(total)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(n)]
AB.sort(key=lambda ab: -ab[1])
counts = [0] * m
cands = [0] * m
total = 0
for j in range(n):
    if AB[j][0] > m:
        continue
    for i in range(AB[j][0] - 1 + counts[AB[j][0] - 1], m):
        if cands[i] > 0:
            continue
        cands[i] = AB[j][1]
        counts[AB[j][0] - 1] += 1
        total += 1
        break
    if total == m:
        break
print(sum(cands))


Fixed submission:
import heapq

n, m = map(int, input().split())

cands = [[] for _ in range(m)]
for _ in range(n):
    a, b = map(int, input().split())
    if a <= m:
        cands[a - 1].append(-b)

heapq.heapify(cands[0])
total = 0
for i in range(m):
    if len(cands[0]) > 0:
        total -= heapq.heappop(cands[0])
    if i < m - 1:
        for c in cands[i + 1]:
            heapq.heappush(cands[0], c)

print(total)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(n)]
AB.sort(key=lambda ab: -ab[1])
counts = [0] * m
cands = [0] * m
total = 0
for a, b in AB:
    if a > m:
        continue
    for i in range(a - 1 + counts[a - 1], m):
        if cands[i] > 0:
            continue
        cands[i] = b
        counts[a - 1] += 1
        total += 1
        break
    if total == m:
        break
print(sum(cands))


Fixed submission:
import heapq

n, m = map(int, input().split())

cands = [[] for _ in range(m)]
for _ in range(n):
    a, b = map(int, input().split())
    if a <= m:
        cands[a - 1].append(-b)

heapq.heapify(cands[0])
total = 0
for i in range(m):
    if len(cands[0]) > 0:
        total -= heapq.heappop(cands[0])
    if i < m - 1:
        for c in cands[i + 1]:
            heapq.heappush(cands[0], c)

print(total)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(n)]
AB.sort(key=lambda ab: -ab[1])
counts = [0] * m
cands = [0] * m
total = 0
for a, b in AB:
    if a <= m:
        for i in range(a - 1 + counts[a - 1], m):
            if cands[i] > 0:
                continue
            cands[i] = b
            counts[a - 1] += 1
            total += 1
            break
    if total == m:
        break
print(sum(cands))


Fixed submission:
import heapq

n, m = map(int, input().split())

cands = [[] for _ in range(m)]
for _ in range(n):
    a, b = map(int, input().split())
    if a <= m:
        cands[a - 1].append(-b)

heapq.heapify(cands[0])
total = 0
for i in range(m):
    if len(cands[0]) > 0:
        total -= heapq.heappop(cands[0])
    if i < m - 1:
        for c in cands[i + 1]:
            heapq.heappush(cands[0], c)

print(total)

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
AB = [None] * n
maxa = 0
for i in range(n):
    AB[i] = tuple(map(int, input().split()))
    if AB[i][0] > maxa:
        maxa = AB[i][0]
AB.sort(key=lambda ab: -ab[1])
cands = [AB[i][1] for i in range(m - maxa)]
counts = [0] * m
for i in range(m - maxa, n):
    if AB[i][0] <= m and sum(counts[AB[i][0] - 1:]) <= m - AB[i][0]:
        cands.append(AB[i][1])
        counts[AB[i][0] - 1] += 1
print(sum(cands[:m]))


Fixed submission:
import heapq

n, m = map(int, input().split())

cands = [[] for _ in range(m)]
for _ in range(n):
    a, b = map(int, input().split())
    if a <= m:
        cands[a - 1].append(-b)

heapq.heapify(cands[0])
total = 0
for i in range(m):
    if len(cands[0]) > 0:
        total -= heapq.heappop(cands[0])
    if i < m - 1:
        for c in cands[i + 1]:
            heapq.heappush(cands[0], c)

print(total)

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
cands = [[] for _ in range(m)]
counts = [0] * m
for _ in range(n):
    a, b = map(int, input().split())
    if a <= m:
        cands[a - 1].append(b)
        counts[a - 1] += 1
for i in range(m):
    if cands[i]:
        cands[i].sort()
score = 0
total = 0
for i in range(m):
    maxv = 0
    idx = -1
    for j in range(i + 1):
        if counts[j] and maxv < cands[counts[j] - 1]:
            maxv = cands[counts[j] - 1]
            idx = j
    if idx >= 0:
        score += maxv
        counts[idx] -= 1
    total += 1
    if total >= n:
        break
print(score)

Fixed submission:
import heapq

n, m = map(int, input().split())

cands = [[] for _ in range(m)]
for _ in range(n):
    a, b = map(int, input().split())
    if a <= m:
        cands[a - 1].append(-b)

heapq.heapify(cands[0])
total = 0
for i in range(m):
    if len(cands[0]) > 0:
        total -= heapq.heappop(cands[0])
    if i < m - 1:
        for c in cands[i + 1]:
            heapq.heappush(cands[0], c)

print(total)