CodeEff/ECCO huggingface dataset:
# Original solution
import sys
input = sys.stdin.buffer.readline

import bisect

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

for i in range(M):
    B, C = list(map(int,input().split()))
    index = bisect.bisect_left(A,C)
    if B < index :
        A = [C] * (B) + A[B:]
    else:
        A = [C] * (index) + A[index:]
    A.sort()
print((sum(A)))

# Optimized solution
import bisect
import itertools

N, M  = list(map(int,input().split()))
A = sorted(list(map(int,input().split())))
D = []

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

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

cnt = 0
ans = []
for b, c in D:
    for i in range(b):
        ans.append(c)
    cnt += b
    if cnt >=N:
        break
#print(D)
#print(ans)
res = sum(A)
for i in range(min(N,len(ans))):
    if ans[i]-A[i]>0:
        res += ans[i] - A[i]
    else:
        break
print(res)
# Original solution
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, m = list(map(int, input().split()))
    A = sorted(list(map(int, input().split())))
    query = sorted([list(map(int, input().split())) for _ in range(m)], key=lambda x: x[1],
                   reverse=True)

    B = []
    for i in range(m):
        b, c = query[i]
        while b and len(B) != n:
            B.append(c)
            b -= 1
    B.sort(reverse=True)

    for i in range(len(B)):
        if A[i] < B[i]:
            A[i] = B[i]
    print((sum(A)))


def resolve2():
    n, m = list(map(int, input().split()))
    A = []
    for a in map(int, input().split()):
        A.append([1, a])

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

    num, res = 0, 0
    for a, b in A:
        if num + a <= n:
            res += a * b
            num += a
        else:
            res += (n - num) * b
            break
    print(res)


if __name__ == '__main__':
    resolve2()


# Optimized solution
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, m = list(map(int, input().split()))
    A = list(map(int, input().split()))
    BC = sorted([list(map(int, input().split())) for _ in range(m)], key=lambda x: -x[1])
    C = []
    for b, c in BC:
        if len(C) >= n:
            break
        C += [c] * b
    A += C
    A.sort(reverse=True)
    res = sum(A[:n])
    print(res)


if __name__ == '__main__':
    resolve()
# Original solution
N,M = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
A.sort()
li = [0] * M
for i in range(M):
    B,C= [int(j) for j in input().split()]
    lia = [C,B]
    li[i] = lia
li.sort()
seq = []
for i in range(M):
    lia = [li[i][0] for _ in range(li[i][1])]
    seq += lia
for i in range(min(N,len(seq))):
    k = (i+1)* (-1)
    if A[i] >= seq[k]:
        break
    else:
        A[i] = seq[k]
print((sum(A)))

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

cb_dict = {}
for _ in range(m):
    b, c = list(map(int, input().split()))
    new = {c:b}
    if c in list(cb_dict.keys()):
        cb_dict[c] += b
    else:
        cb_dict.update(new)

cb_dict = sorted(list(cb_dict.items()), key=lambda x:x[0], reverse=True)

now_c = 0
now_b = 0
for i in range(n):
    if A[i] >= cb_dict[now_c][0]:
        break
    else:
        A[i] = cb_dict[now_c][0]
        now_b += 1
        if now_b == cb_dict[now_c][1]:
            now_c += 1
            now_b = 0
        if now_c > len(cb_dict)-1:
            break

ans = sum(A)
print(ans)
# Original solution
import copy
N,M = list(map(int,input().split()))
A = [int(i) for i in input().split()]
A.sort()
A_copy = copy.deepcopy(A)
CB = []
for i in range(M):
    b,c = list(map(int,input().split()))
    CB.append([c,b])
CB.sort(reverse = True)
tmp = 0
for c,b in CB:
    for i in range(b):
        if tmp+i >= N:
            print((sum(A)))
            exit()
            
        if A_copy[tmp+i]<c:
            A[tmp+i] = c
        else:
            break
    tmp += b
print((sum(A)))


# Optimized solution
n,m = list(map(int,input().split()))
A = [int(i) for i in 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)
ans = sum(A)
index = 0
for c,b in CB:
    for i in range(b):
        if index >=n:
            print(ans)
            exit()
        if A[index] < c:
            ans += c-A[index]
            A[index] = c
            index += 1
        else:
            print(ans)
            exit()
print(ans)
# Original solution
N, M = [int(elm) for elm in input().split(" ")] 

As = [int(elm) for elm in input().split(" ")] 

Bs = []
Cs = []
for i in range(M):
    B, C = [int(elm) for elm in input().split(" ")] 
    As += ([C] * B)

t = sorted(As, reverse=True)
out = sum(t[:N])
print(out)

# Optimized solution
N, M = [int(elm) for elm in input().split(" ")] 

As = [int(elm) for elm in input().split(" ")] 

dic = {}
keys= []
for A in As:
    if A not in keys:
        dic[A] = 1
        keys.append(A)
    else:
        dic[A] += 1

Bs = []
Cs = []
for i in range(M):
    B, C = [int(elm) for elm in input().split(" ")] 

    if C not in keys:
        dic[C] = B
        keys.append(C)
    else:
        dic[C] += B

    #Bs.append(B)
    #Cs.append(C)

keys = sorted(keys, reverse=True)

count = 0
out = 0

for key in keys:
    b = dic[key]
    if b < N - count:
        out += (key * b) 
        count += b
    else:
        out += (key * (N-count))
        break

print(out)
# Original solution
N, M = [int(elm) for elm in input().split(" ")] 

As = [int(elm) for elm in input().split(" ")] 

dic = {}
keys= []
for A in As:
    if A not in keys:
        dic[A] = 1
        keys.append(A)
    else:
        dic[A] += 1

Bs = []
Cs = []
for i in range(M):
    B, C = [int(elm) for elm in input().split(" ")] 

    if C not in keys:
        dic[C] = B
        keys.append(C)
    else:
        dic[C] += B

    #Bs.append(B)
    #Cs.append(C)

keys = sorted(keys, reverse=True)

count = 0
out = 0

for key in keys:
    b = dic[key]
    if b < N - count:
        out += (key * b) 
        count += b
    else:
        out += (key * (N-count))
        break

print(out)

# Optimized solution
N, M = [int(elm) for elm in input().split(" ")] 

As = sorted([int(elm) for elm in input().split(" ")], reverse=True)


BCs = []
for i in range(M):
    B, C = [int(elm) for elm in input().split(" ")] 
    BCs.append([B, C])

BCs = sorted(BCs, key=lambda x: -x[1])

out = 0
bc_i = 0
count = 0
B, C = BCs[bc_i]

for A in As:
    if C >= A and bc_i < M:
        while C >= A: 
            #print("plus", B, C, "A", A)
            out += (B*C)

            count += B
            bc_i += 1
            if bc_i == M or count > N:
                break
            B, C = BCs[bc_i]
            
    if count > N:
        out -= C * (count-N)
        break
    elif count == N:
        break

    out += A
    #print("plus", A)
    count += 1

print(out)
# Original solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
B_C = []
for _ in range(M):
    B, C = list(map(int, input().split()))
    B_C.append((C, B))
B_C = sorted(B_C, reverse=True)
Cs = []
rem = N
for c, b in B_C:
    if rem >= b:
        Cs += [c]*b
    else:
        Cs += [c]*rem
        break
ans = sum(sorted(A+Cs, reverse=True)[:N])
print(ans)

# Optimized solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
B_C = []
for _ in range(M):
    B, C = list(map(int, input().split()))
    B_C.append((C, B))
B_C = sorted(B_C, reverse=True)
Cs = []
rem = N
for c, b in B_C:
    if rem >= b:
        Cs += [c]*b
        rem -= b
    else:
        Cs += [c]*rem
        break
ans = sum(sorted(A+Cs, reverse=True)[:N])
print(ans)
# Original solution
N,M=list(map(int,input().split()))
A=list(map(int,input().split()))
CB=[[0,0] for _ in range(M)]

for i in range(M):
    CB[i][1],CB[i][0]=list(map(int,input().split()))
    
A.sort()
CB.sort(reverse=True)

#Aを小さい順に見る，Cが大きい順に変更操作をして確定していく
cc=0
ans=0
for i in range(N):
    if cc<M:
        if A[i]<CB[cc][0]:
            A[i]=CB[cc][0]
            CB[cc][1]-=1
            if CB[cc][1]==0:
                cc+=1
    ans+=A[i]
        
print(ans)

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

def I(): return int(eval(input()))
def MI(): return list(map(int, input().split()))
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N,M=MI()
    A=LI()
    CB=[[0,0]for _ in range(M)]
    A.sort()
    for i in range(M):
        CB[i][1],CB[i][0]=MI()
        
    CB.sort(reverse=True)
    now=0
    
    for i in range(N):
        if CB[now][1]<=0:
            now+=1
        
        if now>=M:
            break    
        
        ch=CB[now][0]
        if A[i]<ch:
            A[i]=ch
            CB[now][1]-=1
        else:
            break
        
    print((sum(A)))
    

main()
# Original solution
from itertools import chain
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
a.sort()
t=[]
for i in range(m):
    b,c=list(map(int,input().split()))
    t.append([c]*b)
t=list(chain.from_iterable(t))
t.sort(reverse=True)

ans=0

for i in range(n):
    if i<len(t):
        ans+=max(a[i],t[i])
    else:
        ans+=a[i]
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)]
a.sort()
t=[]
for i in range(m):
    for k in range(bc[i][0]):
        t.append(bc[i][1])
t.sort(reverse=True)
ans=0
 
for i in range(n):
    if i<len(t):
        ans+=max(a[i],t[i])
    else:
        ans+=a[i]
print(ans)
# Original solution
import bisect
n, m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a.sort()
for i in range(m):
    b, c = [int(i) for i in input().split()]
    idx = bisect.bisect_left(a, c)
    a[idx:idx]=[c]*b
a = a[::-1]
print((sum(a[:n])))

# Optimized solution
import bisect
n, m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a.sort()
d = []
cnt = 1
for i in range(n-1):
    if a[i+1] == a[i]:
        cnt += 1
    else:
        d.append([a[i], cnt])
        cnt = 1
d.append([a[n-1], cnt])


for i in range(m):
    b, c = [int(i) for i in input().split()]
    d.append([c, b])
d.sort(reverse=True)
ans = 0
j = 0
for i in range(n):
    if d[j][1] > 0:
        ans += d[j][0]
        d[j][1] -= 1
    if d[j][1] == 0:
        j += 1
print(ans)
# Original solution
# TLEを承知で出す
n, m = list(map(int, input().split()))

nums = sorted([int(i) for i in input().split()])[::-1]

for _ in range(m):
    a, b = list(map(int, input().split()))
    nums += [b] * a

nums = sorted(nums)[::-1]
print((sum(nums[:n])))


# Optimized solution
# from collections import defaultdict
# n, m = map(int, input().split())
# nums = [int(i) for i in input().split()]
#
# num_freq = defaultdict(int)
#
# for _ in range(m):
#     b, c = map(int, input().split())
#     num_freq[c] += b
#
#
# new_list = []
# for num, freq in sorted(num_freq.items(), key=lambda x: -x[0]):
#     new_list += [num] * freq
#     if len(new_list) >= n:
#         break
# tmp = sorted(nums + new_list)[::-1]
# print(sum(tmp[:n]))

# ↑と同じ処理なのに↑のはREになる
n, m = list(map(int, input().split()))
nums = list(map(int, input().split()))
exc = []

for i in range(m):
    exc.append(list(map(int, input().split())))
exc.sort(key=lambda x: x[1], reverse=True)
e = []
count = 0
for i in range(m):
    freq = exc[i][0]
    e += [exc[i][1]] * freq
    count += freq
    if count >= n:
        break
cards = sorted(nums + e, reverse=True)
print((sum(cards[:n])))
# Original solution
N,M=list(map(int, input().split()))
import sys
def input(): return sys.stdin.readline().strip()
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)
from bisect import bisect_left
ans=0
for m in range(M):
    if BC[m][0]<len(A) and A[BC[m][0]]<BC[m][1]:
        ans=ans+BC[m][1]*BC[m][0]
        A=A[BC[m][0]:]
    else:    
        r=bisect_left(A,BC[m][1])
        if r:
            ans=ans+BC[m][1]*r
            A=A[r:]
        break
ans+=sum(A)
print(ans)

# Optimized solution
N,M=list(map(int, input().split()))
import sys
def input(): return sys.stdin.readline().strip()
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)
from bisect import bisect_left
ans=0
l=0
for m in range(M):
    if BC[m][0]<len(A)-l and A[BC[m][0]+l]<BC[m][1]:
        ans=ans+BC[m][1]*BC[m][0]
        l+=BC[m][0]
    else:     
        r=bisect_left(A[l:],BC[m][1])
        if r:
            ans=ans+BC[m][1]*r
            l+=r
        break
ans+=sum(A[l:])
print(ans)
# Original solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for _ in range(M)]

for i in range(M):
  A += [BC[i][1]] * BC[i][0]

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

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

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

ans, left = 0, N
for (i, j) in D:
    use = min(j, left)
    ans += use * i
    left -= use
print(ans)
# Original solution
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
a.sort()
tmp=[]
change=[]
for i in range(m):
    q,r=list(map(int,input().split()))
    tmp.append([r,q])
tmp.sort(reverse=True)

for i in range(0,m):
    t=0
    cnt=0
    while t<tmp[i][1]:
        change.append(tmp[i][0])
        t+=1
        cnt+=1
    i+=cnt

for i in range(0,min(n,len(change))):
    a[i]=max(a[i],change[i])

print((sum(a)))


# Optimized solution
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
rep=[]
for i in range(m):
    b,c=list(map(int,input().split()))
    rep.append([c,b])
rep.sort(reverse=True)
Card=[0]*n
cnt=0
fg=0
while fg<n and cnt<m:
    tt=0
    while tt<rep[cnt][1] and fg+tt <n :
        Card[fg+tt]=rep[cnt][0]
        tt+=1
    fg+=tt
    cnt+=1

a.sort()
ans=[]
for i in range(n):
    ans.append(max(a[i],Card[i]))

print((sum(ans)))
# Original solution
N, M = list(map(int, input().split()))
A = sorted([int(i) for i in input().split()])

min_A = A[0]

BC = [[int(i) for i in input().split()] for _ in range(M)]

for B, C in BC :
    if C > min_A :
        A += [C] * B
        A = sorted(A)[B:]
        min_A = A[0]

print((sum(A)))


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

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

i = 0
res = 0

for B, C in BC :
    if i + B <= N :
        res += C * B
        i += B
    else :
        res += C * (N - i)
        break

print(res)
# Original solution
import heapq

from operator import itemgetter

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

a = list(map(int,input().split()))

info = [0] * m
for i in range(m):
    info[i] = list(map(int,input().split()))

info = sorted(info,key = itemgetter(1),reverse = True)
reinfo = info[0:n]
#print(a)

heapq.heapify(a)
#print(a)

d = 0

for j in range(len(reinfo)):
    for k in range(reinfo[j][0]):
        if a[0] > reinfo[j][1]:
            d = 1
            break
        else:
            heapq.heappushpop(a,reinfo[j][1])
    if d == 1:
        break

print((sum(a)))

# Optimized solution
import heapq

from operator import itemgetter

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

a = list(map(int,input().split()))

info = [0] * m
for i in range(m):
    info[i] = list(map(int,input().split()))

info = sorted(info,key = itemgetter(1),reverse = True)
#print(a)

heapq.heapify(a)
#print(a)

d = 0
count = 0

for j in range(len(info)):
    for k in range(info[j][0]):
        if a[0] > info[j][1]:
            d = 1
            break
        else:
            heapq.heappushpop(a,info[j][1])
            count += 1
        if count >= n:
            d = 1
            break
    if d == 1:
        break

print((sum(a)))
# Original solution
import cProfile
n,m = list(map(int,input().split()))
a = list(map(int, input().split()))
a.sort()
s = []
for i in range(m):
    b,c = list(map(int,input().split()))
    s += [c]*b
    
s.sort()
s.reverse()
d = len(s)
#print(a[n-1])
for i in range(n):
    if s[0] > a[i]:
        a[i] = s[0]
        s.remove(s[0])
    else:
        break
    if i == d-1:
        break


print((sum(a)))
#cProfile.run()



# Optimized solution
n,m = list(map(int,input().split()))
a = list(map(int, input().split()))
a.sort()
e =0
bc = []
for i in range(m):
    d = list(map(int,input().split()))
    bc.append([d[1],d[0]])
    e += d[0] 
#print(a)  
 
 
bc.sort()
bc.reverse()
#print(bc)
 
#print(e)
#print(a[n-1])
j = 0
for i in range(n):
    if j == bc[0][1] :
        bc.remove(bc[0])
        j = 0
    if bc[0][0] > a[i]:
        a[i] = bc[0][0]
        j += 1
    else:
        break
    if i == e-1:
        break
 
 
#print(a)
print((sum(a)))
# Original solution
def main():
  N, M = list(map(int, input().split()))
  C = list(map(int, input().split()))
  C = sorted(C)
  AB = [list(map(int, input().split())) for i in range(M)]
  AB = sorted(AB, key=lambda x: x[1], reverse=True)
  AB = [ab[1] for ab in AB for i in range(ab[0])]
  
  index = min([N, len(AB)])
  result = 0
  for i in range(index):
    if AB[i] < C[i]:
      index = i
      break
    result += AB[i]
  
  if index < N:
    result += sum(C[index:])
  print(result)

if __name__ == "__main__":
  main()

# Optimized solution
def main():
  N, M = list(map(int, input().split()))
  C = list(map(int, input().split()))
  C = sorted(C)
  AB = [list(map(int, input().split())) for i in range(M)]
  AB = sorted(AB, key=lambda x: x[1], reverse=True)
  
  result = 0
  i, j = 0, 0
  while i < N:
    A, B = AB[j] if j < M else (0, 0)
    j += 1
    if i+A < N and B >= C[i+A]:
      i += A
      result += A * B
    else:
      break
    
  while i < N:
    if C[i] > B:
      result += sum(C[i:])
      break
    else:
      result += B
      A -= 1      
      if j < M and A == 0:
        A, B = AB[j] if j < M else (0, 0)
        j += 1
    
    i += 1
  
  print(result)

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


n,m = [int(_) for _ in input().split()]
a = [int(_) for _ in input().split()]
bc = []
for i in range(m):
    bc.append([int(_) for _ in input().split()])
bc = sorted(bc, key=lambda x:x[1])
heapq.heapify(a)
for _ in range(m):
    b,c = bc.pop()
    for i in range(b):
        num = heapq.heappop(a)
        if num < c:
            heapq.heappush(a, c)
        else:
            heapq.heappush(a, num)
ans = 0
for i in range(len(a)):
    ans += a[i]
print(ans)

# Optimized solution
import bisect


n,m = [int(_) for _ in input().split()]
a = [int(_) for _ in input().split()]
bc = []
for i in range(m):
    bc.append([int(_) for _ in input().split()])
bc = sorted(bc, key=lambda x:x[1])
a = sorted(a)
t = 0
ans = 0
for _ in range(m):
    b,c = bc.pop()
    idx = bisect.bisect_left(a, c)
    if idx > t:
        ans += c * min(idx - t, b)
        t += min(idx - t, b)
    else:
        continue
for i in range(t, n):
    ans += a[i]
print(ans)
# Original solution
n,k = list(map(int,input().split()))
li = list(map(int,input().split()))
rc = [list(map(int,input().split())) for i in range(k)]

for i in rc:
    li = li + [i[1]]*i[0]

print((sum(sorted(li, reverse=True)[0:n])))

# Optimized solution
n,k = list(map(int,input().split()))
li = list(map(int,input().split()))
rc = [list(map(int,input().split())) for i in range(k)]

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

tmp = 0

for i in rc:
    li += [i[1]]*i[0]
    tmp += i[0]
    if tmp>n:
        break 

print((sum(sorted(li, reverse=True)[0:n])))
# Original solution
n,m=list(map(int,input().split()))
a=[int(_) for _ in input().split()]
for iii in range(m):
  b,c=list(map(int,input().split()))
  a+=[c]*b

a=sorted(a,reverse=True)
print((sum(a[0:n])))


# Optimized solution
from heapq import heappush, heappop
n,m=list(map(int,input().split()))
hq=[]
for _ in input().split():
  heappush(hq,(-int(_),1))
for iii in range(m):
  b,c=list(map(int,input().split()))
  heappush(hq,(-c,b))
s=0
for iii in range(n):
  a=heappop(hq)
  s-=a[0]
  if a[1]!=1:
    heappush(hq,(a[0],a[1]-1))
print(s)
# Original solution
def main():
    N, M = list(map(int, input().split()))
    A = [int(x) for x in input().split()]
    for m in range(0, M):
        tmp = [int(x) for x in input().split()]
        A.extend([tmp[1]] * min(tmp[0],N))
 
    A.sort()
    A.reverse()
    A = A[:N]
    print((sum(A)))
 
 
if __name__ == "__main__":
    main()

# Optimized solution
def main():
    N, M = list(map(int, input().split()))
    A = [int(x) for x in input().split()]
    changes = []
    for i in range(M):
    	changes.append([int(e) for e in input().split(" ")])
    changes.sort(key=lambda x:(x[1],x[0]),reverse = True)
	
    cnt = N
    for i in changes:
    	A.extend([i[1]] * min(i[0],cnt))
    	cnt -= min(i[0],cnt)
    A.sort()
    A.reverse()
    A = A[:N]
    print((sum(A)))
if __name__ == "__main__":
    main()
# Original solution
from operator import itemgetter
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
op = [list(map(int, input().split())) for _ in range(m)]
op.sort(key=itemgetter(1), reverse=True)
a.sort()

idx = 0
for b, c in op:
    for i in range(b):
        if idx < n and a[idx] < c:
            a[idx] = c
            idx += 1
        if idx >= n:
            break
    if idx >= n:
        break


print((sum(a)))


# Optimized solution
from operator import itemgetter
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
op = [list(map(int, input().split())) for _ in range(m)]
op.sort(key=itemgetter(1), reverse=True)
a.sort()

idx = 0
ans = 0
for i in range(n):
    if idx < m and op[idx][1] > a[i]:
        ans += op[idx][1]
    else:
        ans += a[i]

    if idx < m and op[idx][0] > 1:
        op[idx][0] -= 1
    else:
        idx += 1


print(ans)
# Original solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
L = []
cnt = 0
for i in range(M):
    L.append(list(map(int, input().split())))
sL = sorted(L, key=lambda x:x[1], reverse = True)

for i in range(M):
    if cnt > N:
        break
    if L[i][1] <= min(A):
        pass
    else:
        for j in range(L[i][0]):
            A.append(L[i][1])
            A.remove(min(A))
            cnt += 1
            if cnt > N:
                break

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

# Optimized solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
K = [1] * N
L = list(zip(K,A))
for i in range(M):
	L.append(list(map(int, input().split())))
S = sorted(L, key = lambda x:x[1], reverse = True)
sum = 0
cnt = 0
for i in range(N):
	if N - cnt <= S[i][0]:
		sum += S[i][1] * (N - cnt)
		break
	else:
		sum += S[i][1] * S[i][0]
		cnt += S[i][0]
print(sum)
# Original solution
def main():
    from heapq import heapify,heapreplace
    import sys
    input = sys.stdin.readline
    n,m = list(map(int,input().split()))
    A = list(map(int,input().split()))
    BC = [list(map(int,input().split())) for _ in range(m)]

    heapify(A)

    BC = sorted(BC,key=lambda x:(x[1]),reverse=True)
    for b,c in BC:
        if c<A[0]:
            break
        for i in range(b):
            if A[0]>c:
                break
            heapreplace(A,c)
        
    #print(A)
    print((sum(A)))
if __name__=='__main__':
    main()

# Optimized solution
def main():
    import sys
    input = sys.stdin.readline
    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]),reverse=True)
    append_list = []
    for b,c in BC:
        if len(append_list)>=n:
            break
        append_list+=[c]*b
    A+=append_list
    A.sort(reverse=True)
    print((sum(A[:n])))
if __name__=='__main__':
    main()