Buggy python submission (verdict: Runtime Error):
from collections import deque

N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [[0] * 2 for i in range(M)]
for i in range(M):
    BC[i][0],BC[i][1] = map(int, input().split())

A.sort()
BC = sorted(BC, key=lambda x:x[1],reverse=True)
flag = False

k = 0
for i in range(M):
    for j in range(BC[i][0]):
        if A[k] < BC[i][1]:
            A[k] = BC[i][1]
            k += 1
        else:
            print(sum(A))
            flag = True
            break
    if flag == True:
        break

print(sum(A))

Fixed submission:
from collections import deque
import sys

N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [[0] * 2 for i in range(M)]
for i in range(M):
    BC[i][0],BC[i][1] = map(int, input().split())

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

k = 0
for i in range(M):
    for j in range(BC[i][0]):
        
        if k >= N:
            print(sum(A))
            sys.exit()
        
        if A[k] < BC[i][1]:
            A[k] = BC[i][1]
            k += 1
        else:
            print(sum(A))
            sys.exit()

print(sum(A))

Buggy python submission (verdict: Runtime Error):
from collections import deque
import sys

N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [[0] * 2 for i in range(M)]
for i in range(M):
    BC[i][0],BC[i][1] = map(int, input().split())

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

k = 0
for i in range(M):
    for j in range(BC[i][0]):
        if A[k] < BC[i][1]:
            A[k] = BC[i][1]
            k += 1
        else:
            print(sum(A))
            sys.exit()

print(sum(A))

Fixed submission:
from collections import deque
import sys

N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [[0] * 2 for i in range(M)]
for i in range(M):
    BC[i][0],BC[i][1] = map(int, input().split())

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

k = 0
for i in range(M):
    for j in range(BC[i][0]):
        
        if k >= N:
            print(sum(A))
            sys.exit()
        
        if A[k] < BC[i][1]:
            A[k] = BC[i][1]
            k += 1
        else:
            print(sum(A))
            sys.exit()

print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [[0] * 2 for i in range(M)]
for i in range(M):
    BC[i][0],BC[i][1] = map(int, input().split())


for i in range(M):
    A.sort()
    for j in range(BC[i][0]):
        if A[j] < BC[i][1]:
            A[j] = BC[i][1]
        else:
            break

print(sum(A))

Fixed submission:
from collections import deque
import sys

N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [[0] * 2 for i in range(M)]
for i in range(M):
    BC[i][0],BC[i][1] = map(int, input().split())

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

k = 0
for i in range(M):
    for j in range(BC[i][0]):
        
        if k >= N:
            print(sum(A))
            sys.exit()
        
        if A[k] < BC[i][1]:
            A[k] = BC[i][1]
            k += 1
        else:
            print(sum(A))
            sys.exit()

print(sum(A))