Problem:


Score : 600 points

Problem Statement

There is a game that involves three variables, denoted A, B, and C.

As the game progresses, there will be N events where you are asked to make a choice.
Each of these choices is represented by a string s_i. If s_i is AB, you must add 1 to A or B then subtract 1 from the other; if s_i is AC, you must add 1 to A or C then subtract 1 from the other; if s_i is BC, you must add 1 to B or C then subtract 1 from the other.

After each choice, none of A, B, and C should be negative.

Determine whether it is possible to make N choices under this condition. If it is possible, also give one such way to make the choices.

Constraints

1 \leq N \leq 10^5

0 \leq A,B,C \leq 10^9

N, A, B, C are integers.

s_i is AB, AC, or BC.

Input

Input is given from Standard Input in the following format:

N A B C
s_1
s_2
:
s_N

Output

If it is possible to make N choices under the condition, print Yes; otherwise, print No.

Also, in the former case, show one such way to make the choices in the subsequent N lines. The (i+1)-th line should contain the name of the variable (A, B, or C) to which you add 1 in the i-th choice.

Sample Input 1

2 1 3 0
AB
AC

Sample Output 1

Yes
A
C

You can successfully make two choices, as follows:

In the first choice, add 1 to A and subtract 1 from B. A becomes 2, and B becomes 2.

In the second choice, add 1 to C and subtract 1 from A. C becomes 1, and A becomes 1.

Sample Input 2

3 1 0 0
AB
BC
AB

Sample Output 2

No

Sample Input 3

1 0 9 0
AC

Sample Output 3

No

Sample Input 4

8 6 9 1
AC
BC
AB
BC
AC
BC
AB
AB

Sample Output 4

Yes
C
B
B
C
C
B
A
A



Sample input:
2 1 3 0
AB
AC


Sample output:
Yes
A
C

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
N,A,B,C=map(int,input().split())
z=[A,B,C].count(0)
s=[input() for i in range(N)]
t=1
if z==3:
    print("No")
    t=0
if z==2:
    if sum([A,B,C])==1:
        a,b,c=A,B,C
        for i in range(N):
            if s[i]=="AC":
                if A:
                    A-=1
                    C+=1
                elif C:
                    A+=1
                    C-=1
                else:
                    t=0
                    print("No")
                    break
            elif s[i]=="AB":
                if A:
                    A-=1
                    B+=1
                elif B:
                    A+=1
                    B-=1
                else:
                    t=0
                    print("No")
                    break
            if s[i]=="BC":
                if B:
                    B-=1
                    C+=1
                elif C:
                    B+=1
                    C-=1
                else:
                    t=0
                    print("No")
                    break
        if t:
            print("Yes")
            A,B,C=a,b,c
            for i in range(N):
                if s[i]=="AC":
                    if A:
                        A-=1
                        C+=1
                        print("C")
                    elif C:
                        A+=1
                        C-=1
                        print("A")
                elif s[i]=="AB":
                    if A:
                        A-=1
                        print("B")
                        B+=1
                    elif B:
                        A+=1
                        B-=1
                        print("A")
                if s[i]=="BC":
                    if B:
                        B-=1
                        C+=1
                        print("C")
                    elif C:
                        B+=1
                        C-=1
                        print("B")
    else:
        if s[0]=="AB":
            if A+B==0:
                print("No")
                t=0
        elif s[0]=="AC":
            if A+C==0:
                print("No")
                t=0
        if s[0]=="BC":
            if C+B==0:
                print("No")
                t=0
if t:
    print("Yes")
    for i in range(N):
        if s[i]=="AB":
            if A==B==1 and C==0 and i!=N-1:
                if s[i+1]=="BC":
                    print("B")
                    B+=1
                    A-=1
                else:
                    print("A")
                    A+=1
                    B-=1
            else:
                if A<B:
                    print("A")
                    A+=1
                    B-=1
                else:
                    print("B")
                    B+=1
                    A-=1
        elif s[i]=="AC":
            if A==C==1 and B==0 and i!=N-1:
                if s[i+1]=="BC":
                    print("C")
                    C+=1
                    A-=1
                else:
                    print("A")
                    A+=1
                    C-=1
            else:
                if A<C:
                    print("A")
                    A+=1
                    C-=1
                else:
                    print("C")
                    C+=1
                    A-=1
        elif s[i]=="BC":
            if C==B==1 and A==0 and i!=N-1:
                if s[i+1]=="AB":
                    print("B")
                    B+=1
                    C-=1
                else:
                    print("C")
                    C+=1
                    B-=1
            else:
                if C<B:
                    print("C")
                    C+=1
                    B-=1
                else:
                    print("B")
                    B+=1
                    C-=1

Submission status: Wrong Answer

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

if A == 0 and B == 0 and C == 0:
    print("NG")
elif A == 0 and B == 0 and C == 1 \
    or A == 0 and B == 1 and C == 0 \
    or A == 1 and B == 0 and C == 0:
    flag = True
    ans = []
    for i in range(N):
        if s[i] == "AB":
            if A == 0 and B == 0:
                flag = False
                break
            elif A < B:
                A += 1
                B -= 1
                ans.append("A")
            else:
                A -= 1
                B += 1
                ans.append("B")
        elif s[i] == "BC":
            if B == 0 and C == 0:
                flag = False
                break
            elif B < C:
                B += 1
                C -= 1
                ans.append("B")
            else:
                B -= 1
                C += 1
                ans.append("C")
        else:
            if C == 0 and A == 0:
                flag = False
                break
            elif C < A:
                C += 1
                A -= 1
                ans.append("C")
            else:
                C -= 1
                A += 1
                ans.append("A")
    if flag:
        print("Yes")
        print(*ans)
    else:
        print("No")
elif A == 0 and B == 0 and C > 1 and s[0] == "AB" \
        or A == 0 and B > 1 and C == 0 and s[0] == "AC" \
        or A > 1 and B == 0 and C == 0 and s[0] == "BC":
        print("No")
else:
    print("Yes")
    flag = 0
    ans = []
    for i in range(N):
        if flag > 0:
            if flag == 1:
                if s[i] == "AB":
                    ans.append("A")
                    ans.append("B")
                    continue
                elif s[i] == "BC":
                    A -= 1
                    C += 1
                    ans.append("B")
                    ans.append("C")
                else:
                    B -= 1
                    C += 1
                    ans.append("A")
                    ans.append("C")
            elif flag == 2:
                if s[i] == "BC":
                    ans.append("B")
                    ans.append("C")
                    continue
                elif s[i] == "AC":
                    B -= 1
                    A += 1
                    ans.append("C")
                    ans.append("A")
                else:
                    C -= 1
                    A += 1
                    ans.append("B")
                    ans.append("A")
            elif flag == 3:
                if s[i] == "AC":
                    ans.append("A")
                    ans.append("C")
                    continue
                elif s[i] == "AB":
                    C -= 1
                    B += 1
                    ans.append("A")
                    ans.append("B")
                else:
                    A -= 1
                    B += 1
                    ans.append("C")
                    ans.append("B")
            flag = 0
        else:
            if s[i] == "AB":
                if A < B:
                    A += 1
                    B -= 1
                    ans.append("A")
                elif A == 1 and B ==1:
                    flag = 1
                else:
                    A -= 1
                    B += 1
                    ans.append("B")
            elif s[i] == "BC":
                if B < C:
                    B += 1
                    C -= 1
                    ans.append("B")
                elif B == 1 and C ==1:
                    flag = 2
                else:
                    B -= 1
                    C += 1
                    ans.append("C")
            else:
                if C < A:
                    C += 1
                    A -= 1
                    ans.append("C")
                elif C == 1 and A ==1:
                    flag = 3
                else:
                    C -= 1
                    A += 1
                    ans.append("A")
    print(*ans)

Submission status: Wrong Answer

Solution code:
def examA():
    S = SI()
    if S=="ARC":
        ans = "ABC"
    else:
        ans = "ARC"
    print(ans)
    return

def examB():
    N, K = LI()
    snuke = set(range(1,N+1))
    for _ in range(K):
        d = I()
        A = LI()
        for a in A:
            snuke.discard(a)
    ans = len(snuke)
    print(ans)
    return

def examC():
    N, M = LI()
    H = LI()
    good = set(range(1,N+1))
    for _ in range(M):
        a, b = LI()
        if H[a-1]>=H[b-1]:
            good.discard(b)
        if H[b-1]>=H[a-1]:
            good.discard(a)
    ans = len(good)
    print(ans)
    return

def examD():
    X = I()
    #print(1000**5-999**5)
    for a in range(-1000,1000):
        for b in range(-1000,1000):
            if a**5-b**5==X:
                print(a,b)
                return
    return

def examE():
    N = I()
    A = LI()
    B = [0]*N
    D = defaultdict(int)
    for i in range(N):
        B[i] = A[i]-i
        c = A[i]+i
        D[c] += 1
    #print(B)
    #print(D)
    ans = 0
    for b in B:
        ans += D[-b]
    print(ans)
    return

def examF():
    def Run_Length_Encoding(s):
        tmp, count, res = s[0], 1, []
        for i in range(1, len(s)):
            if tmp == s[i]:
                count += 1
            else:
                res.append((count, tmp))
                tmp = s[i]
                count = 1
        res.append((count, tmp))
        return res

    N, A, B, C = LI()
    S = [SI()for _ in range(N)]
    SS = Run_Length_Encoding(S)
    #print(SS)
    n = len(SS)
    ans = [""]*(N)
    def judge(a,b):
        if a<0 or b<0:
            return False
        return True
    def dfs(i,num,A,B,C):
        if i==n:
            if A>=0 and B>=0 and C>=0:
                print("Yes")
                for v in ans:
                    print(v)
                exit()
            return
        cnt, s = SS[i]
        if s=="AB":
            if A==0 and B==0:
                return
            for j in range(cnt//2):
                if A==0:
                    ans[num] = "A"
                    num += 1
                    ans[num] = "B"
                else:
                    ans[num] = "B"
                    num += 1
                    ans[num] = "A"
            if cnt%2==0:
                dfs(i+1,num+1,A,B,C)
                return
            A += 1; B -= 1
            if judge(A,B):
                ans[num] = "A"
                dfs(i+1,num+1,A,B,C)
            A -= 2; B += 2
            if judge(A,B):
                ans[num] = "B"
                dfs(i+1,num+1,A,B,C)
            A += 1; B -= 2
        elif s=="BC":
            if B==0 and C==0:
                return
            for j in range(cnt//2):
                if B==0:
                    ans[num] = "B"
                    num += 1
                    ans[num] = "C"
                else:
                    ans[num] = "C"
                    num += 1
                    ans[num] = "B"
            if cnt%2==0:
                dfs(i+1,num+1,A,B,C)
                return
            B += 1; C -= 1
            if judge(B,C):
                ans[num] = "B"
                dfs(i+1,num+1,A,B,C)
            B -= 2; C += 2
            if judge(B,C):
                ans[num] = "C"
                dfs(i+1,num+1,A,B,C)
            B += 1; C -= 2
        else:
            if A==0 and C==0:
                return
            for j in range(cnt//2):
                if A==0:
                    ans[num] = "A"
                    num += 1
                    ans[num] = "C"
                else:
                    ans[num] = "C"
                    num += 1
                    ans[num] = "A"
            if cnt%2==0:
                dfs(i+1,num+1,A,B,C)
                return
            A += 1; C -= 1
            if judge(A, C):
                ans[num] = "A"
                dfs(i + 1,num+1,A,B,C)
            A -= 2; C += 2
            if judge(A, C):
                ans[num] = "C"
                dfs(i + 1,num+1,A,B,C)
            A += 1; C -= 2
        return
    dfs(0,0,A,B,C)
    print("No")
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(readline())
def LI(): return list(map(int,readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examF()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, Counter, namedtuple, deque
import itertools
import functools
import bisect
import heapq
import math


NN = 202020
MOD = 10**9+7
# MOD = 998244353
INF = float("inf")

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

ans = []
for i in range(n):
    if s[i] == "AB":
        if num[0] > num[1]:
            num[0], num[1] = num[0]-1, num[1]+1
            ans.append("B")
        elif num[0] < num[1]:
            num[0], num[1] = num[0]+1, num[1]-1
            ans.append("A")
        else:
            if num[0] == num[1] == 0:
                print("No")
                exit()
            if i < n-1 and s[i] == "BC":
                num[0], num[1] = num[0] - 1, num[1] + 1
                ans.append("B")
            else:
                num[0], num[1] = num[0] + 1, num[1] - 1
                ans.append("A")

    elif s[i] == "AC":
        if num[0] > num[2]:
            num[0], num[2] = num[0]-1, num[2]+1
            ans.append("C")
        elif num[0] < num[2]:
            num[0], num[2] = num[0]+1, num[2]-1
            ans.append("A")
        else:
            if num[0] == num[2] == 0:
                print("No")
                exit()
            if i < n-1 and s[i] == "BC":
                num[0], num[2] = num[0] - 1, num[2] + 1
                ans.append("C")
            else:
                num[0], num[2] = num[0] + 1, num[2] - 1
                ans.append("A")

    elif s[i] == "BC":
        if num[1] > num[2]:
            num[1], num[2] = num[1]-1, num[2]+1
            ans.append("C")
        elif num[1] < num[2]:
            num[1], num[2] = num[1]+1, num[2]-1
            ans.append("B")
        else:
            if num[1] == num[2] == 0:
                print("No")
                exit()
            if i < n-1 and s[i] == "AC":
                num[1], num[2] = num[1] - 1, num[2] + 1
                ans.append("C")
            else:
                num[1], num[2] = num[1] + 1, num[2] - 1
                ans.append("B")
    # print(num)

print("Yes")
for i in range(n):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
n, a, b, c = map(int, input().split())
q_l = [input() for _ in range(n)]

result = []
for q in q_l: 
    if q == "AB":
        if a > b:
            result.append("B")
            a -= 1
            b += 1
        else:
            result.append("A")
            a += 1
            b -= 1
    elif q == "BC":
        if b > c:
            result.append("C")
            b -= 1
            c += 1
        else:
            result.append("B")
            b += 1
            c -= 1
    else:
        if a > c:
            result.append("C")
            a -= 1
            c += 1
        else:
            result.append("A")
            a += 1
            c -= 1
    if a < 0 or b < 0 or c < 0:
        print("No")
        exit()
print("Yes")
for r in result:
    print(r)

Submission status: Wrong Answer

Solution code:
n, a, b, c = map(int, input().split())
ss = []
for i in range(n):
    ss.append(input())

res_list = []
flag = 9
if a == 0 and b == 0 and c == 0:
    flag = 0
elif a * b > 1 or a * c > 1 or b * c > 1:
    flag = 1
    for s in ss:
        if s == 'AB':
            if not b in [0, 1]:
                res_list.append('A')
                a, b = a+1, b-1
            else:
                res_list.append('B')
                a, b = a-1, b+1
        elif s == 'BC':
            if not c in [0, 1]:
                res_list.append('B')
                b, c = b+1, c-1
            else:
                res_list.append('C')
                b, c = b-1, c+1
        else:
            if not c in [0, 1]:
                res_list.append('A')
                a, c = a+1, c-1
            else:
                res_list.append('C')
                a, c = a-1, c+1
else:
    if a == 1 and b == 1 and c == 1:
        flag = 1
        for s in ss:
            if s == 'AB':
                if b == 0:
                    res_list.append('B')
                    a, b = a-1, b+1
                elif b == 1 and a > 1:
                    res_list.append('B')
                    a, b = a-1, b+1
                else:
                    res_list.append('A')
                    a, b = a+1, b-1
            elif s == 'BC':
                if c == 0:
                    res_list.append('C')
                    b, c = b-1, c+1
                elif c == 1 and b > 1:
                    res_list.append('C')
                    b, c = b-1, c+1
                else:
                    res_list.append('B')
                    b, c = b+1, c-1
            else:
                if c == 0:
                    res_list.append('C')
                    a, c = a-1, c+1
                elif c == 1 and a > 1:
                    res_list.append('C')
                    a, c = a-1, c+1
                else:
                    res_list.append('A')
                    a, c = a+1, c-1
    elif 1 in [a*b, b*c, a*c]:
        flag = 1
        for i in range(n):
            if ss[i] == 'AB':
                if a * b == 0:
                    if a == 0:
                        res_list.append('A')
                    else:
                        res_list.append('B')
                    a, b = b, a
                else:
                    if i == n-1:
                        continue
                    elif ss[i+1] == 'AC':
                        res_list.append('A')
                    else:
                        res_list.append('B')
                    a, b = b, a
            elif ss[i] == 'BC':
                if b * c == 0:
                    if b == 0:
                        res_list.append('B')
                    else:
                        res_list.append('C')
                    b, c = c, b
                else:
                    if i == n-1:
                        continue
                    elif ss[i+1] == 'AC':
                        res_list.append('C')
                    else:
                        res_list.append('B')
                    b, c = c, b
            else:
                if a * c == 0:
                    if a == 0:
                        res_list.append('A')
                    else:
                        res_list.append('C')
                    a, c = c, a
                else:
                    if i == n-1:
                        continue
                    elif ss[i+1] == 'AB':
                        res_list.append('A')
                    else:
                        res_list.append('C')
                    a, c = c, a
    elif a+b+c == 1:
        flag = 1
        for s in ss:
            if s == 'AB' and a == 0 and b == 0:
                flag = 0
            elif s == 'BC' and b == 0 and c == 0:
                flag = 0
            elif s == 'AC' and a == 0 and c == 0:
                flag = 0
            else:
                if s == 'AB':
                    if a == 1:
                        res_list.append('A')
                    else:
                        res_list.append('B')
                    a, b = b, a
                elif s == 'BC':
                    if b == 1:
                        res_list.append('B')
                    else:
                        res_list.append('C')
                    b, c = c, b
                else:
                    if c == 1:
                        res_list.append('C')
                    else:
                        res_list.append('A')
                    a, c = c, a
    else:
        if ss[0] == 'AB' and a == 0 and b == 0:
            flag = 0
        elif ss[0] == 'AC' and a == 0 and c == 0:
            flag = 0
        elif ss[0] == 'BC' and b == 0 and c == 0:
            flag = 0
        else:
            flag = 1
            for s in ss:
                if s == 'AB':
                    if not b in [0, 1]:
                        res_list.append('A')
                        a, b = a + 1, b - 1
                    else:
                        res_list.append('B')
                        a, b = a - 1, b + 1
                elif s == 'BC':
                    if not c in [0, 1]:
                        res_list.append('B')
                        b, c = b + 1, c - 1
                    else:
                        res_list.append('C')
                        b, c = b - 1, c + 1
                else:
                    if not c in [0, 1]:
                        res_list.append('A')
                        a, c = a + 1, c - 1
                    else:
                        res_list.append('C')
                        a, c = a - 1, c + 1

if flag == 0:
    print('No')
else:
    print('Yes')
    for res in res_list:
        print(res)

Submission status: Wrong Answer

Solution code:
n, a, b, c = map(int, input().split())
s = []

for i in range(n):
  s.append(input())

ans = []
flag = True
for i in range(n):
  if s[i] == "AB":
    if a < b:
      ans.append("A")
      a += 1
      b -= 1
    elif a == b == 0:
      flag = False
      break
    else:
      ans.append("B")
      a -= 1
      b += 1
  if s[i] == "AC":
    if a < c:
      ans.append("A")
      a += 1
      c -= 1
    elif a == c == 0:
      flag = False
      break
    else:
      ans.append("C")
      a -= 1
      c += 1
  if s[i] == "BC":
    if b < c:
      ans.append("B")
      b += 1
      c -= 1
    elif b == c == 0:
      flag = False
      break
    else:
      ans.append("C")
      b -= 1
      c += 1

print("Yes" if flag else "No")
if flag:
  for _ans in ans:
    print(_ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, a, b, c = LI()
ans = []
D = {'A':a, 'B':b, 'C':c}
sx = SR(n)
for i in range(n):
    s = sx[i]
    s1 = s[0]
    s2 = s[1]
    if D[s1] == D[s2] == 0:
        print('No')
        exit()
    elif D[s[0]] == D[s[1]] == 1 and i + 1 < n:
        if s1 in sx[i + 1]:
            D[s1] += 1
            D[s2] -= 1
            ans += [s1]
        else:
            D[s2] += 1
            D[s1] -= 1
            ans += [s2]
    else:
        if D[s1] == 0:
            ans += [s1]
            D[s1] += 1
            D[s2] -= 1
        else:
            ans += [s2]
            D[s1] -= 1
            D[s2] += 1

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

Submission status: Wrong Answer

Solution code:
d={}
count={}

d['A']=0
d['B']=0
d['C']=0

count['A']=0
count['B']=0
count['C']=0

ip=[int(x) for x in input().split()]
n=ip[0]
d['A']=ip[1]
d['B']=ip[2]
d['C']=ip[3]

flag=1
a=[]
for i in range(n):
	s=input()
	a.append(s)
	count[s[0]]+=1
	count[s[1]]+=1

ans=[]
for i in range(n):
	count[s[0]]-=1
	count[s[1]]-=1
	s=a[i]
	if d[s[0]]>d[s[1]]:
		ans.append(s[1])
		d[s[1]]+=1
		d[s[0]]-=1
	elif d[s[1]]>d[s[0]]:
		ans.append(s[0])
		d[s[1]]-=1
		d[s[0]]+=1
	else:
		if count[s[0]]>count[s[1]]:
			d[s[0]]+=1
			d[s[1]]-=1
			ans.append(s[0])
		else:
			d[s[1]]+=1
			d[s[0]]-=1
			ans.append(s[1])
	if d['A']<0 or d['B']<0 or d['C']<0:
		flag=0

if flag==0:
	print("No")
else:
	print("Yes")
	for i in range(n):
		print(ans[i])

Submission status: Wrong Answer

Solution code:
N, A, B, C = [int(_) for _ in input().split()]
S = [input() for _ in range(N)] + ['X']


def no():
    print('No')
    exit()


ans = []
for sa, sb in zip(S, S[1:]):
    if A == B == 0:
        if sa == 'AB':
            no()
    elif B == C == 0:
        if sa == 'BC':
            no()
    elif C == A == 0:
        if sa == 'AC':
            no()
    if sa == 'AB':
        if B == 0:
            ans += ['B']
            B += 1
            A -= 1
        elif A == 0:
            ans += ['A']
            A += 1
            B -= 1
        elif A == 1 and C == 0 and sb == 'AC':
            ans += ['A']
            A += 1
            B -= 1
        else:
            ans += ['B']
            B += 1
            A -= 1
    elif sa == 'BC':
        if B == 0:
            ans += ['B']
            B += 1
            C -= 1
        elif C == 0:
            ans += ['C']
            C += 1
            B -= 1
        elif B == 1 and A == 0 and sb == 'AB':
            ans += ['B']
            B += 1
            C -= 1
        else:
            ans += ['C']
            C += 1
            B -= 1
    elif sa == 'AC':
        if C == 0:
            ans += ['C']
            C += 1
            A -= 1
        elif A == 0:
            ans += ['A']
            A += 1
            C -= 1
        elif A == 1 and B == 0 and sb == 'AB':
            ans += ['A']
            A += 1
            C -= 1
        else:
            ans += ['C']
            C += 1
            A -= 1
print('Yes')
print('\n'.join(ans))

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()

ops = ''
def process(_arr, i, j):
    global ops
    arr = _arr.copy()
    if arr[i] > arr[j]:
        i, j = j, i
    arr[i] += 1
    arr[j] -= 1
    if i == 0:
        c = 'A'
    if i == 1:
        c = 'B'
    if i == 2:
        c = 'C'
    ops += c
    return arr

def main():
    N, A, B, C = LI()
    arr = [A, B, C]
    for _ in range(N):
        s = S()
        if s == 'AB':
            arr = process(arr, 0, 1)
        if s == 'BC':
            arr = process(arr, 1, 2)
        if s == 'AC':
            arr = process(arr, 0, 2)
        for elem in arr:
            if elem < 0:
                print('No')
                return
    print('Yes')
    for c in ops:
        print(c)

main()