CodeEff/ECCO huggingface dataset:
# Original solution
import sys

def main():
    input = sys.stdin.readline
    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 = sorted(A, key=lambda x: -x)
    return sum(A[:N])


if __name__ == '__main__':
    print((main()))


# Optimized solution
import sys

def main():
    input = sys.stdin.readline
    N, M = list(map(int, input().split()))
    A = list(map(int, input().split()))
    BC = []
    for _ in range(M):
        b, c = list(map(int, input().split()))
        BC.append((b, c))

    BC = sorted(BC, key=lambda x: -x[1])
    n = 0
    for b, c in BC:
        n += b
        A += [c] * b
        
        if n >= N:
            break

    A = sorted(A, key=lambda x: -x)
    return sum(A[:N])


if __name__ == '__main__':
    print((main()))
# Original solution
N, M = list(map(int,input().split()))
A = sorted(list(map(int,input().split())))
D = [list(map(int,input().split())) for k in range(M)]
D = sorted(D, key = lambda x: -x[1])

now = 0
for d in D:
    for k in range(now,now+d[0]):
        if now == N:
            break
        if A[k] < d[1]:
            A[k] = d[1]
            now += 1
        else:
            now = k
            break

print((sum(A)))

# Optimized solution
N, M = list(map(int,input().split()))
A = sorted(list(map(int,input().split())))
L = [list(map(int,input().split())) for k in range(M)]
L = sorted(L,key=lambda x: -x[1])
now = 0
for e in L:
    if now == N:
        break
    while True:
        if A[now] < e[1] and e[0] > 0:
            A[now] = e[1]
            now += 1
            e[0] -= 1
            if now == N:
                break
        else:
            break

print((sum(A)))
# Original solution
N, M = list(map(int,input().split()))
A = sorted(list(map(int,input().split())))
L = []
for k in range(M):
    B, C = list(map(int,input().split()))
    L.append([B,C])
L = sorted(L,key=lambda x: -x[1])
ans = 0
i = 0
for k in range(N):
    if i < M:
        if A[k] < L[i][1]:
            L[i][0] -= 1
            ans += L[i][1]
            if L[i][0] == 0:
                i += 1
        else:
            ans += A[k]
    else:
        ans += A[k]
print(ans)


# Optimized solution
N, M = list(map(int,input().split()))
A = sorted(list(map(int,input().split())))
D = [list(map(int,input().split())) for k in range(M)]
D = sorted(D, key = lambda x: -x[1])
D.append([10**9+1,-1])
t = 0
ans = 0
for k in range(N):
    if A[k] < D[t][1]:
        ans += D[t][1]
        D[t][0] -= 1
        if D[t][0] == 0:
            t += 1
    else:
        ans += A[k]

print(ans)
# Original solution
from collections import Counter
def merge_dict_add_values(d1, d2):
    return dict(Counter(d1) + Counter(d2))
  
n,m=(int(i) for i in input().split())
a=[int(i) for i in input().split()]

mydict={}
mydict[a[0]]=1
for i in range(1,len(a)):
    d2={}
    d2[a[i]]=1
    mydict=merge_dict_add_values(mydict, d2)
#print(mydict)

#list_bc=[]
b,c=(int(i) for i in input().split())
mydict[c]=b
for i in range(1,m):
    b,c=(int(i) for i in input().split())
    d2={}
    d2[c]=b
    mydict=merge_dict_add_values(mydict, d2)
#print(n,m,a,mydict_bc)

mylist = sorted(list(mydict.items()), key=lambda x: -x[0])

#print(mylist)
cnt=0
cnt0=0
i=0
while cnt <= n:
    cnt0+=mylist[i][0]*mylist[i][1]
    v=mylist[i][0]
    cnt+=mylist[i][1]
    i+=1

#print(cnt0,v,cnt)
print((cnt0-v*(cnt-n)))


# Optimized solution
import heapq
h = []

n,m=(int(i) for i in input().split())
a=[int(i) for i in input().split()]
for i in a:
    heapq.heappush(h, [(-1)*i,1])

for i in range(m):
    b,c=(int(i) for i in input().split())
    heapq.heappush(h, [(-1)*c,b])

num=0
data=0
while num <= n:
    v=heapq.heappop(h)
    data+=v[0]*v[1]
    num+=v[1]

print(((-1)*(data-v[0]*(num-n))))
# Original solution
import sys
from heapq import heapify, heappush, heappop

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


for _ in range(M):
	b,c=list(map(int, sys.stdin.readline().split()))
	for i in range(b):
		if A[0]<c:
			heappop(A)
			heappush(A, c)
		else:
			break

print(sum(A))


# Optimized solution
import sys

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

B=[ list(map(int, sys.stdin.readline().split())) for _ in range(M)] 
B.sort(key=lambda x: x[1], reverse=True)

i=0

for b,c in B:
	for i in range(i,N):
		if A[i]<c and 0<b:
			A[i]=c
			b-=1
		if b==0:
			break
	i+=1

print(sum(A))
# Original solution
# -*- coding: utf-8 -*-
import sys
from heapq import heappush, heappop, heapify
from collections import Counter,defaultdict
dp=defaultdict(lambda: 0)
N,M=list(map(int, sys.stdin.readline().split()))
A=list(map(int, sys.stdin.readline().split()))
BC=defaultdict(lambda: 0)
for _ in range(M):
    b,c=list(map(int, sys.stdin.readline().split()))
    BC[c]+=b

C=Counter(A)
A=list(C.items())
heapify(A)    #(カードの数字、カード枚数)

for c,b in list(BC.items()):
    while A[0][0]<c and 0<b:
        value=A[0][0]
        cards=A[0][1]
        heappop(A)
        if cards<=b:    #取った枚数以上に手元の枚数を持っていれば、カードを全交換できる
            heappush(A,(c,cards))
        else:     #手元の方が少ない場合
            heappush(A,(c,b))   #新しいカードの値を挿入
            heappush(A,(value,cards-b))   #取ったカードも一部元に戻す
        b-=cards

ans=0
for a,cards in A:
    ans+=a*cards
print(ans)


# Optimized solution
# -*- coding: utf-8 -*-
import sys
N,M=list(map(int, sys.stdin.readline().split()))
A=list(map(int, sys.stdin.readline().split()))
B=[ list(map(int, sys.stdin.readline().split())) for _ in range(M) ]
B.sort(key=lambda x:-x[1] )

n=N
L=[]    #Bから降順でN個選ぶ
for cards,value in B:
    if cards<=n:    #カード全てがn枚に収まる場合
        L+=[ value for _ in range(cards) ]
        n-=cards
    else:    #カード全てがn枚に収まさらない場合
        L+=[ value for _ in range(n) ]
        n=0
        break
A+=L
A.sort(reverse=True)
print(sum(A[:N]))
# Original solution
import sys
import bisect
from collections import defaultdict

ans=0
#n=int(input())
n,m=list(map(int,input().split()))
a=[(1,int(i)) for i in input().split()]

b=[0]*m
c=[0]*m
for i in range(m):
    b[i],c[i]=list(map(int,input().split()))
    a.append((b[i],c[i]))
#a=sorted(a,reverse=True,key=lambda x:x[1])
a=sorted(a,key=lambda x:x[1])
#print(a)
t=0
while t<n:
    k,x=a.pop()
    if t+k<n:
        ans+=x*k
    else:
        ans+=x*(n-t)
    t+=k
    
print(ans)

# Optimized solution
ans=0
n,m=list(map(int,input().split()))
a=[(1,int(i)) for i in input().split()]
for i in range(m):
    b,c=list(map(int,input().split()))
    a.append((b,c))
a=sorted(a,key=lambda x:x[1])

t=0
while t<n:
    k,x=a.pop()
    ans+=x*min(k,n-t)
    t+=k
print(ans)
# Original 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)]
#print(N,M)
#print(A)
#print(BC)
#print("---")
A = sorted(A)
times = [bc[0] for bc in BC]
values = [bc[1] for bc in BC]
values, times = list(zip(*sorted(zip(values, times),reverse=True)))
#print(values)
#print(times)
#values.sort(reverse=True)


for i, v in enumerate(values) :
    tmp = [n for n,i in enumerate(A) if i > v]
    if len(tmp) == N :
        break
    elif len(tmp) == 0 :
        ind = N + 1
    else :
        ind = tmp[0]

    if times[i] < ind :
        A[0:times[i]] = [v] * times[i]
    else :
        A[0:ind] = [v] * ind
    #print(A)
    A = sorted(A)
print((sum(A)))


#for i, v in enumerate(values) :
#    for t in range(times[i]) :
#        min_A = min(A)
#        if v > min_A :
#            A[A.index(min(A))] = v
#            print(A)
#        else :
#            break
#    else:
#        continue
#    break
#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 i in range(M)]
#print(N,M)
#print(A)
#print(BC)
#print("---")
A = sorted(A)
BC = sorted(BC, key = lambda x: -x[1])
D = []
for bc in BC :
    for i in range(bc[0]) :
        if len(D) < N :
            D.append(bc[1])
        else :
            break
    else :
        continue
    break
if N > len(D) :
    S = len(D)
else :
    S = N
for i in range(S) :
    if A[i] < D[i] :
        A[i] = D[i]
    else :
        break
print((sum(A)))

#times = [bc[0] for bc in BC]
#values = [bc[1] for bc in BC]
#values, times = zip(*sorted(zip(values, times),reverse=True))
#print(values)
#print(times)
#values.sort(reverse=True)


# for i, v in enumerate(values) :
#     tmp = [n for n,i in enumerate(A) if i > v]
#     if len(tmp) == N :
#         break
#     elif len(tmp) == 0 :
#         ind = N + 1
#     else :
#         ind = tmp[0]
#
#     if times[i] < ind :
#         A[0:times[i]] = [v] * times[i]
#     else :
#         A[0:ind] = [v] * ind
#     #print(A)
#     A = sorted(A)
# print(sum(A))

#for i, v in enumerate(values) :
#    for t in range(times[i]) :
#        min_A = min(A)
#        if v > min_A :
#            A[A.index(min(A))] = v
#            print(A)
#        else :
#            break
#    else:
#        continue
#    break
#print(sum(A))
# Original 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)]

L = []

# cnt_sum = 0
# min_num = BC[0][1]
# for i in range(M):
#     cnt = BC[i][0]
#     for j in range(N):
#         if cnt_sum <= N:
#             L.append(BC[i][1])
#             cnt_sum += 1
#             cnt -= 1
#             if BC[i][1] < min_num:
#                 min_num = BC[i][1]
#         else:
#             if BC[i][1] < min_num:
#                 break
#             if L[j] < BC[i][1]:
#                 L[j] = BC[i][1]
#                 cnt -= 1
#         if cnt <= 0:
#             break
len_L = 0
for i in range(M):
    # for j in range(BC[i][0]):
    #     L.append(BC[i][1])
    L.extend([BC[i][1] for j in range(BC[i][0])])
    len_L += BC[i][0]
L.sort(reverse=True)
A.sort()
# ans = sum(A)
ans = 0
flag = True
for k in range(N):
    if k <= len_L-1:
        if flag and L[k] > A[k]:
            ans += L[k]
        else:
            flag = False
            ans += A[k]
    else:
        ans += A[k]
print(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)]

L = []
BC.sort(key=lambda x:x[1], reverse=True)
A.sort()
# ans = sum(A)
ans = 0
# flag = False
cnt = 0
# for k in range(N):
#     if k <= len_L-1:
#         if flag and L[k] > A[k]:
#             ans += L[k]
#         else:
#             flag = False
#             ans += A[k]
#     else:
#         ans += A[k]
for i in range(M):
    for j in range(BC[i][0]):
        if cnt < N and A[cnt] < BC[i][1]:
            ans += BC[i][1]
            cnt += 1
        else:
            break
    else:
        continue
    break

if cnt < N:
    ans += sum(A[cnt::])

print(ans)
# Original solution
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))

b = [0] * (m)
c = [0] * (m)

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

d = []

for i in range(m):
	for j in range(b[i]):
		d.append(c[i])

a.extend(d)
a.sort(reverse=True)

print((sum(a[:n])))


# Optimized solution
n, m = list(map(int, input().split()))
a = [(1, int(i)) for i in input().split()]
for _ in range(m):
    b, c = list(map(int, input().split()))
    a.append((b, c))
a = sorted(a, key=lambda x: x[1])
res = 0
idx = 0
while idx < n:
    b, c = a.pop()
    p = min(b, n-idx)
    res += p * c
    idx += p
print(res)
# Original solution
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return list(map(int, stdin.readline().split()))
def li_(): return [int(x) - 1 for x in stdin.readline().split()]
def lf(): return list(map(float, stdin.readline().split()))
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from collections import defaultdict

dic = defaultdict(int)

n, m = li()
a = list(li())
key = set()


for ai in a:
    dic[ai] += 1
    key.add(ai)

for _ in range(m):
    bi, ci = li()
    dic[ci] += bi
    key.add(ci)

res = n
ans = 0
for ki in sorted(list(key), reverse=True):
    if res - dic[ki] >= 0:
        ans += ki*dic[ki]
        res -= dic[ki]
    else:
        ans += ki*res
        res = 0

    if res == 0:
        break
print(ans)


# Optimized solution
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return list(map(int, stdin.readline().split()))
def li_(): return [int(x) - 1 for x in stdin.readline().split()]
def lf(): return list(map(float, stdin.readline().split()))
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from collections import defaultdict

n, m = li()
a = list(li())

dic = defaultdict(int)

for ai in a:
    dic[ai] += 1

for _ in range(m):
    b, c = li()
    dic[c] += b

keys = sorted(list(dic.keys()), reverse=True)

rest = n
ans = 0
for ki in keys:
    if dic[ki] > rest:
        ans += ki * rest
        rest = 0

    elif dic[ki] == rest:
        ans += ki * dic[ki]
        rest = 0

    else:
        ans += ki * dic[ki]
        rest -= dic[ki]

    if rest <= 0:
        break

print(ans)
# Original solution
from heapq import heappop, heappush, heapify

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

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

heapify(A)

i = 0
while i<M:
    m = min(A)
    tmp = List[i][1]
    cnt = List[i][0]
    while (0<cnt) and (m<tmp):
            m = heappop(A)
            heappush(A, tmp)
            tmp = List[i][1]
            cnt-=1
    i+=1

print((sum(A)))

# Optimized solution
from heapq import heappop, heappush, heapify

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

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

heapify(A)

i = 0
while i<M:
    m = heappop(A)
    heappush(A, m)
    tmp = List[i][1]
    cnt = List[i][0]
    while (0<cnt) and (m<tmp):
            m = heappop(A)
            heappush(A, tmp)
            tmp = List[i][1]
            cnt-=1
    i+=1

print((sum(A)))
# Original solution
from bisect import bisect_left
from collections import deque
from itertools import islice


N, M = list(map(int, input().split()))
As = list(map(int, input().split()))
cards = {}
for i in range(N):
    cards[As[i]] = cards.get(As[i], 0) + 1
# sorted_keys = deque(sorted(cards.keys()))
sorted_keys = list(sorted(cards.keys()))

for i in range(M):
    b, c = list(map(int, input().split()))
    cnt = 0
    # for k in islice(sorted_keys,0,bisect_left(sorted_keys, c)):
    for k in sorted_keys[0:bisect_left(sorted_keys, c)]:
        for _ in range(cards.get(k,0)):
            cards[k] = cards[k] - 1
            if cards[k] == 0:
                del(cards[k])
                sorted_keys.remove(k)
            cards[c] = cards.get(c, 0) + 1
            if c not in sorted_keys:
                sorted_keys.insert(bisect_left(sorted_keys, c),c )
            cnt += 1
            if cnt >= b:
                break
        if cnt >= b:
            break

print((sum([k * cnt for k,cnt in list(cards.items())])))




# Optimized solution
N, M  = list(map(int,input().split()))
As = sorted(list(map(int,input().split())))
di = {}
for i in range(M):
    b, c = list(map(int,input().split()))
    di[c] = di.get(c, 0) + b

keys = list(sorted(list(di.keys()), reverse=True))
ichi = 0
for i in range(N):
    try:
        if keys[ichi] > As[i]:
            As[i] = keys[ichi]
            di[keys[ichi]] -= 1
        else:
            break
        if di[keys[ichi]] == 0:
            ichi += 1
    except:
        pass
print((sum(As)))
# Original solution
from collections import Counter
import heapq

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

hq  = sorted(tuple(Counter(l).items()), key=lambda x:x[0])

heapq.heapify(hq)
# print(hq)
BC = [list(map(int, input().split())) for i in range(m)]
BC = sorted(BC,key = lambda x:-x[1])

for b,c in BC:
    while b > 0:
        x,y = heapq.heappop(hq)
        if x < c:
            if b < y:
                heapq.heappush(hq,(c,b))
                heapq.heappush(hq,(x,y-b))
                b = 0
            else:
                heapq.heappush(hq,(c,y))
                b -= y
        else:
            heapq.heappush(hq,(x,y))
            break
    # print(hq)

ans = 0
for i in range(len(hq)):
    ans += hq[i][0]*hq[i][1]
print(ans)

# Optimized solution
import sys
import heapq
input = sys.stdin.readline

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

BC = [list(map(int, input().split())) for i in range(m)]
BC = sorted(BC,key = lambda x:-x[1])

for b,c in BC:
    for i in range(b):
        a = heapq.heappop(A)
        if a < c:
            heapq.heappush(A,c)
        else:
            heapq.heappush(A,a)
            break
print((sum(A)))
# Original solution
import sys
import heapq
input = sys.stdin.readline

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

BC = [list(map(int, input().split())) for i in range(m)]
BC = sorted(BC,key = lambda x:-x[1])

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

# Optimized solution
import sys
input = sys.stdin.readline

n,m = list(map(int,input().split()))
A = list(map(int,input().split()))
BC = [tuple(map(int,input().split())) for i in range(m)]
BC = sorted(BC,key=lambda x:-x[1])
cnt = 0
flag = True
while True:
    for b,c in BC:
        A += [c]*b
        cnt += b
        if cnt > n:
            break
    break
A = sorted(A,reverse=True)
print((sum(A[: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
print((sum(sorted(A,reverse=True)[:n])))

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

sub = []
for i in range(m):
   sub += [bc[i][1]] * bc[i][0]
   if len(sub) > n:
       break

A += sub

print((sum(sorted(A,reverse=True)[:n])))
# Original solution
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)


def main():
    n,m = LI()
    a = sorted(LI())
    aa = sorted([LI() for _ in range(m)], key=lambda x: -x[1])
    ai = 0
    for b,c in aa:
        for j in range(b):
            if ai >= n:
                break
            if a[ai] < c:
                a[ai] = c
                ai += 1
        if ai >= n:
            break

    return sum(a)


print(main())



# Optimized solution
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)


def main():
    n,m = LI()
    a = sorted(LI())
    aa = sorted([LI() for _ in range(m)], key=lambda x: -x[1])
    ai = 0
    for b,c in aa:
        for j in range(b):
            if ai >= n:
                break
            if a[ai] < c:
                a[ai] = c
                ai += 1
            else:
                break
        if ai >= n:
            break

    return sum(a)


print(main())
# Original solution
from heapq import heapify, heappush, heappop
class HeapQue(object):
    def __init__(self, iterable, max_heap=False):
        self.sign = -1 if max_heap else 1
        sequence = [self.sign * number for number in iterable]
        heapify(sequence)
        self.heapque = sequence

    def push(self, item):
        heappush(self.heapque, item * self.sign)

    def pop(self):
        return heappop(self.heapque) * self.sign

    def dump(self):
        return [heappop(self.heapque) * self.sign for _ in range(len(self.heapque))]

N, M = list(map(int, input().split()))
q = HeapQue(list(map(int, input().split())))
pairs = sorted((tuple(map(int, input().split())) for _ in range(M)), key=lambda t: t[1], reverse=True)
for b, c in pairs:
    for _ in range(b):
        x = q.pop()
        if x < c:
            q.push(c)
        else:
            q.push(x)
            print((sum(q.dump())))
            quit()
print((sum(q.dump())))

# Optimized solution
from heapq import heapify, heappush, heappop
N, M = list(map(int, input().split()))
q = list(map(int, input().split()))
heapify(q)
pairs = sorted((tuple(map(int, input().split())) for _ in range(M)), key=lambda t: t[1], reverse=True)
for b, c in pairs:
    for _ in range(b):
        x = heappop(q)
        if x < c:
            heappush(q, c)
        else:
            heappush(q, x)
            print((sum(q)))
            quit()
print((sum(q)))
# Original solution
from heapq import heappush, heappop

# input
N,M =list(map(int,input().split()))
A = [0]*N
B = [0]*M
C = [0]*M

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


def solve():

    pairs = list(zip(C, B))
    sortedpairs = sorted(pairs, key=lambda x: x[0])
    for i in range(M):
        B[i]=sortedpairs[i][1]
        C[i]=sortedpairs[i][0]

    ansA=[0]*N
    for j in range(N):
        ansA[j] = A[j]

    p = int(0)
    j = int(0)
    while  j<N and M>=1+p :
        jd = int(0)
        if ansA[j] >= C[M - 1 - p]:
            break
        while  B[M-1-p]-jd>0 :
            if j==N : break
            if ansA[j]>=C[M-1-p] :
                break
            else:
                ansA[j]=C[M-1-p]
            jd +=1
            j +=1

        p +=1

    ans = int(0)
    for j in range(N):
        ans += ansA[j]

    print(ans)

solve()



# Optimized solution
import heapq
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ans = []

N, M = LI()
A = LI()
BC = [LI() for _ in range(M)]
A = list([x*(-1) for x in A])
heapq.heapify(A)
BC = sorted(BC, key=lambda x: x[1])[::-1]
#print(BC)
#print(A)
k = int(0)
ansC = int(0)
ansA = heapq.heappop(A)*(-1)
for i in range(N):
    if k<M and BC[k][0]==0:
        k +=1
    if k<M and BC[k][1]>ansA:
        ansC = BC[k][1]
        BC[k][0] -=1
    else:
        ansC = ansA
        if A!=[]:
            ansA = heapq.heappop(A)*(-1)
    ans.append(ansC)

#print(ans)
print((sum(ans)))