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

for i in range(len(BC)):
    D[1].append(BC[i][0])
    D[0].append(BC[i][1])

def matome(A):
    C = [[],[]]
    A.sort()
    for i in range(len(A)):
        if A[i] not in C[0]:
            C[0].append(A[i])
            C[1].append(1)
        else:
            C[1][C[0].index(A[i])] += 1
    return(C)

X = matome(X)

def futatu(A,B):
    for i in range(len(B[0])):
        if B[0][i] not in A[0]:
            A[0].append(B[0][i])
            A[1].append(B[1][i])
        else:
            A[1][A[0].index(B[0][i])] += B[1][i]
    return(A)

X = futatu(X,D)

def henkan(A):
    C = []
    for i in range(len(A[0])):
        C.append([A[0][i],A[1][i]])
    return(C)

X = sorted(henkan(X),reverse=True)

maisuu = 0
i = 0
ans = 0
while maisuu+X[i][1] <= N:
    ans += X[i][0]*X[i][1]
    maisuu += X[i][1]
    i += 1

ans += X[i][0]*(N-maisuu)

print(ans)

Fixed submission:
import collections

N,M = map(int,input().split())
A = list(map(int,input().split()))
BC = [list(map(int, input().split())) for _ in range(M)]

c = collections.Counter(A)
values,counts = zip(*c.most_common())
D=[]
for i in range(len(counts)):
    D.append([counts[i],values[i]])
for i in range(M):
    D.append(BC[i])
D=sorted(D,key=lambda x:x[1], reverse=True)

t=0
i=0
ans=0
while t<N:
    if D[i][0]+t<=N:
        ans+=D[i][1]*D[i][0]
        t+=D[i][0]
        i+=1
    else:
        ans+=D[i][1]*(N-t)
        t+=N-t
print(ans)

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

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

for i in range(M):
    for j in range(BC[i][0]):
        a = heapq.heappop(A)
        heapq.heappush(A,max(BC[i][1],a))

print(sum(A))

Fixed submission:
import collections

N,M = map(int,input().split())
A = list(map(int,input().split()))
BC = [list(map(int, input().split())) for _ in range(M)]

c = collections.Counter(A)
values,counts = zip(*c.most_common())
D=[]
for i in range(len(counts)):
    D.append([counts[i],values[i]])
for i in range(M):
    D.append(BC[i])
D=sorted(D,key=lambda x:x[1], reverse=True)

t=0
i=0
ans=0
while t<N:
    if D[i][0]+t<=N:
        ans+=D[i][1]*D[i][0]
        t+=D[i][0]
        i+=1
    else:
        ans+=D[i][1]*(N-t)
        t+=N-t
print(ans)

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 i in range(M):
    for j in range(BC[i][0]):
        if A[0] < BC[i][1]:
            A[0] = BC[i][1]
            A = sorted(A)

print(sum(A))

Fixed submission:
import collections

N,M = map(int,input().split())
A = list(map(int,input().split()))
BC = [list(map(int, input().split())) for _ in range(M)]

c = collections.Counter(A)
values,counts = zip(*c.most_common())
D=[]
for i in range(len(counts)):
    D.append([counts[i],values[i]])
for i in range(M):
    D.append(BC[i])
D=sorted(D,key=lambda x:x[1], reverse=True)

t=0
i=0
ans=0
while t<N:
    if D[i][0]+t<=N:
        ans+=D[i][1]*D[i][0]
        t+=D[i][0]
        i+=1
    else:
        ans+=D[i][1]*(N-t)
        t+=N-t
print(ans)