Problem p03008 (python)

Fixed submission:
N = int(input())
A = tuple(map(int, input().split()))
B = tuple(map(int, input().split()))

def calc(A, B, N):
    M = N
    if len([i for i, (a, b) in enumerate(zip(A, B)) if a < b]) == 1:
        i = [i for i, (a, b) in enumerate(zip(A, B)) if a < b][0]

        for k in range(N // A[i] + 1):
            D = N - k * A[i]
            if D < 0:
                continue

            D += k * B[i]
            M = max(M, D)
    elif len([i for i, (a, b) in enumerate(zip(A, B)) if a < b]) == 2:
        i, j = [i for i, (a, b) in enumerate(zip(A, B)) if a < b]

        for k in range(N // A[i] + 1):
            D = N - k * A[i]
            l, D = divmod(D, A[j])

            D += k * B[i]
            D += l * B[j]

            M = max(M, D)
    elif len([i for i, (a, b) in enumerate(zip(A, B)) if a < b]) == 3:
        for g in range(N // A[0] + 1):
            for s in range(N // A[1] + 1):
                D = N - g * A[0] - s * A[1]

                if D < 0:
                    break

                q, r = divmod(D, A[2])

                D += g * B[0]
                D += s * B[1]

                r += g * B[0]
                r += s * B[1]
                r += q * B[2]

                M = max(M, D, r)
    return M

print(calc(B, A, calc(A, B, N)))

Buggy submission (verdict: Runtime Error):
N = int(input())
A = tuple(map(int, input().split()))
B = tuple(map(int, input().split()))

if len([i for i, (a, b) in enumerate(zip(A, B)) if a != b]) == 0:
    print(N)
    exit()

if len([i for i, (a, b) in enumerate(zip(A, B)) if a > b]) == 1:
    M = N
    i = [i for i, (a, b) in enumerate(zip(A, B)) if a > b][0]

    for k in range(N // A[i] + 1):
        D = N - k * A[i]
        D += k * B[i]
        M = max(M, D)

    i, j = [i for i, (a, b) in enumerate(zip(A, B)) if not a > b]
    ans = M
    for k in range(N // B[i] + 1):
        D = M - k * B[i]
        l, D = divmod(D, B[j])

        D += k * A[i]
        D += l * A[j]

        ans = max(ans, D)

    print(ans)
    exit()

if len([i for i, (a, b) in enumerate(zip(A, B)) if a > b]) == 2:
    M = N

    i, j = [i for i, (a, b) in enumerate(zip(A, B)) if not a > b]
    for k in range(N // B[i] + 1):
        D = N - k * B[i]
        l, D = divmod(D, B[j])

        D += k * A[i]
        D += l * A[j]

        M = max(M, D)

    i = [i for i, (a, b) in enumerate(zip(A, B)) if a > b][0]
    ans = M
    for k in range(N // A[i] + 1):
        D = M - k * A[i]
        D += k * B[i]
        ans = max(ans, D)

    print(ans)
    exit()

def calc(fr, to):
    ret = N
    for g in range(N // fr[0] + 1):
        for s in range(N // fr[1] + 1):
            D = N - g * fr[0] - s * fr[1]

            if D < 0:
                break

            q, r = divmod(D, fr[2])

            D += g * to[0]
            D += s * to[1]

            r += g * to[0]
            r += s * to[1]
            r += q * to[2]

            ret = max(ret, D, r)
    return ret

print(calc(A, B))

Buggy submission (verdict: Runtime Error):
N = int(input())
A = tuple(map(int, input().split()))
B = tuple(map(int, input().split()))

if len([0 for a, b in zip(A, B) if a != b]) == 0:
    print(N)
    exit()

if len([0 for a, b in zip(A, B) if a > b]) == 1:
    M = N
    i = [i for i, (a, b) in zip(A, B) if a > b][0]

    for k in range(N // A[i] + 1):
        D = N - k * A[i]
        D += k * B[i]
        M = max(M, D)

    i, j = [i for i, (a, b) in zip(A, B) if not a > b]
    ans = M
    for k in range(N // B[i] + 1):
        D = M - k * B[i]
        l, D = divmod(D, B[j])

        D += k * A[i]
        D += l * A[j]

        ans = max(ans, D)

    print(ans)
    exit()

if len([0 for a, b in zip(A, B) if a > b]) == 2:
    M = N

    i, j = [i for i, (a, b) in zip(A, B) if not a > b]
    for k in range(N // B[i] + 1):
        D = N - k * B[i]
        l, D = divmod(D, B[j])

        D += k * A[i]
        D += l * A[j]

        M = max(M, D)

    i = [i for i, (a, b) in zip(A, B) if a > b][0]
    ans = M
    for k in range(N // A[i] + 1):
        D = M - k * A[i]
        D += k * B[i]
        ans = max(ans, D)

    print(ans)
    exit()

def calc(fr, to):
    ret = N
    for g in range(N // fr[0] + 1):
        for s in range(N // fr[1] + 1):
            D = N - g * fr[0] - s * fr[1]

            if D < 0:
                break

            q, r = divmod(D, fr[2])

            D += g * to[0]
            D += s * to[1]

            r += g * to[0]
            r += s * to[1]
            r += q * to[2]

            ret = max(ret, D, r)
    return ret

print(calc(A, B))