Buggy python submission (verdict: Time Limit Exceeded):
class SegTree:
    init_val = -1  # 問題によって変える

    def __init__(self, init_l):
        self.n = len(init_l)
        self.n0 = 2 ** (self.n - 1).bit_length()  # n以上で最小の2のべき乗
        self.l = [self.init_val] * 2 * self.n0  # 区間外の値を初期化
        for i in range(self.n):
            self.l[i + self.n0 - 1] = init_l[i]
        for i in range(self.n0 - 2, -1, -1):
            self.l[i] = self.func(self.l[2 * i + 1], self.l[2 * i + 2])

    # 問題によって変える
    def func(self, x, y):
        return max(x, y)

    def update(self, i, new):
        i += self.n0 - 1
        self.l[i] = new
        while i + 1:
            i = (i - 1) // 2
            self.l[i] = self.func(self.l[i * 2 + 1], self.l[i * 2 + 2])

    # Gets min between [a, b)
    def query(self, a, b):
        return self._query(a, b, 0, 0, self.n0)

    def _query(self, a, b, k, l, r):
        if r <= a or b <= l:
            return self.init_val
        if a <= l and r <= b:
            return self.l[k]
        vl = self._query(a, b, k * 2 + 1, l, (l + r) // 2)
        vr = self._query(a, b, k * 2 + 2, (l + r) // 2, r)
        return self.func(vl, vr)

import sys
input = sys.stdin.readline

n, m = map(int, input().split())
ba = []
for _ in range(n):
    a, b = map(int, input().split())
    ba.append([b, a])
ba.sort(reverse=True)
days = [x for x in range(m)]
ans = 0
s = SegTree(days)
for x in ba:
    a = x[1]
    b = x[0]
    lim = m - a
    if lim < 0:
        continue
    d = s.query(0, lim+1)
    if d >= 0:
        ans += b
        s.update(d, -1)
print(ans)


Fixed submission:
import sys
input = sys.stdin.readline
from heapq import *


n, m = map(int, input().split())
tmp = [ [] for _ in range(10 ** 5 + 1) ]
for _ in range(n):
    a, b = map(int, input().split())
    tmp[a].append(b)

h = []
ans = 0
for i in range(1, m+1):
    for bb in tmp[i]:
        heappush(h, -bb)
    if h:
        ans += -heappop(h)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from bisect import bisect_right
input = sys.stdin.readline

n, m = map(int, input().split())
ba = []
for _ in range(n):
    a, b = map(int, input().split())
    ba.append([b, a])
ba.sort(reverse=True)
days = [x for x in range(m)]
ans = 0
for x in ba:
    a = x[1]
    b = x[0]
    lim = m - a
    d = bisect_right(days, lim)
    if d > 0:
        ans += b
        days = days[:d-1] + days[d:]
print(ans)


Fixed submission:
import sys
input = sys.stdin.readline
from heapq import *


n, m = map(int, input().split())
tmp = [ [] for _ in range(10 ** 5 + 1) ]
for _ in range(n):
    a, b = map(int, input().split())
    tmp[a].append(b)

h = []
ans = 0
for i in range(1, m+1):
    for bb in tmp[i]:
        heappush(h, -bb)
    if h:
        ans += -heappop(h)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from bisect import bisect_right
input = sys.stdin.readline

n, m = map(int, input().split())
ba = []
for _ in range(n):
    a, b = map(int, input().split())
    ba.append([b, a])
ba.sort(reverse=True)
days = [x for x in range(m)]
ans = 0
for x in ba:
    a = x[1]
    b = x[0]
    lim = m - a
    d = bisect_right(days, lim)
    if d > 0:
        ans += b
        days = days[:d-1] + days[d:]
print(ans)

Fixed submission:
import sys
input = sys.stdin.readline
from heapq import *


n, m = map(int, input().split())
tmp = [ [] for _ in range(10 ** 5 + 1) ]
for _ in range(n):
    a, b = map(int, input().split())
    tmp[a].append(b)

h = []
ans = 0
for i in range(1, m+1):
    for bb in tmp[i]:
        heappush(h, -bb)
    if h:
        ans += -heappop(h)
print(ans)