Problem p02692 (python)

Fixed submission:
def main():
    N, A, B, C = map(int, input().split())
    S = [input() for _ in range(N)]
    res = []
    if A + B + C == 2:
        for i in range(N):
            s = S[i]
            if s == "AB":
                if A == 0 and B == 0:
                    print("No")
                    return 1
                if A > B:
                    A -= 1
                    B += 1
                    res.append("B")
                elif A < B:
                    A += 1
                    B -= 1
                    res.append("A")
                else:
                    if i == N - 1:
                        res.append("A")
                        break
                    elif S[i + 1] == "BC":
                        A -= 1
                        B += 1
                        res.append("B")
                    else:
                        A += 1
                        B -= 1
                        res.append("A")
            if s == "AC":
                if A == 0 and C == 0:
                    print("No")
                    return 1
                if A > C:
                    A -= 1
                    C += 1
                    res.append("C")
                elif A < C:
                    A += 1
                    C -= 1
                    res.append("A")
                else:
                    if i == N - 1:
                        res.append("A")
                        break
                    elif S[i + 1] == "BC":
                        A -= 1
                        C += 1
                        res.append("C")
                    else:
                        A += 1
                        C -= 1
                        res.append("A")
            if s == "BC":
                if B == 0 and C == 0:
                    print("No")
                    return 1
                if B > C:
                    B -= 1
                    C += 1
                    res.append("C")
                elif B < C:
                    B += 1
                    C -= 1
                    res.append("B")
                else:
                    if i == N - 1:
                        res.append("B")
                        break
                    elif S[i + 1] == "AC":
                        B -= 1
                        C += 1
                        res.append("C")
                    else:
                        B += 1
                        C -= 1
                        res.append("B")
        print("Yes")
        print("\n".join(map(str, res)))
        return 0
    if A == 0 and B == 0:
        if S[0] == "AB":
            print("No")
            return 1
    if A == 0 and C == 0:
        if S[0] == "AC":
            print("No")
            return 1
    if C == 0 and B == 0:
        if S[0] == "BC":
            print("No")
            return 1

    for s in S:
        if s == "AB":
            if A == 0 and B == 0:
                print("No")
                return 1
            if A >= B:
                A -= 1
                B += 1
                res.append("B")
            else:
                A += 1
                B -= 1
                res.append("A")
        elif s == "AC":
            if A == 0 and C == 0:
                print("No")
                return 1
            if A >= C:
                A -= 1
                C += 1
                res.append("C")
            else:
                A += 1
                C -= 1
                res.append("A")

        elif s == "BC":
            if B == 0 and C == 0:
                print("No")
                return 1
            if B >= C:
                B -= 1
                C += 1
                res.append("C")
            else:
                B += 1
                C -= 1
                res.append("B")
        else:
            print("ERROR")
    print("Yes")
    print("\n".join(map(str, res)))
    return 0


main()

Buggy submission (verdict: Runtime Error):
def main():
    N, A, B, C = map(int, input().split())
    S = [input() for _ in range(N)]
    res = []
    if A + B + C == 2:
        for i in range(N):
            s = S[i]
            if s == "AB":
                if A == 0 and B == 0:
                    print("No")
                    return 1
                if A > B:
                    A -= 1
                    B += 1
                    res.append("B")
                elif A < B:
                    A += 1
                    B -= 1
                    res.append("A")
                else:
                    if S == N - 1:
                        res.append("A")
                        break
                    elif S[i + 1] == "BC":
                        A -= 1
                        B += 1
                        res.append("B")
                    else:
                        A += 1
                        B -= 1
                        res.append("A")
            if s == "AC":
                if A == 0 and C == 0:
                    print("No")
                    return 1
                if A > C:
                    A -= 1
                    C += 1
                    res.append("C")
                elif A < C:
                    A += 1
                    C -= 1
                    res.append("A")
                else:
                    if S == N - 1:
                        res.append("A")
                        break
                    elif S[i + 1] == "BC":
                        A -= 1
                        C += 1
                        res.append("C")
                    else:
                        A += 1
                        C -= 1
                        res.append("A")
            if s == "BC":
                if B == 0 and C == 0:
                    print("No")
                    return 1
                if B > C:
                    B -= 1
                    C += 1
                    res.append("C")
                elif B < C:
                    B += 1
                    C -= 1
                    res.append("B")
                else:
                    if S == N - 1:
                        res.append("B")
                        break
                    elif S[i + 1] == "AC":
                        B -= 1
                        C += 1
                        res.append("C")
                    else:
                        B += 1
                        C -= 1
                        res.append("B")
        print("Yes")
        print("\n".join(map(str, res)))
        return 0
    if A == 0 and B == 0:
        if S[0] == "AB":
            print("No")
            return 1
    if A == 0 and C == 0:
        if S[0] == "AC":
            print("No")
            return 1
    if C == 0 and B == 0:
        if S[0] == "BC":
            print("No")
            return 1

    for s in S:
        if s == "AB":
            if A == 0 and B == 0:
                print("No")
                return 1
            if A >= B:
                A -= 1
                B += 1
                res.append("B")
            else:
                A += 1
                B -= 1
                res.append("A")
        elif s == "AC":
            if A == 0 and C == 0:
                print("No")
                return 1
            if A >= C:
                A -= 1
                C += 1
                res.append("C")
            else:
                A += 1
                C -= 1
                res.append("A")

        elif s == "BC":
            if B == 0 and C == 0:
                print("No")
                return 1
            if B >= C:
                B -= 1
                C += 1
                res.append("C")
            else:
                B += 1
                C -= 1
                res.append("B")
        else:
            print("ERROR")
    print("Yes")
    print("\n".join(map(str, res)))
    return 0


main()