Buggy python submission (verdict: Time Limit Exceeded):
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
bc = [[0] * 2] * m

for i in range(m):
    bc[i] = list(map(int, input().split()))

a.sort()
bc = sorted(bc, key=lambda x: -x[1])
new = []

for e in bc:
    new.extend([e[1]] * e[0])

for i in range(min(n, len(new))):
    a[i] = max(a[i], new[i])

print(sum(a))


Fixed submission:
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
bc = [[0] * 2] * m

for i in range(m):
    bc[i] = list(map(int, input().split()))

a.sort()
bc = sorted(bc, key=lambda x: -x[1])
new = []

cnt = 0

for e in bc:
    new += [e[1]] * e[0]
    cnt += e[0]

    if (n <= cnt):
        break

for i in range(min(n, cnt)):
    a[i] = max(a[i], new[i])

print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
bc = [[0] * 2] * m

for i in range(m):
    bc[i] = list(map(int, input().split()))

a.sort()
bc = sorted(bc, key=lambda x: -x[1])
new = []

for e in bc:
    new.extend([e[1]] * e[0])

for i in range(min(n, len(new))):
    a[i] = max(a[i], new[i])

print(sum(a))


Fixed submission:
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
bc = [[0] * 2] * m

for i in range(m):
    bc[i] = list(map(int, input().split()))

a.sort()
bc = sorted(bc, key=lambda x: -x[1])
new = []

cnt = 0

for e in bc:
    new += [e[1]] * e[0]
    cnt += e[0]

    if (n <= cnt):
        break

for i in range(min(n, cnt)):
    a[i] = max(a[i], new[i])

print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
bc = [[0] * 2] * m

for i in range(m):
    bc[i] = list(map(int, input().split()))

new = a

for i in range(m):
    new.extend([bc[i][1]] * bc[i][0])

new.sort()
ans = 0
n_len = len(new)

for i in range(n_len - 1, n_len - n - 1, -1):
    ans += new[i]

print(ans)


Fixed submission:
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
bc = [[0] * 2] * m

for i in range(m):
    bc[i] = list(map(int, input().split()))

a.sort()
bc = sorted(bc, key=lambda x: -x[1])
new = []

cnt = 0

for e in bc:
    new += [e[1]] * e[0]
    cnt += e[0]

    if (n <= cnt):
        break

for i in range(min(n, cnt)):
    a[i] = max(a[i], new[i])

print(sum(a))

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

n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
bc = [[0] * 2] * m

for i in range(m):
    bc[i] = list(map(int, input().split()))

bc.sort(key=lambda x: x[0])
idx = bisect.bisect_left(a, bc[m - 1][0])

new = a

for i in range(m):
    new.extend([bc[i][1]] * bc[i][0])

new.sort()
ans = 0
n_len = len(new)

for i in range(n_len - 1, n_len - n - 1, -1):
    ans += new[i]

print(ans)


Fixed submission:
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
bc = [[0] * 2] * m

for i in range(m):
    bc[i] = list(map(int, input().split()))

a.sort()
bc = sorted(bc, key=lambda x: -x[1])
new = []

cnt = 0

for e in bc:
    new += [e[1]] * e[0]
    cnt += e[0]

    if (n <= cnt):
        break

for i in range(min(n, cnt)):
    a[i] = max(a[i], new[i])

print(sum(a))

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

n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
b = [0] * m
c = [0] * m

for i in range(m):
    b[i], c[i] = list(map(int, input().split()))

max_value = max(b)
heapq.heapify(a)

for i in range(m):
    for j in range(b[i] - 1, -1, -1):
        heapq.heappush(a, max(heapq.heappop(a), c[i]))

print(sum(a))


Fixed submission:
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
bc = [[0] * 2] * m

for i in range(m):
    bc[i] = list(map(int, input().split()))

a.sort()
bc = sorted(bc, key=lambda x: -x[1])
new = []

cnt = 0

for e in bc:
    new += [e[1]] * e[0]
    cnt += e[0]

    if (n <= cnt):
        break

for i in range(min(n, cnt)):
    a[i] = max(a[i], new[i])

print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
bc = [[0] * 2] * m

for i in range(m):
    bc[i] = list(map(int, input().split()))

a.sort()

for i in range(m):
    for j in range(bc[i][0], -1, -1):
        m = min(a)

        if (j == 0 or bc[i][1] <= m):
            break
        else:
            a[a.index(m)] = bc[i][1]
            bc[i][0] -= 1

print(sum(a))


Fixed submission:
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
bc = [[0] * 2] * m

for i in range(m):
    bc[i] = list(map(int, input().split()))

a.sort()
bc = sorted(bc, key=lambda x: -x[1])
new = []

cnt = 0

for e in bc:
    new += [e[1]] * e[0]
    cnt += e[0]

    if (n <= cnt):
        break

for i in range(min(n, cnt)):
    a[i] = max(a[i], new[i])

print(sum(a))