import bisect
import numpy as np

N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
A = np.array(A, dtype=np.int64)

lst = [list(map(int, input().split())) for _ in range(M)]
lst = sorted(lst, key=lambda x:x[1], reverse=True)

x = 0
for i in range(len(lst)):
    idx = bisect.bisect_left(A[x:], lst[i][1])
    
    if(A[x+idx-1] <= lst[i][1]):     
        if(lst[i][0] >= idx):
            A[x:(x+idx)] = lst[i][1]
            x = x+idx
        else:
            A[x:(x+lst[i][0])] = lst[i][1]
            x = x + lst[i][0]
    else:
        break
    
print(sum(A))   import bisect bisect import numpy as np numpy N, M = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split M A = list(map(int, input().split())) A list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split A.sort() A.sort A sort A = np.array(A, dtype=np.int64) A np.array(A, dtype=np.int64) np.array np array A dtype=np.int64 dtype np.int64 np int64 list(map(int, input().split())) for _ in range(M) for _ in range(M) _ range(M) range M for _ in range(M) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split lst = [list(map(int, input().split())) for _ in range(M)] lst [list(map(int, input().split())) for _ in range(M)] lst = sorted(lst, key=lambda x:x[1], reverse=True) lst sorted(lst, key=lambda x:x[1], reverse=True) sorted lst key=lambda x:x[1] key lambda x:x[1] x[1] x 1 x reverse=True reverse True x = 0 x 0 for i in range(len(lst)):
    idx = bisect.bisect_left(A[x:], lst[i][1])
    
    if(A[x+idx-1] <= lst[i][1]):     
        if(lst[i][0] >= idx):
            A[x:(x+idx)] = lst[i][1]
            x = x+idx
        else:
            A[x:(x+lst[i][0])] = lst[i][1]
            x = x + lst[i][0]
    else:
        break
     i range(len(lst)) range len(lst) len lst idx = bisect.bisect_left(A[x:], lst[i][1]) idx bisect.bisect_left(A[x:], lst[i][1]) bisect.bisect_left bisect bisect_left A[x:] A x: x lst[i][1] [i] lst i 1 if(A[x+idx-1] <= lst[i][1]):     
        if(lst[i][0] >= idx):
            A[x:(x+idx)] = lst[i][1]
            x = x+idx
        else:
            A[x:(x+lst[i][0])] = lst[i][1]
            x = x + lst[i][0]
    else:
        break
     A[x+idx-1] <= lst[i][1] A[x+idx-1] A x+idx-1 x+idx x idx 1 lst[i][1] [i] lst i 1 if(lst[i][0] >= idx):
            A[x:(x+idx)] = lst[i][1]
            x = x+idx
        else:
            A[x:(x+lst[i][0])] = lst[i][1]
            x = x + lst[i][0]
     lst[i][0] >= idx lst[i][0] [i] lst i 0 idx A[x:(x+idx)] = lst[i][1] A[x:(x+idx)] A x:(x+idx) x x+idx x idx lst[i][1] [i] lst i 1 x = x+idx x x+idx x idx A[x:(x+lst[i][0])] = lst[i][1] A[x:(x+lst[i][0])] A x:(x+lst[i][0]) x x+lst[i][0] x lst[i][0] [i] lst i 0 lst[i][1] [i] lst i 1 x = x + lst[i][0] x x + lst[i][0] x lst[i][0] [i] lst i 0 break print(sum(A)) print sum(A) sum A x = 0 0 x idx = bisect.bisect_left(A[x:], lst[i][1]) bisect.bisect_left(A[x:], lst[i][1]) idx A = np.array(A, dtype=np.int64) np.array(A, dtype=np.int64) A M = map(int, input().split()) map(int, input().split()) M x = x + lst[i][0] x + lst[i][0] x A = list(map(int, input().split())) list(map(int, input().split())) A x = x+idx x+idx x lst = [list(map(int, input().split())) for _ in range(M)] [list(map(int, input().split())) for _ in range(M)] lst N, M = map(int, input().split()) map(int, input().split()) N lst = sorted(lst, key=lambda x:x[1], reverse=True) sorted(lst, key=lambda x:x[1], reverse=True) lst
import bisect
import numpy as np

N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
A = np.array(A, dtype=np.int64)

lst = [list(map(int, input().split())) for _ in range(M)]
lst = sorted(lst, key=lambda x:x[1], reverse=True)

x = 0
for i in range(len(lst)):
    idx = bisect.bisect_left(A[x:], lst[i][1])
    
    if(A[x+idx-1] <= lst[i][1]):     
        if(lst[i][0] >= idx):
            A[x:(x+idx)] = lst[i][1]
            x = x+idx
        else:
            A[x:(x+lst[i][0])] = lst[i][1]
            x = x + lst[i][0]
    else:
        break
    
print(sum(A))  