CodeEff/ECCO huggingface dataset:
# Original solution
import sys
input = sys.stdin.readline
import heapq
N,M=list(map(int,input().split()))
list1=list(map(int,input().split()))
for _ in range(M):
  B,C=list(map(int,input().split()))
  list1+=[C]*B
list1=heapq.nlargest(N,list1)
SUM=sum(list1)
print(SUM)

# Optimized solution
import sys
input = sys.stdin.readline
N,M=list(map(int,input().split()))
list1=list(map(int,input().split()))
for _ in range(M):
  B,C=list(map(int,input().split()))
  if len(list1)<N or min(list1)<C:
    list1+=[C]*B
    if len(list1)>N:
      for i in range(len(list1)-N):
        list1.remove(min(list1))
SUM=sum(list1)
print(SUM)
# Original solution
import sys
input = sys.stdin.readline
N,M=list(map(int,input().split()))
list1=list(map(int,input().split()))
for _ in range(M):
  B,C=list(map(int,input().split()))
  list1+=[C]*B
while len(list1)>=N+1:
  list1.remove(min(list1))
SUM=sum(list1)
print(SUM)

# Optimized solution
import sys
input = sys.stdin.readline
N,M=list(map(int,input().split()))
list1=list(map(int,input().split()))
for _ in range(M):
  B,C=list(map(int,input().split()))
  list1+=[C]*B
  for _ in range(B):
    list1.remove(min(list1))
SUM=sum(list1)
print(SUM)
# Original solution
import sys
input = sys.stdin.readline
N,M=list(map(int,input().split()))
list1=list(map(int,input().split()))
list2=list(set(list1))
listBC=[[list2[i],list1.count(list2[i])] for i in range(len(list2))]
listC=[list2[i] for i in range(len(list2))]
for _ in range(M):
  B,C=list(map(int,input().split()))
  listBC+=[[C,B]]
listBC.sort(reverse=True)
i=-1
SUM=0
ans=0
while SUM<N:
  i+=1
  ans+=listBC[i][0]*listBC[i][1]
  SUM+=listBC[i][1]
ans-=listBC[i][0]*(SUM-N)  
print(ans)

# Optimized solution
N,M = list(map(int, input().split()))
listA = list(map(int, input().split()))
listA.sort()
 
listBC = [list(map(int, input().split())) for i in range(M)]
listBC = sorted(listBC, key=lambda x: x[1], reverse=True)
countB = 0
 
for i in range(M):
  B, C = listBC[i][0], listBC[i][1]
  listA += [C]*B
  countB += B
  if countB >= N:
    break

listA.sort(reverse=True)
print((sum(listA[:N])))
# Original solution
import sys
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def II(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
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 SI(): return input()
YN = lambda b: print('YES') if b else print('NO')
yn = lambda b: print('Yes') if b else print('No')

N, M = LI()
A = LI()
BandC = []
for i in range(M):
    BandC.append(LI())

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

BandC_list = []
for i in BandC:
    BandC_list += [i[1]]*i[0]

ans = 0
for i, a in enumerate(A):
    if i < len(BandC_list):
        ans += max(a, BandC_list[i])
    else:
        ans += a

print(ans)


# Optimized solution
import sys
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def II(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
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 SI(): return input()
YN = lambda b: print('YES') if b else print('NO')
yn = lambda b: print('Yes') if b else print('No')

def main():
    N, M = LI()
    A = LI()
    BandC = []
    for i in range(M):
        BandC.append(LI())
 
    A = sorted(A)
    BandC = sorted(BandC, key=lambda x: x[1], reverse=True)
 
    BandC_counter = 0
    BandC_position = 0
    BandC_sum = 0
    for A_position in range(N):
        if A[A_position] > BandC[BandC_position][1]:
            break
        BandC_sum += BandC[BandC_position][1]
        BandC_counter += 1
        if BandC_counter >= BandC[BandC_position][0]:
            BandC_position += 1
            BandC_counter = 0
            if BandC_position >= len(BandC):
                A_position += 1
                break
    else:
        A_position += 1
 
    print(BandC_sum + sum(A[A_position:]))

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

a.sort()
bc = [list(map(int,input().split())) for _ in range(m)]
bc.sort(key=lambda x:x[1],reverse=True)
l = []
[l.extend([i[1]] * i[0]) for i in bc]

for i in range(min(len(a),len(l))):
    a[i]=max(a[i],l[i])
print((sum(a)))

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

a.sort()
bc = [list(map(int,input().split())) for _ in range(m)]
bc.sort(key=lambda x:x[1],reverse=True)
l = []
for i in bc:
    l.extend([i[1]] * i[0])
    if len(l) > len(a):
        break

for i in range(min(len(a),len(l))):
    a[i] = max(a[i],l[i])
print((sum(a)))
# Original solution
#-------------------------------------------------------------------
import sys
def p(*a):
  s=" ".join(map(str,a))
  #print(s)
  sys.stderr.write(s+"\n")
#-------------------------------------------------------------------

N, M = list(map(int, input().split()))			# "5 7" -> ["5", "7"] -> 5, 7 => N=5,K=7

A = sorted(list( map(int, input().split()) ))	# 1 2 3 4 5 ...(ソート付き)

p("A ",A)

B=[]
for i in range(M):
	b,c = list(map(int, input().split()))			# "5 7" -> ["5", "7"] -> 5, 7 => N=5,K=7
	B.append([b,c])


p("B ",B)
B=sorted(B, key=lambda x: x[1]*-1)
p("B ",B)


C=[]

x=[]
for b in B:
	x = x + ( [b[1]]*b[0] )

p("x ", x)

iMax=min(len(A), len(x))
for i in range(iMax):
	if A[i]<x[i]: A[i]=x[i]

print((sum(A)))



# Optimized solution
#-------------------------------------------------------------------
import sys
def p(*a):
  s=" ".join(map(str,a))
  #print(s)
  sys.stderr.write(s+"\n")
#-------------------------------------------------------------------

N, M = list(map(int, input().split()))			# "5 7" -> ["5", "7"] -> 5, 7 => N=5,K=7

A = sorted(list( map(int, input().split()) ))	# 1 2 3 4 5 ...(ソート付き)

p("A ",A)

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


p("B ",B)
B=sorted(B, key=lambda x: x[1]*-1)
p("B ",B)


a=0
for i in range(len(B)):
	b = B[i]
	for j in range(b[0]):
		
		if A[a]<b[1]: A[a]=b[1]
		a+=1
		if a>=len(A): break

	if a>=len(A): break


print((sum(A)))
# Original solution
import bisect
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])) 
for b,c in bc:
    index = bisect.bisect_left(a, c)
    d = min(index,b)
    for j in range(d):
        a[j] = c
    a.sort()

print((sum(a)))
    


# Optimized solution
n,m = list(map(int, input().split()))
a = sorted(list(map(int,input().split())))
d = [list(map(int, input().split())) for _ in range(m)]
d.sort(key=lambda x:(-x[1]))
e = []
for b,c in d:
    e += [c]*b
    if len(e) >= n:
        break
e = sorted(e)[::-1]
ans = 0
j = 0
for i in range(n):
    if j < len(e):
        if a[i] >= e[j]:
            ans += a[i]
        else:
            ans += e[j]
            j += 1
    else:
        ans += a[i]
print(ans)
# Original solution
n,m = list(map(int,input().split()))
a = [int(i) for i in input().split()]
for i in range(m):
    b,c = list(map(int,input().split()))
    a.extend([c]*b)
a.sort()
print((sum(a[len(a)-n:])))

# Optimized solution
n,m = list(map(int,input().split()))
a = [[1,int(i)] for i in input().split()]
bc = [[int(i) for i in input().split()] for j in range(m)]
x = a+bc
x.sort(key=lambda y:y[1])
total = 0
ans = 0
for i in reversed(list(range(n+m))):
    if total >= n:
        break
    else:
        ans += x[i][1]*min(n-total,x[i][0])
        total += x[i][0]
print(ans)
# Original solution
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()
 
n,m = na()
a = na()
for i in range(m):
    b,c = na()
    a += [c]*b

aa = list(reversed(sorted(a)))

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

# Optimized solution
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()
 
n,m = na()
a = na()
for i in range(m):
    b,c = na()
    for j in range(b):
        a.append(c)

aa = sorted(a)

print((sum(aa[len(aa)-n:])))
# Original solution
import sys
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n,m = na()
a = sorted(na())
k = 0

import heapq

for i in range(m):
    b,c = na()
    k += b
    for j in range(b):
        heapq.heappush(a, c)

for i in range(k):
    heapq.heappop(a)

print((sum(a)))


# Optimized solution
import sys
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n,m = na()
a = na()

bc = [na() for i in range(m)]
bc.sort(key=lambda x:x[1])

for b,c in bc[::-1]:
  a.extend([c]*b)
  if len(a)>2*n:
    break

a.sort(reverse=True)
print((sum(a[:n])))
# Original solution
from bisect import bisect_left


def main():
    n, m = list(map(int, input().split()))
    arr = list(map(int, input().split()))
    arr.sort()
    s = 0

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

    for b, c in bc:
        idx = bisect_left(arr, c)

        if b >= idx:
            s += sum(arr[idx:])
            arr = [c] * idx
        else:
            s += sum(arr[idx:])
            arr = arr[b:idx] + [c] * b

        if not arr:
            break

    print((s + sum(arr)))


main()

# Optimized solution
def main():
    n, m = list(map(int, input().split()))
    a = list(map(int, input().split()))
    s = 0

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

    arr = []
    sum_b = 0
    for b, c in bc:
        arr += [c] * b
        sum_b += b
        if sum_b >= n:
            break

    arr += a
    arr.sort(reverse=True)
    print((sum(arr[:n])))


main()
# Original solution
import sys
from heapq import heapify, heappushpop
from operator import itemgetter

if sys.platform =='ios':
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(eval(input()))
def MAP(): return [int(s) for s in input().split()]

def main():
	N, M = MAP()
	A = MAP()
	BC = [MAP() for _ in range(M)]
	
	BC.sort(key= itemgetter(1), reverse=True)
	
	heapify(A)
	
	fin = False
	for b, c in BC:
		#print(A, b, c)
		for _ in range(b):
			a = A[0]
			if a>c:
				fin = True
				break
			else:
				heappushpop(A, c)
		if fin:
			break
	
	print((sum(A)))

if __name__ == '__main__':
	main()

# Optimized solution
import sys
from heapq import heapify, heappop
from operator import itemgetter

if sys.platform =='ios':
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(eval(input()))
def MAP(): return [int(s) for s in input().split()]

def main():
	N, M = MAP()
	A = MAP()
	BC = [MAP() for _ in range(M)]
	
	BC.sort(key= itemgetter(1), reverse=True)
	
	heapify(A)
	
	ans = 0
	
	fin = False
	for b, c in BC:
		#print(A, b, c)
		for _ in range(b):
			a = A[0]
			if a>c:
				fin = True
				break
			else:
				heappop(A)
				ans += c
				N -= 1
				if N == 0:
					fin = True
					break
		if fin:
			break
	
	print((ans+sum(A)))

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

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

for i in range(M):
    B,C = list(map(int, input().split()))
    insert_index = bisect.bisect_left(A,C)
    if insert_index == 0 and C > A[0]:
        A[0] = C
        continue
    elif insert_index == 0:
        continue

    if insert_index >= B:
        tmp = A[B:insert_index]+[C]*B+A[insert_index:]
    else:
        tmp = [C]*insert_index+A[insert_index:]
    A = tmp

print((sum(A)))

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

BC = []

for i in range(M):
    BC.append(list(map(int, input().split())))

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

bc = 0
a = 0
ans = 0
for i in range(N):
    if (bc < M) and (BC[bc][1] >= A[a]):
        ans += BC[bc][1]
        BC[bc][0] -= 1
        if BC[bc][0] == 0:
            bc += 1
    else:
        ans += A[a]
        a += 1

print(ans)
# Original solution
from bisect import bisect_left

N, M = list(map(int, input().split()))
d = dict()
A = list(map(int, input().split()))
for i in range(N):
	d[A[i]] = d.get(A[i], 0) + 1
for i in range(M):
	B, C = list(map(int, input().split()))
	d[C] = d.get(C, 0) + B

ans = 0
keys = sorted(list(d.keys()), reverse = True)
for k in keys:
	kosuu = min(N, d.get(k))
	ans += k * kosuu
	N -= kosuu
	if N == 0:
		break
print(ans)

# Optimized solution
N, M = list(map(int, input().split()))
d = dict()
A = list(map(int, input().split()))
for i in range(N):
	d[A[i]] = d.get(A[i], 0) + 1
for i in range(M):
	B, C = list(map(int, input().split()))
	d[C] = d.get(C, 0) + B

ans = 0
keys = sorted(list(d.keys()), reverse = True)
for k in keys:
	kosuu = min(N, d.get(k))
	ans += k * kosuu
	N -= kosuu
	if N == 0:
		break
print(ans)
# Original solution
N, M = list(map(int, input().split()))
A = sorted(list(map(int, input().split())))

for i in range(M):
    Bi, Ci = list(map(int, input().split()))
    A.extend([Ci] * Bi)

print((sum(sorted(A, reverse=True)[:N])))

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

for i in range(M):
    Bi, Ci = list(map(int, input().split()))
    A.extend([Ci] * Bi)

print((sum(sorted(A, reverse=True)[:N])))
# Original solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))

for i in range(M):
    Bi, Ci = list(map(int, input().split()))
    A.extend([Ci] * Bi)

print((sum(sorted(A, reverse=True)[:N])))

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

for i in range(M):
    Bi, Ci = list(map(int, input().split()))
    if Ci not in card:
        card[Ci] = Bi
    else:
        card[Ci] += Bi

total = 0
count = 0
for num in sorted(list(card.keys()), reverse=True):
    if count + card[num] <= N:
        count += card[num]
        total += num * card[num]
    else:
        added = N - count
        count += added
        total += added * num
        break
print(total)
# Original solution

import sys
from collections import deque, defaultdict
import copy
import bisect
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
import math

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)

A.sort()
for i in range(M):
	loc = bisect.bisect_left(A, C[i])
	loc2 = min(B[i], loc)
	A[loc:loc] = [C[i]]*loc2
	A = A[loc2:]
    
print((sum(A)))

# Optimized solution


import sys
def input():
	return sys.stdin.readline().strip()

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

sumb = 0
change = []
while sumb <= N and len(CB) > 0:
	C, B = CB.pop()
	sumb += B
	change.extend([C]*B)

change = change[0:N]

for i in range(len(change)):
	if A[i] < change[i]:
		A[i] = change[i]
	else:
		break

print((sum(A)))
# Original solution
import heapq

N, M = list(map(int, input().split()))
A = list([int(x) for x in input().split()])
heapq.heapify(A)

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

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

print((sum(A)))



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

for i in range(M):
    Q.append(tuple(map(int, input().split())))

A.sort()

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

cur = 0
for i in range(M):
    cnt = Q[i][0]
    while cnt > 0 and cur < N:
        if A[cur] >= Q[i][1]:
            break
        A[cur] = Q[i][1]
        cur += 1
        cnt -= 1

print((sum(A)))
# Original solution
num1, num2 = [int(i) for i in input().split()]
num3 = [int(i) for i in input().split()]

numArray = [[0]*2]*num2

for index3 in range(num2):
    numArray[index3] = [int(j) for j in input().split()]


for index1 in range(num2):
    num4 = 0
    num3.sort()
    #numArray[index1].sort()
    for index2 in range(num1):

        if num3[index2] < numArray[index1][1]:
            num3[index2] = numArray[index1][1]
            num4 += 1
        if num4 >= numArray[index1][0] :
            break
print((sum(num3)))

# Optimized solution
num1, num2 = [int(i) for i in input().split()]
num3 = [int(i) for i in input().split()]

numArray = [[0]*2]*num2

for index3 in range(num2):
    numArray[index3] = [int(j) for j in input().split()]

numArray.sort(key=lambda x:(-x[1]))

fg1 = True

for index1 in range(num2):
    for index2 in range(numArray[index1][0]):
        num3.append(numArray[index1][1])
        if len(num3)>=(num1*2):
            fg1= False
            break
    if fg1 == False:
        break

num3.sort(reverse=True)

sum1 = 0
for index4 in range(num1):
    sum1 +=num3[index4]

print((str(sum1)))
# Original solution
import heapq

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

heapq.heapify(A)

for _ in range(M):
    b,c=list(map(int,input().split()))

    i=0
    while i<b:
        a=heapq.heappop(A)

        if a>=c:
            heapq.heappush(A,a)
            break

        heapq.heappush(A,c)
        i+=1

print((sum(A)))


# Optimized solution
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],reverse=True)

D=[0]*N
i=0
for b,c in bc:
    cnt=0
    while i<N and cnt<b:
        D[i]=c
        i+=1
        cnt+=1
    if i==N:
        break

E=[max(A[i],D[i]) for i in range(N)]
print((sum(E)))
# 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)]

minA = min(A)
C = [[j]*i for i,j in BC if j > minA]
for i in range(len(C)):
    A = A[:N]
    if C[i][0] > min(A):
        A.extend(C[i])
        A.sort(reverse=True)


print((sum(A[:N])))


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

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

idx = 0
for b, c in BC:
  for j in range(idx, min(N, idx + b)):
    if A[j] < c:
      A[j] = c
  idx += b
print((sum(A)))
# Original solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
BC = []
for _ in range(M):
    bc = list(map(int, input().split()))
    BC.append(bc)
BC = sorted(BC, key=lambda x:x[1], reverse=True)

for b, c in BC:
    A = sorted(A)
    if A[0] >= c:
        break
    for i in range(b):
        if A[i] < c:
            A[i] = c
        else:
            break
print((sum(A)))

# Optimized solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
BC = []
for _ in range(M):
    bc = list(map(int, input().split()))
    BC.append(bc)
BC = sorted(BC, key=lambda x:x[1], reverse=True)
cnt = 0
for b, c in BC:
    A += [c] * b
    cnt += b
    if cnt >= N:
        break
A.sort(reverse=True)
print((sum(A[:N])))
# Original solution
n,m = list(map(int,input().split()))
alis = list(map(int,input().split()))
lis = [list(map(int,input().split())) for x in range(m)]
clis = [[x[1]]*x[0] for x in lis]
clis=(sum(clis,[]))
clis.sort(reverse=True)
alis.sort()
num = 0
ans_lis=[]
for x,y in zip(alis,clis):
    if x>=y:
        ans_lis.append(x)
    else:
        ans_lis.append(y)
    num += 1
if num < len(alis):
    ans__ = sum(alis[num:])
    print((sum(ans_lis,ans__)))
else:
    print((sum(ans_lis)))


# Optimized solution
n,m = list(map(int,input().split()))
alis = list(map(int,input().split()))
lis = [list(map(int,input().split())) for x in range(m)]
clis = [[x[1]]*x[0] for x in lis]
clis=(sum(clis,[]))
clis.sort(reverse=True)
alis.sort()
ans_lis=[x if x>=y else y for x,y in zip(alis,clis)]
if len(clis) < len(alis):
    ans__ = sum(alis[len(clis):])
    print((sum(ans_lis,ans__)))
else:
    print((sum(ans_lis)))
# Original solution
n,m = list(map(int,input().split()))
alis = list(map(int,input().split()))
lis = [list(map(int,input().split())) for x in range(m)]
clis = [[x[1]]*x[0] for x in lis]
clis=(sum(clis,[]))
clis.sort(reverse=True)
alis.sort()
ans_lis=[x if x>=y else y for x,y in zip(alis,clis)]
if len(clis) < len(alis):
    ans__ = sum(alis[len(clis):])
    print((sum(ans_lis,ans__)))
else:
    print((sum(ans_lis)))


# Optimized solution
n,m = list(map(int,input().split()))
alis = list(map(int,input().split()))
lis = [list(map(int,input().split())) for x in range(m)]
clis = [[x[1]]*x[0] for x in lis]
clis = [x for y in clis for x in y]
clis.sort(reverse=True)
alis.sort()
ans_lis=[x if x>=y else y for x,y in zip(alis,clis)]
if len(clis) < len(alis):
    ans__ = sum(alis[len(clis):])
    ans_=sum(ans_lis)
    print((ans_+ans__))
else:
    print((sum(ans_lis)))
# Original solution
n,m = list(map(int,input().split()))
alis = list(map(int,input().split()))
lis = [list(map(int,input().split())) for x in range(m)]
clis = [[x[1]]*x[0] for x in lis]
clis = [x for y in clis for x in y]
l = len(clis)
clis.sort(reverse=True)
alis.sort()
ans = 0
for x,y in zip(alis,clis):
    ans += max(x,y)
if n>l:
    ans += sum(alis[l:])
print(ans)


# Optimized solution
n,m = list(map(int,input().split()))
alis = list(map(int,input().split()))
lis = [list(map(int,input().split())) for x in range(m)]
for x in alis:
    lis.append([1,x])
lis.sort(reverse=True,key=lambda x:x[1])
ans = 0
for x in lis:
    (number,point) = x
    if number >= n:
        number = n
        ans += number * point
        #print(n)
        #print(ans,'+=',number,'*',point)
        break
    ans += number * point
    n -= number
    #print(n)
    #print(ans,'+=',number,'*',point)
print(ans)
#print(lis)
# Original solution
import sys
input=sys.stdin.readline
n,m=list(map(int,input().split()))
a=sorted(list(map(int,input().split())))
bc=[]

for i in range(m):
    b,c=list(map(int,input().split()))
    bc+=[c]*b
num=min(len(bc),n)
bc=sorted(bc,reverse=True)[:num]

ans=sum(a)
temp=sum(a)
for i in range(n):
    if num<=i:continue
    temp+=-a[i]+bc[i]
    if temp>ans:ans=temp
print(ans)

# Optimized solution
import sys
input=sys.stdin.readline
n,m=list(map(int,input().split()))
a=sorted(list(map(int,input().split())))
bc=[list(map(int,input().split())) for i in range(m)]
bc=sorted(bc,reverse=True,key=lambda x:x[1])
c=[]

cnt=0
for B,C in bc:
    for i in range(B):
        if cnt==n:break
        cnt+=1
        c.append(C)
c=sorted(c,reverse=True)

ans=sum(a)
temp=ans
for i in range(n):
    if cnt<=i:continue
    temp+=-a[i]+c[i]
    if temp>ans:ans=temp
print(ans)