Buggy python submission (verdict: Runtime Error):
def update(i, x):
    i += 1
    while i < m + 1:
        bit[i] += x
        i += i & -i

def get_sum(i):
    i += 1
    s = 0
    while i > 0:
        s += bit[i]
        i -= i & -i
    return s

n, m = map(int, input().split())
bit = [0] * (m + 1)
ab = [list(map(int, input().split())) for _ in range(n)]
ab.sort(key=lambda x: (-x[1], -x[0]))
cnt = 0
ans = 0
for a, b in ab:
    if a <= m:
        a = m - a
        if get_sum(a) < a + 1:
            update(a, 1)
            cnt += 1
            ans += b
    print(a,b,bit,get_sum(a))
    if cnt == m: break
print(ans)


Fixed submission:
# pdf
from heapq import *

n, m = map(int, input().split())
hp = []
for _ in range(n):
    a, b = map(int, input().split())
    heappush(hp, [a, b])
ans = 0
mx = b
ch = []
a, b = heappop(hp)
flag = 1
for i in range(1, m + 1):
    while a <= i:
        if flag:
            heappush(ch, -b)
            flag = 0
        if not hp:break
        a, b = heappop(hp)
        flag = 1
    if ch:
        ans -= heappop(ch)
print(ans)

Buggy python submission (verdict: Runtime Error):
def update(i, x):
    i += 1
    while i < n + 1:
        bit[i] += x
        i += i & -i

def get_sum(i):
    i += 1
    s = 0
    while i > 0:
        s += bit[i]
        i -= i & -i
    return s

n, m = map(int, input().split())
bit = [0] * (m + 2)
ab = [list(map(int, input().split())) for _ in range(n)]
ab.sort(key=lambda x:(-x[1],-x[0]))
cnt = 0
ans = 0
for a,b in ab:
    if a <= m:
        a = m - a
        if get_sum(a) < a + 1:
            update(a, 1)
            cnt += 1
            ans += b
    if cnt==m:break
print(ans)


Fixed submission:
# pdf
from heapq import *

n, m = map(int, input().split())
hp = []
for _ in range(n):
    a, b = map(int, input().split())
    heappush(hp, [a, b])
ans = 0
mx = b
ch = []
a, b = heappop(hp)
flag = 1
for i in range(1, m + 1):
    while a <= i:
        if flag:
            heappush(ch, -b)
            flag = 0
        if not hp:break
        a, b = heappop(hp)
        flag = 1
    if ch:
        ans -= heappop(ch)
print(ans)

Buggy python submission (verdict: Runtime Error):
from heapq import *

def update(i, x):
    i += 1
    while i < n + 1:
        bit[i] += x
        i += i & -i

def get_sum(i):
    i += 1
    s = 0
    while i > 0:
        s += bit[i]
        i -= i & -i
    return s

n, m = map(int, input().split())
bit = [0] * (m + 2)
hp = []
for _ in range(n):
    a, b = map(int, input().split())
    heappush(hp, [-b, a])
cnt = 0
ans = 0
while cnt < m and hp:
    b, a = heappop(hp)
    if a <= m:
        a = m - a
        if get_sum(a) < a + 1:
            update(a, 1)
            cnt += 1
            ans -= b
print(ans)


Fixed submission:
# pdf
from heapq import *

n, m = map(int, input().split())
hp = []
for _ in range(n):
    a, b = map(int, input().split())
    heappush(hp, [a, b])
ans = 0
mx = b
ch = []
a, b = heappop(hp)
flag = 1
for i in range(1, m + 1):
    while a <= i:
        if flag:
            heappush(ch, -b)
            flag = 0
        if not hp:break
        a, b = heappop(hp)
        flag = 1
    if ch:
        ans -= heappop(ch)
print(ans)

Buggy python submission (verdict: Runtime Error):
from heapq import *

n, m = map(int, input().split())
hp = []
used = [0] * (m + 1)
for _ in range(n):
    a, b = map(int, input().split())
    heappush(hp, [-b, a])
while hp:
    b, a = heappop(hp)
    while a <= m:
        if used[a] == 0: break
        a += 1
    if a == m + 1:
        continue
    else:
        used[a] = -b
print(sum(used))


Fixed submission:
# pdf
from heapq import *

n, m = map(int, input().split())
hp = []
for _ in range(n):
    a, b = map(int, input().split())
    heappush(hp, [a, b])
ans = 0
mx = b
ch = []
a, b = heappop(hp)
flag = 1
for i in range(1, m + 1):
    while a <= i:
        if flag:
            heappush(ch, -b)
            flag = 0
        if not hp:break
        a, b = heappop(hp)
        flag = 1
    if ch:
        ans -= heappop(ch)
print(ans)