Buggy python submission (verdict: Runtime Error):
from sys import stdin, exit

N, M = map(int, stdin.readline().split())
nums = [0]*100000
A = list(map(int, stdin.readline().split()))
for a in A:
    nums[a] += 1
for _ in range(M):
    B, C = map(int, stdin.readline().split())
    for _ in range(B):
        nums[C] += 1

ans = 0
count = 0
for i in range(99999, -1, -1):
    if nums[i] != 0:
        if count >= N:
            print(ans)
            exit()
        if nums[i] + count >= N:
            ans += (N-count)*i
            print(ans)
            exit()
        else:
            ans += (nums[i] * i)
            count += nums[i]

print(ans)

Fixed submission:
from sys import stdin, exit

N, M = map(int, stdin.readline().split())
A = list(map(int, stdin.readline().split()))
p = [(A[i], 1) for i in range(N)]
for _ in range(M):
    B, C = map(int, stdin.readline().split())
    p.append((C, B))

p.sort(reverse=True)

ans, cnt = 0, N
for (v, c) in p:
    use = min(c, cnt)
    ans += use * v
    cnt -= use

print(ans)

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

N, M = map(int, stdin.readline().split())
A = list(map(int, stdin.readline().split()))
for _ in range(M):
    B, C = map(int, stdin.readline().split())
    A.extend([C]*B)
A.sort(reverse=True)
ans = 0
for i in range(N):
    ans += A[i]

print(ans)


Fixed submission:
from sys import stdin, exit

N, M = map(int, stdin.readline().split())
A = list(map(int, stdin.readline().split()))
p = [(A[i], 1) for i in range(N)]
for _ in range(M):
    B, C = map(int, stdin.readline().split())
    p.append((C, B))

p.sort(reverse=True)

ans, cnt = 0, N
for (v, c) in p:
    use = min(c, cnt)
    ans += use * v
    cnt -= use

print(ans)

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

N, M = map(int, stdin.readline().split())
A = list(map(int, stdin.readline().split()))
for _ in range(M):
    B, C = map(int, stdin.readline().split())
    A.extend([C]*B)
A.sort(reverse=True)
ans = 0
for i in range(N):
    ans += A[i]

print(ans)


Fixed submission:
from sys import stdin, exit

N, M = map(int, stdin.readline().split())
A = list(map(int, stdin.readline().split()))
p = [(A[i], 1) for i in range(N)]
for _ in range(M):
    B, C = map(int, stdin.readline().split())
    p.append((C, B))

p.sort(reverse=True)

ans, cnt = 0, N
for (v, c) in p:
    use = min(c, cnt)
    ans += use * v
    cnt -= use

print(ans)

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

N, M = map(int, stdin.readline().split())
A = list(map(int, stdin.readline().split()))
low = min(A)
for _ in range(M):
    B, C = map(int, stdin.readline().split())
    if low < C:
        A.extend([C]*B)
        A.sort(reverse=True)
        A = A[:N]
        low = A[-1]

print(sum(A))


Fixed submission:
from sys import stdin, exit

N, M = map(int, stdin.readline().split())
A = list(map(int, stdin.readline().split()))
p = [(A[i], 1) for i in range(N)]
for _ in range(M):
    B, C = map(int, stdin.readline().split())
    p.append((C, B))

p.sort(reverse=True)

ans, cnt = 0, N
for (v, c) in p:
    use = min(c, cnt)
    ans += use * v
    cnt -= use

print(ans)

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

N, M = map(int, stdin.readline().split())
A = list(map(int, stdin.readline().split()))
low = min(A)
for _ in range(M):
    B, C = map(int, stdin.readline().split())
    if low < C:
        A.extend([C]*B)
A.sort(reverse=True)
A = A[:N]

print(sum(A))

Fixed submission:
from sys import stdin, exit

N, M = map(int, stdin.readline().split())
A = list(map(int, stdin.readline().split()))
p = [(A[i], 1) for i in range(N)]
for _ in range(M):
    B, C = map(int, stdin.readline().split())
    p.append((C, B))

p.sort(reverse=True)

ans, cnt = 0, N
for (v, c) in p:
    use = min(c, cnt)
    ans += use * v
    cnt -= use

print(ans)

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

N, M = map(int, stdin.readline().split())
A = list(map(int, stdin.readline().split()))
lst = []
for _ in range(M):
    B, C = map(int, stdin.readline().split())
    lst.extend([C]*B)
lst.sort(reverse=True)
A.sort()
for i in range(min(len(A), len(lst))):
    A[i] = max(A[i], lst[i])

print(sum(A))

Fixed submission:
from sys import stdin, exit

N, M = map(int, stdin.readline().split())
A = list(map(int, stdin.readline().split()))
p = [(A[i], 1) for i in range(N)]
for _ in range(M):
    B, C = map(int, stdin.readline().split())
    p.append((C, B))

p.sort(reverse=True)

ans, cnt = 0, N
for (v, c) in p:
    use = min(c, cnt)
    ans += use * v
    cnt -= use

print(ans)