CodeEff/ECCO huggingface dataset:
# 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)]
BC.sort(key=lambda x: x[1], reverse=True)
D = []
for B, C in BC:
    D += [C] * B
A+=D
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 _ in range(M)]
BC.sort(key=lambda x: x[1], reverse=True)
D = []
sum_B=0
for B, C in BC:
    D += [C] * B
    sum_B += B
    if sum_B>N:
        break

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

for i in range(M):
    b, c=list(map(int, input().split()))
    
    if c>k[0]:
        for j in range(b):
            if c-k[j]>0:
                k[j]=c
            else:
                break
    k.sort()

print((sum(k)))

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

card=[]

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

card.sort(reverse=True)
k=0
for c, b in card:
    while k < N and b > 0:
        if a[k]<c:
            a[k]=c
            b-=1
        k+=1

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

card=[]

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

card.sort(reverse=True)
k=0
for c, b in card:
    while k < N and b > 0:
        if a[k]<c:
            a[k]=c
            b-=1
        k+=1

print((sum(a)))

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

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

change.sort(reverse=True)
elem=0

for c, b in change:
    while elem<n and b>0:
        if a[elem]<c:
            a[elem]=c
            b-=1
        elem+=1

print((sum(a)))
# Original solution
import sys

N, M = list(map(int, input().split()))
A = list(map(int, sys.stdin.readline().rsplit()))
A.sort()
C = []

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

C.sort(reverse=True)

res = 0
for i in range(N):
    if i < len(C):
        if A[i] < C[i]:
            res += C[i]
        else:
            res += A[i]
    else:
        res += A[i]
print(res)


# Optimized solution
import sys

N, M = list(map(int, input().split()))
A = list(map(int, sys.stdin.readline().rsplit()))
BC = [list(map(int, sys.stdin.readline().rsplit())) for _ in range(M)]
A.sort()
BC.sort(reverse=True, key=lambda x: x[1])
C = []

for b, c in BC:
    if len(C) >= N:
        break
    for _ in range(b):
        C.append(c)

C.sort(reverse=True)

res = 0
lenC = len(C)
for i in range(N):
    if i < lenC and A[i] < C[i]:
        res += C[i]
    else:
        res += A[i]
print(res)
# Original solution
x,y= list(map(int, input().split()))
a = list(map(int, input().split()))
b=[]
for _ in range(y):
    b.append(list(map(int, input().split())))
b.sort(key = lambda x: x[1],reverse=True) 
for l in b:
    a.extend([l[1]]*l[0])
a.sort(reverse=True)
print((sum(a[:x])))


# Optimized solution
x,y= list(map(int, input().split()))
a = list(map(int, input().split()))
#print(a)
b=[]
for _ in range(y):
    b.append(list(map(int, input().split())))
b.sort(key = lambda x: x[1],reverse=True) 
for l in b:
    if(len(a)<=x*2):
        a.extend([l[1]]*l[0])
#print(len(a))        
#print(a)
a.sort(reverse=True)
print((sum(a[:x])))
# Original solution
import heapq
N,M=list(map(int,input().split()))
A=list(map(int,input().split()))
heapq.heapify(A)
bc=[]
for i in range(M):
  bc.append(list(map(int,input().split())))

for i in range(M):
  for j in range(bc[i][0]):
    if A[0]<bc[i][1]:
      heapq.heappop(A)
      heapq.heappush(A,bc[i][1])
    else:
      break

print((sum(A)))

# Optimized solution
N,M=list(map(int,input().split()))
A=list(map(int,input().split()))
A.sort()
bc=[]
for i in range(M):
  bc.append(list(map(int,input().split())))

bc.sort(key=lambda x:x[1],reverse=True)
nbc=[0]*N
inde=0
for i in range(M):
  for j in range(bc[i][0]):
    if inde<=N-1:
      nbc[inde]=bc[i][1]
      inde+=1
      if inde==N:
        break
  if inde==N:
    break
  
ind=N
for i in range(N):
  if A[i]>=nbc[i]:
    ind=i
    break

print((sum(nbc[:ind])+sum(A[ind:])))
# Original solution
n,m = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort()
bc = sorted([list(map(int, input().split())) for i in range(m)], key =lambda x:x[1])

t = True
cnt = 0

while t == True and cnt < len(bc):
  cnt += 1
  num = bc[-cnt][0]
  large = bc[-cnt][1]
  if large > min(a):
    for i in a:
      if i < large and a.index(i)+1 <= num:
        a[a.index(i)] = large
      else:
        break
    a.sort()
  else:
     t = False

print((sum(a)))

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

flg = 0
cnt = 0

for i in range(n):
  if bc[flg][1] >a[i] and flg < m:
    a[i] = bc[flg][1]
    cnt +=1
    if cnt == bc[flg][0]:
      flg += 1
      cnt = 0
      if flg == m:
        break
  else:
      break

print((sum(a)))
# Original solution
n,m = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort()
bc = sorted([list(map(int, input().split())) for i in range(m)], key =lambda x:x[1])
bc.reverse()

flg = 0
cnt = 0

for i in range(n):
  if bc[flg][1] >a[i] and flg < m:
    a[i] = bc[flg][1]
    cnt +=1
    if cnt == bc[flg][0]:
      flg += 1
      cnt = 0
      if flg == m:
        break
  else:
      break

print((sum(a)))

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

flg = 0
cnt = 0

for i in range(n):
  if flg < m and bc[flg][1] >a[i]:
    a[i] = bc[flg][1]
    cnt +=1
    if cnt == bc[flg][0]:
      flg += 1
      cnt = 0
  else:
      break

print((sum(a)))
# Original solution
import sys
import heapq

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

n, m = inm()
a = inl()


def solve():
    heapq.heapify(a)
    for i in range(m):
        b, c = inm()
        for j in range(b):
            if a[0] >= c:
                break
            heapq.heappush(a, c)
            heapq.heappop(a)
    return sum(a)


print(solve())


# Optimized solution
import sys
import heapq

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

n, m = inm()
a = inl()


def solve():
    g = []
    for i in range(m):
        b, c = inm()
        g.append((c, b))
    g.sort(reverse=True)
    h = []
    for c, b in g:
        if len(h) == n:
            break
        for i in range(b):
            h.append(c)
            if len(h) == n:
                break
    assert len(h) <= n

    heapq.heapify(a)
    for x in h:
        if a[0] >= x:
            break
        heapq.heappush(a, x)
        heapq.heappop(a)
    return sum(a)


print(solve())
# Original solution
n, m = list(map(int, input().split()))
cards = sorted(list(map(int, input().split())))
ope = [list(map(int, input().split())) for i in range(m)]
ope.sort(key=lambda x: x[1], reverse=True)
i, j, k = 0, 0, 0
while i < n and j < m and cards[i] < ope[j][1]:
    cards[i] = ope[j][1]
    i += 1
    k += 1
    if k >= ope[j][0]:
        j += 1
        k = 0
print((sum(cards)))

# Optimized solution
n, m = list(map(int, input().split()))
a = sorted([int(i) for i in input().split()])
bc = sorted([tuple(map(int, input().split())) for i in range(m)], key=lambda x: x[1], reverse=True)
i = 0
for b, c in bc:
    while b:
        if i < n and a[i] < c:
            a[i] = c
            b -= 1
        else:
            break
        i += 1
print((sum(a)))
# Original solution
from functools import cmp_to_key
n, m = list(map(int, input().split()))
a = [int(e) for e in input().split()]
bc = [[int(e) for e in input().split()] for _ in range(m)]
bc.sort(key = cmp_to_key(lambda x, y: y[1] - x[1]))
t = 0
for b, c in bc:
  a.extend([c] * b)
  t += b
  if t > n:
    break
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 _ in range(m)]
bc.sort(key = lambda x: x[1], reverse = True)
t = 0
for b, c in bc:
  a.extend([c] * b)
  t += b
  if t > n:
    break
a.sort(reverse = True)
print((sum(a[:n])))
# Original solution
# D - Integer Cards
# https://atcoder.jp/contests/abc127/tasks/abc127_d

import sys

n, m = list(map(int, sys.stdin.readline().split()))
cards = list(map(int, sys.stdin.readline().split()))
cards.sort()

set = [list(map(int, sys.stdin.readline().split())) for _ in range(m)]

# combinations.sort(key = lambda x: x[1], reverse = True)
combinations = []
for b, c in set:
    combinations.extend([c] * b)

combinations.sort(reverse = True)

ans = 0
left = 0
for i in range(len(combinations)):
    if i > len(cards) or combinations[i] <= cards[i]:
        break
    else:
        ans += combinations[i]
        left += 1

print((ans + sum(cards[left:])))

# Optimized solution
# D - Integer Cards
# https://atcoder.jp/contests/abc127/tasks/abc127_d

import sys

n, m = list(map(int, sys.stdin.readline().split()))
cards = list(map(int, sys.stdin.readline().split()))

combinations = [list(map(int, sys.stdin.readline().split())) for _ in range(m)]

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

i = 0
for b, c in combinations:
    for k in range(i, min(n, i + b)):
        if cards[k] < c:
            cards[k] = c
            i += 1
        else:
            break

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

A=list(map(int,input().split()))
A.sort()
dc=[]

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

dc.sort(key=lambda x: x[1],reverse=True)
bf=False
i=0
for l in dc:
    B,C=l
    while B>0:
        if i<N and A[i]<C:
            A[i]=C
            B-=1
            i+=1
        elif i<N:
            bf=True
            i+=1
            break
    if bf:
        break

print((sum(A)))


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

A=list(map(int,input().split()))
A.sort()
dc=[]

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

dc.sort(key=lambda x: x[1],reverse=True)
bf=False
i=0
for l in dc:
    B,C=l
    if bf:
        break
    while B>0:
        if i<N and A[i]<C:
            A[i]=C
            B-=1
            i+=1
        else:
            bf=True
            break


print((sum(A)))
# Original solution
import bisect
N,M = list(map(int,input().split()))
A = list(map(int,input().split()))
A.sort(reverse=True)
D = []
for _ in range(M):
  D.append(list(map(int,input().split())))
D.sort(key=lambda x:x[1], reverse=True)
cnt = 0
ans = []
for d in D:
  b,c = d[0],d[1]
  ans += [c for _ in range(b)]
  cnt += b
  if cnt>=N:
    break
ans = ans[:min(cnt,N)]
cnt = min(cnt,N)
for a in A:
  if cnt<N:
    ans.append(a)
    cnt+=1
  else:
    if cnt==N:
      cnt+=1
      ans.sort()
    if ans[0]<a:
      ans[0]=a
      ans.sort()
print((sum(ans)))

# Optimized solution
import bisect
N,M = list(map(int,input().split()))
A = list(map(int,input().split()))
D = []
for _ in range(M):
  D.append(list(map(int,input().split())))
D.sort(key=lambda x:x[1], reverse=True)
cnt = 0
ans = []
for d in D:
  b,c = d[0],d[1]
  ans += [c for _ in range(b)]
  cnt += b
  if cnt>=N:
    break
ans = ans[:min(cnt,N)] + A
ans.sort(reverse=True)
print((sum(ans[:N])))
# Original solution
from heapq import heapify, heappop, heappush
N,M = list(map(int,input().split()))
A = list(map(int,input().split()))
heapify(A)
for i in range(M):
    B,C = list(map(int,input().split()))
    for k in range(B):
        heappush(A,C)
        a = heappop(A)
        if a>=C:break
print((sum(A)))

# Optimized solution
from heapq import heapify, heappop, heappush
N,M = list(map(int,input().split()))
A = list(map(int,input().split()))
BC = []
heapify(A)
for i in range(M):
    B,C = list(map(int,input().split()))
    BC.append((B,C))
BC.sort(key=lambda x:-x[1])
for S in BC:
    B,C = S
    for k in range(B):
        heappush(A,C)
        a = heappop(A)
        if a>=C:break
print((sum(A)))
# Original solution
# coding: utf-8
import re
import bisect
from datetime import datetime as dt

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()))
    for i in range(b):
        A.append(c)
A.sort()
print((sum(A[len(A)-n:len(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 [0]*m]
a.sort()
bc.sort(key=lambda x: x[1], reverse=True)  
i=0
for b,c in bc:
    for j in range(i,min(n,i+b)):
        if a[j] < c:
            a[j] = c
            i+=1
        else:
            break
print((sum(a)))
# Original solution
N,M=list(map(int, input().split()))

A = list(map(int, input().split()))
S = [list(map(int,input().split())) for i in range(M)]

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

X = [0]*(len(A))
idx = 0

for i in range(M):
    if idx <= N:
        X[idx:S[i][0]] = [S[i][1]] * S[i][0]
        idx += S[i][0]
    else:
        break

for i in range(N):
    A[i] = max(A[i], X[i])

print((sum(A)))

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

A = list(map(int, input().split()))
S = [list(map(int,input().split())) for i in range(M)]

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

idx = 0

for i in range(M):
    for j in range(idx, min(N, idx + S[i][0])):
        if A[j] < S[i][1]:
            A[j] = S[i][1]
            idx += 1
        else:
            break

print((sum(A)))
# Original solution
N, M = list(map(int, input().split()))
A = [int(i) for i in input().split()]
for _ in range(M):
  B, C = list(map(int, input().split()))
  A.extend([C]*B)
  
A = sorted(A, reverse=True)
print((sum(A[:N])))

# Optimized solution
N, M = list(map(int, input().split()))
A = [int(i) for i in input().split()]
min_val = sorted(A)[0]
for _ in range(M):
  B, C = list(map(int, input().split()))
  if min_val <= C:
  	A.extend([C]*B)
  
A = sorted(A, reverse=True)
print((sum(A[:N])))
# Original solution
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
l = []
for i in range(m):
    data = list(map(int,input().split()))
    l.append(data)
l.sort(key=lambda x:x[1])

import heapq
heapq.heapify(a)

for tmp in l:
    b=tmp[0]
    c=tmp[1]

    for i in range(b):
        heapq.heappushpop(a,c)

print((sum(a)))

# Optimized solution
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
l = []
for i in range(m):
    data = list(map(int,input().split()))
    l.append(data)
l.sort(key=lambda x:x[1])
l.reverse()

a.sort()

b_idx=0
nokori=l[b_idx][0]
for i in range(n):
    if a[i] > l[b_idx][1]:
        break
    else:
        a[i] = l[b_idx][1]
        nokori-=1
        if nokori == 0:
            b_idx+=1
            if b_idx >= m:
                break
            nokori=l[b_idx][0]

print((sum(a)))
# Original solution
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
a.sort()
ans=sum(a)
l=[]
for i in range(m):
    b,c=list(map(int,input().split()))
    l.append((b,c))
l.sort(key=lambda x:x[1],reverse=True)
x=[]
for i in range(m):
    xx=[l[i][1]]*l[i][0]
    x.extend(xx)
c=0
while a[c]<x[c] :
    ans+=x[c]-a[c]
    c+=1
    if c>=len(a) or c>=len(x):
        break
print(ans)

# Optimized solution
import bisect
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
a.sort()
bc=[]
for i in range(m):
    b,c=list(map(int,input().split()))
    bc.append([b,c])
bc.sort(key=lambda x:x[1],reverse=True)
for i in range(m):
    for j in range(min(bisect.bisect_left(a,bc[i][1]), bc[i][0])):
        a[j]=bc[i][1]
    a.sort()

print((sum(a)))
# 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)
import bisect
i=0
for i in range(m):
    if bc[i][1]>a[0]:
        k=min(bc[i][0],bisect.bisect_left(a,bc[i][1]))
        a[:k]=[bc[i][1]]*k
        a.sort()
    else:
        break
print((sum(a)))


# Optimized solution
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
l=[]
for i in range(n):
    l.append([1,a[i]])
for i in range(m):
    l.append(list(map(int,input().split())))
l.sort(key=lambda x:x[1], reverse=True)
c=0
ans=0
for i in range(n+m):
    ans+=l[i][1]*l[i][0]
    c+=l[i][0]
    if c>n:
        ans-=l[i][1]*(c-n)
        break
print(ans)
# Original solution
from bisect import *
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort()
data = []
for _ in range(M):
    B, C = list(map(int, input().split()))
    data.append((B, C))
data.sort(key=lambda x:x[1], reverse=True)

D = []
for B, C in data:
    i = bisect_left(A, C)
    if B<=i:
        D += [C]*B
    else:
        D += [C]*i

res = 0
for i in range(N):
    if i>=len(D) or A[i] > D[i]:
        res += A[i]
    else:
        res += D[i]
print(res)

# Optimized solution
from bisect import *
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort()
data = []
for _ in range(M):
    B, C = list(map(int, input().split()))
    data.append((B, C))
data.sort(key=lambda x:x[1], reverse=True)

D = []
for B, C in data:
    i = bisect_left(A, C)
    p = min(B, i)
    if len(D)+p <N:
        D += [C]*p
    else:
        D += [C]*(N-len(D))
        break

res = 0
for i in range(N):
    if i>=len(D) or A[i] > D[i]:
        res += A[i]
    else:
        res += D[i]
print(res)
# Original solution
N,M = list(map(int,input().split()))
A = list(map(int,input().split()))

#counter 辞書(改良版)
def counter_imp(A):
 dic = {}                
 for a in A:
    dic[a] = dic.get(a,0) + 1
 return dic
#counter 2次元リスト
def counter_list(A):
 dic = counter_imp(A)
 C = []
 for k in dic:
    C.append([k,dic[k]])
 return C

#（発想）数値を全部追加してソート、大きい順に「Aの要素数」分の要素をとってきて和を求める
A = counter_list(A) 
for i in range(M):
 B,C = list(map(int,input().split()))
 A.append([C,B]) #[C(数値),B(個数)]
 
A.sort(reverse = True) #降順ソート

num,n_index = 0,0 #num:現在和をとった要素の個数,n_index:現在の要素のインデックス
ans = 0 #求める和 
while(1):
  if(num == N): break
  if((N-num) >= A[n_index][1]):
   #N-num :残り必要な要素数
   #A[n_index][1] : 要素A[n_index][0]の個数
   ans += A[n_index][0]*A[n_index][1]
   num += A[n_index][1]
  else:
   ans += A[n_index][0]*(N-num)
   num = N
  n_index += 1 #indexを1つ進める
  
print(ans)

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

def counter_list(A):
 def counter_dic(A): #counter 辞書
  dic = {}
  for a in A:
   dic[a] = dic.get(a,0) + 1
  return dic

 dic_ = counter_dic(A)
 B = []
 for k in dic_:
  B.append([k,dic_[k]])
 return B

#（発想）数値を全部追加してソート、大きい順に「Aの要素数」分の要素をとってきて和を求める
A = counter_list(A) 
for i in range(M):
 B,C = list(map(int,input().split()))
 A.append([C,B]) #[C(数値),B(個数)]
 
A.sort(reverse = True) #降順ソート

num,n_index = 0,0 #num:現在和をとった要素の個数,n_index:現在の要素のインデックス
ans = 0 #求める和 
while(1):
  if(num == N): break
  if((N-num) >= A[n_index][1]):
   #N-num :残り必要な要素数
   #A[n_index][1] : 要素A[n_index][0]の個数
   ans += A[n_index][0]*A[n_index][1]
   num += A[n_index][1]
  else:
   ans += A[n_index][0]*(N-num)
   num = N
  n_index += 1 #indexを1つ進める
  
print(ans)
# Original solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
C = []
for i in range(M):
  b, c = list(map(int, input().split()))
  for i in range(b):
    C.append(c)

A.sort()
C.sort(reverse=True)
j = 0
while (j < len(C)) and (A[j] < C[j]):
  j += 1
  
ans = int(sum(C[:j]) + sum(A[j:]))
print(ans)

# Optimized solution
import sys
from operator import itemgetter

def solve():
    input = sys.stdin.readline
    N, M = list(map(int, input().split()))
    A = [int(a) for a in input().split()]
    A.sort()
    change = [[int(i) for i in input().split()] for _ in range(M)]
    change.sort(key = itemgetter(1), reverse = True)

    ai = 0
    for i in range(M):
        cb, c = change[i]
        if ai >= N or c < A[ai]: break
        for _ in range(cb):
            if ai >= N: break
            if A[ai] <= c:
                A[ai] = c
                ai += 1
            else: break
    print((sum(A)))
    return 0

if __name__ == "__main__":
    solve()
# Original solution
from operator import itemgetter
n,m = list(map(int,input().split()))
a_list = list(map(int,input().split()))
b_list = []
for i in range(m):
	b,c = list(map(int,input().split())) 
	b_list.append([b,c]) #b_list[i][枚数、value]

a_list.sort()
b_list.sort(reverse = True,key = itemgetter(1)) #cについてソート

now_position = 0
for i in range(m):
	if (now_position + b_list[i][0] < len(a_list)) and a_list[now_position + b_list[i][0]] < b_list[i][1]:
		del a_list[now_position:now_position + b_list[i][0]]
		a_list.insert(now_position,b_list[i][1]*b_list[i][0])
		now_position += 1
	else:
		for j in range(b_list[i][0]):
			if (now_position + j < len(a_list)) and a_list[now_position + j] < b_list[i][1]:
				a_list[now_position + j] = b_list[i][1]

			else:
				break
		break

print((sum(a_list)))

# Optimized solution
from operator import itemgetter
n,m = list(map(int,input().split()))
a_list = list(map(int,input().split()))
b_list = []
for i in range(m):
	b,c = list(map(int,input().split())) 
	b_list.append([b,c]) #b_list[i][枚数、value]

a_list.sort()
b_list.sort(reverse = True,key = itemgetter(1)) #cについてソート
sum_list = []
now_position = 0
j = 0
for i in range(m):
	if (now_position + b_list[i][0] <= len(a_list)) and a_list[now_position + b_list[i][0]-1] < b_list[i][1]:
		sum_list.append(b_list[i][1]*b_list[i][0])
		now_position += b_list[i][0] 
	else:
		for j in range(b_list[i][0]):
			if (now_position + j < len(a_list)) and a_list[now_position + j] < b_list[i][1]:
				sum_list.append(b_list[i][1])

			else:
				break
		break
if (now_position + j <= len(a_list)-1):
	print((sum(sum_list)+sum(a_list[now_position+j:])))
else:
	print((sum(sum_list)))
# Original solution
from collections import*
I=lambda:map(int,input().split())
n,m=I()
a=Counter(I())
exec("b,c=I();a[c]+=b;"*m)
s=0
for k,v in sorted(a.items())[::-1]:
 if n<v:exit(print(s+k*n))
 n-=v;s+=k*v


# Optimized solution
from collections import*
I=lambda:map(int,input().split())
n,m=I()
a=Counter(I())
for _ in[0]*m:b,c=I();a[c]+=b
s=0
for k,v in sorted(a.items())[::-1]:
 if n<v:exit(print(s+k*n))
 n-=v;s+=k*v
# Original solution
n,m = list(map(int,input().split()))
a = sorted([int(x) for x in input().split()])

for i in range(m):
    j,k = list(map(int,input().split()))
    a.extend([k]*j)

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


# Optimized solution
n,m = list(map(int,input().split()))
a = sorted([int(x) for x in input().split()])

for i in range(m):
    j,k = list(map(int,input().split()))
    if min(a) < k:
        a.extend([k]*j)
    a = sorted(a,reverse=True)[:n]

print((sum(a)))