qyliang/CodeNanoFix huggingface dataset:
Buggy code:
import bisect
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], reverse=True)

print(bc)
# print(a)
tmp=[]
count=0
for i in range(m):
    for j in range(bc[i][0]):
        if count==n:
            break
        tmp.append(bc[i][1])
        count+=1


change=tmp[:n]
# print(tmp)
# print(change)

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

Fixed code:
import bisect
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], reverse=True)

# print(bc)
# print(a)
tmp=[]
count=0
for i in range(m):
    for j in range(bc[i][0]):
        if count==n:
            break
        tmp.append(bc[i][1])
        count+=1


change=tmp[:n]
# print(tmp)
# print(change)

for i in range(len(change)):
    if change[i]>a[i]:
        a[i]=change[i]
    else:
        break
print(sum(a))
Buggy code:

n, m = map(int,input().split())
l = list(map(int,input().split()))
s = [list(map(int,input().split())) for i in range(m)]
# n,m = 3,2
# l = [5,1,4]
# s = [[2,3],[1,5]]
# l = [1, 8, 5, 7, 100, 4, 52, 33, 13, 5]
l.sort()
s.sort(key=lambda x: x[1], reverse=True)
idx = 0
for i in range(m):
    for j in range(idx, min(n, idx + s[i][0])):
        if l[j] < s[i][1]:
            l[j] = s[i][1]
        else:
            idx += s[i][0]
            break
print(sum(l))

Fixed code:
n, m = map(int,input().split())
l = list(map(int,input().split()))
s = [list(map(int,input().split())) for i in range(m)]
# n,m = 3,2
# l = [5,1,4]
# s = [[2,3],[1,5]]
# l = [1, 8, 5, 7, 100, 4, 52, 33, 13, 5]
l.sort()
s.sort(key=lambda x: x[1], reverse=True)
idx = 0
for i in range(m):
    for j in range(idx, min(n, idx + s[i][0])):
        if l[j] < s[i][1]:
            l[j] = s[i][1]
    idx += s[i][0]
        # else:
        #     idx += s[i][0]
        #     break
print(sum(l))
Buggy code:
N,M = map(int, input().split())
listA = list(map(int, input().split()))
listA.sort()
 
listBC = [list(map(int, input().split())) for i in range(M)]
listBC = sorted(bc, key=lambda x: x[1], reverse=True)
countB = 0
 
for i in range(M):
  B, C = listBC[i][0], listBC[i][1]
  listA += [C]*B
  countB += B
  if countB >= N:
    break

listA.sort(reverse=True)
print(sum(l[:N]))

Fixed code:
N,M = map(int, input().split())
listA = list(map(int, input().split()))
listA.sort()
 
listBC = [list(map(int, input().split())) for i in range(M)]
listBC = sorted(listBC, key=lambda x: x[1], reverse=True)
countB = 0
 
for i in range(M):
  B, C = listBC[i][0], listBC[i][1]
  listA += [C]*B
  countB += B
  if countB >= N:
    break

listA.sort(reverse=True)
print(sum(listA[:N]))
Buggy code:
from collections import defaultdict
inf = input().split()
N = int(inf[0])
W = int(inf[1])
dic = defaultdict(int)

line = input().split()

for a in line:
    dic[int(a)]= 1

for w in range(W):
    inf = input().split()
    b = int(inf[0])
    c = int(inf[1])
    dic[c] += b
    
keys = list(dic.keys())

keys.sort(reverse=True)

count = 0
ans = 0
for k in keys:
    while dic[k]>0:
        if count == N:
            break
        ans += k
        dic[k] -= 1
        count += 1
    if count == N:
        break

print(ans)

Fixed code:
from collections import defaultdict
inf = input().split()
N = int(inf[0])
W = int(inf[1])
dic = defaultdict(int)

line = input().split()

for a in line:
    dic[int(a)] += 1

for w in range(W):
    inf = input().split()
    b = int(inf[0])
    c = int(inf[1])
    dic[c] += b
    
keys = list(dic.keys())

keys.sort(reverse=True)

count = 0
ans = 0
for k in keys:
    while dic[k]>0:
        if count == N:
            break
        ans += k
        dic[k] -= 1
        count += 1
    if count == N:
        break

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

a.sort()

pairs = [list(map(int, input().split())) for _ in range(m)]
pairs = sorted(pairs, key=lambda x: x[1])
cnt = 0
ans = 0
while cnt < n:
    if pairs != [] and a[-1] < pairs[-1][1] and cnt+pairs[-1][0] <= n:
        cnt += pairs[-1][0]
        ans += pairs[-1][0]*pairs[-1][1]
        pairs.pop(-1)
    else:
        cnt+= 1
        ans += a[-1]
        a.pop(-1)

print(ans)
 

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

a.sort()

pairs = [list(map(int, input().split())) for _ in range(m)]
pairs = sorted(pairs, key=lambda x: x[1])
cnt = 0
ans = 0
while cnt < n:
    if pairs != [] and a[-1] < pairs[-1][1] and cnt+pairs[-1][0] <= n:
        cnt += pairs[-1][0]
        ans += pairs[-1][0]*pairs[-1][1]
        pairs.pop(-1)
    elif pairs != [] and a[-1] < pairs[-1][1] and cnt+pairs[-1][0] > n:
        ans += (n - cnt)*pairs[-1][1]
        cnt += n - cnt
        break
    else:
        cnt+= 1
        ans += a[-1]
        a.pop(-1)

print(ans)
Buggy code:
N, M = map(int, input().split())
from  bisect import bisect_right
inf = float("inf")
A = sorted(list(map(int, input().split())))
BC = sorted([tuple(map(int, input().split())) for _ in range(M)], key=lambda x: -x[1])
cnt = 0
L, R = 0, N
I = []
for b, c in BC:
    idx = bisect_right(A, c, lo=L, hi=R)
    idx -= L
    if idx == 0:
        I.append((L, R, 0, c))
        break
    elif idx < b:
        I.append((L+idx, R, idx, c))
        break
    elif idx >= b:
        I.append((L+idx, R, b, c))
        tmp = L
        L += b
        R = tmp + idx     

    cnt += 1
    if cnt == M:
        I.append((L, R, 0, c))

S = [0]
for a in A:
    S.append(a+S[-1])
ans = 0
for L, R, x, c in I:
    print(L, R)
    ans += S[R] - S[L] + x*c
    
print(ans)




Fixed code:
N, M = map(int, input().split())
from  bisect import bisect_right
inf = float("inf")
A = sorted(list(map(int, input().split())))
BC = sorted([tuple(map(int, input().split())) for _ in range(M)], key=lambda x: -x[1])
cnt = 0
L, R = 0, N
I = []
for b, c in BC:
    idx = bisect_right(A, c, lo=L, hi=R)
    idx -= L
    if idx == 0:
        I.append((L, R, 0, c))
        break
    elif idx < b:
        I.append((L+idx, R, idx, c))
        break
    elif idx >= b:
        I.append((L+idx, R, b, c))
        tmp = L
        L += b
        R = tmp + idx     

    cnt += 1
    if cnt == M:
        I.append((L, R, 0, c))

S = [0]
for a in A:
    S.append(a+S[-1])
ans = 0
for L, R, x, c in I:
    ans += S[R] - S[L] + x*c

print(ans)
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)]
bc = sorted(bc, key = lambda x : x[1], reverse=True)

sum_b = 0
for b, c in bc:
    sum_b += b
    a += [c] * b
    print(a)
    if n < sum_b:
        break
a.sort(reverse=True)
a = a[:n]

print(sum(a))

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)]
bc = sorted(bc, key = lambda x : x[1], reverse=True)

sum_b = 0
for b, c in bc:
    sum_b += b
    a += [c] * b
    if n < sum_b:
        break
a.sort(reverse=True)
a = a[:n]

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

n, m = map(int, input().split())
a = list(map(int, input().split()))
q = []
for x in a:
  heappush(q, [-x, 1])
for i in range(m):
  b, c = map(int, input().split())
  heappush(q, [-c, b])
r = []
k = 0
while k < n:
  c, b = heappop(q)
  c = -c
  r += [c] * b
  k += b
print(sum(r[-n:]))


Fixed code:
from heapq import heappop, heappush

n, m = map(int, input().split())
a = list(map(int, input().split()))
q = []
for x in a:
  heappush(q, [-x, 1])
for i in range(m):
  b, c = map(int, input().split())
  heappush(q, [-c, b])
r = []
k = 0
while k < n:
  c, b = heappop(q)
  c = -c
  r += [c] * b
  k += b
print(sum(r[:n]))
Buggy code:
N, M = map(int, input().split())
As = list(map(int, input().split()))
ones = [1 for _ in range(N)]
for _ in range(M):
  b, c = map(int, input().split())
  Bs.append(b)
  Cs.append(c)
both = list(zip(As, ones))
both.append(list(zip(Cs, Bs)))
both.sort()
a, b = zip(*both)
cnt = 0
out = 0
for k in reversed(range(N+M)):
  if N - cnt > b[k]:
    out += a[k] * b[k]
    cnt += b[k]
  else:
    out += a[k] * (N - cnt)
    break
print(out)

Fixed code:
N, M = map(int, input().split())
As = list(map(int, input().split()))
ones = [1 for _ in range(N)]
Bs = []
Cs = []
both = list(zip(As, ones))
for _ in range(M):
  b, c = map(int, input().split())
  both.append((c, b))
both.sort()
a, b = zip(*both)
cnt = 0
out = 0
for k in reversed(range(N+M)):
  if N - cnt > b[k]:
    out += a[k] * b[k]
    cnt += b[k]
  else:
    out += a[k] * (N - cnt)
    break
print(out)
Buggy code:
import sys
input = sys.stdin.readline
import bisect
n,m = map(int,input().split())
A = list(map(int,input().split()))
A.sort()

minA = A[0]
Query = []
for _ in range(m):
    b,c = map(int,input().split())
    if c > minA:
        Query.append([b,c])
#Query = sorted(Query, key=lambda x:(x[1], x[0]), reverse=True)

ans = 0
index = 0
for b,c in Query:
    if index >= n:
        break
    #print("----")
    cnt = 0
    Flag = True
    while Flag:
        #print(A,b,c,index)
        if A[index] < c:
            A[index] = c
            cnt += 1
            index += 1
            if (cnt >= b) or (index >= n):
                Flag = False
        else:
            Flag = False
    '''
    #A.sort()
    if A[0] >= c:
        break
    else:
        index = min(bisect.bisect_left(A,c),b)
        if index > len(A):
            ans += c * len(A)
            break
        else:
            ans += c * index
            A = A[index:]
    print(A)
    '''
print(sum(A))

Fixed code:
import sys
input = sys.stdin.readline
import bisect
n,m = map(int,input().split())
A = list(map(int,input().split()))
A.sort()

minA = A[0]
Query = []
for _ in range(m):
    b,c = map(int,input().split())
    if c > minA:
        Query.append([b,c])
Query = sorted(Query, key=lambda x:(x[1], x[0]), reverse=True)

ans = 0
index = 0
for b,c in Query:
    if index >= n:
        break
    #print("----")
    cnt = 0
    Flag = True
    while Flag:
        #print(A,b,c,index)
        if A[index] < c:
            A[index] = c
            cnt += 1
            index += 1
            if (cnt >= b) or (index >= n):
                Flag = False
        else:
            Flag = False
    '''
    #A.sort()
    if A[0] >= c:
        break
    else:
        index = min(bisect.bisect_left(A,c),b)
        if index > len(A):
            ans += c * len(A)
            break
        else:
            ans += c * index
            A = A[index:]
    print(A)
    '''
print(sum(A))
Buggy code:
n, m = map(int, input().split())
a = sorted(list(map(int, input().split())), reverse = True)
b = [list(map(int, input().split())) for i in range(m)]
b.sort(key=lambda x: -x[1])

ct1 = 0
ct2 = 0
ans = 0
i = 0

while i in range(n):
    if a[ct1] > b[ct2][1]:
        ans += a[ct1]
        ct1 += 1
        i += 1
    else:
        j = min(n-i, b[ct2][0])
        ans += b[ct2][1] * j
        i += j
        ct2 += 1

print(ans)


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

ct1 = 0
ct2 = 0
ans = 0
i = 0

while i in range(n):
    if ct2 < m:
        if a[ct1] < b[ct2][1]:
            j = min(n-i, b[ct2][0])
            ans += b[ct2][1] * j
            i += j
            ct2 += 1
        else:
            ans += a[ct1]
            ct1 += 1
            i += 1
    else:
        ans += a[ct1]
        ct1 += 1
        i += 1
print(ans)
Buggy code:
import heapq as hq
import numpy as np
N, M = map(int, input().split())
A = list(map(int,input().split()))
A = [(-i, 1) for i in A]

for i in range(M):
    B, C = map(int, input().split())
    hq.heappush(A,(-C, B))
sum_A = 0
for i in range(N):
    add_num = hq.heappop(A)
    sum_A += add_num[0]*-1
    if add_num[1] > 1:
        hq.heappush(A,(add_num[0],add_num[1]-1))
print(sum_A)
    
    


Fixed code:
import heapq as hq
N, M = map(int, input().split())
A = list(map(int,input().split()))
A = [(-i,1) for i in A]
hq.heapify(A)
for i in range(M):
    B, C = map(int, input().split())
    hq.heappush(A,(-C,B))
sum_A = 0
for i in range(N):
    add_num = hq.heappop(A)
    sum_A += add_num[0]*-1
    if add_num[1] > 1:
        hq.heappush(A,(add_num[0],add_num[1]-1))
print(sum_A)
Buggy code:
n,m = map(int,input().split())
a_array = [int(x) for x in input().split()]
d = dict()
for a in a_array:
    d.setdefault(a, 0)
    d[a] += 1
for _ in range(m):
    b,c = map(int, input().split())
    d.setdefault(c, 0)
    d[c] += b
key = sort(d.keys(),reverse=True)
ans = 0
index = 0
while n > 0:
    num = key[index]
    if d[num] >= n:
        ans += num * n
        break
    ans += num * d[num]
    n -= d[num]
    index += 1

print(ans)

Fixed code:
n,m = map(int,input().split())
a_array = [int(x) for x in input().split()]
d = dict()
for a in a_array:
    d.setdefault(a, 0)
    d[a] += 1
for _ in range(m):
    b,c = map(int, input().split())
    d.setdefault(c, 0)
    d[c] += b
key = sorted(d.keys(),reverse=True)
ans = 0
index = 0
while n > 0:
    num = key[index]
    if d[num] >= n:
        ans += num * n
        break
    ans += num * d[num]
    n -= d[num]
    index += 1

print(ans)
Buggy code:
import heapq
import sys
N,M=map(int,input().split())
A=sorted(list(map(int,input().split())))
lst=[[0,0] for _ in range(M)]
h=[]
for i in range(M):
  lst[i]=list(map(int,input().split()))
  heapq.heappush(h,[-1*lst[i][1],lst[i][0]])

tmp=0
while len(h)!=0:
  v=heapq.heappop(h)
  v1,v2=-1*v[0],v[1]
  if A[0]>v1:
    print(sum(A))
    sys.exit()
  else:
    while v2!=0 and A[0]<v1:
      A.pop(0)
      tmp+=v1
      v2-=1
    if v2>=1:
      print(sum(A)+tmp)
      sys.exit()
print(sum(A)+tmp)

Fixed code:
import heapq
import sys
N,M=map(int,input().split())
A=sorted(list(map(int,input().split())))
lst=[[0,0] for _ in range(M)]
h=[]
for i in range(M):
  lst[i]=list(map(int,input().split()))
  heapq.heappush(h,[-1*lst[i][1],lst[i][0]])
i=0
while len(h)!=0:
    v=heapq.heappop(h)
    v1,v2=-1*v[0],v[1]
    while i<=N-1 and A[i]<v1 and v2!=0:
        A[i]=v1
        i+=1
        v2-=1
    if v2>=1:
        print(sum(A))
        sys.exit()
print(sum(A))
Buggy code:
import bisect
import sys
input = sys.stdin.readline
def main():
    N,M = map(int,input().split())
    A= list(map(int,input().split()))
    A = sorted(A)

    BC=[list(map(int,input().split())) for i in range(M)]
    BC = sorted(BC, key=lambda x: x[1],reverse=True)
    index=0
    
    for B,C in BC:
        temp = [C]*B
        for t in temp:
            if t>A[index]:
                A[index] = t
            else:
                return A
            index+=1
    
if __name__ == '__main__':
    A=main()
    print(sum(A))


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

    BC=[list(map(int,input().split())) for i in range(M)]
    BC = sorted(BC, key=lambda x: x[1],reverse=True)
    index=0
    max_index=len(A)
    
    for B,C in BC:
        temp = [C]*B
        for t in temp:
            if max_index>index and  t>A[index]:
                A[index] = t
            else:
                return A
            index+=1
    return A
    
if __name__ == '__main__':
    A=main()
    print(sum(A))
Buggy code:
n,m = map(int,input().split())
a = list(map(int,input().split()))
bc = list(list(map(int,input().split())) for i in range(m))

a.sort()
print(a)
bc.sort(reverse=True,key=lambda x:x[1])
t = 0
for i in range(m):
    for j in range(bc[i][0]):
        if a[t] < bc[i][1]:
            a[t] = bc[i][1]
            t+=1
        else:
            break

print(sum(a))


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

a.sort()
bc.sort(reverse=True,key=lambda x:x[1])
t = 0
for i in range(m):
    for j in range(bc[i][0]):
        if t != n:
            if a[t] < bc[i][1]:
                a[t] = bc[i][1]
                t+=1
            else:
                break
        else:
            break
    else:
        continue
    break

print(sum(a))
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 bc: bc[1], reverse=True)
update_bc = [0 for _ in range(N)]
cnt = 0
for b, c in BC:
    for i in range(b):
        if cnt < N:
            update_bc[cnt] = c
            cnt += 1
bc_max = min(N, len(BC))
for i in range(bc_max):
    if A[i] < update_bc[i]:
        A[i] = update_bc[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 bc: bc[1], reverse=True)
update_bc = [0 for _ in range(N)]
cnt = 0
for b, c in BC:
    for i in range(b):
        if cnt < N:
            update_bc[cnt] = c
            cnt += 1
        else:
            break
    if cnt >= N:
        break
for i in range(N):
    if A[i] < update_bc[i]:
        A[i] = update_bc[i]
    else:
        break
print(sum(A))
Buggy code:
n,m=map(int,input().split())
a=sorted(map(int,input().split()))
l=list()
for i in range(m):
    l.append(list(map(int,input().split())))
l.sort(key=lambda x:-x[1])
i=0
for b,c in l:
    for j in range(b):
        if i+j<n and a[i+j]<c:
            a[i+j]=c
        else:
            exit()
    i+=b
print(sum(a))

Fixed code:
n,m=map(int,input().split())
a=sorted(map(int,input().split()))
l=list()
for i in range(m):
    l.append(list(map(int,input().split())))
l.sort(key=lambda x:-x[1])
i=0
for b,c in l:
    for j in range(b):
        if i+j<n and a[i+j]<c:
            a[i+j]=c
        else:
            print(sum(a))
            exit()
    i+=b
print(sum(a))
Buggy code:
def input_from_console():
    n, m = map(int, input().split())
    a_list = map(int, input().split())

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


def solve(n, m, a_list, bc_list):
    bc_list.sort(key=lambda x: x[1], reverse=True)
    a_list = sorted(a_list)

    final_list = []
    result_sum = 0
    position = 0
    a_length = len(a_list)

    while position < a_length:
        for item in bc_list:
            b, c = item
            for i in range(b):
                if position >= a_length or a_list[position] >= c:
                    position = a_length
                    return sum(a_list)
                else:
                    a_list[position] = c
                    position += 1
    # print(a_list)
    return sum(a_list)


def main():
    n, m, a_list, bc_list = input_from_console()
    print(solve(n, m, a_list, bc_list))


if __name__ == "__main__":
    main()


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

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


def solve(n, m, a_list, bc_list):
    bc_list.sort(key=lambda x: x[1], reverse=True)
    a_list = sorted(a_list)

    position = 0
    a_length = len(a_list)

    for item in bc_list:
        b, c = item
        for i in range(b):
            if position >= a_length or a_list[position] >= c:
                return sum(a_list)
            else:
                a_list[position] = c
                position += 1
    return sum(a_list)


def main():
    n, m, a_list, bc_list = input_from_console()
    print(solve(n, m, a_list, bc_list))


if __name__ == "__main__":
    main()
Buggy code:
n, m = map(int, input().split())
a=sorted(list(map(int, input().split())), reverse = True)
bc = sorted([list(map(int, input().split())) for _ in range(m)], key = lambda x:x[1], reverse = True)
i=0
j=0
cnt=0
ans=0
while True:
    if a[i]<bc[j][1]:
        ans+=min(n-cnt, bc[j][0])*bc[j][1]
        cnt+=min(n-cnt, bc[j][0])
        j+=1
    else:
        ans+=a[i]
        i+=1
        cnt+=1
    if cnt==n:
        break
print(ans)
    

Fixed code:
n, m = map(int, input().split())
a=sorted(list(map(int, input().split())), reverse = True)
bc = sorted([list(map(int, input().split())) for _ in range(m)], key = lambda x:x[1], reverse = True)
i=0
j=0
cnt=0
ans=0
while True:
    if j<m and a[i]<bc[j][1]:
        ans+=min(n-cnt, bc[j][0])*bc[j][1]
        cnt+=min(n-cnt, bc[j][0])
        j+=1
    else:
        ans+=a[i]
        i+=1
        cnt+=1
    if cnt==n:
        break
print(ans)
Buggy code:
n, m = map(int, input().split())
a = sorted(list(map(int, input().split())))
bc = [list(map(int, input().split())) for i in range(m)]
sort_bc = sorted(bc, key = lambda x:x[1], reverse = True)
cnt_a = 0
cnt_m = 0
print(sort_bc)
for i in sort_bc:
    if cnt_a <= n - 1:
        for j in range(i[0]):
            if cnt_a <= n - 1:
                if i[1] > a[cnt_a]:
                    a[cnt_a] = i[1]
                    cnt_a += 1
                else:
                    break
    else:
        break
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 i in range(m)]
sort_bc = sorted(bc, key = lambda x:x[1], reverse = True)
cnt_a = 0
cnt_m = 0
for i in sort_bc:
    if cnt_a <= n - 1:
        for j in range(i[0]):
            if cnt_a <= n - 1:
                if i[1] > a[cnt_a]:
                    a[cnt_a] = i[1]
                    cnt_a += 1
                else:
                    break
    else:
        break
print(sum(a))
Buggy code:
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [[] for _ in range(M)]
for i in range(M):
  BC[i] = list(map(int, input().split()))

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

for i in BC:
  A.extend([i[1]]*i[0])
  if len(A) > N:
    break
    
A = sorted(A,reverse=True)
print(sum(A[:N]))



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

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

for i in BC:
  A.extend([i[1]]*i[0])
  if len(A) > 2*N:
    break
    
A = sorted(A,reverse=True)
print(sum(A[:N]))
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)]
li = []
cnt = 0
for b, c in sorted(BC, key=lambda x:x[1], reverse=True):
  cnt += b
  if cnt >= N:
    break
  for _ in range(b):
    li.append(c)
for i in range(min(N,cnt)):
  if A[i] < li[i]:
    A[i] = li[i]
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)]
li = []
cnt = 0
for b, c in sorted(BC, key=lambda x:x[1], reverse=True):
  cnt += b
  for _ in range(b):
    li.append(c)
  if cnt >= N:
    break
for i in range(min(N,cnt)):
  if A[i] < li[i]:
    A[i] = li[i]
print(sum(A))