qyliang/CodeNanoFix huggingface dataset:
Buggy code:
import sys
n, m = map(int,input().split())
aList = list(map(int,input().split()))
aList.sort()
bcDict = dict()
for i in range(m):
  b, c = map(int,input().split())
  if b in bcDict:
    bcDict[b] += c
  else:
    bcDict[b] = c
bcDict_sorted = sorted(bcDict.items(), key=lambda x:-x[0])
num = 0
for (k, v) in bcDict_sorted:
  for i in range(k):
    if num < len(aList) and aList[num] < v:
      aList[num] = v
      num += 1
    else:
      print(sum(aList))
      sys.exit()
print(sum(aList))

Fixed code:
import sys
n, m = map(int,input().split())
aList = list(map(int,input().split()))
aList.sort()
bcDict = dict()
for i in range(m):
  b, c = map(int,input().split())
  if c in bcDict:
    bcDict[c] += b
  else:
    bcDict[c] = b
bcDict_sorted = sorted(bcDict.items(), key=lambda x:-x[0])
num = 0
for (k, v) in bcDict_sorted:
  for i in range(v):
    if num < len(aList) and aList[num] < k:
      aList[num] = k
      num += 1
    else:
      print(sum(aList))
      sys.exit()
print(sum(aList))
Buggy code:
#20:58
n,m = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
b = []
for i in range(m):
  now = list(map(int,input().split()))
  now.reverse()
  b.append(now)
b.sort()
b.reverse()
c = []
for i in range(m):
  c += [b[i][0]] * b[i][1]
  if len(c) > n:
    c = c[:n]
    break
if len(c) < n:
  c += [0] * (n-len(c))
ans = 0
for i in range(n):
  ans += max(a[i],c[i])
print(a)
print(c)
print(ans)

Fixed code:
#20:58
n,m = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
b = []
for i in range(m):
  now = list(map(int,input().split()))
  now.reverse()
  b.append(now)
b.sort()
b.reverse()
c = []
for i in range(m):
  c += [b[i][0]] * b[i][1]
  if len(c) > n:
    c = c[:n]
    break
if len(c) < n:
  c += [0] * (n-len(c))
ans = 0
for i in range(n):
  ans += max(a[i],c[i])
#print(a)
#print(c)
print(ans)
Buggy code:
n,m = map(int,input().split())
a = list(map(int,input().split()))
p = [[aa,1] for aa in a]
for _ in range(m):
  b = list(map(int,input().split()))
  b.reverse()
  p.append(b)
rest = n
score = 0
for pp in p:
  use = min(pp[1],rest)
  rest -= use
  score += pp[0] * use
print(score)

Fixed code:
n,m = map(int,input().split())
a = list(map(int,input().split()))
p = [[aa,1] for aa in a]
for _ in range(m):
  b = list(map(int,input().split()))
  b.reverse()
  p.append(b)
p.sort()
p.reverse()
rest = n
score = 0
for pp in p:
  use = min(pp[1],rest)
  rest -= use
  score += pp[0] * use
print(score)
Buggy code:
import sys

def my_index(l, x):
    if x in l:
        return l.index(x)
    else:
        return -1

input = sys.stdin.readline
n,m = map(int, input().split())
A = list(map(int,input().split()))
d = [list(map(int,input().split())) for _ in range(m)]

d.sort(key=lambda x:-x[1])
total = sum(A)
heapq.heapify(A)

for i in range(m):
    for j in range(d[i][0]):
        tmp = heapq.heappushpop(A, d[i][1])
        if tmp < d[i][1]:
                total += (d[i][1] - tmp)
            else:
                break

print(total)


Fixed code:
import sys
import heapq

input = sys.stdin.readline
n,m = map(int, input().split())
A = list(map(int,input().split()))
d = [list(map(int,input().split())) for _ in range(m)]

d.sort(key=lambda x:-x[1])
total = sum(A)
heapq.heapify(A)

for i in range(m):
    for j in range(d[i][0]):
        tmp = heapq.heappushpop(A, d[i][1])
        if tmp < d[i][1]:
                total += (d[i][1] - tmp)
        else:
            break

print(total)
Buggy code:
n,m=map(int,input().split())
lists=list(map(int,input().split()))
checklist=[]
count=0

for i in range(m):
    b,c=map(int,input().split())
    checklist.append((c,b))
checklist=sorted(checklist,reverse=True)
lists=sorted(lists)
uselist=[]
count=0

for i in checklist:
    for _ in range(i[1]):
        uselist.append(i[0])
    count+=i[1]

    if count>=n:
        break
answer=0
for i in range(n):
    if lists[i]<uselist[i]:
            lists[i]=uselist[i]
    else:
        break
print(sum(lists))

Fixed code:
n,m=map(int,input().split())
lists=list(map(int,input().split()))
checklist=[]
count=0

for i in range(m):
    b,c=map(int,input().split())
    checklist.append((c,b))
checklist=sorted(checklist,reverse=True)
lists=sorted(lists)
uselist=[]
count=0

for i in checklist:
    for _ in range(i[1]):
        uselist.append(i[0])
    count+=i[1]

    if count>=n:
        break
answer=0
n=min(n,len(uselist))
for i in range(n):
    if lists[i]<uselist[i]:
            lists[i]=uselist[i]
    else:
        break
print(sum(lists))
Buggy code:
n, m = map(int, input().split())
A = [int(x) for x in input().split()]
A.sort()

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

BC.sort(key=lambda x : x[1], reverse=True)
D = []
i = 0
count = 0
while (len(D) < n) and (len(BC) > i) :
    D.append(BC[i][1])
    count += 1
    if count == BC[i][0]:
        count = 0
        i += 1
        
j = 0
while A[j] <= D[j]:
    A[j] = D[j]
    j += 1
    
print(sum(A))

Fixed code:
n, m = map(int, input().split())
A = [int(x) for x in input().split()]
A.sort()

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

BC.sort(key=lambda x : x[1], reverse=True)
D = []
i = 0
count = 0
while (len(D) < n) and (len(BC) > i) :
    D.append(BC[i][1])
    count += 1
    if count == BC[i][0]:
        count = 0
        i += 1
        
j = 0
while (len(D) > j) and (A[j] <= D[j]) :
    A[j] = D[j]
    j += 1
    
print(sum(A))
Buggy code:
import sys
input = sys.stdin.readline

n,m = map(int,input().split())
heap = [int(i) for i in input().split()]
heap.sort()
ans = sum(heap)
ml = []
cnt = 0

for i in range(m):
  a,b = map(int,input().split())
  ml += [[b,a]]
ml.sort(reverse=True)

for i in range(m):
  for j in range(ml[i][1]):
    if ml[i][0] > heap[cnt]:
      ans += ml[i][0] - heap[cnt]
      cnt += 1
      if cnt == n:
        print(ans)
        exit()
    else:
      print(ans)
      exit()

Fixed code:
import sys
input = sys.stdin.readline

n,m = map(int,input().split())
heap = [int(i) for i in input().split()]
heap.sort()
ans = sum(heap)
ml = []
cnt = 0

for i in range(m):
  a,b = map(int,input().split())
  ml += [[b,a]]
ml.sort(reverse=True)

for i in range(m):
  for j in range(ml[i][1]):
    if ml[i][0] > heap[cnt]:
      ans += ml[i][0] - heap[cnt]
      cnt += 1
      if cnt == n:
        print(ans)
        exit()
    else:
      print(ans)
      exit()
print(ans)
Buggy code:
import sys  
input = sys.stdin.readline
 
N, M = list(map(int, input().split()))
 
A = list(map(int, input().split()))



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

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

cnt = 0
com = []
for b, c in BC:
    cnt += b
    if cnt > N:
        add = N-cnt+b
        com.extend([c]*add)
        break
    else:
        x = [c]*b 
        com.extend(x)

A.extend(com)
A = sorted(A, reverse=True)   

print(sum(A[:N])

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



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

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

cnt = 0
com = []
for b, c in BC:
    cnt += b
    if cnt > N:
        add = N-cnt+b
        com.extend([c]*add)
        break
    else:
        x = [c]*b 
        com.extend(x)
A.sort()   
num = -1
for k in range(N):
    if k == len(com):
        num = k
        break
    if A[k] > com[k]:
        num = k
        break

if num == -1:
    print(sum(com))
else:
    print(sum(com[:num])+sum(A[num:]))
Buggy code:
n,m=map(input().split())
a=list(map(int,input().split()))
a=[(1,z)for z in A]
b=[tuple(map(int,input().split()))for i in range(m)]
b+=a
b.sort(key=lambda x:x[1])
cnt=0
ans=0
while True:
    c=b.pop()
    if cut+c[0]<=n:
        ans+=c[0]*c[1]
        cnt+=c[0]
    else:
        ans+=c[1]*(n-cnt)
        break
print('{0}'.format(ans))

Fixed code:
n,m=map(int,input().split())
a=list(map(int,input().split()))
a=[(1,z)for z in a]
b=[tuple(map(int,input().split()))for i in range(m)]
b+=a
b.sort(key=lambda x:x[1])
cnt=0
ans=0
while True:
    c=b.pop()
    if cnt+c[0]<=n:
        ans+=c[0]*c[1]
        cnt+=c[0]
    else:
        ans+=c[1]*(n-cnt)
        break
print('{0}'.format(ans))
Buggy code:
N, M = map(int, input().split())
a = list(map(int, input().split()))
bc = [tuple(map(int, input().split())) for _ in range(M)]
bc.sort(key=lambda x: x[1], reverse=True)
rest = N
for b, c in bc:
  a += [c] * min(rest, b)
  rest -= min(rest, b)
a.sort(reversed=True)
print(sum(a[:N]))

Fixed code:
N, M = map(int, input().split())
a = list(map(int, input().split()))
bc = [tuple(map(int, input().split())) for _ in range(M)]
bc.sort(key=lambda x: x[1], reverse=True)
rest = N
for b, c in bc:
  a += [c] * min(rest, b)
  rest -= min(rest, b)
a.sort(reverse=True)
print(sum(a[:N]))
Buggy code:
def main():
    N, M = map(int, input().split())
    A = [int(x) for x in input().split()]

	for i in range(m):
    	changes.append([int(e) for e in input().split(" ")])
	changes.sort(key=lambda x:(x[1],x[0]),reverse = True)
	
    cnt = N
	for i in changes:
    	A.extend([i[1]] * min(i[0],cnt))
    	cnt -= min(i[0],cnt)
    A.sort()
    A.reverse()
    A = A[:N]
    print(sum(A))
if __name__ == "__main__":
    main()

Fixed code:
def main():
    N, M = map(int, input().split())
    A = [int(x) for x in input().split()]
    changes = []
    for i in range(M):
    	changes.append([int(e) for e in input().split(" ")])
    changes.sort(key=lambda x:(x[1],x[0]),reverse = True)
	
    cnt = N
    for i in changes:
    	A.extend([i[1]] * min(i[0],cnt))
    	cnt -= min(i[0],cnt)
    A.sort()
    A.reverse()
    A = A[:N]
    print(sum(A))
if __name__ == "__main__":
    main()
Buggy code:
from bisect import bisect_right

n, m = list(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], reverse=True)

res = 0
fin = 0
for b, c in bc:
    count = bisect_right(a, c, lo=fin, hi=min(b, len(a))) - fin
    fin += count
    res += count * c
for i in range(fin, len(a)):
    res += a[i]

print(res)


Fixed code:
from bisect import bisect_right

n, m = list(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], reverse=True)

res = 0
fin = 0
for b, c in bc:
    count = bisect_right(a, c, lo=fin, hi=min(fin+b, len(a))) - fin
    fin += count
    res += count * c
for i in range(fin, len(a)):
    res += a[i]

print(res)
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)]
A.sort(key=lambda x:-x)
BC.sort(key=lambda x:x[1])
C = []
while BC and len(B) < N:
    b,c = BC.pop()
    for i in range(b):
        C.append(c)
        if len(C) == N:
            break
C.sort()
ans = 0
while C:
    a = A.pop()
    c = C.pop()
    ans += max(a,c)
ans += sum(A)
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)]
A.sort(key=lambda x:-x)
BC.sort(key=lambda x:x[1])
C = []
while BC and len(C) < N:
    b,c = BC.pop()
    for i in range(b):
        C.append(c)
        if len(C) == N:
            break
C.sort()
ans = 0
while C:
    a = A.pop()
    c = C.pop()
    ans += max(a,c)
ans += sum(A)
print(ans)
Buggy code:
n, m = map(int, input().split())

from collections import defaultdict

cb = defaultdict(int)

for a in input().split():
    cb[int(a)] += 1

for _ in range(m):
    b, c = map(int, input().split())
    cb[c] += b

cbl = sorted(cb.items())

res = n

ans = 0

while res > 0:
    c, b = cbl.pop()
    ncount = min(res, b)
    ans += c * ncount
    res -= ncount

ans

Fixed code:
n, m = map(int, input().split())

from collections import defaultdict

cb = defaultdict(int)

for a in input().split():
    cb[int(a)] += 1

for _ in range(m):
    b, c = map(int, input().split())
    cb[c] += b

cbl = sorted(cb.items())

res = n

ans = 0

while res > 0:
    c, b = cbl.pop()
    ncount = min(res, b)
    ans += c * ncount
    res -= ncount

print(ans)
Buggy code:
n,m=map(int,input().split())
a=list(map(int,input().split()))
arr=[]

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

a=sorted(a)
arr=sorted(arr,key=lambda x:-x[1])

flag=0
left=0
for tup in arr:
    for i in range(left,max(left+tup[0],n+1)):
        if tup[1]>a[i]:
            a[i]=tup[1]
        else:
            flag=-1
            break
        if i==n:
            flag=-1
            break
    left+=tup[0]
    if flag==-1:
        break

print(sum(a))

Fixed code:
n,m=map(int,input().split())
a=list(map(int,input().split()))
arr=[]

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

a=sorted(a)
arr=sorted(arr,key=lambda x:-x[1])

flag=0
left=0
for tup in arr:
    for i in range(left,min(left+tup[0],n)):
        if tup[1]>a[i]:
            a[i]=tup[1]
        else:
            flag=-1
            break
        if i==n-1:
            flag=-1
            break
    left+=tup[0]
    if flag==-1:
        break

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

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

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

for i in range(M):
    for j in range(B[i][0]):
        c += 1
        if c > N:
            break
        if A[0] < B[i][1]:
            heapq.heappop(A)
            heapq.heappush(A,B[i][1])
            print(A)
        else:
            break
    else:
        continue
    break
    
print(sum(A))

Fixed code:
import heapq

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

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

c = 0
for i in range(M):
    for j in range(B[i][0]):
        c += 1
        if c > N:
            break
        if A[0] < B[i][1]:
            heapq.heappop(A)
            heapq.heappush(A,B[i][1])
            #print(A)
        else:
            break
    else:
        continue
    break
    
print(sum(A))
Buggy code:
import copy

n, m = [int(i) for i in input().split()]
ans = []
a = list(map(int, input().split()))
for i in range(len(a)):
    ans.append([1, a[i]])
for i in range(m):
    d, e = [int(i) for i in input().split()]
    ans.append([d, e])

ans1 = sorted(ans, key=lambda x: x[1], reverse = True)
print(ans1)
cnt = 0
ans2 = 0
k = 0
while cnt < n:
    ans2 += ans1[k][0] * ans1[k][1]
    cnt += ans1[k][0]
    k += 1
ans2 -= (cnt - n) * ans1[k-1][1]
print(ans2)


Fixed code:
import copy

n, m = [int(i) for i in input().split()]
ans = []
a = list(map(int, input().split()))
for i in range(len(a)):
    ans.append([1, a[i]])
for i in range(m):
    d, e = [int(i) for i in input().split()]
    ans.append([d, e])

ans1 = sorted(ans, key=lambda x: x[1], reverse = True)
cnt = 0
ans2 = 0
k = 0
while cnt < n:
    ans2 += ans1[k][0] * ans1[k][1]
    cnt += ans1[k][0]
    k += 1
ans2 -= (cnt - n) * ans1[k-1][1]
print(ans2)
Buggy code:
N, M = map(int, input().split())
A = sorted(map(int, input().split()))
BC = sorted([ list(map(int, input().split())) for _ in range(M)], key=lambda x:x[1], reverse=True)

s = 0
i = 0
B = 0
for a in A:
  if B == 0:
    if i < len(BC):
      B, C = BC[i]
      i += 1

  if B > 0 and C > a:
    s += C
    B -= 1
  else
    s += a

print(s)


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

s = 0
i = 0
B = 0
for a in A:
  if B == 0:
    if i < len(BC):
      B, C = BC[i]
      i += 1

  if B > 0 and C > a:
    s += C
    B -= 1
  else:
    s += a

print(s)
Buggy code:
import sys

fin = sys.stdin.readline

N, M = map(int, fin().split())
As = list(map(int, fin().split()))
BC = [tuple(map(int, fin().split())) for _ in range(M)]

As = sorted(As)
# BC = sorted(BC, key=lambda x: x[1], reverse=True)

BC_ = {}
for num, val in BC:
    if val in BC_:
        BC_[val] += num
    else:
        BC_[val] = num

BC_ = sorted(BC_.items(), reverse=True)


def f(a, n, v):
    count = 0
    for i in range(min(n, len(a))):
        if a[i] < v:
            count += 1
        else:
            break
    return v * count, a[count:]


sum__ = 0
for val, num in BC:
    if len(As) == 0 or As[0] >= val:
        break
    sum_, As = f(As, num, val)
    sum__ += sum_

print(sum__ + sum(As))


Fixed code:
import sys

fin = sys.stdin.readline

N, M = map(int, fin().split())
As = list(map(int, fin().split()))
BC = [tuple(map(int, fin().split())) for _ in range(M)]

As = sorted(As)
# BC = sorted(BC, key=lambda x: x[1], reverse=True)

BC_ = {}
for num, val in BC:
    if val in BC_:
        BC_[val] += num
    else:
        BC_[val] = num

BC_ = sorted(BC_.items(), reverse=True)

#
# def f(a, n, v):
#     if a[min(n-1, len(a)-1)] < v:
#         return v * min(n, len(a)), a[n:]
#     count = 0
#     for i in range(min(n, len(a))):
#         if a[i] < v:
#             count += 1
#         else:
#             break
#     return v * count, a[count:]


sum__ = 0
now = 0
for val, num in BC_:
    if now >= len(As) or As[now] >= val:
        break

    if As[min(num - 1 + now, len(As) - 1)] < val:
        sum_ = val * min(num, len(As) - now)
        now += min(num, len(As) - now)
    else:
        count = 0
        for i in range(min(num, len(As)-now)):
            if As[i + now] < val:
                count += 1
            else:
                break
        sum_ = val * count
        now += count
    sum__ += sum_

print(sum__ + sum(As[now:]))
Buggy code:
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
# x = list(map(int, input().split()))
# abc = [int(input()) for i in range(5)]
# n = int(input())
# n, m = list(map(int, input().split()))
bc = [list(map(int, input().split())) for i in range(m)]
a.sort()
bc_sorted = sorted(bc, key=lambda x: x[1], reverse=True)
l = 0
for i in range(m):
    l += bc[i][0]
j = 0
for i in range(min(l, n)):
    print(a[i], end=" ")
    print(bc_sorted[j][1])
    if a[i] <= bc_sorted[j][1]:
        a[i] = bc_sorted[j][1]
        bc_sorted[j][0] -= 1
        if bc_sorted[j][0] == 0:
            j += 1
    else:
        break
print(sum(a))


Fixed code:
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
# x = list(map(int, input().split()))
# abc = [int(input()) for i in range(5)]
# n = int(input())
# n, m = list(map(int, input().split()))
bc = [list(map(int, input().split())) for i in range(m)]
a.sort()
bc_sorted = sorted(bc, key=lambda x: x[1], reverse=True)
l = 0
for i in range(m):
    l += bc[i][0]
j = 0
for i in range(min(l, n)):
    if a[i] <= bc_sorted[j][1]:
        a[i] = bc_sorted[j][1]
        bc_sorted[j][0] -= 1
        if bc_sorted[j][0] == 0:
            j += 1
    else:
        break
print(sum(a))
Buggy code:
n, m = map(int, input().split()) 
card = list(map(int, input().split()))
card.sort()
command = [list(map(int, input().split())) for i in range(m)]
command.sort(key=lambda x: x[1], reverse=True)

ind = 0

for i in command:
    for j in range(i[0]):
        if card[ind] >= i[1] | len(card) == ind + 1:
            break
        card[ind] = i[1]
        ind += 1
    else:
        continue
    break
print(sum(card))

Fixed code:
n, m = map(int, input().split()) 
card = list(map(int, input().split()))
card.sort()
command = [list(map(int, input().split())) for i in range(m)]
command.sort(key=lambda x: x[1], reverse=True)

ind = 0
maxNum = len(card)
for i in command:
    for j in range(i[0]):
        if maxNum < ind+1:
            break
        if card[ind] >= i[1]:
            break
        card[ind] = i[1]
        ind += 1
    else:
        continue
    break
print(sum(card))
Buggy code:
from operator import itemgetter

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

A.sort(reverse=True)
BC.sort(key=itemgetter(1), reverse=True)

n = 0
ans = 0
while True:
    if not BC:
        break
    tgt = BC[0][1]
    a = A[0]
    if a >= tgt:
        ans += A.pop(0)
        n += 1
        if n == N:
            break
    else:
        tgt_n = BC[0][0]
        if n + tgt_n < N:
            ans += tgt * tgt_n
            n += tgt_n
            _ = BC.pop(0)
        else:
            ans += tgt * (N - n)
            break

print(ans)

Fixed code:
from operator import itemgetter

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

A.sort(reverse=True)
BC.sort(key=itemgetter(1), reverse=True)

n = 0
ans = 0
while True:
    if not BC:
        ans += sum(A[: N - n])
        break
    tgt = BC[0][1]
    a = A[0]
    if a >= tgt:
        ans += A.pop(0)
        n += 1
        if n == N:
            break
    else:
        tgt_n = BC[0][0]
        if n + tgt_n < N:
            ans += tgt * tgt_n
            n += tgt_n
            _ = BC.pop(0)
        else:
            ans += tgt * (N - n)
            break

print(ans)
Buggy code:
import sys
import bisect
import numpy as np
input = sys.stdin.readline
N,M=map(int,input().split())
A=[int(i) for i in input().split()]
B=[0]*M
C=[0]*M
for i in range(M):
    B[i],C[i]=[int(x) for x in input().split()]
matrix = np.vstack((np.array(B),np.array(C)))
matrix = matrix[:, np.argsort(-matrix[1])]
list2=np.zeros(N)
i=0
for b,c in zip(matrix[0],matrix[1]):
    i += b
    print(i-b,b)
    if i < N:
        list2[i-b:i] = c
        print(list2)
    else: 
        list2[i-b:]=c
        print(list2)
A.extend(list(list2))
A.sort()
int(sum(A[N:]))

Fixed code:
import sys
import bisect
import numpy as np
#input = sys.stdin.readline
N,M=map(int,input().split())
A=[int(i) for i in input().split()]
B=[0]*M
C=[0]*M
for i in range(M):
    B[i],C[i]=[int(x) for x in input().split()]
matrix = np.vstack((np.array(B),np.array(C)))
matrix = matrix[:, np.argsort(-matrix[1])]
list2=np.zeros(N)
i=0
for b,c in zip(matrix[0],matrix[1]):
    i += b
    #print(i-b,b)
    if i < N:
        list2[i-b:i] = c
        #print(list2)
    else: 
        list2[i-b:]=c
        #print(list2)
A.extend(list(list2))
A.sort()
print(int(sum(A[N:])))
Buggy code:
N,M=map(int, input().split())
import sys
def input(): return sys.stdin.readline().strip()
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)
from bisect import bisect_left
ans=0
for m in range(M):
    if A[BC[m][0]]<BC[m][1]:
        ans=ans+BC[m][1]*BC[m][0]
        A=A[BC[m][0]:]
    else:    
        r=bisect_left(A,BC[m][1])
        ans=ans+BC[m][1]*r
        A=A[r:]
ans+=sum(A)
print(ans)

Fixed code:
N,M=map(int, input().split())
import sys
def input(): return sys.stdin.readline().strip()
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)
from bisect import bisect_left
ans=0
l=0
for m in range(M):
    if BC[m][0]<len(A)-l and A[BC[m][0]+l]<BC[m][1]:
        ans=ans+BC[m][1]*BC[m][0]
        l+=BC[m][0]
    else:     
        r=bisect_left(A[l:],BC[m][1])
        if r:
            ans=ans+BC[m][1]*r
            l+=r
        break
ans+=sum(A[l:])
print(ans)