Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
from itertools import chain
 
n, m = map(int, input().split())
A = list(map(int, input().split()))
bc = [list(map(int, input().split())) for _ in range(m)]
 
A.sort()
bc.sort(reverse=True, key=lambda x: x[1])
C = list(chain(*[[c]*b for b, c in bc]))
threshold = np.count_nonzero((np.array(C[:len(A)]) - np.array(A[:len(C)])) > 0)
 
print(sum(C[:threshold]) + sum(A[threshold:]))

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [list(map(int, input().split())) for _ in range(m)]

a.sort()
bc.sort(reverse=True, key=lambda x: x[1])
j = 0
for i in range(m):
    while j < len(a) and bc[i][0] > 0:
        if a[j] < bc[i][1]:
            a[j] = bc[i][1]
            bc[i][0] -= 1
        j += 1
print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
A = list(map(int, input().split()))
for _ in range(m):
    b, c = map(int, input().split())
    A += [c]*b

A.sort(reverse=True)

print(sum(A[:n]))

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [list(map(int, input().split())) for _ in range(m)]

a.sort()
bc.sort(reverse=True, key=lambda x: x[1])
j = 0
for i in range(m):
    while j < len(a) and bc[i][0] > 0:
        if a[j] < bc[i][1]:
            a[j] = bc[i][1]
            bc[i][0] -= 1
        j += 1
print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

n, m = map(int, input().split())
A = list(map(int, input().split()))
C = []
for _ in range(m):
    b, c = map(int, input().split())
    C += [c]*b

A.sort()
C.sort(reverse=True)
threshold = np.count_nonzero((np.array(C[:len(A)]) - np.array(A[:len(C)])) > 0)

print(sum(C[:threshold]) + sum(A[threshold:]))

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [list(map(int, input().split())) for _ in range(m)]

a.sort()
bc.sort(reverse=True, key=lambda x: x[1])
j = 0
for i in range(m):
    while j < len(a) and bc[i][0] > 0:
        if a[j] < bc[i][1]:
            a[j] = bc[i][1]
            bc[i][0] -= 1
        j += 1
print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = []
for _ in range(m):
    b, c = map(int, input().split())
    bc.append([b, c])
    
a = sorted(a)
bc = sorted(bc, key=lambda x: x[1])

que = []
for b, c in bc:
    que += [c]*b


c = que.pop()
ans = 0
i = 0
while True:
    if a[i] < c:
        ans += c
        if que:
            c = que.pop()
            i += 1
        else:
            i += 1
            break
    else:
        break
print(ans + sum(a[i:]))

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [list(map(int, input().split())) for _ in range(m)]

a.sort()
bc.sort(reverse=True, key=lambda x: x[1])
j = 0
for i in range(m):
    while j < len(a) and bc[i][0] > 0:
        if a[j] < bc[i][1]:
            a[j] = bc[i][1]
            bc[i][0] -= 1
        j += 1
print(sum(a))

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = []
for _ in range(m):
    b, c = map(int, input().split())
    bc.append([b, c])
    
a = sorted(a)
bc = sorted(bc, key=lambda x: x[1])

que = []
for b, c in bc:
    que += [c]*b


c = que.pop()
ans = 0
i = 0
while True:
    if a[i] < c:
        ans += c
        if que:
            c = que.pop()
            i += 1
        else:
            i += 1
            break
    else:
        break
print(ans + sum(a[i:]))

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [list(map(int, input().split())) for _ in range(m)]

a.sort()
bc.sort(reverse=True, key=lambda x: x[1])
j = 0
for i in range(m):
    while j < len(a) and bc[i][0] > 0:
        if a[j] < bc[i][1]:
            a[j] = bc[i][1]
            bc[i][0] -= 1
        j += 1
print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = []
for _ in range(m):
    b, c = map(int, input().split())
    bc.append([b, c])
    
sorted_a = sorted(a)
sorted_bc = sorted(bc, key=lambda x: x[1])

que = []
for b, c in sorted_bc:
    que += [c]*b
    
c = que.pop()    
ans = 0
for x in sorted_a:
    if x < c:
        ans += c
        if que:
            c = que.pop()
        else:
            c = 0
    else:
        ans += x
print(ans)

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [list(map(int, input().split())) for _ in range(m)]

a.sort()
bc.sort(reverse=True, key=lambda x: x[1])
j = 0
for i in range(m):
    while j < len(a) and bc[i][0] > 0:
        if a[j] < bc[i][1]:
            a[j] = bc[i][1]
            bc[i][0] -= 1
        j += 1
print(sum(a))