qyliang/CodeNanoFix huggingface dataset:
Buggy code:
N,M=map(int,input().split())
S=list(map(int,input().split()))
S.sort()

TRADE=[[0 for j in range(2)] for i in range(M)]

for i in range(M):
    B,C=map(int,input().split())
    TRADE[i][0]=B
    TRADE[i][1]=C


T=sorted(TRADE,key=lambda x:x[1],reverse=True)


TLEN=0
pos=0
flag=True
while TLEN<M and flag=True:
    for i in range(T[TLEN][0]):
        if S[pos]<T[TLEN][1]:
            S[pos]=T[TLEN][1]
            pos+=1
        else:
            flag=False
            break
    TLEN+=1

print(sum(S))

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

TRADE=[[0 for j in range(2)] for i in range(M)]

for i in range(M):
    B,C=map(int,input().split())
    TRADE[i][0]=B
    TRADE[i][1]=C


T=sorted(TRADE,key=lambda x:x[1],reverse=True)


TLEN=0
pos=0
flag=True
while TLEN<M and flag==True:
    for i in range(T[TLEN][0]):
        if S[pos]<T[TLEN][1]:
            S[pos]=T[TLEN][1]
            pos+=1
        else:
            flag=False
            break
        if pos>len(S)-1:
            flag=False
            break
    TLEN+=1

print(sum(S))
Buggy code:
n,m = map(int,input().split())
a = map(int,input().split())
bc =[list(map(int,input().split()))for _ in [0]*m]

a.sort()
bc.sort(key=lamda x:-x[1])
i =0
for b,c in bc:
    for j in range(i,min(n,i+b)):
        if a[j]< c:a[j]=c
    i = i+b
print(sum(a))

Fixed code:
n,m = map(int,input().split())
*a, = map(int,input().split())
bc =[list(map(int,input().split()))for _ in [0]*m]
 
a.sort()
bc.sort(key=lambda x:-x[1])
i =0
for b,c in bc:
    for j in range(i,min(n,i+b)):
        if a[j]< c:a[j]=c
    i = i+b
print(sum(a))
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])
res = 0
idx = 0
while idx < n:
    b, c = a.pop()
    p = min(b, n-t)
    res += p * c
    idx += p
print(res)


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])
res = 0
idx = 0
while idx < n:
    b, c = a.pop()
    p = min(b, n-idx)
    res += p * c
    idx += p
print(res)
Buggy code:
from heapq import heappush,heappop

N,M = list(map(int,input().split()))
A = list(map(int,input().split()))
change = [list(map(int,input().split())) for _ in range(M)]

def tempchange(l,change):
  for i in range(M):
    B,C = change[i][0],change[i][1]
    for j in range(B):
      a = heappop(l)
      dif = C - a
      if dif > 0:
        heappush(l,C)
      else:
        heappush(l,a)
  return sum(l)

#A.sort(reverse = True)
change.sort(key = lambda x:x[1],reverse =True)
print(tempchange(A,change))

Fixed code:
from heapq import heappush,heappop,heapify

N,M = list(map(int,input().split()))
A = list(map(int,input().split()))
change = [list(map(int,input().split())) for _ in range(M)]

def tempchange(l,change):
  heapify(l)  
  for B,C in change:
    for j in range(B):
      a = heappop(l)
      if C - a > 0:
        heappush(l,C)
      else:
        heappush(l,a)
        break
  return sum(l)

#A.sort(reverse = True)
change.sort(key = lambda x:x[1],reverse =True)
print(tempchange(A,change))
Buggy code:
import heapq as hq

def get_num():
    return list(map(int,input().split(' ')))

def main():
    n,m = get_num()
    card = get_num()
    card_sum=sum(card)
    hq.heapify(card)
    d_set=[]
    d=[]
    for i in range(m):
        b,c=get_num()
        hq.heappush(d_set,(-c,b))
    while len(d)<n:
        cnt = hq.heappop(d_set)
        for i in range(cnt[1]):
            d.append(-cnt[0])
            if len(d)==n:
                break
    for i in range(n):
        c_min=hq.heappop(card)
        d_max=d[i]
        if c_min>=d_max:
            break
        else:
            card_sum += (d_max-c_min)
    print(card_sum)
main()

Fixed code:
import heapq as hq

def get_num():
    return list(map(int,input().split(' ')))

def main():
    n,m = get_num()
    card = get_num()
    card_sum=sum(card)
    hq.heapify(card)
    d_set=[]
    d=[]
    for i in range(m):
        b,c=get_num()
        hq.heappush(d_set,(-c,b))
    for i in range(m):
        if len(d) ==n:
            break
        cnt = hq.heappop(d_set)
        for i in range(cnt[1]):
            d.append(-cnt[0])
            if len(d)==n:
                break
    for i in range(len(d)):
        c_min=hq.heappop(card)
        d_max=d[i]
        if c_min>=d_max:
            break
        else:
            card_sum += (d_max-c_min)
    print(card_sum)
main()
Buggy code:
N, M = map(int,input().split())
A = [[1,int(a)] for a in input.split()]
BC = A + [list(map(int,input().split())) for _ in range(M)]

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

i = 0
ans = 0
for B,C in BC:
    if i + B <= N:
        ans += B*C
    else:
        ans += (N - i) * C

print(ans)

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

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

i = 0
ans = 0
for B,C in BC:
    if i + B <= N:
        ans += B*C
        i += B
    else:
        ans += (N - i) * C
        break

print(ans)
Buggy code:
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = {}
result_m = 0
for m in range(M):
  B, C = map(int, input().split())
  if BC.get(C) != None:
    BC[C] += B
  else:
    BC[C] = B
A.sort()
BC = sorted(BC.items(), reverse=True)
count = 0
for m in range(len(BC)):
  if A[count]>BC[m][0]:
    break
  for i in range(BC[m][1]):
    if count=>len(A):
      break
    if BC[m][0] > A[count]:
      A[count] = BC[m][0]
      count+=1
print(sum(A))

Fixed code:
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = {}
result_m = 0
for m in range(M):
  B, C = map(int, input().split())
  if BC.get(C) != None:
    BC[C] += B
  else:
    BC[C] = B
A.sort()
BC = sorted(BC.items(), reverse=True)
count = 0
for m in range(len(BC)):
  if count>=len(A):
    break
  if A[count]>BC[m][0]:
    break
  for i in range(BC[m][1]):
    if count>=len(A):
      break
    if BC[m][0] > A[count]:
      A[count] = BC[m][0]
      count+=1
print(sum(A))
Buggy code:
from itertools import zip_longest

n, m = 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])

stack = []
i = 0
while len(stack) <= n and i < m:
  for _ in range(BC[i][0]):
    stack.append(BC[i][1])
    if len(stack) > n:
      break
  i += 1

ans = 0
for a, s in zip_longest(A, stack, fillvalue=0):
  ans += max(a, s)

print(ans)

Fixed code:
from itertools import zip_longest

n, m = 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])

stack = []
i = 0
while len(stack) < n and i < m:
  for _ in range(BC[i][0]):
    stack.append(BC[i][1])
    if len(stack) >= n:
      break
  i += 1

ans = 0
for a, s in zip_longest(A, stack, fillvalue=0):
  ans += max(a, s)

print(ans)
Buggy code:
import heapq
n, m = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
BC = [list(map(int, input().split())) for _ in range(m)]
BC.sort(key=lambda x: -x[1])

tank = []

idx = 0
while (len(tank) < n + 1) and (idx < m):
  for _ in range(BC[idx][0]): 
    heapq.heappush(tank, -BC[idx][1])
  idx +=  1

# print(A)
# print(BC)
# print(tank)

ans = 0
p = -heapq.heappop(tank)
for a in A:
  if p > a:
    ans += p
    p = -heapq.heappop(tank)
  else:
    ans += a

print(ans)
    
    

Fixed code:
import heapq
n, m = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
BC = [list(map(int, input().split())) for _ in range(m)]
BC.sort(key=lambda x: -x[1])

tank = []

idx = 0
while (len(tank) < n + 1) and (idx < m):
  for _ in range(BC[idx][0]): 
    heapq.heappush(tank, -BC[idx][1])
  idx +=  1

# print(A)
# print(BC)
# print(tank)

ans = 0
p = -heapq.heappop(tank)
for a in A:
  if p > a:
    ans += p
    if tank:
      p = -heapq.heappop(tank)
    else:
      p = - 10 ** 9
  else:
    ans += a

print(ans)
Buggy code:
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
L = set()
for i in range(M):
	b, c = map(int, input().split())
	L.add((c, b))
L = sorted(L, reverse = True)
L.append((0, N))
temp = 0
sum = 0
for i in range(M + 1):
	for j in range(L[i][1]):
		if A[temp] < L[i][0]:
			sum += L[i][0]
			temp += 1
		else:
			sum += A[temp]
			temp += 1
		if temp == N:
			break
	if temp == N:
		break
print(sum)

Fixed code:
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
L = []
for i in range(M):
	b, c = map(int, input().split())
	L.append((c, b))
L = sorted(L, reverse = True)
L.append((0, N))
temp = 0
sum = 0
for i in range(M + 1):
	for j in range(L[i][1]):
		if A[temp] < L[i][0]:
			sum += L[i][0]
			temp += 1
		else:
			sum += A[temp]
			temp += 1
		if temp == N:
			break
	if temp == N:
		break
print(sum)
Buggy code:
N,M = map(int,input().split(' '))
A = list(map(int,input().split(' ')))
l = [list(map(int,input().split())) for i in range(M)]

A.sort()
l = sorted(l,key = lambda x:x[1])

ind =0
for b,c in l[::-1]:
    mx = min(N,ind+b)
    for i in range(ind,mx):
        if c>A[i]:
            A = A[1:].append(c)
    if mx==N:
        break
    ind += b
print(sum(A))

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

A.sort()
l = sorted(l,key = lambda x:x[1])

ind =0
for b,c in l[::-1]:
    mx = min(N,ind+b)
    for i in range(ind,mx):
        if c>A[i]:
            A[i] = c
    if mx==N:
        break
    ind += b
print(sum(A))
Buggy code:
n,m = map(int,input().split())
a = [int(i) for i in input().split()]
bc = [[int(i) for i in input().split()] for i in range(m)]
a.sort()
bc.sort(key=lambda x:x[1],reverse=True)
d = []
cnt1 = 0
cnt2 = 0
x = 0

for lst in bc:
    cnt2 = 0
    while cnt1 < n and cnt2 < lst[0]:
        d.append(lst[1])
        cnt1 += 1
        cnt2 += 1

for i in range(n):
    if d[i] - a[i] < 0:
        x = i
        break

print(sum(d[:x]+a[x:]))

Fixed code:
n,m = map(int,input().split())
a = [int(i) for i in input().split()]
bc = [[int(i) for i in input().split()] for i in range(m)]
a.sort()
bc.sort(key=lambda x:x[1],reverse=True)
d = []
cnt1 = 0
cnt2 = 0
x = 0

for lst in bc:
    cnt2 = 0
    while cnt1 < n and cnt2 < lst[0]:
        d.append(lst[1])
        cnt1 += 1
        cnt2 += 1

for i in range(len(d)):
    if d[i] - a[i] < 0:
        x = i
        break
else:
    x += i+1

print(sum(d[:x]+a[x:]))
Buggy code:
n,m = map(int,input().split())
a = [int(i) for i in input().split()]
bc = [[int(i) for i in input().split()] for i in range(m)]
ans = 0
cnt = 0
d = {}

for i in range(n): 
    d[a[i]] = d.get(a[i],0)+1
for k in d: 
    bc.append([d[k],k])
bc.sort(key= lambda x:x[1],reverse=True)

for i in range(len(bc)):
    if cnt < n:
        ans += bc[i][1]*bc[i][0]
        cnt += bc[i][0]
    else:
        ans -= (cnt-n)*bc[i-1][1]
        break

print(ans)

Fixed code:
n,m = map(int,input().split())
a = [int(i) for i in input().split()]
bc = [[int(i) for i in input().split()] for i in range(m)]
ans = 0
cnt = 0
d = {}

for i in range(n): 
    d[a[i]] = d.get(a[i],0)+1
for k in d: 
    bc.append([d[k],k])
bc.sort(key= lambda x:x[1],reverse=True)

for i in range(len(bc)):
    if cnt < n:
        ans += bc[i][1]*bc[i][0]
        cnt += bc[i][0]
    else:
        ans -= (cnt-n)*bc[i-1][1]
        break
else:
    ans -= (cnt-n)*bc[i][1]

print(ans)
Buggy code:
from heapq import heappush, heappop

n,m = map(int,input().split())
heap = []

for i in map(int,input().split()):
    heappush(heap,i)

bc = [list(map(int, input().split())) for _ in range(m)]
bc.sort(key = lambda x: -x[1])

for b,c in bc:
    for j in range(b):
        a = heappop(heap)
        if a < c:
            heappush(heap,c)
        
        else:
            heappush(heap, a)
            break
        
        print(heap)
print(sum(heap))
    

Fixed code:
from heapq import heappush, heappop

n,m = map(int,input().split())
heap = []

for i in map(int,input().split()):
    heappush(heap,i)

bc = [list(map(int, input().split())) for _ in range(m)]
bc.sort(key = lambda x: -x[1])

for b,c in bc:
    for j in range(b):
        a = heappop(heap)
        if a < c:
            heappush(heap,c)
        
        else:
            heappush(heap, a)
            break
        
print(sum(heap))
Buggy code:
import heapq

N, M = list(map(int,input().split()))
A = list(map(int,input().split()))
B = [list(map(int,input().split())) for i in range(M)]
B = sorted(B, key = lambda x: x[1])

for i in range(N):  A[i] *= -1
heapq.heapify(A)
cnt = 0
for i in range(M):
    for _ in range(B[i][0]):  heapq.heappush(A, -max(-heapq.heappop(A), B[i][1]))
    cnt += B[i][0]
    if cnt >= N:  break
print(-sum(A))

Fixed code:
import heapq

N, M = list(map(int,input().split()))
A = list(map(int,input().split()))
B = [list(map(int,input().split())) for i in range(M)]
B = sorted(B, key = lambda x: x[1])[::-1]

heapq.heapify(A)
cnt = 0
for i in range(M):
    for _ in range(B[i][0]):  heapq.heappush(A, max(heapq.heappop(A), B[i][1]))
    cnt += B[i][0]
    if cnt >= N:  break
print(sum(A))
Buggy code:
def solve():
  N,M = map(int, input().split())
  A = list(map(int, input().split()))
  A.sort()
  ret = sum(A)
  CB = {}
  C_heap = []
  for _ in range(M):
    b,c  = map(int, input().split())
    if c in CB:
      CB[c] += b
    else:
      CB[c] = b
      heapq.heappush(C_heap, -c)

  c = (-1)*heapq.heappop(C_heap)
  for a in A:
    if CB[c] == 0:
      if not C_heap: break

      c = (-1)*heapq.heappop(C_heap)
    
    if c <= a:
      break

    ret += c-a
    CB[c] -= 1

  print(ret)
  
solve()

Fixed code:
import heapq

def solve():
  N,M = map(int, input().split())
  A = list(map(int, input().split()))
  A.sort()
  ret = sum(A)
  CB = {}
  C_heap = []
  for _ in range(M):
    b,c  = map(int, input().split())
    if c in CB:
      CB[c] += b
    else:
      CB[c] = b
      heapq.heappush(C_heap, -c)

  c = (-1)*heapq.heappop(C_heap)
  for a in A:
    if CB[c] == 0:
      if not C_heap: break

      c = (-1)*heapq.heappop(C_heap)
    
    if c <= a:
      break

    ret += c-a
    CB[c] -= 1

  print(ret)
  
solve()
Buggy code:
def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    BC = []

    D = []
    for i in range(M):
        b, c = map(int, input().split())
        BC.append((b, c))
    BC.sort(key=lambda x: x[1], reverse=True)
    ld = 0
    for b, c in BC:
        D += [c] * b
        ld += b
        if ld > N:
            break
    
    res = 0
    A.sort()
    for i in range(N):
        res += max(A[i], D[i])
        

    res = 0
    for i in range(N):
        res += max(A[i], D[i])

    print(res)

if __name__ == "__main__":
    main()

Fixed code:
def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    BC = []

    D = []
    for i in range(M):
        b, c = map(int, input().split())
        BC.append((b, c))
    BC.sort(key=lambda x: x[1], reverse=True)
    ld = 0
    for b, c in BC:
        D += [c] * b
        ld += b
        if ld > N:
            break
    
    res = 0
    A.sort()
    for i in range(N):
        if i < ld:
            res += max(A[i], D[i])
        else:
            res += A[i]
    

    print(res)

if __name__ == "__main__":
    main()
Buggy code:
import bisect

N, M = [int(i) for i in input().split()]
alist = [int(i) for i in input().split()]
alist.sort()
inputs = [[int(i) for i in input().split()] for j in range(M)]
inputs.sort(key=lambda x:x[1], reverse=True)

for i in range(N):
    if inputs[0][1] > alist[i]:
        alist[i] = inputs[0][1]
        inputs[0][0] -= 1
        if inputs[0][0] == 0:
            inputs.pop(0)
    else:
        break
print(sum(alist))


Fixed code:
import bisect

N, M = [int(i) for i in input().split()]
alist = [int(i) for i in input().split()]
alist.sort()
inputs = [[int(i) for i in input().split()] for j in range(M)]
inputs.sort(key=lambda x:x[1], reverse=True)

for i in range(N):
    if len(inputs) == 0:
        break
    if inputs[0][1] > alist[i]:
        alist[i] = inputs[0][1]
        inputs[0][0] -= 1
        if inputs[0][0] == 0:
            inputs.pop(0)
    else:
        break
print(sum(alist))
Buggy code:
N,M=map(int,input().split())
A=list(map(int,input().split()))
BC=[list(map(int,input().split())) for _ in range(M)]
count=0
D=[]
BC.sort(key=lambda x:x[1],reverse=True)
A.sort()
for B,C in BC:
  D+=[C]*B
  count+=B
  if count>=N:
    break
  
D=D[:N]
for i in range(N):
  d=D[i]
  if A[i]<d:
    A[i]=d
  else:
    break
  
print(sum(A))

Fixed code:
N,M=map(int,input().split())
A=list(map(int,input().split()))
BC=[list(map(int,input().split())) for _ in range(M)]
count=0
D=[]
BC.sort(key=lambda x:x[1],reverse=True)
A.sort()
for B,C in BC:
  D+=[C]*B
  count+=B
  if count>=N:
    break

D=D[:N]
for i in range(len(D)):
  d=D[i]
  if A[i]<d:
    A[i]=d
  else:
    break
  
print(sum(A))
Buggy code:
N,M=map(int,input().split())


A=[int(i) for i in (input().split(" "))]

A.sort()

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

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

for b,c in BC:
    A.expend([c]*b)
    if len(A)>2*N:
        break

A.sort(reverse=True)
suma=0
#for i in A:
#    suma+=i

print(sum(A[:N]))

Fixed code:

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


A=[int(i) for i in (input().split(" "))]

A.sort()

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

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

for b,c in BC:
    A.extend([c]*b)
    if len(A)>2*N:
        break

A.sort(reverse=True)
suma=0
#for i in A:
#    suma+=i

print(sum(A[:N]))
Buggy code:
import heapq
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ans = []

N, M = LI()
A = LI()
BC = [LI() for _ in range(M)]
A = list(map(lambda x: x*(-1), A))
heapq.heapify(A)
BC = sorted(BC, key=lambda x: x[1])[::-1]
#print(BC)
#print(A)
k = int(0)
ansC = int(0)
ansA = heapq.heappop(A)*(-1)
for i in range(N):
    if BC[k][0]==0:
        k +=1
    if BC[k][1]>ansA:
        ansC = BC[k][1]
        BC[k][0] -=1
    else:
        ansC = ansA
        if A!=[]:
            ansA = heapq.heappop(A)*(-1)
    ans.append(ansC)

#print(ans)
print(sum(ans))


Fixed code:
import heapq
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ans = []

N, M = LI()
A = LI()
BC = [LI() for _ in range(M)]
A = list(map(lambda x: x*(-1), A))
heapq.heapify(A)
BC = sorted(BC, key=lambda x: x[1])[::-1]
#print(BC)
#print(A)
k = int(0)
ansC = int(0)
ansA = heapq.heappop(A)*(-1)
for i in range(N):
    if k<M and BC[k][0]==0:
        k +=1
    if k<M and BC[k][1]>ansA:
        ansC = BC[k][1]
        BC[k][0] -=1
    else:
        ansC = ansA
        if A!=[]:
            ansA = heapq.heappop(A)*(-1)
    ans.append(ansC)

#print(ans)
print(sum(ans))
Buggy code:
from heapq import heappush, heappop

# input
N,M =map(int,input().split())
A = [0]*N
B = [0]*M
C = [0]*M
ans = int(0)

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


def solve():

    pairs = zip(C, B)
    sortedpairs = sorted(pairs, key=lambda x: x[0])
    for i in range(M):
        B[i]=sortedpairs[i][1]
        C[i]=sortedpairs[i][0]

    ansA=[0]*N
    for j in range(N):
        ansA[j] = A[j]
    p = int(0)
    j = int(0)
    while  j<N :
        jd = int(0)
        while  B[M-1-p]-jd>0 :
            if ansA[j]>=C[M-1-p] :
                break
            ansA[j]=C[M-1-p]
            jd +=1
            j +=1
            if j==N : break
        if ansA[j] >= C[M - 1 - p]:
            break
        p +=1

    for j in range(N):
        ans += ansA[j]

    print(ans)

solve()

Fixed code:
from heapq import heappush, heappop

# input
N,M =map(int,input().split())
A = [0]*N
B = [0]*M
C = [0]*M

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


def solve():

    pairs = zip(C, B)
    sortedpairs = sorted(pairs, key=lambda x: x[0])
    for i in range(M):
        B[i]=sortedpairs[i][1]
        C[i]=sortedpairs[i][0]

    ansA=[0]*N
    for j in range(N):
        ansA[j] = A[j]

    p = int(0)
    j = int(0)
    while  j<N and M>=1+p :
        jd = int(0)
        if ansA[j] >= C[M - 1 - p]:
            break
        while  B[M-1-p]-jd>0 :
            if j==N : break
            if ansA[j]>=C[M-1-p] :
                break
            else:
                ansA[j]=C[M-1-p]
            jd +=1
            j +=1

        p +=1

    ans = int(0)
    for j in range(N):
        ans += ansA[j]

    print(ans)

solve()
Buggy code:
# -*- coding: utf-8 -*-
import heapq
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N, M = map(int, input().split())
L = list(map(int, input().split()))
OP = [list(map(int, input().split())) for _ in range(M)]
heapq.heapify(L)
OP = sorted(OP, key=lambda x: x[1], reverse=True)
ans = 0

for m, n in OP:
    for _ in range(m):
        head = heapq.heappop(L)
        if head < n:
            ans += n
        else:
            print(ans+sum(L)+head)
            exit()
print(ans)


Fixed code:
# -*- coding: utf-8 -*-
import heapq
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N, M = map(int, input().split())
L = list(map(int, input().split()))
OP = [list(map(int, input().split())) for _ in range(M)]
heapq.heapify(L)
OP = sorted(OP, key=lambda x: x[1], reverse=True)
ans = 0

for m, n in OP:
    for _ in range(m):
        if L:
            head = heapq.heappop(L)
            if head < n:
                ans += n
            else:
                print(ans+sum(L)+head)
                exit()
        else:
            print(ans)
            exit()
print(ans+sum(L))
Buggy code:
N, M = map(int, input().split())
A = [int(i) for i in input().split()]
A.sort()
BC = []

for i in range(M):
  bc = [int(i) for i in input().split()]
  BC.append(bc)

BC = sorted(BC, key=lambda x: x[1])
BC.reverse()

k = 0
for i in BC:
  if k = N:
    break
  if i[1] <= A[k]:
    break
  for j in range(i[0]):
    if i[1] > A[k]:
      A[k] = i[1]
      k += 1
    else:
      break

print(sum(A))

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

for i in range(M):
  bc = [int(i) for i in input().split()]
  BC.append(bc)

BC = sorted(BC, key=lambda x: x[1])
BC.reverse()

k = 0
for i in BC:
  if k == N:
    break
  if i[1] <= A[k]:
    break
  for j in range(i[0]):
    if k == N:
      break
    if i[1] > A[k]:
      A[k] = i[1]
      k += 1
    else:
      break

print(sum(A))
Buggy code:
import bisect
from collections import deque

n,m = map(int,input().split())
a = list(map(int,input().split()))
bc = []
for _ in range(m):
    tmp = list(map(int,input().split()))
    bc.append(tmp[::-1])

a.sort()
a = deque(a)
bc.sort(reverse=True)

ans = 0
num = 0
for i in range(m):
    index = bisect.bisect_left(a,bc[i][0])
    if index == 0:
        break
    if bc[i][1] > index:
        ans += bc[i][0] * index
        num += index
    else:
        ans += bc[i][0] * bc[i][1]
        num += bc[i][1]
    for i in range(num):
        a.popleft()

print(ans + sum(a))


Fixed code:
import bisect
from collections import deque

n,m = map(int,input().split())
a = list(map(int,input().split()))
bc = []
for _ in range(m):
    tmp = list(map(int,input().split()))
    bc.append(tmp[::-1])

a.sort()
a = deque(a)
bc.sort(reverse=True)

ans = 0
num = 0
for i in range(m):
    index = bisect.bisect_left(a,bc[i][0])
    if index == 0:
        break
    if bc[i][1] > index:
        ans += bc[i][0] * index
        num = index
    else:
        ans += bc[i][0] * bc[i][1]
        num = bc[i][1]
    for _ in range(num):
        a.popleft()

print(ans + sum(a))