Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)
BC = sorted(
    [list(map(int, input().split())) for _ in range(M)],
    reverse=True,
    key=lambda x: x[1]
)
 
BClist = []
for m in range(M):
    b, c = BC[m]
    BClist.extend([c for _ in range(b)])
    if len(BClist) > N:
        break
 
final_list = []
for n in range(N):
    if BClist == []:
        new = A[0]
        A.pop(0)
    else:
        if A[0] > BClist[0]:
            new = A[0]
            A.pop(0)
        else:
            new = BClist[0]
            BClist.pop(0)
    final_list.append(new)
print(sum(final_list))

Fixed submission:
N, M = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)
BC = sorted(
    [list(map(int, input().split())) for _ in range(M)],
    reverse=True,
    key=lambda x: x[1]
)

BClist = []
for m in range(M):
    b, c = BC[m]
    BClist.extend([c for _ in range(b)])
    if len(BClist) > N:
        break

final_list = [None]*N
i_a = 0
i_bc = 0
for n in range(N):
    if i_bc >= len(BClist):
        final_list[n] = A[i_a]
        i_a += 1
    else:
        if A[i_a] > BClist[i_bc]:
            final_list[n] = A[i_a]
            i_a += 1
        else:
            final_list[n] = BClist[i_bc]
            i_bc += 1
print(sum(final_list))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)
BC = sorted(
    [list(map(int, input().split())) for _ in range(M)],
    reverse=True,
    key=lambda x: x[1]
)

BClist = []
for m in range(M):
    b, c = BC[m]
    BClist.extend([c for _ in range(b)])
    if len(BClist) > N:
        break

final_list = []
for n in range(N):
    if BClist == []:
        new = A[0]
        A.pop(0)
    else:
        if A[0] > BClist[0]:
            new = A[0]
            A.pop(0)
        else:
            new = BClist[0]
            BClist.pop(0)
    final_list.append(new)
print(sum(final_list))

Fixed submission:
N, M = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)
BC = sorted(
    [list(map(int, input().split())) for _ in range(M)],
    reverse=True,
    key=lambda x: x[1]
)

BClist = []
for m in range(M):
    b, c = BC[m]
    BClist.extend([c for _ in range(b)])
    if len(BClist) > N:
        break

final_list = [None]*N
i_a = 0
i_bc = 0
for n in range(N):
    if i_bc >= len(BClist):
        final_list[n] = A[i_a]
        i_a += 1
    else:
        if A[i_a] > BClist[i_bc]:
            final_list[n] = A[i_a]
            i_a += 1
        else:
            final_list[n] = BClist[i_bc]
            i_bc += 1
print(sum(final_list))

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

for b, c in BC:
    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 = sorted(list(map(int, input().split())), reverse=True)
BC = sorted(
    [list(map(int, input().split())) for _ in range(M)],
    reverse=True,
    key=lambda x: x[1]
)

BClist = []
for m in range(M):
    b, c = BC[m]
    BClist.extend([c for _ in range(b)])
    if len(BClist) > N:
        break

final_list = [None]*N
i_a = 0
i_bc = 0
for n in range(N):
    if i_bc >= len(BClist):
        final_list[n] = A[i_a]
        i_a += 1
    else:
        if A[i_a] > BClist[i_bc]:
            final_list[n] = A[i_a]
            i_a += 1
        else:
            final_list[n] = BClist[i_bc]
            i_bc += 1
print(sum(final_list))