qyliang/CodeNanoFix huggingface dataset:
Buggy code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate
import sys



def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LIM(): return list(map(lambda x:int(x) - 1, sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 1000000007


n, m = LI()
nums = LI()
que = []
for k, v in Counter(nums).items():
    heappush(que, (-k, v))
for i, j in LIR(m):
    heappush(que, (-j, i))


ret = 0
ans = 0
while ret < n:
    k, v = heappop(que)
    ret += v
    ans += -k * v


print(ans - (ret - n) * v)

Fixed code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate
import sys



def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LIM(): return list(map(lambda x:int(x) - 1, sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 1000000007


n, m = LI()
nums = LI()
que = []
for k, v in Counter(nums).items():
    heappush(que, (-k, v))
for i, j in LIR(m):
    heappush(que, (-j, i))


ret = 0
ans = 0
while ret < n:
    k, v = heappop(que)
    ret += v
    ans += -k * v


print(ans + (ret - n) * k)
Buggy code:
N, M = map(int, input().split())
a = [(1, int(i) for i in input().split())]
for _ in range(M):
    b, c = map(int, input().split())
    a.append((b, c))
a = sorted(a, key=lambda x: x[1])
ans = 0
t = 0
while t < N:
    b, c = a.pop()
    p = min(b, N-t)
    ans += p * c
    t += p
print(ans)


Fixed code:
n, m = map(int, input().split())
a = [(1, int(i)) for i in input().split()]
for _ in range(m):
    b, c = map(int, input().split())
    a.append((b, c))
a = sorted(a, key=lambda x: x[1])
ans = 0
t = 0
while t < n:
    b, c = a.pop()
    p = min(b, n-t)
    ans += p * c
    t += p
print(ans)
Buggy code:
def slove():
    import sys, heapq
    input = sys.stdin.readline
    n, m = list(map(int, input().rstrip('\n').split()))
    a = list(map(int, input().rstrip('\n').split()))
    bc = [list(map(int, input().rstrip('\n').split())) for _ in range(m)]
    heapq.heapify(a)
    bs = sorted(bc, key=lambda x: x[1], reverse=True)
    for b, c in bc:
        bo = True
        for i in range(b):
            p = heapq.heappop(a)
            if p < c:
                heapq.heappush(a, c)
            else:
                heapq.heappush(a, p)
                bo = False
                break
        if not bo:
            break
    print(sum(a))


if __name__ == '__main__':
    slove()


Fixed code:
def slove():
    import sys, heapq
    input = sys.stdin.readline
    n, m = list(map(int, input().rstrip('\n').split()))
    a = list(map(int, input().rstrip('\n').split()))
    bc = [list(map(int, input().rstrip('\n').split())) for _ in range(m)]
    heapq.heapify(a)
    bc = sorted(bc, key=lambda x: x[1], reverse=True)
    for b, c in bc:
        bo = True
        for i in range(b):
            p = heapq.heappop(a)
            if p < c:
                heapq.heappush(a, c)
            else:
                heapq.heappush(a, p)
                bo = False
                break
        if not bo:
            break
    print(sum(a))


if __name__ == '__main__':
    slove()
Buggy code:

def main():

    N,M = map(int,input().split())
    A = [i for i in map(int,input().split())]
    A.sort()
    tmpC = 0
    targetList = []

    for i in range(M):

        element = list(map(int,input().split()))
        targetList.append(element)

    targetList.sort(key=lambda x:x[1],reverse=True)
    
    flag = False
    ans = 0
    for b,c in targetList:
        for j in range(b):
            if N > tmpC and A[tmpC]:
                ans += c
                tmpC +=1
            else:
                flag = True
                break
        if flag:
            break
    
    if tmpC < N:

        ans += sum(A[tmpC:])


    print(ans)






if __name__== "__main__":
    main() 


Fixed code:

def main():

    N,M = map(int,input().split())
    A = [i for i in map(int,input().split())]
    A.sort()
    tmpC = 0
    targetList = []

    for i in range(M):

        element = list(map(int,input().split()))
        targetList.append(element)

    targetList.sort(key=lambda x:x[1],reverse=True)
    
    flag = False
    ans = 0
    for b,c in targetList:
        for j in range(b):
            if N > tmpC and A[tmpC] < c:
                ans += c
                tmpC +=1
            else:
                flag = True
                break
        if flag:
            break
    
    if tmpC < N:

        ans += sum(A[tmpC:])


    print(ans)






if __name__== "__main__":
    main()
Buggy code:
N,M = map(int,input().split())
A = list(map(int,input().split()))
BC = [(A[i], 1) for i in range(N)]
for _ in range(M):
    B, C = map(int, input().split())
    BC.append((C,B))
BC = sorted(BC,reverse = True)
ans = 0
a = 0
count = N
for c,b in BC:
  print(b,c)
  lamb = min(count,b)
  ans += c * lamb
  count -= lamb

print(ans)

Fixed code:
N,M = map(int,input().split())
A = list(map(int,input().split()))
BC = [(A[i], 1) for i in range(N)]
for _ in range(M):
    B, C = map(int, input().split())
    BC.append((C,B))
BC = sorted(BC,reverse = True)
ans = 0
a = 0
count = N
for c,b in BC:
  lamb = min(count,b)
  ans += c * lamb
  count -= lamb

print(ans)
Buggy code:
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
bc=[list(map(int,input().split()))[::-1]for _ in range(m)]+[[0,1000000000000]]
bc.sort(reverse=1)
t=[]
for c,b in bc:
  for _ in range(b):
    t.append(c)
    if len(t)==n:break
ans=0
for i in range(n):ans+=max(a[i],t[i])
print(ans)

Fixed code:
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
bc=[list(map(int,input().split()))[::-1]for _ in range(m)]+[[0,1000000000000]]
bc.sort(reverse=1)
t=[]
for c,b in bc:
  for _ in range(b):
    t.append(c)
    if len(t)>=n:break
ans=0
for i in range(n):ans+=max(a[i],t[i])
print(ans)
Buggy code:
from collections import defaultdict
N, M = map(int, input().split())
A = [int(i) for i in input().split()]

dic = defaultdict(int)
for i in range(M):
    b, c = map(int, input().split())
    dic[c] += b
for x in A:
    dic[x] += 1
dic = sorted(dic.items(), key=lambda x:x[0], reverse=True)
cur = 0
ans = 0
for x, y in dic.items():
    cur += y
    if cur > len(A):
        print(ans + x*(y - (cur - len(A)))
        sys.exit()
    else:
        ans += x*y

Fixed code:
from collections import defaultdict
import sys
N, M = map(int, input().split())
A = [int(i) for i in input().split()]

dic = defaultdict(int)
for i in range(M):
    b, c = map(int, input().split())
    dic[c] += b
for x in A:
    dic[x] += 1
dic = sorted(dic.items(), key=lambda x:x[0], reverse=True)
cur = 0
ans = 0
for x, y in dic:
    cur += y
    if cur > len(A):
        print(ans + x*(y - (cur - len(A))))
        sys.exit()
    else:
        ans += x*y
Buggy code:
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
CB = []
for i in range(M):
    B, C = list(map(int, input().split()))
    CB.append([C, B])
 
A.sort()
A.reverse()
CB.sort()
CB.reverse()
 
ans = 0
a_index = 0
cb_index = 0
f = 0
 
while True:
    if f = 1 or A[a_index] >= CB[cb_index][0]:
        ans += A[a_index]
        a_index += 1
        N -= 1
        if N == 0:
            break
    else:
        if N <= CB[cb_index][1]:
            ans += CB[cb_index][0] * N
            break
        else:
            ans += CB[cb_index][0] * CB[cb_index][1]
            N -= CB[cb_index][1]
            cb_index += 1
            if len(CB) == cb_index:
              f = 1
 
print(ans)
 

Fixed code:
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
CB = []
for i in range(M):
    B, C = list(map(int, input().split()))
    CB.append([C, B])
 
A.sort()
A.reverse()
CB.sort()
CB.reverse()
 
ans = 0
a_index = 0
cb_index = 0
f = 0
 
while True:
    if f == 1 or A[a_index] >= CB[cb_index][0]:
        ans += A[a_index]
        a_index += 1
        N -= 1
        if N == 0:
            break
    else:
        if N <= CB[cb_index][1]:
            ans += CB[cb_index][0] * N
            break
        else:
            ans += CB[cb_index][0] * CB[cb_index][1]
            N -= CB[cb_index][1]
            cb_index += 1
            if len(CB) == cb_index:
              f = 1
 
print(ans)
Buggy code:
n, m = map(int, input().split())
a = sorted(list(map(int, input().split())))
w = [tuple(map(int, input().split()))for _ in range(m)]
w.sort(key=lambda x: x[1], reverse=True)
ans = 0
p, i = 0, 0
b, c = w[i][0], w[i][1]

while p < n and i < m:
    cur = a[p]
    if cur < c:
        b -= 1
        ans += c
    else:
        ans += cur
    if b == 0:
        i += 1
        b, c = w[i][0], w[i][1]
    p += 1

print(ans)

Fixed code:
n, m = map(int, input().split())
a = sorted(list(map(int, input().split())))
w = [tuple(map(int, input().split()))for _ in range(m)]
w.sort(key=lambda x: x[1], reverse=True)
ans = 0
p, i = 0, 0
b, c = w[i][0], w[i][1]
while p < n and i < m:
    cur = a[p]
    if cur < c:
        b -= 1
        ans += c
    else:
        ans += cur
    if b == 0:
        i += 1
        if i == m:
            ans += sum(a[p+1:])
            break
        b, c = w[i][0], w[i][1]
    p += 1
print(ans)
Buggy code:
from collections import deque
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
a=deque(a)
x=[]
for i in range(m):x.append(list(map(int,input().split()))[::-1])
x.sort(reverse=1)
for i,j in x:
  for k in range(j):
    if i<=a[0]:s=1;print(sum(a));exit()
    a.popleft();a.append(i)

Fixed code:
from collections import deque
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
a=deque(a)
x=[]
for i in range(m):x.append(list(map(int,input().split()))[::-1])
x.sort(reverse=1)
for i,j in x:
  for k in range(j):
    if i<=a[0]:print(sum(a));exit()
    a.popleft();a.append(i)
print(sum(a))
Buggy code:
n,m = map(int,input().split())
a_list = list(map(int,input().split()))
a_list.sort()
b_c_list = []
for i in range(m):
  b_c_list.append(list(map(int,input().split())))

b_c_list.sort(key=lambda x: x[1],reverse = True)
                  
count = 0
d = []
for b_c in b_c_list:
  for i in range(b_c[0]):
    d.append(b_c[1])
    count += 1
  if count == n:
    break
    

for i in range(n):
  if a_list[i] < d[i]:
    a_list[i] = d[i]
  else:
    break
    
print(sum(a_list))

Fixed code:
n,m = map(int,input().split())
a_list = list(map(int,input().split()))
a_list.sort()
b_c_list = []
for i in range(m):
  b_c_list.append(list(map(int,input().split())))

b_c_list.sort(key=lambda x: x[1],reverse = True)
                  
count = 0
d = []
for b_c in b_c_list:
  for i in range(b_c[0]):
    d.append(b_c[1])
    count += 1
    if count >= n:
      break
  if count >= n:
    break
    

for i in range(min(n,len(d))):
  if a_list[i] < d[i]:
    a_list[i] = d[i]
  else:
    break
    
print(sum(a_list))
Buggy code:
from heapq import heappop,heappush
n,m=map(int,input().split())
a=[int(i) for i in input().split()]
a.sort()
bc=[[int(i) for i in input().split()]for j in range(m)]
bc=sorted(bc, key=lambda x:x[1],reverse=True)

ans=[]
for b,c in bc:    
    while b>0 and a: 
        x = heappop(a)
        if x<c:
            ans.append(c)
            b-=1
        else:
            ans.append(x)
print(sum(ans))

Fixed code:
from heapq import heappop,heappush
n,m=map(int,input().split())
a=[int(i) for i in input().split()]
a.sort()
bc=[[int(i) for i in input().split()]for j in range(m)]
bc=sorted(bc, key=lambda x:x[1],reverse=True)

ans=[]
for b,c in bc:    
    while b>0 and a: 
        x = heappop(a)
        if x<c:
            ans.append(c)
            b-=1
        else:
            ans.append(x)
print(sum(ans)+sum(a))
Buggy code:
import bisect

N,M = map(int,input().split())
A = sorted(list(map(int,input().split())))
BC = []
for i in range(M):
  B,C = map(int,input().split())
  BC.append([B,C])
BC.sort(key = lambda val: val[1], reverse = True)
#print(A)
#print(BC)

ans = 0
j = 0
count = BC[j][0]
for i in range(N):
  #print(ans)
  if A[i] < BC[j][1]:
    ans += BC[j][1]
    count -= 1
    if count == 0:
      j += 1
      count = BC[j][0]
      if j >= M:
        break
  else:
    break
if i < N:
  ans += sum(A[i:])
print(ans)


Fixed code:
import bisect

N,M = map(int,input().split())
A = sorted(list(map(int,input().split())))
BC = []
for i in range(M):
  B,C = map(int,input().split())
  BC.append([B,C])
BC.sort(key = lambda val: val[1], reverse = True)
#print(A)
#print(BC)

ans = 0
j = 0
count = BC[j][0]
for i in range(N):
  #print(ans)
  if A[i] < BC[j][1]:
    ans += BC[j][1]
    count -= 1
    if count == 0:
      j += 1
      if j >= M:
        if i < N-1:
          ans += sum(A[i+1:])
        break
      count = BC[j][0]
  else:
    ans += sum(A[i:])
    break
print(ans)
Buggy code:
N,M=map(int,input().split())
A=list(map(int,input().split()))
BC=[list(map(int,input().split())) for _ in range(M)]

dic=dict({})
for i in range(N):
    if A[i] not in dic.keys():
        dic[A[i]]=1
    else:
        dic[A[i]]+=1

for i in range(M):
    if BC[i][1] not in dic.keys():
        dic[BC[i][1]]=BC[i][0]
    else:
        dic[BC[i][1]]+=BC[i][0]

dic = sorted(dic.items(), reverse=True)
print(dic)
cnt=0
ans=0
for i in range(len(dic)):
    if cnt>=N:
        break
    else:
        if cnt+dic[i][1]<=N:
            cnt+=dic[i][1]
            ans+=dic[i][0]*dic[i][1]
        else:
            ans+=dic[i][0]*(N-cnt)
            break
print(ans)

Fixed code:
N,M=map(int,input().split())
A=list(map(int,input().split()))
BC=[list(map(int,input().split())) for _ in range(M)]

dic=dict({})
for i in range(N):
    if A[i] not in dic.keys():
        dic[A[i]]=1
    else:
        dic[A[i]]+=1

for i in range(M):
    if BC[i][1] not in dic.keys():
        dic[BC[i][1]]=BC[i][0]
    else:
        dic[BC[i][1]]+=BC[i][0]

dic = sorted(dic.items(), reverse=True)
cnt=0
ans=0
for i in range(len(dic)):
    if cnt>=N:
        break
    else:
        if cnt+dic[i][1]<=N:
            cnt+=dic[i][1]
            ans+=dic[i][0]*dic[i][1]
        else:
            ans+=dic[i][0]*(N-cnt)
            break
print(ans)
Buggy code:
import bisect
n,m=map(int,input().split())
a=list(map(int,input().split()))
bc=[list(map(int,input().split())) for i in range(m)]
a.sort()
bc.sort(key=lambda x:x[1],reverse=True)
s=0
for i in range(m):
  if a[s]<bc[i][1]:
    t=bisect.bisect_left(a,bc[i][1],s,n)
    for j in range(s,t):
      a[j]=bc[i][1]
    s=t-1
  else:
    break
print(sum(a))

Fixed code:
import bisect
n,m=map(int,input().split())
a=list(map(int,input().split()))
bc=[list(map(int,input().split())) for i in range(m)]
a.sort()
bc.sort(key=lambda x:x[1],reverse=True)
s=0
for i in range(m):
  if a[s]<bc[i][1]:
    t=bisect.bisect_left(a,bc[i][1],s,n)
    for j in range(s,min(t,s+bc[i][0])):
      a[j]=bc[i][1]
    s=min(t,s+bc[i][0])
    if s>=n:
      break
  else:
    break
print(sum(a))
Buggy code:
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
changeit = []
for i in range(0, M):
	changeit.append(list(map(int, input().split())))
changeit.sort(key = lambda v: v[0], reverse = True)
sum = 0
index = 0
flag = False
for i in range(0, M):
	for j in range(0, changeit[i][1]):
		if index == N:
			flag = True
			break
		if changeit[i][0] < A[index]:
			flag = True
			break
		sum += changeit[i][1]
		index += 1
	if flag:
		break
for i in range(index, N):
	sum += A[i]
print(sum)

Fixed code:
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
changeit = []
for i in range(0, M):
	changeit.append(list(map(int, input().split())))
changeit.sort(key = lambda v: v[1], reverse = True)
sum = 0
index = 0
flag = False
for i in range(0, M):
	for j in range(0, changeit[i][0]):
		if index == N:
			flag = True
			break
		if changeit[i][1] < A[index]:
			flag = True
			break
		sum += changeit[i][1]
		index += 1
	if flag:
		break
for i in range(index, N):
	sum += A[i]
print(sum)
Buggy code:
N, M = map(int,input().split())
A = list(map(int,input().split()))
A.sort(reverse=True)
print(A)

BC = [[] for i in range(M)]

for i in range(M):
    BC[i]=list(map(int,input().split()))

BC = sorted(BC, key=lambda x: x[1], reverse=True)
maxC = BC[0][1]

count = 0
result = 0
while count < N:
    if len(BC) >0:
        if A[0] < BC[0][1]:
            for i in range(BC[0][0]):
                if count < N:
                    result += BC[0][1]
                    count+=1
                else:
                    break
            else:
                BC.pop(0)
        else:
            result+=A.pop(0)
            count+=1
    else:
        result+=A.pop(0)
        count+=1

print(result)   




Fixed code:
N, M = map(int,input().split())
A = list(map(int,input().split()))
A.sort(reverse=True)

BC = [[] for i in range(M)]

for i in range(M):
    BC[i]=list(map(int,input().split()))

BC = sorted(BC, key=lambda x: x[1], reverse=True)
maxC = BC[0][1]

count = 0
result = 0
while count < N:
    if len(BC) >0:
        if A[0] < BC[0][1]:
            for i in range(BC[0][0]):
                if count < N:
                    result += BC[0][1]
                    count+=1
                else:
                    break
            else:
                BC.pop(0)
        else:
            result+=A.pop(0)
            count+=1
    else:
        result+=A.pop(0)
        count+=1

print(result)
Buggy code:
n,m = map(int,input().split())
a = list(map(int,input().split()))
bc = [list(map(int,input().split()))for i in range(m)]
p =[]
d = []
a.sort()
for i in range(m):
    b = bc[i][0]
    c = bc[i][1]
    p.append([c,b])
p.sort(reverse = True)
look = 0
for i in range(n):
    if look > m-1:break
    if p[look][1] >0:
        d.append(p[look][0])
        p[look][1]-=1
    else:
        look+=1
        d.append(p[look][0])
        p[look][1]-=1
sm = sum(a)
updatefrom = 0
updateto = 0
ans = 0
for x in range(n):
    updatefrom +=a[x]
    updateto +=d[x]
    ans = max(ans,sm-updatefrom +updateto)
print(ans)
    

    
    

Fixed code:
n,m = map(int,input().split())
a = list(map(int,input().split()))
bc = [list(map(int,input().split()))for i in range(m)]
p =[]
d = []
a.sort()
for i in range(m):
    b = bc[i][0]
    c = bc[i][1]
    p.append([c,b])
p.sort(reverse = True)
look = 0
for i in range(n):
    if look > m-1:break
    if p[look][1] >0:
        d.append(p[look][0])
        p[look][1]-=1
    
    else:
        if look ==m-1:break
        look+=1
        d.append(p[look][0])
        p[look][1]-=1
sm = sum(a)
updatefrom = 0
updateto = 0
ans = sm
for x in range(n):
    if x >= len(d):break
    updatefrom +=a[x]
    updateto +=d[x]
    ans = max(ans,sm-updatefrom +updateto)
print(ans)
Buggy code:
N,M  =  list(map(int, input().split()))
A =  list(map(int, input().split()))
BC = [ list(map(int, input().split())) for _ in range(M)]

A.sort()
BC.sort(key = lambda x:-x[1])

i = 0 
flag = True
for b,c in BC:
    if not flag:
        break
    for _ in range(b):
        if i >=N:
            flag = False
         if A[i] < c:
            A[i] = c
            i+=1 
        else:
            flag = False
            break
print(sum(A))


Fixed code:
N,M  =  list(map(int, input().split()))
A =  list(map(int, input().split()))
BC = [ list(map(int, input().split())) for _ in range(M)]

A.sort()
BC.sort(key = lambda x:-x[1])

i = 0 
flag = True
for b,c in BC:
    if not flag:
        break
    for _ in range(b):
        if i >=N:
            flag = False
            break
        if A[i] < c:
            A[i] = c
            i+=1 
        else:
            flag = False
            break
print(sum(A))
Buggy code:
import sys
from itertools import zip_longest
input = sys.stdin.readline
n,m = map(int, input().split(" "))
a = list(map(int, input().split(" ")))
a.sort()
bc=[None]*m
for i in range(m):
    bc[i] = list(map(int, input().split(" ")))
bc.sort(reverse=True, key=lambda x:x[1])
l=[]
for [b,c] in bc:
    l.extend([c]*b)
    if n < len(l):
        break
print(sum(list(map(lambda x: x[1] if x[0] < x[1] else x[0], zip_longest(a,l,fillvalue=0)))))


Fixed code:
import sys
from itertools import zip_longest
input = sys.stdin.readline
n,m = map(int, input().split(" "))
a = list(map(int, input().split(" ")))
a.sort()
bc=[None]*m
for i in range(m):
    bc[i] = list(map(int, input().split(" ")))
bc.sort(reverse=True, key=lambda x:x[1])
l=[]
for [b,c] in bc:
    l.extend([c]*b)
    if n < len(l):
        l = l[0:n]
        break
print(sum(list(map(lambda x: x[1] if x[0] < x[1] else x[0], zip_longest(a,l,fillvalue=0)))))
Buggy code:
import sys
input = sys.stdin.readline
n,m = map(int, input().split(" "))
a = list(map(int, input().split(" ")))
a.sort()
bc=[None]*m
for i in range(m):
    bc[i] = list(map(int, input().split(" ")))
bc.sort(reverse=True, key=lambda x:x[1])
l=[]
for [b,c] in bc:
    l.extend([c]*b)
    if n < len(l):
        break
print(l)

Fixed code:
import sys
input = sys.stdin.readline
n,m = map(int, input().split(" "))
a = list(map(int, input().split(" ")))
a.sort()
bc=[None]*m
for i in range(m):
    bc[i] = list(map(int, input().split(" ")))
bc.sort(reverse=True, key=lambda x:x[1])
l=[]
for [b,c] in bc:
    l.extend([c]*b)
    if n < len(l):
        break
for i in range(n):
    if len(l) <= i or l[i] <= a[i]:
        break
    a[i] = l[i]
print(sum(a))
Buggy code:
import numpy as np

n, m  = map(int, input().split())
nums  = list(map(int, input().split()))
length = len(nums)

arr = np.array([list(map(int, input().split())) for i in range(m)])
mask = arr.T[1].argsort()[::-1]
arr = arr[mask]

cnt = 0
for b, c in arr:
    nums.extend([int(c)]*b)
    if cnt > length:
        break

nums = list(nums)
nums.sort(reverse=True)

sum(nums[:length])

Fixed code:
import numpy as np

n, m  = map(int, input().split())
nums  = list(map(int, input().split()))
length = len(nums)

arr = np.array([list(map(int, input().split())) for i in range(m)])
mask = arr.T[1].argsort()[::-1]
arr = arr[mask]

cnt = 0
for b, c in arr:
    nums.extend([int(c)]*b)
    cnt += b
    if cnt > length:
        break

nums = list(nums)
nums.sort(reverse=True)

print(sum(nums[:length]))
Buggy code:
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
print(A)
X = [0 for i in range(N)]
CB = []
for i in range(M):
    b,c = map(int,input().split())
    CB.append([c,b])
CB.sort(reverse=True)
print(CB)
index = 0
for i in range(M):
    for j in range(CB[i][1]):
        A[index] = max(A[index],CB[i][0])
        if index != N - 1:
            index += 1
            continue
        else:
            print(sum(A))
            exit()
print(sum(A))

Fixed code:
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
#print(A)
X = [0 for i in range(N)]
CB = []
for i in range(M):
    b,c = map(int,input().split())
    CB.append([c,b])
CB.sort(reverse=True)
#print(CB)
index = 0
for i in range(M):
    for j in range(CB[i][1]):
        A[index] = max(A[index],CB[i][0])
        if index != N - 1:
            index += 1
            continue
        else:
            print(sum(A))
            exit()
print(sum(A))