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

H,W=list(map(int,input().split()))
l=[list(input()) for i in range(H)]
l_count=[[0]*W for i in range(H)]
from collections import deque
def answer(st_X,st_Y):#X,Y座標を指定してそこからの距離を考える
   tmp_X,tmp_Y=st_X,st_Y
   que=deque([[tmp_X,tmp_Y,0]])
   after=deque([])
   while que:#スタート地点からの距離を数えながら行ったことのない場所を探索する
      tmp_X,tmp_Y,nu=que.popleft()
      after.append([tmp_X,tmp_Y])
      if tmp_X>0 and l[tmp_Y][tmp_X-1]=="." and [tmp_X-1,tmp_Y] not in after:
         l_count[tmp_Y][tmp_X-1]=nu+1
         que.append([tmp_X-1,tmp_Y,nu+1])
      if tmp_Y>0 and l[tmp_Y-1][tmp_X]=="." and [tmp_X,tmp_Y-1] not in after:
         l_count[tmp_Y-1][tmp_X]=nu+1
         que.append([tmp_X,tmp_Y-1,nu+1])
      if tmp_X<W-1 and l[tmp_Y][tmp_X+1]=="." and [tmp_X+1,tmp_Y] not in after:
         l_count[tmp_Y][tmp_X+1]=nu+1
         que.append([tmp_X+1,tmp_Y,nu+1])
      if tmp_Y<H-1 and l[tmp_Y+1][tmp_X]=="." and [tmp_X,tmp_Y+1] not in after:
         l_count[tmp_Y+1][tmp_X]=nu+1
         que.append([tmp_X,tmp_Y+1,nu+1])
ans=0
for i in range(H):
   for j in range(W):
      if l[i][j]==".":
         l_count=[[0]*W for i in range(H)]
         answer(j,i)
         tmp=max(sum(l_count,[]))
         ans=max(ans,tmp)
print(ans)

### FIXED CODE ###

H,W=list(map(int,input().split()))
l=[list(input()) for i in range(H)]
l_count=[[0]*W for i in range(H)]
from collections import deque
def answer(st_X,st_Y):#X,Y座標を指定してそこからの距離を考える
   tmp_X,tmp_Y=st_X,st_Y
   que=deque([[tmp_X,tmp_Y]])
   after=set()
   while que:#スタート地点からの距離を数えながら行ったことのない場所を探索する
      tmp_X,tmp_Y=que.popleft()
      nu=l_count[tmp_Y][tmp_X]
      after.add((tmp_X,tmp_Y))
      if tmp_X>0 and l[tmp_Y][tmp_X-1]=="." and (tmp_X-1,tmp_Y) not in after and [tmp_X-1,tmp_Y] not in que:
         l_count[tmp_Y][tmp_X-1]=nu+1
         que.append([tmp_X-1,tmp_Y])
      if tmp_Y>0 and l[tmp_Y-1][tmp_X]=="." and (tmp_X,tmp_Y-1) not in after and [tmp_X,tmp_Y-1] not in que:
         l_count[tmp_Y-1][tmp_X]=nu+1
         que.append([tmp_X,tmp_Y-1])
      if tmp_X<W-1 and l[tmp_Y][tmp_X+1]=="." and (tmp_X+1,tmp_Y) not in after and [tmp_X+1,tmp_Y] not in que:
         l_count[tmp_Y][tmp_X+1]=nu+1
         que.append([tmp_X+1,tmp_Y])
      if tmp_Y<H-1 and l[tmp_Y+1][tmp_X]=="." and (tmp_X,tmp_Y+1) not in after and [tmp_X,tmp_Y+1] not in que:
         l_count[tmp_Y+1][tmp_X]=nu+1
         que.append([tmp_X,tmp_Y+1])
ans=0
for i in range(H):
   for j in range(W):
      if l[i][j]==".":
         l_count=[[0]*W for i in range(H)]
         answer(j,i)
         tmp=max(sum(l_count,[]))
         ans=max(ans,tmp)
print(ans)
### BUGGY CODE ###

n=int(input())

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

day=0
zan=[n-1]*n
zann=sum(zan)

while day<10**6:
  day+=1
  kouho=list(range(n))
  sumi=set()
  
  for i in range(n):
    try:
      if A[A[i][-1]-1][-1]-1==i:
        sumi.add(i)
        sumi.add(A[i][-1]-1)
    except:
      pass

  for k in sumi:
    A[k].pop()
    zan[k]-=1

  if zann==sum(zan):
    print(-1)
    exit(0)
  
  zann=sum(zan)
  
  if zann==0:
    print(day)
    exit(0)
    
print(-1)

### FIXED CODE ###

n=int(input())

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

day=0
zan=[n-1]*n
zann=sum(zan)
sumi=set(range(n))

while True:
  day+=1

  kouho=list(sumi)
  sumi=set()
  
  for i in kouho:
    try:
      if A[A[i][-1]-1][-1]-1==i:
        sumi.add(i)
        sumi.add(A[i][-1]-1)
    except:
      pass

  for k in sumi:
    A[k].pop()
    zan[k]-=1

  if zann==sum(zan):
    print(-1)
    exit(0)
  
  zann=sum(zan)
  
  if zann==0:
    print(day)
    exit(0)
### BUGGY CODE ###

Input = map(int,raw_input().split(' '))
N = Input[0]
K = Input[1]
A = map(int,raw_input().split(' '))
A = sorted(A)
count = []
i = 0
while i < len(A):
	count.append(A.count(A[i]))
	i += A.count(A[i])
count = sorted(count)
dim = len(count)
rslt = 0
if dim <= K:
	print rslt
else:
	rslt += sum(count[:dim-K])
	print rslt

### FIXED CODE ###

import bisect
Input = map(int,raw_input().split(' '))
N = Input[0]
K = Input[1]
A = map(int,raw_input().split(' '))
A = sorted(A)
count = []
i = 0
while i < len(A):
	count.append(bisect.bisect_right(A,A[i]) - bisect.bisect_left(A,A[i]))
	i = bisect.bisect_right(A,A[i])
count = sorted(count)
dim = len(count)
rslt = 0
if dim <= K:
	print rslt
else:
	rslt += sum(count[:dim-K])
	print rslt
### BUGGY CODE ###

n=int(input())
s=str(input())
ans=[]
for i in range(n):
    cnt=0
    cnt+=s[:i].count("W")
    cnt+=s[i+1:].count("E")
    ans.append(cnt)

print(min(ans))    
    

### FIXED CODE ###

n=int(input())
s=str(input())
ans=[]
e=s.count("E")
w=0
for i in range(n):
    if s[i]=="W":
        ans.append(w+e)
        w+=1    
    else:
        e-=1 
        ans.append(w+e)
print(min(ans))
### BUGGY CODE ###

import numpy as np

N = int(input())
A = list(map(int, input().split()))
exist = np.zeros(10**6+1, dtype=int)
for a in A:
    exist[a] += 1

if np.gcd.reduce(A) != 1:
    print('not coprime')
elif np.all([exist[i::i].sum() <= 1 for i in range(2, 10**6+1)]):
    print('pairwise coprime')
else:
    print('setwise coprime')

### FIXED CODE ###

from math import gcd

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

g_all = A[0]
for a in A[1:]:
    g_all = gcd(g_all, a)

if g_all > 1:
    print('not coprime')
    quit()

def sieve(n):
    is_prime = [True for _ in range(n+1)]
    is_prime[0] = False
    for i in range(2, n+1):
        if is_prime[i-1]:
            j = 2 * i
            while j <= n:
                is_prime[j-1] = False
                j += i
    table = [ i for i in range(1, n+1) if is_prime[i-1]]
    return is_prime, table


appeared = [False] * (10**6 + 1)
primes = sieve(10**3)[1]
for a in A:
    if a == 1:
        continue
    for p in primes:
        if a % p == 0:
            if appeared[p]:
                print('setwise coprime')
                quit()
            appeared[p] = True
            while a % p == 0:
                a //= p
    
    if a != 1:
        if appeared[a]:
            print('setwise coprime')
            quit()
        
        appeared[a] = True

print('pairwise coprime')
### BUGGY CODE ###

n = int(input())
s = input()

result = 0

for fst_index in range(n):
    for scd_index in range(fst_index + 1, n):
        if s[fst_index] == s[scd_index]:
            continue
        for trd_index in range(scd_index + 1, n):
            if s[trd_index] == s[fst_index] or s[trd_index] == s[scd_index] or \
               fst_index + trd_index == scd_index * 2:
                continue
            result += 1

print(result)


### FIXED CODE ###

n = int(input())
s = input()

result = s.count('R') * s.count('G') * s.count('B')

for fst_idx in range(n):
    for scd_idx in range(fst_idx + 1, n):
        trd_idx = scd_idx * 2 - fst_idx
        if trd_idx >= n:
            break
        elif s[fst_idx] != s[scd_idx] and s[scd_idx] != s[trd_idx] and \
                s[trd_idx] != s[fst_idx]:
            result -= 1

print(result)
### BUGGY CODE ###

from collections import deque

def argmax_poss(a):
    if len(a) == 0:
        return []
    all_ = [0]
    max_ = a[0]
    for i in range(1, len(a)):
        if a[i] > max_:
            all_ = [i]
            max_ = a[i]
        elif a[i] == max_:
            all_.append(i)
    return all_

h,w,m = map(int,input().split())
Ms = []

h_scores = [0 for i in range(h)]
w_scores = [0 for i in range(w)]

record = deque([])

for i in range(m):
  h,w = map(int,input().split())
  record.append((h-1,w-1))
  h_scores[h-1] += 1
  w_scores[w-1] += 1

max_h_poss = argmax_poss(h_scores)
max_w_poss = argmax_poss(w_scores)

out_pos_count = 0 

is_ok = False

for h in max_h_poss:
    for w in max_w_poss:
        if (h,w) not in record:
            is_ok = True
            break
        
if is_ok:
    print(max(h_scores) + max(w_scores))
else:
    print(max(h_scores) + max(w_scores)-1)



### FIXED CODE ###

from collections import deque

h,w,m = map(int,input().split())
Ms = []

h_scores = [0 for i in range(h)]
w_scores = [0 for i in range(w)]

record = set()

for i in range(m):
  h,w = map(int,input().split())
  record.add((h-1,w-1))
  h_scores[h-1] += 1
  w_scores[w-1] += 1

max_h = max(h_scores)
max_w = max(w_scores)
max_h_poss = [i for i,v in enumerate(h_scores) if v == max_h]
max_w_poss = [i for i,v in enumerate(w_scores) if v == max_w]

out_pos_count = 0 

c = False

for i in max_h_poss:
    for j in max_w_poss:
        if (i,j) not in record:
            c = True
            break        

if c:
    print(max(h_scores) + max(w_scores))
else:
    print(max(h_scores) + max(w_scores)-1)
### BUGGY CODE ###

n = int(input())
L = list(map(int, input().split()))
L.sort()
count = 0
for i in range(n - 2):
  a = L[i]
  for j in range(i + 1, n - 1):
    b = L[j]
    l = j
    r = n
    while r - l > 1:
      p = (l + r) // 2
      if L[p] < a + b:
        l = p
      else:
        r = p
    count += l - j
print(count)


### FIXED CODE ###

n = int(input())
L = list(map(int, input().split()))
L.sort()
count = 0
for i in range(n - 2):
  a = L[i]
  for j in range(i + 1, n - 1):
    b = L[j]
    l = j
    r = n
    while r - l > 1:
      p = (l + r) // 2
      if L[p] < a + b:
        l = p
      else:
        r = p
    count += l - j
print(count)
### BUGGY CODE ###

def main():
    X, Y, Z, K = map(int, input().split())
    A = sorted(list(map(int, input().split())), reverse=True)
    B = sorted(list(map(int, input().split())), reverse=True)
    C = sorted(list(map(int, input().split())), reverse=True)
    L = []
    for a in A:
        for b in B:
            L.append(a + b)
    LL = []
    for ab in L:
        for c in C:
            LL.append(ab + c)
    for x in sorted(LL, reverse=True)[:K]:
        print(x)


main()


### FIXED CODE ###

def main():
    X, Y, Z, K = map(int, input().split())
    A = sorted(list(map(int, input().split())), reverse=True)
    B = sorted(list(map(int, input().split())), reverse=True)
    C = sorted(list(map(int, input().split())), reverse=True)
    AB = []
    for a in A:
        for b in B:
            AB.append(a + b)
    AB.sort(reverse=True)
    ABC = []
    for i in range(min(K, len(AB))):
        for c in C:
            ABC.append(AB[i] + c)
    ABC.sort(reverse=True)
    for x in ABC[:K]:
        print(x)


main()
### BUGGY CODE ###

n=int(input())
a=[]
for i in range(n):
  a.append(int(input()))
print(max(a[1:]))
for j in range(1,n-1):
  print(max(max(a[:j]),max(a[j+1:])))
print(max(a[:n-1]))


### FIXED CODE ###

import collections
n=int(input())
a=[]
for i in range(n):
  a.append(int(input()))
b=collections.Counter(a)
d=list(b.items())
d.sort(key=lambda x:-int(x[0]))
if d[0][1]==1:
  for i in range(n):
    if a[i]==int(d[0][0]):
      print(int(d[1][0]))
    else:
      print(int(d[0][0]))
else:
  for i in range(n):
    print(int(d[0][0]))
### BUGGY CODE ###

import numpy as np

MOD = 10**9+7
S = str(input())
check = []
ans = 0
plo = np.zeros(13, dtype = int)
plo[0] = 1
N = len(S)
for i in range(N):
    if S[N - 1 - i] == '?':
        temp = np.zeros(13, dtype = int)
        for k in range(10):
            j = (k * 10 ** i) % 13
            temp += np.r_[plo[-j:],plo[:-j]]
        plo = temp
        plo = plo % MOD
    else:
        p = int(S[N - 1 - i])
        j = (p * 10 ** i) % 13
        plo = np.r_[plo[-j:],plo[:-j]]
print(plo[5])

### FIXED CODE ###

from copy import copy

MOD = 10**9 + 7
s = input()
count = [0 for _ in range(13)]
count[0] = 1
for i in range(len(s)):
    new = [0 for _ in range(13)]
    for m in range(13):
        new[(m * 10) % 13] = count[m]
    count = new
    if s[i] != "?":
        div = int(s[i])
        count = count[-div:] +  count[:-div]
    else:
        new = [0 for _ in range(13)]
        pre = sum(count[:3])
        SUM = sum(count) % MOD
        for j in range(13):
            pre += count[(j+3) % 13]
            pre -= count[j % 13]
            new[j] = (SUM - pre) % MOD
        count = new
print(count[5])
### BUGGY CODE ###

n=int(input())
l=list(map(int,input().split()))
cnt=0
for ind_a,a in enumerate(l[:-2]):
    for ind_b,b in enumerate(l[ind_a+1:-1]):
        for c in l[ind_a+ind_b+2:]:
            #print(a,b,c)
            if a+b>c and a+c>b and b+c>a:
                #print("yes",a,b,c)
                cnt+=1

print(cnt)


### FIXED CODE ###

n=int(input())
l=list(map(int,input().split()))

l.sort()
l.append(10**9)
cnt=0
i=0
#print(l)

while i<n-2:
    j=i+1
    while j<n-1:
        LL=j+1
        RR=n-1
        #print("#",l[i],l[j],l[LL],l[RR])
        while LL<=RR:
            k=(LL+RR)//2
            if k==n:
                break
            #print(l)
            #print(i,j,LL,RR,k)
            #print(l[i]+l[j],l[k],l[k+1])
            if l[i]+l[j]>l[k] and l[i]+l[j]<=l[k+1]:
                cnt+=k-j
                break
            elif LL==RR:
                break
            elif l[i]+l[j]<=l[k]:
                RR=k
            elif l[i]+l[j]>l[k]:
                LL=k+1
            elif l[i]+l[j]<l[k] and k==j+1:
                break
        j+=1
    i+=1

print(cnt)
### BUGGY CODE ###

S = input()[:: -1]
mod = 10 ** 9 + 7
dp = [[0 for _ in range(13)] for _ in range(len(S) + 1)]

if S[0] == "?":
    for k in range(10):
        dp[1][k % 13] += 1
else:
    dp[1][int(S[0]) % 13] += 1

tmp = 1
for i in range(2, len(S) + 1):
    tmp *= 10 
    for j in range(13):
        if dp[i - 1][j] > 0:
            if S[i - 1] == "?":
                for k in range(10):
                    dp[i][(tmp * k + j) % 13] += dp[i - 1][j]
            else:
                dp[i][(tmp * int(S[i - 1]) + j) % 13] += dp[i - 1][j]

print(dp[len(S)][5] % mod)

### FIXED CODE ###

S = input()[:: -1]
mod = 10 ** 9 + 7
dp = [[0 for _ in range(13)] for _ in range(len(S) + 1)]

if S[0] == "?":
    for k in range(10):
        dp[1][k % 13] += 1
else:
    dp[1][int(S[0]) % 13] += 1

tmp = 1
for i in range(2, len(S) + 1):
    tmp *= 10
    tmp %= 13
    for j in range(13):
        if dp[i - 1][j] > 0:
            if S[i - 1] == "?":
                for k in range(10):
                    dp[i][(tmp * k + j) % 13] += dp[i - 1][j] % mod
            else:
                dp[i][(tmp * int(S[i - 1]) + j) % 13] += dp[i - 1][j] % mod

print(dp[len(S)][5] % mod)
### BUGGY CODE ###

n = int(input())
num = 0
count = 0
a_list = []

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

if a_list.count(1) == 0:
    print(-1)
    exit()

while num != 1:
    num = a_list[num]
    count += 1

print(count)

### FIXED CODE ###

n = int(input())
num = 0
count = 0
a_list = []

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

OK = False

for i in range(n):
    num = a_list[num]
    count += 1
    if num == 1:
        OK = True
        break
if OK:    
    print(count)
else:
    print(-1)
### BUGGY CODE ###

import sys
input = sys.stdin.readline

N = int(input())
ans = 0
for i in range(1, N+1):
    til = N//i
    divsum = int(til*(til+1)/2)
    ans += divsum*i
print(ans)


### FIXED CODE ###

N = int(input())
ans = 0
for i in range(1, N+1):
    til = N//i
    divsum = int(til*(til+1)/2)
    ans += divsum*i
print(ans)
### BUGGY CODE ###

import sys

n=int(input())

a=list(map(int,input().split()))

for i in range(len(a)-1):

  if a.count(a[i])>=2:
    print("NO")
    sys.exit()

print("YES")


### FIXED CODE ###

import sys

n=int(input())

a=list(map(int,input().split()))

a.sort()

for i in range(len(a)-1):
  if a[i]==a[i+1]:
    print("NO")
    sys.exit()

print("YES")
### BUGGY CODE ###

import heapq
def SME(start,end):

    if end > N : end = N
    count = 0
    for i,x in enumerate(Whole[start:end]):
        heapq.heappush(A,[x,i])
    for j in range(start,end):
        a = heapq.heappop(A)
        for i in range(L):
            idx = a[1] + i
            try :
                if ANS[idx] : continue
                else :
                    ANS[idx] = a[0]
                    count += 1
            except IndexError : continue
        if count > N * parameter : break
    for i in range(L):
        if ANS[i] : continue
        else : ANS[i] = min(Whole[:i+1])
    for i in range(L,N):
        if ANS[i] : continue
        else : ANS[i] = min(Whole[i+1-L:i+1])
    return ANS
  
[N,L] = [int(x) for x in input().split()]
Whole = [int(x) for x in input().split()]
A = []
parameter = 0.9998
ANS = [None]*N
ANS = SME(0, N)
for i in ANS[L-1:-1]:print(i, end=" ")
print(ANS[-1])

### FIXED CODE ###

def ascend(a,b):
    mini = min(A[a:a+L])
    ans[a] = mini
    for i in range(a+1,b):
        if A[i+L-1] <= mini : mini = A[i+L-1]
        elif A[i-1] == mini : mini = min(A[i:i+L])
        ans[i] = mini

def descend(a,b):
    mini = min(A[b-1:b+L-1])
    ans[b-1] = mini
    for i in range(b-2,a-1,-1):
        if A[i] <= mini : mini = A[i]
        elif A[i+L] == mini: mini = min(A[i:i+L])
        ans[i] = mini
    
[N,L] = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
ans = [None]*(N-L+1)
count = 0;
if N > 10 ** 4:
    for i in range(100):
        if A[i+1] > A[i] : count += 1
    if count > 80 : descend(0, N-L+1)
    else : ascend(0,N-L+1)
else :
    ascend(0, N-L+1)
for i in ans[:-1] : print(i, end=" ")
print(ans[-1])
### BUGGY CODE ###

n, k = map(int, input().split(' '))
a = [int(i) for i in input().split(' ')]

dp = [float('inf')] * n
dp[0] = 0
for i in range(1, n):
    for x in range(1, min(i, k) + 1):
        z = abs(a[i - x] - a[i]) + dp[i - x]
        if dp[i] > z:
            dp[i] = z
print(dp[-1])

### FIXED CODE ###

n, k = map(int, input().split(' '))
a = [int(i) for i in input().split(' ')]

dp = [float('inf')] * n
dp[0] = 0
for i in range(1, n):
    for x in range(1, min(i, k) + 1):
        z = abs(a[i - x] - a[i]) + dp[i - x]
        if dp[i] > z:
            dp[i] = z
print(dp[-1])
### BUGGY CODE ###


def knapsack(n,c,w,v):
  if n == 0 or c == 0:
    return n
  if w[n] > c:
    return knapsack(n-1,c,w,v)
  else:
    opt1 = v[n] + knapsack(n-1,c-w[n],w,v)
    opt2 = knapsack(n-1,c,w,v)
    return max(opt1,opt2)


n,c = map(int,input().split())

w = [-1]; v = [-1]

for _ in range(n):
  wi , vi= map(int,input().split())
  w.append(wi)
  v.append(vi)

result = knapsack(n,c,w,v)
print(result)        
        
        

### FIXED CODE ###

n ,c = map(int,input().split())
wt =[]; val=[]

for _ in range(n):
  wi , vi = map(int,input().split())
  wt.append(wi)
  val.append(vi)
  
# dp [n][c+1] = 0 
dp = [[0 for i in range(c+1)]for j in range(n)]

for w in range(c+1):
  wi = wt[0]
  if wi<=w:
    dp[0][w]=val[0]
  else:
    dp[0][0]=0

for i in range(1,n):
  wi = wt[i];vi = val[i]
  for w in range(1,c+1):
    if wi<=w:
      dp[i][w] = max(vi+dp[i-1][w-wi] , dp[i-1][w])
    else:
      dp[i][w] = dp[i-1][w]

print(dp[n-1][c])
### BUGGY CODE ###

import itertools
N, M, Q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(Q)]
M_chr = []
for i in range(1,M+1):
    M_chr.append(str(i))
As = list(itertools.combinations_with_replacement(M_chr, N))
pointA_max = 0
for i, A in enumerate(As):
    pointA = 0
    for i in range(Q):
        a = abcd[i][0]
        b = abcd[i][1]
        c = abcd[i][2]
        d = abcd[i][3]
        if int(A[b-1])-int(A[a-1]) == c:
            pointA = pointA + d
    if pointA_max < pointA:
        pointA_max = pointA
print(pointA_max)

### FIXED CODE ###

import itertools
N, M, Q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(Q)]
M_chr = []
for i in range(1,M+1):
    M_chr.append(str(i))
As = list(itertools.combinations_with_replacement(M_chr, N))
pointA_max = 0
for i, A in enumerate(As):
    pointA = 0
    for i in range(Q):
        a = abcd[i][0]
        b = abcd[i][1]
        c = abcd[i][2]
        d = abcd[i][3]
        if int(A[b-1])-int(A[a-1]) == c:
            pointA = pointA + d
    if pointA_max < pointA:
        pointA_max = pointA
print(pointA_max)
### BUGGY CODE ###

import sys

n = int(input())
A = list(map(int, sys.stdin.readline()[:-1].split()))

q = int(input())
M = list(map(int, sys.stdin.readline()[:-1].split()))

def solve(A, n, i, m):
    if i == n:
        return 'no'
    a = A[i]
    if a == m:
        return('yes')
    use = solve(A, n, i+1, m)
    no_use = 'no'
    if a < m:
        no_use = solve(A, n, i+1, m - a)
    if use == 'yes' or no_use == 'yes':
        return 'yes'
    else:
        return 'no'
for m in M:
    print(solve(A, n, 0, m))

### FIXED CODE ###

import sys

n = int(input())
A = list(map(int, sys.stdin.readline()[:-1].split()))

q = int(input())
M = list(map(int, sys.stdin.readline()[:-1].split()))

R = [[None] * 2000 for i in range(n+1)]
def solve(A, n, i, m):
    if R[i][m] != None:
        return R[i][m]
    if m == 0:
        R[i][m] = True
        return True
    if n <= i:
        R[i][m] = False
        return False
    a = A[i]
    R[i][m] = (solve(A, n, i+1, m) or solve(A, n, i+1, m - a))
    return R[i][m]

for m in M:
    if solve(A, n, 0, m):
        print('yes')
    else:
        print('no')
### BUGGY CODE ###

A, B, C, x, y = list(map(int, input().split()))

cost = 1e+12
for i in range(x+1):
  for j in range(y+1):
    z = max(x-i, y-j) * 2
    cost = min(cost, A * i + B * j + C * z)
print(cost)

### FIXED CODE ###

a, b, ab, x, y = list(map(int, input().split()))

ab1 = ab * 2
abp = a + b
mins = min(x, y)
maxs = max(x, y)
nedan = min(ab1, abp) * mins

x -= mins
y -= mins
nedan_abp = x * a + y * b
nedan_ab = (maxs - mins) * ab1

if nedan_abp > nedan_ab:
  nedan += nedan_ab
else:
  nedan += nedan_abp
  
print(nedan)
### BUGGY CODE ###

# -*- coding: utf-8 -*-
from itertools import product
from heapq import heappop, heappush
import numpy as np


def small_dp(N,k,X):
    if k == 0:
        return N
    if k == 1:
        return X[0][1]*(N//X[0][0]) + N%X[0][0]
    if k == 2:
        return max(X[1][1]*i + X[0][1]*((N-i*X[1][0])//X[0][0]) + (N-i*X[1][0])%X[0][0] for i in range(N//X[1][0]+1))
    if k == 3:
        Y = sorted([(-x[1]/x[0], -x[0], x[1]) for x in X])
        X = [(-y[1], y[2]) for y in Y]
        M = np.arange(N+1)
        dp0 = X[0][1]*(M//X[0][0]) + M%X[0][0]
        d = X[1][1]*(M//X[1][0]) + M%X[1][0]
        dp1 = [np.max(dp0[w::-1] + d[:w+1]) for w in range(N,-1,-1)]
        return np.max(dp1 + X[2][1]*(M//X[2][0]) + M%X[2][0])

def solve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    
    for C in ((A,B), (B,A)):
        *X, = filter(lambda c: int.__sub__(*c)<0, zip(*C))

        N = small_dp(N, len(X), X)
    res = N

    return str(res)

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


### FIXED CODE ###

# -*- coding: utf-8 -*-
from itertools import product
from heapq import heappop, heappush

def small_dp(N,A,B):
    *X, = filter(lambda c: int.__sub__(*c)<0, zip(A,B))
    k = len(X)
    if k == 0:
        return N
    if k == 1:
        return X[0][1]*(N//X[0][0]) + N%X[0][0]
    if k == 2:
        c = 0
        for i in range(N//X[0][0]+1):
            j = (N-i*X[0][0])//X[1][0]
            c = max(X[0][1]*i + X[1][1]*j + (N-i*X[0][0])%X[1][0], c)
        return c
    if k == 3:
        if (N//X[1][0]+1)*(N//X[0][0]+1) <= 10**6 :
            return max(X[0][1]*i + max(X[1][1]*j + X[2][1]*((N-i*X[0][0]-j*X[1][0])//X[2][0]) + ((N-i*X[0][0]-j*X[1][0])%X[2][0]) for j in range((N-i*X[0][0])//X[1][0]+1)) for i in range(N//X[0][0]+1))
        else:
            import numpy as np
            dp = np.zeros(N+1, dtype=int)
            for w in range(1,N+1):
                dp[w] = max(dp[w-X[i][0]]+X[i][1] if w>=X[i][0] else w for i in range(k))
            N = dp[-1]
            return N

def solve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    res = small_dp(small_dp(N, A, B), B, A)

    return str(res)

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