Buggy python submission (verdict: Time Limit Exceeded):
def main():
    from collections import deque
    import sys
    input = sys.stdin.readline

    n, a, b, c = map(int, input().split())
    s = [input().rstrip() for _ in range(n)]

    dq = deque()
    dq.append((a, b, c, '', 0))

    ans = []
    while dq:
        A, B, C, ops, cur = dq.popleft()
        # print(f'{A=},{B=},{C=},{ops=},{cur=}')
        
        if cur == n:
            ans = list(ops)
            break

        op = s[cur]
        if op == 'AB':
            if A == B == 0:
                continue
            if A >= B:
                dq.append((A - 1, B + 1, C, ops + 'B', cur + 1))
            if A <= B:
                dq.append((A + 1, B - 1, C, ops + 'A', cur + 1))

        elif op == 'AC':
            if A == C == 0:
                continue
            if A >= C:
                dq.append((A - 1, B, C + 1, ops + 'C', cur + 1))
            if A <= C:
                dq.append((A + 1, B, C - 1, ops + 'A', cur + 1))

        else:
            if B == C == 0:
                continue
            if B >= C:
                dq.append((A, B - 1, C + 1, ops + 'C', cur + 1))
            if B <= C:
                dq.append((A, B + 1, C - 1, ops + 'B', cur + 1))

    if ans:
        print('Yes')
        print(*ans, sep='\n')
    else:
        print('No')


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    import sys
    input = sys.stdin.readline

    N, A, B, C = map(int, input().split())
    s = [input().rstrip() for _ in range(N)]

    ans = []
    for i, op in enumerate(s):
        # print(op)
        if op == 'AB':
            if A == B == 0:
                print('No')
                return

            if A > B:
                A -= 1
                B += 1
                ans.append('B')
            else:
                if A == B and i < N - 1:
                    if s[i + 1] == 'AC' and A == 1:
                        A += 1
                        B -= 1
                        ans.append('A')
                        continue
                    elif s[i + 1] == 'BC' and B == 1:
                        A -= 1
                        B += 1
                        ans.append('B')
                        continue
                A += 1
                B -= 1
                ans.append('A')
        elif op == 'AC':
            if A == C == 0:
                print('No')
                return

            if A > C:
                A -= 1
                C += 1
                ans.append('C')
            else:
                if A == C and i < N - 1:
                    if s[i + 1] == 'AB' and A == 1:
                        A += 1
                        C -= 1
                        ans.append('A')
                        continue
                    elif s[i + 1] == 'BC' and C == 1:
                        A -= 1
                        C += 1
                        ans.append('C')
                        continue
                A += 1
                C -= 1
                ans.append('A')
        else:
            if B == C == 0:
                print('No')
                return

            if B > C:
                B -= 1
                C += 1
                ans.append('C')
            else:
                if B == C and i < N - 1:
                    if s[i + 1] == 'AB' and B == 1:
                        B += 1
                        C -= 1
                        ans.append('B')
                        continue
                    elif s[i + 1] == 'AC' and C == 1:
                        B -= 1
                        C += 1
                        ans.append('C')
                        continue
                B += 1
                C -= 1
                ans.append('B')
        # print(A, B, C)

    print('Yes')
    print(*ans, sep='\n')


if __name__ == '__main__':
    main()

# import sys
# input = sys.stdin.readline
# 
# sys.setrecursionlimit(10 ** 7)
# 
# (int(x)-1 for x in input().split())
# rstrip()
#
# def binary_search(*, ok, ng, func):
#     while abs(ok - ng) > 1:
#         mid = (ok + ng) // 2
#         if func(mid):
#             ok = mid
#         else:
#             ng = mid
#     return ok