qyliang/CodeNanoFix huggingface dataset:
Buggy code:
import bisect

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

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

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

ans, i = 0, 0
for bc in BC:
    ti = bisect.bisect(A, bc[1]) + i
    if (ti > i) and (bc[0] >= (ti - i)):
        ans += bc[1] * (ti - i)
        i = ti
    elif (ti > i) and (bc[0] < (ti - i)):
        ans += bc[1] * bc[0]
        i = i + bc[0]

print(ans + sum(A[i:]))


Fixed code:
import bisect

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

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

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

ans, i = 0, 0
for bc in BC:
    ti = bisect.bisect(A, bc[1])
    if (ti > i) and (bc[0] >= (ti - i)):
        ans += bc[1] * (ti - i)
        i = ti
    elif (ti > i) and (bc[0] < (ti - i)):
        ans += bc[1] * bc[0]
        i = i + bc[0]

print(ans + sum(A[i:]))
Buggy code:
N,M = map(int,input().split())
A = list(map(int,input().split()))
CB = []
for i in range(0,M,1):
    b,c = map(int,input().split())
    CB.append([c,b])
A.sort()
CB.sort(reverse=True)

L = 0
ans = 0
owaowari = False
for i in range(0,M,1):
    print(i)
    for j in range(L,min(N,L+CB[i][1]),1):
        if A[j] < CB[i][0]:
            L = j+1
            A[j]=CB[i][0]
        else:
            owaowari = True
    if owaowari or L > N-1:
        break
print(sum(A))

Fixed code:
N,M = map(int,input().split())
A = list(map(int,input().split()))
CB = []
for i in range(0,M,1):
    b,c = map(int,input().split())
    CB.append([c,b])
A.sort()
CB.sort(reverse=True)

L = 0
ans = 0
owaowari = False
for i in range(0,M,1):
    for j in range(L,min(N,L+CB[i][1]),1):
        if A[j] < CB[i][0]:
            L = j+1
            A[j]=CB[i][0]
        else:
            owaowari = True
    if owaowari or L > N-1:
        break
print(sum(A))
Buggy code:
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
bc = [map(int, input().split()) for _ in range(m)]

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

i = 0
for b,c in bc:
    for j in range(i, min(n, i+b)):
        if a[j]<c:
            a[j] = c
    i = i+b
print(sum(a))


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

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

i = 0
for b,c in bc:
    for j in range(i, min(n, i+b)):
        if a[j]<c:
            a[j] = c
    i = i+b
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)]
lenA = len(A)
minA = min(A)
BC.sort(reverse = True, key = lambda x:x[1])

i = 0
cnt = 0
while cnt < lenA:
  A.extend([BC[i][1] for _ in range(BC[i][0])])
  cnt += BC[i][0]
  i += 1
A.sort(reverse = True)
l = A[0:lenA]

print(sum(l))

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

i = 0
cnt = 0
while cnt < lenA and i < M:
  A.extend([BC[i][1] for _ in range(BC[i][0])])
  cnt += BC[i][0]
  i += 1
A.sort(reverse = True)
l = A[0:lenA]

print(sum(l))
Buggy code:
from operator import itemgetter
n,m = map(int,input().split())
*a,= map(int,input().split())
l = [tuple(map(int,input().split()))for _ in range(m)]
a.sort()
l.sort(key = itemgetter(1), reverse = True)
cmp = []
cnt = 0
for b,c in l:
	cmp += [c]*b
	cnt += b
	if cnt >= n:
		cnt = n-1
		break
cut = cnt
print(a,cmp)
for i,(x,y) in enumerate(zip(a,cmp[:cnt+1])):
	if x > y:
		cut = i
		break
print(sum(cmp[:cut] + a[cut:]))

Fixed code:
from operator import itemgetter
n,m = map(int,input().split())
*a,= map(int,input().split())
l = [tuple(map(int,input().split()))for _ in range(m)]
a.sort()
l.sort(key = itemgetter(1), reverse = True)
cmp = []
cnt = 0
for b,c in l:
	cmp += [c]*b
	cnt += b
	if cnt >= n:
		cnt = n
		break
cut = cnt
for i,(x,y) in enumerate(zip(a,cmp[:min(n,cnt+1)])):
	if x > y:
		cut = i
		break
print(sum(cmp[:cut] + a[cut:]))
Buggy code:
import bisect

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

for _ in range(m):
    l.append(list(map(int, input().split())))

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

focus = 0
for b, c in l:
    for j in range(focus, focus+b):
        a[j] = max(c, a[j])
    focus += b

print(sum(a))


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

for _ in range(m):
    l.append(list(map(int, input().split())))

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

focus = 0
for b, c in l:
    for j in range(focus, min(focus+b, n)):
        a[j] = max(c, a[j])
    focus += b

print(sum(a))
Buggy code:
n,m=map(int,input().split())
from collections import Counter as co
d=co(list(map(int,input().split())))
for i in range(m):
    b,c=map(int,input().split())
    if c in d:d[c]+=b
    else:d[c]=b
f=sorted(d.items(),reverse=1)
print(f)
now =0
s=0
for k,v in f:
    if now+v<=n:
        now+=v
        s+=k*v
    else:
        
        print(s+(n-now)*k);exit()

Fixed code:
n,m=map(int,input().split())
from collections import Counter as co
d=co(list(map(int,input().split())))
for i in range(m):
    b,c=map(int,input().split())
    if c in d:d[c]+=b
    else:d[c]=b
f=sorted(d.items(),reverse=1)

now =0
s=0
for k,v in f:
    if now+v<=n:
        now+=v
        s+=k*v
    else:
        
        print(s+(n-now)*k);exit()
Buggy code:
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
T = []
for i in range(M):
    a,b = map(int,input().split())
    T.append([b,a])
T.sort(reverse=True)

ans = 0
c = 0
k = 0
t = len(T)
for i in range(N):
    if c == t:
        break
    n = T[c][0]
    l = T[c][1]
    k += 1
    if A[i] < n:
        A[i] = n
    else:
        break
    if k == l:
        c += 1
        k = 0
print(A)
print(sum(A))

Fixed code:
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
T = []
for i in range(M):
    a,b = map(int,input().split())
    T.append([b,a])
T.sort(reverse=True)

ans = 0
c = 0
k = 0
t = len(T)
for i in range(N):
    if c == t:
        break
    n = T[c][0]
    l = T[c][1]
    k += 1
    if A[i] < n:
        A[i] = n
    else:
        break
    if k == l:
        c += 1
        k = 0
print(sum(A))
Buggy code:
import sys
input = sys.stdin.readline
from operator import itemgetter

def solve():
    cardsNum, turnsNum = tuple(map(int, input().split()))
    cards = sorted([int(x) for x in input().split()])
    turns = sorted([tuple(int(x) for x in input().split()) for _ in range(turnsNum)], key=itemgetter(1), reverse=True)
    
    largeCount = 0
    largeCards = []
    for chances, num in turns:
        for _ in range(num):
            if (largeCount < cardsNum):
                largeCards.append(num)
                largeCount += 1
            else:
                break
        if (largeCount >= cardsNum):
            break 
    
    for i in range(cardsNum):
        if (largeCards[i] > cards[i]):
            cards[i] = largeCards[i]
        else:
            break
    print(sum(cards))
solve()

Fixed code:
import sys
input = sys.stdin.readline
from operator import itemgetter

def solve():
    cardsNum, turnsNum = tuple(map(int, input().split()))
    cards = sorted([int(x) for x in input().split()])
    turns = sorted([tuple(int(x) for x in input().split()) for _ in range(turnsNum)], key=itemgetter(1), reverse=True)
    
    largeCount = 0
    largeCards = [0,] * cardsNum
    for chances, num in turns:
        for _ in range(chances):
            if (largeCount < cardsNum):
                largeCards[largeCount] = num
                largeCount += 1
            else:
                break
        if (largeCount >= cardsNum):
            break 
    
    for i in range(cardsNum):
        if (largeCards[i] > cards[i]):
            cards[i] = largeCards[i]
        else:
            break
    print(sum(cards))
solve()
Buggy code:

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

abc = [(A[i],1) for i in range(N)]

for j in range(M):
    B, C = map(int, input().split())
    abc.append((C,B))
    
abc.sort()
abc.reverse()

ans = 0
cnt = N

for (c,b) in abc:
    num_c = min(b,cnt)
    ans += num_c * c
    cnt -= num_c

print(ans)

Fixed code:
N, M = map(int,input().split())
A = [int(a) for a in input().split()]

abc = [(A[i],1) for i in range(N)]

for j in range(M):
    B, C = map(int, input().split())
    abc.append((C,B))
    
abc.sort()
abc.reverse()

ans = 0
cnt = N

for (c,b) in abc:
    num_c = min(b,cnt)
    ans += num_c * c
    cnt -= num_c

print(ans)
Buggy code:
n,m = map(int,input().split())
a = list(map(int,input().split()))
lis = []
for i in range(m):
    wa = list(map(int,input().split()))
    lis.append(wa)
a.sort()
lis = sorted(lis, key=lambda x:x[1], reverse=True)
cnt = 0
for i in range(m):
    b,c = lis[i]
    for j in range(b):
        if c > a[cnt]:
            a[cnt] = c
        else:
            print(sum(a))
            exit()
        cnt += 1
        if cnt == n:
            print(sum(a))
            exit()

Fixed code:
n,m = map(int,input().split())
a = list(map(int,input().split()))
lis = []
for i in range(m):
    wa = list(map(int,input().split()))
    lis.append(wa)
a.sort()
lis = sorted(lis, key=lambda x:x[1], reverse=True)
cnt = 0
for i in range(m):
    b,c = lis[i]
    for j in range(b):
        if c > a[cnt]:
            a[cnt] = c
        else:
            print(sum(a))
            exit()
        cnt += 1
        if cnt == n:
            print(sum(a))
            exit()
print(sum(a))
Buggy code:
import heapq
from itertools import starmap
from operator import mul

R = lambda: map(int, input().split())
n, m = R()
a = [[x, 1] for x in R()]
heapq.heapify(a)
for _ in range(m):
    b, c = R()
    i = b
    while i and a[0][0] < c:
        x, y = a[0]
        if i < y:
            a[0][1] -= i
            i = 0
        else:
            heapq.heappop(a)
            i -= y
    if i < b:
        heapq.heappush(a, (c, b - i))
print(sum(starmap(mul, a)))


Fixed code:
import heapq
from itertools import starmap
from operator import mul

R = lambda: map(int, input().split())
n, m = R()
a = [[x, 1] for x in R()]
heapq.heapify(a)
for _ in range(m):
    b, c = R()
    i = b
    while i and a[0][0] < c:
        x, y = a[0]
        if i < y:
            a[0][1] -= i
            i = 0
        else:
            heapq.heappop(a)
            i -= y
    if i < b:
        heapq.heappush(a, [c, b - i])
print(sum(starmap(mul, a)))
Buggy code:
import heapq
from itertools import repeat, starmap
from operator import mul

R = lambda: map(int, input().split())
n, m = R()
a = list(zip(R(), repeat(1)))
heapq.heapify(a)
for _ in range(m):
    b, c = R()
    i = b
    while i:
        x, y = a[0]
        if x >= c:
            break
        if y > i:
            heapq.heapreplace(a, (x, y - i))
            break
        else:
            heapq.heappop(a)
            i -= y
    heapq.heappush(a, (c, b - i))
print(sum(starmap(mul, a)))


Fixed code:
import heapq
from itertools import repeat, starmap
from operator import mul

R = lambda: map(int, input().split())
n, m = R()
a = list(zip(R(), repeat(1)))
heapq.heapify(a)
for _ in range(m):
    b, c = R()
    i = b
    while i:
        x, y = a[0]
        if x >= c:
            break
        heapq.heappop(a)
        d = min(i, y)
        i -= d
        y -= d
        if y:
            heapq.heappush(a, (x, y))
    heapq.heappush(a, (c, b - i))
print(sum(starmap(mul, a)))
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 i in range(m)], key=lambda x:x[1], reverse=True)
ans = 0
x, y = 0, 0
for i in range(n):
    if a[x] > bc[y][1] or y == m:
        ans += a[x]
        x += 1
    else:
        ans += bc[y][1]
        bc[y][0] -= 1
        if bc[y][0] == 0:
            y += 1
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 i in range(m)], key=lambda x:x[1], reverse=True)
ans = 0
x, y = 0, 0
for i in range(n):
    if y == m or a[x] > bc[y][1]:
        ans += a[x]
        x += 1
    else:
        ans += bc[y][1]
        bc[y][0] -= 1
        if bc[y][0] == 0:
            y += 1
print(ans)
Buggy code:
N, M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
BC = [list(map(int,input().split())) for _ in range(M)]
BC.sort(key=lambda x: x[1], reverse=True)
j = 0
for i in range(N):
    if A[i] >= BC[j][1]:
        break
    else:
        A[i] = BC[j][1]
        BC[j][0] -= 1
        if BC[j][0] == 0:
            j += 1
print(sum(A))

Fixed code:
N, M = 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)
j = 0
for i in range(N):
    if A[i] >= BC[j][1]:
        break
    else:
        A[i] = BC[j][1]
        BC[j][0] -= 1
        if BC[j][0] == 0:
            j += 1
        if j >= M:
            break
print(sum(A))
Buggy code:
import heapq

n, m = map(int, input().split())
list_score = list(map(int, input().split()))
list_kard = [list(map(int, input().split(" "))) for i in range(m)]

list_kard.sort(key=lambda x: x[1], reverse=True)
heapq.heapify(list_score)

for i in range(m):
    k = list_kard[i]
    for _ in range(k[0]):
        if list_score[0] < k[1]:
            heapq.heappop(list_score)
            heapq.heappush(list_score, k[1])
        else:
            print(sum(list_score))
            exit()

Fixed code:
import heapq

n, m = map(int, input().split())
list_score = list(map(int, input().split()))
list_kard = [list(map(int, input().split(" "))) for i in range(m)]

list_kard.sort(key=lambda x: x[1], reverse=True)
heapq.heapify(list_score)

for i in range(m):
    k = list_kard[i]
    for _ in range(k[0]):
        if list_score[0] < k[1]:
            heapq.heappop(list_score)
            heapq.heappush(list_score, k[1])
        else:
            break

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

def main():
    N, M = map(int, input().split())
    A_card = [-int(x) for x in input().split()]

    change_list = []
    for i in range(M):
        B, C = [int(x) for x in input().split()]
        change_list.append(((-1) * C, B))

    hq = []
    for i in range(N):
        heappush(hq, (A_card[i], 1))

    for i in range(M):
        heappush(hq, change_list[i])

    result_sum = 0
    count = 0
    while True:
        tmp = heappop(hq)
        if count + tmp[1] <= N:
            result_sum += (tmp[0] * tmp[1])
            count += tmp[1]
        else:
            result_sum += (tmp[0] * (count + tmp[1] - N + 1))
            break

    print(result_sum * (-1))

if __name__ == "__main__":
    main()


Fixed code:
from heapq import heappush, heappop, heapify

def main():
    N, M = map(int, input().split())
    A_card = [-int(x) for x in input().split()]

    change_list = []
    for i in range(M):
        B, C = [int(x) for x in input().split()]
        change_list.append(((-1) * C, B))

    hq = []
    for i in range(N):
        heappush(hq, (A_card[i], 1))

    for i in range(M):
        heappush(hq, change_list[i])

    result_sum = 0
    count = 0
    while True:
        if count == N:
            break
        tmp = heappop(hq)
        if count + tmp[1] <= N:
            result_sum += (tmp[0] * tmp[1])
            count += tmp[1]
        else:
            result_sum += (tmp[0] * (N - count))
            break

    print(result_sum * (-1))

if __name__ == "__main__":
    main()
Buggy code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,m = inpl()
a = inpl()
a.sort()
bc = [inpl() for _ in range(m)]
bc.sort(key=lambda x:x[1], reverse = True)
ind = -1
now = -1
cnt = 0
for i in range(n):
    if cnt == 0:
        ind += 1
        cnt, now = bc[ind]
    if a[i] >= now:
        break
    a[i] = now
    cnt -= 1
print(sum(a))

Fixed code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,m = inpl()
a = inpl()
a.sort()
bc = [inpl() for _ in range(m)]
bc.sort(key=lambda x:x[1], reverse = True)
ind = -1
now = -1
cnt = 0
for i in range(n):
    if cnt == 0:
        ind += 1
        if ind >= m: break
        cnt, now = bc[ind]
    if a[i] >= now:
        break
    a[i] = now
    cnt -= 1
print(sum(a))
Buggy code:
from sys import stdin
import collections

N, M = [int(x) for x in stdin.readline().rstrip().split()]
A = [int(x) for x in stdin.readline().rstrip().split()]
A = collections.Counter(A)
for i in range(M):
    B, C = [int(x) for x in stdin.readline().rstrip().split()]
    A[C] += B 
A = dict(A)

S = 0
j = -1

A_key = list(A.keys())
A_value = list(A.values())

while True:
    if A_value[j] < N:
        S += A_value[j] * A_key[j]
        N -= A_value[j]
        j -= 1
    else:
        S += N * A_key[j]
        break
print(S)

Fixed code:
from sys import stdin
import collections

N, M = [int(x) for x in stdin.readline().rstrip().split()]
A = [int(x) for x in stdin.readline().rstrip().split()]
A = collections.Counter(A)
for i in range(M):
    B, C = [int(x) for x in stdin.readline().rstrip().split()]
    A[C] += B 
A = dict(A)
A = dict(sorted(A.items()))

S = 0
j = -1

A_key = list(A.keys())
A_value = list(A.values())

while True:
    if A_value[j] < N:
        S += A_value[j] * A_key[j]
        N -= A_value[j]
        j -= 1
    else:
        S += N * A_key[j]
        break
print(S)
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])

B=[]
for bc in BC:
    B+=[bc[1]]*bc[0]
    if len(B)>=N:
        break
B+=[0]*(N-len(B))
print(sum([max(A[i],B[i]) for i in range(N)]))


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

B=[]
for bc in BC:
    B+=[bc[1]]*bc[0]
    if len(B)>=N:
        break
B+=[0]*(N-len(B))
print(sum([max(A[i],B[i]) for i in range(N)]))
Buggy code:
from collections import deque

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

BC_dict={}
for m in range(M):
    b,c = list(map(int,input().split()))
    if c in BC_dict.keys():
        BC_dict[c]+=b  
    else:    
        BC_dict[c]=b  
    
C = list(BC_dict.keys())

A.sort()
C.sort(reverse=True)

C=deque(C)

check = C.popleft()

for a_idx,a in enumerate(A):
    if a >= check:
        break
    
    A[a_idx] = check
    BC_dict[check] -= 1
    
    if BC_dict[check] == 0:
        if len(C) == 0:
            break
        else:
            print(C)
            check = C.popleft()
        
print(sum(A))

Fixed code:
from collections import deque

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

BC_dict={}
for m in range(M):
    b,c = list(map(int,input().split()))
    if c in BC_dict.keys():
        BC_dict[c]+=b  
    else:    
        BC_dict[c]=b  
    
C = list(BC_dict.keys())

A.sort()
C.sort(reverse=True)

C=deque(C)

check = C.popleft()

for a_idx,a in enumerate(A):
    if a >= check:
        break
    
    A[a_idx] = check
    BC_dict[check] -= 1
    
    if BC_dict[check] == 0:
        if len(C) == 0:
            break
        else:
            check = C.popleft()
        
print(sum(A))
Buggy code:
from heapq import heapify, heappop, heappush
from sys import stdin

N, M = map(int, input().split())
A = list(map(int, stdin.readline().split()))
heapify(A)
BC = [None] * M
for i in range(M):
    BC[i] = tuple(map(int, stdin.readline().split()))
BC = sorted(BC, key=lambda x: x[1], reversed=True)

print(BC)

enough = False
for bc in BC:
    if enough:
        break
    for i in range(bc[0]):
        if bc[1] <= A[0]:
            enough = True
            break
        heappop(A)
        heappush(A, bc[1])

print(sum(A))


Fixed code:
from heapq import heapify, heappop, heappush
from sys import stdin

N, M = map(int, input().split())
A = list(map(int, stdin.readline().split()))
heapify(A)
BC = [None] * M
for i in range(M):
    BC[i] = tuple(map(int, stdin.readline().split()))
BC = sorted(BC, key=lambda x: x[1], reverse=True)

enough = False
for bc in BC:
    if enough:
        break
    for i in range(bc[0]):
        if bc[1] <= A[0]:
            enough = True
            break
        heappop(A)
        heappush(A, bc[1])

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

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

#a = list(map(int, input().split()))

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

a.sort()

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

now=0
for i in range(n):
    if(a[i] < bc[now][1]):
        a[i] = bc[now][1]
        bc[now][0] -= 1
    else:
        break
    if(bc[now][0]==0):
        now+=1
    
print(sum(a))
#print(a)

Fixed code:
import math

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

#a = list(map(int, input().split()))

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

a.sort()

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

now=0
for i in range(n):
    if(a[i] < bc[now][1]):
        a[i] = bc[now][1]
        bc[now][0] -= 1
    else:
        break
    if(bc[now][0]==0):
        now+=1
    if(now>=m):
        break
    
print(sum(a))
#print(a)