Buggy python submission (verdict: Runtime Error):
import sys  
input = sys.stdin.readline
 
N, M = list(map(int, input().split()))
 
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:
import sys  
input = sys.stdin.readline
 
N, M = list(map(int, input().split()))
 
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.sort()   
num = -1
for k in range(N):
    if k == len(com):
        num = k
        break
    if A[k] > com[k]:
        num = k
        break

if num == -1:
    print(sum(com))
else:
    print(sum(com[:num])+sum(A[num:]))

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

def count_sort(arr):
    max_num = max(arr)
    min_num = min(arr)
    count = [0] * (max_num - min_num + 1)
    for ele in arr:
        count[ele - min_num] += 1

    return [ele for ele, cnt in enumerate(count, start=min_num) for __ in range(cnt)]


for i in range(M):
    b, c = list(map(int, input().split()))
    x = [c]*b
    A.extend(x)
 
A_ = count_sort(A)

    
 
print(sum(A_[-N:]))

Fixed submission:
import sys  
input = sys.stdin.readline
 
N, M = list(map(int, input().split()))
 
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.sort()   
num = -1
for k in range(N):
    if k == len(com):
        num = k
        break
    if A[k] > com[k]:
        num = k
        break

if num == -1:
    print(sum(com))
else:
    print(sum(com[:num])+sum(A[num:]))

Buggy python submission (verdict: Time Limit Exceeded):
import sys  
input = sys.stdin.readline
import numpy as np
 
N, M = list(map(int, input().split()))
 
A = list(map(int, input().split()))

card = []
for i in range(M):
    b, c = list(map(int, input().split()))
    x = [c]*b
    card.extend(x)
 
A.extend(card)
A.sort()
    
 
print(sum(A[-N:]))

Fixed submission:
import sys  
input = sys.stdin.readline
 
N, M = list(map(int, input().split()))
 
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.sort()   
num = -1
for k in range(N):
    if k == len(com):
        num = k
        break
    if A[k] > com[k]:
        num = k
        break

if num == -1:
    print(sum(com))
else:
    print(sum(com[:num])+sum(A[num:]))

Buggy python submission (verdict: Time Limit Exceeded):
import sys  
input = sys.stdin.readline
import numpy as np

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

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

card.sort()
card.reverse()

num = -1
for i in range(N):
    if i == len(card) :
        num = i
        break
    if card[i] < A[i]:
        num = i
        break
    


if num == -1:
    print(sum(card[:N]))
else:
    print(sum(card[:num]) + sum(A[num:]))

Fixed submission:
import sys  
input = sys.stdin.readline
 
N, M = list(map(int, input().split()))
 
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.sort()   
num = -1
for k in range(N):
    if k == len(com):
        num = k
        break
    if A[k] > com[k]:
        num = k
        break

if num == -1:
    print(sum(com))
else:
    print(sum(com[:num])+sum(A[num:]))