Problem p03599 (python)

Fixed submission:
# encoding: utf-8
A, B, C, D, E, F = map(int, input().split())

max_rate = 0
ans = (1, 0, 0, 0)
for i in range(30 // A + 1):
    for j in range(30 // B + 1):
        water = A * i + B * j
        # escape
        if water == 0: continue # avoid 0 div.
        elif 100 * water > F: break
        elif (F - 100 * water) / water < max_rate: continue
        # search (safer version)
        salt_upper = int(E * water)
        max_rate_tmp = 0
        ans_tmp = None
        for k in list(range(salt_upper // C + 1))[::-1]:
            for l in list(range(salt_upper // D + 1))[::-1]:
                salt = k * C + D * l
                # escape
                if salt == 0: continue
                elif salt / water > E: continue
                elif salt + 100 * water > F: continue
                # judge (only for max l)
                if salt / water > max_rate_tmp:
                    ans_tmp = (i, j, k, l)
                    max_rate_tmp = salt / water
                break
        # end loop
        if max_rate_tmp == 0: continue
        elif max_rate_tmp > max_rate:
            ans = ans_tmp
            max_rate = max_rate_tmp
        if max_rate == E: break
    if max_rate == E: break

salt = C * ans[2] + D * ans[3]
water = 100 * (A * ans[0] + B * ans[1])
    
print(salt + water, salt)

Buggy submission (verdict: Runtime Error):
# encoding: utf-8
A, B, C, D, E, F = map(int, input().split())

max_rate = -1
ans = None
for i in range(30 // A + 1):
    for j in range(30 // B + 1):
        water = A * i + B * j
        # escape
        if water == 0: continue
        elif 100 * water > F: break
        elif (F - 100 * water) / water < max_rate: continue
        # search (safer version)
        salt_upper = int(E * water)
        max_rate_tmp = 0
        ans_tmp = None
        for k in list(range(salt_upper // C + 1))[::-1]:
            for l in list(range(salt_upper // D + 1))[::-1]:
                salt = k * C + D * l
                # escape
                if salt / water > E: continue
                elif salt + 100 * water > F: continue
                # judge (only for max l)
                if salt / water > max_rate_tmp:
                    ans_tmp = (i, j, k, l)
                    max_rate_tmp = salt / water
                break
        # end loop
        if ans_tmp == None: continue
        if max_rate_tmp > max_rate:
            ans = ans_tmp
            max_rate = max_rate_tmp
        if max_rate == E: break
    if max_rate == E: break

salt = C * ans[2] + D * ans[3]
water = 100 * (A * ans[0] + B * ans[1])
    
print(salt + water, salt)

Buggy submission (verdict: Runtime Error):
# encoding: utf-8
A, B, C, D, E, F = map(int, input().split())

max_rate = -1
ans = None
for i in range(30 // A + 1):
    for j in range(30 // B + 1):
        water = A * i + B * j
        # escape
        if water == 0: continue
        elif 100 * water > F: break
        elif (F - 100 * water) / water < max_rate: continue
        # search
        salt_upper = int(E * water)
        max_rate_tmp = 0
        ans_tmp = None
        for l in list(range(salt_upper // D + 1))[::-1]:
            salt = ((salt_upper - l * D) // C) * C + l * D
            rate_tmp = salt / water
            if rate_tmp == E:
                if 100 * water + salt > F: continue
                ans_tmp = (i, j, ((salt_upper - l * D) // C), l)
                break
            elif rate_tmp > max_rate_tmp:
                if 100 * water + salt > F: continue
                ans_tmp = (i, j, ((salt_upper - l * D) // C), l)
                max_rate_tmp = rate_tmp
                continue
            else:
                continue
        # end loop
        if ans_tmp == None: continue
        if max_rate_tmp > max_rate:
            ans = ans_tmp
            max_rate = max_rate_tmp
        if max_rate == E: break
    if max_rate == E: break

salt = C * ans[2] + D * ans[3]
water = 100 * (A * ans[0] + B * ans[1])
    
print(salt + water, salt)