peterverebics/CodeNet_Python_428 huggingface dataset:
# Judge status: Accepted
import itertools

n = int(input())
ps = tuple(map(int, input().split()))
qs = tuple(map(int, input().split()))

num_list = list(itertools.permutations([i for i in range(1, n+1)]))

print(abs(num_list.index(ps) - num_list.index(qs)))
# Judge status: Accepted
n = int(input())
alp = [chr(i) for i in range(97, 97+26)]
 
def update(a, m):
    a_new = []
    m_new = []
    for val_a, val_m in zip(a,m):
        for i in range(val_m + 2):
            a_new += [val_a + alp[i]]
            m_new += [max(val_m, i)]
    return a_new, m_new
 
a = ['a']
m = [0]
 
for i in range(n-1):
  a, m = update(a,m)
 
for v in a:
  print(v)
# Judge status: Accepted
N,M=[int(x) for x in input().split()]
print(min(abs(N-N//M*M),abs(N-(N//M+1)*M)))
# Judge status: Accepted
from itertools import combinations

N = int(input())
L = list(map(int, input().split()))
if len(L) < 3:
    print(0)
    exit()
cnt = 0
for edges in combinations(L, 3):
    if len(set(edges)) == 3:
        edges = list(edges)
        edges.sort()
        a, b, c = edges
        if c < a + b:
            cnt += 1
print(cnt)
# Judge status: Wrong Answer
n,m = map(int, input().split())

k = n*4

r = k-m

if(r == 0):
    print("Yes")
else:
    if(r >= 0):
        if(r/2):
            print("Yes")
        else:
            print("No")
    else:
        print("No")
# Judge status: Accepted
s,w = map(int,input().split())

if s > w:
    print("safe")
else:
    print("unsafe")
# Judge status: Accepted
N,K = map(int,input().split())
p = list(map(int,input().split()))
summ = sum(p[i] for i in range(K))
sum_list = [summ]
maximum = 0

for i in range(N - K):
    summ += p[i + K] - p[i]
    sum_list.append(summ)

maximum = max(sum_list)

print("{}".format((maximum + K) / 2))
# Judge status: Accepted
def main():
    import sys
    input = sys.stdin.readline
    s = list(input().strip())
    K = int(input())
    for i, c in enumerate(s):
        if c == 'a':
            continue
        z = 123 - ord(c)
        if K >= z:
            s[i] = 'a'
            K -= z
        if K == 0:
            break
    
    s[-1] = chr(((ord(s[-1])-97+K))%26+97)

    print(''.join(s))

if __name__ == '__main__':
     main()
# Judge status: Accepted
from collections import Counter


N = int(input())
C = Counter()

for a in map(int, input().split()):
    C[a] += 1
    C[a - 1] += 1
    C[a + 1] += 1

print(max(C.values()))
# Judge status: Accepted
n = int(input())
l = list(map(int, input().split()))
money = 1000
for i in range(n-1) :
	if l[i] < l[i+1] :
		t = money // l[i]
		money += t*(l[i+1]-l[i])
print(money)
# Judge status: Accepted
li = input().split()
print("A"+li[1][0].upper()+"C")
# Judge status: Wrong Answer
from functools import reduce
from fractions import gcd
n=int(input())
s=list(map(int,input().split()))
b = reduce(gcd, s)

if len(s)==2:
  print(max(s))
  exit()

ma=1
for i in range(len(s)-1):
  t=s[i]
  s[i]=s[i+1]
  c=reduce(gcd,s)
  ma=max(ma,c)
  s[i]=t
else:
  t=s[i]
  s[i]=s[i-1]
  c=reduce(gcd,s)
  ma=max(ma,c)
  s[i]=t
print(ma)
# Judge status: Wrong Answer
N = int(input())
W=[]
ss=True
for i in range (N):
  W.append(input())



for t in range (N-1):
    if W[t-1][-1]==W[t][0]:
      ss=True
    else:
      ss=False 
     
      
W=dict.fromkeys(W)
if len(W)!=N:   
  ss=False
     
if ss==False:
  print("No")
else:
  print("Yes")
# Judge status: Accepted
N = int(input())
print(0 if N % 1000 == 0 else 1000 - N % 1000)
# Judge status: Wrong Answer
N,M = map(int,input().split())
L = 1
R = N

for i in range(M):
    l,r = map(int,input().split())
    if L <= l:
        L = l
    if r <= R:
        R = r

print(R-L+1)
# Judge status: Accepted
n,k = map(int,input().split())
a_L = list(map(int,input().split()))

ans = 1
n = n-k

while True:
    if n<=0:
        print(ans)
        exit()
    n = n-k+1
    ans += 1
# Judge status: Accepted
n,m=map(int,input().split())
a=[input() for i in range(n)]
b=[input() for _ in range(m)]

for i in range(n-m+1):
    for j in range(n-m+1):
        flag=True
        for k in range(m):
            for l in range(m):
                if a[i+k][j+l]!=b[k][l]:
                    flag=False
        if flag:
            print("Yes")
            exit()
print("No")
# Judge status: Wrong Answer
a = map(int, input().split())
if sum(a) <= 21:
  print('win')
else:
  print('burst')
# Judge status: Wrong Answer
import numpy

N = int(input())
A = [int(x) for x in input().split()]

B = numpy.prod(A)

if B >= 10**8:
    print(-1)
else:
    print(B)
# Judge status: Accepted
n, k = map(int, input().split())
a_i = list(map(int, input().split()))
for x in range(n - k):
    if a_i[x] < a_i[x + k]: print("Yes")
    else: print("No")
# Judge status: Accepted
n = int(input())
s = input()
r = 0
l = 0
cnt = 0
for i in s:
    if i==')':
        cnt+=1
        if cnt>0:
            cnt-=1
            l+=1
    else:
        cnt-=1
print(l*'('+s+')'*(-cnt))
# Judge status: Wrong Answer
n,m=map(int,input().split())
l=list(map(int,input().split()))
suml=sum(l)
##print(suml)



temp=4*m
temp=suml/temp

flag=False

for i in range(n):
    if m==0:
        flag=True
        break
    else:
        if l[i]>temp:
##            print(l[i],temp)
            m-=1

if flag:
    print("Yes ")
else:
    print("No ")
# Judge status: Accepted
def main():
   s = list(input())
   s[:4] = ['2','0','1','8']
   print(''.join(s))
main()
# Judge status: Wrong Answer
import bisect

N = int(input())
L = list(map(int, input().split()))
s = 0

L = sorted(L)

for c in reversed(range(2, N)):
	for b in reversed(range(1, c)):
		a = bisect.bisect_left(L, L[c]-L[b])
		if a<b:
			s += b-a
print(s)
# Judge status: Accepted
a, b = map(int, input().split())

h_gnd = -b
for i in range(b - a):
  h_gnd += b - a - i
  
print(h_gnd)
# Judge status: Accepted
A,B,C = map(int,input().split())
count = 0
if A == B:
    count += 1
if A == C:
    count += 1
if B == C:
    count += 1
if count == 1:
    print("Yes")
else:
    print("No")
# Judge status: Accepted
n,t=int(input()),10**9
print("0 0 1 "+str(t)+" "+str((n-1)//t+1)+" "+str(t-1-(n-1)%t))
# Judge status: Accepted
n = int(input())
items = {}

for i in range(n):
    item = input()
    items[item] = True

print(len(items))
# Judge status: Wrong Answer
X,A,B = map(int,input().split())
if A-B > 0:
    print("delicious")
elif A-B > -X:
    print("safe")
else:
    print("dangerous")
# Judge status: Wrong Answer
#C - Rectangle Cutting
W,H,x,y = map(int,input().split())
def main(W,H,x,y):
    if W == x or H == y or x==0 or y==0:
        ans1 = W*H
        ans2 = 0
    else:
        tmp1 = min((W-x)*H,x*H)
        tmp2 = min(W*(H-y),W*y)
        ans1 = max(tmp1,tmp2)
        if tmp1 == tmp2:
            ans2 = 1
        else:
            ans2 = 0
    print(ans1,ans2)
main(W,H,x,y)
# Judge status: Wrong Answer
#!/usr/bin/env python3
import sys

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

M = 1000
kabu = 0

cur = A[0]

for i in range(1,N):
    if cur > A[i]:
        # 持ってる株全部売る
        M = M + kabu * cur
        kabu = 0
    if cur < A[i]:
        # 所持金全部株に変える
        kabu = M // cur
        M = M % cur
    cur = A[i]

M = M + kabu * A[N-1]

print(M)
# Judge status: Accepted
n = int(input())
a = list(map(int,input().split()))[::-1]
b = list(map(int,input().split()))[::-1]

ans = 0
for i in range(n):
  t=min(b[i],a[i])
  ans+=t
  a[i]-=t
  b[i]-=t
  t=min(b[i],a[i+1])
  ans+=t
  a[i+1]-=t
  
print(ans)
# Judge status: Accepted
n = int(input())
num_list = list(map(int, input().split()))
ans = 1
cnt_list = [0] * n
a = int()

for i in range(1,n):
    if num_list[i-1] > num_list[i]:
        cnt_list[i] = -1
    if num_list[i-1] < num_list[i]:
        cnt_list[i] = 1

while 1 in cnt_list and -1 in cnt_list:
    del cnt_list[:max(cnt_list.index(1),cnt_list.index(-1))]
    cnt_list[0] = 0
    ans += 1

print(ans)
# Judge status: Wrong Answer
def divisors(n):
    s = set()
    for x in range(2, int(n ** 0.5) + 1):
        while n % x == 0:
            s.add(x)
            n /= x
            if n == 1:
                break
    return s

a, b = map(int, input().split())
d = divisors(a) & divisors(b)
print(len(d) + 1)
# Judge status: Accepted
import bisect
n=int(input())
a=sorted(list(map(int,input().split())))
b=sorted(list(map(int,input().split())))
c=sorted(list(map(int,input().split())))
ans=0
for i in b:
    A=bisect.bisect_left(a,i)
    B=len(b)-bisect.bisect_right(c,i)
    ans+=A*B
print(ans)
# Judge status: Accepted
N, K = map(int, input().split())
A = [int(i) for i in input().split()]
l = 0
r = 10 ** 9 + 1
while l + 1 < r:
    p = (l + r) // 2
    n = 0
    ok = False
    for v in A:
        n += (v // p - 1)
        if not v % p == 0:
            n += 1
        if n > K:
            break
    else:
        ok = True
    if not ok:
        l = p
    else:
        r = p

print(r)
# Judge status: Accepted
n = int(input())
l = [int(input()) for _ in range(5)]

if min(l) >= n:
    print(5)
else:
    print((n-1)//min(l) + 5)
# Judge status: Accepted
a, b = map(int, input().split())

if a >= 13:
    print(b)
elif 6<=a<=12:
    print(b//2)
else:
    print(0)
# Judge status: Accepted
import numpy as np

N, W = map(int, input().split(' '))
items = [tuple(map(int, input().split(' '))) for _ in range(N)]
dp = np.zeros(shape=W + 1, dtype=np.int64)

for weight, value in items:
    dp[weight:] = np.maximum(dp[weight:], dp[:-weight] + value)

print(dp[-1])
# Judge status: Wrong Answer
list = str(input())

if list is "AAA" or list is "BBB" :
    print("No")
else : 
    print("Yes")
# Judge status: Accepted
S,T = input().split()


ans = T + S

print(ans)
# Judge status: Accepted
import math
import sys
from collections import deque
import heapq
import copy
import itertools
from itertools import permutations
from itertools import combinations
import bisect
def mi() : return map(int,sys.stdin.readline().split())
def ii() : return int(sys.stdin.readline().rstrip())
def i() : return sys.stdin.readline().rstrip()
a,b=mi()
while a!=0:
  c=a
  a=b%a
  b=c
ans=1
for i in range(2,int(math.sqrt(b)+2)):
  if b%i==0:
    ans+=1
    while b%i==0:
      b=int(b/i)
if b!=1:
    ans+=1
print(ans)
# Judge status: Accepted
def main():
    N = int(input())
    ans = [0 for _ in range(10005)]

    for i in range(1,100):
        for j in range(1,100):
            for k in range(1,100):
                v = i**2 + j**2 + k**2 + i*j + j*k + k*i
                if v < 10005:
                    ans[v]+=1
    for i in range(1,N+1):
        print(ans[i])

if __name__ == "__main__":
    main()
# Judge status: Accepted
N=int(input())
ans=N*(N-1)//2
print(int(ans))
# Judge status: Accepted
N = int(input())

def exists():
    for seven in range(N):
        M = N - seven * 7
        if 0 <= M and M % 4 == 0:
            return True
    return False

print('Yes' if exists() else 'No')
# Judge status: Wrong Answer
a, b, c= map(int, input().split())
d=(a,b,c)
e=len(d)
print(e)
# Judge status: Accepted
n, k = map(int, input().split())
l = list(map(int, input().split()))

ptns = []
for i in range(n-k+1):
    ptns.append(abs(0 -l[i]) + abs(l[i] - l[i+k-1]))
    ptns.append(abs(0 -l[i+k-1]) + abs(l[i+k-1] - l[i]))

print(min(ptns))
# Judge status: Accepted
a,b = map(int,input().split())

if a * b <= 0:
    print("Zero")
    exit()
if a > 0:
    print("Positive")
    exit()
if (b - a) % 2 == 0:
    print("Negative")
    exit()
else:
    print("Positive")
# Judge status: Accepted
def make_divisors(n):
    div=[]
    for i in range(1,-int(-n**0.5//1)):
        if n%i==0:
            div.append(i)
            div.append(n//i)
    if n%(n**0.5)==0:
        div.append(int(n**0.5))
    return div

n=int(input())
ans=[]
for i in range(1,n+1,2):
    if len(make_divisors(i))==8:
        ans.append(i)
print(len(ans))
# Judge status: Accepted
N = int(input())
a = sorted(list(map(int, input().split())))
b = sorted(list(map(int, input().split())))
c = sorted(list(map(int, input().split())))

a_b = 0
b_c = 0
count = 0
for b_item in b:
    while a_b < N and a[a_b] < b_item:
        a_b += 1
    while b_c < N and c[b_c] <= b_item:
        b_c += 1
    count += (a_b * (N - b_c))

print(count)
# Judge status: Accepted
from collections import Counter
n = int(input())
s = Counter(input())
for i in range(n-1):
  s &= Counter(input())
print("".join(sorted(s.elements())))
# Judge status: Accepted
D = int(input())
c = [int(_) for _ in input().split()]
s = [[int(_) for _ in input().split()] for _ in range(D)]

sat = 0
last = [0] * 26
for d in range(D):
    v = -1000000
    for x in range(26):
        if v < (d + 1 - last[x]) * c[x]:
            j = x
            v = (d + 1 - last[x]) * c[x]
    last[j] = d + 1
    print(j+1)

    for i in range(26):
        sat -= (d + 1 - last[i]) * c[i]
    sat += s[d][j]
    # print(sat)
# Judge status: Accepted
a = int(input())
b = int(input())
c = int(input())
x = int(input())

s = 0

for i in range(a+1):
    for j in range(b+1):
        for k in range(c+1):
            if 500 * i + 100 * j + 50 * k == x:
                s += 1
    
print(s)
# Judge status: Accepted
s = input()
print(s.replace('7','8',1))
# Judge status: Wrong Answer
#!/usr/bin/env python3
import collections
import itertools as it
import math
#import numpy as np
 
#  = input()
#  = int(input())
a, b, x  = map(int, input().split())
#  = list(map(int, input().split()))
#  = [int(input()) for i in range(N)]
#
# c = collections.Counter()

ans = (b - a) // x
ans += 1 if a % x == 0 else 0
#ans += 1 if b % x == 0 else 0
print(ans)
# Judge status: Wrong Answer
c = input()
if c == "a" or c == "e" or c == "i" or c == "o" or c == "u":
    print("vowel")
else:
    print("constant")
# Judge status: Wrong Answer
a = input().split()
b = 0
for i in range(len(a)):
    a.sort(reverse=True)
    b = b + int(a[0])
    if int(a[0]) <= 0:
        continue
    else:
        a[0] = str(int(a[0]) - 1)
print(b)
# Judge status: Wrong Answer
N=int(input())

if N == 1:
  print(1)
elif N == 2:
  print(1)
else:
  if N % 2 == 1:
    print ((N / 2) + 1)
  else:
    print (N / 2)
# Judge status: Accepted
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

x = ii()
y = 100

for i in range(1,10**18):
    y = int(y * 1.01)
    if y >= x:
        print(i)
        break
# Judge status: Accepted
N = input()
a = 0
for i in N:
    a += int(i)
print("Yes" if a%9==0 else "No")
# Judge status: Accepted
import numpy as np
N,K = (int(x) for x in input().split())
p1 = np.array(list(map(int, input().split())))
s1 = p1.cumsum()
s2 = np.append(0, s1)
p2 = np.array([(s2[i+K] - s2[i]) for i in range(N-K+1)])
max = np.amax(p2)
print((max+K) / 2)
# Judge status: Accepted
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

k,n = map(int,input().split())
A = list(map(int,input().split()))
mans = k + A[0] - A[-1]
for i in range(n-1):
  mans = max(mans,A[i+1]-A[i])
print(k-mans)
# Judge status: Accepted
o = input()
e = input()

ans = ''
for i in range(len(e)):
    ans += o[i]+e[i]

if len(o) != len(e):
    ans += o[len(o)-1]

print(ans)
# Judge status: Accepted
from collections import Counter
def solve():
  N = int(input())
  S = [input() for _ in range(N)]
  c = Counter(S)
  M = max(c.values())
  ans = []
  for k,v in c.items():
    if v==M:
      ans.append(k)
  ans.sort()
  return ans
print(*solve(),sep='\n')
# Judge status: Accepted
day = input()

days = {'SUN':0, 'MON':1, 'TUE':2, 'WED':3, 'THU':4, 'FRI': 5,'SAT':6}

print(7 - days[day])
# Judge status: Accepted
import bisect,collections,copy,heapq,itertools,math,string
from collections import defaultdict as D
from functools import reduce
import numpy as np
from numba import njit
import sys
sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

N,D = LI()
_XY = [LI() for _ in range(N)]

XY = np.array(_XY)
#C = np.zeros(N + 1)
# print(XY)

# print(np.linalg.norm(XY[1]-XY[0]))

count = 0
for XiYi in XY:
    if (XiYi[0]**2 + XiYi[1]**2) <= D**2:
        count += 1
print(count)
# Judge status: Accepted
s=input()
n = len(s)

ss =s[::-1]

sss =s[1:((n-1)//2)-1]
mmm = sss[::-1]

ssss = s[((n+3)//2)-1:n]
mmmm = ssss[::-1]


if s == ss and sss == mmm and ssss == mmmm:
    print('Yes')
else:
    print('No')
# Judge status: Wrong Answer
# -*- coding: utf-8 -*-
import itertools

# 棒の本数
n = int(input())

# 棒のリスト
lods = list(map(int, input().split()))

# 3本なければ三角形は作れない
if (n<3):
  print(0)

# 確認
count = 0
for data in itertools.combinations(lods, 3):
  if (data[0] != data[1]
      and data[1] != data[2]
      and data[2] != data[0]
      and (data[0]+data[1]) > data[2]
      and (data[1]+data[2]) > data[0]
      and (data[2]+data[0]) > data[1]):
    count += 1

print(count)
# Judge status: Wrong Answer
import numpy as np

N = int(input())

AB = []

for n in range(N):
  AB.append(list(map(int, input().split())))

index = [AB[i][0] for i in range(len(AB))]
index = np.argsort(index)
AB = list(np.array(AB)[index])

if len(AB)%2==0:
  A = AB[len(AB)//2-1]
  B = AB[len(AB)//2]
  middle = max(B) - min(A)
  print(middle+1)
else:
  C = AB[len(AB)//2]
  print(C[-1]-C[0])
# Judge status: Wrong Answer
from collections import Counter
n = int(input())
s = list(input())
c = Counter(s)
ans = 1
for i in c.values():
    ans *= (i+1)
print(ans-1)
# Judge status: Accepted
a = []
for i in range(3):
    a.extend(list(map(int,input().split())))
n = int(input())
b = []

cnt=[]
pattern=[[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]]

for i in range(n):
    b.append(int(input()))
    for j , x in enumerate(a):
        if x == b[i]:
            cnt.append(j)

for q,w,e in pattern:
    if q in cnt and w in cnt and e in cnt:
        print('Yes')
        break
else:
    print('No')
# Judge status: Accepted
N,D = map(int,input().split())
X = []
for i in range(N):
  x = list(map(int,input().split()))
  X.append(x)
Count = 0
for i in range(N-1):
  for j in range(i+1,N):
    k = 0
    for t in range(D):
      k += (X[i][t]-X[j][t])**2
    k = k**(1/2)
    if k == int(k):
      Count += 1
print(Count)
# Judge status: Accepted

def get_char(x):
    return chr(ord('a') + x)


def submit():
    n = int(input())
    n -= 1

    ans = []
    while True:
        r, q = divmod(n, 26)
        c = get_char(q)
        ans.append(c)
        if r == 0:
            break
        n -= q
        n //= 26
        n -= 1
    
    print("".join(reversed(ans)))
    

submit()
# Judge status: Accepted
n,m=map(int,input().split())
ab=[0]*n
for i in range(m):
    a,b=map(int,input().split())
    ab[a-1]+=1
    ab[b-1]+=1

for i in ab:
    print(i)
# Judge status: Wrong Answer
s = input()

flg = 0

for i in range(len(s)):
    if i % 2 == 0 and (s[i] != "R" and s[i] != "U" and s[i] != "D"):
        flg = 1
    elif i % 2 == 1 and (s[i] != "L" and s[i] != "U" and s[i] != "D"):
        flg = 1
    else:
        continue

if flg == 0:
    print("YES")
else:
    print("NO")
# Judge status: Accepted
#n = int(input())
#a, b = map(int,input().split())
#l = list(map(int,input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
s = input()
n = len(s)


def isrepeat(s):
    res = False
    n = len(s)
    if s[0:n//2]+s[0:n//2] == s:
        res = True
    return res


for delnum in range(1, n):
    if (n-delnum) % 2 == 0:
        if isrepeat(s[0:n-delnum]):
            ans = n-delnum
            break
print(ans)
# Judge status: Wrong Answer
S = input()

if len(S) != len(set(S)):
    print('yes')
else:
    print('no')
# Judge status: Accepted
print("ABC" if int(input()) < 1000 else "ABD")
# Judge status: Wrong Answer

n = int(input())
x = [-1]*n
for i in range(n):
    x[i] = list(map(int, input().split()))

ans = [[0]*3 for i in range(n)]
ans[0] = x[0]

for k in range(1,n):
    for j in range(3):
        ans[k][j] = max(ans[k-1][(j+1)%3], ans[k-1][(j+2)%3]) + x[k][j]
    print(ans[i])

print(max(ans[n-1]))
# Judge status: Accepted
import bisect

n = int(input())
a = []

for i in range(n):
    x = int(input())
    a.append(x)
a = a[::-1]

stack = [a[0]]

for i in range(1, n):
    index = bisect.bisect_right(stack, a[i])
    if index == len(stack):
        stack.append(a[i])
    else:
        stack[index] = a[i]

print(len(stack))
# Judge status: Accepted
N=int(input())
A=[0]*N
for i in range(N):
    A[i]=int(input())
A.append(0)

K=0
S=0
for a in A:
    if a:
        S+=a
    else:
        K+=S//2
        S=0

print(K)
# Judge status: Wrong Answer
n = int(input())
a = list(map(int, input().split()))
a.sort()
l = [x//400 if x//400 <= 7 else 8 for x in a]
p = [0]*8
for i in l:
    if i < 8:
        p[i] = 1
pmin = sum(p) if sum(p) != 0 else 1
pmax = sum(p) + l.count(8)
print(l)
print(p)
print(pmin, pmax)
# Judge status: Accepted
import sys
input = sys.stdin.buffer.readline

def main():
    N = int(input())
    use = N//11
    rest = N%11
    if rest == 0:
        print(2*use)
    elif rest <= 6:
        print(2*use+1)
    else:
        print(2*use+2)

if __name__ == "__main__":
    main()
# Judge status: Accepted
#5618213
n,m = map(int,input().split())
a = list(map(int,input().split()))
bc = [list(map(int,input().split())) for i 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)
print(sum(a[:n]))
# Judge status: Accepted
n, k = map(int, input().split())

if n < k:
  print(1)
elif n % k == 0:
  print(0)
else:
  print(1)
# Judge status: Accepted
N,Y = map(int,input().split())

for i in range(N+1):
  for j in range(N+1):
    a = i
    b = j
    c = N-i-j
    if c < 0:
      continue
    else:
      price = a*1000+b*5000+c*10000
      if price == Y:
        print(c,b,a)
        exit()
print(-1,-1,-1)
# Judge status: Accepted
n, m = map(int, input().split())
a = [int(_) for _ in input().split()]
aa = [0] * n

for i in range(n):
    aa[i] = a[i] * (4*m)

lim = sum(a)
ans = 0
for i in range(n):
    if aa[i] >= lim:
        ans += 1
    else:
        pass
if m <= ans:
    print('Yes')
else:
    print('No')
# Judge status: Accepted
n = int(input())
arr = list(map(int,input().split()))
ans = 0
for i in range(0,len(arr)+(n%2),2):
    if arr[i] % 2 != 0:
        ans += 1
print(ans)
# Judge status: Accepted
import sys

K = int(input())
A,B = map(int,input().split())

tmp = A//K

for i in range(1000):
    t = (tmp+i) * K
    if A<= t <= B:
        print('OK')
        sys.exit()
    if t > B:
        break
print('NG')
# Judge status: Accepted
K = int(input())
S = input()

if len(S) <= K:
    print(S)
else:
    S = list(S)
    for i in range(K):
        print(S[i], end='')
    print('...')
# Judge status: Wrong Answer
a, b, c = map(int, input().split())

if a + b + c <= 22:
  print('bust')
else:
  print('win')
# Judge status: Accepted
import fractions
a,b = map(int,input().split())

gcd = fractions.gcd(a,b)

def prime_factorize(n):
    result = []
    for i in range(2,int(n**0.5)+1):
        while n % i == 0:
            n //= i
            result.append(i)
    if n != 1:
        result.append(n)
    return result

ans = len(set(prime_factorize(gcd)))

print(ans+1)
# Judge status: Accepted
s = input()
r = True
if len(s) % 2 == 0:
  for i in range(0, len(s)-1, 2):
    if s[i:i+2] != "hi":
      r = False
  print("Yes" if r else "No")
else :
  print("No")
# Judge status: Accepted
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
ans = 0
for i in range(N):
  one = min(A[i],B[i])
  two = min(A[i+1],B[i]-one)
  ans+=one+two
  A[i+1]-=two
print(ans)
# Judge status: Accepted
def main():
    N = int(input())
    A = [int(x) for x in input().split()]
    B = [int(x) for x in input().split()]
    C = [int(x) for x in input().split()]
    ans = sum(B)
    k = -1
    for i in A:
        if i == k + 1:
            ans += C[k-1]
        k = i
    print(ans)


if __name__ == "__main__":
    main()
# Judge status: Accepted
k,a,b=map(int,input().split())
if a+1<b:
    if a-1+2>k:
        print(1+k)
    else:
        num1=a-1+2
        num2=b-a
        print(b+((k-num1)//2)*num2+(k-num1)%2)
else:
    print(1+k)