Buggy python submission (verdict: Time Limit Exceeded):
N,M = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]

tmp = []
for _ in range(M):
    b,c = [int(i) for i in input().split()]
    tmp += [c]*b

A.sort()
tmp.sort(reverse=True)

z = len(tmp)
ans = 0
for i in range(N):
    if i < z:
        ans += max(A[i],tmp[i])
    else:
        ans += A[i]

print(ans)


Fixed submission:
N,M = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
bc = [tuple(int(i) for i in input().split()) for _ in range(M)]

A.sort()
bc.sort(key=lambda t: t[1], reverse=True)

index = 0
count = 0
for i in range(N):
    if index >= M or A[i] >= bc[index][1]:
        break
    A[i] = bc[index][1]
    count += 1
    if bc[index][0] <= count:
        index += 1
        count = 0

print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
N,M = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]

tmp = []
for _ in range(M):
    b,c = [int(i) for i in input().split()]
    tmp += [c]*b

A += tmp
A.sort(reverse=True)
print(sum(A[:N]))


Fixed submission:
N,M = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
bc = [tuple(int(i) for i in input().split()) for _ in range(M)]

A.sort()
bc.sort(key=lambda t: t[1], reverse=True)

index = 0
count = 0
for i in range(N):
    if index >= M or A[i] >= bc[index][1]:
        break
    A[i] = bc[index][1]
    count += 1
    if bc[index][0] <= count:
        index += 1
        count = 0

print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
N,M = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]

for _ in range(M):
    b,c = [int(i) for i in input().split()]
    tmp = [c]*b
    A += tmp

A.sort(reverse=True)
print(sum(A[:N]))


Fixed submission:
N,M = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
bc = [tuple(int(i) for i in input().split()) for _ in range(M)]

A.sort()
bc.sort(key=lambda t: t[1], reverse=True)

index = 0
count = 0
for i in range(N):
    if index >= M or A[i] >= bc[index][1]:
        break
    A[i] = bc[index][1]
    count += 1
    if bc[index][0] <= count:
        index += 1
        count = 0

print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
N,M = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]

for _ in range(M):
    b,c = [int(i) for i in input().split()]
    tmp = [c]*b
    A += tmp

A.sort(reverse=True)
print(sum(A[:N]))


Fixed submission:
N,M = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
bc = [tuple(int(i) for i in input().split()) for _ in range(M)]

A.sort()
bc.sort(key=lambda t: t[1], reverse=True)

index = 0
count = 0
for i in range(N):
    if index >= M or A[i] >= bc[index][1]:
        break
    A[i] = bc[index][1]
    count += 1
    if bc[index][0] <= count:
        index += 1
        count = 0

print(sum(A))

Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

N,M = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]

l = [[] for _ in range(M)]
for j in range(M):
    b,c = [int(i) for i in input().split()]
    l[j] = [c,b]

l.sort(reverse=True)
A.sort()
ans = 0

for i,j in l:
    for k in range(N):
        if i <= A[k]:
            break
    k = min(k,j)
    ans += k*i
    A = A[k:]
if A:
    print(ans+sum(A))
else:
    print(ans)


Fixed submission:
N,M = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
bc = [tuple(int(i) for i in input().split()) for _ in range(M)]

A.sort()
bc.sort(key=lambda t: t[1], reverse=True)

index = 0
count = 0
for i in range(N):
    if index >= M or A[i] >= bc[index][1]:
        break
    A[i] = bc[index][1]
    count += 1
    if bc[index][0] <= count:
        index += 1
        count = 0

print(sum(A))

Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

N,M = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]

l = [[] for _ in range(M)]
for j in range(M):
    b,c = [int(i) for i in input().split()]
    l[j] = [c,b]

l.sort(reverse=True)
A.sort()
ans = 0

for i,j in l:
    for k in range(N):
        if i <= A[k]:
            break
    k = min(k,j)
    ans += k*i
    A = A[k:]
print(ans+sum(A))


Fixed submission:
N,M = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
bc = [tuple(int(i) for i in input().split()) for _ in range(M)]

A.sort()
bc.sort(key=lambda t: t[1], reverse=True)

index = 0
count = 0
for i in range(N):
    if index >= M or A[i] >= bc[index][1]:
        break
    A[i] = bc[index][1]
    count += 1
    if bc[index][0] <= count:
        index += 1
        count = 0

print(sum(A))

Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

N,M = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]

l = [[] for _ in range(M)]
for j in range(M):
    b,c = [int(i) for i in input().split()]
    l[j] = [c,b]

l.sort(reverse=True)
A.sort()
ans = 0

for i,j in l:
    for k in range(N):
        if i <= A[k]:
            break
    k = min(k,j)
    ans += k*i
    A = A[k:]
    print(ans)

print(ans+sum(A))


Fixed submission:
N,M = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
bc = [tuple(int(i) for i in input().split()) for _ in range(M)]

A.sort()
bc.sort(key=lambda t: t[1], reverse=True)

index = 0
count = 0
for i in range(N):
    if index >= M or A[i] >= bc[index][1]:
        break
    A[i] = bc[index][1]
    count += 1
    if bc[index][0] <= count:
        index += 1
        count = 0

print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import sys
input = sys.stdin.readline

N,M = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]

l = [[] for _ in range(M)]
for j in range(M):
    b,c = [int(i) for i in input().split()]
    l[j] = [c,b]
l.sort(reverse=True)
A.sort()
ans = 0
for i,j in l:
    insert_index = min(bisect.bisect_left(A,i), j)
    ans += insert_index*i
    A = A[insert_index:]
print(ans+sum(A))


Fixed submission:
N,M = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
bc = [tuple(int(i) for i in input().split()) for _ in range(M)]

A.sort()
bc.sort(key=lambda t: t[1], reverse=True)

index = 0
count = 0
for i in range(N):
    if index >= M or A[i] >= bc[index][1]:
        break
    A[i] = bc[index][1]
    count += 1
    if bc[index][0] <= count:
        index += 1
        count = 0

print(sum(A))