CodeEff/ECCO huggingface dataset:
# Original solution
import heapq
n,m=list(map(int,input().split()))
a=sorted(list(map(int, input().split())))
bc=[list(map(int, input().split())) for _ in range(m)]
bc.sort(key=lambda x:x[1])
heapq.heapify(a)
for b,c in bc:
  for j in range(b):
    amin=heapq.heappop(a)
    if amin<c:
      heapq.heappush(a,c)
    else:
      heapq.heappush(a,amin)
      break
print((sum(a)))

# Optimized solution
def main():
    import heapq
    n,m=list(map(int,input().split()))
    a=sorted(list(map(int, input().split())))
    bc=[list(map(int, input().split())) for _ in range(m)]
    bc.sort(key=lambda x:-x[1])
    heapq.heapify(a)
    for b,c in bc:
      for j in range(b):
        amin=heapq.heappop(a)
        if amin<c:
          heapq.heappush(a,c)
        else:
          heapq.heappush(a,amin)
          break
    print((sum(a)))
main()
# Original solution
import heapq

n, m = list(map(int, input().split()))
cards = list(map(int, input().split()))
heapq.heapify(cards)
queries = []
for i in range(m):
    q = list(map(int, input().split()))
    queries.append(q)

for q in queries:
    b, c = q
    cnt = 0
    while cnt < b:
            heapq.heappushpop(cards, c)
            cnt += 1

ans = 0
for i in range(n):
    ans += cards[i]

print(ans)


# Optimized solution
import heapq

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

queries = []
for i in range(m):
    q = list(map(int, input().split()))
    queries.append(q)
queries.sort(key=lambda x: x[1])
queries.reverse()

ans = 0
pos = 0
for q in queries:
    b, c = q
    if cards[min(pos+b-1, n-1)] < c:
        # print('a')
        pos = pos + b
        ans += c * (b - max(0, pos-n))
        if pos >= n:
            break
    else:
        for i in range(b):
            if cards[pos+i] < c:
                # print('b')
                ans += c
            else:
                # print('c')
                pos += i
                for j in range(pos, n):
                    ans += cards[j]

                pos = n
                break
        break

if pos < n:
    for j in range(pos, n):
        ans += cards[j]

print(ans)
# Original solution

def s0():return eval(input())
def s1():return input().split()
def s2(n):return [eval(input()) for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(eval(input()))
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(eval(input())) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print((yes if b else no))
# from sys import setrecursionlimit
# setrecursionlimit(1000000)
# from collections import Counter,deque,defaultdict
# import itertools
# import math
# import networkx as nx
# from bisect import bisect_left,bisect_right
from heapq import heapify,heappush,heappop
n,m=n1()
A=n1()
BC=t3(m)

heapify(A)
# BC.sort(key=lambda x:x[1])

for b,c in BC:
    for _ in range(b):
        a=heappop(A)
        if a>=c:
            heappush(A,a)
            break;
        else:
            heappush(A,c)

print((sum(A)))

# Optimized solution

def s0():return eval(input())
def s1():return input().split()
def s2(n):return [eval(input()) for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(eval(input()))
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(eval(input())) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print((yes if b else no))
# from sys import setrecursionlimit
# setrecursionlimit(1000000)
# from collections import Counter,deque,defaultdict
# import itertools
# import math
# import networkx as nx
# from bisect import bisect_left,bisect_right
from heapq import heapify,heappush,heappop
n,m=n1()
A=n1()
BC=t3(m)

heapify(A)
BC.sort(key=lambda x:-x[1])

D=[]
for b,c in BC:
    D.extend([c]*b)
    if len(D)>=n:
        break
D=D[:n]


for d in D:
    a=heappop(A)
    if a>=d:
        heappush(A,a)
        break;
    else:
        heappush(A,d)

print((sum(A)))
# Original solution
def binary_search(cards,n,number):
    low=0
    high=len(cards)-1

    while high-low>1:
        mid=(low+high)//2
        base=cards[mid]
        if base==number:
            for i in range(n):
                cards.insert(mid,number)
            return cards,mid
        if base<number:
            low=mid
        else:
            high=mid

    if cards[high]==cards[low]:
        if number>cards[high]:
            for i in range(n):
                cards.insert(high+1, number)
        else:
            for i in range(n):
                cards.insert(low, number)
    else:
        for i in range(n):
            cards.insert(high,number)
    return cards,mid

#1 3 5 7 9 11 <=8
#1 3 [5] 7 9 11 <= 8
#1 3 (5) 7 9 (11) <=8
#1 3 5 [7] 9 11 <= 8
#1 3 5 (7) 9 (11) <= 8
#1 3 5 7 [9] 11 <= 8
#1 3 5 (7) (9) 11
#1 3 5 [7] 9 11 <=8
#1 3 5 (7)
import sys
from heapq import heappush,heappop

def main():
    inp1=list([int(x) for x in input().split()])
    cards=list([int(x) for x in input().split()])
    cards_heap=[]
    for i in cards:
        heappush(cards_heap,i)

    N=inp1[0]
    M=inp1[1]
    B=[]
    C=[]
    for i in range(M):
        changes=list([int(x) for x in input().split()])
        B.append(changes[0])
        C.append(changes[1])

    cb=list(zip(C,B))
    cb=sorted(cb,reverse=True)
    C,B=list(zip(*cb))

    hand=[]
    for i in range(M):
        hand.extend([C[i] for j in range(B[i])])

    for i in hand:
        a=heappop(cards_heap)
        if i>a:
            heappush(cards_heap,i)
        else:
            heappush(cards_heap,a)
            break

    print((sum(cards_heap)))

if __name__=="__main__":
    main()

# Optimized solution
from heapq import heappush,heappop

def main():
    inp1=list([int(x) for x in input().split()])
    cards=list([int(x) for x in input().split()])
    cards_heap=[]
    for i in cards:
        heappush(cards_heap,i)

    N=inp1[0]
    M=inp1[1]
    B=[]
    C=[]
    for i in range(M):
        changes=list([int(x) for x in input().split()])
        B.append(changes[0])
        C.append(changes[1])

    cb=list(zip(C,B))
    cb=sorted(cb,reverse=True)


    for C,B in cb:
        for _ in range(B):
            a=heappop(cards_heap)
            if C>a:
                heappush(cards_heap,C)
            else:
                heappush(cards_heap,a)
                break

    print((sum(cards_heap)))

if __name__=="__main__":
    main()
# Original solution
from bisect import bisect

n, m = list(map(int, input().split()))
*a, = list(map(int, input().split()))
a.sort()
BC = []
for _ 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
num = 0
for b, c in BC:
    idx = bisect(a[num:], c)
    p = min(idx, b)
    num += p
    ans += c * p
print((sum(a[num:])+ans))


# 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])
ans = 0
t = 0
while t < n:
    b, c = a.pop()
    p = min(b, n-t)
    ans += p * c
    t += p
print(ans)
# Original solution
import bisect

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))
a.sort()
BC = sorted(BC, key=lambda x: x[1], reverse=True)
ans = 0
for b, c in BC:
    idx = bisect.bisect_right(a, c)
    if b <= idx:
        ans += b * c
        a = a[b:]
    else:
        ans += idx * c
        a = a[idx:]
ans += sum(a)
print(ans)


# 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])
ans = 0
t = 0
while t < N:
    b, c = a.pop()
    p = min(b, N-t)
    ans += p * c
    t += p
print(ans)
# Original solution
import heapq

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

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

CB.sort()
D = []
for c, b in CB:
  D += [c]*b

for i in range(N):
  if D:
    d = D.pop()
    g = heapq.heappushpop(A, d)
    if d == g:
      break
  else:
    break

print((sum(A)))

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

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

D = [[a, 1] for a in A] + CB

D.sort()

ans = 0
while N > 0:
  c, b = D.pop()
  ans += min(N, b)*c
  N -= b

print(ans)
# Original solution
from operator import itemgetter

N,M = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
A.sort()
BC = [[int(x) for x in input().split()]for _ in range(M)]
BC.sort(key = itemgetter(1),reverse=True)
x = 0
for i in BC:
    y = 0
    while(x+y<len(A) and y<i[0]):
        if(A[x+y]<i[1]):
            A[x+y]=i[1]
            y += 1
        else:
            x += 1
    x = y
print((sum(A)))


# Optimized solution
from operator import itemgetter
import sys
sys.setrecursionlimit(1000000)

N,M = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
A.sort()
BC = [[int(x) for x in input().split()]for _ in range(M)]
BC.sort(key = itemgetter(1),reverse=True)

x = 0
bc_ind = 0
ans = 0
for ind,i in enumerate(A):
    bc = 0
    if(BC[bc_ind][0]>x):
        bc = BC[bc_ind][1]
        x+=1
    else:
        bc_ind+=1
        x = 1
        if(len(BC)<=bc_ind):
            ans += sum(A[ind:])
            break
        else:
            bc = BC[bc_ind][1]
    if(bc>i):
        ans += bc
    else:
        ans += sum(A[ind:])
        break
print(ans)
# Original solution
#!/usr/bin/env python3
N, M = list(map(int, input().rstrip().split()))
A = list(map(int, input().rstrip().split()))

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

Index = sorted(list(range(M)), key=lambda i: C[i], reverse=True)

sum = 0
for index in Index:
    A.sort()
    count = 0
    i = 0
    while True:
        if i >= len(A):
            break
        if A[i] < C[index] and count < B[index]:
            sum += C[index]
            A.pop(i)
            # print(A)
            count += 1
            continue
        if A[i] >= C[index] or count >= B[index]:
            break
        i += 1

if A:
    for a in A:
        sum += a

print(sum)


# Optimized solution
#!/usr/bin/env python3
N, M = list(map(int, input().rstrip().split()))
A = list(map(int, input().rstrip().split()))

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

Index = sorted(list(range(M)), key=lambda i: C[i], reverse=True)

sum = 0
A.sort()
for index in Index:
    count = 0
    i = 0
    while True:
        if i >= len(A):
            break
        if A[i] < C[index] and count < B[index]:
            sum += C[index]
            A.pop(i)
            # print(A)
            count += 1
            continue
        if A[i] >= C[index] or count >= B[index]:
            break
        i += 1

if A:
    for a in A:
        sum += a

print(sum)
# Original solution
#!/usr/bin/env python3
N, M = list(map(int, input().rstrip().split()))
A = list(map(int, input().rstrip().split()))

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

Index = sorted(list(range(M)), key=lambda i: C[i], reverse=True)

sum = 0
A.sort()
for index in Index:
    count = 0
    i = 0
    while True:
        if i >= len(A):
            break
        if A[i] < C[index] and count < B[index]:
            sum += C[index]
            A.pop(i)
            # print(A)
            count += 1
            continue
        if A[i] >= C[index] or count >= B[index]:
            break
        i += 1

if A:
    for a in A:
        sum += a

print(sum)


# Optimized solution
#!/usr/bin/env python3
N, M = list(map(int, input().rstrip().split()))
A = list(map(int, input().rstrip().split()))

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

Index = sorted(list(range(M)), key=lambda i: C[i], reverse=True)

sum = 0
A.sort()
for index in Index:
    if A and A[0] >= C[index]:
        break
    count = 0
    i = 0
    while True:
        if i >= len(A):
            break
        if A[i] < C[index] and count < B[index]:
            sum += C[index]
            A.pop(i)
            # print(A)
            count += 1
            continue
        if A[i] >= C[index] or count >= B[index]:
            break
        i += 1

if A:
    for a in A:
        sum += a

print(sum)
# Original solution
import math
import fractions
import copy
#以下てんぷら
def j(q):
    if q==1: print("YES")
    elif q == 0:print("NO")
    exit(0)
rem = pow(10,9)+7
"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(eval(input()))
def printrow(a):
    for i in a:
        print(i)
def combinations(n,r):
    if n<r:return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def permutations(n,r):
    if n<r:return 0
    return math.factorial(n) // math.factorial(n - r)
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)


#n = ip()                                     #入力整数1つ
n,m= (int(i) for i in input().split())       #入力整数横2つ以上
a = [int(i) for i in input().split()]        #入力整数配列
#a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列
#n = ip()                      #入力セット(整数改行あり)(1/2)
#a=[ip() for i in range(n)]    #入力セット(整数改行あり)(2/2)
#a=[input() for i in range(n)]    #入力セット(整数改行あり)(2/2)
#jの変数はしようできないので注意
#全足しにsum変数使用はsum関数使用できないので注意
b = []
for i in range(m):
    x,y = (int(i) for i in input().split())
    b.append([y,x])
b.sort(reverse=True)
a.sort()
s = 0
while (len(a)):
    if a[0] < b[0][0]:
        a.pop(0)
        s+=b[0][0]
        b[0][1]-=1
        if b[0][1] == 0:
            b.pop(0)
            if b == []:
                s += sum(a)
                break
    else:
        b.pop(0)
        if b == []:
            s+=sum(a)
            break
print(s)

# Optimized solution
import math
import fractions
import copy
#以下てんぷら
def j(q):
    if q==1: print("YES")
    elif q == 0:print("NO")
    exit(0)
rem = pow(10,9)+7
"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(eval(input()))
def printrow(a):
    for i in a:
        print(i)
def combinations(n,r):
    if n<r:return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def permutations(n,r):
    if n<r:return 0
    return math.factorial(n) // math.factorial(n - r)
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)


#n = ip()                                     #入力整数1つ
n,m= (int(i) for i in input().split())       #入力整数横2つ以上
a = [int(i) for i in input().split()]        #入力整数配列
#a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列
#n = ip()                      #入力セット(整数改行あり)(1/2)
#a=[ip() for i in range(n)]    #入力セット(整数改行あり)(2/2)
#a=[input() for i in range(n)]    #入力セット(整数改行あり)(2/2)
#jの変数はしようできないので注意
#全足しにsum変数使用はsum関数使用できないので注意
b = []
for i in range(m):
    x,y = (int(i) for i in input().split())
    b.append([y,x])
b.sort(reverse=True)
a.sort()
s = 0
pa = 0
pb = 0
while (pa < len(a)):
    if a[pa] < b[pb][0]:
        pa+=1
        s+=b[pb][0]
        b[pb][1]-=1
        if b[pb][1] == 0:
            pb+=1
            if len(b) == pb:
                s += sum(a[pa:])
                break
    else:
        pb+=1
        if len(b) == pb:
            s+=sum(a[pa:])
            break
print(s)
# Original solution
N, M = [int(i) for i in input().strip().split()]
A = [int(i) for i in input().strip().split()]
A.sort()
for i in range(M):
    B, C = [int(i) for i in input().strip().split()]
    is_changed = False
    for j in range(0, B):
        if A[j] < C:
            A[j] = C
            is_changed = True
        else:
            break
    if is_changed:
        A.sort()
print((sum(A)))


# Optimized solution
N, M = [int(i) for i in input().strip().split()]
A = [int(i) for i in input().strip().split()]
A.sort()

BC = [[0, 0] for _ in range(M)]
for j in range(M):
    BC[j][0], BC[j][1] = [int(i) for i in input().strip().split()]
BC.sort(key=lambda x: x[1])

res = 0
cnt = 0
i = N - 1
j = M - 1
while True:
    if cnt == N:
        break

    if j == -1:
        res += A[i]
        i -= 1
    elif A[i] > BC[j][1]:
        res += A[i]
        i -= 1
    elif A[i] <= BC[j][1]:
        res += BC[j][1]
        BC[j][0] -= 1
        if BC[j][0] == 0:
            j -= 1
    else:
        raise ValueError
    cnt += 1
print(res)
# Original solution
from heapq import heapify, heappop, heappush


def solve():
    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):
            if A[0] < C:
                heappop(A)
                heappush(A, C)
            else:
                break
    
    print((sum(A)))


if __name__ == '__main__':
    solve()

# Optimized solution
from operator import itemgetter


def solve():
    N, M = list(map(int, input().split()))
    A = list(map(int, input().split()))
    BC = [[] for _ in range(M)]
    for i in range(M):
        BC[i] = list(map(int, input().split()))
    BC.sort(key=itemgetter(1), reverse=True)
    
    change = []
    for b, c in BC:
        change += [c for _ in range(b)]
        if N < len(change):
            break
    
    A += change
    A.sort()
    ans = sum(A[-N:])
    print(ans)


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

import heapq
heapq.heapify(A)

for i in range(M):
  b,c=list(map(int,input().split()))
  for j in range(b):
    val=heapq.heappop(A)
    if val<c:
      heapq.heappush(A,c)
    else:
      heapq.heappush(A,val)
      break
print((sum(A)))

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

Q=[None]*M
for i in range(M):
  b,c=list(map(int,input().split()))
  Q[i]=[b,c]
  
Q=sorted(Q,key=lambda x:x[1])[::-1]

A=sorted(A)
ind=0
for i in range(len(Q)):
  b,c=Q[i]
  for p in range(b):
    if ind+p>=len(A):
      print((sum(A)))
      exit(0)
    if A[ind+p]<c:
      A[ind+p]=c
    else:
      break
  ind+=b
else:
  print((sum(A)))
# Original solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort()
L = []
for i in range(M):
	b, c = list(map(int, input().split()))
	_ = [c, b]
	L.append(_)
	L.sort()
	L.reverse()
c = 0
D = []
while c < N and len(L) > 0:
	for i in range(L[0][1]):
		D.append(L[0][0])
		c += 1
	del L[0]
i = 0
while D[i] >= A[i] and i < N and i < len(D):
	i += 1
	if i == len(D) or i == N:
		break
x = sum(D[:i]) + sum(A[i:])
print(x)

# Optimized solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort()
L = []
for i in range(M):
	b, c = list(map(int, input().split()))
	L.append((c, b))
L = sorted(L, reverse = True)
L.append((0, N))
temp = 0
sum = 0
for i in range(M + 1):
	for j in range(L[i][1]):
		if A[temp] < L[i][0]:
			sum += L[i][0]
			temp += 1
		else:
			sum += A[temp]
			temp += 1
		if temp == N:
			break
	if temp == N:
		break
print(sum)
# Original solution
n, m = list(map(int, input().split(' ')))
aaa = list(map(int, input().split(' ')))
r = 100000
cccbbb = []
for i in range(m):
    b, c = list(map(int, input().split(' ')))
    cccbbb += [c] * b
aaa = sorted(aaa)
cccbbb = sorted(cccbbb)[::-1]
len_cccbbb = len(cccbbb)
len_aaa = len(aaa)
#print(aaa)
#print(cccbbb)
if len_aaa < len_cccbbb:
    #print(([max(a, c) for a, c in zip(aaa, cccbbb[:len_aaa])]))
    print((sum([max(a, c) for a, c in zip(aaa, cccbbb[:len_aaa])])))
elif len_aaa >= len_cccbbb:
    #print(([max(a, c) for a, c in zip(aaa, cccbbb + [0] * (len_aaa - len_cccbbb))]))
    print((sum([max(a, c) for a, c in zip(aaa, cccbbb + [0] * (len_aaa - len_cccbbb))])))


# Optimized solution
n, m = list(map(int, input().split(' ')))
aaa = list(map(int, input().split(' ')))
aaa = sorted(aaa)
len_aaa = len(aaa)
cccbbb = []
for i in range(m):
    b, c = list(map(int, input().split(' ')))
    cccbbb += [c] * b
    cccbbb = sorted(cccbbb)[::-1]
    if len(cccbbb) > len(aaa):
        cccbbb = cccbbb[:len_aaa]
len_cccbbb = len(cccbbb)
if len_aaa < len_cccbbb:
    print((sum([max(a, c) for a, c in zip(aaa, cccbbb[:len_aaa])])))
elif len_aaa >= len_cccbbb:
    print((sum([max(a, c) for a, c in zip(aaa, cccbbb + [0] * (len_aaa - len_cccbbb))])))
# Original solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
opes = [tuple(map(int, input().split())) for _ in range(M)]

D = [C for B, C in opes for _ in range(B)]
D = sorted(D, reverse=True)[:N]

A.sort()
for i, d in enumerate(D):
    if A[i] >= d:
        break
    A[i] = d

print((sum(A)))


# Optimized solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
opes = [tuple(map(int, input().split())) for _ in range(M)]
opes.sort(key=lambda x:-x[1])

D = []
lenD = 0
for B, C in opes:
    D += [C]*B
    lenD += B
    if lenD >= N:
        break

A.sort()
for i, d in enumerate(D[:N]):
    if A[i] >= d:
        break
    A[i] = d

print((sum(A)))
# Original solution
# coding:utf-8

import math
import re

def main(params):
    n,m = params[0]
    a = params[1]
    tmpA = []
    for i in a:
        i = int(i)
        tmpA.append(i)
    a = tmpA
    a.sort()
    a.reverse()
    r = []
    for g in params[2:]:
        b = int(g.split()[0])
        c = int(g.split()[1])
        t = [c] * b
        r.extend(t)
    r.sort()
    r.reverse()
    cnt = 0
    total = 0
    while True:
        if len(a) == 0:
            total += r[0]
            r.pop(0)
        elif len(r) == 0:
            total += a[0]
            a.pop(0)
        elif a[0] < r[0]:
            total += r[0]
            r.pop(0)
        else:
            total += a[0]
            a.pop(0)
        cnt += 1
        if cnt == n:
            break
    print(total)
#    a = params[1].split()
#    b = list(params[2])

if __name__ == "__main__":
    n,m = input().split()
    n = int(n)
    m = int(m)
    a = input().split()
    cnt = m # TODO: edit here
    params = [[n,m],a]
    for i in range(cnt):
        param = input()
        params.append(param)
    main(params)



# Optimized solution
# coding:utf-8

import math
import re

def main(params):
    n,m = params[0]
    a = params[1]
    tmpA = []
    for i in a:
        i = int(i)
        tmpA.append(i)
    a = tmpA
    a.sort()
    a.reverse()
    r = {}
    for g in params[2:]:
        b = int(g.split()[0])
        c = int(g.split()[1])
        if c not in r:
            r[c] = b
        else:
            r[c] += b
#        t = [c] * b
#        r.extend(t)
#    r.sort()
#    r.reverse()
    rV = list(r.keys())
    rV.sort()
    rV.reverse()
    cnt = 0
    total = 0
    while True:
        if len(a) == 0:
            total += rV[0]
            r[rV[0]] -= 1
            if r[rV[0]] == 0:
                rV.pop(0)
        elif len(rV) == 0:
            total += a[0]
            a.pop(0)
        elif a[0] < rV[0]:
            total += rV[0]
            r[rV[0]] -= 1
            if r[rV[0]] == 0:
                rV.pop(0)
        else:
            total += a[0]
            a.pop(0)
        cnt += 1
        if cnt == n:
            break
    print(total)
#    a = params[1].split()
#    b = list(params[2])

if __name__ == "__main__":
    n,m = input().split()
    n = int(n)
    m = int(m)
    a = input().split()
    cnt = m # TODO: edit here
    params = [[n,m],a]
    for i in range(cnt):
        param = input()
        params.append(param)
    main(params)