CodeEff/ECCO huggingface dataset:
# Original solution
N,M = list(map(int,input().split()))
A = list(map(int,input().split()))

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

A.sort()

result = 0
start = len(A)-1
for k in range(start,start-N,-1):
    result += A[k]
print(result)


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

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

cards.sort()
cards.reverse()

result = 0
total = N
for num, cnt in cards:
    total -= cnt
    result += num*cnt
    if total < 0:
        result += num * total
        break
print(result)
# Original solution
# 高速版input
import sys
input = sys.stdin.readline

# 二分探索
import bisect

n,m = (int(i) for i in input().split())
a = sorted([int(i) for i in input().split()])
bc = [[int(i) for i in input().split()] for i in range(m)]

for b,c in bc:
    idx = bisect.bisect_left(a,c)
    a = (a[:idx] + [c]*b + a[idx:])[b:]

print((sum(a)))

# Optimized solution
# 高速版input
import sys
input = sys.stdin.readline

n,m = (int(i) for i in input().split())
a = [int(i) for i in input().split()]
bc = [[int(i) for i in input().split()] for i in range(m)]

x = []
for b,c in sorted(bc, key=lambda x: x[1], reverse=True):
    x.extend([c]*b)
    if len(x) > n:
        break
        
a.extend(x)
print((sum(sorted(a,reverse=True)[:n])))
# Original solution
import heapq

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

for i in range(m):
    b , c = list(map(int,input().split()))
    for j in range(b):
        p = heapq.heappop(a)
        if p >= c:
            heapq.heappush(a,p)
            break
        else:
            heapq.heappush(a,c)

print((sum(a)))

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

for i in range(m):
    b , c = list(map(int,input().split()))
    a.append([c,b])
a.sort(reverse = True)

ans = 0
for x,y in a:
    if y < n:
        ans += x*y
        n = n-y
    elif y >=n:
        ans += x*n
        break
print(ans)
# Original solution
import sys
sys.setrecursionlimit(10**6)

n, m = list(map(int, input().split()))
readline = sys.stdin.readline
a = [int(i) for i in readline().split()]
#bc = [[int(i) for i in readline().split()] for _ in range(m)]

import heapq

heapq.heapify(a)

for i in range(m):
#    b = bc[i][0]; c = bc[i][1]
    b,c = [int(i) for i in readline().split()]
    for _ in range(b):
        if a[0] < c:
            _ = heapq.heappop(a)
            heapq.heappush(a, c)
        else:
            break
    
ans = sum(a)
print(ans)

# Optimized solution
import sys
sys.setrecursionlimit(10**6)

n, m = list(map(int, input().split()))
readline = sys.stdin.readline
A = [int(i) for i in readline().split()]

A.sort()

CB = []
for _ in range(m):
    b, c = [int(i) for i in readline().split()]
    CB.append([c, b])
    
CB.sort(reverse=True)

idx = 0
c = 0
for i in range(n):
    if A[i] < CB[idx][0]:
        A[i] = CB[idx][0]
        c += 1
    if CB[idx][1] <= c:
        idx += 1
        c = 0
    if idx >= m:
        break

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


# Optimized solution
N,M = list(map(int,input().split()))
A_s = list(map(int,input().split()))
BC_s = []
for _ in range(M):
  tempB,tempC = list(map(int,input().split()))
  BC_s.append([tempB,tempC])
BC_s = reversed(sorted(BC_s, key=lambda x:x[1]))
D = []
A_s = sorted(A_s)

A_s_size = N
for BC in BC_s:
    B = BC[0]
    C = BC[1]
    if A_s_size == 0:
        break
    elif A_s_size < B:
        D.extend([C]*A_s_size)
        A_s_size -= A_s_size
    else:
        D.extend([C]*B)
        A_s_size -= B
minLength = min(N,len(D))
for i in range(minLength):
    if A_s[i] < D[i]:
        A_s[i] = D[i]
    else:
        break
print((sum(A_s[-N:])))
# Original solution
n, m = list(map(int,input().split()))
l = list(map(int,input().split()))
s = [list(map(int,input().split())) for i in range(m)]
# n,m = 3,2
# l = [5,1,4]
# s = [[2,3],[1,5]]
# l = [1, 8, 5, 7, 100, 4, 52, 33, 13, 5]
l.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 l[j] < s[i][1]:
            l[j] = s[i][1]
    idx += s[i][0]
        # else:
        #     idx += s[i][0]
        #     break
print((sum(l)))

# Optimized solution
n, m = list(map(int,input().split()))
l = list(map(int,input().split()))
s = [list(map(int,input().split())) for i in range(m)]
# n,m = 3,2
# l = [5,1,4]
# s = [[2,3],[1,5]]
# l = [1, 8, 5, 7, 100, 4, 52, 33, 13, 5]
l.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 l[j] < s[i][1]:
            l[j] = s[i][1]
        else:
            break
    idx += s[i][0]
print((sum(l)))
# Original solution
def main():
    N, M = (int(i) for i in input().split())
    A = [int(i) for i in input().split()]
    bcs = [[int(i) for i in input().split()] for _ in range(M)]
    bcs.sort(reverse=True,key=lambda c:c[1])

    ans = 0
    sel = []
    for a in A:
        if a < bcs[0][1]:
            sel.append(a)
        else:
            ans += a
    sel.sort()
    bcs = [[c] * b for b,c in bcs]
    stock = len(sel)
    from itertools import chain,zip_longest
    cnt = 0
    for a,card in zip_longest(sel,chain.from_iterable(bcs),fillvalue=0):
        if cnt == stock:
            break
        if a < card:
            ans += card
        else:
            ans += a
        cnt += 1
    print(ans)

if __name__ == "__main__":
    main()


# Optimized solution
def main():
    N, M = (int(i) for i in input().split())
    A = [int(i) for i in input().split()]
    bcs = [[int(i) for i in input().split()] for _ in range(M)]
    bcs.sort(reverse=True,key=lambda c:c[1])

    ans = 0
    sel = []
    for a in A:
        if a < bcs[0][1]:
            sel.append(a)
        else:
            ans += a
    sel.sort()
    stock = len(sel)
    cards = []
    for b,c in bcs:
        cards.extend([c] * b)
        if len(cards) >= stock:
            break
    from itertools import zip_longest
    cnt = 0
    for a,card in zip_longest(sel,cards,fillvalue=0):
        if cnt == stock:
            break
        if a < card:
            ans += card
        else:
            ans += a
        cnt += 1
    print(ans)

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

N, M = list(map(int, input().split(' ')))
A = list(map(int, input().split(' ')))
AA = [[a, 1] for a in A]
while 2 <= len(AA) and AA[0][0] == AA[1][0] :
    _, cnt = AA.pop(0)
    AA[0][1] += cnt

H = []
for aa in AA :
    heapq.heappush(H, aa)


for i in range(M) :
    #print(H)
    B, C = list(map(int, input().split(' ')))
    s = 0
    while len(H) > 0 and B > 0 :
        h = heapq.heappop(H)
        if C <= h[0] :
            heapq.heappush(H, h)
            break
        elif B <= h[1] :
            h[1] -= B
            if h[1] > 0 :
                heapq.heappush(H, h)
            s += B
            break
        else :
            B -= h[1]
            s += h[1]
    if s > 0 :
        heapq.heappush(H, [C, s])
#print(H)
ans = 0
for h in H :
    ans += h[0] * h[1]

print(ans)


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

D = [ tuple(map(int, input().split(' '))) for _ in range(M)]

AD = A + D
AD.sort(key = lambda x : -x[1])

ans = 0
cnt = 0
for ad in AD :
    if N < cnt + ad[0] :
        ans += ad[1] * (N - cnt)
        break
    else :
        cnt += ad[0]
        ans += ad[1] * ad[0]

print(ans)
# Original solution
argN,argM = list(map(int,input().split()))
aryA = list(map(int,input().split()))
wkA = aryA[:]
for i in range(argM):
  argB,argC = list(map(int,input().split()))
  wkA += [argC] * argB
wkA.sort()
wkA.reverse()
print((sum(wkA[:len(aryA)])))

# Optimized solution
argN,argM = list(map(int,input().split()))
aryA = list(map(int,input().split()))
aryCB = [] 
for i in range(argM):
  aryBC = list(map(int,input().split()))
  #print(aryBC)
  aryBC.reverse()
  aryCB.append(aryBC[:])
#print(aryCB)
aryCB.sort(reverse=True)
#print(aryCB)

aryD = []
for i in range(len(aryCB)):
  if len(aryD) + aryCB[i][1] < argN:
    aryD += [aryCB[i][0]] * aryCB[i][1]
  elif len(aryD) < argN:
    wk = argN - len(aryD)
    aryD += [aryCB[i][0]] * wk

wkA = aryA[:] + aryD[:]
#print(wkA)
wkA.sort(reverse=True)
print((sum(wkA[:len(aryA)])))
# Original solution
import heapq
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
heapq.heapify(a)
for i in range(m):
  b,c=list(map(int,input().split()))
  for j in range(b):
    x=a[0]
    if x<c:
      heapq.heappop(a)
      heapq.heappush(a, c)
    else:
      break
print((sum(a)))

# 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([b,c])
d=sorted(d, key=lambda x: x[1], reverse=True)
i=0
j=d[0][1]
k=0
while a[i]<j:
  a[i]=j
  d[k][0]-=1
  if d[k][0]==0:
    k+=1
    if k==m:
      break
    else:
      j=d[k][1]
  i+=1
  if i==n:
    break
print((sum(a)))
# Original solution
import heapq

N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
h = []
for a in A:
    heapq.heappush(h, a)

for i in range(M):
    B, C = list(map(int, input().split()))
    for i in range(B):
        vmin = heapq.heappop(h)
        if vmin < C:
            heapq.heappush(h, C)
        else:
            heapq.heappush(h, vmin)
            break

print((sum(h)))


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

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

p = 0
for C, B in CB:
    while p < len(A) and A[p] >= C:
        p += 1
    if p >= len(A):
        break
    for i in range(B):
        if p >= len(A):
            break
        if A[p] < C:
            A[p] = C
            p += 1
        else:
            break
print((sum(A)))
# Original solution
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return eval(input())
from collections import defaultdict

def main():
    N, M = LI()
    A = LI()
    BC = []
    for _ in range(M):
        BC.append(LI())
    A.sort()
    for B, C in BC:
        for i in range(B):
            if C <= A[i]:
                break
            A[i] = C
        A.sort()
    ans = sum(A)
    return ans

print((main()))

# Optimized solution
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return eval(input())
from collections import defaultdict
from operator import itemgetter

def main():
    N, M = LI()
    A = LI()
    BC = []
    for _ in range(M):
        BC.append(LI())
    for a in A:
        BC.append([1, a])
    BC.sort(reverse=True, key=itemgetter(1))
    cnt = 0
    ans = 0
    for B, C in BC:
        if cnt + B >= len(A):
            ans += C * (len(A) - cnt)
            break
        ans += C * B
        cnt += B
    return ans

print((main()))
# Original solution
import heapq
def main():
    N, M = list(map(int, input().split()))
    A = list(map(int, input().split()))
    heapq.heapify(A)
    for _ in range(M):
        b, c = list(map(int, input().split()))
        for i in range(b):
            if A[0] < c:
                heapq.heappushpop(A, c)
            else:
                break
    return sum(A)
print((main()))



# Optimized solution
import heapq
def main():
    N, M = list(map(int, input().split()))
    A = list(map(int, input().split()))
    B = [(1, -i) for i in A]
    heapq.heapify(A)
    for _ in range(M):
        b, c = list(map(int, input().split()))
        B.append((b,-c))
    B.sort(key=lambda x: x[1])
    t = 0
    i = 0
    tl = []
    while t < N:
        tl.extend([-B[i][1]] * B[i][0])
        t += B[i][0]
        i += 1
    return sum(tl[:(N)])
print((main()))
# Original solution
N, M = list(map(int, input().split()))
A = [int(i) for i in input().split()]
B = [0] * M
C = [0] * M
Y = []
for i in range(M):
    B[i], C[i] = list(map(int, input().split()))
    Y += [C[i]] * B[i]

A.extend(Y)
A.sort(reverse=True)
print((sum(A[:N])))

# Optimized solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
CB = []
for i in range(M):
    B, C = list(map(int, input().split()))
    CB.append([C, B])
 
A.sort()
A.reverse()
CB.sort()
CB.reverse()
 
ans = 0
a_index = 0
cb_index = 0
f = 0
 
while True:
    if f == 1 or A[a_index] >= CB[cb_index][0]:
        ans += A[a_index]
        a_index += 1
        N -= 1
        if N == 0:
            break
    else:
        if N <= CB[cb_index][1]:
            ans += CB[cb_index][0] * N
            break
        else:
            ans += CB[cb_index][0] * CB[cb_index][1]
            N -= CB[cb_index][1]
            cb_index += 1
            if len(CB) == cb_index:
              f = 1
 
print(ans)
# Original solution
def I(): return int(eval(input()))
def LI(): return list(map(int,input().split()))
def MI(): return list(map(int,input().split()))
def LLI(n): return [list(map(int, input().split())) for _ in range(n)]

n,m = MI()
card = LI()
for i in range(m):
    b,c = MI()
    card += [c]*b
card.sort(reverse = True)
print((sum(card[:n])))


# Optimized solution
def I(): return int(eval(input()))
def LI(): return list(map(int,input().split()))
def MI(): return list(map(int,input().split()))
def LLI(n): return [list(map(int, input().split())) for _ in range(n)]

#bisect.bisect_left(list,key)はlistのなかでkey未満の数字がいくつあるかを返す
#つまりlist[i] < x となる i の個数
#bisect.bisect_right(list, key)はlistのなかでkey以下の数字がいくつあるかを返す
#つまりlist[i] <= x となる i の個数
#これを応用することで
#len(list) - bisect.bisect_left(list,key)はlistのなかでkey以上の数字がいくつあるかを返す
#len(list) - bisect.bisect_right(list,key)はlistのなかでkeyより大きい数字がいくつあるかを返す
#これらを使うときはあらかじめlistをソートしておくこと！

n,m = MI()
card = LI()
for i in range(m):
    b,c = MI()
    card += [c]*b
card.sort(reverse = True)
print((sum(card[:n])))
# Original solution
import queue


def integer_cards(N: int, M: int, A: list, queries: list)->int:
    que = queue.PriorityQueue()

    for a in A:
        que.put(a)

    for B, C in queries:
        for _ in range(B):
            x = que.get()

            que.put(max(x, C))

    res = 0
    for _ in range(N):
        res += que.get()
    return res


if __name__ == "__main__":
    M = 0

    N, M = list(map(int, input().split()))
    A = [int(s) for s in input().split()]
    queries = [tuple(int(s) for s in input().split()) for _ in range(M)]

    ans = integer_cards(N, M, A, queries)
    print(ans)


# Optimized solution
import queue


def integer_cards(N: int, M: int, A: list, queries: list)->int:
    que = queue.PriorityQueue()

    for a in A:
        que.put(a)

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

    for B, C in queries:
        x = que.get()
        que.put(x)

        if C <= x:
            break

        for _ in range(B):
            x = que.get()

            que.put(max(x, C))

            if C <= x:
                break

    res = 0
    for _ in range(N):
        res += que.get()
    return res


if __name__ == "__main__":
    M = 0

    N, M = list(map(int, input().split()))
    A = [int(s) for s in input().split()]
    queries = [tuple(int(s) for s in input().split()) for _ in range(M)]

    ans = integer_cards(N, M, A, queries)
    print(ans)
# Original solution
ri = lambda: int(input())
rl = lambda: list(map(int,input().split()))
rr = lambda N: [ri() for _ in range(N)]
YN = lambda b: print('YES') if b else print('NO')
yn = lambda b: print('Yes') if b else print('No')
OE = lambda x: print('Odd') if x%2 else print('Even')
INF = 10**18
MOD=10**9+7

N,M=rl()
A=rl()
B=[0]*M
C=[0]*M
for i in range(M):
    B[i],C[i]=rl()

for b,c in zip(B,C):
    A.extend([c]*b)

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


# Optimized solution
import sys
input = sys.stdin.readline
ri = lambda: int(input())
rl = lambda: list(map(int,input().split()))
rr = lambda N: [ri() for _ in range(N)]
YN = lambda b: print('YES') if b else print('NO')
yn = lambda b: print('Yes') if b else print('No')
OE = lambda x: print('Odd') if x%2 else print('Even')
INF = 10**18
MOD=10**9+7

N,M=rl()
A=rl()
B=[0]*M
C=[0]*M
for i in range(M):
    B[i],C[i]=rl()


D = []
for a in A:
    D.append([a,1])
for b,c in zip(B,C):
    D.append([c,b])

D.sort(reverse=True)

ans = 0
cnt = N
for val,num in D:
    if cnt >= num:
        cnt -= num
        ans += val*num
    else:
        ans += val*cnt
        break
print(ans)
# Original solution
n, m = list(map(int, input().split()))
cards = list(map(int, input().split()))

for i in range(m):
    b, c = list(map(int, input().split()))
    cards += ([c] * b)

cards.sort(reverse=True)

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

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

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

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

for card in cards:
    A += [card[1]]*card[0]
A.sort(reverse=True)

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


# Optimized 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 += [c]*b
A.sort(reverse=True)

print((sum(A[:N])))
# Original solution
import heapq
N, M = list(map(int, input().split()))
arr = list(map(int, input().split()))
heapq.heapify(arr)
for i in range(M):
    B, C = list(map(int, input().split()))
    count = 0
    while True:
        a = heapq.heappop(arr)
        if a >= C or B <= count:
            heapq.heappush(arr,a)
            break
        else:
            count += 1
            heapq.heappush(arr,C)
print((sum(arr)))

# Optimized solution
import heapq
N, M = list(map(int, input().split()))
a = list(map(int, input().split()))
bc = [list(map(int, input().split())) for _ in range(M)]
heapq.heapify(a)
bc = sorted(bc, reverse=True, key=lambda x: x[1])
for i in range(M):
    count = 0
    while True:
        t = heapq.heappop(a)
        if t >= bc[i][1] or bc[i][0] <= count:
            heapq.heappush(a,t)
            break
        else:
            count += 1
            heapq.heappush(a,bc[i][1])
print((sum(a)))
# Original solution
import sys
import heapq

input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


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

    H = []
    for i in list(map(int, input().split())):
        heapq.heappush(H, [-i, 1])

    for i in range(M):
        b, c = list(map(int, input().split()))
        heapq.heappush(H, [-c, b])

    cnt = 1
    ans = 0
    while cnt <= N:
        c, b = heapq.heappop(H)
        c = -c
        ans += c
        if b > 1:
            b -= 1
            heapq.heappush(H, [-c, b])
        cnt += 1

    print(ans)


if __name__ == "__main__":
    main()


# Optimized solution
import sys
import heapq

input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


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

    H = []
    for i in list(map(int, input().split())):
        heapq.heappush(H, (-i, 1))

    for i in range(M):
        b, c = list(map(int, input().split()))
        heapq.heappush(H, (-c, b))

    cnt = 1
    ans = 0
    while cnt <= N:
        c, b = heapq.heappop(H)
        c = -c
        ans += c
        if b > 1:
            b -= 1
            heapq.heappush(H, (-c, b))
        cnt += 1

    print(ans)


if __name__ == "__main__":
    main()
# Original solution
n, m = list(map(int, input().split()))
a = [int(i) for i in input().split()]
a = sorted(a)

l = []
for i in range(m):
  b, c = list(map(int, input().split()))
  x = [c]*b
  l += x
  
a = a+l
a = sorted(a)
a = a[-n:]
print((sum(a)))

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

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

b = []
for _ in range(n):
  b.append(l[0][1])
  l[0][0] -= 1
  if l[0][0] == 0:
    l.pop(0)
  if l == []:
    break

for i in range(len(b)):
  if a[i] < b[i]:
    a[i] = b[i]
  else:
    break
    
print((sum(a)))
# Original solution
import sys
from collections import deque
input = sys.stdin.readline
N, M = list(map(int, input().split()))
*A, = list(map(int, input().split()))
lis = []
for _ in range(M):
    B, C = list(map(int, input().split()))
    for _ in range(B):
        lis.append(C)
A.sort()
d = deque(A)
lis.sort(reverse=True)
l = len(lis)
for i in range(l):
    if d[0] < lis[i]:
        d.popleft()
        d.append(lis[i])
print((sum(d)))


# Optimized solution
import sys
input = sys.stdin.readline
N, M = list(map(int, input().split()))
*A, = list(map(int, input().split()))
dic = {}
for a in A:
    if a in dic:
        dic[a] += 1
    else:
        dic[a] = 1
for _ in range(M):
    B, C = list(map(int, input().split()))
    if C in dic:
        dic[C] += B
    else:
        dic[C] = B
dic = list([list(x) for x in sorted(list(dic.items()), reverse=True)])
i = 0
ans = 0
for _ in range(N):
    ans += dic[i][0]
    dic[i][1] -= 1
    if dic[i][1] == 0:
        i += 1
print(ans)
# Original solution
import heapq

nm = list([int(x) for x in input().split()])
n = nm[0]
m = nm[1]

a = list([int(x) for x in input().split()])
bc = [list([int(x) for x in input().split()]) for i in range(m)]

head_a = []

for a_item in a:
  heapq.heappush(head_a, a_item)

# そーと
bc.sort(key=lambda x: x[1])
bc = list(reversed(bc))

for bc_item in bc:
  for i in range(bc_item[0]):
    head_a_item = heapq.heappop(head_a)
    heapq.heappush(head_a, max(head_a_item, bc_item[1]))

print((sum(head_a)))

# Optimized solution
import heapq

nm = list([int(x) for x in input().split()])
n = nm[0]
m = nm[1]

a = list([int(x) for x in input().split()])
bc = [list([int(x) for x in input().split()]) for i in range(m)]

head_a = []

for a_item in a:
  heapq.heappush(head_a, a_item)

# そーと
bc.sort(key=lambda x: x[1])
bc = list(reversed(bc))

for bc_item in bc:
  done = False
  for i in range(bc_item[0]):
    head_a_item = heapq.heappop(head_a)
    if (head_a_item >= bc_item[1]):
      heapq.heappush(head_a, max(head_a_item, bc_item[1]))
      done = True
      break
    else:
      heapq.heappush(head_a, max(head_a_item, bc_item[1]))
  if (done):
    break

print((sum(head_a)))
# Original solution

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


A=[int(i) for i in (input().split(" "))]



BC = [list(map(int,input().split())) for _ in range(M)]

for b,c in BC:
    count=0
    A.sort()
    for i in range(b):
        if c<A[i]:
            break
        else:
            A[i]=c
    
suma=0
#for i in A:
#    suma+=i

print((sum(A)))

# Optimized solution

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


A=[int(i) for i in (input().split(" "))]

A.sort()

BC = [list(map(int,input().split())) for _ 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)
suma=0
#for i in A:
#    suma+=i

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