def dfs():

    n = int(eval(input()))

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

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

    

    

    def knapsack(n, weight, value):

        dp = [i for i in range(n+1)]

        for i in range(3):

            for j in range(weight[i], n+1):

                dp[j] = max(dp[j], dp[j-weight[i]] + value[i])

        return(dp[-1])

    

    

    new_n=knapsack(n=n, weight=weight, value=value)

    print((knapsack(n=new_n, weight=value, value=weight)))

 

    

dfs()
# W

# N K

# A1 B1

# A2 B2

# :

# AN BN



n = int(eval(input()))

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

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







dp = list(range(n + 1))



for ci in range(3):

    for wi in range(a_list[ci], n + 1):

        dp[wi] = max(dp[wi - a_list[ci]] + b_list[ci], dp[wi])







n = dp[-1]

dp = list(range(n + 1))





for ci in range(3):

    for wi in range(b_list[ci], n + 1):

        dp[wi] = max(dp[wi - b_list[ci]] + a_list[ci], dp[wi])









print((dp[-1]))
def dfs():
    n = int(eval(input()))
    weight = list(map(int, input().split()))
    value = list(map(int, input().split()))

    def knapsack(n, weight, value):
        dp = [i for i in range(n + 1)]
        for i in range(3):
            for j in range(weight[i], n + 1):
                dp[j] = max(dp[j], dp[j - weight[i]] + value[i])
        return dp[-1]

    new_n = knapsack(n=n, weight=weight, value=value)
    print((knapsack(n=new_n, weight=value, value=weight)))


dfs()

# W
# N K
# A1 B1
# A2 B2
# :
# AN BN
n = int(eval(input()))
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
dp = list(range(n + 1))
for ci in range(3):
    for wi in range(a_list[ci], n + 1):
        dp[wi] = max(dp[wi - a_list[ci]] + b_list[ci], dp[wi])
n = dp[-1]
dp = list(range(n + 1))
for ci in range(3):
    for wi in range(b_list[ci], n + 1):
        dp[wi] = max(dp[wi - b_list[ci]] + a_list[ci], dp[wi])
print((dp[-1]))

-def dfs(): -    n = int(eval(input())) -    weight = list(map(int, input().split())) -    value = list(map(int, input().split())) - -    def knapsack(n, weight, value): -        dp = [i for i in range(n + 1)] -        for i in range(3): -            for j in range(weight[i], n + 1): -                dp[j] = max(dp[j], dp[j - weight[i]] + value[i]) -        return dp[-1] - -    new_n = knapsack(n=n, weight=weight, value=value) -    print((knapsack(n=new_n, weight=value, value=weight))) - - -dfs() +# W +# N K +# A1 B1 +# A2 B2 +# : +# AN BN +n = int(eval(input())) +a_list = list(map(int, input().split())) +b_list = list(map(int, input().split())) +dp = list(range(n + 1)) +for ci in range(3): +    for wi in range(a_list[ci], n + 1): +        dp[wi] = max(dp[wi - a_list[ci]] + b_list[ci], dp[wi]) +n = dp[-1] +dp = list(range(n + 1)) +for ci in range(3): +    for wi in range(b_list[ci], n + 1): +        dp[wi] = max(dp[wi - b_list[ci]] + a_list[ci], dp[wi]) +print((dp[-1]))