Buggy python submission (verdict: Time Limit Exceeded):
import heapq
N, M = map(int, input().split())
w = [[] for i in range(M)]
for i in range(N):
    a, b = map(int, input().split())
    try:
        w[a-1].append(-b)
    except:
        pass
ans = 0
a = []
for i in range(M):
    a += w[i]
    heapq.heapify(a)
    try:
        ans += -heapq.heappop(a)
    except:
        pass
print(ans)

Fixed submission:
import heapq
N, M = map(int, input().split())
w = [[] for i in range(M)]
for i in range(N):
    a, b = map(int, input().split())
    try:
        w[a-1].append(-b)
    except:
        pass
ans = 0
a = []
heapq.heapify(a)
for i in range(M):
    for x in w[i]:
        heapq.heappush(a, x)
    try:
        ans += -heapq.heappop(a)
    except:
        pass
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
w = [[] for i in range(M)]
for i in range(N):
    a, b = map(int, input().split())
    try:
        w[a-1].append(b)
    except:
        pass
for x in w:
    x.sort()
ans = 0
for i in range(M):
    if i>=1:
        w[i] += w[i-1][:-1]
        w[i].sort()
    try:
        ans += w[i][-1]
    except:
        pass
print(ans)

Fixed submission:
import heapq
N, M = map(int, input().split())
w = [[] for i in range(M)]
for i in range(N):
    a, b = map(int, input().split())
    try:
        w[a-1].append(-b)
    except:
        pass
ans = 0
a = []
heapq.heapify(a)
for i in range(M):
    for x in w[i]:
        heapq.heappush(a, x)
    try:
        ans += -heapq.heappop(a)
    except:
        pass
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
w = [[] for i in range(M)]
for i in range(N):
    a, b = map(int, input().split())
    try:
        w[a-1].append(b)
    except:
        pass
w.reverse()
a= [[] for i in range(M)]
ans = 0
for i in range(M):
    a[i] = w[M-i-1]
    if i>=1:
        a[i-1].sort()
        a[i] += a[i-1][:-1]
    try:
        ans += max(a[i])
    except:
        pass
print(ans)

Fixed submission:
import heapq
N, M = map(int, input().split())
w = [[] for i in range(M)]
for i in range(N):
    a, b = map(int, input().split())
    try:
        w[a-1].append(-b)
    except:
        pass
ans = 0
a = []
heapq.heapify(a)
for i in range(M):
    for x in w[i]:
        heapq.heappush(a, x)
    try:
        ans += -heapq.heappop(a)
    except:
        pass
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
w = [[] for i in range(M)]
for i in range(N):
    a, b = map(int, input().split())
    try:
        w[a-1].append(b)
    except:
        pass
w.reverse()
a= [[] for i in range(M)]
ans = 0
for i in range(M):
    a[i] = w[M-i-1]
    if i>=1:
        a[i-1].sort()
        a[i] += a[i-1][:-1]
    try:
        ans += max(a[i])
    except:
        pass
print(ans)

Fixed submission:
import heapq
N, M = map(int, input().split())
w = [[] for i in range(M)]
for i in range(N):
    a, b = map(int, input().split())
    try:
        w[a-1].append(-b)
    except:
        pass
ans = 0
a = []
heapq.heapify(a)
for i in range(M):
    for x in w[i]:
        heapq.heappush(a, x)
    try:
        ans += -heapq.heappop(a)
    except:
        pass
print(ans)