N=int(eval(input()))

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

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

dp=[N]*(N+1)

#dp[i];=i個目まで選んで交換したときの最大

C=[B[i]-A[i] for i in range(3)]

for i in range(N):

    dp[i+1]=max(dp[i+1],dp[i])

    for j in range(3):

        if i+1-A[j]>=0:

            dp[i+1]=max(dp[i+1],dp[i+1-A[j]]+C[j])

M=dp[N]

dp=[0]*(M+1)

C=[A[i]-B[i] for i in range(3) if A[i]-B[i]>=0]

C=[]

b=[]

a=[]

for i in range(3):

    if A[i]-B[i]>=0:

        C.append(A[i]-B[i])

        b.append(B[i])

        a.append(A[i])

B=b

A=a

for i in range(M):

    dp[i+1]=max(dp[i+1],dp[i])

    for j in range(len(C)):

        if i+1-B[j]>=0:

            dp[i+1]=max(dp[i+1],dp[i+1-B[j]]+C[j])

print((M+dp[M]))

def main():

    N=int(eval(input()))

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

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

    dp=[N]*(N+1)

    #dp[i];=i個目まで選んで交換したときの最大

    C=[B[i]-A[i] for i in range(3)]

    for i in range(N):

        dp[i+1]=max(dp[i+1],dp[i])

        for j in range(3):

            if i+1-A[j]>=0:

                dp[i+1]=max(dp[i+1],dp[i+1-A[j]]+C[j])

    M=dp[N]

    dp=[0]*(M+1)

    C=[A[i]-B[i] for i in range(3) if A[i]-B[i]>=0]

    C=[]

    b=[]

    a=[]

    for i in range(3):

        if A[i]-B[i]>=0:

            C.append(A[i]-B[i])

            b.append(B[i])

            a.append(A[i])

    B=b

    A=a

    for i in range(M):

        dp[i+1]=max(dp[i+1],dp[i])

        for j in range(len(C)):

            if i+1-B[j]>=0:

                dp[i+1]=max(dp[i+1],dp[i+1-B[j]]+C[j])

    print((M+dp[M]))

if __name__=='__main__':#おまじない

  main()

N = int(eval(input()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
dp = [N] * (N + 1)
# dp[i];=i個目まで選んで交換したときの最大
C = [B[i] - A[i] for i in range(3)]
for i in range(N):
    dp[i + 1] = max(dp[i + 1], dp[i])
    for j in range(3):
        if i + 1 - A[j] >= 0:
            dp[i + 1] = max(dp[i + 1], dp[i + 1 - A[j]] + C[j])
M = dp[N]
dp = [0] * (M + 1)
C = [A[i] - B[i] for i in range(3) if A[i] - B[i] >= 0]
C = []
b = []
a = []
for i in range(3):
    if A[i] - B[i] >= 0:
        C.append(A[i] - B[i])
        b.append(B[i])
        a.append(A[i])
B = b
A = a
for i in range(M):
    dp[i + 1] = max(dp[i + 1], dp[i])
    for j in range(len(C)):
        if i + 1 - B[j] >= 0:
            dp[i + 1] = max(dp[i + 1], dp[i + 1 - B[j]] + C[j])
print((M + dp[M]))

def main():
    N = int(eval(input()))
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    dp = [N] * (N + 1)
    # dp[i];=i個目まで選んで交換したときの最大
    C = [B[i] - A[i] for i in range(3)]
    for i in range(N):
        dp[i + 1] = max(dp[i + 1], dp[i])
        for j in range(3):
            if i + 1 - A[j] >= 0:
                dp[i + 1] = max(dp[i + 1], dp[i + 1 - A[j]] + C[j])
    M = dp[N]
    dp = [0] * (M + 1)
    C = [A[i] - B[i] for i in range(3) if A[i] - B[i] >= 0]
    C = []
    b = []
    a = []
    for i in range(3):
        if A[i] - B[i] >= 0:
            C.append(A[i] - B[i])
            b.append(B[i])
            a.append(A[i])
    B = b
    A = a
    for i in range(M):
        dp[i + 1] = max(dp[i + 1], dp[i])
        for j in range(len(C)):
            if i + 1 - B[j] >= 0:
                dp[i + 1] = max(dp[i + 1], dp[i + 1 - B[j]] + C[j])
    print((M + dp[M]))


if __name__ == "__main__":  # おまじない
    main()

-N = int(eval(input())) -A = list(map(int, input().split())) -B = list(map(int, input().split())) -dp = [N] * (N + 1) -# dp[i];=i個目まで選んで交換したときの最大 -C = [B[i] - A[i] for i in range(3)] -for i in range(N): -    dp[i + 1] = max(dp[i + 1], dp[i]) -    for j in range(3): -        if i + 1 - A[j] >= 0: -            dp[i + 1] = max(dp[i + 1], dp[i + 1 - A[j]] + C[j]) -M = dp[N] -dp = [0] * (M + 1) -C = [A[i] - B[i] for i in range(3) if A[i] - B[i] >= 0] -C = [] -b = [] -a = [] -for i in range(3): -    if A[i] - B[i] >= 0: -        C.append(A[i] - B[i]) -        b.append(B[i]) -        a.append(A[i]) -B = b -A = a -for i in range(M): -    dp[i + 1] = max(dp[i + 1], dp[i]) -    for j in range(len(C)): -        if i + 1 - B[j] >= 0: -            dp[i + 1] = max(dp[i + 1], dp[i + 1 - B[j]] + C[j]) -print((M + dp[M])) +def main(): +    N = int(eval(input())) +    A = list(map(int, input().split())) +    B = list(map(int, input().split())) +    dp = [N] * (N + 1) +    # dp[i];=i個目まで選んで交換したときの最大 +    C = [B[i] - A[i] for i in range(3)] +    for i in range(N): +        dp[i + 1] = max(dp[i + 1], dp[i]) +        for j in range(3): +            if i + 1 - A[j] >= 0: +                dp[i + 1] = max(dp[i + 1], dp[i + 1 - A[j]] + C[j]) +    M = dp[N] +    dp = [0] * (M + 1) +    C = [A[i] - B[i] for i in range(3) if A[i] - B[i] >= 0] +    C = [] +    b = [] +    a = [] +    for i in range(3): +        if A[i] - B[i] >= 0: +            C.append(A[i] - B[i]) +            b.append(B[i]) +            a.append(A[i]) +    B = b +    A = a +    for i in range(M): +        dp[i + 1] = max(dp[i + 1], dp[i]) +        for j in range(len(C)): +            if i + 1 - B[j] >= 0: +                dp[i + 1] = max(dp[i + 1], dp[i + 1 - B[j]] + C[j]) +    print((M + dp[M])) + + +if __name__ == "__main__":  # おまじない +    main()