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

A.sort(reverse=True)
BC0.sort(key = lambda x:x[1], reverse=True)

BC = []
for m in range(M):
    BC += [BC0[m][1]] * BC0[m][0]
    
summation = 0
for n in range(N):
    if len(BC)==0 or A[0]>=BC[0]:
        summation += A.pop(0)
    else:
        summation += BC.pop(0)
        
print(summation)

Fixed submission:
N, M = map(int, input().split())
A0 = list(map(int, input().split()))
BC = [list(map(int, input().split())) for _ in range(M)]

A = [[1,a] for a in A0] 
ABC = A + BC
ABC.sort(key=lambda x:x[1], reverse=True)

summation = 0
p = 0
n = 0

while n<N:
    summation += ABC[p][1] * (min(ABC[p][0], N-n))
    n += min(ABC[p][0], N-n)
    p += 1

print(summation)

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

N, M = map(int, sys.stdin.readline().split())
A = list(map(int, sys.stdin.readline().split()))
BC0 = [0]*M
for m in range(M):
    BC0[m] = list(map(int, sys.stdin.readline().split()))
A.sort()
BC0.sort(key=lambda x:x[1])
BC = []
for bc0 in BC0:
    BC = BC + [bc0[1]]*bc0[0]

S = [0] * (N+1)
S[0] = sum(A)
for n in range(min(N,len(BC))):
    S[n+1] = S[n] - A[n] + BC[-n-1]
print(max(S))

Fixed submission:
N, M = map(int, input().split())
A0 = list(map(int, input().split()))
BC = [list(map(int, input().split())) for _ in range(M)]

A = [[1,a] for a in A0] 
ABC = A + BC
ABC.sort(key=lambda x:x[1], reverse=True)

summation = 0
p = 0
n = 0

while n<N:
    summation += ABC[p][1] * (min(ABC[p][0], N-n))
    n += min(ABC[p][0], N-n)
    p += 1

print(summation)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC0 = [0]*M
for m in range(M):
    BC0[m] = list(map(int, input().split()))
A.sort()
BC0.sort(key=lambda x:x[1])
BC = []
for bc0 in BC0:
    BC = BC + [bc0[1]]*bc0[0]

S = [0] * (N+1)
S[0] = sum(A)
for n in range(min(N,len(BC))):
    S[n+1] = S[n] - A[n] + BC[-n-1]
print(max(S))

Fixed submission:
N, M = map(int, input().split())
A0 = list(map(int, input().split()))
BC = [list(map(int, input().split())) for _ in range(M)]

A = [[1,a] for a in A0] 
ABC = A + BC
ABC.sort(key=lambda x:x[1], reverse=True)

summation = 0
p = 0
n = 0

while n<N:
    summation += ABC[p][1] * (min(ABC[p][0], N-n))
    n += min(ABC[p][0], N-n)
    p += 1

print(summation)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC0 = [0]*M
for m in range(M):
    BC0[m] = list(map(int, input().split()))
A.sort()
BC0.sort(key=lambda x:x[1])
BC = []
for bc0 in BC0:
    BC = BC + [bc0[1]]*bc0[0]

S = []
while len(S)<N:
    if len(BC)==0:
        S = S + A[-(N-len(S)):]
    elif A[-1] >= BC[-1]:
        S.append(A.pop())
    else:
        S.append(BC.pop())
        
print(sum(S))

Fixed submission:
N, M = map(int, input().split())
A0 = list(map(int, input().split()))
BC = [list(map(int, input().split())) for _ in range(M)]

A = [[1,a] for a in A0] 
ABC = A + BC
ABC.sort(key=lambda x:x[1], reverse=True)

summation = 0
p = 0
n = 0

while n<N:
    summation += ABC[p][1] * (min(ABC[p][0], N-n))
    n += min(ABC[p][0], N-n)
    p += 1

print(summation)

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC0 = [0]*M
for m in range(M):
    BC0[m] = list(map(int, input().split()))
A.sort()
BC0.sort(key=lambda x:x[1])
BC = []
for bc0 in BC0:
    BC = BC + [bc0[1]]*bc0[0]

S = []
while len(S)<N:
    if len(A)==0:
        S = S + A[-(N-len(S))]
    elif A[-1] >= BC[-1]:
        S.append(A.pop())
    else:
        S.append(BC.pop())
        
print(sum(S))

Fixed submission:
N, M = map(int, input().split())
A0 = list(map(int, input().split()))
BC = [list(map(int, input().split())) for _ in range(M)]

A = [[1,a] for a in A0] 
ABC = A + BC
ABC.sort(key=lambda x:x[1], reverse=True)

summation = 0
p = 0
n = 0

while n<N:
    summation += ABC[p][1] * (min(ABC[p][0], N-n))
    n += min(ABC[p][0], N-n)
    p += 1

print(summation)

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC0 = [0]*M
for m in range(M):
    BC0[m] = list(map(int, input().split()))
A.sort()
BC0.sort(key=lambda x:x[1])
BC = []
for bc0 in BC0:
    BC = BC + [bc0[1]]*bc0[0]

S = []
while len(S)<N:
    if A[-1] >= BC[-1]:
        S.append(A.pop())
    else:
        S.append(BC.pop())
        
print(sum(S))

Fixed submission:
N, M = map(int, input().split())
A0 = list(map(int, input().split()))
BC = [list(map(int, input().split())) for _ in range(M)]

A = [[1,a] for a in A0] 
ABC = A + BC
ABC.sort(key=lambda x:x[1], reverse=True)

summation = 0
p = 0
n = 0

while n<N:
    summation += ABC[p][1] * (min(ABC[p][0], N-n))
    n += min(ABC[p][0], N-n)
    p += 1

print(summation)