Buggy python submission (verdict: Time Limit Exceeded):
import heapq
import bisect
N, M = map(int, input().split())
ABs = [list(map(int, input().split())) for _ in range(N)]

lists = [[] for _ in range(10**5 + 1)]
availables = []
for A, B in ABs:
    bisect.insort(lists[A], B)

ans = 0
for i in range(1, M + 1):
    availables = list(heapq.merge(availables, lists[i]))
    if availables:
        ans += availables.pop()

print(ans)

Fixed submission:
import heapq
N, M = map(int, input().split())
ABs = [list(map(int, input().split())) for _ in range(N)]

heapq.heapify(ABs)
availables = []
ans = 0
for i in range(1, M + 1):
    while ABs:
        a = heapq.heappop(ABs)
        if a[0] == i:
            heapq.heappush(availables, -a[1])
        else:
            heapq.heappush(ABs, a)
            break
    if availables:
        ans -= heapq.heappop(availables)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import chain
import bisect
N, M = map(int, input().split())
ABs = [list(map(int, input().split())) for _ in range(N)]

lists = [[] for _ in range(10**5 + 1)]
availables = []
for A, B in ABs:
    bisect.insort(lists[A], B)

ans = 0
for i in range(1, M + 1):
    availables = sorted(chain(availables, lists[i]))
    if availables:
        ans += availables.pop()

print(ans)

Fixed submission:
import heapq
N, M = map(int, input().split())
ABs = [list(map(int, input().split())) for _ in range(N)]

heapq.heapify(ABs)
availables = []
ans = 0
for i in range(1, M + 1):
    while ABs:
        a = heapq.heappop(ABs)
        if a[0] == i:
            heapq.heappush(availables, -a[1])
        else:
            heapq.heappush(ABs, a)
            break
    if availables:
        ans -= heapq.heappop(availables)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
N, M = map(int, input().split())
ABs = [list(map(int, input().split())) for _ in range(N)]

lists = [[] for _ in range(10**5 + 1)]
availables = []
for A, B in ABs:
    bisect.insort(lists[A], B)
    
    ans = 0
for i in range(1, M + 1):
    for b in lists[i]:
        bisect.insort(availables, b)
    if availables:
        ans += availables.pop()

print(ans)


Fixed submission:
import heapq
N, M = map(int, input().split())
ABs = [list(map(int, input().split())) for _ in range(N)]

heapq.heapify(ABs)
availables = []
ans = 0
for i in range(1, M + 1):
    while ABs:
        a = heapq.heappop(ABs)
        if a[0] == i:
            heapq.heappush(availables, -a[1])
        else:
            heapq.heappush(ABs, a)
            break
    if availables:
        ans -= heapq.heappop(availables)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
ABs = [list(map(int, input().split())) for _ in range(N)]

ans = 0
for i in range(1, M + 1):
    max_ = [-1, 0]
    for AB in ABs:
        if AB[0] <= i and AB[1] > max_[1]:
            max_ = AB
    ans += max_[1]
    if max_[0] != -1:
        ABs.remove(max_)

print(ans)

Fixed submission:
import heapq
N, M = map(int, input().split())
ABs = [list(map(int, input().split())) for _ in range(N)]

heapq.heapify(ABs)
availables = []
ans = 0
for i in range(1, M + 1):
    while ABs:
        a = heapq.heappop(ABs)
        if a[0] == i:
            heapq.heappush(availables, -a[1])
        else:
            heapq.heappush(ABs, a)
            break
    if availables:
        ans -= heapq.heappop(availables)

print(ans)