Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heapify, merge, nlargest
from operator import itemgetter

n, m = map(int, input().split())
aa = list(map(int, input().split()))
bcbc = [list(map(int, input().split())) for _ in range(m)]
heapify(aa)
bcbc.sort(key=itemgetter(1), reverse=True)

for b, c in bcbc :
    cc = [c]*min(b, n)
    dd = merge(aa, cc)
    dd = nlargest(n, dd)
    if aa == dd :
        break
    aa = dd
print(sum(dd))

Fixed submission:
from operator import itemgetter

n, m = map(int, input().split())
aa = list(map(int, input().split()))
bcbc = [list(map(int, input().split())) for _ in range(m)]
bcbc.extend([[1, a] for a in aa])
bcbc.sort(key=itemgetter(1), reverse=True)

count = 0
s = 0
for b, c in bcbc :
    if count + b >= n :
        b = n - count
    count += b
    s += b*c
    if count == n :
        break
print(s)

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heapify, heappushpop

n, m = map(int, input().split())
aa = list(map(int, input().split()))
bcbc = [list(map(int, input().split())) for _ in range(m)]
heapify(aa)
for b, c in bcbc :
    for _ in range(min(n, b)) :
        heappushpop(aa, c)
print(sum(aa))

Fixed submission:
from operator import itemgetter

n, m = map(int, input().split())
aa = list(map(int, input().split()))
bcbc = [list(map(int, input().split())) for _ in range(m)]
bcbc.extend([[1, a] for a in aa])
bcbc.sort(key=itemgetter(1), reverse=True)

count = 0
s = 0
for b, c in bcbc :
    if count + b >= n :
        b = n - count
    count += b
    s += b*c
    if count == n :
        break
print(s)

Buggy python submission (verdict: Time Limit Exceeded):
#D
n, m = list(map(int, input().split()))
alist = list(map(int, input().split()))
for i in range(m) :
    b, c = list(map(int, input().split()))
    alist += [c] * min(n, b)
alist.sort()
print(sum(alist[-n:]))

Fixed submission:
from operator import itemgetter

n, m = map(int, input().split())
aa = list(map(int, input().split()))
bcbc = [list(map(int, input().split())) for _ in range(m)]
bcbc.extend([[1, a] for a in aa])
bcbc.sort(key=itemgetter(1), reverse=True)

count = 0
s = 0
for b, c in bcbc :
    if count + b >= n :
        b = n - count
    count += b
    s += b*c
    if count == n :
        break
print(s)

Buggy python submission (verdict: Time Limit Exceeded):
#D
n, m = list(map(int, input().split()))
alist = list(map(int, input().split()))
for i in range(m) :
    b, c = list(map(int, input().split()))
    alist += [c] * b
    alist.sort()
    alist = alist[-n:]
print(sum(alist))

Fixed submission:
from operator import itemgetter

n, m = map(int, input().split())
aa = list(map(int, input().split()))
bcbc = [list(map(int, input().split())) for _ in range(m)]
bcbc.extend([[1, a] for a in aa])
bcbc.sort(key=itemgetter(1), reverse=True)

count = 0
s = 0
for b, c in bcbc :
    if count + b >= n :
        b = n - count
    count += b
    s += b*c
    if count == n :
        break
print(s)

Buggy python submission (verdict: Time Limit Exceeded):
#D
n, m = list(map(int, input().split()))
alist = list(map(int, input().split()))
for i in range(m) :
    b, c = list(map(int, input().split()))
    alist += [c] * b
alist.sort()
print(sum(alist[-n:]))

Fixed submission:
from operator import itemgetter

n, m = map(int, input().split())
aa = list(map(int, input().split()))
bcbc = [list(map(int, input().split())) for _ in range(m)]
bcbc.extend([[1, a] for a in aa])
bcbc.sort(key=itemgetter(1), reverse=True)

count = 0
s = 0
for b, c in bcbc :
    if count + b >= n :
        b = n - count
    count += b
    s += b*c
    if count == n :
        break
print(s)

Buggy python submission (verdict: Runtime Error):
#D
import sys
sys.setrecursionlimit(200000)

n, m = list(map(int, input().split()))
alist = list(map(int, input().split()))
alist.sort()

def inserter(key, list1) :
    if list1 == [] :
        return [key]
    elif list1[0][1] < key[1] :
        return [key] + list1
    else :
        return [list1[0]] + inserter(key, list1[1:])
def rewriter(list1, list2) :
    if list1 == [] :
        return []
    elif list2 == [] :
        return list1
    else :
        return [max(list1[0], list2[0])] + rewriter(list1[1:], list2[1:])
    
bclist = []
for i in range(m) :
    bc = list(map(int, input().split()))
    bclist = inserter(bc, bclist)
rwlist = []
for j in len(bclist) :
    if j < len(alist) :
        rwlist += [j[1]] * j[0]
    else :
        break
ans = rewriter(alist, rwlist)
print(sum(ans))

Fixed submission:
from operator import itemgetter

n, m = map(int, input().split())
aa = list(map(int, input().split()))
bcbc = [list(map(int, input().split())) for _ in range(m)]
bcbc.extend([[1, a] for a in aa])
bcbc.sort(key=itemgetter(1), reverse=True)

count = 0
s = 0
for b, c in bcbc :
    if count + b >= n :
        b = n - count
    count += b
    s += b*c
    if count == n :
        break
print(s)

Buggy python submission (verdict: Runtime Error):
#D
n, m = list(map(int, input().split()))
alist = list(map(int, input().split()))
alist.sort()
def inserter(key, list1) :
    if list1 == [] :
        return [key]
    elif list1[0][1] > key[1] :
        return [key] + list1
    else :
        return [list1[0]] + inserter(key, list1[1:])
def rewriter(list1, list2) :
    if list1 == [] :
        return []
    elif list2 == [] :
        return list1
    else :
        return [max(list1[0], list2[0])] + rewriter(list1[1:], list2[1:])
bclist = []
for i in range(m) :
    bc = list(map(int, input().split()))
    bclist = inserter(bc, bclist)
bclist.reverse()
rwlist = []
for j in bclist :
    rwlist += [j[1]] * j[0]
ans = rewriter(alist, rwlist)
print(sum(ans))

Fixed submission:
from operator import itemgetter

n, m = map(int, input().split())
aa = list(map(int, input().split()))
bcbc = [list(map(int, input().split())) for _ in range(m)]
bcbc.extend([[1, a] for a in aa])
bcbc.sort(key=itemgetter(1), reverse=True)

count = 0
s = 0
for b, c in bcbc :
    if count + b >= n :
        b = n - count
    count += b
    s += b*c
    if count == n :
        break
print(s)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = list(map(int, input().split()))
alist = list(map(int, input().split()))
for i in range(m) :
    b, c = list(map(int, input().split()))
    alist.sort()
    for j in range(b) :
        if alist[j] < c :
            alist[j] = c
        else :
            break
print(sum(alist))

Fixed submission:
from operator import itemgetter

n, m = map(int, input().split())
aa = list(map(int, input().split()))
bcbc = [list(map(int, input().split())) for _ in range(m)]
bcbc.extend([[1, a] for a in aa])
bcbc.sort(key=itemgetter(1), reverse=True)

count = 0
s = 0
for b, c in bcbc :
    if count + b >= n :
        b = n - count
    count += b
    s += b*c
    if count == n :
        break
print(s)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = list(map(int, input().split()))
alist = list(map(int, input().split()))
for i in range(m) :
    b, c = list(map(int, input().split()))
    alist.sort()
    for j in range(b) :
        if alist[j] < c :
            alist[j] = c
print(sum(alist))

Fixed submission:
from operator import itemgetter

n, m = map(int, input().split())
aa = list(map(int, input().split()))
bcbc = [list(map(int, input().split())) for _ in range(m)]
bcbc.extend([[1, a] for a in aa])
bcbc.sort(key=itemgetter(1), reverse=True)

count = 0
s = 0
for b, c in bcbc :
    if count + b >= n :
        b = n - count
    count += b
    s += b*c
    if count == n :
        break
print(s)