Buggy python submission (verdict: Time Limit Exceeded):
n,a,b,c = map(int,input().split())
now = [[a,b,c,[]]]
for _ in range(n):
  q = input()
  tmp = []
  if q == 'AB':
    for x in now:
      a,b,c,his = x
      if a > 0:
        tmp.append([a-1,b+1,c,his+['B']])
      if b > 0:
        tmp.append([a+1,b-1,c,his+['A']])
  elif q == 'AC':
    for x in now:
      a,b,c,his = x
      if a > 0:
        tmp.append([a-1,b,c+1,his+['C']])
      if c > 0:
        tmp.append([a+1,b,c-1,his+['A']])
  else:
    for x in now:
      a,b,c,his = x
      if b > 0:
        tmp.append([a,b-1,c+1,his+['C']])
      if c > 0:
        tmp.append([a,b+1,c-1,his+['B']])
  if not tmp:
    print('No')
    exit()
  now = []
  now2 = []
  for d in range(3):
    m = max(tmp,key=lambda x:x[d])
    if m[:3] not in now2:
      now.append(m)
      now2.append(m[:3])
    m = min(tmp,key=lambda x:x[d])
    if m[:3] not in now2:
      now.append(m)
      now2.append(m[:3])
print('Yes')
ans = now[0][3]
for s in ans:
  print(s)

Fixed submission:
#人生終わり
#解説解答
n,a,b,c = map(int,input().split())
if a + b + c == 0:
  print('No')
  exit()
elif a + b + c == 1:
  ans = []
  for _ in range(n):
    q = input()
    if q == 'AB':
      if a + b == 0:
        print('No')
        exit()
      a,b = b,a
      if a:
        ans.append('A')
      else:
        ans.append('B')
    elif q == 'BC':
      if b + c == 0:
        print('No')
        exit()
      b,c = c,b
      if b:
        ans.append('B')
      else:
        ans.append('C')
    else:
      if c + a == 0:
        print('No')
        exit()
      c,a = a,c
      if a:
        ans.append('A')
      else:
        ans.append('C')
  print('Yes')
  for x in ans:
    print(x)
  exit()
p = []
for _ in range(n):
  p.append(input())
if a + b == 0 and p[0] == 'AB':
    print('No')
    exit()
if b + c == 0 and p[0] == 'BC':
    print('No')
    exit()
if a + c == 0 and p[0] == 'AC':
    print('No')
    exit()
print('Yes')
for i in range(n):
  if p[i] == 'AB':
    if a == 0:
      a += 1
      b -= 1
      print('A')
    elif b == 0:
      a -= 1
      b += 1
      print('B')
    elif i != n-1:
      if p[i+1] == 'BC':
        a -= 1
        b += 1
        print('B')
      else:
        a += 1
        b -= 1
        print('A')
    else:
      a += 1
      b -= 1
      print('A')
  elif p[i] == 'BC':
    if b == 0:
      b += 1
      c -= 1
      print('B')
    elif c == 0:
      b -= 1
      c += 1
      print('C')
    elif i != n-1:
      if p[i+1] == 'AC':
        b -= 1
        c += 1
        print('C')
      else:
        b += 1
        c -= 1
        print('B')
    else:
      b += 1
      c -= 1
      print('B')
  else:
    if a == 0:
      a += 1
      c -= 1
      print('A')
    elif c == 0:
      a -= 1
      c += 1
      print('C')
    elif i != n-1:
      if p[i+1] == 'BC':
        a -= 1
        c += 1
        print('C')
      else:
        a += 1
        c -= 1
        print('A')
    else:
      a += 1
      c -= 1
      print('A')

Buggy python submission (verdict: Time Limit Exceeded):
n,a,b,c = map(int,input().split())
now = [[a,b,c,[]]]
for _ in range(n):
  q = input()
  tmp = []
  if q == 'AB':
    for x in now:
      a,b,c,his = x
      if a > 0:
        tmp.append([a-1,b+1,c,his+['B']])
      if b > 0:
        tmp.append([a+1,b-1,c,his+['A']])
  elif q == 'AC':
    for x in now:
      a,b,c,his = x
      if a > 0:
        tmp.append([a-1,b,c+1,his+['C']])
      if c > 0:
        tmp.append([a+1,b,c-1,his+['A']])
  else:
    for x in now:
      a,b,c,his = x
      if b > 0:
        tmp.append([a,b-1,c+1,his+['C']])
      if c > 0:
        tmp.append([a,b+1,c-1,his+['B']])
  if not tmp:
    print('No')
    exit()
  now = []
  m = max(tmp,key=lambda x:x[0])
  if m not in now:
    now.append(m)
  m = min(tmp,key=lambda x:x[0])
  if m not in now:
    now.append(m)
  m = max(tmp,key=lambda x:x[1])
  if m not in now:
    now.append(m)
  m = min(tmp,key=lambda x:x[1])
  if m not in now:
    now.append(m)
  m = max(tmp,key=lambda x:x[2])
  if m not in now:
    now.append(m)
  m = min(tmp,key=lambda x:x[2])
  if m not in now:
    now.append(m)
print('Yes')
ans = now[0][3]
for s in ans:
  print(s)

Fixed submission:
#人生終わり
#解説解答
n,a,b,c = map(int,input().split())
if a + b + c == 0:
  print('No')
  exit()
elif a + b + c == 1:
  ans = []
  for _ in range(n):
    q = input()
    if q == 'AB':
      if a + b == 0:
        print('No')
        exit()
      a,b = b,a
      if a:
        ans.append('A')
      else:
        ans.append('B')
    elif q == 'BC':
      if b + c == 0:
        print('No')
        exit()
      b,c = c,b
      if b:
        ans.append('B')
      else:
        ans.append('C')
    else:
      if c + a == 0:
        print('No')
        exit()
      c,a = a,c
      if a:
        ans.append('A')
      else:
        ans.append('C')
  print('Yes')
  for x in ans:
    print(x)
  exit()
p = []
for _ in range(n):
  p.append(input())
if a + b == 0 and p[0] == 'AB':
    print('No')
    exit()
if b + c == 0 and p[0] == 'BC':
    print('No')
    exit()
if a + c == 0 and p[0] == 'AC':
    print('No')
    exit()
print('Yes')
for i in range(n):
  if p[i] == 'AB':
    if a == 0:
      a += 1
      b -= 1
      print('A')
    elif b == 0:
      a -= 1
      b += 1
      print('B')
    elif i != n-1:
      if p[i+1] == 'BC':
        a -= 1
        b += 1
        print('B')
      else:
        a += 1
        b -= 1
        print('A')
    else:
      a += 1
      b -= 1
      print('A')
  elif p[i] == 'BC':
    if b == 0:
      b += 1
      c -= 1
      print('B')
    elif c == 0:
      b -= 1
      c += 1
      print('C')
    elif i != n-1:
      if p[i+1] == 'AC':
        b -= 1
        c += 1
        print('C')
      else:
        b += 1
        c -= 1
        print('B')
    else:
      b += 1
      c -= 1
      print('B')
  else:
    if a == 0:
      a += 1
      c -= 1
      print('A')
    elif c == 0:
      a -= 1
      c += 1
      print('C')
    elif i != n-1:
      if p[i+1] == 'BC':
        a -= 1
        c += 1
        print('C')
      else:
        a += 1
        c -= 1
        print('A')
    else:
      a += 1
      c -= 1
      print('A')