Problem p02948 (python)

Fixed submission:
import sys

icase=0
if icase==0:
    n,m=map(int,input().split())
    ab=[[] for i in range(m)]
    for i in range(n):
        ai,bi=map(int,input().split())
        if m-ai>=0:
            ab[m-ai].append(-bi)
elif icase==1:
    n,m=3,4
    ab=[[-3, -1], [], [-2], []]    
elif icase==2:
    n,m=5,3
    ab=[[], [-1, -3], [-2, -3, -4]]
elif icase==3:
    n,m=1,1
    ab=[[]]

from heapq import heappop,heappush

if len(ab)==0:
    print(0)
    sys.exit()

n=len(ab)
h=[]
asum=0
for i in range(m-1,-1,-1):
    if len(ab[i])>0:
        for abi in ab[i]:
            heappush(h,abi)
#    print(i,h,asum)
    if len(h)>0:
        asum+=heappop(h)

print(-asum)

Buggy submission (verdict: Runtime Error):
import sys

icase=0
if icase==0:
    n,m=map(int,input().split())
#    ab=[[0]*2 for i in range(n)]
    ab=[]
    for i in range(n):
        ai,bi=map(int,input().split())
        if m-ai>=0:
            ab.append([m-ai,-bi])
#        ab[i][0]=m-ai
#        ab[i][1]=-bi
elif icase==1:
    n,m=3,4
    ab=[[2, -2], [0, -1], [0, -3]]    
elif icase==2:
    n,m=5,3
    ab=[[1, -3], [1, -1], [2, -4], [2, -3], [2, -2]]
elif icase==3:
    n,m=1,1
    ab=[]

from heapq import heappop,heappush
#heappush(H,l) 
#print(heappop(H)) #2

#ab.sort(reverse=True)
if len(ab)==0:
    print(0)
    sys.exit()
ab.sort()

m=len(ab)
a0=ab[0][0]
h=[]
asum=0
idx=0
for i in range(m):
    while ab[idx][0]<m-i:
        heappush(h,ab[idx][1])
#        print("i:",i,"idx:",idx,h)
        idx+=1
        if idx>=n:
            asum+=heappop(h)
#            print("1:",idx,asum,h)
            break
    if idx>=n:
        asum+=heappop(h)
#        print("2:",idx,asum,h)
        break

if idx>=n:
    asum+=heappop(h)
#    print("3:",idx,asum,h)

print(-asum)

Buggy submission (verdict: Runtime Error):
import sys

icase=0
if icase==0:
    n,m=map(int,input().split())
#    ab=[[0]*2 for i in range(n)]
    ab=[]
    for i in range(n):
        ai,bi=map(int,input().split())
        if m-ai>=0:
            ab.append([m-ai,-bi])
#        ab[i][0]=m-ai
#        ab[i][1]=-bi
elif icase==1:
    n,m=3,4
    ab=[[2, -2], [0, -1], [0, -3]]    
elif icase==2:
    n,m=5,3
    ab=[[1, -3], [1, -1], [2, -4], [2, -3], [2, -2]]
elif icase==3:
    n,m=1,1
    ab=[]

from heapq import heappop,heappush
#heappush(H,l) 
#print(heappop(H)) #2

#ab.sort(reverse=True)
if len(ab)==0:
    print(0)
    sys.exit()
ab.sort()

a0=ab[0][0]
h=[]
asum=0
idx=0
for i in range(m):
    while ab[idx][0]<m-i:
        heappush(h,ab[idx][1])
#        print("i:",i,"idx:",idx,h)
        idx+=1
        if idx>=n:
            asum+=heappop(h)
#            print("1:",idx,asum,h)
            break
    if idx>=n:
        asum+=heappop(h)
#        print("2:",idx,asum,h)
        break

if idx>=n:
    asum+=heappop(h)
#    print("3:",idx,asum,h)

print(-asum)

Buggy submission (verdict: Runtime Error):
icase=0
if icase==0:
    n,m=map(int,input().split())
    ab=[[0]*2 for i in range(n)]
    for i in range(n):
        ai,bi=map(int,input().split())
        ab[i][0]=m-ai
        ab[i][1]=-bi
elif icase==1:
    n,m=3,4
    ab=[[2, -2], [0, -1], [0, -3]]    
elif icase==2:
    n,m=5,3
    ab=[[1, -3], [1, -1], [2, -4], [2, -3], [2, -2]]

from heapq import heappop,heappush
#heappush(H,l) 
#print(heappop(H)) #2

#ab.sort(reverse=True)
ab.sort()
a0=ab[0][0]
h=[]
asum=0
idx=0
for i in range(m):
    while ab[idx][0]<m-i:
        heappush(h,ab[idx][1])
#        print("i:",i,"idx:",idx,h)
        idx+=1
        if idx>=n:
            asum+=heappop(h)
#            print("1:",idx,asum,h)
            break
    if idx>=n:
        asum+=heappop(h)
#        print("2:",idx,asum,h)
        break

if idx>=n:
    asum+=heappop(h)
#    print("3:",idx,asum,h)

print(-asum)