Problem p03599 (python)

Fixed submission:
import math
A, B, C, D, E, F = map(int,input().split())
w_max = 30

def reduce(x): #約分
  #print(x)
  d = math.gcd(x[0], x[1])
  return (x[0] // d, x[1] // d)

def is_valid(s, w): #砂糖s、水100w
  return s <= w * E and s + 100*w <= F

ratios = set()
recipe = {}

Waters = [] 
for a in range(0, w_max+1, A):
  for b in range(0, w_max+1, B):
    if (a == 0 and b == 0) or a + b > F:
      continue
    Waters.append(a+b)
Waters.sort()

s_max = 0
for x in Waters:
  s_max = max(s_max, min(x*E,F-100*x))

Sugars = [] 
for c in range(0, s_max+1, C):
  for d in range(0, s_max+1, D):
    Sugars.append(c+d)
Sugars.sort()

i = 0
ascend = True
for w in Waters:
  while i >= 0 and not is_valid(Sugars[i], w):
    i -= 1
  while 0 <= i < len(Sugars) and is_valid(Sugars[i], w):
    i += 1
  i -= 1
  if i >= 0:
    ratios.add(reduce((Sugars[i], w)))
    recipe[reduce((Sugars[i], w))] = (Sugars[i], w)

ans = recipe[max(ratios, key = lambda x: float(x[0]/x[1]))]
print(ans[1]*100 + ans[0], ans[0])

Buggy submission (verdict: Runtime Error):
import math
A, B, C, D, E, F = map(int,input().split())
w_max = 30

def reduce(x): #約分
  #print(x)
  d = math.gcd(x[0], x[1])
  return (x[0] // d, x[1] // d)

def is_valid(s, w): #砂糖s、水100w
  return s <= w * E and s + 100*w <= F

ratios = set()

Waters = [] 
for a in range(0, w_max+1, A):
  for b in range(0, w_max+1, B):
    if a == 0 and b == 0:
      continue
    Waters.append(a+b)
Waters.sort()

s_max = 0
for x in Waters:
  s_max = max(s_max, min(x*E,F-100*x))

Sugars = [] 
for c in range(0, s_max+1, C):
  for d in range(0, s_max+1, D):
    Sugars.append(c+d)
Sugars.sort()

i = 0
ascend = True
for w in Waters:
  if Sugars[i]*100 + w > F:
    ascend = False
  if ascend:
    while i < len(Sugars) and is_valid(Sugars[i], w):
      i += 1
    i -= 1
  else:
    while i >= 0 and not is_valid(Sugars[i], w):
      i -= 1
  ratios.add(reduce((Sugars[i], w)))

ans = max(ratios, key = lambda x: float(x[0]/x[1]))
print(ans[1]*100 + ans[0], ans[0])

Buggy submission (verdict: Runtime Error):
import math
A, B, C, D, E, F = map(int,input().split())
w_max = 30

def reduce(x): #約分
  #print(x)
  d = math.gcd(x[0], x[1])
  return (x[0] // d, x[1] // d)

def is_valid(s, w): #砂糖s、水100w
  return s <= w * E and s + 100*w <= F

ratios = set()

Waters = [] 
for a in range(0, w_max, A):
  for b in range(0, w_max, B):
    if a == 0 and b == 0:
      continue
    Waters.append(a+b)
Waters.sort()

s_max = 0
for x in Waters:
  s_max = max(s_max, min(x*E,F-100*x))

Sugars = [] 
for c in range(0, s_max, C):
  for d in range(0, s_max, D):
    Sugars.append(c+d)
Sugars.sort()

i = 0
ascend = True
for w in Waters:
  if Sugars[i]*100 + w > F:
    ascend = False
  if ascend:
    while i < len(Sugars) and is_valid(Sugars[i], w):
      i += 1
    i -= 1
  else:
    while i >= 0 and not is_valid(Sugars[i], w):
      i -= 1
  ratios.add(reduce((Sugars[i], w)))

ans = max(ratios, key = lambda x: float(x[0]/x[1]))
print(ans[1]*100 + ans[0], ans[0])

Buggy submission (verdict: Runtime Error):
import math
A, B, C, D, E, F = map(int,input().split())
w_max = 30

def reduce(x): #約分
  #print(x)
  d = math.gcd(x[0], x[1])
  return (x[0] // d, x[1] // d)

def is_valid(s, w): #砂糖s、水100w
  return s <= w * E and s + 100*w <= F

ratios = set()

Waters = [] 
for a in range(0, w_max, A):
  for b in range(0, w_max, B):
    if a == 0 and b == 0:
      continue
    Waters.append(a+b)
Waters.sort()

s_max = 0
for x in Waters:
  s_max = max(s_max, min(x*E,F-100*x))

Sugars = [] 
for c in range(0, s_max, C):
  for d in range(0, s_max, D):
    Sugars.append(c+d)
Sugars.sort()

i = 0
ascend = True
for w in Waters:
  if Sugars[i]*100 + w > F:
    ascend = False
  if ascend:
    while i < len(Sugars) and is_valid(Sugars[i], w):
      i += 1
    i -= 1
  else:
    while i >= 0 and not is_valid(Sugars[i], w):
      i -= 1
  ratios.add(reduce((Sugars[i], w)))

ans = max(ratios, key = lambda x: float(x[0]/x[1]))
print(ans[1]*100 + ans[0], ans[0])