CodeEff/ECCO huggingface dataset:
# Original solution
N, M = list(map(int, input().split()))
lst_A = list(map(int, input().split()))
lst_BC = [tuple(map(int, input().split())) for _ in range(M)]


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


lst_D = []
add_num = N
for tpl in lst_BC:
    b, c = tpl[0], tpl[1]
    if add_num >= b:
        lst_D = lst_D + [c] * b
        add_num -= b
    else:
        lst_D = lst_D + [c] * add_num
lst_D = lst_D + [0] * (N - len(lst_D))


for i in range(N):
    if lst_A[i] >= lst_D[i]:
        break
    lst_A[i] = lst_D[i]


print((sum(lst_A)))

# Optimized solution
N, M = list(map(int, input().split()))
lst_A = list(map(int, input().split()))
lst_BC = [list(map(int, input().split())) for _ in range(M)]


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


lst_D = []
add_num = N
for b, c in lst_BC:
    lst_D += [c] * min(b, N - len(lst_D))
lst_D = lst_D + [0] * (N - len(lst_D))


for i in range(N):
    if lst_A[i] >= lst_D[i]:
        break
    lst_A[i] = lst_D[i]


print((sum(lst_A)))
# Original solution
N, M = list(map(int, input().split()))
lst_A = list(map(int, input().split()))
lst_BC = [list(map(int, input().split())) for _ in range(M)]


lst_A.sort()
# lst_BC.sort(key=lambda x:-x[1])
lst_BC.sort(key=lambda x:x[1], reverse=True)
# print(lst_A)
# print(lst_BC)


lst_D = []
for b, c in lst_BC:
    lst_D += [c] * min(b, N - len(lst_D))
lst_D = lst_D + [0] * (N - len(lst_D))
# print(lst_D)


for i in range(N):
    if lst_A[i] >= lst_D[i]:
        break
    lst_A[i] = lst_D[i]
# print(lst_A)


print((sum(lst_A)))

# Optimized solution
N, M = list(map(int, input().split()))
lst_A = list(map(int, input().split()))
lst_BC = [tuple(map(int, input().split())) for _ in range(M)]


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


lst_D = []
for b, c in lst_BC:
    lst_D += [c] * min(b, N - len(lst_D))
lst_D = lst_D + [0] * (N - len(lst_D))


for i in range(N):
    if lst_A[i] >= lst_D[i]:
        break
    lst_A[i] = lst_D[i]


print((sum(lst_A)))
# Original solution
from heapq import heapify, heappush, heappop


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


hq = []
heapify(hq)
for a in lst_A:
    heappush(hq, [-a, 1])
for _ in range(M):
    b, c = list(map(int, input().split()))
    heappush(hq, [-c, b])

ans = 0
while N > 0:
    c, b = heappop(hq)
    c *= -1
    if b < N:
        ans += c * b
        N -= b
    else:
        ans += c * N
        N = 0


print(ans)

# Optimized solution
from heapq import heapify, heappush, heappop


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


hq = []
# heapify(hq)
for a in lst_A:
    heappush(hq, [-a, 1])
for _ in range(M):
    b, c = list(map(int, input().split()))
    heappush(hq, [-c, b])

ans = 0
while N > 0:
    c, b = heappop(hq)
    c *= -1
    if b < N:
        ans += c * b
        N -= b
    else:
        ans += c * N
        N = 0


print(ans)
# Original solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
D = []

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

E = sorted(A + D, reverse=True)
print((sum(E[0:N])))

# Optimized solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
D = []
E = []
for i in range(M):
    B, C = list(map(int, input().split()))
    D.append([C, B])
D = sorted(D, reverse = True)

for j in range(len(D)):
    E += [D[j][0]]*D[j][1]
    if len(E) >= N:
        break

F = sorted(A + E, reverse = True)
print((sum(F[0:N])))
# Original solution
from collections import Counter
n,m=list(map(int,input().split()))
d=Counter(list(map(int,input().split())))
for i in range(m):
    b,c=list(map(int,input().split()))
    if c in d:
        d[c]+=b
    else:
        d[c]=b
d=sorted(list(Counter(d).items()),reverse=True)
check=n
ans=0
for i in d:
    if i[1]<check:
        check-=i[1]
        ans+=(i[0]*i[1])
    else:
        ans+=(i[0]*check)
        print(ans)
        break

# Optimized solution
from collections import Counter
n,m=list(map(int,input().split()))
d=Counter(list(map(int,input().split())))
for i in range(m):
    b,c=list(map(int,input().split()))
    if c in list(d.keys()):
        d[c]+=b
    else:
        d[c]=b
d=sorted(list(d.items()),reverse=True)
check=n
ans=0
for i in d:
    if i[1]<check:
        check-=i[1]
        ans+=(i[0]*i[1])
    else:
        ans+=(i[0]*check)
        print(ans)
        break
# Original solution
from collections import Counter
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))

X = Counter(A)
for i in range(M):
    B, C = list(map(int, input().split()))
    X[C] += B

Z = []
for key, value in list(X.items()):
    Z.append((key, value))

Z.sort(key=lambda x: x[0], reverse=True)

ans = 0
cnt = 0

for value, key in Z:
    if cnt + key <= N:
        ans += key * value
        cnt += key
    else:
        ans += (N - cnt) * value
        break

print(ans)


# Optimized solution
N, M = list(map(int, input().split()))
A = sorted(list(map(int, input().split())))
query = []
for i in range(M):
    b, c = list(map(int, input().split()))
    query.append([b, c])
query.sort(key=lambda q: q[1], reverse=True)


i = 0
for b, c in query:
    while i < N and b > 0:
        if A[i] < c:
            A[i] = c
            b -= 1
        i += 1

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

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

BC = []
for m in range(M):
    BC += [BC0[m][1]] * BC0[m][0]
    
summation = 0
for n in range(N):
    if len(BC)==0 or A[0]>=BC[0]:
        summation += A.pop(0)
    else:
        summation += BC.pop(0)
        
print(summation)

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

A = [[1,a] for a in A0] 
ABC = A + BC
ABC.sort(key=lambda x:x[1], reverse=True)

summation = 0
p = 0
n = 0

while n<N:
    summation += ABC[p][1] * (min(ABC[p][0], N-n))
    n += min(ABC[p][0], N-n)
    p += 1

print(summation)
# Original solution
import sys
input=sys.stdin.readline
import bisect

def main():
    _,M= list(map(int,input().split()))
    A = list(map(int,input().split()))
    A.sort()
    
    B = []
    for _ in range(M):
        b,c = list(map(int,input().split()))
        B.append((b,c))
    B.sort(key=lambda x: x[1])

    x = 0
    while B:
        b,c = B.pop()
        n = bisect.bisect(A,c,hi=min(b,len(A)))
        x += n*c
        A = A[n:]

    print((x + sum(A)))

if __name__ == '__main__':
    main()


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

def main():
    _,M= list(map(int,input().split()))
    A = list(map(int,input().split()))
    A.sort()
    
    B = []
    for _ in range(M):
        b,c = list(map(int,input().split()))
        B.append((b,c))
    B.sort(key=lambda x: x[1])

    x = 0
    m = 0
    while B:
        b,c = B.pop()
        n = bisect.bisect(A,c,lo=m,hi=min(m+b,len(A)))
        x += (n-m)*c
        m = n

    print((x + sum(A[n:])))

if __name__ == '__main__':
    main()
# Original solution
# ABC127D - Integer Cards
import sys
input = sys.stdin.readline

n, m = list(map(int, input().rstrip().split()))
num = list(map(int, input().rstrip().split()))
lst = [list(map(int, input().rstrip().split())) for _ in range(m)]
for i in lst:
    num += [i[1]] * i[0]
print((sum(sorted(num, reverse=True)[:n])))

# Optimized solution
# ABC127D - Integer Cards
import sys
input = sys.stdin.readline

n, m = list(map(int, input().rstrip().split()))
num = sorted(list(map(int, input().rstrip().split())))
lst = sorted([list(map(int, input().rstrip().split())) for _ in range(m)], key=lambda x:x[1], reverse=True)
ans = 0
idx = 0
flg = False
for i, j in lst:
    for _ in range(i):
        if idx < n and num[idx] < j:
            ans += j
            idx += 1
        else:
            flg = True
            break
    if flg:
        break
if idx < n:
    ans += sum(num[idx:])
print(ans)
# Original solution
from bisect import bisect_left

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

for _ in range(m):
  b, c = list(map(int, input().split()))
  i = bisect_left(alist, c)
  if i + 1 > b:
    alist = alist[b:] + [c]*b
  else:
    alist = alist[i:] + [c]*i
  alist.sort()

print((sum(alist)))



# Optimized solution
n, m = list(map(int, input().split()))
alist = list(sorted(map(int, input().split())))
bc = []
total = 0
for _ in range(m):
  bc.append(tuple(map(int, input().split())))

lim = n-1
total = 0
ans = 0
for b, c in sorted(bc, key=lambda x: -x[1]):
  while lim >= 0 and alist[lim] >= c:
    ans += alist[lim]
    lim -= 1
    total += 1
  ans += c * min(b, n-total)
  total += min(b, n-total)
  if total == n:
    break
if total == n:
  print(ans)
else:
  print((ans + sum(alist[total:])))
# Original solution
import heapq
def main():
    N,M=list(map(int,input().split()))
    A=list(map(int,input().split()))
    heapq.heapify(A)
    BC=[]
    for i in range(M):
        bc=list(map(int,input().split()))
        BC.append(bc)

    BC.sort(reverse=True,key=lambda x:x[1])
    new_BC=[]
    count=0
    for i in range(M):
        for j in range(BC[i][0]):
            new_BC.append(BC[i][1])
            count+=1
            if count==N:
                break
        if count==N:
            break

    flag=False
    for i in range(len(new_BC)):
        a=heapq.heappop(A)
        if a<new_BC[i]:
            heapq.heappush(A,new_BC[i])
        else:
            heapq.heappush(A,a)
            flag=True
        if flag:
            break
    
    print((sum(A)))
if __name__=="__main__":
    main()

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

    BC.sort(reverse=True,key=lambda x:x[1])
    new_BC=[]
    count=0
    for i in range(M):
        for j in range(BC[i][0]):
            new_BC.append(BC[i][1])
            count+=1
            if count==N:
                break
        if count==N:
            break

    for i in range(len(new_BC)):
        if A[i]<new_BC[i]:
            A[i]=new_BC[i]
        else:
            break
    
    print((sum(A)))
if __name__=="__main__":
    main()
# Original solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
rewritelist = []
for i in range(0, M):
	rewritelist.append(list(map(int, input().split())))
A.sort()
rewritelist.sort(key = lambda val: val[1], reverse = True)
cardlist = []
for i in range(0, N):
	if rewritelist == []: break
	cardlist.append(rewritelist[0][1])
	rewritelist[0][0] -= 1
	if rewritelist[0][0] == 0: rewritelist.pop(0)
for i in range(0, len(cardlist)):
	if cardlist[i] <= A[i]: break
	A[i] = cardlist[i]
sum = 0
for i in range(0, N):
	sum += A[i]
print(sum)

# Optimized solution
def smaller(x, y):
	if x < y: return x
	return y
def larger(x, y):
	if x > y: return x
	return y

N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort()
Asum = []
sum = 0
for i in range(0, N):
	sum += A[i]
	Asum.append(sum)
controllist = []
for i in range(0, M):
	controllist.append(list(map(int, input().split())))
controllist.sort(key = lambda v: v[1], reverse = True)
addlist = []
flag = False
for i in range(0, M):
	for j in range(0, controllist[i][0]):
		addlist.append(controllist[i][1])
		if len(addlist) == N:
			flag = True
			break
	if flag: break
sum = 0
for i in range(0, smaller(N, len(addlist))):
	sum += larger(A[i], addlist[i])
for i in range(len(addlist), N):
	sum += A[i]
print(sum)
# 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)


def f():
    s = sum(a)
    i = 0
    for v in bc:
        b, c = list(map(int, v))
        while b > 0:
            if i >= n:
                return s
            if c <= a[i]:
                return s
            s += c - a[i]
            b -= 1
            i += 1
    return s


print((f()))

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

d = []
for v in bc:
    b, c = list(map(int, v))
    d.extend([c] * b)
    if len(d) >= len(a):
        break

ans = sum(a)
for ai, di in zip(a, d):
    if ai < di:
        ans += di - ai

print(ans)
# Original solution
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush,heapify
from bisect import bisect_left

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

if __name__ == "__main__":
    main()


# Optimized solution
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush,heapify
from bisect import bisect_left

def main():
    N, M = list(map(int, input().split()))
    A = list(map(int, input().split()))
    A.sort()
    q = []
    for i in range(M):
        B, C = list(map(int, input().split()))
        heappush(q,(-C,B))
    
    ans = 0
    index = 0
    # print(A)
    while q:
        c,b = heappop(q)
        c *= -1
        # print(c,b)
        count = 0
        for i in range(b):
            if i + index == N:
                break
            if c > A[index+i]:
                ans += c
                count += 1
            else:
                break

        # print(ans,index)
        if count < b:
            index += count
            break
        else:
            index += count
            
    print((ans + sum(A[index:])))

if __name__ == "__main__":
    main()
# Original solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
#SA = sorted(A)

D = []
for _ in range(M):
  B, C = list(map(int, input().split()))
  D += B * [C]

A += D
SA = sorted(A, reverse=True)
#X = min(N, len(SD))
#SD = SD[:X]

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


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

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

SD = sorted(D, key=lambda x:x[1], reverse=True)

for b, c in SD:
  A += b * [c]
  if len(A) > N*2:
    break

SA = sorted(A, reverse=True)
#X = min(N, len(SD))
#SD = SD[:X]

print((sum(SA[:N])))
# Original solution
import bisect

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

for _ in range(m):
  b,c = list(map(int, input().split()))
  ind = bisect.bisect_left(A, c)
  A = A[:ind]+[c]*b+A[ind:]

print((sum(A[-n:])))

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

add = []

for b,c in BC:
  add += [c]*b
  if len(add)>n:
    break
    
ans = sorted(A+add)    
print((sum(ans[-n:])))
# Original solution
from sys import stdin
nii=lambda:list(map(int,stdin.readline().split()))

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

l=[]
for i in range(m):
  b,c=nii()
  l+=[c]*b
l=sorted(l,reverse=True)
l_len=len(l)
if l_len<n:
 l+=[0]*(n-l_len)

ans=0
for i in range(n):
  ans+=max(a[i],l[i])
print(ans)

# Optimized solution
from sys import stdin
nii=lambda:list(map(int,stdin.readline().split()))

n,m=list(map(int,input().split()))
a=sorted(list(nii()))
l=[list(nii()) for i in range(m)]
l=sorted(l,key=lambda x:x[1],reverse=True)

man=[]
num=0
for b,c in l:
  man+=[c]*b
  num+=b
  if num>n:
    break

ans=0
for i in range(n):
  if i<num:
    ans+=max(a[i],man[i])
  else:
    ans+=a[i]
print(ans)
# Original solution
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
l=[list(map(int,input().split())) for i in range(m)]

b_sum=0
for b,c in l:
  a+=[c]*b
  b_sum+=b

a=sorted(a)[b_sum:]

print((sum(a)))

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

c_l=[]
for b,c in l:
  c_l+=[c]*b
  if len(c_l)>=n:
    break

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

print((sum(a)))
# Original solution
from fractions import *
import sys
N, M = list(map(int, input().split()))

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

a = sorted(a)
bc = [list(map(int, sys.stdin.readline().split())) for _ in range(M)]

bc = sorted(bc)    

subar = a
for i in bc:
    sc = [i[1]] * i[0]
    suba = []
    ra = subar[i[0]:]
    if i[0] == len(a):
        suba = a
    suba = subar[:i[0]]
    if i[1] - max(suba) < 0:
        continue
    subar = sc + ra
    subar.sort()

print((sum(subar)))





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

a.sort()

pairs = [list(map(int, input().split())) for _ in range(m)]
pairs = sorted(pairs, key=lambda x: x[1])
cnt = 0
ans = 0
while cnt < n:
    if pairs != [] and a[-1] < pairs[-1][1] and cnt+pairs[-1][0] <= n:
        cnt += pairs[-1][0]
        ans += pairs[-1][0]*pairs[-1][1]
        pairs.pop(-1)
    elif pairs != [] and a[-1] < pairs[-1][1] and cnt+pairs[-1][0] > n:
        ans += (n - cnt)*pairs[-1][1]
        cnt += n - cnt
        break
    else:
        cnt+= 1
        ans += a[-1]
        a.pop(-1)

print(ans)
# Original solution
N,M = list(map(int,input().split()))
B = []
C = []
A = list(map(int,input().split()))
for j in range(1,M+1):
  B_val,C_val = list(map(int,input().split()))
  B.append(B_val)
  C.append(C_val)
A.sort()
for k in range(0,M):
  for l in range(0,B[k]):
    if A[0] <= C[k]:
      A[0] = C[k]
      A.sort()
    else:
      continue
print((sum(A)))

# Optimized solution
n,m = list(map(int,input().split()))
a = sorted(list(map(int,input().split())))
bc = sorted([list(map(int,input().split())) for _ in range(m)],key = lambda x:-x[1])
flag = 0
flag_2 = 0
#print(a,bc)
for i in range(n):
  if a[i] < bc[flag_2][1]:
    a[i] = bc[flag_2][1]
  else:
    break
  flag += 1
  if flag == bc[flag_2][0]:
    flag_2 += 1
    flag = 0
  if flag_2 == m:
    break
print((sum(a)))
# Original solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
CB = [0]*(M)
for i in range(M):
    b, c = list(map(int, input().split()))
    CB[i] = (c, b)

A.sort()
CB = list(reversed(sorted(CB)))

ind = 0
for i in range(M):
    (c, b) = CB[i]
    for _ in range(b):
        if ind >= N:
            break
        if A[ind] < c:
            A[ind] = c
            ind += 1
    if ind >= N:
        break

print((sum(A)))


# Optimized solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
CB = [0]*(M)
for i in range(M):
    b, c = list(map(int, input().split()))
    CB[i] = (c, b)

A.sort()
CB = list(reversed(sorted(CB)))

ind = 0
for i in range(M):
    (c, b) = CB[i]
    for _ in range(b):
        if ind >= N:
            break
        if A[ind] < c:
            A[ind] = c
            ind += 1
        else:
            break
    if ind >= N:
        break

print((sum(A)))
# Original solution
N, M = [int(s) for s in input().split(' ')]
As = [int(s) for s in input().split(' ')]
for m in range(M):
    B, C = [int(s) for s in input().split(' ')]
    As += [C] * B
As.sort()
print((sum(As[-N:])))



# Optimized solution
N, M = [int(s) for s in input().split(' ')]
As = [int(s) for s in input().split(' ')]
CBs = []
for m in range(M):
    B, C = [int(s) for s in input().split(' ')]
    CBs.append((C, B))
As.sort(reverse=True)
CBs.sort(reverse=True)
CBs.append((0, 0))
ret = 0
a = 0
bc = 0
n = 0
while n < N:
    C, B = CBs[bc]
    if As[a] < C:
        bc += 1
        pick = min(N - n, B)
        ret += pick * C
        n += pick
    else:
        ret += As[a]
        a += 1
        n += 1
print(ret)
# Original solution
#ABC127-D
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(eval(input()))
def S(): return list(eval(input()))
import bisect
n,m=IL()
A=IL()
A.sort()

for i in range(m):
    b,c=IL()
    s=bisect.bisect(A,c)
    
    if s<=b:
        A=[c]*s+A[s:]
    else:
        A=A[b:s]+[c]*b+A[s:]
           
print((sum(A)))

# Optimized solution
#ABC127-D
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(eval(input()))
def S(): return list(eval(input()))
n,m=IL()
A=IL()
A.sort()
Q=[IL() for i in range(m)]
Q.sort(key=lambda x:-x[1])
j=0
for i in range(n):
    if Q[j][1]<A[i]:
        break
    else:
        Q[j][0]-=1
        A[i]=Q[j][1]
    if Q[j][0]==0:
        j+=1
    if j==m:
        break
print((sum(A)))
# Original solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
numlist = []
for i in range(M):
    B, C = list(map(int, input().split()))
    numlist.append([C, B])
for i in range(N):
    numlist.append([A[i], 1])
numlist.sort(reverse = True)
count = N
ans = 0
while count >= 0:
    b, c = numlist.pop(0)
    if c <= count:
        ans += b * c
    else:
        ans += b * count
    count -= c
print(ans)



# Optimized solution

N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
num = dict()
for i in range(M):
    B, C = list(map(int, input().split()))
    if C in list(num.keys()):
        num[C] += B
    else:
        num[C] = B
for i in range(N):
    if A[i] in list(num.keys()):
        num[A[i]] += 1
    else:
        num[A[i]] = 1
dct = sorted(list(num.items()), reverse = True)
count = N
ans = 0
for i in range(len(dct)):
    b, c = dct[i]
    if c <= count:
        ans += b * c
    else:
        ans += b * count
    count -= c
    if count <= 0:
        break
print(ans)
# Original solution
n,m = list(map(int,input().split()))
A = list(map(int,input().split()))
for _ in range(m):
  b,c = list(map(int,input().split()))
  A.extend([c for _ in range(b)])
A.sort(reverse = True)

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



# Optimized solution
n,m = list(map(int,input().split()))
A = list(map(int,input().split()))
C = []
for _ in range(m):
  b,c = list(map(int,input().split()))
  C.append((c,b))
C.sort(reverse = True)

count = 0
for c,b in C:
  A.extend([c for _ in range(b)])
  count += b
  if count > n:
    break
A.sort(reverse = True)

print((sum(A[:n])))
# Original solution
n,m = list(map(int,input().split()))
li = list(map(int,input().split()))
tmp_li = []
for _ in range(m):
    b,c = list(map(int,input().split()))
    tmp_li.append((c,b))
tmp_li.sort(reverse=True)
#print(tmp_li)
count = 0
for t in tmp_li:
    kazu = min(t[1],n-count+1)
    li.extend([t[0]]*kazu)
    if count>=n:
        break
li.sort(reverse=True)
print((sum(li[:n])))

# Optimized solution
n,m = list(map(int,input().split()))
li = list(map(int,input().split()))
tmp_li = []
for _ in range(m):
    b,c = list(map(int,input().split()))
    tmp_li.append((c,b))
tmp_li.sort(reverse=True)
#print(tmp_li)
for c,b in tmp_li:
    li.extend([c]*b)
    if len(li)>= 2*n:
        break
li.sort(reverse=True)
print((sum(li[:n])))
# Original solution
n,m = list(map(int,input().split()))
li = list(map(int,input().split()))
tmp_li = []
for _ in range(m):
    b,c = list(map(int,input().split()))
    tmp_li.append((c,b))
tmp_li.sort(reverse=True)
#print(tmp_li)
""" for c,b in tmp_li:
    li.extend([c]*b)
    if len(li)>= 2*n:
        break
li.sort(reverse=True)
print(sum(li[:n])) """

count = 0
for c,d in tmp_li:
    kazu = min(d,n-count+1)
    li.extend([c]*kazu)
    if count>=n:
        break
li.sort(reverse=True)
print((sum(li[:n])))

# Optimized solution
n,m = list(map(int,input().split()))
li = list(map(int,input().split()))
tmp_li = []
for _ in range(m):
    b,c = list(map(int,input().split()))
    tmp_li.append([c,b])
tmp_li.sort(reverse=True)
#print(tmp_li)
count = 0
for t in tmp_li:
    li += [t[0]]*t[1]
    if len(li)>=2*n:
        break
li.sort(reverse=True)
print((sum(li[:n])))