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

from collections import defaultdict
class UnionFind:


    def __init__(self,n):
        self.parent = [i for i in range(n)]
        self.rank = [0]*n
        self.n = n
    def find(self,x):
        if self.parent[x] == x:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]
    def union(self,x,y):
        x = self.find(x)
        y = self.find(y)
        if x == y:return
        if self.rank[x] < self.rank[y]:
            self.parent[x] = y
        else:
            self.parent[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
    def same(self,x,y):
        return bool(self.find(x)==self.find(y))
    def sameset(self):
        ret = defaultdict(set)
        for i in range(self.n):
            ret[self.find(i)] = ret[self.find(i)] | {i}
        return ret

from sys import stdin
def main():
    N,K,L = map(int,stdin.readline().split())
    uf = UnionFind(N)
    uf1 = UnionFind(N)
    ans = [0 for _ in range(N)]
    for _ in range(K):
        p,q = map(int,stdin.readline().split())
        uf.union(p-1,q-1)
    for _ in range(L):
        r,s = map(int,stdin.readline().split())
        if not uf.same(s-1,r-1):continue
        uf1.union(r-1,s-1)
    a = uf.sameset()
    b = uf1.sameset()
    for u in b.values():
        for v in a.values():
            tmp = list(u&v)
            if tmp:
                l = len(tmp)
                for j in tmp:
                    ans[j] += l
            else:continue
    print(*ans)

if __name__ == "__main__":
    main()

### FIXED CODE ###

from collections import defaultdict
class UnionFind:


    def __init__(self,n):
        self.parent = [i for i in range(n)]
        self.rank = [0]*n
        self.n = n
    def find(self,x):
        if self.parent[x] == x:
            return x
        while x != self.parent[x]:
            self.parent[x] = self.parent[self.parent[x]]
            x = self.parent[x]
        return x
    def union(self,x,y):
        x = self.find(x)
        y = self.find(y)
        if x == y:return
        if self.rank[x] < self.rank[y]:
            self.parent[x] = y
        else:
            self.parent[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
    def same(self,x,y):
        return bool(self.find(x)==self.find(y))

from sys import stdin
def main():
    N,K,L = map(int,stdin.readline().split())
    uf = UnionFind(N)
    uf1 = UnionFind(N)

    for _ in range(K):
        p,q = map(int,stdin.readline().split())
        uf.union(p-1,q-1)
    for _ in range(L):
        r,s = map(int,stdin.readline().split())
        uf1.union(r-1,s-1)
    d = defaultdict(int)
    for i in range(N):
        d[(uf.find(i),uf1.find(i))] += 1
    print(*(d[uf.find(i),uf1.find(i)]for i in range(N)))
    #print(l)

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

from itertools import combinations
N=input()
A=list(map(int,input().split()))
p=10**9+7
ans=0
for i in list(combinations(A,2)):
    a=((i[0]%p)*(i[1]%p))%p
    ans+=a
    ans=ans%p
print(ans)

### FIXED CODE ###

from itertools import accumulate
N=int(input())
A=list(map(int,input().split()))
p=10**9+7
ans=0
B=list(accumulate(A[::-1]))
for i,value in enumerate(A):
    if i==(N-1):
        break
    ans+=((value%p)*(B[-2-i]%p))%p
    ans=ans%p
print(ans)
### BUGGY CODE ###

N = int(input())
A = [int(a) for a in input().split()]
ans = []
for i in range(N):
  ans.append(A.index(min(A)) + 1)
  A[A.index(min(A))] = N + 1
for i in range(N):
  print(ans[i], end = " ")


### FIXED CODE ###

N = int(input())
A = [int(a) for a in input().split()]
ans = [0] * N
for i in range(N):
  ans[A[i] - 1] = i + 1
print(*ans)
### BUGGY CODE ###

N=int(input())

li=[]
l=0
m=0

for i in range(N):
  if N%(i+1)==0:
    l=i
    m=N/(i+1)-1
    li.append(l+m)
  else:
    pass

print(int(min(li)))



### FIXED CODE ###

import math 

N=int(input())

li=[]
l=0
m=0

for i in range(math.floor(math.sqrt(N))):
  if N%(i+1)==0:
    l=i
    m=N/(i+1)-1
    li.append(l+m)
  else:
    pass

print(int(min(li)))
### BUGGY CODE ###

S = list(input())
count = 0

while True:
    change = False
    for i in range(len(S)-1):
        if S[i] != S[i+1]:
            S.pop(i)
            S.pop(i)
            change = True
            count += 2
            break
    if not change:
        break

print(count)

### FIXED CODE ###

from collections import Counter

S = Counter(input()).most_common()[::-1]

if len(S) == 1:
    print(0)
else:
    print(S[0][1] * 2)
### BUGGY CODE ###

#inport
import sys
import numpy as np

#input
N,M = map(int, sys.stdin.readline().split())
P = [list(map(int, sys.stdin.readline().split())) for i in range(M)]

#shori
##初期化
P = np.array(P)     
ans = np.zeros((N,2))
tmp_latest = np.zeros(1)
tmp_latest_tmp = np.zeros(1)
tmp_latest[0] = 1
pos_latest = 0
num_latest = 1
ans[0][1] = 1 #部屋1をてがかりありに
flg_pos = 1
flg_fale = 0
num_crew = 1
num_left_room = N-1
left_room = np.zeros(N)

while num_crew < N:
    if flg_pos == 0:
        flg_fale = 1
        break

    flg_pos = 0
    for i in range(num_latest):
        tmp_room_num = tmp_latest[i]
        tmp_root_point = np.where(P == int(tmp_room_num))[0]
        root_loop_n = int(np.size(tmp_root_point))
        for j in range(root_loop_n):
            if P[tmp_root_point[j]][0]==tmp_latest[i]:
                if ans[P[tmp_root_point[j]][1]-1][1]==0:
                    tmp_latest_tmp = np.insert(tmp_latest_tmp, 0, P[tmp_root_point[j]][1])
                    ans[P[tmp_root_point[j]][1]-1][1] = tmp_latest[i]
                    num_crew = num_crew + 1
                    flg_pos = 1
            elif P[tmp_root_point[j]][1]==tmp_latest[i]:
                if ans[P[tmp_root_point[j]][0]-1][1]==0:
                    tmp_latest_tmp = np.insert(tmp_latest_tmp, 0, P[tmp_root_point[j]][0])
                    ans[P[tmp_root_point[j]][0]-1][1] = tmp_latest[i]
                    num_crew = num_crew + 1
                    flg_pos = 1               
        P = np.delete(P, tmp_root_point, axis=0)    

    #Nループ終了時にtmp_latestを更新
    tmp_latest = np.insert(tmp_latest, num_latest, tmp_latest_tmp)
    for j in range(num_latest):
        tmp_latest = np.delete(tmp_latest,0)
    num_latest = np.size(tmp_latest)
    tmp_latest = np.delete(tmp_latest,num_latest-1)
    num_latest = np.size(tmp_latest)
    tmp_latest_tmp = np.zeros(1)

    
if flg_fale == 0:
    print('Yes')
    for i in range(N-1):
        print(int(ans[i+1][1]))
else:
    print('No')


### FIXED CODE ###

#inport
import sys
import numpy as np
from collections import deque


#input
N,M = [int(x) for x in sys.stdin.readline().split()]
g = [[] for _ in range(N+1)]

for _ in range(M):
    a, b = [int(x) for x in sys.stdin.readline().split()]
    g[a].append(b)
    g[b].append(a)



quetmp = deque([1])
d = [None] * (N+1)
d[1] = 0
num_crew = 1
while quetmp:
    v = quetmp.popleft()
    for i in g[v]:
        if d[i] is None:
            d[i] = v
            quetmp.append(i)
            num_crew = num_crew + 1

if num_crew == N:
    print('Yes')
    for i in range(N-1):
        print(d[i+2])
else:
    print('No')
### BUGGY CODE ###

n = int(input())
l = sorted(list(map(int,input().split())))
cnt = 0
for i in range(n):
    for j in range(i+1,n):
        a = l[i]
        b = l[j]
        for k in range(j+1,n):
            if l[k] < a+b:
                c = l[k]
            else:
                break
            if b+c>a and c+a>b:
                cnt+=1
print(cnt)

### FIXED CODE ###

import bisect
n = int(input())
l = list(map(int,input().split()))
cnt = 0
l = sorted(l)
for i in range(n):
    for j in range(i+1,n):
        d = l[i]+ l[j]
        cnt +=bisect.bisect_left(l,d) - (j+1)
print(cnt)
### BUGGY CODE ###

from collections import Counter

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

s = input()

if 10 % p == 0:
    ans = 0
    for i, x in enumerate(s):
        if int(x) % p == 0:
            ans += i + 1
    print(ans)
else:
    li = [0]
    for i, x in enumerate(s[::-1]):
        li.append((li[-1] + int(x) * (10**i)) % p)

    ans = 0
    c = Counter(li[1:])
    for v in c.values():
        ans += v * (v-1)
    ans = ans // 2
    ans += c[0]
    print(ans)


### FIXED CODE ###

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

s = input()

if 10 % p == 0:
    ans = sum(i + 1 for i, x in enumerate(s) if int(x) % p == 0)
    print(ans)
else:
    counter = [0] * p
    counter[0] = 1
    cur = 0
    p10 = 1
    ans = 0
    for x in s[::-1]:
        cur = (cur + int(x) * p10) % p
        ans += counter[cur]
        counter[cur] += 1
        p10 *= 10
        p10 %= p
    print(ans)
### BUGGY CODE ###

import sys
n, ma, mb = map(int, input().split())
t = [[[] for _ in range(100 * n + 1)] for _ in range(n + 1)]
for i in range(n):
  a, b, c = map(int, input().split())
  t[i + 1][c].append((a, b))
  for j in range(1, 100 * n + 1):
    for aa, bb in t[i][j]:
      t[i + 1][j].append((aa, bb))
      t[i + 1][c + j].append((a + aa, b + bb))
for i in range(1, 100 * n + 1):
  for a, b in t[n][i]:
    if a % ma == 0 and b % mb == 0 and a // ma == b // mb:
      print(i)
      sys.exit()
print(-1)


### FIXED CODE ###

import sys
inf = float('inf')
n, ma, mb = map(int, input().split())
t = [[inf] * 401 for _ in range(401)]
for _ in range(n):
  a, b, c = map(int, input().split())
  for aa in range(400, 0, -1):
    for bb in range(400, 0, -1):
      if t[aa][bb] == inf:
        continue
      if t[a + aa][b + bb] > c + t[aa][bb]:
        t[a + aa][b + bb] = c + t[aa][bb]
  if t[a][b] > c:
    t[a][b] = c
result = inf
for a in range(400, 0, -1):
  for b in range(400, 0, -1):
    if a * mb == b * ma and t[a][b] < result:
      result = t[a][b]
if result == inf:
  print(-1)
else:
  print(result)
### BUGGY CODE ###

s = input()
flag = True
count = 0
while flag:
	flag = False
	for i in range(len(s) - 1):
		if s[i:i+2] == "BW":
			s = s[:i] + "WB" + s[i+2:]
			count += 1
			flag = True
print(count)

### FIXED CODE ###

s = input()
b = 0
ans = 0
for c in s:
	if c == 'B':
		b += 1
	else:
		ans += b
print(ans)
### BUGGY CODE ###

def my_sign(n):
    return (n > 0) - (n < 0)
def kugiri(cnt, lst):
    if len(lst) <= 1:
        return cnt
    else:
        pm = lst[0]
        i = 1
        fl = 0
        while i < len(lst):
            if pm == lst[i]:
                i += 1
                continue
            else:
                fl = 1
                break
        if fl == 1:
            new_lst = []
            for j in range(i + 1, len(lst)):
                new_lst.append(lst[j])
            return kugiri(cnt + 1, new_lst)
        else:
            return cnt

N = int(input())
A_lst = [int(x) for x in input().split()]
cnt = 1
if N == 1:
    pass
else:
    S_lst = []
    for i in range(1, N):
        S_lst.append(my_sign(A_lst[i] - A_lst[i - 1]))
    for i in range(len(S_lst)):
        if i == 0 and S_lst[i] == 0:
            for j in range(len(S_lst)):
                if not S_lst[j] == 0:
                    S_lst[i] = S_lst[j]
        elif S_lst[i] == 0:
            S_lst[i] = S_lst[i - 1]
    cnt = kugiri(cnt, S_lst)
print(cnt)


### FIXED CODE ###

def main():
    N = int(input())
    lst = [int(_) for _ in input().split()]
    last = lst[0]
    A = [last]
    for i in range(1, N):
        if lst[i] == last: continue
        last = lst[i]
        A.append(lst[i])
    left = right = output = 0
    while left < len(A):
        if left == len(A)-1:
            output += 1
            break
        if A[left+1] - A[left] > 0:
            while right < len(A)-1 and A[right] < A[right+1]:
                right += 1
        elif A[left+1] - A[left] < 0:
            while right < len(A)-1 and A[right] > A[right+1]:
                right += 1
        output += 1
        left = right + 1
        right = left
    print(output)
    return

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

N=int(input())
sisterN=[]
SISTERn=[]
SISterN=[]
for n in range(N):
    _=int(input())
    if _<40000:
        _s= _
        if sisterN.count(_s)==1:
            sisterN.remove(_s)
        else:
            sisterN.append(_s)
    elif 40001<_<80000:
        _m= _
        if SISterN.count(_m)==1:
            SISterN.remove(_m)
        else:
            SISterN.append(_m)
    elif 80001<_:
        _b= _
        if SISTERn.count(_b)==1:
            SISTERn.remove(_b)
        else:
            SISTERn.append(_b)

print(len(sisterN)+len(SISTERn)+len(SISterN))

### FIXED CODE ###

N=int(input())
S=set()

for n in range(N):
    _=int(input())
    if _ in S:
        S.remove(_)
    else:
        S.add(_)

print(len(S))
### BUGGY CODE ###

A = input()

S = [0]
dic = {0:1}

for n in range(1, len(A)+1):
    S.append(S[-1] + (int(A[-n])*(10**(n-1))))
    sm = S[-1] % 2019
    S.pop(0)
    if sm in dic:
        dic[sm] += 1
    else:
        dic[sm] = 1

result = 0

for n in dic.values():
    result += (n*(n-1)/2)


print(int(result))


### FIXED CODE ###

S = input()
 
res = [0] * 2019
res[0] = 1
 
d = 1
a = 0
 
for c in reversed(S):
    a += int(c) * d
    a %= 2019
    res[a] += 1
    d *= 10
    d %= 2019
 
ans = 0
for r in res:
    ans += r*(r-1) // 2
print(ans)
### BUGGY CODE ###

# -*- coding: utf-8 -*-


class Node(object):
    def __init__(self, key, prev=None, nex=None):
        self.key = key
        self.prev = prev
        self.next = nex


class DoublyLinkedLiset(object):
    def __init__(self):
        self.nil = Node(None)
        self.nil.next = self.nil
        self.nil.prev = self.nil

    def insert(self, x):
        X = Node(x)
        X.next = self.nil.next
        self.nil.next.prev = X
        self.nil.next = X
        X.prev = self.nil

    def listSearch(self, x):
        cur = self.nil.next
        while (cur != self.nil and cur.key != x):
            cur = cur.next

        return cur

    def deleteNode(self, X):
        #if X == self.nil:
        #    return
        X.prev.next = X.next
        X.next.prev = X.prev
        del X

    def deleteFirst(self):
        self.deleteNode(self.nil.next)

    def deleteLast(self):
        self.deleteNode(self.nil.prev)

    def deleteKey(self, x):
        X = self.listSearch(x)
        if X == self.nil:
            return
        self.deleteNode(X)

    def printList(self):
        cur = self.nil.next
        ret = []

        while cur.key is not None:
            ret.append(cur.key)
            cur = cur.next

        print(' '.join(ret))


if __name__ == '__main__':

    n = int(input())
    dll = DoublyLinkedLiset()

    for _ in range(n):
        command = input().split(" ")
        if command[0] == "insert":
            dll.insert(command[1])
        elif command[0] == "delete":
            dll.deleteKey(command[1])
        elif command[0] == "deleteFirst":
            dll.deleteFirst()
        elif command[0] == "deleteLast":
            dll.deleteLast()

    dll.printList()



### FIXED CODE ###

# -*- coding: utf-8 -*-

from collections import deque

if __name__ == '__main__':
    n = int(input())
    L = deque()

    for _ in range(n):
        command = input().split(" ")
        if command[0] == "insert":
            L.appendleft(command[1])
        elif command[0] == "delete":
            try:
                L.remove(command[1])
            except:
                pass
        elif command[0] == "deleteFirst":
            L.popleft()
        elif command[0] == "deleteLast":
            L.pop()

    print(" ".join(L))
### BUGGY CODE ###

from collections import deque
import numpy as np


def bfs(y, x, H, W, S):
    dirs = [(-1, 0), (0, -1), (1, 0), (0, 1)]
    depth = np.full((H+2, W+2), -1, dtype=np.int32)
    q = deque()
    q.append((y, x))
    depth[y][x] = 0
    max_depth = 0
    while q:
        y, x = q.popleft()
        max_depth = max(max_depth, depth[y][x])
        for dy, dx in dirs:
            ny, nx = y + dy, x + dx
            if depth[ny][nx] == -1 and S[ny][nx] == '.':
                depth[ny][nx] = depth[y][x] + 1
                q.append((ny, nx))
    return max_depth


def solve():
    H, W = map(int, input().split())
    S = list()
    S.append(['#'] * (W+2))
    for _ in range(H):
        buf = input()
        S.append(list('#' + buf + '#'))
    S.append(['#'] * (W+2))

    dist_max = 0
    for y in range(H):
        for x in range(W):
            if S[y+1][x+1] == '.':
                d = bfs(y+1, x+1, H, W, S)
                dist_max = max(d, dist_max)
    print(dist_max)


if __name__ == '__main__':
    solve()


### FIXED CODE ###

from collections import deque


def bfs(y, x, H, W, S):
    dirs = [(-1, 0), (0, -1), (1, 0), (0, 1)]
    depth = [[-1] * (W+2) for h in range(H+2)]
    q = deque()
    q.append((y, x))
    depth[y][x] = 0
    max_depth = 0
    while q:
        y, x = q.popleft()
        max_depth = max(max_depth, depth[y][x])
        for dy, dx in dirs:
            ny, nx = y + dy, x + dx
            if S[ny][nx] == '.' and depth[ny][nx] == -1:
                depth[ny][nx] = depth[y][x] + 1
                q.append((ny, nx))
    return max_depth


def solve():
    H, W = map(int, input().split())
    S = list()
    S.append(['#'] * (W+2))
    for _ in range(H):
        buf = input()
        S.append(list('#' + buf + '#'))
    S.append(['#'] * (W+2))

    dist_max = 0
    for y in range(H):
        for x in range(W):
            if S[y+1][x+1] == '.':
                d = bfs(y+1, x+1, H, W, S)
                dist_max = max(d, dist_max)
    print(dist_max)


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

n=int(input())
graph=[[] for i in range(n)]
node=[[-1]*n for i in range(n)]
color=[-1]*n
for i in range(n-1):
    a,b,c=map(int,input().split())
    graph[a-1].append([b-1,c%2])
    graph[b-1].append([a-1,c%2])
def coloring(pre,c):
  color[pre]=c
  for i,j in graph[pre]:
    if color[i]<0:
      coloring(i,color[pre]^j)
coloring(0,1)
for i in color:
  print(i)

### FIXED CODE ###

import sys
sys.setrecursionlimit(1000000)
n=int(input())
graph=[[] for i in range(n)]
color=[-1]*n
for i in range(n-1):
    a,b,c=map(int,input().split())
    graph[a-1].append([b-1,c%2])
    graph[b-1].append([a-1,c%2])
def coloring(pre,c):
  color[pre]=c
  for i,j in graph[pre]:
    if color[i]<0:
      coloring(i,color[pre]^j)
coloring(0,1)
for i in color:
  print(i)
### BUGGY CODE ###

n, k, q = map(int, input().split())
anss = []
for i in range(q):
  anss.append(int(input()))
#N人、持ち点K、問題Q
atd = []
for i in range(n):
  atd.append(k)
  
for i in anss:
  atd = list(map(lambda x:x -1,atd))
  atd[i-1] += 1
  
for i in atd:
  if i >0:
    print("Yes")
  else:
    print("No")

### FIXED CODE ###

n, k, q = map(int, input().split())
#N人、持ち点K、問題Q
anss = [0] * n
tmp = 0
for i in range(q):
  tmp = int(input())
  anss[tmp-1] += 1
for i in anss:
  if i > q - k:
    print("Yes")
  else:
    print("No")
### BUGGY CODE ###

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

for i in range(M):
  A.sort()
  A[-1] //=2
print(sum(A))

### FIXED CODE ###

import sys
import heapq
from bisect import insort_right
input = sys.stdin.readline
N, M = map(int, input().split())
A = list(map(int, input().split()))
A = [-a for a in A]
heapq.heapify(A)
  
for i in range(M):
  tmp = heapq.heappop(A)
  heapq.heappush(A,int(tmp/2))
print(sum([-a for a in A]))
### BUGGY CODE ###

from collections import deque

def main():
    h,w=map(int,input().split())
    maze=[list(input()) for i in range(h)]
    d=[[float("inf")]*w for i in range(h)]
    que=deque([])
    dx=[1,0,-1,0]
    dy=[0,1,0,-1]
    for i in range(h):
        for j in range(w):
            if maze[i][j]=="#":
                que.append((i,j))           
                d[i][j]=0
    ans=0
    while que:
        p=que.popleft()
        ans=d[p[0]][p[1]]
        for i in range(4):
            nx=p[0]+dx[i]
            ny=p[1]+dy[i]
            if 0<=nx and nx<h and 0<=ny and ny<w and maze[nx][ny]!="#" and d[nx][ny]==float("inf"):
                d[nx][ny]=ans+1
                que.append((nx,ny))  
    return ans
if __name__=='__main__':
    print(main())




### FIXED CODE ###

from collections import deque

def main():
    h,w=map(int,input().split())
    maze=[list(input()) for i in range(h)]
    d=[[-1]*w for i in range(h)]
    que=deque([])
    dd=((1,0),(0,1),(-1,0),(0,-1))
    for i in range(h):
        for j in range(w):
            if maze[i][j]=="#":
                que.append((i,j))           
                d[i][j]=0
    ans=0
    while que:
        p=que.popleft()
        ans=d[p[0]][p[1]]
        for dx,dy in dd:
            nx=p[0]+dx
            ny=p[1]+dy
            if 0<=nx and nx<h and 0<=ny and ny<w and maze[nx][ny]!="#" and d[nx][ny]==-1:
                d[nx][ny]=ans+1
                que.append((nx,ny))  
    return ans
if __name__=='__main__':
    print(main())
### BUGGY CODE ###

S = list(input())
Q = int(input())

reverse = False
cnt = 0
for i in range(Q):
    inp = input().split()
    if inp[0] == "1":
        cnt += 1
        reverse = (cnt % 2 != 0)
        #S = S[::-1]
    else:
        f, c = inp[1:]
        if int(f) == 1:
            if reverse:
                S.append(c)
            else:
                S.insert(0, c)
        else:
            if reverse:
                S.insert(0, c)
            else:
                S.append(c)
S = "".join(S)
print(S[::-1] if reverse else S)

### FIXED CODE ###

from collections import deque

S = deque(input())
Q = int(input())

reverse = False
cnt = 0
for i in range(Q):
    inp = input().split()
    if inp[0] == "1":
        cnt += 1
        reverse = (cnt % 2 != 0)
        #S = S[::-1]
    else:
        f, c = inp[1:]
        if int(f) == 1:
            if reverse:
                S.append(c)
            else:
                S.appendleft(c)
        else:
            if reverse:
                S.appendleft(c)
            else:
                S.append(c)
S = "".join(S)
print(S[::-1] if reverse else S)
### BUGGY CODE ###

N = int(input())

ga, sa, ba = list(map(int, input().split()))
gb, sb, bb = list(map(int, input().split()))


# case
# すべて a > b
# すべて b < a
# 2個  a > b
# 1個  a > b

# weight:ga price:gb どんぐり weight:1 price:1


vmax = 0
for g in range(N+1):
    for s in range(N+1-g):
        v = (g//ga)*gb + (s//sa)*sb
        amari = N - g -s
        if amari < 0:
            break
        if ba < bb:
            v += amari//ba * bb
            amari %= ba
        v += amari

        vmax = max(vmax,v)

D = vmax
vmax = D
for g in range(D + 1):
    if ga < gb and g  > 0:
        break
    for s in range(D + 1 - g):
        if sa < sb and s > 0:
            break
        v = (g//gb)*ga + (s//sb)*sa
        amari = D - g - s
        if amari < 0:
            break
        if ba > bb:
            v += amari // bb * ba
            amari %= bb
        v += amari

        vmax = max(vmax, v)

print(vmax)




### FIXED CODE ###

N = int(input())

ga, sa, ba = list(map(int, input().split()))
gb, sb, bb = list(map(int, input().split()))

# case
# すべて a > b
# すべて b < a
# 2個  a > b
# 1個  a > b

# weight:ga price:gb どんぐり weight:1 price:1


vmax = 0
for g in range(0,N + 1, ga):
    for s in range(0,N + 1 - g, sa):
        v = (g // ga) * gb + (s // sa) * sb
        amari = N - g - s
        if amari < 0:
            break
        if ba < bb:
            v += amari // ba * bb
            amari %= ba
        v += amari

        vmax = max(vmax, v)

D = vmax
vmax = D

if ba < bb:
    tmp = sa,sb
    sa,sb = ba,bb
    ba,bb = tmp

for g in range(0,D + 1, gb):
    if ga < gb and g > 0:
        break
    for s in range(0, D + 1 - g, sb):
        if sa < sb and s > 0:
            break
        v = (g // gb) * ga + (s // sb) * sa
        amari = D - g - s
        if amari < 0:
            break
        if ba > bb:
            v += amari // bb * ba
            amari %= bb
        v += amari

        vmax = max(vmax, v)

print(vmax)