Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
BC = [list(map(int, input().split())) for _ in range(M)]
BC.sort(key=lambda x:x[1],reverse=True)
for chenge in BC:
    kaisuu = chenge[0];  value = chenge[1]
    cnt = 0
    for i in range(len(A)):
        if A[i] < value:
            A[i] = value
            cnt += 1
        if cnt == kaisuu:
            break
print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
BC = [list(map(int, input().split())) for _ in range(M)]
BC.sort(key=lambda x:x[1],reverse=True)
i = 0
for bc in BC:
    for _ in range(bc[0]):
        if i > len(A)-1:
            break
        if A[i] > bc[1]:
            break
        A[i] = bc[1]
        i += 1
print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import heapq
N, M = map(int, input().split())
A = list(map(int, input().split()))
heapq.heapify(A)
BC = [list(map(int, input().split())) for _ in range(M)]
BC.sort(key=lambda x:x[1])
BC = deque(BC)
#print(f'A:{A} BC:{BC}')
for _ in range(M):
    tmp = BC.popleft()
    B = tmp[0]; C = tmp[1]
    #print(f'B:{B} C:{C}')
    for i in range(B):
        tmp = heapq.heappop(A)
        if tmp < C:
            heapq.heappush(A, C)
        else:
            heapq.heappush(A, tmp)
            break
    #print(f'A:{A}')
print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
BC = [list(map(int, input().split())) for _ in range(M)]
BC.sort(key=lambda x:x[1],reverse=True)
i = 0
for bc in BC:
    for _ in range(bc[0]):
        if i > len(A)-1:
            break
        if A[i] > bc[1]:
            break
        A[i] = bc[1]
        i += 1
print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
BC = [list(map(int, input().split())) for _ in range(M)]
BC.sort(key=lambda x:x[1])
for chenge in BC:
    B = chenge[0]; C = chenge[1]
    for i in range(B):
        if A[i] < C:
            A[i] = C
        else:
            break
    A.sort()
print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
BC = [list(map(int, input().split())) for _ in range(M)]
BC.sort(key=lambda x:x[1],reverse=True)
i = 0
for bc in BC:
    for _ in range(bc[0]):
        if i > len(A)-1:
            break
        if A[i] > bc[1]:
            break
        A[i] = bc[1]
        i += 1
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()))
A.sort()
BC = [list(map(int, input().split())) for _ in range(M)]
BC.sort(key=lambda x:x[1])
#print(f'A:{A} BC:{BC}')
for chenge in BC:
    B = chenge[0]; C = chenge[1]
    #print(f'B:{B} C:{C}')
    for i in range(B):
        if A[i] < C:
            A[i] = C
        else:
            break
    A.sort()
    #print(f'A:{A}')
print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
BC = [list(map(int, input().split())) for _ in range(M)]
BC.sort(key=lambda x:x[1],reverse=True)
i = 0
for bc in BC:
    for _ in range(bc[0]):
        if i > len(A)-1:
            break
        if A[i] > bc[1]:
            break
        A[i] = bc[1]
        i += 1
print(sum(A))