Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

X,Y,A,B,C = map(int,input().split())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))
p = sorted(p)
q = sorted(q)
r = sorted(r)
P = A - X
Q = B - Y
R = C - 1
found = True
while found and R >= 0:
    if (r[R] > p[P]):
        if (q[Q] > p[P]):
            P += 1
            R -= 1
    elif (r[R] > q[Q]):
        Q += 1
        R -= 1
    else:
        found = False
print(sum(p[P:])+sum(q[Q:])+sum(r[R+1:]))

### FIXED CODE ###

X,Y,A,B,C = map(int,input().split())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))
p = sorted(p)
q = sorted(q)
r = sorted(r)
P = A - X
Q = B - Y
R = C - 1
found = True
for i in range(C+1):
    if R < 0:
        break
    if P < A:
        if (r[C-i-1] >= p[P]):
            if Q < B:
                if (q[Q] >= p[P]):
                    P += 1
                    R -= 1
                elif r[C-i-1] > q[Q]:
                    Q += 1
                    R -= 1
                else:
                    break
            else:
                P += 1
                R -= 1
        elif Q < B:
            if r[C-i-1] > q[Q]:
                Q += 1
                R -= 1
            else:
                break
        else:
            break
    elif Q < B:
        if r[C-i-1] > q[Q]:
            Q += 1
            R -= 1
        else:
            break    
    else:
        break
print(sum(p[P:])+sum(q[Q:])+sum(r[R+1:]))
### BUGGY CODE ###

N = int(input())
A = list(map(int, input().split()))
for i in range(N):
    cost = 0
    pos = 0
    for j in range(N):
        if(i == j):
            continue
        cost += abs(A[j] - pos)
        pos = A[j]
    cost += abs(pos)
    print(cost)

### FIXED CODE ###

N = int(input())
A = [0]
B = list(map(int, input().split()))
for i in range(len(B)):
    A.append(B[i])
A.append(0)
S = []
cost = 0
pos = 0
for i in range(1, N + 1):
    cost += abs(A[i] - pos)
    pos = A[i]
    S.append(cost)
S.append(cost + abs(pos))

for i in range(N):
    print(S[-1] + abs(A[i] - A[i + 2]) - (abs(A[i] - A[i + 1]) + abs(A[i + 1] - A[i + 2])))
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
r = []
for i in range(n):
  tmp = [(i + 2 * k + 1) % n for k in range(n//2)]
  c = 0
  for j in range(n):
    if j in tmp:
      c -= a[j]
    else:
      c += a[j]
  r.append(c)
print(*r)

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
r = []
c = 0
for j in range(n):
  if j % 2 == 1:
    c -= a[j]
  else:
    c += a[j]
r.append(c)
for i in range(n-1):
  r.append((a[i] - r[i] // 2) * 2)
print(*r)
### BUGGY CODE ###

import itertools
n = int(input())
l = list(map(int,input().split()))
l_comb = itertools.combinations(l, 3)
ans = 0

for i in l_comb:
  if i[0] < i[1] + i[2] and i[1] < i[0] + i[2] and i[2] < i[0] + i[1]:
    ans += 1

print(ans)

### FIXED CODE ###

import itertools
import bisect
n = int(input())
l = list(map(int,input().split()))
l.sort()
ans = 0

for i in range(len(l)-2):
  for j in range(i+1,len(l)):
    k = bisect.bisect_left(l, l[i]+l[j])
    if k > j:
      ans += k - j -1

print(ans)
### BUGGY CODE ###

import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def solve():
    dp = [[[-inf] * 4 for _ in range(w)] for _ in range(h)]
    dp[0][0][0] = 0
    for i in range(h):
        for j in range(w):
            for c in range(4):
                pre = dp[i][j][c]
                if pre == -inf: continue
                if i + 1 < h: dp[i + 1][j][0] = max(dp[i + 1][j][0], pre)
                if j + 1 < w: dp[i][j + 1][c] = max(dp[i][j + 1][c], pre)
                if aa[i][j]:
                    if c < 3 and i + 1 < h: dp[i + 1][j][0] = max(dp[i + 1][j][0], pre + aa[i][j])
                    if c < 3 and j + 1 < w: dp[i][j + 1][c + 1] = max(dp[i][j + 1][c + 1], pre + aa[i][j])

    ans = -inf
    for c in range(4):
        if c == 3: ans = max(ans, dp[h - 1][w - 1][c])
        else: ans = max(ans, dp[h - 1][w - 1][c] + aa[h - 1][w - 1])
    print(ans)

inf=10**16
h,w,k=MI()
aa=[[0]*w for _ in range(h)]
for _ in range(k):
    i,j,v=MI()
    aa[i-1][j-1]=v
solve()


### FIXED CODE ###

import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def solve():
    dp0 = [0] * (w + 1)
    dp1 = [0] * (w + 1)
    dp2 = [0] * (w + 1)
    dp3 = [0] * (w + 1)
    for i in range(h):
        for j in range(w):
            v = vv[i * w + j]
            if v:
                dp3[j] = max(dp3[j], dp2[j] + v)
                dp2[j] = max(dp2[j], dp1[j] + v)
                dp1[j] = max(dp1[j], dp0[j] + v)
            dp3[j + 1] = max(dp3[j + 1], dp3[j])
            dp2[j + 1] = max(dp2[j + 1], dp2[j])
            dp1[j + 1] = max(dp1[j + 1], dp1[j])
            dp0[j] = max(dp1[j], dp2[j], dp3[j])
    print(dp0[-2])

h, w, k = MI()
vv = [0] * h * w
for _ in range(k):
    i, j, v = MI()
    i, j = i - 1, j - 1
    vv[i * w + j] = v
solve()
### BUGGY CODE ###

#https://atcoder.jp/contests/abc162/tasks/abc162_c
# -*- coding: utf-8 -*-
import fractions
import itertools
N = int(input())
A = 0
M = []
T =0
for i in range(1,N+1):
  M.append(i)
for i in itertools.combinations_with_replacement(M, 3):
  tmp1 = list(i)
  tmp2 = fractions.gcd(tmp1[0],tmp1[1])
  tmp3 = fractions.gcd(tmp2,tmp1[2])
  if (tmp1[0]==tmp1[1] and tmp1[0]!=tmp1[2]) or (tmp1[1]==tmp1[2] and tmp1[1]!=tmp1[0]) or (tmp1[2]==tmp1[0] and tmp1[2]!=tmp1[1]):
    A += tmp3*3
  elif tmp1[0] != tmp1[1] and tmp1[0] != tmp1[2] and tmp1[1] != tmp1[2]:
    A += tmp3*6
  else:
    A += tmp3
  T +=1
print(A)

### FIXED CODE ###

#https://atcoder.jp/contests/abc162/tasks/abc162_c
# -*- coding: utf-8 -*-
import math
ans = 0
K = int(input())
for i in range(1,K+1):
  for j in range(1,K+1):
    for k in range(1,K+1):
      ans += math.gcd(math.gcd(i,j),k)
print(ans)
### BUGGY CODE ###

from functools import reduce
import functools
import fractions
 
def gcd(*numbers):
  return reduce(gcd_, numbers)

def gcd_(a, b):
  if (b == 0):
    return a
  return gcd(b, a % b)
 
sum = 0
K = int(input())
 
for i in range(1,K+1):
  for j in range(i,K+1):
    for k in range(j,K+1):
      if (i == j) & (j == k):
        sum += gcd(i,j,k)
      elif (i == j) & (j != k):
        sum += gcd(i,j,k)*3
      elif (i != j) & (j == k):
        sum += gcd(i,j,k)*3
      elif (i != j) & (j != k):
        sum += gcd(i,j,k)*6

print(sum)


### FIXED CODE ###

from functools import reduce
import functools
import fractions
 
@functools.lru_cache(maxsize=None)
def gcd(*numbers):
  return reduce(gcd_, numbers)

@functools.lru_cache(maxsize=None)
def gcd_(a, b):
  if (b == 0):
    return a
  return gcd(b, a % b)
 
sum = 0
K = int(input())
 
for i in range(1,K+1):
  for j in range(i,K+1):
    for k in range(j,K+1):
      if (i == j) & (j == k):
        sum += gcd(i,j,k)
      elif (i == j) & (j != k):
        sum += gcd(i,j,k)*3
      elif (i != j) & (j == k):
        sum += gcd(i,j,k)*3
      elif (i != j) & (j != k):
        sum += gcd(i,j,k)*6

print(sum)
### BUGGY CODE ###

n,m=map(int,input().split())
a=sorted(list(map(int,input().split())))
for i in range(m):
  a.sort()
  b,c=map(int,input().split())
  for j in range(b):
    if a[j]<c:
      a[j]=c
    else:
      break
print(sum(a))

### FIXED CODE ###

import heapq

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

li=[list(map(int,input().split())) for i in range(m)]
li=sorted(li,key=lambda x:x[1],reverse=True)
for i in range(m):
  tmp=heapq.heappop(a)
  if tmp>li[i][1]:
    heapq.heappush(a,tmp)
    break
  else:
    heapq.heappush(a,tmp)
    for j in range(li[i][0]):
      tmp=heapq.heappop(a)
      if tmp<li[i][1]:
        heapq.heappush(a,li[i][1])
      else:
        heapq.heappush(a,tmp)
        break
print(sum(a))
### BUGGY CODE ###

n, y = list(map(int, input().split()))
nasi = True
for d in range(0,n+1):
    if nasi == False:
        break
    for e in range(0,n+1):
        if nasi == False:
            break
        for f in range(0,n+1):
            if nasi == False:
                break
            if (((10000 * d) + (5000 * e) + (1000 * f)) == y) & (d+e+f==n):
                nasi = False
                print(d,e,f)
                break
if nasi == True:
    print('-1 -1 -1')

### FIXED CODE ###

n, y = list(map(int, input().split()))
nasi = True
for d in range(0,n+1):
    if nasi == False:
        break
    for e in range(0,(n+1)-d):
        if nasi == False:
            break
        f = n - d - e
        if ((10000 * d) + (5000 * e) + (1000 * f) == y):
            nasi = False
            print(d,e,f)
            break
if nasi == True:
    print('-1 -1 -1')
### BUGGY CODE ###

S = input()
Q = int(input())
query = []
for _ in range(Q):
    q = list(input().split())
    if len(q) != 1:
        q = [int(x) for x in q[:2]] + [q[2]]
    else:
        q = list(map(int, q)) + [0, '']
    query.append(q)

S = list(S)
forward = 1
for t, f, c in query:
    if t == 1:
        forward *= -1
    else:
        if f == 1:
            if forward == 1:
                S = [c] + S
            else:
                S = S + [c]
        else:
            if forward == 1:
                S = S + [c]
            else:
                S = [c] + S
if forward == -1:
    S = list(reversed(S))
print(''.join(S))

### FIXED CODE ###

S = input()
Q = int(input())
query = [list(input().split()) for _ in range(Q)]
forward = 1
f = ''
b = ''
for i in range(Q):
    if query[i][0] == '1':
        forward *= -1
    else:
        if query[i][1] == '1':
            if forward == 1:
                f += query[i][2]
            else:
                b += query[i][2] 
        else:
            if forward == 1:
                b += query[i][2] 
            else:
                f += query[i][2]
S = ''.join(list(reversed(f))) + S + b

if forward == -1:
    ans = ''.join(list(reversed(S)))
else:
    ans = S
print(ans)
### BUGGY CODE ###

s = input()
length=len(s)
l = int(len(s)/2)
def erase_01_10(moji):
  if moji.count('01')>0:
    moji = moji.replace('01','')
  elif moji.count('10')>0:
    moji = moji.replace('10','')
  return moji
for i in range(l):
  if s == erase_01_10(s):
    break
  s = erase_01_10(s)
print(length-len(s))

### FIXED CODE ###

s = input()
length=len(s)
#print(s.count('0'))
#print(s.count('1'))
m=min(s.count('0'),s.count('1'))
print(2*m)
### BUGGY CODE ###

n, m = map(int, input().split())
ab = [list(map(int, input().split())) for i in range(m)]
contain_one = [[i, j] for i, j in ab if i == 1]

for a, b in contain_one:
    if [b, n] in ab:
        print("POSSIBLE")
        exit()
else:
    print("IMPOSSIBLE")


### FIXED CODE ###

n, m = map(int, input().split())
ab = []
for i in range(m):
    a, b = map(int, input().split())
    if a == 1:
        ab.append(b)
    if b == n:
        ab.append(a)

if len(ab) != len(set(ab)):
        print("POSSIBLE")
        exit()
else:
    print("IMPOSSIBLE")
### BUGGY CODE ###

S= input()
Q = int(input())
rev_cnt = 0

a = ""
b = ""

for _ in range(Q):
    query = input()
    if query[0]=="1":
        t = 1
        rev_cnt +=1
        a,b = b[::-1],a[::-1]
    else:
        t,f,c = query.split()
        if f=="1":
            a = c+a
        elif f=="2":
            b = b+c
if rev_cnt%2==0:
    ans = a+S+b
else:
    ans = a+S[::-1]+b
print(ans)

### FIXED CODE ###

S= input()
Q = int(input())
rev_cnt = 0

a = "0"
a_rev_cnt = 0
b = "0"
b_rev_cnt = 0

for _ in range(Q):
    query = input()
    if query[0]=="1":
        t = 1
        rev_cnt  = -1*rev_cnt+1
        a,b = b,a

    else:
        t,f,c = query.split()
        if f=="1":
            if rev_cnt ==0:
                a = c+a
            else:
                a = a+c

        elif f=="2":
            if rev_cnt ==0:
                b = b+c
            else:
                b = c+b
if a[0]=="0":
    a = a[::-1]
a = a[:-1]
if b[-1]=="0":
    b = b[::-1]
b = b[1:]


if rev_cnt%2==0:
    ans = a+S+b
else:
    ans = a+S[::-1]+b
print(ans)
### BUGGY CODE ###

N,K=map(int,input().split())
S=list(map(int,input().split()))

# K=4
# S=(17, 13, 13, 12, 15, 20, 10, 13, 17, 11)
# K=3
# S=[1,2,2,4,5]

# K個連続した区間での最大が期待値の最大として、SでK個連続した最大値を探す。
maxSKsum=0
startSindex=0

#1.大きい区間を探す
for i in range(len(S)):  #ここがrange(len(S)-K)ではダメっぽいのがわからない・・・
    tempSKsum=sum(S[i:i+K])
    if maxSKsum<tempSKsum:
        maxSKsum=tempSKsum
        startSindex=i
    else:
        pass

#2.1で探した大きい区間の計算をする。
EVlist=[]
for s in range(K):
    tempEV=S[startSindex+s]*(S[startSindex+s]+1)/2/S[startSindex+s]
    EVlist.append(tempEV)

print(sum(EVlist))

### FIXED CODE ###

N,K=map(int,input().split())
_=input().split()
S=[]
for s in _:
    S.append((int(s)+1)/2) #初めから期待値で配列作成

maxsum=sum(S[0:K])
tempsum=maxsum

for i in range(N-K):
    tempsum=tempsum-S[i]+S[i+K]
    if maxsum<tempsum:
        maxsum=tempsum
    else:
        pass

print(maxsum)
### BUGGY CODE ###

from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions
from decimal import Decimal
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,k = inpl()
xary = []; yary = []
a = []
for i in range(n):
    x,y = inpl()
    xary += [x]; yary += [y]
    a += [[x,y]]
xary.sort(); yary.sort()
res = INF
for xi in range(n-1):
    for xj in range(xi+1,n):
        for yi in range(n-1):
            for yj in range(yi+1,n):
                cnt = 0
                l = xary[xi]; r = xary[xj]
                d = yary[yi]; u = yary[yj]
                # print(l,r,d,u)
                for i in range(n):
                    x,y = a[i]
                    if l<=x<=r and d<=y<=u:
                        cnt += 1
                if cnt >= k:
                    res = min(res, (r-l)*(u-d))
print(res)


### FIXED CODE ###

from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions
from decimal import Decimal
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,K = inpl()
a = [inpl() for _ in range(n)]
res = INF
for i in range(1):
    a.sort(key=lambda x:x[i])
    for j in range(n-1):
        for k in range(j+1,n):
            l = a[j][i]
            r = a[k][i]
            b = [a[j],a[k]]
            cnt = 2
            for _ in range(n):
                if _ == j or _ == k:
                    continue
                if l < a[_][i] < r:
                    cnt += 1
                    b += [a[_]]
            if cnt < K: continue
            b.sort(key=lambda x:x[i^1])
            # print(j,k,b,i)
            for _ in range(cnt-K+1):
                u = max(x[i^1] for x in b[_:_+K])
                d = min(x[i^1] for x in b[_:_+K])
                res = min(res, (r-l)*(u-d))
                # print(res)
print(res)
### BUGGY CODE ###

n,q=map(int,input().split())
s=input()
for i in range(q):
    l,r=map(int,input().split())
    print(s[l-1:r].count("AC"))
    



### FIXED CODE ###

N,Q=map(int,input().split())
S=input()
count=[0]*N
for i in range(1,N):
    count[i]+=count[i-1]
    if S[i-1:i+1]=="AC":count[i]+=1
for i in range(Q):
    l,r=map(int,input().split())
    print(count[r-1]-count[l-1])
### BUGGY CODE ###

def solve():
  N, M, K = map(int,input().split())
  AA = list(map(int,input().split()))
  BB = list(map(int,input().split()))
  A_max = 0
  AAs = [0]*N
  AAs[0] = AA[0]
  if K>=AAs[0]:
    A_max = 1
  for i in range(1,N):
    AAs[i] = AAs[i-1] + AA[i]
    if K>=AAs[i]:
      A_max+=1
  BBs = [0]*M
  BBs[0] = BB[0]
  for i in range(1,M):
    BBs[i] = BBs[i-1] + BB[i]
  Book_max = 0
  for i in range(A_max+1):
    b_max = i
    if i == 0:
      B_time = K
    else:
      B_time = K - AAs[i-1]
    for j in range(M):
      if B_time>=BBs[j]:
        b_max +=1
    if Book_max<b_max:
      Book_max = b_max
  print(Book_max) 
solve()

### FIXED CODE ###

def solve():
  N, M, K = map(int,input().split())
  AA = list(map(int,input().split()))
  BB = list(map(int,input().split()))
  A_max = 0
  AAs = [0]*N
  AAs[0] = AA[0]
  if K>=AAs[0]:
    A_max = 1
  for i in range(1,N):
    AAs[i] = AAs[i-1] + AA[i]
    if K>=AAs[i]:
      A_max+=1
  BBs = [0]*M
  BBs[0] = BB[0]
  BM = M
  for i in range(1,M):
    BBs[i] = BBs[i-1] + BB[i]
  Book_max = 0
  for i in range(A_max+1):
    b_max = 0
    if i == 0:
      B_time = K
    else:
      B_time = K - AAs[i-1]
    for j in range(BM-1,-1,-1):
      #print(i,j,A_max,b_max,B_time,BM)
      if B_time>=BBs[j]:
        b_max = j+1
        BM = b_max
        break
    if Book_max<b_max+i:
      Book_max = b_max+i
  print(Book_max) 
solve()
### BUGGY CODE ###

n,q=map(int,input().split())
s=input()
a=[]
for i in range(n):
    t=s[0:i+1]
    a.append(t.count("AC"))
for i in range(q):
    l,r=map(int,input().split())
    print(a[r-1]-a[l-1])

### FIXED CODE ###

n,q=map(int,input().split())
s=input()
a=[0]*(n+10)
for i in range(1,n):
    if s[i-1]=="A" and s[i]=="C":
        a[i]=a[i-1]+1
    else:
        a[i]=a[i-1]
for i in range(q):
    l,r=map(int,input().split())
    print(a[r-1]-a[l-1])
### BUGGY CODE ###

import sys


def resolve(in_):
    k = int(next(in_))
    if not k % 2:
        return -1

    seven = 7 % k

    for i in range(1, 1000000000):
        if not seven:
            return i
        i += 1
        seven = seven * 10 + 7
        seven %= k

    return -1

def main():
    answer = resolve(sys.stdin.buffer)
    print(answer)


if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys


def resolve(in_):
    k = int(next(in_))
    if not k % 2:
        return -1

    seven = 7 % k

    for i in range(1, 1000000):
        if not seven:
            return i
        i += 1
        seven = seven * 10 + 7
        if seven >= k:
            seven %= k

    return -1

def main():
    answer = resolve(sys.stdin.buffer)
    print(answer)


if __name__ == '__main__':
    main()
### BUGGY CODE ###

def main():
  n = int(input())
  As = map(int, input().split())
  Bs = map(int, input().split())

  diffs = [a - b for a, b in zip(As, Bs)]
  positives = list(sorted(filter(lambda x: x>0, diffs), reverse=True))
  negatives = list(sorted(filter(lambda x: x<0, diffs)))
  if abs(sum(negatives)) > sum(positives):
    print(-1)
    return
  max_i = -1
  for neg in negatives:
    for i, pos in enumerate(positives):
      neg += pos
      max_i = max(i, max_i)
      if neg >= 0:
        positives[i] = neg
        break
      else:
        positives[i] = 0
  print(len(negatives) + max_i + 1)


if __name__ == '__main__':
  main()


### FIXED CODE ###

from itertools import accumulate


def main():
  n = int(input())
  As = map(int, input().split())
  Bs = map(int, input().split())

  diffs = [a - b for a, b in zip(As, Bs)]
  pos_acc = list(accumulate(sorted(filter(lambda x: x>0, diffs), reverse=True)))
  negatives = list(filter(lambda x: x<0, diffs))
  neg_sum = abs(sum(negatives))
  num_negs = len(negatives)
  if num_negs == 0:
    print(0)
    return
  for i, pa in enumerate(pos_acc):
    if neg_sum <= pa:
      print(num_negs + i + 1)
      return
  print(-1)


if __name__ == '__main__':
  main()
### BUGGY CODE ###

N=int(input())
i,j=1,1
C=[(i,j)]
while True:
    p=C.pop(0)
    i,j=p[0],p[1]
    if p[0]*p[1]==N:
      print(p[0]+p[1]-2)
      break
    else:
      C.append((i,j+1))
      if i+1==j:
        C.append((i+1,j))

### FIXED CODE ###

N=int(input())
for i in range(int(N**0.5)+1,0,-1):
  if N%i==0:
    print(int(N/i+i-2))
    exit()
### BUGGY CODE ###

n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=True)
l = [list(map(int, input().split())) for i in range(m)]
l.sort(key=lambda x: x[1], reverse=True)
d = []
while l and len(d) <= n:
    bc = l.pop(0)
    d.extend([bc[1]]*bc[0])
ret = 0
for i in range(n):
    if d and d[0] > a[0]:
        ret += d.pop(0)
    else:
        ret += a.pop(0)
print(ret)

### FIXED CODE ###

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

res = 0
pos_1 = 0
pos_2 = 0
for _ in range(n):
    if pos_2 == m or a[pos_1] > l[pos_2][1]:
        res += a[pos_1]
        pos_1 += 1
    else:
        res += l[pos_2][1]
        l[pos_2][0] -= 1
        if l[pos_2][0] == 0:
            pos_2 += 1
 
print(res)

# d = []
# while l and len(d) <= n:
#     bc = l.pop(0)
#     d.extend([bc[1]]*bc[0])
# ret = 0
# for i in range(n):
#     if d and d[0] > a[0]:
#         ret += d.pop(0)
#     else:
#         ret += a.pop(0)
# print(ret)
### BUGGY CODE ###

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

for b, c in BC:
    A = sorted(A)
    if A[0] >= c:
        break
    for i in range(b):
        if A[i] < c:
            A[i] = c
        else:
            break
print(sum(A))

### FIXED CODE ###

N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = []
for _ in range(M):
    bc = list(map(int, input().split()))
    BC.append(bc)
BC = sorted(BC, key=lambda x:x[1], reverse=True)
cnt = 0
for b, c in BC:
    A += [c] * b
    cnt += b
    if cnt >= N:
        break
A.sort(reverse=True)
print(sum(A[:N]))
### BUGGY CODE ###

N, M = [int(nm) for nm in input().split()]
A = [int(a) for a in input().split()]

for m in range(M):
  mx_A = max(A)
  A.remove(mx_A)
  A.append(mx_A // 2)

ans = sum(A)
print(ans)

### FIXED CODE ###

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

while M >= 1:
  A[0] //= 2
  M -= 1
  
  i = 1
  while True:
    if N >= i*2 + 1:
      A1, A2, A3 = A[i-1], A[i*2 - 1], A[i*2]
    elif N == i*2:
      A1, A2, A3 = A[i-1], A[i*2 - 1], 0
    else:
      A1, A2, A3 = A[i-1], 0, 0

    if A1 < A2 or A1 < A3:
      if A2 >= A3:
        A[i-1], A[i*2 - 1] = A2, A1
        i = i*2
      else:
        A[i-1], A[i*2] = A3, A1
        i = i*2 + 1
    else:
      break

ans = sum(A)
print(ans)
### BUGGY CODE ###

import random
from fractions import gcd
lim=10**9+7
def lcm(a):
    x = a[0]
    for i in range(1, len(a)):
        x = (x * a[i]) // gcd(x, a[i])
    return int(x)
n=int(input())
#A=random.sample(list(range(1, 10**6)), k=10**4)
A=[int(i) for i in input().split()]
Z=lcm(A)
ans=0
for a in A:
    ans =(ans+Z//a) % lim
print(ans)

### FIXED CODE ###

from fractions import gcd
lim=10**9+7
def lcm(a):
    x = a[0]
    for i in range(1, len(a)):
        x = (x * a[i]) // gcd(x, a[i])
    return int(x)


n=int(input())
A=[int(i) for i in input().split()]
A_inv = [pow(a,lim-2,lim) for a in A]

Z = A[0]
k=1
for i in range(1, n):
    Z = Z // gcd(Z, A[i]) * A[i]
ans=0

Z=Z % lim
for i in range(n):
    ans = ( ans + Z*A_inv[i] ) % lim
print(ans)