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

N, M = map(int, input().split())
AB = [False]*N
for i in range(N):
    A, B = map(int, input().split())
    AB[i] = (A, -B)

AB.sort()
hq = []

ans, ct, idx = 0, 0, 0
for i in range(1, M+1):
    for j in AB[idx:]:
        A, B = j
        if A == i:
            heapq.heappush(hq, B)
            ct += 1
        else:
            idx = ct
            break
    if hq:
        B = heapq.heappop(hq)
        ans -= B
print(ans)


Fixed submission:
import heapq

N, M = map(int, input().split())
W = [[] for _ in range(100005)]
for i in range(N):
    A, B = map(int, input().split())
    W[A].append(B)

hq = []

ans, ct, idx = 0, 0, 0
for A in range(1, M+1):
    for B in W[A]:
        heapq.heappush(hq, -B)
    if hq:
        B = heapq.heappop(hq)
        ans -= B
print(ans)

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

N, M = map(int, input().split())
AB = [False]*N
for i in range(N):
    A, B = map(int, input().split())
    AB[i] = (-B, A)

AB.sort(key=lambda x: x[1])
hq = []

ans, ct, idx = 0, 0, 0
for i in range(1, M+1):
    for j in AB[idx:]:
        if j[1] == i:
            heapq.heappush(hq, j)
            ct += 1
        else:
            idx = ct
            break
    if hq:
        B, A = heapq.heappop(hq)
        ans -= B
print(ans)


Fixed submission:
import heapq

N, M = map(int, input().split())
W = [[] for _ in range(100005)]
for i in range(N):
    A, B = map(int, input().split())
    W[A].append(B)

hq = []

ans, ct, idx = 0, 0, 0
for A in range(1, M+1):
    for B in W[A]:
        heapq.heappush(hq, -B)
    if hq:
        B = heapq.heappop(hq)
        ans -= B
print(ans)

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

N, M = map(int, input().split())
AB = [False]*N
for i in range(N):
    A, B = map(int, input().split())
    AB[i] = (-B, A)

AB.sort(key=lambda x: x[1])
hq = []

ans, ct, idx = 0, 0, 0
for i in range(1, M+1):
    for j in AB[idx:]:
        if j[1] == i:
            heapq.heappush(hq, j)
            ct += 1
        else:
            idx = ct
            break
    while hq:
        B, A = heapq.heappop(hq)
        if A <= i:
            ans -= B
            break
print(ans)


Fixed submission:
import heapq

N, M = map(int, input().split())
W = [[] for _ in range(100005)]
for i in range(N):
    A, B = map(int, input().split())
    W[A].append(B)

hq = []

ans, ct, idx = 0, 0, 0
for A in range(1, M+1):
    for B in W[A]:
        heapq.heappush(hq, -B)
    if hq:
        B = heapq.heappop(hq)
        ans -= B
print(ans)

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

N, M = map(int, input().split())
AB = [False]*N
for i in range(N):
    A, B = map(int, input().split())
    AB[i] = (-B, A)

AB.sort(key=lambda x: x[1])
hq = []

ans, ct, idx = 0, 0, 0
for i in range(1, M+1):
    for j in AB[idx:]:
        if j[1] == i:
            heapq.heappush(hq, j)
            ct += 1
        else:
            idx = ct
            break
    while hq:
        B, A = heapq.heappop(hq)
        if A <= i:
            ans -= B
            break
print(ans)


Fixed submission:
import heapq

N, M = map(int, input().split())
W = [[] for _ in range(100005)]
for i in range(N):
    A, B = map(int, input().split())
    W[A].append(B)

hq = []

ans, ct, idx = 0, 0, 0
for A in range(1, M+1):
    for B in W[A]:
        heapq.heappush(hq, -B)
    if hq:
        B = heapq.heappop(hq)
        ans -= B
print(ans)