Problem p03038 (python)

Fixed submission:
import sys
input = sys.stdin.readline
#n = int(input())
#l = list(map(int, input().split()))
'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''

n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
bc=[list(map(int,input().split())) for _ in range(m)]
bc.sort(key=lambda x: x[1],reverse=True)
#print(bc)
cnt=0
for i in range(n):
    if bc[cnt][0]>0:
        if bc[cnt][1]>a[i]:
            a[i]=bc[cnt][1]
        bc[cnt][0]-=1
    else:
        cnt+=1
        if cnt==len(bc):
            break
        if bc[cnt][1]>a[i]:
            a[i]=bc[cnt][1]
        bc[cnt][0]-=1
    #print(cnt,bc,a)
print(sum(a))

Buggy submission (verdict: Runtime Error):
def main():
    import sys
    input = sys.stdin.readline
    #n = int(input())
    #l = list(map(int, input().split()))
    '''
    A=[]
    B=[]
    for i in range():
        a, b = map(int, input().split())
        A.append(a)
        B.append(b)'''

    #import bisect
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    a.sort()
    #print(a)

    bc=[]
    for i in range(m):
        B = list(map(int, input().split()))
        bc.append(B)
    bc.sort(key=lambda x: x[1])
    #print(bc)

    clis=[]
    for i in range(m)[::-1]:
        clis+=[bc[i][1] for i in range(bc[i][0])]
    #print(clis)
    ans=0
    le=len(clis)
    for i in range(n):
        #print(clis[i],a[i])
        if i<le and clis[i]>a[i]:
            ans+=clis[i]
        else:
            ans+=a[i]
        #print(ans)
    print(ans)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
def main():
    import sys
    input = sys.stdin.readline
    #n = int(input())
    #l = list(map(int, input().split()))
    '''
    A=[]
    B=[]
    for i in range():
        a, b = map(int, input().split())
        A.append(a)
        B.append(b)'''

    #import bisect
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    a.sort()
    #print(a)

    bc=[]
    for i in range(m):
        B = list(map(int, input().split()))
        bc.append(B)
    bc.sort(key=lambda x: x[1])
    #print(bc)

    clis=[]
    for i in range(m)[::-1]:
        clis+=[bc[i][1]] for i in range(bc[i][0])
    #print(clis)
    ans=0
    le=len(clis)
    for i in range(n):
        #print(clis[i],a[i])
        if i<le and clis[i]>a[i]:
            ans+=clis[i]
        else:
            ans+=a[i]
        #print(ans)
    print(ans)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
def main():
    import sys
    input = sys.stdin.readline
    #n = int(input())
    #l = list(map(int, input().split()))
    '''
    A=[]
    B=[]
    for i in range():
        a, b = map(int, input().split())
        A.append(a)
        B.append(b)'''

    #import bisect
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    a.sort()
    #print(a)

    bc=[]
    for i in range(m):
        B = list(map(int, input().split()))
        bc.append(B)
    bc.sort(key=lambda x: x[1])
    #print(bc)

    clis=[]
    for i in range(m)[::-1]:
        clis+=[bc[i][1]] for i in range(bc[i][0])
    #print(clis)
    ans=0
    le=len(clis)
    for i in range(n):
        #print(clis[i],a[i])
        if i<le and clis[i]>a[i]:
            ans+=clis[i]
        else:
            ans+=a[i]
        #print(ans)
    print(ans)

if __name__ == "__main__":
    main()