Problem p03599 (python)

Fixed submission:
A,B,C,D,E,F = map(int,input().split())
f = F//100

ans = [0,0]
per = -1
#水の量を100g単位ずつ全探索
for w in range(1,f+1):
    #そのような水の取り方があるか確認
    wt = 0
    for b in range(0,w+1,B):
        if (w-b)%A == 0:
            wt = 1
            break
    if wt == 0:
        continue

    water = w*100
    sugar = min(w*E,F-water)
    if ans[0] == 0:
        ans[0] = water
    st = 0
    #砂糖として考えられる量を大きい順に0gまでに全探索
    for s in range(sugar,0,-1):
        st = 0
        for d in range(0,s+1,D):
            if (s-d)%C == 0:
                st = 1
                break
        if st == 1:
            break
    if st == 1 and s+water <= F and s/(s+water) > per:
        ans = [s+water,s]
        per = s/(s+water)

print(' '.join(map(str,ans)))

Buggy submission (verdict: Runtime Error):
#pypyは内包表記使わない
def main():
    import sys
    #input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations
    #from itertools import accumulate, product
    from bisect import bisect_left,bisect_right
    from math import floor, ceil
    #from operator import itemgetter

    #mod = 1000000007

    A, B, C, D, E, F = map(int, input().split())
    rate = 0
    for i in range(30//A+1):
        for j in range(30//B+1):
            if i==0 and j==0:
                continue
            water = A*i + B*j
            if water*100 > F:
                continue
            limit = water * E
            for s in range(limit//C+1):
                sugar = limit - (limit-C*s)%D
                if water*100 + sugar > F:
                    continue
                if rate < sugar/(sugar+water*100):
                    rate = sugar/(sugar+water*100)
                    res_water = water * 100
                    res_sugar = sugar
    print(res_water+res_sugar, res_sugar)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#pypyは内包表記使わない
def main():
    import sys
    #input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations
    #from itertools import accumulate, product
    from bisect import bisect_left,bisect_right
    from math import floor, ceil
    #from operator import itemgetter

    #mod = 1000000007

    A, B, C, D, E, F = map(int, input().split())
    rate = 0
    for i in range(30//A+1):
        for j in range(30//B+1):
            if i==0 and j==0:
                continue
            water = A*i + B*j
            if water*100 > F:
                continue
            limit = water * E
            for s in range(limit//C+1):
                sugar = limit-((limit-C*s)%D)
                if sugar==0:
                    continue
                if water*100 + sugar > F:
                    continue
                if rate < sugar/(sugar+water):
                    rate = sugar/(sugar+water)
                    res_water = water * 100
                    res_sugar = sugar
    print(res_water+res_sugar, res_sugar)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#pypyは内包表記使わない
def main():
    import sys
    #input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations
    #from itertools import accumulate, product
    from bisect import bisect_left,bisect_right
    from math import floor, ceil
    #from operator import itemgetter

    #mod = 1000000007

    A, B, C, D, E, F = map(int, input().split())
    rate = 0
    for i in range(30//A+1):
        for j in range(30//B+1):
            if i==0 and j==0:
                continue
            water = A*i + B*j
            if water*100 > F:
                continue
            limit = water * E
            for s in range(limit//C+1):
                sugar = limit-((limit-C*s)%D)
                if water*100 + sugar > F:
                    continue
                if rate < sugar/(sugar+water):
                    rate = sugar/(sugar+water)
                    res_water = water * 100
                    res_sugar = sugar
    print(res_water+res_sugar, res_sugar)


if __name__ == '__main__':
    main()