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

N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))
ans = sum(A)

for _ in range(M):
    b, c = map(int, input().split())
    if c <= A[0]:
        continue
    index = bisect.bisect_left(A, c)
    index = min(index, b)
    for i in range(index):
        ans += c - A[i]
        A[i] = c
    if index > 0:
        A = sorted(A)

print(ans)



Fixed submission:
N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))

bc = [list(map(int, input().split())) for _ in range(M)]

ans = sum(A)

bc = sorted(bc, key=lambda i: i[1], reverse=True)

i = 0
for b, c in bc:
    for _ in range(b):
        if A[i] > c:
            break
        ans += c - A[i]
        i += 1
        if i >= N:
            break
    else:
        continue
    break

print(ans)

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

N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))
ans = sum(A)
for _ in range(M):
    b, c = map(int, input().split())
    index = bisect.bisect_left(A, c)
    index = min(index, b)
    for i in range(index):
        ans += c - A[i]
        A[i] = c
    if index > 0:
        A.sort()

print(ans)



Fixed submission:
N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))

bc = [list(map(int, input().split())) for _ in range(M)]

ans = sum(A)

bc = sorted(bc, key=lambda i: i[1], reverse=True)

i = 0
for b, c in bc:
    for _ in range(b):
        if A[i] > c:
            break
        ans += c - A[i]
        i += 1
        if i >= N:
            break
    else:
        continue
    break

print(ans)

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

N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))
ans = sum(A)
for _ in range(M):
    b, c = map(int, input().split())
    index = bisect.bisect_left(A, c)
    index = min(index, b)
    for i in range(index):
        ans += c - A[i]
        A[i] = c
    if index > 0:
        A = sorted(A)

print(ans)



Fixed submission:
N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))

bc = [list(map(int, input().split())) for _ in range(M)]

ans = sum(A)

bc = sorted(bc, key=lambda i: i[1], reverse=True)

i = 0
for b, c in bc:
    for _ in range(b):
        if A[i] > c:
            break
        ans += c - A[i]
        i += 1
        if i >= N:
            break
    else:
        continue
    break

print(ans)

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

N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))

for _ in range(M):
    b, c = map(int, input().split())
    index = bisect.bisect_left(A, c)
    index = min(index, b)
    for i in range(index):
        A[i] = c
    if index > 0:
        A = sorted(A)

print(sum(A))



Fixed submission:
N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))

bc = [list(map(int, input().split())) for _ in range(M)]

ans = sum(A)

bc = sorted(bc, key=lambda i: i[1], reverse=True)

i = 0
for b, c in bc:
    for _ in range(b):
        if A[i] > c:
            break
        ans += c - A[i]
        i += 1
        if i >= N:
            break
    else:
        continue
    break

print(ans)

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

N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))

for _ in range(M):
    b, c = map(int, input().split())
    index = bisect.bisect_left(A, c)
    index = min(index, b)
    for i in range(index):
        A[i] = c
    if index > 0:
        A = sorted(A)

print(sum(A))



Fixed submission:
N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))

bc = [list(map(int, input().split())) for _ in range(M)]

ans = sum(A)

bc = sorted(bc, key=lambda i: i[1], reverse=True)

i = 0
for b, c in bc:
    for _ in range(b):
        if A[i] > c:
            break
        ans += c - A[i]
        i += 1
        if i >= N:
            break
    else:
        continue
    break

print(ans)