Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
addable = []
for m in range(M):
    B, C = map(int, input().split())
    addable += [C] * B

addable.sort()
addable = addable[-len(A):]
NA = A + addable
NA.sort()
print(sum(NA[-len(A):]))


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
p = [(A[i], 1) for i in range(N)]
for m in range(M):
    B, C = map(int, input().split())
    p.append((C, B))

p.sort()
p.reverse()

ans = 0
for v, n in p:
    use = min(N, n)
    ans += use * v
    N -= use
    if N == 0:
        break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
for m in range(M):
    B, C = map(int, input().split())
    NA = A + [C] * B
    NA.sort()
    NA = NA[B:]
    A = NA
print(sum(A))


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
p = [(A[i], 1) for i in range(N)]
for m in range(M):
    B, C = map(int, input().split())
    p.append((C, B))

p.sort()
p.reverse()

ans = 0
for v, n in p:
    use = min(N, n)
    ans += use * v
    N -= use
    if N == 0:
        break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
for m in range(M):
    B, C = map(int, input().split())
    NA = A + [C for i in range(B)]
    NA.sort()
    NA = NA[B:]
    A = NA
print(sum(A))


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
p = [(A[i], 1) for i in range(N)]
for m in range(M):
    B, C = map(int, input().split())
    p.append((C, B))

p.sort()
p.reverse()

ans = 0
for v, n in p:
    use = min(N, n)
    ans += use * v
    N -= use
    if N == 0:
        break
print(ans)

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

N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
for m in range(M):

    B, C = map(int, input().split())
    assi = min(len(A), B)-1

    if A[assi] <= C:
        A[0:assi+1] = [C for c in range(assi+1)]
        A.sort()
    else:
        assi = bisect.bisect_left(A, C)
        A[0:assi] = [C for c in range(assi)]

print(sum(A))


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
p = [(A[i], 1) for i in range(N)]
for m in range(M):
    B, C = map(int, input().split())
    p.append((C, B))

p.sort()
p.reverse()

ans = 0
for v, n in p:
    use = min(N, n)
    ans += use * v
    N -= use
    if N == 0:
        break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
for m in range(M):

    B, C = map(int, input().split())
    assi = min(len(A), B)-1

    if A[assi] <= C:
        A[0:assi+1] = [C for c in range(assi+1)]
        A.sort()
    else:
        for a in range(assi):
            if A[a] >= C:
                break
            A[a] = C

print(sum(A))


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
p = [(A[i], 1) for i in range(N)]
for m in range(M):
    B, C = map(int, input().split())
    p.append((C, B))

p.sort()
p.reverse()

ans = 0
for v, n in p:
    use = min(N, n)
    ans += use * v
    N -= use
    if N == 0:
        break
print(ans)