qyliang/CodeNanoFix huggingface dataset:
Buggy code:
N,M=[int(s) for s in input().split()]
dic={}
ls=[int(s) for s in input().split()]
for i in range(N):
  if(ls[i] in dic):
    dic[ls[i]]+=1
  else:
    dic[ls[i]]=1
for i in range(M):
  a,b=[int(s) for s in input().split()]
  if(b in dic):
    dic[b]+=a
  else:
    dic[b]=a
x=list(dic)
x=sorted(x, key=lambda e : -e)
count=0
ans=0
for i in x:
  count+=dic[i]
  ans+=i*dic[i]
  print(count,ans)
  if count>=N:
    ans-=i*(count-N)
    break
print(ans)

Fixed code:
N,M=[int(s) for s in input().split()]
dic={}
ls=[int(s) for s in input().split()]
for i in range(N):
  if(ls[i] in dic):
    dic[ls[i]]+=1
  else:
    dic[ls[i]]=1
for i in range(M):
  a,b=[int(s) for s in input().split()]
  if(b in dic):
    dic[b]+=a
  else:
    dic[b]=a
x=list(dic)
x=sorted(x, key=lambda e : -e)
count=0
ans=0
for i in x:
  count+=dic[i]
  ans+=i*dic[i]
  if count>=N:
    ans-=i*(count-N)
    break
print(ans)
Buggy code:
N,M=map(int,input().split())

A=list(map(int,input().split()))
A.sort()
dc=[]

for i in range(M):
    B,C=map(int,input().split())
    dc.append([B,C])

dc.sort(key=lambda x: x[1],reverse=True)
bf=False
i=0

for l in dc:
    B,C=l
    if bf or not i<N:
        break
    while B>0:
        if A[i]<C:
            A[i]=C
            B-=1
        else:
            bf=True
            break

print(sum(A))


Fixed code:
N,M=map(int,input().split())

A=list(map(int,input().split()))
A.sort()
dc=[]

for i in range(M):
    B,C=map(int,input().split())
    dc.append([B,C])

dc.sort(key=lambda x: x[1],reverse=True)
bf=False
i=0
for l in dc:
    B,C=l
    if bf:
        break
    while B>0:
        if i<N and A[i]<C:
            A[i]=C
            B-=1
            i+=1
        else:
            bf=True
            break


print(sum(A))
Buggy code:
N,M=map(int,input().split())
A=[int(x) for x in input().split()]
CB=[]
for i in range(M):
  B,C = map(int,input().split())
  CB +=[[C,B]]

for j in range(N):
  CB +=[[A[j],1]]

CB=sorted(CB,reverse = True)
count = 0
result =0
print(CB)
i=0
while count<N:
  count +=CB[i][1]
  result +=CB[i][0]*CB[i][1]
  i +=1

result -= (count-N)*(CB[i-1][0])
print(result)

Fixed code:
N,M=map(int,input().split())
A=[int(x) for x in input().split()]
CB=[]
for i in range(M):
  B,C = map(int,input().split())
  CB +=[[C,B]]

for j in range(N):
  CB +=[[A[j],1]]

CB=sorted(CB,reverse = True)
count = 0
result =0

i=0
while count<N:
  count +=CB[i][1]
  result +=CB[i][0]*CB[i][1]
  i +=1

result -= (count-N)*(CB[i-1][0])
print(result)
Buggy code:
# AtCoder
from collections import Counter
N, M = map(int, input().split())
A = Counter(list(map(int, input().split())))

for i in range(M):
    b, c = map(int, input().split())
    A[c] += b

A = sorted(A.items(), reverse=True, key=lambda x: x[0])

ans = 0
for n, c in A:
    if N > c:
        ans += n*c
        N -= c
    else:
        ans += n*c
        print(ans)
        exit()


Fixed code:
# AtCoder
from collections import Counter
N, M = map(int, input().split())
A = Counter(list(map(int, input().split())))

for i in range(M):
    b, c = map(int, input().split())
    A[c] += b

A = sorted(A.items(), reverse=True, key=lambda x: x[0])

ans = 0
for n, c in A:
    if N > c:
        ans += n*c
        N -= c
    else:
        ans += n*N
        print(ans)
        exit()
Buggy code:
n, m = map(int, input().split())
a = map(int, input().split())

abc = Counter(a).most_common()
for i in range(m):
    b, c = map(int, input().split())
    abc.append((c, b))

abc.sort(reverse=True)

ans = 0
count = n
for c, b in abc:
    rep = min(count, b)
    ans += c * rep
    count -= rep
print(ans)

Fixed code:
from collections import Counter

n, m = map(int, input().split())
a = map(int, input().split())

abc = Counter(a).most_common()
for i in range(m):
    b, c = map(int, input().split())
    abc.append((c, b))

abc.sort(reverse=True)

ans = 0
count = n
for c, b in abc:
    rep = min(count, b)
    ans += c * rep
    count -= rep
print(ans)
Buggy code:
N, M = [int(s) for s in input().split(' ')]
As = [int(s) for s in input().split(' ')]
CBs = []
for m in range(M):
    B, C = [int(s) for s in input().split(' ')]
    CBs.append((C, B))
As.sort(reverse=True)
CBs.sort(reverse=True)
ret = 0
a = 0
bc = 0
n = 0
while n < N:
    C, B = CBs[bc]
    if As[a] < C:
        bc += 1
        pick = min(N - n, B)
        ret += pick * C
        n += pick
    else:
        ret += As[a]
        a += 1
        n += 1
print(ret)




Fixed code:
N, M = [int(s) for s in input().split(' ')]
As = [int(s) for s in input().split(' ')]
CBs = []
for m in range(M):
    B, C = [int(s) for s in input().split(' ')]
    CBs.append((C, B))
As.sort(reverse=True)
CBs.sort(reverse=True)
CBs.append((0, 0))
ret = 0
a = 0
bc = 0
n = 0
while n < N:
    C, B = CBs[bc]
    if As[a] < C:
        bc += 1
        pick = min(N - n, B)
        ret += pick * C
        n += pick
    else:
        ret += As[a]
        a += 1
        n += 1
print(ret)
Buggy code:
n, m = map(int, input().split())
A = sorted(list(map(int, input().split())))

bc = []
for i in range(2):
    b, c = map(int, input().split())
    bc.append([b,c])

from operator import itemgetter
bc.sort(key=itemgetter(1))
k = 0
c = bc[0][1]
b = bc[0][1]
bn = 0
while A[k] < c:
    A[k]=c
    k+=1
    if k >= len(A):
        break
    b-=1
    if b  == 0:
        bn += 1
        if bn > len(bc):
            break
        b = bc[bn]
print(sum(A))

Fixed code:
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])

from operator import itemgetter
bc.sort(key=itemgetter(1), reverse = True)
k = 0
c = bc[0][1]
b = bc[0][0]
bn = 0
while A[k] < c:
    A[k]=c
    k+=1
    if k == len(A):
        break
    b-=1
    if b  == 0:
        bn += 1
        if bn == len(bc):
            break
        b = bc[bn][0]
        c = bc[bn][1]
print(sum(A))
Buggy code:

def main():
    N, M = [int(i) for i in input().split()]
    A = [int(i) for i in input().split()]

    A.sort()


    bc = [[int(i) for i in input().split()] for _ in range(M)]
    bc.sort(key=lambda x: x[1])


    ai = N - 1
    mi = M - 1
    pos = 0
    ans = 0
    for _ in range(N):
        if (A[ai] > bc[mi][1]) | (mi < 0):
            pos += 1
            ans += A[ai]
            ai -= 1
        elif pos + bc[mi][0] >= N:
            ans += bc[mi][1] * (N - pos)
            break
        else:
            ans += bc[mi][0] * bc[mi][1]
            pos += bc[mi][0]
            mi -= 1
        if pos >= N:
            break

    print(ans)

Fixed code:
def main():
    N, M = [int(i) for i in input().split()]
    A = [int(i) for i in input().split()]

    A.sort()


    bc = [[int(i) for i in input().split()] for _ in range(M)]
    bc.sort(key=lambda x: x[1])


    ai = N - 1
    mi = M - 1
    pos = 0
    ans = 0
    for _ in range(N):
        if (A[ai] > bc[mi][1]) | (mi < 0):
            pos += 1
            ans += A[ai]
            ai -= 1
        elif pos + bc[mi][0] >= N:
            ans += bc[mi][1] * (N - pos)
            break
        else:
            ans += bc[mi][0] * bc[mi][1]
            pos += bc[mi][0]
            mi -= 1
        if pos >= N:
            break

    print(ans)



if __name__ == "__main__":
    main()
Buggy code:
N,M=map(int,input().split())
arr=[int(x) for x in input().split()]
s=[list(map(int,input().split())) for y in range(M)]
s(key=lambda z: z[1], reverse=True)

sum_a=0
for a,b in s:
    arr+=[b]*a
    sum_a+=a
    if sum_a>=N:
        break

lst=sorted(arr)
ans=sum(lst[-N:])
print(ans)

Fixed code:
N,M=map(int,input().split())
arr=[int(x) for x in input().split()]
s=[list(map(int,input().split())) for y in range(M)]
s.sort(key=lambda z: z[1], reverse=True)

sum_a=0
for a,b in s:
    arr+=[b]*a
    sum_a+=a
    if sum_a>=N:
        break

lst=sorted(arr)
ans=sum(lst[-N:])
print(ans)
Buggy code:
N, M = map(int,input().split())
array = sorted(list(map(int,input().split())))
convert = [list(map(int,input().split())) for _ in range(M)]
convert.sort(key=lambda x: x[1])
convert.reverse()
i = 0
for j in range(M):
  #array.sort()
  print("j=",j)
  start = 0
  while (array[i] < convert[j][1]) & (start < convert[j][0]):
    array[i] = convert[j][1]
    print(array)
    i += 1
    start += 1
print(sum(array))



Fixed code:
N, M = map(int,input().split())
array = sorted(list(map(int,input().split())))
convert = [list(map(int,input().split())) for _ in range(M)]
convert.sort(key=lambda x: x[1])
convert.reverse()
i = 0
for j in range(M):
  #array.sort()
  start = 0
  while (array[i] < convert[j][1]) & (start < convert[j][0]):
    array[i] = convert[j][1]
    #print(array)
    i += 1
    start += 1
    if i == N:
      break
  if i == N:
    break

print(sum(array))
Buggy code:
import heapq
n,m=map(int,input().split())
a=[int(i) for i in input().split()]
d={}
l,q=[],[]
for i in a:
    if i in d: d[i]+=1
    else: d[i]=1

for i in d: heapq.heappush(q, i)

for i in range(m):
    b,c=map(int,input().split())
    l.append((c,b))
l.sort(reverse=True)
for c,b in l:
    if c<=q[0]: break
    while b:
        x=min(b,d[q[0]])
        if c in d:
            d[c]+=x
        else:
            d[c]=x
        b-=x
        d[q[0]]-=x
        if d[q[0]]==0:
            heapq.heappop(q)
ans=0
for i in d:
    ans+=i*d[i]
print(ans)

Fixed code:
import heapq
n,m=map(int,input().split())
a=[int(i) for i in input().split()]
d={}
l,q=[],[]
for i in a:
    if i in d: d[i]+=1
    else: d[i]=1

for i in d: heapq.heappush(q, i)

for i in range(m):
    b,c=map(int,input().split())
    l.append((c,b))
l.sort(reverse=True)

for c,b in l:
    while b:
        if len(q)==0 or c<=q[0]: break
        x=min(b,d[q[0]])
        if c in d:
            d[c]+=x
        else:
            d[c]=x
        b-=x
        d[q[0]]-=x
        if d[q[0]]==0:
            heapq.heappop(q)
ans=0
for i in d:
    ans+=i*d[i]
print(ans)
Buggy code:
n,m=map(int,input().split())
a=[int(i) for i in input().split()]
d={}
w=[]
q=[]
for i in a:
    if i in d:
        d[i]+=1
    else:
        d[i]=1
        w.append(i)
for i in w: heapq.heappush(q, i)
for i in range(m):
    b,c=map(int,input().split())
    while b:
        if c>q[0]:
            x=min(b,d[q[0]])
            if c in d:
                d[c]+=x
                b-=x
                d[q[0]]-=x
                if d[q[0]]==0:
                    heapq.heappop(q)
            else:
                d[c]=x
                b-=x
                d[q[0]]-=x
                heapq.heappush(q,c)
                if d[q[0]]==0:
                    heapq.heappop(q)
        else:
            break
ans=chk=0
for i in d:
    ans+=i*d[i]
print(ans)

Fixed code:
import heapq
n,m=map(int,input().split())
a=[int(i) for i in input().split()]
d={}
w=[]
q=[]
for i in a:
    if i in d:
        d[i]+=1
    else:
        d[i]=1
        w.append(i)
for i in w: heapq.heappush(q, i)
for i in range(m):
    b,c=map(int,input().split())
    while b:
        if c>q[0]:
            x=min(b,d[q[0]])
            if c in d:
                d[c]+=x
                b-=x
                d[q[0]]-=x
                if d[q[0]]==0:
                    heapq.heappop(q)
            else:
                d[c]=x
                b-=x
                d[q[0]]-=x
                heapq.heappush(q,c)
                if d[q[0]]==0:
                    heapq.heappop(q)
        else:
            break
ans=chk=0
for i in d:
    ans+=i*d[i]
print(ans)
Buggy code:
import bisect
n,m =map(int,input().split())
a = list(map(int,input().split()))
b = []
c = []
a = sorted(a)
dic = []
for i in range(m):
    ba,ca = map(int,input().split())
    b.append(ba)
    c.append(ca)
    dic.append([ba,ca])


dic = sorted(dic, key=lambda x:x[0], reverse=True)
#print(dic)

length = 0
lo = []
s = 1
for i in range(len(dic)):
    for k in range(dic[i][0]):
        lo.append(dic[i][1])
        length += 1
        if length > len(a):
            s = 0
            break
    if s == 0:
        break
for i in range(len(a)):

    if i < len(lo) and a[i] < lo[i]:
        a[i] = lo[i]
#print(a)
ans = 0
for i in range(len(a)):
    ans += a[i]
print(ans)


Fixed code:
import bisect
n,m =map(int,input().split())
a = list(map(int,input().split()))
b = []
c = []
a = sorted(a)
dic = []
for i in range(m):
    ba,ca = map(int,input().split())
    b.append(ba)
    c.append(ca)
    dic.append([ba,ca])


dic = sorted(dic, key=lambda x:x[1], reverse=True)
#print(dic)

length = 0
lo = []
s = 1
for i in range(len(dic)):
    for k in range(dic[i][0]):
        lo.append(dic[i][1])
        length += 1
        if length > len(a):
            s = 0
            break
    if s == 0:
        break
for i in range(len(a)):

    if i < len(lo) and a[i] < lo[i]:
        a[i] = lo[i]
#rint(a)
ans = 0
for i in range(len(a)):
    ans += a[i]
print(ans)
Buggy code:
n, m = (int(i) for i in 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], reverse=True)

print(a)
print(bc)

index = 0
break_flag = False

for i in range(m):
    for j in range(bc[i][0]):
        if(a[index] < bc[i][1] and index < n):
            a[index] = bc[i][1]
            index += 1
            print(a)
        else:
            break_flag = True
            break
    if  break_flag:
        break
print(sum(a))

Fixed code:
n, m = (int(i) for i in 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], reverse=True)

# print(a)
# print(bc)

index = 0
break_flag = False

for i in range(m):
    for j in range(bc[i][0]):
        if (index < n):
            if(a[index] < bc[i][1]):
                a[index] = bc[i][1]
                index += 1
                # print(a)
            else:
                break_flag = True
                break
        else:
            break_flag = True
            break
    if  break_flag:
        break
print(sum(a))
Buggy code:
N,M = map(int , input().split())
data = sorted(list(map(int , input().split())))
A=[list(map(int , input().split())) for _ in range(M)]
change_list = sorted(A, key=lambda x:x[1])
cnt_total = 0

for i,x in reversed(change_list):
	for j in range(i):
		if data[cnt_total] <=x:
			data[cnt_total] =x
		else:
			print(sum(data))
			exit()

		cnt_total += 1
		if cnt_total == N:
			print(sum(data))
			exit()

Fixed code:
N,M = map(int , input().split())
data = sorted(list(map(int , input().split())))
A=[list(map(int , input().split())) for _ in range(M)]
change_list = sorted(A, key=lambda x:x[1])
cnt_total = 0

for i,x in reversed(change_list):
	for j in range(i):
		if data[cnt_total] <=x:
			data[cnt_total] =x
		else:
			print(sum(data))
			exit()

		cnt_total += 1
		if cnt_total == N:
			print(sum(data))
			exit()
			
print(sum(data))
Buggy code:
N, M = map(int, input().split())
lst_A = list(map(int, input().split()))
lst_BC = [tuple(map(int, input().split())) for _ in range(M)]


lst_A.sort()
lst_BC.sort(key=lambda x:x[1], reverse=True)
print(lst_A)
print(lst_BC)


lst_D = []
add_num = N
for tpl in lst_BC:
    b, c = tpl[0], tpl[1]
    if add_num >= b:
        lst_D = lst_D + [c] * b
        add_num -= b
    else:
        lst_D = lst_D + [c] * add_num
lst_D = lst_D + [0] * (N - len(lst_D))
print(lst_D)


for i in range(N):
    if lst_A[i] >= lst_D[i]:
        break
    lst_A[i] = lst_D[i]
print(lst_A)


print(sum(lst_A))

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


lst_A.sort()
lst_BC.sort(key=lambda x:x[1], reverse=True)


lst_D = []
for b, c in lst_BC:
    lst_D += [c] * min(b, N - len(lst_D))
lst_D = lst_D + [0] * (N - len(lst_D))


for i in range(N):
    if lst_A[i] >= lst_D[i]:
        break
    lst_A[i] = lst_D[i]


print(sum(lst_A))
Buggy code:
n,m= map(int, input().split())
a = list(map(int, input().split()))
l=[]
for _ in range(m):
    b=list(map(int, input().split()))
    l.append(b)
l.sort(key = lambda x: x[1],reverse=True) 
print(l)
for k in l:
    if(len(a)<=2*n):
        for j in range(k[0]):
            a.extend([k[1]])
a.sort(reverse=True)
print(sum(a[:n]))

Fixed code:
n,m= map(int, input().split())
a = list(map(int, input().split()))
l=[]
for _ in range(m):
    b=list(map(int, input().split()))
    l.append(b)
l.sort(key = lambda x: x[1],reverse=True) 
for k in l:
    if(len(a)<=2*n):
        for j in range(k[0]):
            a.extend([k[1]])
a.sort(reverse=True)
print(sum(a[:n]))
Buggy code:
import heapq
N, M = map(int, input().split())
h = []
for i in input().split():
    heapq.heappush(h, int(i))
data = [list(map(int, input().split())) for _ in range(M)]
data = sorted(data, reverse=1, key=lambda x: x[1])
for b, c in data:
    for j in range(b):
        a = heapq.heappushpop(h, c)
        if a == c:
            print(sum(h))
            exit()


Fixed code:
import heapq
N, M = map(int, input().split())
h = []
for i in input().split():
    heapq.heappush(h, int(i))
data = [list(map(int, input().split())) for _ in range(M)]
data = sorted(data, reverse=1, key=lambda x: x[1])
for b, c in data:
    for j in range(b):
        a = heapq.heappushpop(h, c)
        if a == c:
            print(sum(h))
            exit()
print(sum(h))
Buggy code:
N,M = map(int,input().split())
A = sorted(list(map(int,input().split())))
BC = [list(map(int,input().split())) for _ in range(M)]
BC = sorted(BC, key=lambda x:x[1], reverse=True)
D = []
for b,c in BC:
    D += [c]*b
# print(D)

for i in range(N):
    if A[i] < D[i]:
        A[i] = D[i]
    else:
        break
# print(sorted(A))
print(sum(A))

Fixed code:
N,M = map(int,input().split())
A = sorted(list(map(int,input().split())))
BC = [list(map(int,input().split())) for _ in range(M)]
BC = sorted(BC, key=lambda x:x[1], reverse=True)
D = [0] * N
idx = 0
for b,c in BC:
    for i in range(b):
        D[idx] = c
        idx += 1
        if idx >= N:
            break
    if idx >= N:
        break
# print(D)


# print(D)
# print(sorted(A))
# print(len(D), len(A))
for i in range(N):
    if A[i] < D[i]:
        A[i] = D[i]
    else:
        break
print(sum(A))
Buggy code:
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
b=[]
s=0
for i in range(m):
    p=list(map(int,input().split()))
    b.append(p)
    s+=p[0]
b.sort(key=lambda x:-x[1])
a.sort()
if s>n:
    s=n
c=[]
i=0
sun=0
while sun>=s:
    for j in range(b[i][0]):
        c.append(b[i][1])
    sun+=b[i][0]
    i+=1
c.append(1)
print(c)
for u in range(s):
    if a[u]<c[u]:
        a[u]=c[u]
    
print(sum(a))

Fixed code:
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
b=[]
s=0
for i in range(m):
    p=list(map(int,input().split()))
    b.append(p)
    s+=p[0]
b.sort(key=lambda x:-x[1])
a.sort()
if s>n:
    s=n
c=[]
i=0
sun=0
while sun<s:
    for j in range(b[i][0]):
        c.append(b[i][1])
    sun+=b[i][0]
    i+=1
for u in range(s):
    if a[u]<c[u]:
        a[u]=c[u]
    
print(sum(a))
Buggy code:
from operator import itemgetter

n,m = map(int,input().split())
a = [int(i) for i in input().split()]
w = []
x = 0
for i in range(m):
    b,c = map(int,input().split())
    w.append((b,c))
w = tuple(sorted(w,key=itemgetter(1)))
w.reverse()
for i in w:
    a += [i[1]] * i[0]
    x += b
    if x > n:
        break
a.sort()
a = a[len(a)-n:]
print(sum(a))


Fixed code:
from operator import itemgetter

n,m = map(int,input().split())
a = [int(i) for i in input().split()]
w = []
x = 0
for i in range(m):
    b,c = map(int,input().split())
    w.append((b,c))
w = list(sorted(w,key=itemgetter(1)))
w.reverse()
for i in w:
    a += [i[1]] * i[0]
    x += b
    if x > n:
        break
a.sort()
a = a[len(a)-n:]
print(sum(a))
Buggy code:
import heapq

n, m = list(map(int, input().split()))
cards = list(map(int, input().split()))
cards.sort()

queries = []
for i in range(m):
    q = list(map(int, input().split()))
    queries.append(q)
queries.sort(key=lambda x: x[1])
queries.reverse()

ans = 0
pos = 0
for q in queries:
    b, c = q
    if cards[min(pos+b-1, n-1)] < c:
        # print('a')
        pos = pos + b
        ans += c * b
        if pos >= n:
            break
    else:
        for i in range(b):
            if cards[pos+i, n-1] < c:
                # print('b')
                ans += c
            else:
                # print('c')
                pos += i
                for j in range(pos, n):
                    ans += cards[j]

                pos = n
                break
        break

if pos < n:
    for j in range(pos, n):
        ans += cards[j]

print(ans)



Fixed code:
import heapq

n, m = list(map(int, input().split()))
cards = list(map(int, input().split()))
cards.sort()

queries = []
for i in range(m):
    q = list(map(int, input().split()))
    queries.append(q)
queries.sort(key=lambda x: x[1])
queries.reverse()

ans = 0
pos = 0
for q in queries:
    b, c = q
    if cards[min(pos+b-1, n-1)] < c:
        # print('a')
        pos = pos + b
        ans += c * (b - max(0, pos-n))
        if pos >= n:
            break
    else:
        for i in range(b):
            if cards[pos+i] < c:
                # print('b')
                ans += c
            else:
                # print('c')
                pos += i
                for j in range(pos, n):
                    ans += cards[j]

                pos = n
                break
        break

if pos < n:
    for j in range(pos, n):
        ans += cards[j]

print(ans)
Buggy code:
def resolve():
    n, m = map(int, input().split())
    A = list(map(int, input().split()))
    B=[]
    ans=0
    k=0
    j=0
    for _ in range(m):
        a, b = map(int, input().split())
        B.append([b,a])
    for i in A:
        B.append([i,1])
    B.sort(reverse=True)
    f=B[0][1]
    while f<n:
        k+=B[j][1]
        f+=B[j+1][1]
        ans+=B[j][0]*B[j][1]
        j+=1
    ans+=B[j][0]*(n-k)

    print(ans)
resolve():

Fixed code:
def resolve():
    n, m = map(int, input().split())
    A = list(map(int, input().split()))
    B=[]
    ans=0
    k=0
    j=0
    for _ in range(m):
        a, b = map(int, input().split())
        B.append([b,a])
    for i in A:
        B.append([i,1])
    B.sort(reverse=True)
    f=B[0][1]
    while f<n:
        k+=B[j][1]
        f+=B[j+1][1]
        ans+=B[j][0]*B[j][1]
        j+=1
    ans+=B[j][0]*(n-k)

    print(ans)
resolve()
Buggy code:
N, M = map(int,input().split())
A = list(map(int,input().split()))
B = [list(map(int,input().split())) for i in range(M)]

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

cot = 0
add = 0

for b,c in B:
    cot += b
    if cot > N:
        add = N - cot + b
        A.extend([c] * add)
        break
    else:
        A.extend([c] * b)

print(sum(sorted(A, reverse = True))[:N])

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

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

cot = 0
add = 0

for b,c in B:
    cot += b
    if cot > N:
        add = N - cot + b
        A.extend([c] * add)
        break
    else:
        A.extend([c] * b)

print(sum(sorted(A, reverse = True)[:N]))