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

for i in range(M):
    B, C = map(int,input().split())
    A_li.extend([C]*B)

A_li.sort()
ans = sum(A_li[-N:])
    
print(str(ans))

Fixed submission:
N, M = map(int,input().split())
A_li = list(map(int,input().split()))
CB_li = []
for i in range(M):
    B,C = map(int,input().split())
    CB_li.append([C,B])

CB_li.sort()
CB_li.reverse()

counter = 0
for i in range(M):
    li = [CB_li[i][0]]*CB_li[i][1]
    A_li.extend(li)
    counter += CB_li[i][1]
    if counter > N:
        break

A_li.sort()
ans = sum(A_li[-N:])
    
print(str(ans))

Buggy python submission (verdict: Time Limit Exceeded):

N, M = list(map(int,input().split()))
A_li = list(map(int,input().split()))

for i in range(M):
    B, C = list(map(int,input().split()))
    li = [C]*B
    A_li.extend(li)

A_li.sort()
A_li.reverse()
ans = sum(A_li[:N])
    
print(str(ans))

Fixed submission:
N, M = map(int,input().split())
A_li = list(map(int,input().split()))
CB_li = []
for i in range(M):
    B,C = map(int,input().split())
    CB_li.append([C,B])

CB_li.sort()
CB_li.reverse()

counter = 0
for i in range(M):
    li = [CB_li[i][0]]*CB_li[i][1]
    A_li.extend(li)
    counter += CB_li[i][1]
    if counter > N:
        break

A_li.sort()
ans = sum(A_li[-N:])
    
print(str(ans))

Buggy python submission (verdict: Runtime Error):

N, M = list(map(int,input().split()))
A_li = list(map(int,input().split()))
CB_li = []

for i in range(M):
    B, C = list(map(int,input().split()))
    li = [C]*B
    CB_li.extend(li)

CB_li.sort()
CB_li.reverse()

A_li.sort()
A_li.reverse()

ans_li = []

for i in range(N):
    if A_li[i] < max(CB_li):
        s = sum(A_li[:i])+sum(CB_li[:N-i])
        ans_li.append(sum)
        
print(str(max(ans_li)))


Fixed submission:
N, M = map(int,input().split())
A_li = list(map(int,input().split()))
CB_li = []
for i in range(M):
    B,C = map(int,input().split())
    CB_li.append([C,B])

CB_li.sort()
CB_li.reverse()

counter = 0
for i in range(M):
    li = [CB_li[i][0]]*CB_li[i][1]
    A_li.extend(li)
    counter += CB_li[i][1]
    if counter > N:
        break

A_li.sort()
ans = sum(A_li[-N:])
    
print(str(ans))

Buggy python submission (verdict: Runtime Error):

N, M = list(map(int,input().split()))
A_li = list(map(int,input().split()))
CB_li = []

for i in range(M):
    BandC = list(map(int,input().split()))
    BandC.reverse()
    CB_li.append(BandC)

CB_li.sort()
CB_li.reverse()
overwrite_li = []
for CB in CB_li:
    li = [CB[0]]*CB[1]
    overwrite_li.extend(li)

A_li.sort()
A_li.reverse()
j=0
for i in range(N):
    if A_li[i] < overwrite_li[j]:
        A_li[i] = overwrite_li[j]
        j+=1

ans = sum(A_li)
    
print(str(int(ans)))



Fixed submission:
N, M = map(int,input().split())
A_li = list(map(int,input().split()))
CB_li = []
for i in range(M):
    B,C = map(int,input().split())
    CB_li.append([C,B])

CB_li.sort()
CB_li.reverse()

counter = 0
for i in range(M):
    li = [CB_li[i][0]]*CB_li[i][1]
    A_li.extend(li)
    counter += CB_li[i][1]
    if counter > N:
        break

A_li.sort()
ans = sum(A_li[-N:])
    
print(str(ans))