Buggy python submission (verdict: Runtime Error):
# input
N, A, B, C = map(int, input().split())
S = [input() for _ in range(N)]
V = {"A":A, "B":B, "C":C}

# process
a, b, c = A, B, C
op = ""
for i, s in enumerate(S):
    s0 = s[0]
    s1 = s[1]
    if V[s0] + V[s1] == 0:
        break
    
    x, y = s0, s1
    if V[s1] == 0 or (i < N-1 and s1 in S[i+1]):
        x, y = s1, s0

    V[x] += 1
    V[y] -= 1
    op += x

    print("{} {} {}".format(s, V, op))

# output
if len(op) != N:
    print("No")
else:
    print("Yes")
    [print(x) for x in op]


Fixed submission:
# input
N, A, B, C = map(int, input().split())
S = [input() for _ in range(N)]

# process
V = {"A":A, "B":B, "C":C}
S.append("XX")
op = ""
for i in range(N):
    s0, s1 = S[i]
    if V[s0] + V[s1] == 0:
        break
    
    x, y = s0, s1
    if V[s1] == 0 or (V[s0] != 0 and s1 in S[i+1]):
        x, y = s1, s0

    V[x] += 1
    V[y] -= 1
    op += x

# output
if len(op) != N:
    print("No")
else:
    print("Yes")
    [print(x) for x in op]

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

# input
N, A, B, C = map(int, input().split())
S = []
for _ in range(N):
    S.append(input())

# process
dq = deque()
dq.append((A, B, C, ""))

for i, s in enumerate(S):
    l = len(dq)
    for _ in range(l):
        a, b, c, op = list(dq.popleft())

        if s == "AB":
            if b != 0:
                dq.append((a+1, b-1, c, op+"A"))
            if a != 0:
                dq.append((a-1, b+1, c, op+"B"))
        elif s == "BC":
            if c != 0:
                dq.append((a, b+1, c-1, op+"B"))
            if b != 0:
                dq.append((a, b-1, c+1, op+"C"))
        else:
            if c != 0:
                dq.append((a+1, b, c-1, op+"A"))
            if a != 0:
                dq.append((a-1, b, c+1, op+"C"))

# output
if len(dq) == 0:
    print("No")
else:
    print("Yes")
    _, _, _, op = dq[0]
    [print(x) for x in op]


Fixed submission:
# input
N, A, B, C = map(int, input().split())
S = [input() for _ in range(N)]

# process
V = {"A":A, "B":B, "C":C}
S.append("XX")
op = ""
for i in range(N):
    s0, s1 = S[i]
    if V[s0] + V[s1] == 0:
        break
    
    x, y = s0, s1
    if V[s1] == 0 or (V[s0] != 0 and s1 in S[i+1]):
        x, y = s1, s0

    V[x] += 1
    V[y] -= 1
    op += x

# output
if len(op) != N:
    print("No")
else:
    print("Yes")
    [print(x) for x in op]

Buggy python submission (verdict: Time Limit Exceeded):
import copy
from collections import deque

# input
N, A, B, C = map(int, input().split())
S = []
for _ in range(N):
    S.append(input())

# process
dq = deque()
dq.append((A, B, C, []))

for i, s in enumerate(S):
    l = len(dq)
    for _ in range(l):
        a, b, c, op = list(dq.popleft())
        
        if a < 0 or b < 0 or c < 0:
            continue

        op1 = op
        op2 = copy.copy(op)
        if s == "AB":
            op1.append("A")
            op2.append("B")
            dq.append((a+1, b-1, c, op1))
            dq.append((a-1, b+1, c, op2))
        elif s == "BC":
            op1.append("B")
            op2.append("C")
            dq.append((a, b+1, c-1, op1))
            dq.append((a, b-1, c+1, op2))
        else:
            op1.append("A")
            op2.append("C")
            dq.append((a+1, b, c-1, op1))
            dq.append((a-1, b, c+1, op2))

# output
operation = []
for a, b, c, op in dq:
    if a >= 0 and b >= 0 and c >= 0:
        operation = op
        break

if operation == []:
    print("No")
else:
    print("Yes")
    [print(x) for x in operation]


Fixed submission:
# input
N, A, B, C = map(int, input().split())
S = [input() for _ in range(N)]

# process
V = {"A":A, "B":B, "C":C}
S.append("XX")
op = ""
for i in range(N):
    s0, s1 = S[i]
    if V[s0] + V[s1] == 0:
        break
    
    x, y = s0, s1
    if V[s1] == 0 or (V[s0] != 0 and s1 in S[i+1]):
        x, y = s1, s0

    V[x] += 1
    V[y] -= 1
    op += x

# output
if len(op) != N:
    print("No")
else:
    print("Yes")
    [print(x) for x in op]