Buggy python submission (verdict: Runtime Error):
A = list(map(int, input().split()))
BC = []
for i in range(M):
    b, c = list(map(int, input().split()))
    BC.append([b, c])
BC = sorted(BC, reverse=True, key=lambda x:x[1])
 
cnt = 0
com = []
for b, c in BC:
    cnt += b
    if cnt > N:
        add = N-cnt+b
        com.extend([c]*add)
        break
    else:
        x = [c]*b 
        com.extend(x)
A.extend(com)
A = sorted(A, reverse=True)   
 
print(sum(A[:N]))

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

cnt = 0
for card in cards:
    A += [card[1]]*card[0]
    cnt += card[0]
    if cnt >= N:
        break
A.sort(reverse=True)
print(sum(A[:N]))

Buggy python submission (verdict: Runtime Error):
A = list(map(int, input().split()))
 
 
 
BC = []
for i in range(M):
    b, c = list(map(int, input().split()))
    BC.append([b, c])
 
BC=sorted(BC, reverse=True, key=lambda x:x[1])
 
cnt = 0
com = []
for b, c in BC:
    cnt += b
    if cnt > N:
        add = N-cnt+b
        com.extend([c]*add)
        break
    else:
        x = [c]*b 
        com.extend(x)
 
A.extend(com)
A = sorted(A, reverse=True)   
 
print(sum(A[:N]))

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

cnt = 0
for card in cards:
    A += [card[1]]*card[0]
    cnt += card[0]
    if cnt >= N:
        break
A.sort(reverse=True)
print(sum(A[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))

for i in range(M):
    b, c = list(map(int, input().split()))
    A.extend([c]*b)
A.sort(reverse=True)

print(sum(A[:N]))


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

cnt = 0
for card in cards:
    A += [card[1]]*card[0]
    cnt += card[0]
    if cnt >= N:
        break
A.sort(reverse=True)
print(sum(A[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))

for i in range(M):
    b, c = list(map(int, input().split()))
    A += [c]*b
A.sort(reverse=True)

print(sum(A[:N]))


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

cnt = 0
for card in cards:
    A += [card[1]]*card[0]
    cnt += card[0]
    if cnt >= N:
        break
A.sort(reverse=True)
print(sum(A[:N]))

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

for card in cards:
    A += [card[1]]*card[0]
A.sort(reverse=True)

print(sum(A[:N]))


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

cnt = 0
for card in cards:
    A += [card[1]]*card[0]
    cnt += card[0]
    if cnt >= N:
        break
A.sort(reverse=True)
print(sum(A[:N]))

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

for card in cards:
    A += [card[1]]*card[0]
A.sort(reverse=True)

print(sum(A[:N]))


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

cnt = 0
for card in cards:
    A += [card[1]]*card[0]
    cnt += card[0]
    if cnt >= N:
        break
A.sort(reverse=True)
print(sum(A[:N]))

Buggy python submission (verdict: Runtime Error):
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort()

cards = [list(map(int, input().split())) for i in range(M)]
D = []
for card in cards:
    D += [card[1]]*card[0]
D.sort(reverse=True)

i = 0
ans = sum(A)
while (D[i] > A[i]):
    ans += (D[i] - A[i])
    i += 1
    if i == N - 1:
        print(ans)
        exit()
print(ans)


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

cnt = 0
for card in cards:
    A += [card[1]]*card[0]
    cnt += card[0]
    if cnt >= N:
        break
A.sort(reverse=True)
print(sum(A[:N]))