qyliang/CodeNanoFix huggingface dataset:
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 = sorted(BC, key=lambda x: -x[1])
i = 0
for B, C in BC:
    for _ in range(B):
        if A[i] < C:
            A[i] = C
            i += 1
        else:
            print(sum(A))
            exit()


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 = sorted(BC, key=lambda x: -x[1])
i = 0
for B, C in BC:
    for _ in range(B):
        if i < N and A[i] < C:
            A[i] = C
            i += 1
        else:
            print(sum(A))
            exit()
print(sum(A))
Buggy code:
n,m=map(int,input().split())
a=list(map(int,input().split()))
b=[]
sum=0
res=0
for i in range(m):
  d,e=map(int,input().split())
  b.append([e,d])
a.sort()
b.sort()
b.reverse()
for i in range(1,m):
  b[i][1]+=b[i-1][1]
m=0
if b[0][1]>0 and b[0][0]>a[0]:
  a[0]=b[0][0]
if n>=2:
  for i in range(1,n):
    while i>b[m][1]-1:
      m+=1
    if b[m][0]>a[i]:
      a[i]=b[m][0]
    else:
      break
for i in range(n):
  res+=a[i]
print(res)


Fixed code:
n,m=map(int,input().split())
a=list(map(int,input().split()))
b=[]
sum=0
res=0
for i in range(m):
  d,e=map(int,input().split())
  b.append([e,d])
a.sort()
b.sort()
b.reverse()

for i in range(1,m):
  b[i][1]+=b[i-1][1]

id=0
if b[0][1]>0 and b[0][0]>a[0]:
  a[0]=b[0][0]
if n>=2:
  for i in range(1,n):
    while i>b[id][1]-1:
      id+=1

      if id>m-1:
        break
    if id>m-1:
      break
    if b[id][0]>a[i]:
      a[i]=b[id][0]
    else:
      break
for i in range(n):
  res+=a[i]
print(res)
Buggy code:
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 20 20:47:47 2019

@author: Owner
"""

# -*- coding: utf-8 -*-
"""
Created on Sat Apr 13 09:53:12 2019

@author: Owner
"""

from operator import itemgetter
import itertools
import bisect

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

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

BC = [(0, 0)]*M
for m in range(M):
    BC[m] = tuple(map(int, input().split()))
BC.sort(key=itemgetter(1), reverse = True)

def main(): 
    #print(BC)
    res = 0
    num = 0
    for i in range(M):
        idx = 0         
        idx = bisect.bisect_left(A, BC[i][1])
        if idx == 0:
            break
        else:
            less =min(idx, BC[i][0]) 
            num += less
            res += BC[i][1]*less

    print(res+sum(A[num：]))
                
                
                
            
            
                

    
if __name__ == '__main__':
    main()


Fixed code:
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 20 20:47:47 2019
 
@author: Owner
"""
 
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 13 09:53:12 2019
 
@author: Owner
"""
 
from operator import itemgetter
import itertools
import collections
import bisect
 
N, M = list(map(int, input().split()))
 
A = list(map(int, input().split()))
A.sort()

A = collections.deque(A)

BC = [(0, 0)]*M
for m in range(M):
    BC[m] = tuple(map(int, input().split()))
BC.sort(key=itemgetter(1), reverse = True)
 
def main(): 
    #print(BC)
    res = 0
    for i in range(M):
        idx = 0         
        idx = bisect.bisect_left(A, BC[i][1])
        if idx == 0:
            break
        else:
            less =min(idx, BC[i][0])
            for n in range(less):
                A.popleft()
            res += BC[i][1]*less
 
    print(res+sum(A))
                
                
                
            
            
                
 
    
if __name__ == '__main__':
    main()
Buggy code:
import heapq

n,m = map(int,input().split())
a_list = list(map(int,input().split()))
move = [list(map(int,input().split())) for _ in range(m)]

heapq.heapify(a_list)
a_list = sorted(a_list, key=lambda x: x[1],reverse=True)

for elem in move:
    for i in range(elem[0]):
        _min = heapq.heappop(a_list)
        if _min < elem[1]:
            heapq.heappush(a_list,elem[1])
        else:
            heapq.heappush(a_list,_min)
            break

print(sum(a_list))

Fixed code:
import heapq

n,m = map(int,input().split())
a_list = list(map(int,input().split()))
move = [list(map(int,input().split())) for _ in range(m)]

heapq.heapify(a_list)
move = sorted(move, key=lambda x: x[1],reverse=True)

for elem in move:
    for i in range(elem[0]):
        _min = heapq.heappop(a_list)
        if _min < elem[1]:
            heapq.heappush(a_list,elem[1])
        else:
            heapq.heappush(a_list,_min)
            break

print(sum(a_list))
Buggy code:
n,m = list(map(int, input().split(" ")))

A = list(map(int, input().split(" ")))
bc = []
for _ in range(m):
  b,c = list(map(int, input().split(" ")))
  bc_ append((b,c))

count = 0
bc = sorted(bc,key=lambda x:x(1),reverse = True)
big_list = []
for b,c in bc:
  big_list += [c for _ in range(b)]
  count += b
  if count >= N:
    break
print(sum( sorted((A+big_list),reverse=True)[:N] ))

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

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

count = 0
bc = sorted(bc,key=lambda x:x[1],reverse = True)
big_list = []
for b,c in bc:
  big_list += [c for _ in range(b)]
  count += b
  if count >= n:
    break
print(sum( sorted((A+big_list),reverse=True)[:n] ))
Buggy code:
import sys
sys.setrecursionlimit(10 ** 5)

N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
amin, amax = A[0], A[N-1]

BC = {}
for _ in range(M):
    b, c = map(int, input().split())
    if c in BC:
        BC[c] += b
    else:
        BC[c] = b

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

b = 0
c = 0
for i in range(N):
    if BC[c][0]<=A[i]:
        break
    A[i] = BC[c][0]
    b += 1
    if b==BC[c][1]:
        c += 1
        b = 0

print(sum(A))

Fixed code:
import sys
sys.setrecursionlimit(10 ** 5)

N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
amin, amax = A[0], A[N-1]

BC = {}
for _ in range(M):
    b, c = map(int, input().split())
    if c in BC:
        BC[c] += b
    else:
        BC[c] = b

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

b = 0
c = 0
cmax = len(BC)
for i in range(N):
    if BC[c][0] <= A[i]:
        break
    A[i] = BC[c][0]
    b += 1
    if b == BC[c][1]:
        c += 1
        b = 0
        if c == cmax:
            break

print(sum(A))
Buggy code:
n, m = map(int, input().split())
a = list(map(int, input().split()))
l = []
for i in range(m):
    data = list(map(int,input().split()))
    l.append(data)
l.sort(key=lambda x:x[1])

a.sort()

b_idx=0
nokori=l[b_idx][0]
for i in range(n):
    if a[i] > l[b_idx][1]:
        break
    else:
        a[i] = l[b_idx][1]
        nokori-=1
        if nokori == 0:
            b_idx+=1
            if b_idx >= m:
                break
            nokori=l[b_idx][0]

print(a)

Fixed code:
n, m = map(int, input().split())
a = list(map(int, input().split()))
l = []
for i in range(m):
    data = list(map(int,input().split()))
    l.append(data)
l.sort(key=lambda x:x[1])
l.reverse()

a.sort()

b_idx=0
nokori=l[b_idx][0]
for i in range(n):
    if a[i] > l[b_idx][1]:
        break
    else:
        a[i] = l[b_idx][1]
        nokori-=1
        if nokori == 0:
            b_idx+=1
            if b_idx >= m:
                break
            nokori=l[b_idx][0]

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

N, M = map(int, sys.stdin.readline().split())
a_list = list(map(int, sys.stdin.readline().split()))
items = []
for i in range(M):
    items.append(tuple(map(int, sys.stdin.readline().split())))

from heapq import *

que = []

for i in range(N):
    a = a_list[i]
    heappush(que, (-a, 1))

for b, c in items:
    heappush(que, (-c, b))

cnt = 0
ans = 0
while cnt < N:
    k, v = heappop(que)

    if cnt + v <= N:
        cnt += v
        ans += (-k * v)
    else:
        cnt = N
        ans += (-k) * (N-cnt)
print(ans)


Fixed code:
import sys

N, M = map(int, sys.stdin.readline().split())
a_list = list(map(int, sys.stdin.readline().split()))
items = []
for i in range(M):
    items.append(tuple(map(int, sys.stdin.readline().split())))

from heapq import *

que = []

for i in range(N):
    a = a_list[i]
    heappush(que, (-a, 1))

for b, c in items:
    heappush(que, (-c, b))

cnt = 0
ans = 0
while cnt < N:
    k, v = heappop(que)
    if cnt + v <= N:
        cnt += v
        ans += -(k * v)
    else:
        ans += -(k) * (N-cnt)
        cnt = N
print(ans)
Buggy code:

N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [list(map(int,input().split())) for _ in range(M)]
BC = sorted(BC,key= lambda x: x[1],reverse=True)
A.sort()
n = 0
i = 0
m = 0
while A[i]<BC[n][1]:
    A[i] = BC[n][1]
    m = m+1
    if m >=BC[n][0]:
        m = 0
        n = n+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)]
BC = sorted(BC,key= lambda x: x[1],reverse=True)
A.sort()
n = 0
i = 0
m = 0
while A[i]<BC[n][1]:
    A[i] = BC[n][1]
    m = m+1
    if m >=BC[n][0]:
        m = 0
        n = n+1
    i += 1
    if n==M:
      break
    if i == N:
      break
print(sum(A))
Buggy code:
n,m=map(int,input().split())
arr=list(map(int,input().split()))
arr=sorted(arr)
q=[list(map(int,input().split())) for _ in range(m)]
q=sorted(q,reverse=True,key=lambda x:x[1])
q=sorted(q,reverse=True,key=lambda x:x[0])
pos=0
for i in range(n):
  if arr[i]<=q[pos][1]:
    arr[i]=q[pos][1]
    q[pos][0]-=1
    if q[pos][0]==0:
      pos+=1
      if pos==m:
        break
print(sum(arr))

Fixed code:
n,m=map(int,input().split())
arr=list(map(int,input().split()))
arr=sorted(arr)
q=[list(map(int,input().split())) for _ in range(m)]
q=sorted(q,reverse=True,key=lambda x:x[0])
q=sorted(q,reverse=True,key=lambda x:x[1])
pos=0
for i in range(n):
  if arr[i]<=q[pos][1]:
    arr[i]=q[pos][1]
    q[pos][0]-=1
    if q[pos][0]==0:
      pos+=1
      if pos==m:
        break
print(sum(arr))
Buggy code:
n,m=map(int,input().split())
a=sorted(list(map(int,input().split())))
b=[list(map(int,input().split())) for i in range(m)]
b.sort(key=lambda x:x[1])
p=0
c=[]
for i in b:
  if p>n:
    break
  c+=[i[1]]*i[0]
  p+=i[0]
c=c[:n]
m=sum(a)
x=m
for i in range(n):
  m=m-a[i]+c[i]
  x=max(x,m)
print(x)

Fixed code:
n,m=map(int,input().split())
a=sorted(list(map(int,input().split())))
b=[list(map(int,input().split())) for i in range(m)]
b.sort(key=lambda x:x[1])
p=0
c=[]
for i in b[::-1]:
  if p>n:
    break
  c+=[i[1]]*i[0]
  p+=i[0]
n=min(len(c),n)
c=c[:n]
m=sum(a)
x=m
for i in range(n):
  m=m-a[i]+c[i]
  x=max(x,m)
print(x)
Buggy code:
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
x=[]
for i in range(m):
    b,c=map(int,input().split())
    x.append([b,c])
y=sorted(x,key=lambda x:(x[1],x[0]),reverse=True)

d=[]
for i in range(m):
    d.extend([y[i][1]]*y[i][0])

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

Fixed code:
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
x=[]
for i in range(m):
    b,c=map(int,input().split())
    x.append([b,c])
y=sorted(x,key=lambda x:(x[1],x[0]),reverse=True)

d=[]
cnt=0
for i in range(m):
    cnt+=y[i][0]
    d.extend([y[i][1]]*y[i][0])
    if cnt>n:
        break

for i in range(n):
    if len(d)<=i:
        break
    if d[i]>a[i]:
        a[i]=d[i]
    else:
        break
print(sum(a))
Buggy code:
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
x=[]
for i in range(m):
    b,c=map(int,input().split())
    x.append([b,c])
y=sorted(x,key=lambda x:(x[1],x[0]),reverse=True)

p=0
q=0
for i in range(n):
    if a[i]<y[p][1]:
        a[i]=y[p][1]
        q+=1
    else:
        break
    if q==y[p][0]:
        p+=1

print(sum(a))

Fixed code:
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
x=[]
for i in range(m):
    b,c=map(int,input().split())
    x.append([b,c])
y=sorted(x,key=lambda x:(x[1],x[0]),reverse=True)

p=0
q=0
for i in range(n):
    if a[i]<y[p][1]:
        a[i]=y[p][1]
        q+=1
    else:
        break
    if q==y[p][0]:
        p+=1
        q=0
        if p==m:
            break

print(sum(a))
Buggy code:
n,m = map(int,input().split())
a = [[1,int(i)] for i in input().split()]
bc = [[int(i) for i in input().split()] for j in range(m)]
x = a+bc
x.sort(key=lambda y:y[1])
total = 0
ans = 0
for i in reversed(range(n+m)):
    if total >= n:
        break
    else:
        ans += x[1][i]*min(n-total,x[0][i])
        total += x[0][i]
print(ans)

Fixed code:
n,m = map(int,input().split())
a = [[1,int(i)] for i in input().split()]
bc = [[int(i) for i in input().split()] for j in range(m)]
x = a+bc
x.sort(key=lambda y:y[1])
total = 0
ans = 0
for i in reversed(range(n+m)):
    if total >= n:
        break
    else:
        ans += x[i][1]*min(n-total,x[i][0])
        total += x[i][0]
print(ans)
Buggy code:
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = []
for i in range(M):
    bc = list(map(int, input().split()))
    BC.append(bc)
BC.sort(key = lambda x : x[1])
irekae = []
count = 0
i = 0
while count < N:
    tmpb = BC[M-1-i][0]
    tmpc = BC[M-1-i][1]
    irekae += [tmpc for i in range(tmpb)]
    count += tmpb
    i += 1

ans = A + irekae
ans.sort(reverse = True)
print(sum(ans[:N]))

Fixed code:
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = []
for i in range(M):
    bc = list(map(int, input().split()))
    BC.append(bc)
BC.sort(key = lambda x : x[1])
irekae = []
count = 0
i = 0
while count < N:
    tmpb = BC[M-1-i][0]
    tmpc = BC[M-1-i][1]
    irekae += [tmpc for i in range(tmpb)]
    count += tmpb
    i += 1
    if i == M:
        break
ans = A + irekae
ans.sort(reverse = True)
print(sum(ans[:N]))
Buggy code:
n, m = map(int,input().split())
a = list(map(int,input().split()))
rewrite = []
for i in range(m):
    rewrite.append(list(map(int,input().split())))
rewrite.sort(key= lambda x : x[1], reverse=True)
a.sort()
k = 0
flag = 0
for i in range(min(n,sum(rewrite[0]))):
    for j in range(rewrite[i][0]):
        if rewrite[i][1] > a[k]:
            a[k] = rewrite[i][1]
            k += 1
        else:
            flag = 1
            break
    if flag == 1:
        break
print(sum(a))


Fixed code:
n, m = map(int,input().split())
a = list(map(int,input().split()))
rewrite = []
for i in range(m):
    rewrite.append(list(map(int,input().split())))
rewrite.sort(key= lambda x : x[1], reverse=True)
a.sort()
k = 0
flag = 0
for i in range(m):
    for j in range(rewrite[i][0]):
        if rewrite[i][1] > a[k]:
            a[k] = rewrite[i][1]
            k += 1
            if k == n:
                flag = 1
                break
        else:
            flag = 1
            break
    if flag == 1:
        break
print(sum(a))
Buggy code:
from collections import*
I=lambda:map(int,input().split())
n,m=I()
a=Counter(I())
exec("b,c=I();a[c]+=b"*m)
s=0
for k,v in sorted(a.items())[::-1]:
 if n<v:exit(print(s+k*n))
 n-=v;s+=k*v

Fixed code:
from collections import*
I=lambda:map(int,input().split())
n,m=I()
a=Counter(I())
for _ in[0]*m:b,c=I();a[c]+=b
s=0
for k,v in sorted(a.items())[::-1]:
 if n<v:exit(print(s+k*n))
 n-=v;s+=k*v
Buggy code:
import bisect
n,m = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort()

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


ans = 0
count = 0
#data[][1] = value, data[][0] = card_num
for i in range(m):
    insert_index = bisect.bisect_left(a, data[i][1])
    if insert_index == 0:
        break
    count = min(data[i][0], insert_index)
    ans += count * data[i][1]
print(ans + sum(a))


Fixed code:
import bisect
n,m = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort()

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


ans = 0
count = 0
pre_index = 0
#print(data)
#print(a)
#data[][1] = value, data[][0] = card_num
for i in range(m):
    insert_index = bisect.bisect_left(a, data[i][1])
    count = min(data[i][0], insert_index - pre_index)
    for j in range(pre_index, pre_index + count):
        if j >= n:
            break
        a[j] = 0
    ans += count * data[i][1]
    pre_index = pre_index + count
print(ans + sum(a))
Buggy code:
n,m=map(int,input().split())
a=sorted(list(map(int,input().split())))
l=[list(map(int,input().split())) for i in range(m)]
l.sort(key=lambda x:x[1])
for i in range(n):
    if a[i]<l[-1][1]:
        a[i]=l[-1][1]
        l[-1][0]-=1
        if l[-1][0]==0:l.pop(-1)
    else:break
print(sum(a))

Fixed code:
n,m=map(int,input().split())
a=sorted(list(map(int,input().split())))
l=[list(map(int,input().split())) for i in range(m)]
l.sort(key=lambda x:x[1])
for i in range(n):
    if a[i]<l[-1][1]:
        a[i]=l[-1][1]
        l[-1][0]-=1
        if l[-1][0]==0:
            l.pop(-1)
            if len(l)==0:break
    else:break
print(sum(a))
Buggy code:
import heapq
n, m = map(int, input().split())
a = list(map(int, input().split()))
heapq.heapify(a)
bc = [list(map(int, input().split())) for _ in range(m)]
bc.sort(key = lambda x: x[1], reverse = True)

for i in range(m):
    if a[b-1] >= bc[i][1]:
        break
    else:
        cnt = 0
        while cnt < bc[i][0]:
            heapq.heapreplace(a, bc[i][1])
            cnt += 1

print(sum(a))

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

for i in range(m):
    if a[0] >= bc[i][1]:
        break
    else:
        cnt = 0
        while cnt < bc[i][0]:
            heapq.heapreplace(a, bc[i][1])
            cnt += 1

print(sum(a))
Buggy code:
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=sorted(A)
BC=sorted(BC, key=lambda val:val[1], reverse=True)
print(N,M,A,BC)


change=[]
for bc in BC:
    for _ in range(bc[0]):
        change.append(bc[1])
print(change)

for i in range(len(change)):
    if A[i]>change[i]:
        answer = sum(change[:i]) + sum(A[i:])
        print(answer)
        break
else:
    answer=sum(change[:i+1])+sum(A[i+1:])
    print(answer)


Fixed code:
import copy

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=sorted(A)
BC=sorted(BC, key=lambda val:val[1], reverse=True)
#print(N,M,A,BC)


change=[]
for bc in BC:
    for _ in range(bc[0]):
        change.append(bc[1])
    if len(change)>=N:
        break
#print(change)

if A[0]>=change[0]:
    print(sum(A))
else:
    for i in range(min(N,len(change))):
        if A[i]>change[i]:
            answer = sum(change[:i]) + sum(A[i:])
            print(answer)
            break
    else:
        answer=sum(change[:i+1])+sum(A[i+1:])
        print(answer)
Buggy code:
from operator import itemgetter

n,m=map(int,input().split())
a=list(map(int,input().split()))
bc=[]

for i in range(m):
    x=list(map(int,input().split()))
    bc.append(x)

a.sort()
bc.sort(key=itemgetter(1),reverse=True)

k=0
for i in range(m):
    while(bc[i][0]>0):
        j=bc[i][0]
        c=bc[i][1]
        print([j,c])
        if(j==0):
            break
        if(a[k]>c):
            break
        a[k]=c
        bc[i][0]-=1
        k+=1
    else:
        continue
    break

print(sum(a))

Fixed code:
from operator import itemgetter

n,m=map(int,input().split())
a=list(map(int,input().split()))
bc=[]

for i in range(m):
    x=list(map(int,input().split()))
    bc.append(x)

a.sort()
bc.sort(key=itemgetter(1),reverse=True)

k=0
for i in range(m):
    b=bc[i][0]
    c=bc[i][1]
    for j in range(b):
        if(a[k]>c):
            break
        a[k]=c
        k+=1
        if(k>n-1):
            break
    else:
        continue
    break

print(sum(a))
Buggy code:
import numpy as np
n,m=map(int,input().split())
a=list(map(int,input().split()))
b=[0]*m
c=[0]*m
for i in range(m):
    b[i],c[i]=map(int,input().split())
xc=np.argsort(c)
a.sort()
co=0
for i in range(m-1,-1,-1):
    for j in range(b[xc[i]]):
        a[co]=max(a[j],c[xc[i]])
        if a[j]>c[xc[i]]:
            break                
        co+=1
print(sum(a))

Fixed code:
import numpy as np
n,m=map(int,input().split())
a=list(map(int,input().split()))
b=[0]*m
c=[0]*m
for i in range(m):
    b[i],c[i]=map(int,input().split())
xc=np.argsort(c)
a.sort()
co=0
f=0
for i in range(m-1,-1,-1):
    for j in range(b[xc[i]]):
        a[co]=max(a[co],c[xc[i]])
        if a[co]>c[xc[i]]:
            f=1
            break                
        co+=1
        if co>=n:
            f=1 
            break
    if f==1:
        break
print(sum(a))
Buggy code:
n,m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
bc = sorted([list(map(int, input().split())) for i in range(m)], key =lambda x:x[1])
bc.reverse()

flg = 0
cnt = 0

for i in range(n):
  if bc[flg][1] >a[i] and flg < m:
    a[i] = bc[flg][1]
    cnt +=1
    if cnt == bc[flg][0]:
      flg += 1
      cnt = 0
  else:
      break

print(sum(a))
  

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

flg = 0
cnt = 0

for i in range(n):
  if bc[flg][1] >a[i] and flg < m:
    a[i] = bc[flg][1]
    cnt +=1
    if cnt == bc[flg][0]:
      flg += 1
      cnt = 0
      if flg == m:
        break
  else:
      break

print(sum(a))
Buggy code:
N,M = map(int,input().split())
A = list(map(int,input().split()))
A = sorted(A)
B = [list(map(int,input().split())) for _ in range(M)]
B = sorted(B,key=lambda x:x[1],reverse=True)
cur = 0
cnt = 0
for i in range(N):
    if cnt==B[cur][0]:
        cur +=1
        cnt = 0
    if B[cur][1]<=A[i]:
        break
    A[i] = B[cur][1]
    cnt += 1
print(sum(A))

Fixed code:
N,M = map(int,input().split())
A = list(map(int,input().split()))
A = sorted(A)
B = [list(map(int,input().split())) for _ in range(M)]
B = sorted(B,key=lambda x:x[1],reverse=True)
cur = 0
cnt = 0
for i in range(N):
    if cnt==B[cur][0]:
        cur +=1
        if cur==M:break
        cnt = 0
    if B[cur][1]<=A[i]:
        break
    A[i] = B[cur][1]
    cnt += 1
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(key=lambda x: x[1], reverse=True)
for B, C in BC:
    A.sort()
    mn = A[0]
    if mn >= C:
        break
    if A[B - 1] <= C:
        A[:B] = [C] * B
    else:
        a, b = 0, B
        while a + 1 < b:
            i = a + (b - a) // 2
            if A[i] >= C:
                b = i + 1
            else:
                a = i
        A[:b] = [C] * b

print(sum(A))

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

BC.sort(key=lambda x: x[1], reverse=True)
A.sort()
l, r = 0, N
for B, C in BC:
    if A[l] >= C:
        break
    a, b = l, r
    if A[r - 1] >= C:
        while a + 1 < b:
            i = a + (b - a) // 2
            if A[i] >= C:
                b = i
            else:
                a = i
        r = b
    n = B + l
    if n > r:
        n = r
        B = n - l
    if n <= b:
        A[l:n] = [C] * B
        l = n
    else:
        A[l:b] = [C] * B
        l = b
    if l == r:
        break

print(sum(A))