qyliang/CodeNanoFix huggingface dataset:
Buggy code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
n,m = map(int,readline().split())
a = list(map(int,readline().split()))
import heapq
heapq.heapify(a)

b = []
for i in range(m):
  c = list(map(int,readline().split()))
  b.append(c)
  
from operator import itemgetter
b = tuple(sorted(b,key=itemgetter(1),reverse=True))

cnt = 0
for i in b:
  cnt += i[0]
  for j in range(cnt):
    p = heapq.heappop(a)
    if p >= i[1]:
      cnt += 10**5
      heapq.heappush(a,p)
      break
    heapq.heappush(a,i[1])
  if cnt >= n:
    break
print(sum(a))

Fixed code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
n,m = map(int,readline().split())
a = list(map(int,readline().split()))
import heapq
heapq.heapify(a)

b = []
for i in range(m):
  c = list(map(int,readline().split()))
  b.append(c)
  
from operator import itemgetter
b = tuple(sorted(b,key=itemgetter(0),reverse=True))
b = tuple(sorted(b,key=itemgetter(1),reverse=True))

cnt = 0
for i in b:
  cnt += i[0]
  chk = i[0]
  for j in range(chk):
    p = heapq.heappop(a)
    if p >= i[1]:
      cnt += 10**5
      heapq.heappush(a,p)
      break
    heapq.heappush(a,i[1])
  if cnt >= n:
    break
print(sum(a))
Buggy code:
import sys
imput=sys.stdin.readline
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
y=[list(map(int,input().split()))[::-1] for i in range(m)]
y.sort()
y=y[::-1]
#print(y)

l=[]
w=0
for i in range(m):
    w+=y[i][1]
    l+=[y[i][0]]*y[i][1]
    if w>=n:
        break

#print(l)
l=l[:min(n,m)]
ans=0
#print(a,l)
for i in range(n):
    if i<=len(l)-1:
        ans+=max(a[i],l[i])
    else:
        ans+=a[i]

print(ans)

Fixed code:
import sys
imput=sys.stdin.readline
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
y=[list(map(int,input().split()))[::-1] for i in range(m)]
y.sort()
y=y[::-1]
#print(y)

l=[]
w=0
for i in range(m):
    w+=y[i][1]
    l+=[y[i][0]]*y[i][1]
    if w>=n:
        break

#print(l)
l=l[:n]
ans=0
#print(a,l)
for i in range(n):
    if i<=len(l)-1:
        ans+=max(a[i],l[i])
    else:
        ans+=a[i]

print(ans)
Buggy code:
N, M = map(int,input().split())
As = sorted(list(map(int,input().split())))
BCs = sorted([list(map(int,input().split())) for _ in range(M)],key = lambda x:-x[1])

j = 0
for i in range(N):
  if As[i] > BCs[j][1]:
    break
  else:
    As[i] = BCs[j][1]
    BCs[j][0] -= 1
    if BCs[j][0] == 0:
      j += 1

print(sum(As))
    

Fixed code:
N, M = map(int,input().split())
As = sorted(list(map(int,input().split())))
BCs = sorted([list(map(int,input().split())) for _ in range(M)],key = lambda x:-x[1])

j = 0
for i in range(N):
  if BCs[j][0] <= 0:
    j += 1
  if j == M or As[i] > BCs[j][1]:
    break
  else:
    As[i] = BCs[j][1]
    BCs[j][0] -= 1


print(sum(As))
Buggy code:
import collections
N,M = map(int, input().split())
A = list(map(int,input().split()))
D = collections.Counter(A)

A.sort()
max = 0 

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

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

sum = 0 
count = 0 
for k,v in D:
    print("k : " + str(k) + " v: " + str(v) + " n: " + str(N))
    if N > v : 
        sum += k * v
        N -= v 
    else : 
        sum += k * N 
        break 
print(sum)

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

A.sort()
max = 0 

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

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

sum = 0 
count = 0 
for k,v in D:
    if N > v : 
        sum += k * v
        N -= v 
    else : 
        sum += k * N 
        break 
print(sum)
Buggy code:
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], reverse=True)

i = 0
for b, c in BC:
    for j in range(b):
        A[i] = max(A[i], c)
        i += 1

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)]

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

i = 0
for b, c in BC:
    for j in range(b):
        A[i] = max(A[i], c)
        i += 1
        if i >= N:
            print(sum(A))
            exit()

print(sum(A))
Buggy code:
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
b=[]
sum=0
for i in range(m):
    b.append(list(map(int,input().split())))
b.sort(key=lambda x: x[1])
B=len(b)
b1=b[B-1][0]
b2=b[B-1][1]
b3=0
for i in range(n):
    if a[i]<b2 and b3==0:
        sum+=b2
        print(sum)
        b1-=1
    else:
        sum+=a[i]
    if b1==0:
        B-=1
        if B==0:
            b3=1
        b1=b[B-1][0]
        b2=b[B-1][1]
print(sum)

Fixed code:
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
b=[]
sum=0
for i in range(m):
    b.append(list(map(int,input().split())))
b.sort(key=lambda x: x[1])
B=len(b)
b1=b[B-1][0]
b2=b[B-1][1]
b3=0
for i in range(n):
    if a[i]<b2 and b3==0:
        sum+=b2
        b1-=1
    else:
        sum+=a[i]
    if b1==0:
        B-=1
        if B==0:
            b3=1
        b1=b[B-1][0]
        b2=b[B-1][1]
print(sum)
Buggy code:
N, M = map(int,input().split())
A = 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 x:x[1], reverse=True)

A.sort()

i = 0
j = 0
while i < N and j < M:
    while BC[j][1] > A[i]:
        if BC[j][0] < 1:
            j += 1
            continue
        A[i] = BC[j][1]
        BC[j][0] -= 1
        i += 1
    break

print(sum(A))

Fixed code:
N, M = map(int,input().split())
A = 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 x:x[1], reverse=True)
# print(BC)

A.sort()

i = 0
j = 0
while i < N and j < M and BC[j][1] > A[i]:
    if BC[j][0] < 1:
        j += 1
        continue
    A[i] = BC[j][1]
    BC[j][0] -= 1
    i += 1

print(sum(A))
Buggy code:
import heapq

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

B = []
C = []
BC = []

A.sort()

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


sorted(BC, key=lambda x: x[1])
BC = BC[::-1]
C = C.sort()

heapq.heapify(A)

# print(A)
# print(BC)

buff = []
counter = 0
for i in range(M):
    # if counter > N*2:
    #     break
    for j in range(BC[i][0]):
        # buff.append(BC[i][1])
        # counter += 1
        heapq.heappush(A, BC[i][1])
        heapq.heappop(A)
        if counter > N*2:
            break

# A = A + buff
# A.sort()
# A = A[::-1]
print(A)
print(sum(A))

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

B = []
C = []
BC = []

A.sort()

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


BC = sorted(BC, key=lambda x: x[1])
BC = BC[::-1]
C = C.sort()

# print(A)
# print(BC)

buff = []
counter = 0
for i in range(M):
    if counter > N*2:
        break
    for j in range(BC[i][0]):
        buff.append(BC[i][1])
        counter += 1
        if counter > N*2:
            break

A = A + buff
A.sort()
A = A[::-1]
print(sum(A[:N]))
Buggy code:
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
changer=[]
cl=0
flag=0
for i in range(m):
  b=list(map(int,input().split()))
  changer.append(b)
changer.sort(key=lambda x:x[1],reverse=True)
for i in range(m):
  if flag==1:
    break
  for j in range(cl,min(cl+changer[i][0],n)):
    if a[j]<changer[i][1]:
      a[j]=changer[i][1]
    else:
      flag=1
      break
print(sum(a))

Fixed code:
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
changer=[]
cl=0
flag=0
for i in range(m):
  b=list(map(int,input().split()))
  changer.append(b)
changer.sort(key=lambda x:x[1],reverse=True)
for i in range(m):
  if flag==1:
    break
  for j in range(cl,min(cl+changer[i][0],n)):
    if a[j]<changer[i][1]:
      a[j]=changer[i][1]
    else:
      flag=1
      break
  cl+=changer[i][0]
print(sum(a))
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)]
bc.sort(reverse = True, key = lambda x: x[1])
i = 0
counter = 0
while 1:
    for j in range(bc[i][0]):
        a.append(bc[i][1])
        counter += 1
        if counter == n:
            break
    if counter == n:
        break
    i += 1

a.sort(reverse = True)
print(sum(a[:n]))


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)]
bc.sort(reverse = True, key = lambda x: x[1])
i = 0
counter = 0
while 1:
    for j in range(bc[i][0]):
        a.append(bc[i][1])
        counter += 1
        if counter == n:
            break
    if counter == n or i == len(bc)-1:
        break
    i += 1

a.sort(reverse = True)
print(sum(a[:n]))
Buggy code:
N, M = map(int, input().split())
A = list(map(int, input().split()))

Nd = {}

for i in range(M):
  B, C = map(int, input().split())
  Nd[C] = B
  
for a in A:
  if a in Nd.keys():
    Nd[a] = Nd[a] + 1
  else:
    Nd[a] = 1
    
Nd = sorted(Nd.items(), key=lambda x: -x[0]) # type=list
sum = 0

for key, val in Nd:
  if N >= val:
    sum = sum + key*val
    N = N-val
  else:
    sum = sum + key*N
    break;
  
print(sum)

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

Nd = {}

for i in range(M):
  B, C = map(int, input().split())
  if C in Nd.keys():
    Nd[C] = Nd[C] + B
  else:
    Nd[C] = B
  
for a in A:
  if a in Nd.keys():
    Nd[a] = Nd[a] + 1
  else:
    Nd[a] = 1
    
Nd = sorted(Nd.items(), key=lambda x: -x[0]) # type=list
sum = 0

for key, val in Nd:
  if N >= val:
    sum = sum + key*val
    N = N-val
  else:
    sum = sum + key*N
    break;
  
print(sum)
Buggy code:
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(reverse=True, key=lambda x: x[1])
last_idx = len(a) - 1
idx = 0
for e in bc:
    b, c = e
    for i in range(b):
        if i + idx >= last_idx:
            break
        if a[i + idx] < c:
            a[i + idx] = c
        else:
            break
    else:
        idx += b
        print(a)
        print(idx)
        continue
    break

print(sum(a))


Fixed code:
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(reverse=True, key=lambda x: x[1])
last_idx = len(a) - 1
idx = 0
for e in bc:
    b, c = e
    for i in range(b):
        if i + idx > last_idx:
            break
        if a[i + idx] < c:
            a[i + idx] = c
    idx += b

print(sum(a))
Buggy code:
n, m = map(int, input().split())
a = list(map(int, input().split()))
cb = [list(map(int, input().split()[::-1])) for _ in range(m)]
a.sort()
cb.sort()
summation = 0
a_tmp = a.pop()
cb_tmp = cb.pop()
for i in range(n):
  if a_tmp >= cb_tmp[0]:
    summation += a_tmp
    if a:
      a_tmp = a.pop()
  else:
    summation += cb_tmp[0]
    if cb_tmp[1] > 0:
      cb_tmp[1] -= 1
    elif cb:
      cb_tmp = cb.pop()
    else:
      cb_tmp = [0, n]
print(summation)

Fixed code:
n, m = map(int, input().split())
a = list(map(int, input().split()))
cb = [list(map(int, input().split()[::-1])) for _ in range(m)]
a.sort()
cb.sort()
summation = 0
a_tmp = a.pop()
cb_tmp = cb.pop()
for i in range(n):
  if a_tmp >= cb_tmp[0]:
    summation += a_tmp
    if a:
      a_tmp = a.pop()
  else:
    summation += cb_tmp[0]
    cb_tmp[1] -= 1
    if cb_tmp[1] == 0:
      if cb:
        cb_tmp = cb.pop()
      else:
        cb_tmp = [0, n]
print(summation)
Buggy code:
n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [list(map(int, input().split())) for _ in range(m)]
a = sorted(a)
bc = sorted(bc, key=lambda x: -x[1])
print(bc)
a_len = len(a)
i = 0
for one in bc:
    for j in range(one[0]):
        if i > a_len-1:
            break
        if a[i] > one[1]:
            break
        a[i] = one[1]
        i += 1
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)]
a = sorted(a)
bc = sorted(bc, key=lambda x: -x[1])
a_len = len(a)
i = 0
for one in bc:
    for j in range(one[0]):
        if i > a_len-1:
            break
        if a[i] > one[1]:
            break
        a[i] = one[1]
        i += 1
print(sum(a))
Buggy code:
n,m=map(int,input().split())
a=list(map(int,input().split()))
b=sorted([list(map(int,input().split())) for i in range(m)],key=lambda x:x[1])[::-1]
i=0
for num,v in b:
	for j in range(num):
		if a[i]<v and i<n:
			a[i]=v
			i+=1
		else:
			print(sum(a))
			exit()
else:
	print(sum(a))

Fixed code:
n,m=map(int,input().split())
a=sorted(map(int,input().split()))
b=sorted([list(map(int,input().split())) for i in range(m)],key=lambda x:x[1])[::-1]
i=0
for num,v in b:
	for j in range(num):
		if i<n and a[i]<v:
			a[i]=v
			i+=1
		else:
			print(sum(a))
			exit()
else:
	print(sum(a))
Buggy code:
N, M = map(int, input().split())

A = sorted(list(map(int, input().split())), reverse=True)

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

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

cnt = 0
ans = 0
a = 0
b = 0
while True:
    if b <= M-1 and A[a] <= B[b][1]:
        for k in range(B[b][0]):
            ans += B[b][1]
            cnt += 1
            if cnt == N:
                print(ans)
                break
        b += 1
    elif a <= N-1:
        cnt += 1
        ans += A[a]
        if cnt == N:
            print(ans)
            break
        a += 1

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

A = sorted(list(map(int, input().split())), reverse=True)

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

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

cnt = 0
ans = 0
a = 0
b = 0
while True:
    if b <= M-1 and A[a] <= B[b][1]:
        n = min(B[b][0], N-cnt)
        ans += B[b][1]*n
        cnt += n
        if cnt == N:
            print(ans)
            break
        b += 1
    elif a <= N-1:
        cnt += 1
        ans += A[a]
        if cnt == N:
            print(ans)
            break
        a += 1
Buggy code:
import sys
N, M = map(int, input().split())
A = [int(_) for _ in input().split()]
A.sort()
#print(A)
BC = [0] * M
for m in range(M):
    BC[m] = list(map(int, input().split()))
BC.sort(key=lambda x: x[1], reverse=True)
print(BC)
c = 0
for m in range(M):
    for t in range(BC[m][0]):
        if BC[m][1] > A[c]:
            A[c] = BC[m][1]
        c += 1
        if c == N:
            print(sum(A))
            sys.exit()
print(sum(A))

Fixed code:
import sys
N, M = map(int, input().split())
A = [int(_) for _ in input().split()]
A.sort()
#print(A)
BC = [0] * M
for m in range(M):
    BC[m] = list(map(int, input().split()))
BC.sort(key=lambda x: x[1], reverse=True)
#print(BC)
c = 0
for m in range(M):
    for t in range(BC[m][0]):
        if BC[m][1] > A[c]:
            A[c] = BC[m][1]
        c += 1
        if c == N:
            print(sum(A))
            sys.exit()
print(sum(A))
Buggy code:
import heapq

n, m = map(int, input().split())
a = list(map(int, input().split()))
heapq.heapify(a)
bc = sorted(
    [list(map(int, input().split())) for _ in range(m)],
    reverse=True,
    key=lambda x: x[1],
)

max_b = 0
for i in bc:
    b, c = i
    if b > max_b:
        max_b = b
        for _ in range(b):
            mn = heapq.heappop(a)
            if mn < c:
                heapq.heappush(a, c)
            else:
                heapq.heappush(a, mn)
                break

print(sum(a))

Fixed code:
import heapq

n, m = map(int, input().split())
a = list(map(int, input().split()))
heapq.heapify(a)
bc = sorted(
    [list(map(int, input().split())) for _ in range(m)],
    reverse=True,
    key=lambda x: x[1],
)

for i in bc:
    b, c = i
    for _ in range(b):
        mn = heapq.heappop(a)
        if mn < c:
            heapq.heappush(a, c)
        else:
            heapq.heappush(a, mn)
            break

print(sum(a))
Buggy code:


def read_input():
    n, m = map(int, input().split())
    alist = list(map(int, input().split()))

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

    return alist, clist

def submit():
    alist, clist = read_input()

    alist.sort(reverse=False)
    clist.sort(key=lambda x: x[0], reverse=True)

    j = 0
    result = 0
    for a in alist:
        if j >= len(clist):
            result += a
        elif a >= clist[j][0]:
            result += a
        else:
            if clist[j][1] > 0:
                result += clist[j][0]
                clist[j][1] -= 1
                if clist[j][1] == 0:
                    j += 1

    print(result)


Fixed code:


def read_input():
    n, m = map(int, input().split())
    alist = list(map(int, input().split()))

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

    return alist, clist

def submit():
    alist, clist = read_input()

    alist.sort(reverse=False)
    clist.sort(key=lambda x: x[0], reverse=True)

    j = 0
    result = 0
    for a in alist:
        if j >= len(clist):
            result += a
        elif a >= clist[j][0]:
            result += a
        else:
            if clist[j][1] > 0:
                result += clist[j][0]
                clist[j][1] -= 1
                if clist[j][1] == 0:
                    j += 1

    print(result)



if __name__ == '__main__':
    submit()
Buggy code:
from collections import defaultdict
import sys
# input = sys.stdin.readline

N, M = [int(item) for item in input().split()]
A = [int(item) for item in input().split()]
A.sort()

new_dict = defaultdict(lambda: 0)
for i in range(M):
    b, c = [int(item) for item in input().split()]
    new_dict[c] += b
new_dict = sorted(new_dict.items(), key=lambda x: -x[0])


"""
new_list = []
for k, v in new_dict:
    new_list.extend([k] * v)
    if len(new_list) >= N:
        break
new_list = new_list[:N]

#####

n = min(N, len(new_list))
for i in range(n):
    if A[i] >= new_list[i]:
        break
    A[i] = new_list[i]

print(sum(A))
"""


Fixed code:
from collections import defaultdict
import sys
input = sys.stdin.readline

N, M = [int(item) for item in input().split()]
A = [int(item) for item in input().split()]
A.sort()

new_dict = defaultdict(lambda: 0)
for i in range(M):
    b, c = [int(item) for item in input().split()]
    new_dict[c] += b
new_dict = sorted(new_dict.items(), key=lambda x: -x[0])

new_list = []
for k, v in new_dict:
    if v > N:
        v = N
    new_list += [k] * v
    if len(new_list) >= N:
        break

new_list = new_list[:N]

#####

n = min(N, len(new_list))
for i in range(n):
    if A[i] >= new_list[i]:
        break
    A[i] = new_list[i]

print(sum(A))
Buggy code:
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)
array = []
for _ in range(M):
    tmp = list(map(int, input().split()))
    array.append(tmp)
array.sort(key=lambda x: x[1], reverse=True)

_sum = 0
_num = 0

for i in range(N+M):
    if A[0] > array[0][1]:
        _sum += A.pop(0)
        _num += 1
    else:
        a = array[0][0] if array[0][0] < N - _num else N - _num
        _sum += array.pop(0)[1] * a
        _num += a
        
    if _num == N:
        break
    
print(_sum)

Fixed code:
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)
array = []
for _ in range(M):
    tmp = list(map(int, input().split()))
    array.append(tmp)
array.sort(key=lambda x: x[1], reverse=True)

_sum = 0
_num = 0

for i in range(N+M):
    if A[0] > array[0][1]:
        _sum += A.pop(0)
        _num += 1
        
    else:
        a = array[0][0] if array[0][0] < N - _num else N - _num
        _sum += array.pop(0)[1] * a
        _num += a
        if not array:
            break
        
    if _num == N:
        break
    
if _num != N:
    while _num < N:
        _sum += A.pop(0)
        _num += 1
        
print(_sum)
Buggy code:
#n = int(input())
#n,k = map(int,input().split())
#x = list(map(int,input().split()))


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

A = sorted(A,key=lambda x:x*(-1))
BC = sorted(BC,key=lambda x:x[1]*(-1))
li = []

cnt = 0
j = 0
while(cnt<N and j<M):
    if BC[j][0]>0:
        BC[j][0] -= 1
        li.append(BC[j][1])
    else:
        j+=1
    cnt += 1

ans = 0
ai = 0
bi = 0
cnt2 = 0
while(cnt2<N):
    if bi<len(li) and ai<N:
        if li[bi]>A[ai]:
            ans += li[bi]
            bi += 1
        else:
            ans += A[ai]
            ai += 1
    
    elif bi<len(li):
        ans += li[bi]
        bi += 1

    else:
        ans += A[ai]
        ai += 1
    cnt2 += 1

print(ans)


Fixed code:
#n = int(input())
#n,k = map(int,input().split())
#x = list(map(int,input().split()))


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

A = sorted(A,key=lambda x:x*(-1))
BC = sorted(BC,key=lambda x:x[1]*(-1))
li = []

cnt = 0
j = 0
while(cnt<N and j<M):
    if BC[j][0]>0:
        BC[j][0] -= 1
        li.append(BC[j][1])
        cnt += 1
    else:
        j+=1
    

ans = 0
ai = 0
bi = 0
cnt2 = 0
while(cnt2<N):
    if bi<len(li) and ai<N:
        if li[bi]>A[ai]:
            ans += li[bi]
            bi += 1
        else:
            ans += A[ai]
            ai += 1
    
    elif bi<len(li):
        ans += li[bi]
        bi += 1

    else:
        ans += A[ai]
        ai += 1
    cnt2 += 1

print(ans)
Buggy code:
n, m = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
BC = [list(map(int, input().split())) for i in range(m)]
BC.sort(key=lambda x: x[1], reverse=True)
ans = 0
cmax = BC[0][1]
cnt = 0
j = 0
for i in range(n):
    print(cnt)
    if A[i] < cmax:
        ans += cmax
        cnt += 1
    else:
        ans += A[i]

    if cnt == BC[j][0]:
        if j == m - 1:
            cmax = -1
            continue
        else:
            j += 1
            cmax = BC[j][1]
            cnt = 0
    else:
        continue
print(ans)


Fixed code:
n, m = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
BC = [list(map(int, input().split())) for i in range(m)]
BC.sort(key=lambda x: x[1], reverse=True)
ans = 0
cmax = BC[0][1]
cnt = 0
j = 0
for i in range(n):
    if A[i] < cmax:
        ans += cmax
        cnt += 1
    else:
        ans += A[i]

    if cnt == BC[j][0]:
        if j == m - 1:
            cmax = -1
            continue
        else:
            j += 1
            cmax = BC[j][1]
            cnt = 0
    else:
        continue
print(ans)
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.sort(key=lambda x: x[1], reverse=True )

n = 0
for b,c in bc:
    for i in range(b):
        if i == n or a[i] >= c:
            break

        a[i] = c
        n += 1

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.sort(key=lambda x: x[1], reverse=True )
count = 0
for b,c in bc:
    for _ in range(b):
        if count == n or a[count] >= c:
            break

        a[count] = c
        count += 1

print(sum(a))