Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
A = list(map(int, input().split()))
BC = [tuple(map(int, input().split())) for _ in range(m)]

A.sort()
BC.sort(key=lambda x: x[1], reverse=True)

cnt = 0
for b, c in BC:
    for i in range(b):
        if c < A[cnt]:
            break
        A[cnt] = c
        cnt += 1
    if cnt >= n:
        break

print(sum(A))

Fixed submission:
n, m = map(int, input().split())
A = list(map(int, input().split()))
BC = [tuple(map(int, input().split())) for _ in range(m)]

A.sort()
BC.sort(key=lambda x: x[1], reverse=True)

cnt = 0
for b, c in BC:
    for i in range(b):
        if c < A[cnt]:
            break
        A[cnt] = c
        cnt += 1
        if cnt >= n:
            break
    else:
        continue
    break

print(sum(A))

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
A = list(map(int, input().split()))
BC = [tuple(map(int, input().split())) for _ in range(m)]

A.sort()
BC.sort(key=lambda x: x[1], reverse=True)

cnt = 0
for b, c in BC:
    for i in range(b):
        if c < A[cnt]:
            continue
        A[cnt] = c
        cnt += 1
    if cnt >= n:
        break

print(sum(A))

Fixed submission:
n, m = map(int, input().split())
A = list(map(int, input().split()))
BC = [tuple(map(int, input().split())) for _ in range(m)]

A.sort()
BC.sort(key=lambda x: x[1], reverse=True)

cnt = 0
for b, c in BC:
    for i in range(b):
        if c < A[cnt]:
            break
        A[cnt] = c
        cnt += 1
        if cnt >= n:
            break
    else:
        continue
    break

print(sum(A))

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

q_all=[]
for i in range(M):
    for j in range(sorted_q[i][0]):
        q_all.append(sorted_q[i][1])

if len(A)<=len(q_all):
    A.sort()
    for i in range(N):
        if A[i]<q_all[i]:
            A[i]=q_all[i]
else:
    A.sort()
    for i in range(len(q_all)):
        if A[i]<q_all[i]:
            A[i]=q_all[i]
        
print(sum(A))


Fixed submission:
n, m = map(int, input().split())
A = list(map(int, input().split()))
BC = [tuple(map(int, input().split())) for _ in range(m)]

A.sort()
BC.sort(key=lambda x: x[1], reverse=True)

cnt = 0
for b, c in BC:
    for i in range(b):
        if c < A[cnt]:
            break
        A[cnt] = c
        cnt += 1
        if cnt >= n:
            break
    else:
        continue
    break

print(sum(A))

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

q_all=[]
for i in range(M):
    for j in range(sorted_q[i][0]):
        q_all.append(sorted_q[i][1])

if len(A)<=len(q_all):
    A.sort()
    for i in range(N):
        if A[i]<q_all[i]:
            A[i]=q_all[i]
else:
    A.sort()
    for i in range(len(q_all)):
        if A[i]<q_all[i]:
            A[i]=q_all[i]
        
print(sum(A))

Fixed submission:
n, m = map(int, input().split())
A = list(map(int, input().split()))
BC = [tuple(map(int, input().split())) for _ in range(m)]

A.sort()
BC.sort(key=lambda x: x[1], reverse=True)

cnt = 0
for b, c in BC:
    for i in range(b):
        if c < A[cnt]:
            break
        A[cnt] = c
        cnt += 1
        if cnt >= n:
            break
    else:
        continue
    break

print(sum(A))

Buggy python submission (verdict: Runtime Error):
N,M=map(int,input().split())
A=list(map(int,input().split()))
q=[]
for i in range(M):
    q.append(list(map(int,input().split())))
    
sorted_q=sorted(q,key=lambda x:x[1],reverse=True)

q_all=[]
for i in range(M):
    for j in range(sorted_q[i][0]):
        q_all.append(sorted_q[i][1])

for i in range(N):
    if A[i]<q_all[i]:
        A[i]=q_all[i]
        
print(sum(A))

Fixed submission:
n, m = map(int, input().split())
A = list(map(int, input().split()))
BC = [tuple(map(int, input().split())) for _ in range(m)]

A.sort()
BC.sort(key=lambda x: x[1], reverse=True)

cnt = 0
for b, c in BC:
    for i in range(b):
        if c < A[cnt]:
            break
        A[cnt] = c
        cnt += 1
        if cnt >= n:
            break
    else:
        continue
    break

print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())
A=list(map(int,input().split()))
q=[]
for i in range(M):
    q.append(list(map(int,input().split())))
sorted_q=sorted(q,key=lambda x:x[1],reverse=True)
for i in range(M):
    A.sort()
    for j in range(sorted_q[i][0]):
        if A[j]<sorted_q[i][1]:
            A[j]=sorted_q[i][1]
print(sum(A))

Fixed submission:
n, m = map(int, input().split())
A = list(map(int, input().split()))
BC = [tuple(map(int, input().split())) for _ in range(m)]

A.sort()
BC.sort(key=lambda x: x[1], reverse=True)

cnt = 0
for b, c in BC:
    for i in range(b):
        if c < A[cnt]:
            break
        A[cnt] = c
        cnt += 1
        if cnt >= n:
            break
    else:
        continue
    break

print(sum(A))

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

for i in range(M):
    A.sort()
    for j in range(q[i][0]):
        if A[j]<q[i][1]:
            A[j]=q[i][1]
print(sum(A))

Fixed submission:
n, m = map(int, input().split())
A = list(map(int, input().split()))
BC = [tuple(map(int, input().split())) for _ in range(m)]

A.sort()
BC.sort(key=lambda x: x[1], reverse=True)

cnt = 0
for b, c in BC:
    for i in range(b):
        if c < A[cnt]:
            break
        A[cnt] = c
        cnt += 1
        if cnt >= n:
            break
    else:
        continue
    break

print(sum(A))