CodeEff/ECCO huggingface dataset:
# Original solution
from heapq import heapify, heappush, heappop
import sys
input = sys.stdin.readline
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
a = heapify(A)
B = [0] * M
C = [0] * M
for i in range(M):
    B[i], C[i] = list(map(int, input().split()))

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

for c, b in cb:
    for _ in range(b):
        m = heappop(A)
        heappush(A, max(m, c))
        if m > c:
            break

print((sum(A)))

# Optimized solution
from heapq import heapify, heappush, heappop
import sys
input = sys.stdin.readline
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort()
B = [0] * M
C = [0] * M
for i in range(M):
    B[i], C[i] = list(map(int, input().split()))

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

cnt = 0
exchange = []
for c, b in cb:
    exchange.extend([c] * b)
    cnt += b
    if cnt >= N:
        break

exchange.sort()

for _ in range(len(exchange)):
    m = heappop(A)
    heappush(A, max(m, exchange.pop()))

print((sum(A)))
# Original solution
from heapq import heapify, heappush, heappop
import sys
input = sys.stdin.readline
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort()
B = [0] * M
C = [0] * M
for i in range(M):
    B[i], C[i] = list(map(int, input().split()))

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

cnt = 0
exchange = []
for c, b in cb:
    exchange.extend([c] * b)
    cnt += b
    if cnt >= N:
        break

exchange.sort()

for _ in range(len(exchange)):
    m = heappop(A)
    heappush(A, max(m, exchange.pop()))

print((sum(A)))

# Optimized solution
import sys
input = sys.stdin.readline
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
B = [0] * M
C = [0] * M
for i in range(M):
    B[i], C[i] = list(map(int, input().split()))

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

exchange = []
for c, b in cb:
    exchange.extend([c] * b)
    if len(exchange) >= N:
        break

A.extend(exchange)

A.sort(reverse=True)

print((sum(A[:N])))
# Original solution
import bisect
N,M = list(map(int,input().split()))
A = list(map(int,input().split()))
BC = [list(map(int,input().split())) for _ in range(M)]

for b,c in BC:
    A += [c] * b

# print(A)
# print(sorted(A))
# print(sorted(A)[-N:])
print((sum(sorted(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)]

for b,c in BC:
    A += [c] * b

# print(A)
# print(sorted(A))
# print(sorted(A)[-N:])
print((sum(sorted(A)[-N:])))
# Original solution
import sys
from collections import deque
readline = sys.stdin.readline

n, m = list(map(int, readline().split()))
a = sorted(list(map(int, readline().split())))
bc = []
for _ in range(m):
    b, c = list(map(int, readline().split()))
    for _ in range(b):
        bc.append(c)
bc.sort(key=lambda x: -x)
dbc = deque(bc)
for i in range(n):
    if len(dbc) > 0 and a[i] < dbc[0]:
        a[i] = dbc.popleft()

print((sum(a)))

# Optimized solution
import sys
from collections import deque
readline = sys.stdin.readline

n, m = list(map(int, readline().split()))
a = sorted(list(map(int, readline().split())))
bc = []
for _ in range(m):
    b, c = list(map(int, readline().split()))
    bc.append([b * c, c])
bc.sort(key=lambda x: -x[1])
dbc = deque(bc)
for i in range(n):
    if len(dbc) > 0 and a[i] < dbc[0][1]:
        a[i] = dbc[0][1]
        dbc[0][0] -= dbc[0][1]
        if dbc[0][0] == 0:
            dbc.popleft()

print((sum(a)))
# Original solution
#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(N: int, M: int, A: "List[int]", B: "List[int]", C: "List[int]"):
    A.sort()                    # 昇順 O(NlongN)
    BC = list(zip(B, C))
    BC.sort(key=lambda x: x[1], reverse=True)  # 降順 MlogM
    Cs = []
    for b, c in BC:             # M
        Cs.extend([c]*b)

    s = 0
    for i, (a, c) in enumerate(zip(A, Cs)):
        if a >= c:
            s += a
            break
        s += max(a, c)
    if i < len(A)-1:
        for j in range(i+1, len(A)):
            s += A[j]
    print(s)
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    B = [int()] * (M)  # type: "List[int]"
    C = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        B[i] = int(next(tokens))
        C[i] = int(next(tokens))
    solve(N, M, A, B, C)


if __name__ == '__main__':
    main()


# Optimized solution
#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(N: int, M: int, A: "List[int]", B: "List[int]", C: "List[int]"):
    A.sort()                    # 昇順 O(NlongN)
    BC = list(zip(B, C))
    BC.sort(key=lambda x: x[1], reverse=True)  # 降順 MlogM
    Cs = []
    for b, c in BC:             # M
        Cs.extend([c]*b)

    s = 0
    for i, (a, c) in enumerate(zip(A, Cs)):  # N
        if a >= c:
            s += a
            break
        s += max(a, c)
    if i < len(A)-1:
        for j in range(i+1, len(A)):
            s += A[j]
    print(s)
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    B = [int()] * (M)  # type: "List[int]"
    C = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        B[i] = int(next(tokens))
        C[i] = int(next(tokens))
    solve(N, M, A, B, C)


if __name__ == '__main__':
    main()
# Original solution
import bisect
import sys
input = sys.stdin.readline
from collections import deque
def solve():
  
  n,m = (int(i) for i in input().split())
  a = list(int(i) for i in input().split())
  a.sort()
  tmp = deque([])
  for i in range(m):
    r,l = (int(i) for i in input().split())
    tmp.append((l,r))
  tmp= deque(sorted(tmp,reverse =True))
  ans = 0
  while tmp:
    c,b = tmp.popleft()
    place = bisect.bisect_left(a,c)-1
    if place == -1:
      break
    else:
        if len(a) == 0:
           break
        a = a[min(1+place,b)::]
        ans += c*(min(1+place,b))
    #print(a)
  if a:
   ans += sum(a)
  print (ans)
solve()

# Optimized solution
import bisect
import sys
input = sys.stdin.readline
from collections import deque
def solve():
  
  n,m = (int(i) for i in input().split())
  a = list(int(i) for i in input().split())
  a = deque(sorted(a))
  tmp = deque([])
  for i in range(m):
    r,l = (int(i) for i in input().split())
    tmp.append((l,r))
  tmp= deque(sorted(tmp,reverse =True))
  ans = 0
  while tmp:
    c,b = tmp.popleft()
    place = bisect.bisect_left(a,c)-1
    if place == -1:
      break
    else:
        if len(a) == 0:
           break
        for i in range(min(1+place,b)):
           a.popleft()
        ans += c*(min(1+place,b))
    #print(a)
  if a:
   ans += sum(a)
  print (ans)
solve()
# Original solution
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
input = sys.stdin.readline
 
# bisect_left(lists, 3)
# bisect_right(lists, 3)

# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

def main():
	N, M = list(map(int, input().split(" ")))
	#N = int(input())
	#S = input()

	A = input().split(" ")
	A = list(A)
	#A = A[:-1]
	A = [int(s) * (-1) for s in A]
	heapq.heapify(A)

	for i in range(M):
		b, c = list(map(int, input().split(" ")))
		for _ in range(b):
			heapq.heappush(A, c * (-1))
	
	ans = 0
	for i in range(N):
		tmp = heapq.heappop(A) * (-1)
		ans += tmp
		#print(tmp)
	print(ans)

if __name__ == "__main__":
	main()

# Optimized solution
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
input = sys.stdin.readline
 
# bisect_left(lists, 3)
# bisect_right(lists, 3)

# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

def main():
	N, M = list(map(int, input().split(" ")))
	#N = int(input())
	#S = input()

	A = input().split(" ")
	A = list(A)
	#A = A[:-1]
	A = [(int(s) * (-1), 1) for s in A]
	heapq.heapify(A)

	for i in range(M):
		b, c = list(map(int, input().split(" ")))
		heapq.heappush(A, (c * (-1), b))
	
	ans = []
	for i in range(N):
		t, con = heapq.heappop(A)
		for _ in range(con):
			if len(ans) == N:
				print((sum(ans)))
				sys.exit()
			ans.append(t * (-1))
	print((sum(ans)))


if __name__ == "__main__":
	main()
# Original solution
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
from bisect import bisect_left


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

bc = [tuple(map(int,input().split())) for i in range(m)]
bc = sorted(bc, reverse=True, key=lambda x: x[1])

cnt = 0
for b, c in bc:
    index = bisect_left(a[cnt:], c)+cnt
    tmp = min(index,b+cnt)
    for i in range(cnt,tmp):
        a[i] = c
    cnt = tmp


print((sum(a)))


# Optimized solution
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
from bisect import bisect_left


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

bc = [tuple(map(int,input().split())) for i in range(m)]
bc = sorted(bc, reverse=True, key=lambda x: x[1])

i = 0
for b, c in bc:
    for _ in range(b):
        if c <= a[i]: break
        a[i] = c
        i += 1
        if i >= n :break
    else:
        continue
    break

print((sum(a)))
# Original solution
def d_integer_cards(N, M, A, Operation_info):
    from bisect import bisect_left
    from collections import deque

    a = deque(sorted(A))
    Operation_info.sort(key=lambda x: x[1], reverse=True)

    tmp = []
    for b, c in Operation_info:
        index = bisect_left(a, c)
        if index > 0:
            for j in range(min(b, index)):
                a.popleft()
                tmp.append(c)
    return sum(list(a) + tmp)

N, M = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
Operation_info = [[int(i) for i in input().split()] for j in range(M)]
print((d_integer_cards(N, M, A, Operation_info)))

# Optimized solution
def d_integer_cards(N, M, A, Operation_info):
    # Aの要素は1回選ぶことができるとみなす
    # 値C_iはB_i回選ぶことができるとみなす
    selected_num = [[1, a] for a in A] + Operation_info
    selected_num.sort(key=lambda x: x[1], reverse=True)

    # 値yをx回選ぶことを、選んだ値の個数がNになるまで繰り返す
    select = 0
    ans = 0
    for x, y in selected_num:
        select += x
        ans += x * y
        if select >= N:
            ans -= (select - N) * y
            break
    return ans

N, M = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
Operation_info = [[int(i) for i in input().split()] for j in range(M)]
print((d_integer_cards(N, M, A, Operation_info)))
# Original solution
from collections import deque

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

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

    for j in range(B):
        A_que.append(C)

A = list(A_que)
A.sort(reverse=True)
print((sum(A[0:N])))

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

A.sort()

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

#D = list(D_dict)
D.sort(reverse=True)

D_index = 0
D_index_max = len(D)

ans = 0

ct_D_i = 0
ct_D_i_max = D[0][1]

ct_D_total = 0

for i in range(N):
    a = A[i]
    d = D[D_index][0]
    if a > d:
        break

    A[i] = d


    ct_D_i += 1
    if ct_D_i >= ct_D_i_max:
        D_index += 1
        if D_index >= D_index_max:
            break

        ct_D_i = 0
        ct_D_i_max = D[D_index][1]




print((sum(A)))
# Original solution
from collections import deque

n,m=list(map(int,input().split()))
a=deque(list(map(int,input().split())))
#a=deque(a)
for i in range(m):
  b,c=list(map(int,input().split()))
  l=[c for _ in range(b)]
  a.extend(l)
a=list(a)
#print(a)
a.sort(reverse=1)
a=a[:n]





print((sum(a)))
#print(a)

# Optimized solution
from collections import deque

n,m=list(map(int,input().split()))
a=deque(list(map(int,input().split())))
bc=[list(map(int,input().split())) for _ in range(m)]
bc.sort(key=lambda x: x[1])
bc=bc[::-1]
l=0
for b,c in bc:
  if l>n:
    break
  else:
    d=[c]*b
    a.extend(d)
    l+=b


a=list(a)
#print(a)
a.sort(reverse=1)
a=a[:n]





print((sum(a)))
#print(a)
# Original solution
ni = lambda: int(eval(input()))
nm = lambda: list(map(int, input().split()))
nl = lambda: list(map(int, input().split()))

n, m = nm()

a = nl()

for i in range(m):
    b, c = nm()
    a += [c] * b

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

# Optimized solution
ni = lambda: int(eval(input()))
nm = lambda: list(map(int, input().split()))
nl = lambda: list(map(int, input().split()))

n, m = nm()

a = sorted(nl())

pos = 0

changer = []

for i in range(m):
    b, c = nm()
    changer.append((c, b))

changer = sorted(changer)[::-1]


for i in range(m):

    c = changer[i]
    for j in range(c[1]):
        while pos < n:
            if a[pos] < c[0]:
                a[pos] = c[0]
                break
            pos += 1
    if pos >= n:
        break

print((sum(a)))
# Original solution
import sys
from collections import deque
from copy import copy
from bisect import bisect_right, bisect_left


N,M = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort()
BC=[]
for m in range(M):
    b,c = list(map(int, input().split()))
    BC.append([b,c])

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

_sum = 0

for b,c in BC:
    i = bisect_left(A, c)
    if i == 0:
        break
    nrof_replace = min(i,b)
    _sum += c * nrof_replace
    A = A[nrof_replace:]
    
print((_sum + sum(A)))


# Optimized solution
import sys
from collections import deque
from copy import copy
from bisect import bisect_right, bisect_left


N,M = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort()
A = deque(A)
BC=[]
for m in range(M):
    b,c = list(map(int, input().split()))
    BC.append([b,c])

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

_sum = 0

for b,c in BC:
    i = bisect_left(A, c)
    if i == 0:
        break
    nrof_replace = min(i,b)
    _sum += c * nrof_replace
    #A = A[nrof_replace:]
    for i in range(nrof_replace):
        A.popleft()
    
print((_sum + sum(A)))
# Original solution
from collections import defaultdict
n,m=list(map(int,input().split()))
A=[int(i) for i in input().split()]
A.sort()
D=defaultdict(int)
for i in range(m):
  b,c=list(map(int,input().split()))
  D[c]+=b
D=sorted(list(D.items()),key=lambda x:x[0],reverse=True)
DD=[0]*n
ct=0
for x,y in D:
  for i in range(y):
    if i+ct<n:
      DD[i+ct]+=x
  ct+=y
ans=0
for i in range(n):
  ans+=max(A[i],DD[i])
print(ans)


# Optimized solution
from collections import defaultdict
from collections import Counter
n,m=list(map(int,input().split()))
A=[int(i) for i in input().split()]
AA=dict(Counter(A))
D=defaultdict(int)
for i in range(m):
  b,c=list(map(int,input().split()))
  D[c]+=b
for x in list(AA.keys()):
  D[x]+=AA[x]
D=sorted(list(D.items()),key=lambda x:x[0],reverse=True)
ans,tot=0,0
for x,y in D:
  if tot+y<=n:
    ans+=x*y
    tot+=y
  else:
    ans+=x*(n-tot)
    tot+=n-tot
  if tot==n:
    break
print(ans)
# Original solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))

for i in range(M):
  B, C = list(map(int, input().split()))
  A = A + [C,]*B
  
A.sort(reverse=True)
sum = 0

for i in range(N):
  sum = sum + A[i]
  
print(sum)

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

Nd = {}

for i in range(M):
  B, C = list(map(int, input().split()))
  if C in list(Nd.keys()):
    Nd[C] = Nd[C] + B
  else:
    Nd[C] = B
  
for a in A:
  if a in list(Nd.keys()):
    Nd[a] = Nd[a] + 1
  else:
    Nd[a] = 1
    
Nd = sorted(list(Nd.items()), key=lambda x: -x[0]) # type=list
sum = 0

for key, val in Nd:
  if N >= val:
    sum = sum + key*val
    N = N-val
  else:
    sum = sum + key*N
    break;
  
print(sum)
# Original solution
# ABC127 D-Integer Cards
from operator import itemgetter
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=itemgetter(1),reverse=True)
# print(A)
# print('sort',BC)
for i in range(M):
    x = 0
    while x < BC[i][0]:
        A.sort()
        # print(i,A)
        if BC[i][1] > A[0]:
            A[0] = BC[i][1]
        x += 1 
print((sum(A)))

# Optimized solution
# ABC127 D-Integer Cards
from operator import itemgetter
N,M = list(map(int,input().split()))
A = list(map(int,input().split()))
BC = [list(map(int, input().split())) for _ in range(M)]

A.sort()
BC.sort(key=itemgetter(1),reverse=True)
# print(A)
# print('sort',BC)
c = 0
for i in range(M):
    for j in range(BC[i][0]):
        if A[c] < BC[i][1]:
            A[c] = BC[i][1]
        else:
            print((sum(A)))
            exit()
        c += 1
        if c == N:
            print((sum(A)))
            exit()
# for i in range(M):
#     x = 0
#     while x < BC[i][0]:
#         A.sort()
#         print(i,A)
#         if BC[i][1] > A[0]:
#             A[0] = BC[i][1]
#         x += 1 
print((sum(A)))
# Original solution
# ABC127 D-Integer Cards
from operator import itemgetter
N,M = list(map(int,input().split()))
A = list(map(int,input().split()))
BC = [list(map(int, input().split())) for _ in range(M)]

A.sort()
BC.sort(key=itemgetter(1),reverse=True)
# print(A)
# print('sort',BC)
c = 0
for i in range(M):
    for j in range(BC[i][0]):
        if A[c] < BC[i][1]:
            A[c] = BC[i][1]
        else:
            print((sum(A)))
            exit()
        c += 1
        if c == N:
            print((sum(A)))
            exit()
# for i in range(M):
#     x = 0
#     while x < BC[i][0]:
#         A.sort()
#         print(i,A)
#         if BC[i][1] > A[0]:
#             A[0] = BC[i][1]
#         x += 1 
print((sum(A)))

# Optimized solution
# ABC127 D-Integer Cards
from operator import itemgetter
N,M = list(map(int,input().split()))
A = list(map(int,input().split()))
BC = [list(map(int, input().split())) for _ in range(M)]

A.sort()
BC.sort(key=itemgetter(1),reverse=True)

c = 0
for i in range(M):
    for j in range(BC[i][0]):
        if A[c] < BC[i][1]:
            A[c] = BC[i][1]
        else:
            print((sum(A)))
            exit()
        c += 1
        if c == N:
            print((sum(A)))
            exit()

print((sum(A)))
# Original solution
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
b=[]
for i in range(m):
    bb,cc=list(map(int,input().split()))
    b.append([cc,bb])
a.sort()
b.sort(reverse=True)

i=0
j=0
while i<n:
    if j==m:
        break
    if b[j][1]>0:
        if a[i]<b[j][0]:
            a[i]=b[j][0]
            b[j][1]-=1
            i+=1
        elif a[i]>=b[j][0]:
            break
    else:
        j+=1
print((sum(a)))

# Optimized solution
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
bc=[list(map(int,input().split())) for i in range(m)]
a.sort()
bc.sort(key=lambda x: x[1],reverse=True)
# print(a)
# print(bc)
i=0
for b,c in bc:
  tb=b
  while tb>0:
    if n==i:
      break
    if a[i]>=c:
      break
    a[i]=c
    tb-=1
    i+=1
  if n==i:break
  if a[i]>=c :
    break
print((sum(a)))
# Original solution
import heapq

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

change = []
for m in range(M):
    B, C = list(map(int, input().split()))
    change.append((B, C))
change.sort(key=lambda x: -x[1])

change2 = []
for c in change:
    change2.extend([c[1]] * c[0])
    if len(change2) >= N:
        change2 = change2[:N]
        break


heapq.heapify(A)

for c in change2:
    x = heapq.heappop(A)
    if x >= c:
        # 先頭すでに十分大きい場合は抜ける
        heapq.heappush(A, x)
        break
    else:
        x = max(x, c)
        heapq.heappush(A, x)

print((sum(A)))


# Optimized solution
import heapq

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

# 交換できるカードを大きい順にリスト化する
exchange1 = []
for m in range(M):
    B, C = list(map(int, input().split()))
    exchange1.append((B, C))
exchange1.sort(key=lambda x: -x[1])
exchange2 = []
for c in exchange1:
    exchange2.extend([c[1]] * c[0])
    # 手持ちのカード枚数を超えたら抜ける
    if len(exchange2) >= N:
        exchange2 = exchange2[:N]
        break

heapq.heapify(A)

for c in exchange2:
    x = heapq.heappop(A)
    if x >= c:
        # 先頭すでに十分大きい場合は抜ける
        heapq.heappush(A, x)
        break
    else:
        x = max(x, c)
        heapq.heappush(A, x)

print((sum(A)))
# Original solution
import sys
import heapq
n,m=list(map(int,sys.stdin.readline().split()))
a=list(map(int,sys.stdin.readline().split()))
a.sort
for i in range(m):
    b,c=list(map(int,sys.stdin.readline().split()))
    t=[c]*b
    a=list(heapq.merge(a,t))
print((sum(heapq.nlargest(n,a))))

# Optimized solution
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
bc=[list(map(int,input().split())) for i in range(m)]
bc.sort(key=lambda x:x[1],reverse=True)
for b,c in bc:
    a.extend([c]*b)
    if len(a)>2*n:
        break
a.sort(reverse=True)
print((sum(a[:n])))
# Original solution
N, M = list(map(int, input().split()))
A = sorted([int(i) for i in input().split()])

B = []
for _ in range(M):
    b, c = list(map(int, input().split()))
    B += [c] * b
    
B.sort(reverse = True)

x = min(N, len(B))

for i in range(x):
    A[i] = max(A[i], B[i])
    
print((sum(A)))

# Optimized solution
N, M = list(map(int, input().split()))
A = sorted([int(i) for i in input().split()])
C = sorted([[int(i) for i in input().split()] for i in range(M)], reverse = True, 
           key = lambda x: x[1])

cnt = 0
B = []
for i in range(M):
    if cnt > N:
        break
    B += [C[i][1]] * C[i][0]
    cnt += C[i][0]
    
B.sort(reverse = True)

x = min(N, len(B))

for i in range(x):
    A[i] = max(A[i], B[i])
    
print((sum(A)))
# Original solution
s=input().split()
n=int(s[0])
m=int(s[1])

s=input().split()

l=list(map(int, s))

for i in range(m):
    s=input().split()
    l.extend([int(s[1])]*int(s[0]))

l.sort()
print((sum(l[-n:])))


# Optimized solution
s=input().split()
n=int(s[0])
m=int(s[1])

s=input().split()

l=list(map(int, s))
num=min(l)
for i in range(m):
    s=input().split()
    a=int(s[1])
    if a>num:
        l.extend([a]*int(s[0]))

l.sort()
print((sum(l[-n:])))
# Original solution
import heapq

n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
heapq.heapify(a)
b = []
for i in range(m):
    u, v = list(map(int, input().split()))
    for j in range(u):
        b.append(-v)
heapq.heapify(b)
while len(b) > 0:
    x = heapq.heappop(a)
    y = heapq.heappop(b)
    if x < -y:
        heapq.heappush(a, -y)
    else:
        heapq.heappush(a, x)
        break
print((sum(a)))

# Optimized solution
import heapq

n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
heapq.heapify(a)
for i in range(m):
    u, v = list(map(int, input().split()))
    for j in range(u):
        if v > a[0]:
            heapq.heappushpop(a, v)
print((sum(a)))