Buggy python submission (verdict: Time Limit Exceeded):
N,M = list(map(int,input().split()))
a_list = list(map(int,input().split()))
bc_list = []
for _ in range(M):
    b,c = list(map(int,input().split()))
    bc_list.append((b,c))

a_list = sorted(a_list)
bc_list = sorted(bc_list,key=lambda x: x[1],reverse=True)

count = 0
b_list = []
for b,c in bc_list:
    if count >= N:
        break
    for _ in range(b):
        b_list.append(c)
        
for i,a in enumerate(a_list):
    if i >= len(b_list):
        break
    
    if a < b_list[i]:
        a_list[i] = b_list[i]
    else:
        break
print(sum(a_list))

Fixed submission:
N,M = list(map(int,input().split()))
a_list = list(map(int,input().split()))
bc_list = []
for _ in range(M):
    b,c = list(map(int,input().split()))
    bc_list.append([b,c])

a_list = sorted(a_list)
bc_list = sorted(bc_list,key=lambda x: x[1],reverse=True)

tmp = 0
for i,a in enumerate(a_list):
    if tmp >= len(bc_list):
        break
    
    if a < bc_list[tmp][1]:
        bc_list[tmp][0] -= 1
        a_list[i] = bc_list[tmp][1]
    else:
        break

    if bc_list[tmp][0] == 0:
        tmp += 1
print(sum(a_list))

Buggy python submission (verdict: Time Limit Exceeded):
n,m=[int(i) for i in input().split()]
a_list=[int(i) for i in input().split()]

b_list=[]
for i in range(m):
    b,c=map(int,input().split())
    b_list+=[c]*b
a_list+=b_list
a_list.sort(reverse=True)
print(sum(a_list[:n]))

Fixed submission:
N,M = list(map(int,input().split()))
a_list = list(map(int,input().split()))
bc_list = []
for _ in range(M):
    b,c = list(map(int,input().split()))
    bc_list.append([b,c])

a_list = sorted(a_list)
bc_list = sorted(bc_list,key=lambda x: x[1],reverse=True)

tmp = 0
for i,a in enumerate(a_list):
    if tmp >= len(bc_list):
        break
    
    if a < bc_list[tmp][1]:
        bc_list[tmp][0] -= 1
        a_list[i] = bc_list[tmp][1]
    else:
        break

    if bc_list[tmp][0] == 0:
        tmp += 1
print(sum(a_list))

Buggy python submission (verdict: Time Limit Exceeded):
n,m=[int(i) for i in input().split()]
a_list=[int(i) for i in input().split()]

b_list=[]
for i in range(m):
    b,c=map(int,input().split())
    b_list+=[c]*b
a_list+=b_list
a_list.sort(reverse=True)
print(sum(a_list[:n]))

Fixed submission:
N,M = list(map(int,input().split()))
a_list = list(map(int,input().split()))
bc_list = []
for _ in range(M):
    b,c = list(map(int,input().split()))
    bc_list.append([b,c])

a_list = sorted(a_list)
bc_list = sorted(bc_list,key=lambda x: x[1],reverse=True)

tmp = 0
for i,a in enumerate(a_list):
    if tmp >= len(bc_list):
        break
    
    if a < bc_list[tmp][1]:
        bc_list[tmp][0] -= 1
        a_list[i] = bc_list[tmp][1]
    else:
        break

    if bc_list[tmp][0] == 0:
        tmp += 1
print(sum(a_list))

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

n,m=[int(i) for i in input().split()]
a_list=[int(i) for i in input().split()]
a_list.sort()

for i in range(m):
    b,c=map(int,input().split())
    index=bisect.bisect_left(a_list,c)
    if index<=0:
        continue
    if index>b:
        if b==index:
            a_list=[a_list[b]]+[c]*b+a_list[index:]
        else:
            a_list=a_list[b:index]+[c]*b+a_list[index:]
    else:
        a_list=[c]*index+a_list[index:]
    
print(sum(a_list))

Fixed submission:
N,M = list(map(int,input().split()))
a_list = list(map(int,input().split()))
bc_list = []
for _ in range(M):
    b,c = list(map(int,input().split()))
    bc_list.append([b,c])

a_list = sorted(a_list)
bc_list = sorted(bc_list,key=lambda x: x[1],reverse=True)

tmp = 0
for i,a in enumerate(a_list):
    if tmp >= len(bc_list):
        break
    
    if a < bc_list[tmp][1]:
        bc_list[tmp][0] -= 1
        a_list[i] = bc_list[tmp][1]
    else:
        break

    if bc_list[tmp][0] == 0:
        tmp += 1
print(sum(a_list))

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

n,m=[int(i) for i in input().split()]
a_list=[int(i) for i in input().split()]
a_list.sort()

for i in range(m):
    b,c=map(int,input().split())
    index=bisect.bisect_left(a_list,c)
    if index<=0:
        continue
    if index>b:
        if b==index:
            a_list=[a_list[b]]+[c]*b+a_list[index:]
        else:
            a_list=a_list[b:index]+[c]*b+a_list[index:]
    else:
        a_list=[c]*index+a_list[index:]
    
print(sum(a_list))

Fixed submission:
N,M = list(map(int,input().split()))
a_list = list(map(int,input().split()))
bc_list = []
for _ in range(M):
    b,c = list(map(int,input().split()))
    bc_list.append([b,c])

a_list = sorted(a_list)
bc_list = sorted(bc_list,key=lambda x: x[1],reverse=True)

tmp = 0
for i,a in enumerate(a_list):
    if tmp >= len(bc_list):
        break
    
    if a < bc_list[tmp][1]:
        bc_list[tmp][0] -= 1
        a_list[i] = bc_list[tmp][1]
    else:
        break

    if bc_list[tmp][0] == 0:
        tmp += 1
print(sum(a_list))

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

n,m=[int(i) for i in input().split()]
a_list=[int(i) for i in input().split()]
a_list.sort()

for i in range(m):
    b,c=map(int,input().split())
    index=bisect.bisect_left(a_list,c)
    if index<=0:
        continue
    if index>b:
        index=b
    a_list=[c]*index+a_list[index:]
    a_list.sort()
    
print(sum(a_list))

Fixed submission:
N,M = list(map(int,input().split()))
a_list = list(map(int,input().split()))
bc_list = []
for _ in range(M):
    b,c = list(map(int,input().split()))
    bc_list.append([b,c])

a_list = sorted(a_list)
bc_list = sorted(bc_list,key=lambda x: x[1],reverse=True)

tmp = 0
for i,a in enumerate(a_list):
    if tmp >= len(bc_list):
        break
    
    if a < bc_list[tmp][1]:
        bc_list[tmp][0] -= 1
        a_list[i] = bc_list[tmp][1]
    else:
        break

    if bc_list[tmp][0] == 0:
        tmp += 1
print(sum(a_list))