CodeEff/ECCO huggingface dataset:
# Original solution
N,M = list(map(int,input().split()))
a_list = list(map(int,input().split()))
bc_list = []
for _ in range(M):
    b,c = list(map(int,input().split()))
    bc_list.append((b,c))

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

count = 0
b_list = []
for b,c in bc_list:
    if count >= N:
        break
    for _ in range(b):
        b_list.append(c)
        
for i,a in enumerate(a_list):
    if i >= len(b_list):
        break
    
    if a < b_list[i]:
        a_list[i] = b_list[i]
    else:
        break
print((sum(a_list)))

# Optimized solution
N,M = list(map(int,input().split()))
a_list = list(map(int,input().split()))
bc_list = []
for _ in range(M):
    b,c = list(map(int,input().split()))
    bc_list.append([b,c])

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

tmp = 0
for i,a in enumerate(a_list):
    if tmp >= len(bc_list):
        break
    
    if a < bc_list[tmp][1]:
        bc_list[tmp][0] -= 1
        a_list[i] = bc_list[tmp][1]
    else:
        break

    if bc_list[tmp][0] == 0:
        tmp += 1
print((sum(a_list)))
# Original solution
import heapq

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

for i in range(m):
    b,c = list(map(int,input().split()))
    for j in range(b):
        d = heapq.heappop(a)
        if d >= c:
            heapq.heappush(a, d)
            break
        else:
            heapq.heappush(a, c)
        
print((sum(a)))

# Optimized solution
n,m = list(map(int,input().split()))
a = list(map(int,input().split()))
a.sort()
aa = [-1] * (2*n)
bc = []

for i in range(m):
    b,c = list(map(int,input().split()))
    bc.append((c,b))
bc.sort(reverse = True)

i = 0
for j in range(m):
    if i >= n:
        break
    for k in range(bc[j][1]):
        aa[i] = bc[j][0]
        i += 1

j = 0
for i in range(n):
    if a[i] < aa[j]:
        a[i] = aa[j]
        j += 1
        
print((sum(a)))
# Original solution
import sys

N, M = list(map(int, sys.stdin.readline().split()))

A = list(map(int, sys.stdin.readline().split()))
action_list = []
for i in range(M):
    temp_B, temp_C = list(map(int, sys.stdin.readline().split()))
    action_list.append([temp_B, temp_C])
action_list.sort(key = lambda x:x[1])
action_list.reverse()

current_list = A
count = 0
for i in range(M):
    if min(current_list) > action_list[i][1]:
        break
    if count > N:
        break
    temp_list = [e for e in current_list if e < action_list[i][1]]
    temp_list.sort()
    other_list = [e for e in current_list if e >= action_list[i][1]]
    if len(temp_list) < action_list[i][0]:
        times = len(temp_list)
    else:
        times = action_list[i][0]
    for j in range(times):
        temp_list[j] = action_list[i][1]
    current_list = temp_list + other_list
    count += action_list[i][0]

ans = sum(current_list)
print(ans)

# Optimized solution
import sys

N, M = list(map(int, sys.stdin.readline().split()))

A = list(map(int, sys.stdin.readline().split()))
action_list = []
B = []
for i in range(M):
    temp_B, temp_C = list(map(int, sys.stdin.readline().split()))
    action_list.append([temp_B, temp_C])
    B.append(temp_B)
action_list.sort(key = lambda x:x[1])
action_list.reverse()

current_list = A
current_list.sort()

k = 0
ans_sum = 0
count = 0
action_count = 0
if sum(B) < N:
    times = sum(B)
else:
    times = N
for i in range(times):
    if current_list[i] < action_list[k][1]:
        ans_sum += action_list[k][1]
        count += 1
        action_count += 1
        if action_count == action_list[k][0]:
            k += 1
            action_count = 0
    else:
        break
    if count > N:
        break

ans_sum += sum(current_list[count:])
print(ans_sum)
# Original solution
n,m = list(map(int,input().split()))
ls = list(map(int,input().split()))
di = [list(map(int,input().split())) for _ in range(m)]
for i in range(m):
    ls += [di[i][1]]*di[i][0]
ls.sort()
ls = ls[n*(-1):]
print((sum(ls)))

# Optimized solution
n,m = list(map(int,input().split()))
ls = list(map(int,input().split()))
di = [list(map(int,input().split())) for _ in range(m)]
mn = min(ls)
for i in range(m):
    if di[i][1] > mn:
        ls += [di[i][1]]*di[i][0]
ls.sort()
ls = ls[n*(-1):]
print((sum(ls)))
# Original solution
import heapq
n,m = list(map(int,input().split()))
ls = list(map(int,input().split()))
ls = list([x*(-1) for x in ls])
di = [list(map(int,input().split())) for _ in range(m)]
heapq.heapify(ls)
for i,j in di:
    for k in range(i):
        heapq.heappush(ls,j*(-1))
ans = 0
for l in range(n):
    ans += (-1)*heapq.heappop(ls)
print(ans)

# Optimized solution
n,m = list(map(int,input().split()))
ls = list(map(int,input().split()))
di = [list(map(int,input().split())) for _ in range(m)]
fi = []
for i,j in di:
    fi.append([j,i])
for k in ls:
    fi.append([k,1])
fi.sort(reverse=True)
ans = 0
cnt = 0
how = 0
while True:
    if how + fi[cnt][1] < n:
        how += fi[cnt][1]
        ans += fi[cnt][1]*fi[cnt][0]
    else:
        ans += (n-how)*fi[cnt][0]
        break
    cnt += 1
print(ans)
# Original solution
n,m=list(map(int,input().split()))
A=list(map(int,input().split()))
B=[list(map(int,input().split())) for _ in range(m)]
for b,c in B:A+=[c]*b
print((sum(sorted(A)[-n:])))

# Optimized solution
from operator import itemgetter
n,m=list(map(int,input().split()))
A=list(map(int,input().split()))
B=[list(map(int,input().split())) for _ in range(m)]
B.sort(key=itemgetter(1),reverse=True)
C=[]
for b,c in B:
  C+=[c]*b
  if len(C)>=n:break
print((sum(sorted(A+C)[-n:])))
# Original solution
n,m = list(map(int, input().split()))
a = list(map(int, input().split()))
for i in range(m):
    b, c = list(map(int, input().split()))
    a += [c] * b
a.sort(reverse = True)
print((sum(a[:n])))

# Optimized solution
n,m = list(map(int, input().split()))
a = list(map(int, input().split()))
for i in range(m):
    b, c = list(map(int, input().split()))
    a += [c] * b
    a.sort(reverse = True)
    a = a[:n]
a.sort(reverse = True)
print((sum(a[:n])))
# Original solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
saisyo = min(A)
saidai = max(A)
cb = []
count = 0
memo = 10 **10
for _ in range(M):
    b, c = list(map(int, input().split()))
    if c > saisyo and count < N: #枝刈り
        A += [c] * b
    if c > saidai:
        if count > N:
            if c >= memo:
                cb += [c] * b
        else:
            count += b
            cb += [c] * b
            memo = min(memo, c)

ans = 0
if count < N:
    A.sort(reverse = True)
    for i in range(N):
        ans += A[i]

if count >= N:
    cb.sort(reverse = True)
    for i in range(N):
        ans += cb[i]

print (ans)

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

cb = []
for i in A:
    cb += [[i,1]]
for _ in range(M):
    b, c = list(map(int, input().split()))
    cb += [[c, b]]
cb.sort(reverse = True)
#print (cb)

ans = 0
i = 0
j = 0
while i <= N:
    a = min(N-i, cb[j][1])
    ans += cb[j][0] * a
    j += 1
    i += a
    #print (ans)
    if i == N:
        print (ans)
        i += 1
# Original solution
from heapq import heapify, heappop, heappush 
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
heapify(A)
for i in range(M):
    B, C = list(map(int, input().split()))
    for i in range(B):
        e = heappop(A)
        if e < C:
            heappush(A, C)
        else:
            heappush(A, e)
            break

ans = sum(A)
print(ans)

# Optimized solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
D = {}
for a in A:
    if a in D:
        D[a] += 1
    else:
        D[a] = 1

for i in range(M):
    B, C = list(map(int, input().split()))
    if C in D:
        D[C] += B
    else:
        D[C] = B

K = sorted(list(D.keys()), reverse=True)
ans = 0
cnt = N
for k in K:
   a = min(D[k], cnt)
   ans += k * a
   cnt -= a
   if cnt == 0: break
print(ans)
# Original solution
n,m = list(map(int, input().split()))
a = list(map(int,input().split()))
for _ in range(m):
    b,c = list(map(int, input().split()))
    a += [c]*b
a.sort()
print((sum(a[-n:])))


# Optimized solution
from heapq import heappop, heappush


n,m = list(map(int,input().split()))
A = list(map(int,input().split()))
pq = []
ans = 0

for a in A:
    heappush(pq, [-a, 1])
    
for _ in range(m):
    b,c = list(map(int,input().split()))
    heappush(pq, [-c, b])

for i in range(n):
    c,b = heappop(pq)
    c = -c
    ans += c
    if b > 1:
        b -= 1
        heappush(pq, [-c, b])
        
print(ans)
# Original solution
N,M=list(map(int,input().split()))
A=list(map(int,input().split()))
BC=[list(map(int,input().split())) for _ in range(M)]
A.sort()
for B,C in BC:
  for i in range(N):
    if (B==0)or(A[i]>=C):
      break
    else:
      A[i]=C
      B-=1
      
  A.sort()
  
print((sum(A)))

# Optimized solution
N,M=list(map(int,input().split()))
A=list(map(int,input().split()))
BC=[list(map(int,input().split())) for _ in range(M)]
count=0
D=[]
BC.sort(key=lambda x:x[1],reverse=True)
A.sort()
for B,C in BC:
  D+=[C]*B
  count+=B
  if count>=N:
    break

D=D[:N]
for i in range(len(D)):
  d=D[i]
  if A[i]<d:
    A[i]=d
  else:
    break
  
print((sum(A)))
# Original solution
from heapq import heapify, heapreplace

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

A = list(map(int, input("").split(" ")))
A = list(A)
heapify(A)

for i in range(M):
    B, C = list(map(int, input("").split(" ")))

    for j in range(B):
        if A[0] < C:
            heapreplace(A, C)
        else:
            break

print((sum(A)))

# Optimized solution
N, M = list(map(int, input("").split(" ")))

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

potentials = []
for i in range(M):
    B, C = list(map(int, input("").split(" ")))

    potentials.append((B, C))

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

c = 0
for p in potentials:
    for b in range(p[0]):
        if p[1] > A[c]:
            A[c] = p[1]
            c += 1
            if c >= len(A):
                break
        else:
            break
    if c >= len(A):
        break

print((sum(A)))
# Original solution
import bisect
n, m = list(map(int, input().split()))
A = sorted(map(int, input().split()))
BC = []
for j in range(m):
    b, c = list(map(int, input().split()))
    BC.append((b, c))
BC = sorted(BC, key=lambda x: x[1], reverse=True)
ans = 0
for b, c in BC:
    i = bisect.bisect_left(A, c)
    if i == 0:
        break
    else:
        change = min(i, b)
        ans += c * change
        A = A[change:]
ans += sum(A)
print(ans)

# Optimized solution
n, m = list(map(int, input().split()))
A = sorted(map(int, input().split()))
BC = []
for j in range(m):
    b, c = list(map(int, input().split()))
    BC.append((b, c))
BC = sorted(BC, key=lambda x: x[1], reverse=True)
D = []
for b, c in BC:
    D.extend([c for i in range(b)])
    if len(D) >= n:
        D = D[:n]
        break
for x in range(len(D)):
    if A[x] > D[x]:
        break
else:
    x += 1
print((sum(A[x:]) + sum(D[:x])))
# Original solution
import bisect
from collections import deque
n, m = list(map(int, input().split()))
A = deque(sorted(map(int, input().split())))
BC = []
for j in range(m):
    b, c = list(map(int, input().split()))
    BC.append((b, c))
BC = sorted(BC, key=lambda x: x[1], reverse=True)
for b, c in BC:
    change = 0
    for i, a in enumerate(A):
        if i == b:
            break
        if a < c:
            change += 1
    for _ in range(change):
        A.popleft()
        A.append(c)
print((sum(A)))

# Optimized solution
import bisect
from collections import deque
n, m = list(map(int, input().split()))
A = deque(sorted(map(int, input().split())))
BC = []
for j in range(m):
    b, c = list(map(int, input().split()))
    BC.append((b, c))
BC = sorted(BC, key=lambda x: x[1], reverse=True)
for b, c in BC:
    change = 0
    for i in range(b):
        if A[i] < c:
            change += 1
    if not change:
        break
    for _ in range(change):
        A.popleft()
        A.append(c)
print((sum(A)))
# Original solution
#-*- coding:utf-8 -*-

import bisect

N, M = list(map(int, input().split()))
A = [int(a) for a in input().split()]
BC = [0]*M
for i in range(M):
    BC[i] = [int(n) for n in  input().split()]

A.sort()    # 昇順ソートO(NlogN)
BC.sort(key=(lambda x: x[1]), reverse=True)   # Ciの値で降順ソート

for i in range(M):
    b, c = BC[i]
    # cが降順なので、入替回数が減る？
    idx = bisect.bisect_left(A, c)
    if idx > 0:
        for j in range(min(idx,b)):
            A[j] = c
        A.sort()

print((sum(A)))

# Optimized solution
#-*- coding:utf-8 -*-

import bisect

N, M = list(map(int, input().split()))
A = [int(a) for a in input().split()]
BC = [0]*M
for i in range(M):
    BC[i] = [int(n) for n in  input().split()]

A.sort()    # 昇順ソートO(NlogN)
BC.sort(key=(lambda x: x[1]), reverse=True)   # Ciの値で降順ソート

st = 0 # 編集開始位置
for i in range(M):  # O(MlogN)
    b, c = BC[i]
    idx = bisect.bisect_left(A, c, st)  # A全体でのidxを返す
    edit = idx - st # 編集個数
    edit = min(b, edit)
    for j in range(edit):
        A[st+j] = c
    st += edit

print((sum(A)))
# Original solution
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, log2
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(eval(input()))
def MAP(): return list(map(int, input().split()))
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, M = MAP()
A = LIST()
BC = [LIST() for _ in range(M)]

for B, C in BC:
	for i in range(B):
		A.append(C)

A.sort(reverse=True)
print((sum(A[0:N])))


# Optimized solution
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, log2
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(eval(input()))
def MAP(): return list(map(int, input().split()))
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, M = MAP()
A = LIST()
BC = [LIST() for _ in range(M)]
A.sort(reverse=True)
BC.sort(key=lambda x:-x[1])
# print(A)
# print(BC)
count = 0
ans = 0
while count < N:
	if BC and A[0] < BC[0][1]:  # BCからとる
		if BC[0][0] + count > N:
			ans += BC[0][1] * (N-count)
			count = N
		else:
			count += BC[0][0]
			ans += BC[0][1] * BC[0][0]
		# print(count, ans)
		BC.pop(0)
	else:
		count += 1
		ans += A.pop(0)
		# print(count, ans)
print(ans)
# Original solution
from heapq import heapify, heappop, heappush
import sys
 
input = sys.stdin.readline
 
n, m = list(map(int, input().split()))
A = list(map(int, input().split()))
heapify(A)
 
for _ in range(m):
    b, c = list(map(int, input().split()))
    for _ in range(b):
        min_num = heappop(A)
        if min_num < c:
            heappush(A, c)
        else:
            heappush(A, min_num)
            break
 
total = sum(A)
print(total)

# Optimized solution
from heapq import heapify, heappop, heappush
import sys

input = sys.stdin.readline

n, m = list(map(int, input().split()))
A = list(map(int, input().split()))
heapify(A)

BC = [tuple(map(int, input().split())) for _ in range(m)]
BC.sort(key=lambda x: x[1], reverse=True)

for i in range(m):
    for _ in range(BC[i][0]):
        min_num = heappop(A)
        if min_num < BC[i][1]:
            heappush(A, BC[i][1])
        else:
            heappush(A, min_num)
            break

total = sum(A)
print(total)
# Original solution
from heapq import heappop,heappush,heapify
from collections import deque
import bisect
import sys
input = sys.stdin.readline
N, M = list(map(int, input().split()))
A = [int (i) for i in input().split()]
A.sort()
D = []
P = 0
for i in range(M):
  B,C = list(map(int, input().split()))
  D.extend([C]*B)
  P += B
D.sort(reverse=True)
ans = sum(A)
for i in range(min(P,N)):
  if A[i] < D[i]:
    ans += D[i] - A[i]
  else:
    break
print(ans)

# Optimized solution
from heapq import heappop, heappush, heapify
from collections import deque
N, M = list(map(int, input().split()))
A = [int(i) for i in input().split()]
S = []
for i in range(M):
  B,C = list(map(int, input().split()))
  for j in range(B):
    heappush(S,-C)
A.sort()
i = 0
l = len(S)
while i < min(N,l):
  q = -1*heappop(S)
  if A[i] < q:
    A[i] = q
    i += 1
  else:
    break
print((sum(A)))
# Original solution
from heapq import heappop, heappush, heapify
from collections import deque
import bisect
import sys
input = sys.stdin.readline
N, M = list(map(int, input().split()))
A = [int(i) for i in input().split()]
A.sort()
for i in range(M):
  B,C = list(map(int, input().split()))
  for j in range(B):
    idx = bisect.insort_left(A,C)
print((sum(A[-N:])))

# Optimized solution
N, M = list(map(int, input().split()))
A = [int(i) for i in input().split()]
table = []
for i in range(N):
  table.append((A[i],1))
for i in range(M):
  b,c = list(map(int, input().split()))
  table.append((c,b))
table.sort()
count = 0
ans = 0
while True:
  c, b = table.pop()
  if b <= N - count:
    ans += c*b
    count += b
  else:
    ans += (N-count)*c
    break
print(ans)
# Original solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
B = []
C = []
for i in range(M):
    b, c = list(map(int, input().split()))
    B.append(b)
    C.append(c)

C_arr = []
for i in range(M):
    for _ in range(B[i]):
        C_arr.append(C[i])

C_arr.sort(reverse=True)

A.sort()
ans = []
for i in range(N):
	if i < len(C_arr):
		ans_item = max(A[i], C_arr[i])
	else:
		ans_item = A[i]
	ans.append(ans_item)

print((sum(ans)))


# Optimized solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for i in range(M)]

# Cについて大きい順にソート
BC.sort(key=lambda x: x[1], reverse=True)

temp = []
for i in range(M):
        # CiをBi個追加
    temp += [BC[i][1]] * BC[i][0]
    if len(temp) > N:
        break

A += temp
A.sort(reverse=True)

print((sum(A[:N])))
# Original solution
import heapq

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

heapq.heapify(A)
for b, c in zip(B, C):
    for _ in range(b):
        min_val = A[0]
        if min_val < c:
            heapq.heappop(A)
            heapq.heappush(A, c)
        else:
            break
print((sum(A)))

# Optimized solution
import heapq

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

BC.sort(key=lambda x: x[1], reverse=True)
heapq.heapify(A)
for (b, c) in BC:
    for _ in range(b):
        min_val = A[0]
        if min_val < c:
            heapq.heappop(A)
            heapq.heappush(A, c)
        else:
            break
print((sum(A)))
# Original solution
import sys

fin = sys.stdin.readline

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

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

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

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

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


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

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

print((sum__ + sum(As)))


# Optimized solution
import sys

fin = sys.stdin.readline

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

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

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

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

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


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

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

print((sum__ + sum(As[now:])))
# Original solution
import math  # noqa
import bisect  # noqa
import queue  # noqa


if __name__ == '__main__':
    N, M = list(map(int, input().split()))
    A = list(map(int, input().split()))

    que = queue.PriorityQueue()
    for a in A:
        que.put(a)

    for _ in range(M):
        B, C = list(map(int, input().split()))
        for i in range(0, B):
            a = que.get()
            if a < C:
                que.put(C)
            else:
                que.put(a)
                break

    ans = 0
    while not que.empty():
        ans += que.get()

    print(ans)


# Optimized solution
import math  # noqa
import bisect  # noqa
import queue  # noqa


if __name__ == '__main__':
    N, M = list(map(int, input().split()))
    A = list(map(int, input().split()))

    cnt = {}
    for a in A:
        if a not in cnt:
            cnt[a] = 1
        else:
            cnt[a] += 1

    for _ in range(M):
        B, C = list(map(int, input().split()))
        if C not in cnt:
            cnt[C] = B
        else:
            cnt[C] += B

    cnt = [[k, v] for k, v in list(cnt.items())]
    cnt.sort(reverse=True)

    ans = 0
    for k, v in cnt:
        if N <= v:
            ans += k * N
            break
        else:
            ans += k * v
            N -= v

    print(ans)
# Original solution
N,M = (int(x) for x in input().split())
A_arr = [int(x) for x in input().split()]
BC_arr = [[0,0] for _ in range(M)]
for i in range(M):
    b,c = (int(x) for x in input().split())
    BC_arr[i][0] = b
    BC_arr[i][1] = c

A_arr.sort()

for BC in BC_arr:
    Bi = BC[0]
    Ci = BC[1]
    for j in range(Bi):
        if A_arr[j] < Ci:
            A_arr[j] = Ci
        else:
            break
    A_arr.sort()

print((sum(A_arr)))


# Optimized solution
N,M = (int(x) for x in input().split())
A_arr = [int(x) for x in input().split()]
BC_arr = [[0,0] for _ in range(M)]
for i in range(M):
    b,c = (int(x) for x in input().split())
    BC_arr[i][0] = b
    BC_arr[i][1] = c

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

break_th = False
j_count = 0
for BC in BC_arr:
    Bi = BC[0]
    Ci = BC[1]
    for j in range(Bi):
        if A_arr[j_count] >= Ci:
            break_th = True
            break
        A_arr[j_count] = Ci
        j_count += 1
        if j_count == N:
            break_th = True
            break
    if break_th:
        break
print((sum(A_arr)))