Problem p02692 (python)

Fixed submission:
N, A, B, C = map(int, input().split())
if A + B + C == 0:
    print('No')
elif A + B + C == 1:
    res = []
    for _ in range(N):
        s = input()
        if s == 'AB':
            if A == 0 and B == 0:
                print('No')
                break
            elif A == 0:
                A += 1
                B -= 1
                res.append('A')
            else:
                A -= 1
                B += 1
                res.append('B')
        elif s == 'AC':
            if A == 0 and C == 0:
                print('No')
                break
            elif A == 0:
                A += 1
                C -= 1
                res.append('A')
            else:
                A -= 1
                C += 1
                res.append('C')
        else:
            if B == 0 and C == 0:
                print('No')
                break
            elif B == 0:
                B += 1
                C -= 1
                res.append('B')
            else:
                B -= 1
                C += 1
                res.append('C')
    else:
        print('Yes')
        print('\n'.join(res))
else:
    res = []
    s = input()
    if (s == 'AB' and A == 0 and B == 0) or (s == 'AC' and A == 0 and C == 0) or (s == 'BC' and B == 0 and C == 0):
        print('No')
    else:
        for _ in range(N - 1):
            next = input()
            if s == 'AB':
                if A == 0:
                    B -= 1
                    A += 1
                    res.append('A')
                elif B == 0:
                    B += 1
                    A -= 1
                    res.append('B')
                else:
                    if next == 'AB':
                        A += 1
                        B -= 1
                        res.append('A')
                    elif next == 'AC':
                        A += 1
                        B -= 1
                        res.append('A')
                    else:
                        A -= 1
                        B += 1
                        res.append('B')
            elif s == 'AC':
                if A == 0:
                    C -= 1
                    A += 1
                    res.append('A')
                elif C == 0:
                    C += 1
                    A -= 1
                    res.append('C')
                else:
                    if next == 'AC':
                        A += 1
                        C -= 1
                        res.append('A')
                    elif next == 'AB':
                        A += 1
                        C -= 1
                        res.append('A')
                    else:
                        A -= 1
                        C += 1
                        res.append('C')
            else:
                if C == 0:
                    B -= 1
                    C += 1
                    res.append('C')
                elif B == 0:
                    B += 1
                    C -= 1
                    res.append('B')
                else:
                    if next == 'CB':
                        C += 1
                        B -= 1
                        res.append('C')
                    elif next == 'AC':
                        C += 1
                        B -= 1
                        res.append('C')
                    else:
                        C -= 1
                        B += 1
                        res.append('B')
            s = next
        if s == 'AB':
            if A == 0:
                A += 1
                B -= 1
                res.append('A')
            else:
                A -= 1
                B += 1
                res.append('B')
        elif s == 'AC':
            if A == 0:
                A += 1
                C -= 1
                res.append('A')
            else:
                A -= 1
                C += 1
                res.append('C')
        else:
            if B == 0:
                B += 1
                C -= 1
                res.append('B')
            else:
                B -= 1
                C += 1
                res.append('C')
        print('Yes')
        print('\n'.join(res))

Buggy submission (verdict: Runtime Error):
N, A, B, C = map(int, input().split())
if A + B + C == 0:
    print('No')
elif A + B + C == 1:
    res = []
    for _ in range(N):
        s = input()
        if s == 'AB':
            if A == 0 and B == 0:
                print('No')
                break
            elif A == 0:
                A += 1
                B -= 1
                res.append('A')
            else:
                A -= 1
                B += 1
                res.append('B')
        elif s == 'AC':
            if A == 0 and C == 0:
                print('No')
                break
            elif A == 0:
                A += 1
                C -= 1
                res.append('A')
            else:
                A -= 1
                C += 1
                res.append('C')
        else:
            if B == 0 and C == 0:
                print('No')
                breal
            elif B == 0:
                B += 1
                C -= 1
                res.append('B')
            else:
                B -= 1
                C += 1
                res.append('C')
    else:
        print('Yes')
        print('\n'.join(res))
else:
    res = []
    s = input()
    if (s == 'AB' and A == 0 and B == 0) or (s == 'AC' and A == 0 and C == 0) or (s == 'BC' and B == 0 and C == 0):
        print('No')
    else:
        for _ in range(N - 1):
            next = input()
            if s == 'AB':
                if A == 0:
                    B -= 1
                    A += 1
                    res.append('A')
                elif B == 0:
                    B += 1
                    A -= 1
                    res.append('B')
                else:
                    if next == 'AB':
                        A += 1
                        B -= 1
                        res.append('A')
                    elif next == 'AC':
                        A += 1
                        B -= 1
                        res.append('A')
                    else:
                        A -= 1
                        B += 1
                        res.append('B')
            if s == 'AC':
                if A == 0:
                    C -= 1
                    A += 1
                    res.append('A')
                elif C == 0:
                    C += 1
                    A -= 1
                    res.append('C')
                else:
                    if next == 'AC':
                        A += 1
                        C -= 1
                        res.append('C')
                    elif next == 'AB':
                        A += 1
                        C -= 1
                        res.append('A')
                    else:
                        A -= 1
                        C += 1
                        res.append('C')
            if s == 'BC':
                if C == 0:
                    B -= 1
                    C += 1
                    res.append('C')
                elif B == 0:
                    B += 1
                    C -= 1
                    res.append('B')
                else:
                    if next == 'CB':
                        C += 1
                        B -= 1
                        res.append('C')
                    elif next == 'AC':
                        C += 1
                        B -= 1
                        res.append('C')
                    else:
                        C -= 1
                        B += 1
                        res.append('B')
            s = next
        if s == 'AB':
            if A == 0:
                A += 1
                B -= 1
                res.append('A')
            else:
                A -= 1
                B += 1
                res.append('B')
        elif s == 'AC':
            if A == 0:
                A += 1
                C -= 1
                res.append('A')
            else:
                A -= 1
                C += 1
                res.append('C')
        else:
            if B == 0:
                B += 1
                C -= 1
                res.append('B')
            else:
                B -= 1
                C += 1
                res.append('C')
        print('Yes')
        print('\n'.join(res))