Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
from  bisect import bisect_right
inf = float("inf")
A = sorted(list(map(int, input().split())))
BC = sorted([tuple(map(int, input().split())) for _ in range(M)], key=lambda x: -x[1])
cnt = 0
L, R = 0, N
I = []
for b, c in BC:
    idx = bisect_right(A[L:R], c)

    if idx == 0:
        I.append((L, R, 0, c))
        break
    elif idx < b:
        I.append((L+idx, R, idx, c))
        break
    elif idx >= b:
        I.append((L+idx, R, b, c))
        tmp = L
        L += b
        R = tmp + idx

    cnt += 1
    if cnt == M:
        I.append((L, R, 0, c))

S = [0]
for a in A:
    S.append(a+S[-1])

ans = 0
for L, R, x, c in I:
    ans += S[R] - S[L] + x*c

print(ans)




Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
P = [(A[i], 1) for i in range(N)]
for j in range(M):
    B, C = map(int, input().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):
N, M = map(int, input().split())
from  bisect import bisect_right
inf = float("inf")
A = sorted(list(map(int, input().split())))
BC = sorted([tuple(map(int, input().split())) for _ in range(M)], key=lambda x: -x[1])
ans = 0
cnt = 0
L, R = 0, N
for b, c in BC:
    idx = bisect_right(A[L:R], c)

    if idx == 0:
        ans += sum(A[L:R])
        break
    elif idx < b:
        ans += sum(A[L+idx:R]) + c*idx
        break
    elif idx >= b:
        ans += sum(A[L+idx:R]) + b*c
        tmp = L
        L += b
        R = tmp + idx

    cnt += 1
    if cnt == M:
        ans += sum(A[L:R])

print(ans)



Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
P = [(A[i], 1) for i in range(N)]
for j in range(M):
    B, C = map(int, input().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):
N, M = map(int, input().split())
from  bisect import bisect_right
inf = float("inf")
A = sorted(list(map(int, input().split())))
BC = sorted([tuple(map(int, input().split())) for _ in range(M)], key=lambda x: -x[1])
ans = 0
cnt = 0
L, R = 0, N
for b, c in BC:
    idx = bisect_right(A[L:R], c)

    if idx == 0:
        ans += sum(A[L:R])
        break
    elif idx < b:
        ans += sum(A[L+idx:R]) + c*idx
        break
    elif idx >= b:
        ans += sum(A[L+idx:R]) + b*c
        tmp = L
        L += b
        R = tmp + idx

    cnt += 1
    if cnt == M:
        ans += sum(A[L:R])

print(ans)



Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
P = [(A[i], 1) for i in range(N)]
for j in range(M):
    B, C = map(int, input().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):
N, M = map(int, input().split())
from  bisect import bisect_right
inf = float("inf")
A = sorted(list(map(int, input().split())))
BC = sorted([tuple(map(int, input().split())) for _ in range(M)], key=lambda x: -x[1])
ans = 0
cnt = 0
for b, c in BC:
    idx = bisect_right(A, c)
    if idx == 0:
        ans += sum(A)
        break
    if idx >= b:
        ans += sum(A[idx:]) + b*c
        A = A[b:idx]
    elif idx < b:
        ans += sum(A[idx:]) + c*idx
        break
    cnt += 1
    if cnt == M:
        ans += sum(A)

print(ans)



Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
P = [(A[i], 1) for i in range(N)]
for j in range(M):
    B, C = map(int, input().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):
N, M = map(int, input().split())
from  bisect import bisect_right
inf = float("inf")
A = sorted(list(map(int, input().split()))) + [inf]
BC = sorted([tuple(map(int, input().split())) for _ in range(M)], key=lambda x: -x[1])
for b, c in BC:
    idx = bisect_right(A, c)
    if idx == 0:
        break
    if idx >= b:
        tmp = A[b:idx]
        new = [c]*b
        A = tmp + new + A[idx:]
    elif idx < b:
        tmp = A[idx:]
        new = [c]*idx
        A = new + tmp

print(sum(A[:-1]))

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
P = [(A[i], 1) for i in range(N)]
for j in range(M):
    B, C = map(int, input().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)