Buggy python submission (verdict: Runtime Error):
N,A,B,C = list(map(int, input().split()))
s = [input() for i in range(N)]

ns = [s[0]]
for v in s[1:]:
  if v!=ns[-1]:
    ns.append([v,1])
  else:
    ns[-1][1] += 1

ans = "No"
q = [[] for i in range(N+1)]
q[0] = [(A,B,C)]
q2 = [[] for i in range(N+1)]
q2[0] = [[]]
for i in range(N):
  while q[i]:
    a,b,c = q[i].pop(0)
    trace = q2[i].pop(0)
    if s[i] == "AB":
      if a-1>0 and (a-1,b+1,c) not in q[i+1]:
        q[i+1].append((a-1,b+1,c))
        q2[i+1].append(trace+["B"])
      if b-1>0 and (a+1,b-1,c) not in q[i+1]:
        q[i+1].append((a+1,b-1,c))
        q2[i+1].append(trace+["A"])
    if s[i] == "BC":
      if b-1>0 and (a,b-1,c+1) not in q[i+1]:
        q[i+1].append((a,b-1,c+1))
        q2[i+1].append(trace+["C"])
      if c-1>0 and (a,b+1,c-1) not in q[i+1]:
        q[i+1].append((a,b+1,c-1))
        q2[i+1].append(trace+["B"])
    if s[i] == "AC":
      if a-1>0 and (a-1,b,c+1) not in q[i+1]:
        q[i+1].append((a-1,b,c+1))
        q2[i+1].append(trace+["C"])
      if c-1>0 and (a+1,b,c-1) not in q[i+1]:
        q[i+1].append((a+1,b,c-1))
        q2[i+1].append(trace+["A"])
if q[N]:
  trace = q2[N].pop(0)
  ans = "Yes"


print(ans)
if ans =="Yes":
  for v in trace:
    print(v)
  

Fixed submission:
N,A,B,C = list(map(int, input().split()))
s = [input() for i in range(N)]

u = ["A","B","C"]
ss = ["BC","AC","AB"]
t = [A,B,C]
trace = []
ans = "Yes"
for i in range(N):
  idx = ss.index(s[i])
  if t[(idx+1)%3] == 0 and t[(idx+2)%3] == 0:
    ans = "No"
    break
  elif t[(idx+2)%3] == 0:
    t[(idx+1)%3] -= 1
    t[(idx+2)%3] += 1
    trace.append(u[(idx+2)%3])
  elif t[(idx+1)%3] == 0:
    t[(idx+1)%3] += 1
    t[(idx+2)%3] -= 1
    trace.append(u[(idx+1)%3])
  elif i+1<N and s[i+1] == ss[(idx+1)%3]:
    t[(idx+1)%3] -= 1
    t[(idx+2)%3] += 1
    trace.append(u[(idx+2)%3])
  else:
    t[(idx+1)%3] += 1
    t[(idx+2)%3] -= 1
    trace.append(u[(idx+1)%3])
print(ans)
if ans == "Yes":
  for v in trace:
    print(v)

Buggy python submission (verdict: Runtime Error):

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

ns = [s[0]]
for v in s[1:]:
  if v!=ns[-1]:
    ns.append([v,1])
  else:
    ns[-1][1] += 1

ans = "No"
q = [[] for i in range(N+1)]
q[0] = [(A,B,C,[])]
for i in range(N):
  while q[i]:
    a,b,c,trace = q[i].pop(0)
    if s[i] == "AB":
      if a-1>0:
        q[i+1].append((a-1,b+1,c,trace+["B"]))
      if b-1>0:
        q[i+1].append((a+1,b-1,c,trace+["A"]))
    if s[i] == "BC":
      if b-1>0:
        q[i+1].append((a,b-1,c+1,trace+["C"]))
      if c-1>0:
        q[i+1].append((a,b+1,c-1,trace+["B"]))
    if s[i] == "AC":
      if a-1>0:
        q[i+1].append((a-1,b,c+1,trace+["C"]))
      if c-1>0:
        q[i+1].append((a+1,b,c-1,trace+["A"]))
if q[N]:
  a,b,c,trace = q[N].pop(0)
  ans = "Yes"


print(ans)
if ans =="Yes":
  for v in trace:
    print(v)
  

Fixed submission:
N,A,B,C = list(map(int, input().split()))
s = [input() for i in range(N)]

u = ["A","B","C"]
ss = ["BC","AC","AB"]
t = [A,B,C]
trace = []
ans = "Yes"
for i in range(N):
  idx = ss.index(s[i])
  if t[(idx+1)%3] == 0 and t[(idx+2)%3] == 0:
    ans = "No"
    break
  elif t[(idx+2)%3] == 0:
    t[(idx+1)%3] -= 1
    t[(idx+2)%3] += 1
    trace.append(u[(idx+2)%3])
  elif t[(idx+1)%3] == 0:
    t[(idx+1)%3] += 1
    t[(idx+2)%3] -= 1
    trace.append(u[(idx+1)%3])
  elif i+1<N and s[i+1] == ss[(idx+1)%3]:
    t[(idx+1)%3] -= 1
    t[(idx+2)%3] += 1
    trace.append(u[(idx+2)%3])
  else:
    t[(idx+1)%3] += 1
    t[(idx+2)%3] -= 1
    trace.append(u[(idx+1)%3])
print(ans)
if ans == "Yes":
  for v in trace:
    print(v)