Problem p03038 (python)

Fixed submission:
from heapq import heappush,heappop

def main():
    inp1=list(map(lambda x:int(x),input().split()))
    cards=list(map(lambda x:int(x),input().split()))
    cards_heap=[]
    for i in cards:
        heappush(cards_heap,i)

    N=inp1[0]
    M=inp1[1]
    B=[]
    C=[]
    for i in range(M):
        changes=list(map(lambda x:int(x),input().split()))
        B.append(changes[0])
        C.append(changes[1])

    cb=zip(C,B)
    cb=sorted(cb,reverse=True)


    for C,B in cb:
        for _ in range(B):
            a=heappop(cards_heap)
            if C>a:
                heappush(cards_heap,C)
            else:
                heappush(cards_heap,a)
                break

    print(sum(cards_heap))

if __name__=="__main__":
    main()

Buggy submission (verdict: Runtime Error):
def binary_search(cards,n,number):
    low=0
    high=len(cards)-1

    while high-low>1:
        mid=(low+high)//2
        base=cards[mid]
        if base==number:
            for i in range(n):
                cards.insert(mid,number)
            return cards,mid
        if base<number:
            low=mid
        else:
            high=mid

    if cards[high]==cards[low]:
        if number>cards[high]:
            for i in range(n):
                cards.insert(high+1, number)
        else:
            for i in range(n):
                cards.insert(low, number)
    else:
        for i in range(n):
            cards.insert(high,number)
    return cards,mid

#1 3 5 7 9 11 <=8
#1 3 [5] 7 9 11 <= 8
#1 3 (5) 7 9 (11) <=8
#1 3 5 [7] 9 11 <= 8
#1 3 5 (7) 9 (11) <= 8
#1 3 5 7 [9] 11 <= 8
#1 3 5 (7) (9) 11
#1 3 5 [7] 9 11 <=8
#1 3 5 (7)

def main():
    inp1=list(map(lambda x:int(x),input().split()))
    cards=list(map(lambda x:int(x),input().split()))
    N=inp1[0]
    M=inp1[1]
    B=[]
    C=[]
    for i in range(M):
        changes=list(map(lambda x:int(x),input().split()))
        B.append(changes[0])
        C.append(changes[1])

    cb=zip(C,B)
    cb=sorted(cb)
    C,B=zip(*cb)

    cards.sort()

    for i in range(M):
        cards,pointer=binary_search(cards,B[i],C[i])
        if len(cards)-pointer>N:
            break
    cards=cards[len(cards)-N:len(cards)]

    print(sum(cards))

if __name__=="__main__":
    main()

Buggy submission (verdict: Runtime Error):
def binary_search(cards,n,number):
    low=0
    high=len(cards)-1

    while high-low>1:
        mid=(low+high)//2
        base=cards[mid]
        if base==number:
            for i in range(n):
                cards.insert(mid,number)
            return cards,mid
        if base<number:
            low=mid
        else:
            high=mid

    if cards[high]==cards[low]:
        if number>cards[high]:
            for i in range(n):
                cards.insert(high+1, number)
        else:
            for i in range(n):
                cards.insert(low, number)
    else:
        for i in range(n):
            cards.insert(high,number)
    return cards,mid

#1 3 5 7 9 11 <=8
#1 3 [5] 7 9 11 <= 8
#1 3 (5) 7 9 (11) <=8
#1 3 5 [7] 9 11 <= 8
#1 3 5 (7) 9 (11) <= 8
#1 3 5 7 [9] 11 <= 8
#1 3 5 (7) (9) 11
#1 3 5 [7] 9 11 <=8
#1 3 5 (7)

def main():
    inp1=list(map(lambda x:int(x),input().split()))
    cards=list(map(lambda x:int(x),input().split()))
    N=inp1[0]
    M=inp1[1]
    B=[]
    C=[]
    for i in range(M):
        changes=list(map(lambda x:int(x),input().split()))
        B.append(changes[0])
        C.append(changes[1])

    cb=zip(C,B)
    cb.sort()
    C,B=zip(*cb)

    cards.sort()

    for i in range(M):
        cards,pointer=binary_search(cards,B[i],C[i])
        if len(cards)-pointer>N:
            break
    cards=cards[len(cards)-N:len(cards)]

    print(sum(cards))

if __name__=="__main__":
    main()