Buggy python submission (verdict: Time Limit Exceeded):
import sys

input = sys.stdin.readline
N, M = map(int, input().split())
A = list(map(int, input().split()))
for i in range(M):
    B,C=map(int,input().split(' '))
    A += [C]*B
A = sorted(A,reverse=True)
print(sum(A[:N]))

Fixed submission:
import heapq as hq

def get_num():
    return list(map(int,input().split(' ')))

def main():
    n,m = get_num()
    card = get_num()
    card_sum=sum(card)
    hq.heapify(card)
    d_set=[]
    d=[]
    for i in range(m):
        b,c=get_num()
        hq.heappush(d_set,(-c,b))
    for i in range(m):
        if len(d) ==n:
            break
        cnt = hq.heappop(d_set)
        for i in range(cnt[1]):
            d.append(-cnt[0])
            if len(d)==n:
                break
    for i in range(len(d)):
        c_min=hq.heappop(card)
        d_max=d[i]
        if c_min>=d_max:
            break
        else:
            card_sum += (d_max-c_min)
    print(card_sum)
main()

Buggy python submission (verdict: Runtime Error):
import heapq as hq

def get_num():
    return list(map(int,input().split(' ')))

def main():
    n,m = get_num()
    card = get_num()
    card_sum=sum(card)
    hq.heapify(card)
    d_set=[]
    d=[]
    for i in range(m):
        b,c=get_num()
        hq.heappush(d_set,(-c,b))
    for i in range(m):
        if len(d) ==n:
            break
        cnt = hq.heappop(d_set)
        for i in range(cnt[1]):
            d.append(-cnt[0])
            if len(d)==n:
                break
    for i in range(n):
        c_min=hq.heappop(card)
        d_max=d[i]
        if c_min>=d_max:
            break
        else:
            card_sum += (d_max-c_min)
    print(card_sum)
main()

Fixed submission:
import heapq as hq

def get_num():
    return list(map(int,input().split(' ')))

def main():
    n,m = get_num()
    card = get_num()
    card_sum=sum(card)
    hq.heapify(card)
    d_set=[]
    d=[]
    for i in range(m):
        b,c=get_num()
        hq.heappush(d_set,(-c,b))
    for i in range(m):
        if len(d) ==n:
            break
        cnt = hq.heappop(d_set)
        for i in range(cnt[1]):
            d.append(-cnt[0])
            if len(d)==n:
                break
    for i in range(len(d)):
        c_min=hq.heappop(card)
        d_max=d[i]
        if c_min>=d_max:
            break
        else:
            card_sum += (d_max-c_min)
    print(card_sum)
main()

Buggy python submission (verdict: Runtime Error):
import heapq as hq

def get_num():
    return list(map(int,input().split(' ')))

def main():
    n,m = get_num()
    card = get_num()
    card_sum=sum(card)
    hq.heapify(card)
    d_set=[]
    d=[]
    for i in range(m):
        b,c=get_num()
        hq.heappush(d_set,(-c,b))
    while len(d)<n:
        cnt = hq.heappop(d_set)
        for i in range(cnt[1]):
            d.append(-cnt[0])
            if len(d)==n:
                break
    for i in range(n):
        c_min=hq.heappop(card)
        d_max=d[i]
        if c_min>=d_max:
            break
        else:
            card_sum += (d_max-c_min)
    print(card_sum)
main()

Fixed submission:
import heapq as hq

def get_num():
    return list(map(int,input().split(' ')))

def main():
    n,m = get_num()
    card = get_num()
    card_sum=sum(card)
    hq.heapify(card)
    d_set=[]
    d=[]
    for i in range(m):
        b,c=get_num()
        hq.heappush(d_set,(-c,b))
    for i in range(m):
        if len(d) ==n:
            break
        cnt = hq.heappop(d_set)
        for i in range(cnt[1]):
            d.append(-cnt[0])
            if len(d)==n:
                break
    for i in range(len(d)):
        c_min=hq.heappop(card)
        d_max=d[i]
        if c_min>=d_max:
            break
        else:
            card_sum += (d_max-c_min)
    print(card_sum)
main()

Buggy python submission (verdict: Time Limit Exceeded):
def qsort(a):
    if len(a) in (0,1):
        return a
    else:
        p = a[-1]
        l=[x for x in a[:-1] if x >=p]
        r=[x for x in a[:-1] if x <p]
        return qsort(l) + [p] + qsort(r)

def get_num():
    return list(map(int,input().split(' ')))

def main():
    n,m=get_num()
    card = get_num()
    for i in range(m):
        b,c=get_num()
        card+=[c]*b
    card=qsort(card)
    print(sum(card[:n]))

if __name__=='__main__':
    main()

Fixed submission:
import heapq as hq

def get_num():
    return list(map(int,input().split(' ')))

def main():
    n,m = get_num()
    card = get_num()
    card_sum=sum(card)
    hq.heapify(card)
    d_set=[]
    d=[]
    for i in range(m):
        b,c=get_num()
        hq.heappush(d_set,(-c,b))
    for i in range(m):
        if len(d) ==n:
            break
        cnt = hq.heappop(d_set)
        for i in range(cnt[1]):
            d.append(-cnt[0])
            if len(d)==n:
                break
    for i in range(len(d)):
        c_min=hq.heappop(card)
        d_max=d[i]
        if c_min>=d_max:
            break
        else:
            card_sum += (d_max-c_min)
    print(card_sum)
main()

Buggy python submission (verdict: Time Limit Exceeded):
def qsort(a):
    if len(a) in (0,1):
        return a
    else:
        p = a[-1]
        l=[x for x in a[:-1] if x >=p]
        r=[x for x in a[:-1] if x <p]
        return qsort(l) + [p] + qsort(r)

def get_num():
    return list(map(int,input().split(' ')))

def main():
    n,m=get_num()
    card = get_num()
    for i in range(m):
        b,c=get_num()
        for i in range(b):
            card.append(c)
    card=qsort(card)
    print(sum(card[:n]))

if __name__=='__main__':
    main()

Fixed submission:
import heapq as hq

def get_num():
    return list(map(int,input().split(' ')))

def main():
    n,m = get_num()
    card = get_num()
    card_sum=sum(card)
    hq.heapify(card)
    d_set=[]
    d=[]
    for i in range(m):
        b,c=get_num()
        hq.heappush(d_set,(-c,b))
    for i in range(m):
        if len(d) ==n:
            break
        cnt = hq.heappop(d_set)
        for i in range(cnt[1]):
            d.append(-cnt[0])
            if len(d)==n:
                break
    for i in range(len(d)):
        c_min=hq.heappop(card)
        d_max=d[i]
        if c_min>=d_max:
            break
        else:
            card_sum += (d_max-c_min)
    print(card_sum)
main()

Buggy python submission (verdict: Time Limit Exceeded):
import heapq as hq
def main():
    n,m=list(map(int,input().split(' ')))
    card = list(map(int,input().split(' ')))
    hq.heapify(card)
    for i in range(m):
        b,c=list(map(int,input().split(' ')))
        for i in range(b):
            hq.heappushpop(card,c)
    print(sum(card))
if __name__ =='__main__':
    main()

Fixed submission:
import heapq as hq

def get_num():
    return list(map(int,input().split(' ')))

def main():
    n,m = get_num()
    card = get_num()
    card_sum=sum(card)
    hq.heapify(card)
    d_set=[]
    d=[]
    for i in range(m):
        b,c=get_num()
        hq.heappush(d_set,(-c,b))
    for i in range(m):
        if len(d) ==n:
            break
        cnt = hq.heappop(d_set)
        for i in range(cnt[1]):
            d.append(-cnt[0])
            if len(d)==n:
                break
    for i in range(len(d)):
        c_min=hq.heappop(card)
        d_max=d[i]
        if c_min>=d_max:
            break
        else:
            card_sum += (d_max-c_min)
    print(card_sum)
main()