Problem p02948 (python)

Fixed submission:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()

def _heappush_max(heap, item):
    heap.append(item)
    heapq._siftdown_max(heap, 0, len(heap)-1)
def _heappop_max(heap):
    """Maxheap version of a heappop."""
    lastelt = heap.pop()    # raises appropriate IndexError if heap is empty
    if heap:
        returnitem = heap[0]
        heap[0] = lastelt
        heapq._siftup_max(heap, 0)
        return returnitem
    return lastelt

def main():
    N, M = LI()
    jobsavailable = [[] for _ in range(10**5+1)]
    for _ in range(N):
        a, b = LI()
        if a > M:
            continue
        jobsavailable[M-a].append(b)
    cur_jobs = []
    cnt = 0
    for i in range(1, M+1):
        # a <= iのなかで最大のbがあるものをとりだす
        jobs = jobsavailable[M-i]
        for j in jobs:
            _heappush_max(cur_jobs, j)
        if cur_jobs != []:
            j = _heappop_max(cur_jobs)
            cnt += j
    print(cnt)

main()

Buggy submission (verdict: Runtime Error):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def main():
    N, M = LI()
    jobsavailable = [[] for _ in range(10**5+1)]
    for _ in range(N):
        a, b = LI()
        if a > M:
            continue
        jobsavailable[M-a].append(b)
    cur_jobs = []
    cnt = 0
    for i in range(1, M+1):
        # a <= iのなかで最大のbがあるものをとりだす
        jobs = jobsavailable[M-i]
        cur_jobs.extend(jobs)
        heapq._heapify_max(cur_jobs)
        if cur_jobs != []:
            j = heapq._heappop_max(cur_jobs)
            cnt += j
    print(cnt)

main()

Buggy submission (verdict: Runtime Error):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def main():
    N, M = LI()
    jobsavailable = [[] for _ in range(10**5+1)]
    for _ in range(N):
        a, b = LI()
        if a > M:
            continue
        jobsavailable[M-a].append(b)
    cur_jobs = []
    cnt = 0
    for i in range(1, M+1):
        # a <= iのなかで最大のbがあるものをとりだす
        jobs = jobsavailable[M-i]
        cur_jobs.extend(jobs)
        heapq._heapify_max(cur_jobs)
        if cur_jobs != []:
            j = heapq._heappop_max(cur_jobs)
            cnt += j
    print(cnt)


'''
def main():
    N, M = LI()
    ab = []
    for _ in range(N):
        a, b = LI()
        ab.append((a, b))
    ab = sorted(ab, key=lambda x: (x[1], x[0]))[::-1]
    i = 0
    cnt = 0
    print(ab)
    while 0 < M and i < N:
        day, money = ab[i]
        if day > M:
            pass
        else:
            cnt += money
            M -= 1
        i += 1
    print(cnt)
'''

main()