Buggy python submission (verdict: Time Limit Exceeded):
def solve(n, a, b):
  ab = [(ai, bi) for ai, bi in zip(a, b) if ai < bi]
  dp = [[j for j in range(n+1)] for i in range(len(ab)+1)]
  if len(ab) == 0:
    return n
  if len(ab) == 1:
    a0, b0 = ab[0]
    return (n//a0)*b0 + n%a0
  if len(ab) == 2:
    a0, b0 = ab[0]; a1, b1 = ab[1]
    if a0 < a1: a0, b0, a1, b1 = a1, b1, a0, b0
    t = n // a0
    return max(((n-a0*i)//a1)*b1 + (n-a0*i)%a1 + i*b0 for i in range(t+1))
  else:
    dp = [j for j in range(n+1)]
    for ai, bi in ab:
      for j in range(ai, n+1):
        dp[j] = max(dp[j], dp[j-ai]+bi)
    return dp[-1]

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

N = solve(N, A, B)
N = solve(N, B, A)
print(N)

Fixed submission:
def solve(n, a, b):
  ab = [(ai, bi) for ai, bi in zip(a, b) if ai < bi]
  if len(ab) == 0:
    return n
  elif len(ab) == 1:
    a0, b0 = ab[0]
    return (n//a0)*b0 + n%a0
  elif len(ab) == 2:
    (a0, b0), (a1, b1) = ab
    if b0/a0 < b1/a1:
      a0, b0, a1, b1 = a1, b1, a0, b0
    return max(((n-a1*i)//a0)*b0 + (n-a1*i)%a0 + i*b1 for i in range(a0))
  else:
    dp = [j for j in range(n+1)]
    for ai, bi in ab:
      for j in range(ai, n+1):
        dp[j] = max(dp[j], dp[j-ai]+bi)
    return dp[-1]

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

N = solve(N, A, B)
N = solve(N, B, A)
print(N)

Buggy python submission (verdict: Runtime Error):
import numpy as np

def solve(n, a, b):
  ab = [(ai, bi) for ai, bi in zip(a, b) if ai < bi]
  dp = [[j for j in range(n+1)] for i in range(len(ab)+1)]
  if len(ab) == 0:
    return n
  if len(ab) == 1:
    a0, b0 = ab[0]
    return (n//a0)*b0 + n%a0
  if len(ab) == 2:
    a0, b0 = ab[0]; a1, b1 = ab[1]
    if a0 < a1: a0, b0, a1, b1 = a1, b1, a0, b0
    t = n // a0
    return max(((n-a0*i)//a1)*b1 + (n-a0*i)%a1 + i*bi for i in range(t+1))
  else:
    dp = np.arange(n+1)
    for ai, bi in ab:
      dp[ai:] = np.maximum(dp[ai:], dp[:-ai]+bi)
    return dp[-1]

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

N = solve(N, A, B)
N = solve(N, B, A)
print(N)

Fixed submission:
def solve(n, a, b):
  ab = [(ai, bi) for ai, bi in zip(a, b) if ai < bi]
  if len(ab) == 0:
    return n
  elif len(ab) == 1:
    a0, b0 = ab[0]
    return (n//a0)*b0 + n%a0
  elif len(ab) == 2:
    (a0, b0), (a1, b1) = ab
    if b0/a0 < b1/a1:
      a0, b0, a1, b1 = a1, b1, a0, b0
    return max(((n-a1*i)//a0)*b0 + (n-a1*i)%a0 + i*b1 for i in range(a0))
  else:
    dp = [j for j in range(n+1)]
    for ai, bi in ab:
      for j in range(ai, n+1):
        dp[j] = max(dp[j], dp[j-ai]+bi)
    return dp[-1]

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

N = solve(N, A, B)
N = solve(N, B, A)
print(N)

Buggy python submission (verdict: Time Limit Exceeded):
def solve(n, a, b):
  ab = [(ai, bi) for ai, bi in zip(a, b) if ai < bi]
  dp = [[j for j in range(n+1)] for i in range(len(ab)+1)]
  for i, (ai, bi) in enumerate(ab, 1):
    for j in range(n+1):
      if j >= ai:
        dp[i][j] = max(dp[i-1][j], dp[i][j-ai]+bi)
      else:
        dp[i][j] = dp[i-1][j]
  return dp[-1][-1]  

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

N = solve(N, A, B)
N = solve(N, B, A)
print(N)

Fixed submission:
def solve(n, a, b):
  ab = [(ai, bi) for ai, bi in zip(a, b) if ai < bi]
  if len(ab) == 0:
    return n
  elif len(ab) == 1:
    a0, b0 = ab[0]
    return (n//a0)*b0 + n%a0
  elif len(ab) == 2:
    (a0, b0), (a1, b1) = ab
    if b0/a0 < b1/a1:
      a0, b0, a1, b1 = a1, b1, a0, b0
    return max(((n-a1*i)//a0)*b0 + (n-a1*i)%a0 + i*b1 for i in range(a0))
  else:
    dp = [j for j in range(n+1)]
    for ai, bi in ab:
      for j in range(ai, n+1):
        dp[j] = max(dp[j], dp[j-ai]+bi)
    return dp[-1]

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

N = solve(N, A, B)
N = solve(N, B, A)
print(N)

Buggy python submission (verdict: Time Limit Exceeded):
def solve(n, a, b):
  ab = [(ai, bi) for ai, bi in zip(a, b) if ai < bi]
  dp = [[j for j in range(n+1)] for i in range(len(ab)+1)]
  for i, (ai, bi) in enumerate(ab, 1):
    for j in range(n+1):
      if j >= ai:
        dp[i][j] = max(dp[i-1][j], dp[i][j-ai]+bi)
      else:
        dp[i][j] = dp[i-1][j]
  return dp[-1][-1]  

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

N = solve(N, A, B)
N = solve(N, B, A)
print(N)

Fixed submission:
def solve(n, a, b):
  ab = [(ai, bi) for ai, bi in zip(a, b) if ai < bi]
  if len(ab) == 0:
    return n
  elif len(ab) == 1:
    a0, b0 = ab[0]
    return (n//a0)*b0 + n%a0
  elif len(ab) == 2:
    (a0, b0), (a1, b1) = ab
    if b0/a0 < b1/a1:
      a0, b0, a1, b1 = a1, b1, a0, b0
    return max(((n-a1*i)//a0)*b0 + (n-a1*i)%a0 + i*b1 for i in range(a0))
  else:
    dp = [j for j in range(n+1)]
    for ai, bi in ab:
      for j in range(ai, n+1):
        dp[j] = max(dp[j], dp[j-ai]+bi)
    return dp[-1]

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

N = solve(N, A, B)
N = solve(N, B, A)
print(N)