CodeEff/ECCO huggingface dataset:
# Original solution
N, M = list(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

for i in range(M):
    for j in range(List_BC[i][0]):
        if List_BC[i][1] > List_A[0]:
            List_A[0] = List_BC[i][1]
            List_A.sort()
        else:
            flag = 1
            break
    if flag == 1:
        break

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

# Optimized solution
N, M = list(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)))
# Original solution
import heapq

n, m = list(map(int, input().split()))
A_list = list(map(int, input().split()))
B_list = [0]*m
C_list = [0]*m
for i in range(m):
    b, c = list(map(int, input().split()))
    B_list[i] = b
    C_list[i] = c
heap = []
ans = 0
for a in A_list:
    heapq.heappush(heap, a)
for i in range(m):
    for _ in range(B_list[i]):
        min_ = heapq.heappop(heap)
        if min_ < C_list[i]:
            heapq.heappush(heap, C_list[i])
        else:
            heapq.heappush(heap, min_)
while heap:
    ans += heapq.heappop(heap)
print(ans)

# Optimized solution
n, m = list(map(int, input().split()))
A_list = list(map(int, input().split()))
BC_list = []
for _ in range(m):
    BC_list.append(list(map(int, input().split())))
A_list.sort()
BC_list.sort(key=lambda x: x[1], reverse=True)
C_list = []
for i in range(m):
    for _ in range(BC_list[i][0]):
        if len(C_list) < n:
            C_list.append(BC_list[i][1])
        else:
            break
    else:
      continue
    break

for i in range(min(n, len(C_list))):
    if A_list[i] < C_list[i]:
        A_list[i] = C_list[i]
    else:
        break

ans = sum(A_list)
print(ans)
# Original solution
import heapq
n, m = list(map(int, input().split()))
a_lst = list(map(int, input().split()))
bc_lst = [list(map(int, input().split())) for _ in range(m)]

aa_lst = a_lst[:]

heapq.heapify(aa_lst)
for bc in bc_lst:
    for i in range(bc[0]):
        if aa_lst[0] < bc[1]:
            heapq.heapreplace(aa_lst, bc[1])
        else:
            break
        
print((sum(aa_lst)))

# Optimized solution
import heapq
n, m = list(map(int, input().split()))
a_lst = list(map(int, input().split()))
bc_lst = [list(map(int, input().split())) for _ in range(m)]

bc_lst = sorted(bc_lst, key = lambda x: -x[1])
aa_lst = a_lst[:]

heapq.heapify(aa_lst)
for bc in bc_lst:
    for i in range(bc[0]):
        if aa_lst[0] < bc[1]:
            heapq.heapreplace(aa_lst, bc[1])
        else:
            break
        
print((sum(aa_lst)))
# Original solution
import sys,collections,math,random;sys.setrecursionlimit(10**7)
def Is(): return [int(x) for x in sys.stdin.readline().split()]
def Ss(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return eval(input())

n,m = Is()
a = Is()
for i in range(m):
    b,c = Is()
    a += [c]*b
a = sorted(a,reverse=True)
print((sum(a[:n])))

# Optimized solution
import sys,collections,math,random;sys.setrecursionlimit(10**7)
def Is(): return [int(x) for x in sys.stdin.readline().split()]
def Ss(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return eval(input())

n,m = Is()
a = Is()
d = collections.defaultdict(int)
for e in a:
    d[e] += 1
for i in range(m):
    b,c = Is()
    d[c] += b
a = sorted(list(d.items()),reverse=True)
ans = 0
for e,f in a:
    if n <= f:
        ans += e*n
        break
    else:
        n -= f
    ans += e*f
print(ans)
# Original solution
import bisect

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

for i in range(m):
	max_card, num = list(map(int, input().split() ))

	num_small = bisect.bisect(values, num)
	num_to_change = min(max_card, num_small)

	# sort
	# values = values[num_to_change:num_small] + [num]*num_to_change + values[num_small:]
	del  values[:num_to_change]
	values[num_small - num_to_change : num_small - num_to_change] = [num]*num_to_change

print((sum(values)))

# Optimized solution
from operator import itemgetter
n, m = list(map(int, input().split() ))
values = list(map(int, input().split() ))
values.sort()

card_change = [list(map(int, input().split() )) for _ in range(m)]
card_change.sort(key = itemgetter(1))
# 変更後の数をkeyとしてソート

# X枚書き換えるとして、Xを全探索する方針で書いてみる
now = sum(values)
ans = sum(values)
idx = m - 1
for x in range(n):
	now -= values[x]

	card_change[idx][0] -= 1 # 変更可能枚数
	now += card_change[idx][1] # 変更後の値

	if now > ans:
		ans = now

	if card_change[idx][0] == 0:
		idx -= 1
		if idx < 0:
			break

print(ans)
# Original solution
n,m=list(map(int,input().split()))
arr=list(map(int,input().split()))
arr=sorted(arr)
q=[list(map(int,input().split())) for _ in range(m)]
q=sorted(q,reverse=True,key=lambda x:x[0])
q=sorted(q,reverse=True,key=lambda x:x[1])
pos=0
for i in range(n):
  if arr[i]<=q[pos][1]:
    arr[i]=q[pos][1]
    q[pos][0]-=1
    if q[pos][0]==0:
      pos+=1
      if pos==m:
        break
print((sum(arr)))

# Optimized solution
n,m=list(map(int,input().split()))
ans=list(map(int,input().split()))
arr=[list(map(int,input().split())) for _ in range(m)]
arr=sorted(arr,reverse=True,key=lambda x:x[1])
tmp=0
for cnt,val in arr:
  ans+=[val]*cnt
  tmp+=cnt
  if tmp>=n:
    break
ans=sorted(ans,reverse=True)
print((sum(ans[:n])))
# Original solution
#!/usr/bin/env python3
import sys
import heapq

def solve(N: int, M: int, A: "List[int]", B: "List[int]", C: "List[int]"):

    h = []
    [heapq.heappush(h,-c) for b,c in zip(B, C) for b_ in range(b)]
    [heapq.heappush(h,-a) for a in A]
    k = [heapq.heappop(h) for _ in range(N)]
    print((sum(k)*-1))
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    B = [int()] * (M)  # type: "List[int]"
    C = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        B[i] = int(next(tokens))
        C[i] = int(next(tokens))
    solve(N, M, A, B, C)

if __name__ == '__main__':
    main()


# Optimized solution
#!/usr/bin/env python3
import sys

def solve(N: int, M: int, A: "List[int]", B: "List[int]", C: "List[int]"):

    bc = [c for b,c in zip(B, C) for b_ in range(b)]
    # print(A+bc)
    k = sorted(A+bc)[-N:]
    print((sum(k)))
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    B = [int()] * (M)  # type: "List[int]"
    C = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        B[i] = int(next(tokens))
        C[i] = int(next(tokens))
    solve(N, M, A, B, C)

if __name__ == '__main__':
    main()
# Original solution
#!/usr/bin/env python3
import sys

def solve(N: int, M: int, A: "List[int]", B: "List[int]", C: "List[int]"):

    bc = [c for b,c in zip(B, C) for b_ in range(b)]
    # print(A+bc)
    k = sorted(A+bc)[-N:]
    print((sum(k)))
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    B = [int()] * (M)  # type: "List[int]"
    C = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        B[i] = int(next(tokens))
        C[i] = int(next(tokens))
    solve(N, M, A, B, C)

if __name__ == '__main__':
    main()


# Optimized solution
#!/usr/bin/env python3
import sys

def solve(N: int, M: int, A: "List[int]", B: "List[int]", C: "List[int]"):

    # bc = [c for b,c in zip(B, C) for b_ in range(b)]
    # print(A+bc)
    # k = sorted(A+bc)[-N:]
    a = [(l, 1) for l in A]
    for x, y in zip(C, B):
        a.append((x,y))


    n = N
    ans = 0
    for x, y in sorted(a, reverse=True):
        if n >= y:
            n -= y
            ans += x*y
        else:
            ans += x*n
            break
    
    print(ans)

    # print(sum(k))
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    B = [int()] * (M)  # type: "List[int]"
    C = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        B[i] = int(next(tokens))
        C[i] = int(next(tokens))
    solve(N, M, A, B, C)

if __name__ == '__main__':
    main()
# Original solution
import heapq
def main():
	N, M = list(map(int, input().split()))
	A = list(map(int, input().split()))
	heapq.heapify(A)
	BC = [0]*M
	for i in range(M):
		BC[i] = list(map(int, input().split()))
	BC.sort(key = lambda x: -x[1])
	for i in range(M):
		if A[0] > BC[i][1]:
			break
		for _ in range(BC[i][0]):
			if A[0] > BC[i][1]:
				break
			heapq.heappop(A)
			heapq.heappush(A, BC[i][1])
	print((sum(A)))


if __name__ == '__main__':
	main()

# Optimized solution
import heapq
def main():
	N, M = list(map(int, input().split()))
	A = list(map(int, input().split()))
	heapq.heapify(A)
	BC = [0]*M
	for i in range(M):
		BC[i] = list(map(int, input().split()))
	BC.sort(key = lambda x: -x[1])
	for B, C in BC:
		if A[0] >= C:
			break
		for _ in range(B):
			if A[0] >= C:
				break
			heapq.heapreplace(A, C)
	print((sum(A)))


if __name__ == '__main__':
	main()
# Original solution
N,M = list(map(int,input().split()))
A = sorted(list(map(int,input().split())))
Ap = []
for _ in range(M):
  B,C = list(map(int,input().split()))
  Ap += [C]*B
Ap.sort()
np = len(Ap)
count = 0
i,j = 0,0
while i+j < N:
  a = A[N-i-1]
  b = Ap[np-j-1] if j<np else 0
  if a>=b and i<N:
    count += a
    i += 1
  elif a<b and j<np:
    count += b
    j += 1
print(count)

# Optimized solution
N,M = list(map(int,input().split()))
A = sorted(list(map(int,input().split())))
Ap = {}
for _ in range(M):
  B,C = list(map(int,input().split()))
  if C in list(Ap.keys()):
    Ap[C] = Ap[C]+B
  else:
    Ap[C] = B
Clist = sorted(Ap.keys())
nc = len(Clist)
i,j,k = 0,0,0
count = 0
a = A[N-1]
while i+j < N:
  if k<nc:
    c = Clist[nc-k-1]
    b = Ap[c]
  else:b,c = 0,0
  if a>=c:
    count += a
    i += 1
    a = A[N-i-1]
  elif c>a:
    count += b*c if i+j+b<N else c*(N-i-j)
    j += b
    k += 1
print(count)
# Original solution
N,M = list(map(int,input().split(' ')))
A = list(map(int,input().split(' ')))
BC = []
for i in [0]*M:
	#BC.append(list(map(int,input().split(' '))))
	b,c = list(map(int,input().split(' ')))
	A.extend([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 [0]*M:
	b,c = list(map(int,input().split(' ')))
	A.extend([c]*b)

A.sort(reverse=True)
print((sum(A[:N])))
# Original solution
# https://atcoder.jp/contests/abc127/tasks/abc127_d
# 難しいこと考えすぎないこと。数字を書き換えるとは言ってるけどCjをBj枚追加して大きい方からN枚取るのが最適
# 具体的にBj枚追加していくと死ぬので、枚数はタプルでその数とともに保持しておく


import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


from collections import Counter

N, M = read_ints()
A = read_ints()
BC = read_tuple(M)


cnter = Counter(A)
for b, c in BC:
    cnter[c] += b

num_and_cnt = list(cnter.items())
num_and_cnt.sort(reverse=True)

ans = 0
total = 0
for num, cnt in num_and_cnt:
    if total + cnt <= N:
        total += cnt
        ans += num * cnt
    else:
        while total < N:
            ans += num
            total += 1

print(ans)


# Optimized solution
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, list(zip(*ret))))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right

# https://atcoder.jp/contests/abc127/tasks/abc127_d
# わからん... データ構造で殴ります...
# いや、書き換えるっていうのは追加するっていうのと同じ意味じゃない？
# カードの枚数が多いから追加するのも大変→カードの枚数で管理しよう
N, M = read_ints()
A = read_ints()
A_cnt = Counter(A)
for _ in ra(M):
    b, c = read_ints()
    A_cnt[c] += b

tmp = list(A_cnt.items())
tmp.sort(key=lambda x: x[0], reverse=True)

ans = 0
cnt = 0
for k, v in tmp:
    while v > 0 and cnt < N:
        ans += k
        v -= 1
        cnt += 1
    if cnt == N:
        break


print(ans)
# Original solution
def f(n,m):
     a = list(map(int,input().split()))
     min_a = min(a)
     a = [(1,i) for i in a]
     b_c = [tuple(map(int,input().split())) for i in range(m)]
     a += b_c
     a.sort(reverse=True,key=lambda x:x[1])
     ans = 0
     while True:
          if n>a[0][0]:
               ans += a[0][0]*a[0][1]
               n -= a[0][0]
               a.pop(0)
          else:
               ans += a[0][1]*n
               break
     print(ans)

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

# Optimized solution
def f(n,m):
     a = list(map(int,input().split()))
     min_a = min(a)
     a = [(1,i) for i in a]
     b_c = [tuple(map(int,input().split())) for i in range(m)]
     a += b_c
     a.sort(key=lambda x:x[1])
     ans = 0
     while True:
          if n>a[-1][0]:
               ans += a[-1][0]*a[-1][1]
               n -= a[-1][0]
               a.pop()
          else:
               ans += a[-1][1]*n
               break
     print(ans)

n,m = list(map(int,input().split()))
f(n,m)
# Original solution
def f(n,m):
     a = list(map(int,input().split()))
     min_a = min(a)
     for i in range(m):
          b,c = list(map(int,input().split()))
          if c>min_a:
               a += [c]*b
     a.sort()
     print((sum(a[len(a)-n:])))
 
n,m = list(map(int,input().split()))
f(n,m)

# Optimized solution
from sys import stdin

def f(n,m):
     a = [int(x) for x in stdin.readline().split()]
     min_a = min(a)
     a = [(1,i) for i in a]
     b_c = [tuple(map(int,x.split())) for x in stdin.readlines()]
     a += b_c
     a.sort(key=lambda x:x[1])
     ans = 0
     while True:
          if n>a[-1][0]:
               ans += a[-1][0]*a[-1][1]
               n -= a[-1][0]
               a.pop()
          else:
               ans += a[-1][1]*n
               break
     print(ans)

n,m = [int(x) for x in stdin.readline().split()]
f(n,m)
# Original solution
from sys import stdin

def f():
     n,m = [int(x) for x in stdin.readline().split()]
     a = [int(x) for x in stdin.readline().split()]
     min_a = min(a)
     a = [(1,i) for i in a]
     b_c = [tuple(map(int,x.split())) for x in stdin.readlines()]
     a += b_c
     a.sort(key=lambda x:x[1])
     ans = 0
     while True:
          if n>a[-1][0]:
               ans += a[-1][0]*a[-1][1]
               n -= a[-1][0]
               a.pop()
          else:
               ans += a[-1][1]*n
               break
     print(ans)

f()

# Optimized solution
from sys import stdin

def f():
     n,m = [int(x) for x in stdin.readline().split()]
     a = [int(x) for x in stdin.readline().split()]
     min_a = min(a)
     a = [(1,i) for i in a]
     b_c = [tuple(map(int,x.split())) for x in stdin.readlines()]
     a += b_c
     a.sort(key=lambda x:x[1])
     ans = 0
     while True:
          if n>a[-1][0]:
               ans += a[-1][0]*a[-1][1]
               n -= a[-1][0]
               a.pop()
          else:
               ans += a[-1][1]*n
               break
     print(ans)
if __name__=='__main__':
     f()
# Original solution
def check():
    N, M = list(map(int, input().split()))
    A = sorted([int(i) for i in input().split()])
    sousa = sorted([[int(i) for i in input().split()] for i in range(M)],
                   reverse=True, key=lambda x:x[1])
    index = 0
    for cnt, value in sousa:
        for i in range(cnt):
            if min(A) < value:
                A[index] = value
            else:
                return(sum(A))
            index += 1
    return(sum(A))
  
print((check()))

# Optimized solution
def check():
    N, M = list(map(int, input().split()))
    A = sorted([int(i) for i in input().split()])
    sousa = sorted([[int(i) for i in input().split()] for i in range(M)],
                   reverse=True, key=lambda x:x[1])
    index = 0
    for cnt, value in sousa:
        for i in range(cnt):
            if index == N:
                break
            if A[index] < value:
                A[index] = value
            else:
                return(sum(A))
            index += 1
    return(sum(A))
print((check()))
# Original solution
import heapq

N, M = list(map(int, input().split()))
a = list(map(int, input().split()))
bc = [list(map(int, input().split())) for _ in range(M)]

heapq.heapify(a)

for b, c in bc:
    for i in range(b):
        hq = heapq.heappop(a)
        if hq < c:heapq.heappush(a, c)
        else:
            heapq.heappush(a, hq)
            break
print((sum(a)))

# Optimized solution
import heapq

N, M = list(map(int, input().split()))
a = list(map(int, input().split()))
bc = [list(map(int, input().split())) for _ in range(M)]

bc.sort(key=lambda x:-x[1])
cnt = True

heapq.heapify(a)

for b, c in bc:
    if not cnt: break
    for i in range(b):
        hq = heapq.heappop(a)
        if hq < c:heapq.heappush(a, c)
        else:
            heapq.heappush(a, hq)
            cnt = False
            break
print((sum(a)))
# 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)
A = A[0:n]

print((sum(A)))

# Optimized solution
n, m = list(map(int, input().split()))
A = list(map(int, input().split()))

L = [[0, 0] for i in range(m)]

for i in range(m):
    b, c = list(map(int, input().split()))
    L[i][0] = c
    L[i][1] = b
L.sort(reverse=True)

cnt = 0
for i in range(m):
    c, b = L[i]
    A += [c]*b
    cnt += b
    if cnt >= n:
        break

A.sort(reverse=True)
A = A[0:n]

print((sum(A)))
# Original solution
from collections import deque
N, M = list(map(int, input().split()))
A = sorted(list(map(int, input().split())))
CB = deque([])
for i in range(M):
    B, C = list(map(int, input().split()))
    CB.append([C, B])
CB = sorted(CB, reverse=True)
flag = False
for i in CB:
    if flag:
        break
    c = 0
    while c < i[1]:
        if i[0] > A[c]:
            A[c] = i[0]
            c += 1
        else:
            c = i[1]
            flag = True
    A = sorted(A)
print((sum(A)))

# Optimized solution
from collections import deque
N, M = list(map(int, input().split()))
A = sorted(list(map(int, input().split())))
CB = deque([])
for i in range(M):
    B, C = list(map(int, input().split()))
    CB.append([C, B])
CB = sorted(CB, reverse=True)
flag = False
c = 0
I = 0
for i in CB:
    I += i[1]
    if flag:
        break
    while c < I:
        if c >= N:
            break
        if A[c] < i[0]:
            A[c] = i[0]
            c += 1
        else:
            c = I
            flag = True
print((sum(A)))
# Original solution
import heapq

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


def change(A, B, C):
    for i in range(B):
        tmp = heapq.heappop(A)
        if tmp < C:
            heapq.heappush(A, C)
        else:
            heapq.heappush(A, tmp)
            break


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

print((sum(A)))

# Optimized solution
import heapq

N, M = list(map(int,input().split()))
A = list(map(int,input().split()))
heapq.heapify(A)
B = []
C = []
for i in range(M):
    tmp_B, tmp_C = list(map(int,input().split()))
    B.append(tmp_B)
    C.append(tmp_C)

def change(A, B, C):
    for i in range(B):
        tmp = heapq.heappop(A)
        if tmp < C:
            heapq.heappush(A, C)
        else:
            heapq.heappush(A, tmp)
            break

ZIP = list(zip(C, B))

ZIP = reversed(sorted(ZIP))

C, B = list(zip(*ZIP))

for i in range(M):
    change(A, B[i], C[i])

print((sum(A)))
# Original solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))

import bisect
A.sort()
for i in range(M):
    b, c = list(map(int, input().split()))
    idx = bisect.bisect_left(A, c)
    #print(A[:idx], A[idx:])
    if idx > b:
        A = A[b:idx] + [c] * b + A[idx:]
    else:
        #print("a", b, idx)
        A = [c] * idx + A[idx:]
    #print(A)
print((sum(A)))

# Optimized solution

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

A.sort()

CB = []
for i in range(M):
    B, C = list(map(int, input().split()))
    CB.append([C, B])
    
CB.sort(reverse=True)

idx = 0
c = 0
for i in range(N):
    if A[i] < CB[idx][0]:
        A[i] = CB[idx][0]
        c += 1
    if CB[idx][1] <= c:
        idx += 1
        c = 0
    if idx >= M:
        break
    #print(A)
        
print((sum(A)))