qyliang/CodeNanoFix huggingface dataset:
Buggy code:
# -*- coding: utf-8 -*-
my_dict = dict()
n, m = map(int, input().split())
a_list = list(map(int, input().split()))
for item in a_list:
    if item in my_dict:
        my_dict[item] = my_dict[item] + 1
    else:
        my_dict[item] = 1

for i in range(m):
    b, c = map(int, input().split())
    if c in my_dict:
        my_dict[c] = my_dict[c] + b
    else:
        my_dict[c] = b

items_sorted = sorted(my_dict.items(), reverse=True)
print(items_sorted)

ret = 0
rest = n

for item in items_sorted:
    if item[1] <= rest:
        ret = ret + item[0] * item[1]
        rest = rest - item[1]
    else:
        ret = ret + item[0] * rest
        break

print(ret)


Fixed code:
# -*- coding: utf-8 -*-
my_dict = dict()
n, m = map(int, input().split())
a_list = list(map(int, input().split()))
for item in a_list:
    if item in my_dict:
        my_dict[item] = my_dict[item] + 1
    else:
        my_dict[item] = 1

for i in range(m):
    b, c = map(int, input().split())
    if c in my_dict:
        my_dict[c] = my_dict[c] + b
    else:
        my_dict[c] = b

items_sorted = sorted(my_dict.items(), reverse=True)

ret = 0
rest = n

for item in items_sorted:
    if item[1] <= rest:
        ret = ret + item[0] * item[1]
        rest = rest - item[1]
    else:
        ret = ret + item[0] * rest
        break

print(ret)
Buggy code:
def main():
    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(reverse=True,key=lambda x:x[1])
    new_BC=[]
    count=0
    for i in range(M):
        for j in range(BC[i][0]):
            new_BC.append(BC[i][1])
            count+=1
            if count==N:
                break
        if count==N:
            break

    for i in range(len(new_BC)):
        if A[i]<new_BC[i]:
            A[i]=new_BC[i]
        else:
            break
    
    print(sum(A))
if __name__=="__main__":
    main()

Fixed code:
def main():
    N,M=map(int,input().split())
    A=list(map(int,input().split()))
    A.sort()
    BC=[]
    for i in range(M):
        bc=list(map(int,input().split()))
        BC.append(bc)

    BC.sort(reverse=True,key=lambda x:x[1])
    new_BC=[]
    count=0
    for i in range(M):
        for j in range(BC[i][0]):
            new_BC.append(BC[i][1])
            count+=1
            if count==N:
                break
        if count==N:
            break

    for i in range(len(new_BC)):
        if A[i]<new_BC[i]:
            A[i]=new_BC[i]
        else:
            break
    
    print(sum(A))
if __name__=="__main__":
    main()
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])
tmp = []
for B, C in BC:
    tmp += [C] * B
    if len(tmp) >= N:
        break
for i in range(N):
    if A[i] < tmp[i]:
        A[i] = tmp[i]
    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)]
A.sort()
BC.sort(key=lambda x: - x[1])
tmp = []
for B, C in BC:
    tmp += [C] * B
    if len(tmp) >= N:
        break
if len(tmp) < N:
    n = len(tmp)
else:
    n = N
for i in range(n):
    if A[i] < tmp[i]:
        A[i] = tmp[i]
    else:
        break
print(sum(A))
Buggy code:
N, M = map(int,input().split())
List_A = list(map(int,input().split()))
List_BC = [list(map(int,input().split())) for i in range(M)]

List_A.sort()
List_BC.sort(key=lambda x:x[1], reverse=True)
flag = 0
k = 0

print("{}".format(List_A))
print("{}".format(List_BC))

for i in range(M):
    for j in range(List_BC[i][0]):
        print(List_BC[i][0])
        if k < N:
            flaag = 1
            break
        elif List_BC[i][1] > List_A[k]:
            print(List_A)
            List_A[k] = List_BC[i][1]
            k += 1
        else:
            flag = 1
            break
    if flag == 1:
        break

tmp = 0
for i in range(N):
    tmp += List_A[i]
    
print("{}".format(tmp))

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

List_A.sort()
List_BC.sort(key=lambda x:x[1], reverse=True)
flag = 0
k = 0

for i in range(M):
    for j in range(List_BC[i][0]):
        if k >= N:
            flag = 1
            break
        elif List_BC[i][1] > List_A[k]:
            List_A[k] = List_BC[i][1]
            k += 1
        else:
            flag = 1
            break
    if flag == 1:
        break

tmp = 0
for i in range(N):
    tmp += List_A[i]
    
print("{}".format(tmp))
Buggy code:
N, M = map(int, input().split())
anlist=[]
for a in map(int, input().split()):
    anlist.append([1,a])
for _ in range(M):
    anlist.append(list(map(int, input().split())))
anlist=sorted(anlist, key=lambda x: x[1], reverse=True)
cnt = 0
ans = 0
for b, c in anlist:
    if cnt + c > N:
        ans += b+(N-cnt)
        break
    else:
        ans +=b*c
print(ans)


Fixed code:
N, M = map(int, input().split())
anlist=[]
for a in map(int, input().split()):
    anlist.append([1,a])
for _ in range(M):
    anlist.append(list(map(int, input().split())))
anlist=sorted(anlist, key=lambda x: x[1], reverse=True)
cnt = 0
ans = 0
for b, c in anlist:
    if cnt + b > N:
        ans += c*(N-cnt)
        break
    else:
        ans +=b*c
        cnt += b
print(ans)
Buggy code:
from collections import defaultdict
from collections import Counter
n,m=map(int,input().split())
A=[int(i) for i in input().split()]
AA=dict(Counter(A))
D=defaultdict(int)
for i in range(m):
  b,c=map(int,input().split())
  D[c]+=b
D=dict(D)
D.update(AA)
D=sorted(D.items(),key=lambda x:x[0],reverse=True)
ans,tot=0,0
for x,y in D:
  if tot+y<=n:
    ans+=x*y
    tot+=y
  else:
    ans+=x*(n-tot)
    tot+=n-tot
  if tot==n:
    break
print(ans)

Fixed code:
from collections import defaultdict
from collections import Counter
n,m=map(int,input().split())
A=[int(i) for i in input().split()]
AA=dict(Counter(A))
D=defaultdict(int)
for i in range(m):
  b,c=map(int,input().split())
  D[c]+=b
for x in AA.keys():
  D[x]+=AA[x]
D=sorted(D.items(),key=lambda x:x[0],reverse=True)
ans,tot=0,0
for x,y in D:
  if tot+y<=n:
    ans+=x*y
    tot+=y
  else:
    ans+=x*(n-tot)
    tot+=n-tot
  if tot==n:
    break
print(ans)
Buggy code:
inpl = lambda: list(map(int,input().split()))
N, M = inpl()
A = inpl()
A.sort()
BC = []
for _ in range(M):
    BC.append(inpl())
BC.sort(key=lambda x: -x[1])

D = []
for B, C in BC:
    if len(D) >= N:
        break
    d = [int(C)]*B
    D += d
D.sort(reverse=True)

for i in range(N):
    if D[i] > A[i]:
        A[i] = D[i]
    else:
        break
ans = sum(A)
print(ans)

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

lenD = len(D)
if lenD > N:
    lenD = N

for i in range(lenD):
    if D[i] > A[i]:
        A[i] = D[i]
    else:
        break
ans = sum(A)
print(ans)
Buggy code:
def index_1_sumall(list):
    return list[1]


n, m = tuple(int(i) for i in input().split(" "))
num_list = [int(i) for i in input().split(" ")]
num_list.sort()
change_list = []
ans_list = []
for j in range(m):
    change_list.append([int(i) for i in input().split(" ")])
change_list.sort(key=index_1_sumall, reverse=1)
break_is = 0
for i in change_list:
    for j in range(i[0]):
        try:
            if i[1] > num_list[0]:
            num_list.pop(0)
            ans_list.append(i[1])
            else:
                break_is = 1
                break
        except IndexError:
            break_is = 1
            break
    if break_is:
        break
num_list.append(0)
print(sum(num_list) + sum(ans_list))


Fixed code:
def index_1_sumall(list):
    return list[1]


n, m = tuple(int(i) for i in input().split(" "))
num_list = [int(i) for i in input().split(" ")]
num_list.sort()
change_list = []
ans_list = []
for j in range(m):
    change_list.append([int(i) for i in input().split(" ")])
change_list.sort(key=index_1_sumall, reverse=1)
break_is = 0
for i in change_list:
    for j in range(i[0]):
        try:
            if i[1] > num_list[0]:
                num_list.pop(0)
                ans_list.append(i[1])
            else:
                break_is = 1
                break
        except IndexError:
            break_is = 1
            break
    if break_is:
        break
num_list.append(0)
print(sum(num_list) + sum(ans_list))
Buggy code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
from bisect import bisect_left


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

bc = [tuple(map(int,input().split())) for i in range(m)]
bc = sorted(bc, reverse=True, key=lambda x: x[1])

i = 0
c_card = []
for b, c in bc:
    c_card.extend([c]*b)
    if len(c_card)+b >= n:
        break

ans = 0
for i in range(n):
    if i >= len(c_card):
        ans += a[i]
    else:
        ans += max(a[i], c_card[i])

print(ans)


Fixed code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
from bisect import bisect_left


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

bc = [tuple(map(int,input().split())) for i in range(m)]
bc = sorted(bc, reverse=True, key=lambda x: x[1])

i = 0
d = []
for b, c in bc:
    d.extend([c]*b)
    if len(d) >= n:
        break

ans = 0
for i in range(n):
    if i < len(d):
        ans += max(a[i], d[i])
    else: ans += a[i]

print(ans)
Buggy code:
from collections import deque

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

BC = [list(map(int, input().split())) for i in range(M)]
A.extend(BC)
A = list(A)
A.sort(key=lambda x: x[1], reverse=1)

print(A)

sum = 0
k = 0
for b, c in A:
        if k + b <= N:
                sum += b * c
                k += b
        elif k <= N:
                sum += (N - k) * c
                break
        print(sum)

print(sum)

Fixed code:
from collections import deque

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

BC = [list(map(int, input().split())) for i in range(M)]
A.extend(BC)
A = list(A)
A.sort(key=lambda x: x[1], reverse=1)


sum = 0
k = 0
for b, c in A:
        if k + b <= N:
                sum += b * c
                k += b
        elif k <= N:
                sum += (N - k) * c
                break

print(sum)
Buggy code:
import sys
import operator

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

a = [int(x) for x in sys.stdin.readline().rstrip().split()]
a.sort()

cards = [[int(x) for x in sys.stdin.readline().rstrip().split()] for _ in range(m)]
cards.sort(key=operator.itemgetter(1), reverse=True)

a_idx = 0
a_len = len(a)

for b, c in cards:
    for b_idx in range(b):
        if c <= a[a_idx] or a_idx == a_len:
            print(sum(a))
            exit()
        else:
            a[a_idx] = c
            a_idx += 1


Fixed code:
import sys
import operator

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

a = [int(x) for x in sys.stdin.readline().rstrip().split()]
a.sort()

cards = [[int(x) for x in sys.stdin.readline().rstrip().split()] for _ in range(m)]
cards.sort(key=operator.itemgetter(1), reverse=True)

a_idx = 0
a_len = len(a)

for b, c in cards:
    for b_idx in range(b):
        if a_idx == a_len or c <= a[a_idx]:
            break
        else:
            a[a_idx] = c
            a_idx += 1

print(sum(a))
Buggy code:
import sys
input = lambda : sys.stdin.readline().rstrip()

n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = []
for _ in range(m):
    bc.append(list(map(int, input().split())))
bc = sorted(bc, reverse=True, key=lambda x: x[1])
cnt = n
for b, c in bc:
    if cnt <= b:
        for i in range(cnt):
            a.append(c)
        break
    else:
        for i in range(b):
            a.append(c)
        cnt -= b
a.sort()
print(sum(a) - sum(a[:n]))
print(sum(a) - sum(a[:n]))

Fixed code:
import sys
input = lambda : sys.stdin.readline().rstrip()

n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = []
for _ in range(m):
    bc.append(list(map(int, input().split())))
bc = sorted(bc, reverse=True, key=lambda x: x[1])
cnt = n
for b, c in bc:
    if cnt <= b:
        for i in range(cnt):
            a.append(c)
        break
    else:
        for i in range(b):
            a.append(c)
        cnt -= b
a = sorted(a, reverse=True)
print(sum(a[:n]))
Buggy code:
from functools import cmp_to_key
n, m = map(int, input().split())
a = [int(e) for e in input().split()]
bc = [[int(e) for e in input().split()] for _ in range(m)]
bc.sort(key = cmp_to_key(lambda x, y: x[1] - y[1]))
t = 0
for bc in range(m):
  b, c = bc
  t += b
  a.extend([c] * b)
  if t > n:
    break
a.sort(reverse = True)
print(sum(a[:n]))


Fixed code:
from functools import cmp_to_key
n, m = map(int, input().split())
a = [int(e) for e in input().split()]
bc = [[int(e) for e in input().split()] for _ in range(m)]
bc.sort(key = cmp_to_key(lambda x, y: y[1] - x[1]))
t = 0
for v in bc:
  b, c = v
  t += b
  a.extend([c] * b)
  if t > n:
    break
a.sort(reverse = True)
print(sum(a[:n]))
Buggy code:
from operator import itemgetter

n, m = [int(i) for i in input().split()]
a = sorted([int(i) for i in input().split()])
#print(a)

bc = []
for _ in range(m):
  bc.append([int(i) for i in input().split()])
bc = sorted(bc, key=itemgetter(1), reverse=True)
#print(bc)

change = []
for b,c in bc:
  len_change = len(change)
  b_up = b
  if n - len_change < b:
    b_up = n - len_change
  change += [c]*b_up
  if b_up != b:
    break
#print(change)

ans = 0
for i in range(n):
  #print(ans)
  if a[i] < change[i]:
    ans += change[i]
  else:
    ans += sum(a[i:])
    break
    
print(ans)

Fixed code:
from operator import itemgetter

n, m = [int(i) for i in input().split()]
a = sorted([int(i) for i in input().split()])
#print(a)

bc = []
for _ in range(m):
  bc.append([int(i) for i in input().split()])
bc = sorted(bc, key=itemgetter(1), reverse=True)
#print(bc)

change = []
for b,c in bc:
  len_change = len(change)
  b_up = b
  if n - len_change < b:
    b_up = n - len_change
  #print(b_up)
  change += [c]*b_up
  if b_up != b:
    break
change += [0]*(n-len(change))

ans = 0
for i in range(n):
  #print(ans)
  #print(i)
  if a[i] < change[i]:
    ans += change[i]
  else:
    ans += sum(a[i:])
    break
    
print(ans)
Buggy code:
N, M = map(int,input().split())
A = sorted(list(map(int, input().split())))
p = [ list(map(int, input().split())) for i in range(M) ]
p = sorted(p, reverse=True, key=lambda x: x[1])

maxv = max(A)
minv = min(A)
ans = sum(A)

if p[0][1] < minv:
    print(ans)
else:
    cnt = 0
    j = 0
    i = 0
    for x in p:
        while(i < min(j + x[0], N)):
            if x[1] > A[i]:
                ans = ans - A[i] + x[1]
                i = i+1
            else:
                break
        else:
            j = j+i
            if j >= N:
                break
            continue
        break
    print(ans)

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

maxv = max(A)
minv = min(A)
ans = sum(A)

if p[0][1] < minv:
    print(ans)
else:
    cnt = 0
    j = 0
    i = 0
    for x in p:
        while(i < min(j + x[0], N)):
            if x[1] > A[i]:
                ans = ans - A[i] + x[1]
                i = i+1
            else:
                break
        else:
            j = i
            if j >= N:
                break
            continue
        break
    print(ans)
Buggy code:
N, M = map(int, input().split())
A = list(map(int, input().split()))

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

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

s=0
for b, c in D:
    if s+b>N-1:
        e=N-1
    else:
        e=s+b
    cnt=0
    for i in range(s, e):
        if A[i]<c:
            A[i]=c
        else:
            cnt=i
            break
    if cnt>0:
        s+=cnt
    else:
        s+=b+1

print(sum(A))


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

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

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

s=0
for b, c in D:
    if s+b>N:
        e=N
    else:
        e=s+b
    cnt=0
    for i in range(s, e):
        if A[i]<c:
            A[i]=c
        else:
            cnt=i
            break
    if cnt>0:
        s=cnt
    else:
        s=e

print(sum(A))
Buggy code:
import heapq
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def ii(): return int(stdin.readline())
def li(): return map(int, stdin.readline().split())
N, M = tuple(li())
A = list(li())
heapq.heapify(A)
def update(pq, k, val):
    # update pq in place
    for _ in range(k):
        cur = heapq.heappop(pq)
        if cur < val:
            heapq.heappush(pq, val)
        else:
            heapq.heappush(pq, cur)
            break

ops = []
keep = dict()
for i in range(M):
    k, val = tuple(li())
    if val > keep.get(k, 0):
        keep[k] = val
        ops.append( (k, val) )

for k, val in ops:
    update(A, k, val)

print(sum(A))


Fixed code:
import heapq
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def ii(): return int(stdin.readline())
def li(): return map(int, stdin.readline().split())
N, M = tuple(li())
A = list(li())
heapq.heapify(A)
def update(pq, k, val):
    # update pq in place
    for _ in range(k):
        cur = heapq.heappop(pq)
        if cur < val:
            heapq.heappush(pq, val)
        else:
            heapq.heappush(pq, cur)
            break

ops = []
# val, k_sum
keep = dict()
for i in range(M):
    k, val = tuple(li())
    k_sum = keep.get(val, 0)
    keep[val] = k_sum + k

for val, k in keep.items():
    update(A, k, val)

print(sum(A))
Buggy code:
n,m = map(int,input().split())
A = [int(i) for i in input().split()]
A.sort()
CB = []
for i in range(m):
   b,c = map(int,input().split())
   CB.append([c,b])
CB.sort(reverse=True)
ans = sum(A)
index = 0
for c,b in CB:
    for i in range(b):
        if index >=n:
            print(ans)
            exit()
        if A[index] < c:
            ans += c-A[index]
            A[index] = c
            index += 1
        else:
            print(ans)
            exit()



Fixed code:
n,m = map(int,input().split())
A = [int(i) for i in input().split()]
A.sort()
CB = []
for i in range(m):
   b,c = map(int,input().split())
   CB.append([c,b])
CB.sort(reverse=True)
ans = sum(A)
index = 0
for c,b in CB:
    for i in range(b):
        if index >=n:
            print(ans)
            exit()
        if A[index] < c:
            ans += c-A[index]
            A[index] = c
            index += 1
        else:
            print(ans)
            exit()
print(ans)
Buggy code:
import copy
N,M = map(int,input().split())
A = [int(i) for i in input().split()]
A.sort()
A_copy = copy.deepcopy(A)
CB = []
for i in range(M):
    b,c = map(int,input().split())
    CB.append([c,b])
CB.sort(reverse = True)
tmp = 0
for c,b in CB:
    for i in range(b):
        if A_copy[tmp+i]<=c:
            A[tmp+i] = c
        else:
            print(sum(A))
            exit()
    tmp += b

Fixed code:
import copy
N,M = map(int,input().split())
A = [int(i) for i in input().split()]
A.sort()
A_copy = copy.deepcopy(A)
CB = []
for i in range(M):
    b,c = map(int,input().split())
    CB.append([c,b])
CB.sort(reverse = True)
tmp = 0
for c,b in CB:
    for i in range(b):
        if tmp+i >= N:
            print(sum(A))
            exit()
            
        if A_copy[tmp+i]<c:
            A[tmp+i] = c
        else:
            break
    tmp += b
print(sum(A))
Buggy code:
from operator import itemgetter
n,m=map(int,input().split())
A=list(map(int,input().split()))
l=[]
A.sort()

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

l.sort(key=itemgetter(1),reverse=True)
# print(l)

print(A)
print(l)
x=0
for i in range(m):
    b=l[i][0]
    c=l[i][1]
    while(x<n and b>0):
        if(A[x]<c):
            A[x]=c
            b-=1
        x+=1
    if(x==n):
        break
print(sum(A))
        




Fixed code:
from operator import itemgetter
n,m=map(int,input().split())
A=list(map(int,input().split()))
l=[]
A.sort()

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

l.sort(key=itemgetter(1),reverse=True)
# print(l)

# print(A)
# print(l)
x=0
for i in range(m):
    b=l[i][0]
    c=l[i][1]
    while(x<n and b>0):
        if(A[x]<c):
            A[x]=c
            b-=1
        x+=1
    if(x==n):
        break
print(sum(A))
Buggy code:
n, m = (int(i) for i in input().split())
a = [int(i) for i in input().split()]
bc = []
for i in range(m):
  tmp = [int(i) for i in input().split()]
  bc.append(tmp[::-1])

for i in range(n):
  tmp = [a[i]]
  tmp.append(1)
  bc.append(tmp)
  
bc.sort()
bc = bc[::-1]
print(bc)

ans = 0
i = 0
count = 0
while count < n:
  if bc[i][1] == 0:
    i += 1
  ans += bc[i][0]
  bc[i][1] -= 1
  count += 1
  
print(ans)


Fixed code:
n, m = (int(i) for i in input().split())
a = [int(i) for i in input().split()]
bc = []
for i in range(m):
  tmp = [int(i) for i in input().split()]
  bc.append(tmp[::-1])

for i in range(n):
  tmp = [a[i]]
  tmp.append(1)
  bc.append(tmp)
  
bc.sort()
bc = bc[::-1]
#print(bc)

ans = 0
i = 0
count = 0
while count < n:
  if bc[i][1] == 0:
    i += 1
  ans += bc[i][0]
  bc[i][1] -= 1
  count += 1
  
print(ans)
Buggy code:
#python 3.5.2

N, M = [int(i) for i in input().split()]
As = [int(i) for i in input().split()]
BCs = [[int(j) for j in input().split()] for i in range(M)]
BCs += [[1, a] for a in As]
BCs = sorted(BCs, key=lambda x: x[1], reverse=True)
cnt = 0
sum = 0
print(BCs)
for b, c in BCs:
    print(b, c)
    if N < cnt + b:
        sum += (N - cnt)*c
        break
    else:
        sum += b*c
        cnt += b
print(sum)

Fixed code:
#python 3.5.2

N, M = [int(i) for i in input().split()]
As = [int(i) for i in input().split()]
BCs = [[int(j) for j in input().split()] for i in range(M)]
BCs += [[1, a] for a in As]
BCs = sorted(BCs, key=lambda x: x[1], reverse=True)
cnt = 0
sum = 0
for b, c in BCs:
    if N < cnt + b:
        sum += (N - cnt)*c
        break
    else:
        sum += b*c
        cnt += b
print(sum)
Buggy code:
n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [[int(i) for i in input().split()] for i in range(m)]

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

a.sort()

a_count = 0
for b, c in bc:
        if c > a[a_count]:
                for i in range(b):
                        if c > a[a_count]:
                                a[a_count] = c
                                a_count += 1
                        else:
                                break
        else:
                break

        if a_count > len(bc):
                break


print(sum(a))

Fixed code:
n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [[int(i) for i in input().split()] for i in range(m)]

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

a.sort()

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

        else:
                break
        if a_count == n:
                break        

print(sum(a))