Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
a = [(1, int(i) for i in input().split())]
for _ in range(M):
    b, c = map(int, input().split())
    a.append((b, c))
a = sorted(a, key=lambda x: x[1])
ans = 0
t = 0
while t < N:
    b, c = a.pop()
    p = min(b, N-t)
    ans += p * c
    t += p
print(ans)


Fixed submission:
n, m = map(int, input().split())
a = [(1, int(i)) for i in input().split()]
for _ in range(m):
    b, c = map(int, input().split())
    a.append((b, c))
a = sorted(a, key=lambda x: x[1])
ans = 0
t = 0
while t < n:
    b, c = a.pop()
    p = min(b, n-t)
    ans += p * c
    t += p
print(ans)

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

N, M = map(int, input().split())
*a, = map(int, input().split())
BC = []
for _ in range(M):
    b, c = map(int, input().split())
    BC.append((b, c))
a.sort()
BC = sorted(BC, key=lambda x: x[1], reverse=True)
ans = 0
for b, c in BC:
    idx = bisect.bisect_right(a, c)
    if b <= idx:
        ans += b * c
        a = a[b:]
    else:
        ans += idx * c
        a = a[idx:]
ans += sum(a)
print(ans)


Fixed submission:
n, m = map(int, input().split())
a = [(1, int(i)) for i in input().split()]
for _ in range(m):
    b, c = map(int, input().split())
    a.append((b, c))
a = sorted(a, key=lambda x: x[1])
ans = 0
t = 0
while t < n:
    b, c = a.pop()
    p = min(b, n-t)
    ans += p * c
    t += p
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
*a, = map(int, input().split())
BC = []
for _ in range(M):
    b, c = map(int, input().split())
    BC.append((b, c))
a.sort()
BC = sorted(BC, key=lambda x: x[1], reverse=True)
ans = 0
for b, c in BC:
    for _ in range(b):
        if a[0] < c:
            ans += c
            a = a[1:]
        else:
            break
ans += sum(a)
print(ans)


Fixed submission:
n, m = map(int, input().split())
a = [(1, int(i)) for i in input().split()]
for _ in range(m):
    b, c = map(int, input().split())
    a.append((b, c))
a = sorted(a, key=lambda x: x[1])
ans = 0
t = 0
while t < n:
    b, c = a.pop()
    p = min(b, n-t)
    ans += p * c
    t += p
print(ans)

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

n, m = map(int, input().split())
*a, = map(int, input().split())
a.sort()
BC = []
for _ in range(m):
    b, c = map(int, input().split())
    BC.append((b, c))
BC = sorted(BC, key=lambda x: x[1], reverse=True)
ans = 0
num = 0
for b, c in BC:
    idx = bisect(a[num:], c)
    p = min(idx, b)
    num += p
    ans += c * p
print(sum(a[num:])+ans)


Fixed submission:
n, m = map(int, input().split())
a = [(1, int(i)) for i in input().split()]
for _ in range(m):
    b, c = map(int, input().split())
    a.append((b, c))
a = sorted(a, key=lambda x: x[1])
ans = 0
t = 0
while t < n:
    b, c = a.pop()
    p = min(b, n-t)
    ans += p * c
    t += p
print(ans)

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

n, m = map(int, input().split())
*a, = map(int, input().split())
a.sort()
BC = []
num = 0
for _ in range(m):
    b, c = map(int, input().split())
    BC.append((b, c))
BC = sorted(BC, key=lambda x: x[1], reverse=True)
ans = 0
for b, c in BC:
    idx = bisect(a, c)
    p = min(idx, b)
    a = a[p:]
    ans += c * p
print(sum(a)+ans)


Fixed submission:
n, m = map(int, input().split())
a = [(1, int(i)) for i in input().split()]
for _ in range(m):
    b, c = map(int, input().split())
    a.append((b, c))
a = sorted(a, key=lambda x: x[1])
ans = 0
t = 0
while t < n:
    b, c = a.pop()
    p = min(b, n-t)
    ans += p * c
    t += p
print(ans)

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

n, m = map(int, input().split())
*a, = map(int, input().split())
a.sort()
BC = []
num = 0
for _ in range(m):
    b, c = map(int, input().split())
    BC.append((b, c))
BC = sorted(BC, key=lambda x: x[1], reverse=True)
for b, c in BC:
    idx = bisect(a, c)
    p = min(idx, b)
    for i in range(p):
        a.pop(0)
        a.append(c)
print(sum(a[num:]))


Fixed submission:
n, m = map(int, input().split())
a = [(1, int(i)) for i in input().split()]
for _ in range(m):
    b, c = map(int, input().split())
    a.append((b, c))
a = sorted(a, key=lambda x: x[1])
ans = 0
t = 0
while t < n:
    b, c = a.pop()
    p = min(b, n-t)
    ans += p * c
    t += p
print(ans)