CodeEff/ECCO huggingface dataset:
# Original solution
import copy

n, m = [int(i) for i in input().split()]
ans = []
a = list(map(int, input().split()))
for i in range(len(a)):
    ans.append([1, a[i]])
b = []
c = []
for i in range(m):
    d, e = [int(i) for i in input().split()]
    ans.append([d, e])

ans1 = sorted(ans, key=lambda x: x[1], reverse = True)

ans2 = []
for i in range(len(ans1)):
    for j in range(ans1[i][0]):
        ans2.append(ans1[i])

ans3 = []
for i in range(n):
    ans3.append(ans2[i][1])
print((sum(ans3)))

# Optimized solution
import copy

n, m = [int(i) for i in input().split()]
ans = []
a = list(map(int, input().split()))
for i in range(len(a)):
    ans.append([1, a[i]])
for i in range(m):
    d, e = [int(i) for i in input().split()]
    ans.append([d, e])

ans1 = sorted(ans, key=lambda x: x[1], reverse = True)
cnt = 0
ans2 = 0
k = 0
while cnt < n:
    ans2 += ans1[k][0] * ans1[k][1]
    cnt += ans1[k][0]
    k += 1
ans2 -= (cnt - n) * ans1[k-1][1]
print(ans2)
# Original solution
from operator import itemgetter

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 = sorted(bc, key=itemgetter(1))


for i in range(m):
  k = 0
  a.sort()
  while k<bc[i][0] and a[k]<bc[i][1]:
    a[k] = bc[i][1]
    k += 1
    
print((sum(a)))

# Optimized solution
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)
k=0
for i in range(m):
    j=0
    while j<cb[i][1] and k+j<n and a[k+j]<cb[i][0]:
        a[k+j] = cb[i][0]
        j+=1
    k+=j
    if k==n:
        break
print((sum(a)))
# 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.extend([c]*b)    
d.sort(reverse=True)

if len(d)>n:d=d[:n]

a.extend(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)]
#print(bc)
bc=sorted(bc,key= lambda x:x[1],reverse=True)
#print(bc)

bc2=[]
for i in range(m):
    bc2.extend([bc[i][1]]*bc[i][0])
    if len(bc2)>n: break
#print(bc2)
a.extend(bc2)
a.sort(reverse=True)

print((sum(a[:n])))
# Original solution
#7  Integer Cards
n,m = list(map(int,input().split()))
a = list( map(int,input().split()))
bc=[[0]*2 for _ in range(m)]
for i in range(m):
    bc[i] = list(map(int,input().split()))

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

import bisect
ans=[]
for (bb,cc) in bc:
    idx = bisect.bisect(a,cc)
    n_replace = min(idx,bb)
    ans.extend([cc]*n_replace)
    a=a[n_replace:]
    
print((sum(ans)+sum(a)))


# Optimized solution
n,m = list(map(int,input().split()))
a = list( map(int,input().split()))
bc=[[0]*2 for _ in range(m)]
for i in range(m):
    bc[i] = list(map(int,input().split()))

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

nokori=n
bc2=[]
for (bb,cc) in bc:
    if bb<=nokori:
        bc2.extend([cc]*bb)
        nokori-=bb
    else:
        bc2.extend([cc]*nokori)
        nokori=0
        break

ans=sum(a)
for i in range(0,min(n,len(bc2))):
    if bc2[i]>a[i]:
        ans+=bc2[i]-a[i]
    else:
        break
print(ans)
# Original solution
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
bc=[]

import heapq  
heapq.heapify(a) 

for _ in range(m):
    b,c=list(map(int,input().split()))
    for _ in range(b):
        temp=heapq.heappop(a)
        if temp<c:
            heapq.heappush(a,c)
        else:
            heapq.heappush(a,temp)
            break
#print(a)
print((sum(list(a))))

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

import heapq  
heapq.heapify(a) 

bc=[]
for _ in range(m):
    b,c=list(map(int,input().split()))
    bc.append((c,b))

bc.sort(reverse=True)
for item in bc:
    b=item[1]
    c=item[0]    
    for _ in range(b):
        temp=heapq.heappop(a)
        if temp<c:
            heapq.heappush(a,c)
        else:
            heapq.heappush(a,temp)
            print((sum(list(a))))
            exit()
#print(a)
print((sum(list(a))))
# Original solution
from bisect import bisect_left, bisect_right

def readinput():
    n,m=list(map(int,input().split()))
    a=list(map(int,input().split()))
    b=[]
    c=[]
    for _ in range(m):
        bb,cc=list(map(int,input().split()))
        b.append(bb)
        c.append(cc)
    return n,m,a,b,c

def main(n,m,a,b,c):
    a.sort()
    for i in range(m):
        #print(a)
        bi=b[i]
        ci=c[i]
        j=bisect_left(a,ci)-1
        if j<0:
            continue
        if j>bi-1:
            save=a[bi:j+1]
            l=j-bi+1
            r=j
            a[l:r+1]=[ci]*(r-l+1)
            a[0:l]=save
        else:
            l=0
            r=j
            a[l:r+1]=[ci]*(r-l+1)

    sum=0
    for i in range(n):
        sum+=a[i]
    
    return sum

if __name__=='__main__':
    n,m,a,b,c=readinput()
    ans=main(n,m,a,b,c)
    print(ans)


# Optimized solution
from bisect import bisect_left, bisect_right

def readinput():
    n,m=list(map(int,input().split()))
    a=list(map(int,input().split()))
    b=[]
    c=[]
    for _ in range(m):
        bb,cc=list(map(int,input().split()))
        b.append(bb)
        c.append(cc)
    return n,m,a,b,c

def main(n,m,a,b,c):
    a.sort()
    for i in range(m):
        #print(a)
        bi=b[i]
        ci=c[i]
        j=bisect_left(a,ci)-1
        if j<0:
            continue
        if j>bi-1:
            save=a[bi:j+1]
            l=j-bi+1
            r=j
            a[l:r+1]=[ci]*(r-l+1)
            a[0:l]=save
        else:
            l=0
            r=j
            a[l:r+1]=[ci]*(r-l+1)

    sum=0
    for i in range(n):
        sum+=a[i]
    
    return sum

def main2(n,m,a,b,c):
    all=[]
    for i in range(n):
        all.append((a[i],1))
    for i in range(m):
        all.append((c[i],b[i]))
    all.sort(reverse=True, key=lambda x:x[0])
    #print(all)
    count=n
    sum=0
    i=0
    while(count>0):
        ni=all[i][1]
        if ni<=count:
            sum+=all[i][0]*ni
            count-=ni
        else:
            sum+=all[i][0]*count
            count=0
        i+=1
        #print(sum,count)
    return sum

if __name__=='__main__':
    n,m,a,b,c=readinput()
    ans=main2(n,m,a,b,c)
    print(ans)
# Original solution
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort()

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

cards.sort()
ans = 0
for i, num in enumerate(a):
    if not cards:
        break
    c, b = cards.pop()
    if c > num:
        a[i] = c
        b -= 1
    else:
        break

    if b:
        cards.append([c, b])

print((sum(a)))



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

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

ret = 0
for b, c in x:
    if not a:
        break
    if c > a[-1]:
        while c > a[-1]:
            ret += c
            a.pop()
            b -= 1
            if b == 0:
                break
            if not a:
                break
    else:
        break

print((ret + sum(a)))
# Original solution
from operator import itemgetter

N, M = list(map(int,input().split()))
A = list(map(int,input().split()))
BC = []
for i in range(M):
    tmp = list(map(int,input().split()))
    BC.append(tmp)
A.sort(reverse=True)
BC.sort(key=itemgetter(1),reverse=True)

l = 0
r = N-1
flg = False
for k in range(M):
    # l tansaku
    for p in range(N):
        if BC[k][1] > A[p]:
            l = p
            break
        elif p == N-1:
            flg = True
    #break
    if flg or r < l: break
    tmpC = BC[k][1]
    #chikan
    for o in range(BC[k][0]):
        if r < l or r == -1:
            flg = True
            break
        A[r] = tmpC
        r -= 1

print((sum(A)))

# Optimized solution
from operator import itemgetter

N, M = list(map(int,input().split()))
A = list(map(int,input().split()))
BC = []
for i in range(M):
    tmp = list(map(int,input().split()))
    BC.append(tmp)
A.sort(reverse=True)
BC.sort(key=itemgetter(1),reverse=True)

l = 0
r = N-1
flg = False
for k in range(M):
    # l tansaku
    for p in range(l,N):
        if BC[k][1] > A[p]:
            l = p
            break
        elif p == N-1:
            flg = True
    #break
    if flg or r < l: break
    tmpC = BC[k][1]
    #chikan
    for o in range(BC[k][0]):
        if r < l or r == -1:
            flg = True
            break
        A[r] = tmpC
        r -= 1

print((sum(A)))
# Original solution
N,M = list(map(int,input().split(' ')))
A = list(map(int,input().split(' ')))
B =[]
C =[]

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

A.sort()
l = sorted(l,key = lambda x:x[1])

flag =False
for i in l[::-1]:
    for j in A[:i[0]]:
        k = i[1]
        if j<k:
            if len(A)>1:
                A = A[1:]
                A.append(k)
            else:
                A[0] = k
        else:
            flag = True
            break
    if flag:
        break
print((sum(A)))

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

A.sort()
l = sorted(l,key = lambda x:x[1])

ind =0
for b,c in l[::-1]:
    mx = min(N,ind+b)
    for i in range(ind,mx):
        if c>A[i]:
            A[i] = c
    if mx==N:
        break
    ind += b
print((sum(A)))
# 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)]

A.sort()
BC.sort(key=lambda bc: bc[1], reverse=True)
update_bc = [0 for _ in range(N)]
cnt = 0
for b, c in BC:
    for i in range(b):
        if cnt < N:
            update_bc[cnt] = c
            cnt += 1
for i in range(N):
    if A[i] < update_bc[i]:
        A[i] = update_bc[i]
    else:
        break
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 _ in range(M)]

A.sort()
BC.sort(key=lambda bc: bc[1], reverse=True)
update_bc = [0 for _ in range(N)]
cnt = 0
for b, c in BC:
    for i in range(b):
        if cnt < N:
            update_bc[cnt] = c
            cnt += 1
        else:
            break
    if cnt >= N:
        break
for i in range(N):
    if A[i] < update_bc[i]:
        A[i] = update_bc[i]
    else:
        break
print((sum(A)))
# Original solution
import bisect
import heapq
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
b, c = [], []
heapq.heapify(a)

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

for i in range(m):
    cnt = 0
    while True:
        if cnt == b[i]:
            break
        minV = heapq.heappop(a)
        #print('# minV: ' + str(minV))
        if minV >= c[i]:
            heapq.heappush(a, minV)
            break
        else:
            heapq.heappush(a, c[i])
        cnt += 1

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




# Optimized solution
import bisect
import heapq
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
#b, c = [], []
bc = []
heapq.heapify(a)

for i in range(m):
    bc.append(list(map(int, input().split())))
    # v1, v2 = map(int, input().split())
    # b.append(v1)
    # c.append(v2)
bc.sort(key = lambda x: x[1], reverse=True)

for i in range(m):
    cnt = 0
    while True:
        if cnt == bc[i][0]:
            break
        minV = heapq.heappop(a)
        #print('# minV: ' + str(minV))
        if minV >= bc[i][1]:
            heapq.heappush(a, minV)
            break
        else:
            heapq.heappush(a, bc[i][1])
        cnt += 1

#print(a)
print((sum(a)))
# Original solution
from bisect import bisect_left
from operator import itemgetter

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

X = sorted(X, key=itemgetter(1), reverse=True)

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

Y = A + BC
Y.sort(reverse=True)
answer = sum(Y[:N])
print(answer)

# Optimized solution
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
A = lr()
BC = [lr() for _ in range(M)]
BC.sort(key=lambda x: x[1], reverse=True)
D = []
count = 0
for b, c in BC:
    x = [c] * b
    D.extend(x)
    count += b
    if count >= N:
        break

E = A + D
E.sort(reverse=True)
answer = sum(E[:N])
print(answer)
# Original solution
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
A = lr()
BC = [lr() for _ in range(M)]
BC.sort(key=lambda x: x[1], reverse=True)
D = []
count = 0
for b, c in BC:
    D += [c] * b
    count += b
    if count >= N:
        break

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


# Optimized solution
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
A = lr()
BC = [lr() for _ in range(M)]
BC.sort(key=lambda x: x[1], reverse=True)
D = []
count = 0
for b, c in BC:
    A.extend([c] * b)
    count += b
    if count >= N:
        break

A.sort(reverse=True)
answer = sum(A[:N])
print(answer)
# Original solution
import heapq

n, m = list(map(int,input().split()))
a = [(-x, x) for x in map(int,input().split())]
heapq.heapify(a)
sum_a = 0
for i in range(m):
    b,c = list(map(int,input().split()))
    for j in range(b):
        heapq.heappush(a,(-c, c))
for i in range(n):
    sum_a += heapq.heappop(a)[1]
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):
    b,c = list(map(int,input().split()))
    for j in range(b):
        heapq.heappush(a,c)
        if min(a) > c:
            break
print((sum(heapq.nlargest(n, a))))
# Original 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)]
hq = []
for i in range(N):
    heapq.heappush(hq, A[i])

for bc in BC:
    for i in range(bc[0]):
        tmp = heapq.heappop(hq)
        if tmp < bc[1]:
            heapq.heappush(hq, bc[1])
        else:
            heapq.heappush(hq, tmp)

print((sum(hq)))

# 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)]
BC = list(reversed(sorted(BC, key=lambda x:x[1])))
#print(BC)
hq = []
for i in range(N):
    heapq.heappush(hq, A[i])

flag = False
for bc in BC:
    for i in range(bc[0]):
        tmp = heapq.heappop(hq)
        if tmp < bc[1]:
            heapq.heappush(hq, bc[1])
        else:
            heapq.heappush(hq, tmp)
            flag = True
            break
    if flag:
        break

print((sum(hq)))
# Original solution
# N = int(input())
# A = [int(x) for x in input().split()]
# V = [[0] * 100 for _ in range(100)]

N, M = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
A.sort(reverse=True)

CB = []
for i in range(M):
    b, c = [int(x) for x in input().split()]
    CB.append((c, b))
CB.sort(reverse=True)

cnt = 0
L = []
for cb in CB:
    if cnt >= N:
        break
    L.extend([cb[0]] * cb[1])

newA = A + L
newA.sort(reverse=True)

newA = newA[0:N]
ans = sum(newA)

print(ans)


# Optimized solution
# N = int(input())
# A = [int(x) for x in input().split()]
# V = [[0] * 100 for _ in range(100)]

N, M = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
A.sort(reverse=True)

CB = []
for i in range(M):
    b, c = [int(x) for x in input().split()]
    CB.append((c, b))
CB.sort(reverse=True)


L = []
for cb in CB:
    if len(L) >= N:
        break
    L.extend([cb[0]] * cb[1])

newA = A + L
newA.sort(reverse=True)

newA = newA[0:N]
ans = sum(newA)

print(ans)
# Original solution
import sys, math, collections, heapq, itertools

F = sys.stdin
def single_input(): return F.readline().strip("\n")
def line_input(): return F.readline().strip("\n").split()
def gcd(a, b):
    a, b = max(a, b), min(a, b)
    while a % b > 0: a, b = b, a % b
    return b
  
def solve():
    N, M = list(map(int, line_input()))
    A = [int(a) for a in line_input()]
    change = [None] * M
    for i in range(M):
        b, c = list(map(int, line_input()))
        change[i] = [c, b]
    A.sort()
    change.sort(reverse = True)

    sum = 0
    index = 0
    for c, b in change:
        bleft = b
        while bleft > 0:
            if c > A[index]:
                sum += c
                index += 1
                bleft -= 1
                if index == N: break
            else: break
        if index == N: break
    if index < N:
        for i in range(index, N):
            sum += A[i]
    print(sum)

    return 0
  
if __name__ == "__main__":
    solve()

# 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()
    C = [[int(i) for i in input().split()] for _ in range(M)]
    C.sort(key=itemgetter(1), reverse = True)
    counter = 0
    for b, c in C:
        end = False
        for j in range(b):
            if counter > N - 1: 
                end = True
                break
            if A[counter] < c:
                A[counter] = c
                counter += 1
            else:
                end = True
                break
        if end: break
    print((sum(A)))
        
    return 0

if __name__ == "__main__":
    solve()
# Original solution
import sys
input = sys.stdin.readline
N, M = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
BC = [[int(x) for x in input().split()] for _ in range(M)]
BC = sorted(BC, reverse = True, key = lambda x:x[1])
N2 = N
D = []
for i in BC:
    temp = min(N2,i[0])
    D += [i[1]] * temp
    N2 -= temp
    if not N2:
        break
A += D
A.sort()
print((sum(A[len(A) - N:])))

# Optimized solution
import sys
input = lambda : sys.stdin.readline().rstrip()

n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
bc = []
for _ in range(m):
    bc.append(list(map(int, input().split())))
bc = sorted(bc, reverse=True, key=lambda x: x[1])
cnt = n
for b, c in bc:
    if cnt <= b:
        for i in range(cnt):
            a.append(c)
        break
    else:
        for i in range(b):
            a.append(c)
        cnt -= 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()))
BC = [list(map(int, input().split())) for _ in range(M)]

cards = A.copy()
for b, c in BC:
	cards += [c] * b

print((sum((sorted(cards)[::-1])[: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)]

cards = {}
for a in A:
	if not a in cards:
		cards[a] = 1
	else:
		cards[a] += 1

for b, c in BC:
	if not c in cards:
		cards[c] = b
	else:
		cards[c] += b

count, sum_card = 0, 0
for key in sorted(cards)[::-1]:
	value = cards[key]
	if count + value < N:
		sum_card += key * value
		count += value
	else:
		sum_card += key * (N - count)
		break

print(sum_card)
# Original solution
import itertools
N,M = (int(i) for i in input().split())
A = sorted([int(i) for i in input().split()])
accum = list(itertools.accumulate(A))
e = []
for k, v in sorted([[int(i) for i in input().split()] for i in range(M)], key=lambda x: -x[1]):
    for _ in range(k):
        e.append(v)
for i in range(min(len(e),len(A))):
    if A[i]<e[i]:
        A[i]=e[i]
    else:
        break
print((sum(A)))


# Optimized solution
N,M = (int(i) for i in input().split())
A = sorted([int(i) for i in input().split()])
e,p = [(0,0)],0
for k, v in sorted([[int(i) for i in input().split()] for i in range(M)], key=lambda x: -x[1]):
    e.append((e[p][0]+k,v))
    p+=1

p = 1
for i in range(min(e[-1][0],len(A))):
    B,C = e[p]
    if A[i]<C:
        A[i]=C
    else:
        break
    if i == B-1:
        p+=1
print((sum(A)))
# Original solution
import sys
from heapq import merge
N, M = list(map(int, sys.stdin.readline().split()))
A = sorted([-i for i in list(map(int, sys.stdin.readline().split()))])
BC = sorted([list(map(int, sys.stdin.readline().split())) for _ in range(M)], key=lambda x: x[1], reverse=True)

card_list1 = []
for i in range(M):
    tmp_list = [-1 * BC[i][1]] * BC[i][0] 
    card_list1.extend(tmp_list)

sorted_cards = list(merge(A, card_list1))
ans = sum(sorted_cards[:N]) * -1
print(ans)

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

card_list = A + BC
sorted_cards = sorted(card_list, key=lambda x: x[1], reverse=True)
n = 0
cards_sum = 0
for i in range(N):
    if sorted_cards[i][0] + n <= N:
        cards_sum += sorted_cards[i][1] * sorted_cards[i][0]
        n += sorted_cards[i][0]
    elif n < N and sorted_cards[i][0] + n > N:
        cards_sum += sorted_cards[i][1] * (N - n)
        n = N
        break
    else:
        break

print(cards_sum)
# Original solution
import sys

def intinput():
    return list(map(int,sys.stdin.readline().split()))

n,m=intinput()
a = list(intinput())
a.sort()
l = []
for i in range(m):
    b,c=intinput()
    for j in range(b):
        l.append(c)
l.sort(reverse=1)
sum=0
for i in range(len(a)):
    try:
        sum += max(a[i],l[i])
    except:
        sum += a[i]
print(sum)

# Optimized solution
import sys

def intinput(): return list(map(int,sys.stdin.readline().split()))

n,m=intinput()
a = list(intinput())
a = [(a[x],1) for x in range(n)]
for i in range(m):
    b,c=intinput()
    a.append((c,b))
a.sort(reverse=1)
res,cnt=0,n
for (x,y) in a:
    k = min(cnt,y)
    res += k*x
    cnt -= k
print(res)
# Original solution
import sys
from heapq import heappush, heappop

def main():
    input = sys.stdin.readline
    N, M = list(map(int, input().split()))
    heap = []
    ans = 0
    for a in map(int, input().split()):
        heappush(heap, a)
        ans += a

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

    BC = sorted(BC, key=lambda x: -x[1])
    for B, C in BC:
        for _ in range(B):
            a = heappop(heap)
            if a < C:
                heappush(heap, C)
                ans += C - a
            else:
                heappush(heap, a)
                break

    return ans


if __name__ == '__main__':
    print((main()))



# Optimized solution
import sys
from heapq import heappush, heappop

def main():
    input = sys.stdin.readline
    N, M = list(map(int, input().split()))
    heap = []
    ans = 0
    for a in map(int, input().split()):
        heappush(heap, a)

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

    BC = sorted(BC, key=lambda x: -x[1])
    for B, C in BC:
        for _ in range(B):
            a = heappop(heap)
            if a < C:
                heappush(heap, C)
            else:
                heappush(heap, a)
                break

    return sum(heap)


if __name__ == '__main__':
    print((main()))