Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
bf,bs = 0,0
fir = []
sec = []
fi1 = 1
se1 = 1
ans = n
ansf = n
for i in range(3):
  if a[i]>b[i]:
    if b[i] == 1:
      bs = 1
      if se1 < a[i]:
        se1 = a[i]
    else:     
      sec.append((a[i],b[i]))
  else:
    if a[i] == 1:
      bf = 1
      if fi1 < b[i]:
        fi1 = b[i]
    else:
      fir.append((b[i],a[i]))
sec += [(n,n),(n,n),(n,n)]
fir += [(n,n),(n,n),(n,n)]
for p in range(n//fir[0][1]+1):
  for q in range((n-p*fir[0][1])//fir[1][1]+1):
    rg = n - p*fir[0][1] - q*fir[1][1]
    if rg < 0:
      continue
    if bf:
      res = rg
      r = 0
    else:
      res = rg%fir[2][1]
      r = rg//fir[2][1]
    en = p*fir[0][0] + q*fir[1][0] + r*fir[2][0] + res*fi1
    if ansf < en:
      ansf = en
for p in range(ansf//sec[0][1]+1):
  for q in range((ansf-p*sec[0][1])//sec[1][1]+1):
    rg = ansf - p*sec[0][1] - q*sec[1][1]
    if rg < 0:
      continue
    if bs:
      res =  rg
      r = 0
    else:
      res = rg%sec[2][1]
      r = rg//sec[2][1]
    en = p*sec[0][0] + q*sec[1][0] + r*sec[2][0] + res*se1
    if ans < en:
      ans = en
print(ans)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
dp = [0]*(n+1)
fir = []
sec = []
for i in range(3):
  if a[i]>b[i]:
    sec.append((b[i],a[i]))
  else:
    fir.append((a[i],b[i]))
for i in range(1,n+1):
  dp[i] = i
  for x,y in fir:
    if i-x >= 0:
      dp[i] = max(dp[i],dp[i-x]+y)
n = dp[n]
dp = [0]*(n+1)
for i in range(1,n+1):
  dp[i] = i
  for x,y in sec:
    if i-x >= 0:
      dp[i] = max(dp[i],dp[i-x]+y)
print(dp[n])

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
bf,bs = 0,0
fir = []
sec = []
fi1 = 1
se1 = 1
ans = n
ansf = n
for i in range(3):
  if a[i]>b[i]:
    if b[i] == 1:
      bs = 1
      if se1 < a[i]:
        se1 = a[i]
    else:     
      sec.append((a[i],b[i]))
  else:
    if a[i] == 1:
      bf = 1
      if fi1 < b[i]:
        fi1 = b[i]
    else:
      fir.append((b[i],a[i]))
sec += [(n,n),(n,n),(n,n)]
fir += [(n,n),(n,n),(n,n)]
for p in range(n//fir[0][1]+1):
  for q in range((n-p*fir[0][1])//fir[1][1]+1):
    rg = n - p*fir[0][1] - q*fir[1][1]
    if rg < 0:
      continue
    if bf:
      res = rg
      r = 0
    else:
      res = rg%fir[2][1]
      r = rg//fir[2][1]
    en = p*fir[0][0] + q*fir[1][0] + r*fir[2][0] + res*fi1
    if ansf < en:
      ansf = en
for p in range(ansf//sec[0][1]+1):
  for q in range((ansf-p*sec[0][1])//sec[1][1]+1):
    rg = ansf - p*sec[0][1] - q*sec[1][1]
    if rg < 0:
      continue
    if bs:
      res =  rg
      r = 0
    else:
      res = rg%sec[2][1]
      r = rg//sec[2][1]
    en = p*sec[0][0] + q*sec[1][0] + r*sec[2][0] + res*se1
    if ans < en:
      ans = en
print(ans)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
dp = [0]*(n+1)
fir = []
sec = []
for i in range(3):
  if a[i]>b[i]:
    sec.append((b[i],a[i]))
  else:
    fir.append((a[i],b[i]))
for i in range(1,n+1):
  dp[i] = i
  for x,y in fir:
    if i-x >= 0:
      dp[i] = max(dp[i],dp[i-x]+y)
n = dp[n]
dp = [0]*(n+1)
for i in range(1,n+1):
  dp[i] = i
  for x,y in sec:
    if i-x >= 0:
      dp[i] = max(dp[i],dp[i-x]+y)
print(dp[n])

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
fir = []
sec = []
ans = n
ansf = n
for i in range(3):
  if a[i]>b[i]:
    sec.append((a[i],b[i]))
    fir.append((n,n))
  else:
    sec.append((n,n))
    fir.append((b[i],a[i]))
for p in range(n//fir[0][1]+1):
  for q in range((n-p*fir[0][1])//fir[1][1]+1):
    rg = n - p*fir[0][1] - q*fir[1][1]
    if rg < 0:
      continue
    res = rg%fir[2][1]
    r = rg//fir[2][1]
    en = p*fir[0][0] + q*fir[1][0] + r*fir[2][0] + res
    if ansf < en:
      ansf = en
for p in range(ansf//sec[0][1]+1):
  for q in range((ansf-p*sec[0][1])//sec[1][1]+1):
    rg = ansf - p*sec[0][1] - q*sec[1][1]
    if rg < 0:
      continue
    res = rg%sec[2][1]
    r = rg//sec[2][1]
    en = p*sec[0][0] + q*sec[1][0] + r*sec[2][0] + res
    if ans < en:
      ans = en
print(ans)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
dp = [0]*(n+1)
fir = []
sec = []
for i in range(3):
  if a[i]>b[i]:
    sec.append((b[i],a[i]))
  else:
    fir.append((a[i],b[i]))
for i in range(1,n+1):
  dp[i] = i
  for x,y in fir:
    if i-x >= 0:
      dp[i] = max(dp[i],dp[i-x]+y)
n = dp[n]
dp = [0]*(n+1)
for i in range(1,n+1):
  dp[i] = i
  for x,y in sec:
    if i-x >= 0:
      dp[i] = max(dp[i],dp[i-x]+y)
print(dp[n])

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import permutations as pp
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
fir = []
sec = []
ans = n
ansf = n
for i in range(3):
  if a[i]>b[i]:
    sec.append((a[i],b[i]))
    fir.append((n,n))
  else:
    sec.append((n,n))
    fir.append((b[i],a[i]))
for p in range(n//fir[0][1]+1):
  for q in range((n-p*fir[0][1])//fir[1][1]+1):
    rg = n - p*fir[0][1] - q*fir[1][1]
    if rg < 0:
      continue
    res = rg%fir[2][1]
    r = rg//fir[2][1]
    en = p*fir[0][0] + q*fir[1][0] + r*fir[2][0] + res
    if ansf < en:
      ansf = en
for p in range(ansf//sec[0][1]+1):
  for q in range((ansf-p*sec[0][1])//sec[1][1]+1):
    rg = ansf - p*sec[0][1] - q*sec[1][1]
    if rg < 0:
      continue
    res = rg%sec[2][1]
    r = rg//sec[2][1]
    en = p*sec[0][0] + q*sec[1][0] + r*sec[2][0] + res
    if ans < en:
      ans = en
print(ans)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
dp = [0]*(n+1)
fir = []
sec = []
for i in range(3):
  if a[i]>b[i]:
    sec.append((b[i],a[i]))
  else:
    fir.append((a[i],b[i]))
for i in range(1,n+1):
  dp[i] = i
  for x,y in fir:
    if i-x >= 0:
      dp[i] = max(dp[i],dp[i-x]+y)
n = dp[n]
dp = [0]*(n+1)
for i in range(1,n+1):
  dp[i] = i
  for x,y in sec:
    if i-x >= 0:
      dp[i] = max(dp[i],dp[i-x]+y)
print(dp[n])