Problem p03008 (python)

Fixed submission:
def calc():
  n=int(input())
  arr=[list(map(int,input().split())) for _ in range(2)]
  move1=[]
  move2=[]
  ans=n
  for i in range(3):
    if arr[0][i]-arr[1][i]>0:
      move2+=[i]
    if arr[1][i]-arr[0][i]>0:
      move1+=[i]
  if len(move1)==1:
    idx=move1[0]
    ans=arr[1][idx]*(ans//arr[0][idx])+ans%arr[0][idx]
  elif len(move1)==2:
    idx1=move1[0]
    idx2=move1[1]
    s1=arr[0][idx1]
    s2=arr[0][idx2]
    g1=arr[1][idx1]
    g2=arr[1][idx2]
    d1=g1-s1
    d2=g2-s2
    ta=max(ans+i*d1+((ans-i*s1)//s2)*d2 for i in range(ans//s1+1))
    ans=ta
  elif len(move1)==3:
    idx1=move1[0]
    idx2=move1[1]
    idx3=move1[2]
    s1=arr[0][idx1]
    s2=arr[0][idx2]
    s3=arr[0][idx3]
    g1=arr[1][idx1]
    g2=arr[1][idx2]
    g3=arr[1][idx3]
    if g1/s1<g2/s2:
      g1,g2=g2,g1
      s1,s2=s2,s1
    if g1/s1<g3/s3:
      g1,g3=g3,g1
      s1,s3=s3,s1
    if g2/s2<g3/s3:
      g2,g3=g3,g2
      s2,s3=s3,s2
    d1=g1-s1
    d2=g2-s2
    d3=g3-s3
    if ans//s1<=1000:
      pos=0
    else:
      pos=(ans//s1)//2
    ta=max(ans+i*d1+j*d2+((ans-i*s1-j*s2)//s3)*d3 for i in range(pos,ans//s1+1) for j in range((ans-i*s1)//s2+1))
    ans=ta
  if len(move2)==1:
    idx=move2[0]
    ans=arr[0][idx]*(ans//arr[1][idx])+ans%arr[1][idx]
  elif len(move2)==2:
    idx1=move2[0]
    idx2=move2[1]
    s1=arr[1][idx1]
    s2=arr[1][idx2]
    g1=arr[0][idx1]
    g2=arr[0][idx2]
    if g1/s1<g2/s2:
      g1,g2=g2,g1
      s1,s2=s2,s1
    d1=g1-s1
    d2=g2-s2
    if ans//s1<=1000:
      pos=0
    else:
      pos=(ans//s1)//2
    ta=max(ans+i*d1+((ans-i*s1)//s2)*d2 for i in range(pos,ans//s1+1))
    ans=ta
  elif len(move2)==3:
    idx1=move2[0]
    idx2=move2[1]
    idx3=move2[2]
    s1=arr[1][idx1]
    s2=arr[1][idx2]
    s3=arr[1][idx3]
    g1=arr[0][idx1]
    g2=arr[0][idx2]
    g3=arr[0][idx3]
    if g1/s1<g2/s2:
      g1,g2=g2,g1
      s1,s2=s2,s1
    if g1/s1<g3/s3:
      g1,g3=g3,g1
      s1,s3=s3,s1
    if g2/s2<g3/s3:
      g2,g3=g3,g2
      s2,s3=s3,s2
    d1=g1-s1
    d2=g2-s2
    d3=g3-s3
    if ans//s1<=1000:
      pos=0
    else:
      pos=(ans//s1)//2
    ta=max(ans+i*d1+j*d2+((ans-i*s1-j*s2)//s3)*d3 for i in range(pos,ans//s1+1) for j in range((ans-i*s1)//s2+1))
    ans=ta
  print(ans)
calc()

Buggy submission (verdict: Runtime Error):
def calc():
  n=int(input())
  arr=[list(map(int,input().split())) for _ in range(2)]
  move1=[]
  move2=[]
  ans=n
  for i in range(3):
    if arr[0][i]-arr[1][i]>0:
      move2+=[i]
    if arr[1][i]-arr[0][i]>0:
      move1+=[i]
  if len(move1)==1:
    idx=move1[0]
    ans=arr[1][idx]*(ans//arr[0][idx])+ans%arr[0][idx]
  elif len(move1)==2:
    idx1=move1[0]
    idx2=move1[1]
    s1=arr[0][idx1]
    s2=arr[0][idx2]
    g1=arr[1][idx1]
    g2=arr[1][idx2]
    ta=max(ans+i*(g1-s1)+((ans-i*s1)//s2)*(g2-s2) for i in range(ans//s1+1))
    ans=ta
  elif len(move1)==3:
    idx1=move1[0]
    idx2=move1[1]
    idx3=move1[2]
    s1=arr[0][idx1]
    s2=arr[0][idx2]
    s3=arr[0][idx3]
    g1=arr[1][idx1]
    g2=arr[1][idx2]
    g3=arr[1][idx3]
    ta=max(ans+i*(g1-s1)+j*(g2-s2)+((ans-i*s1-j*s2)//s3)*(g3-s3) for i in range(ans//s1+1) for j in range((ans-i*s1)//s2+1)
    ans=ta
  if len(move2)==1:
    idx=move2[0]
    ans=arr[0][idx]*(ans//arr[1][idx])+ans%arr[1][idx]
  elif len(move2)==2:
    idx1=move2[0]
    idx2=move2[1]
    s1=arr[1][idx1]
    s2=arr[1][idx2]
    g1=arr[0][idx1]
    g2=arr[0][idx2]
    ta=max(ans+i*(g1-s1)+((ans-i*s1)//s2)*(g2-s2) for i in range(ans//s1+1))
    ans=ta
  elif len(move2)==3:
    idx1=move2[0]
    idx2=move2[1]
    idx3=move2[2]
    s1=arr[1][idx1]
    s2=arr[1][idx2]
    s3=arr[1][idx3]
    g1=arr[0][idx1]
    g2=arr[0][idx2]
    g3=arr[0][idx3]
    ta=max(ans+i*(g1-s1)+j*(g2-s2)+((ans-i*s1-j*s2)//s3)*(g3-s3) for i in range(ans//s1+1) for j in range((ans-i*s1)//s2+1)
    ans=ta
  print(ans)
calc()