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: Accepted

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

summ = a + b + c
if summ == 0:
    print('No')
    exit()

if summ >= 3:
    if command[0] == 'AB' and summ == c or command[0] == 'BC' and summ == a or command[0] == 'AC' and summ == b:
        print('No')
    else:
        print('Yes')
        for com in command:
            if com == 'AB':
                if a == 0:
                    print('A')
                    a += 1
                    b -= 1
                elif b == 0:
                    print('B')
                    a -= 1
                    b += 1
                elif a == 1:
                    print('A')
                    a += 1
                    b -= 1
                else:
                    print('B')
                    a -= 1
                    b += 1
            if com == 'BC':
                if b == 0:
                    print('B')
                    b += 1
                    c -= 1
                elif c == 0:
                    print('C')
                    b -= 1
                    c += 1
                elif b == 1:
                    print('B')
                    b += 1
                    c -= 1
                else:
                    print('C')
                    b -= 1
                    c += 1
            if com == 'AC':
                if c == 0:
                    print('C')
                    c += 1
                    a -= 1
                elif a == 0:
                    print('A')
                    c -= 1
                    a += 1
                elif c == 1:
                    print('C')
                    c += 1
                    a -= 1
                else:
                    print('A')
                    c -= 1
                    a += 1


elif summ == 2:
    choice = [''] * n
    for i, com in enumerate(command):

        if com == 'AB':
            if c == 2:
                print('No')
                exit()
            if a == 0:
                choice[i] = 'A'
                a += 1
                b -= 1
            elif b == 0:
                choice[i] = 'B'
                a -= 1
                b += 1
            else:
                if i < n-1 and command[i+1] == 'BC':
                    choice[i] = 'B'
                    a -= 1
                    b += 1
                else:
                    choice[i] = 'A'
                    a += 1
                    b -= 1
        if com == 'BC':
            if a == 2:
                print('No')
                exit()
            if b == 0:
                choice[i] = 'B'
                b += 1
                c -= 1
            elif c == 0:
                choice[i] = 'C'
                b -= 1
                c += 1
            else:
                if i < n-1 and command[i+1] == 'AC':
                    choice[i] = 'C'
                    b -= 1
                    c += 1
                else:
                    choice[i] = 'B'
                    b += 1
                    c -= 1


        if com == 'AC':
            if b == 2:
                print('No')
                exit()
            if c == 0:
                choice[i] = 'C'
                c += 1
                a -= 1
            elif a == 0:
                choice[i] = 'A'
                c -= 1
                a += 1
            else:
                if i < n-1 and command[i+1] == 'AB':
                    choice[i] = 'A'
                    c -= 1
                    a += 1
                else:
                    choice[i] = 'C'
                    c += 1
                    a -= 1
    
    print('Yes')
    for ch in choice:
        print(ch)



elif summ == 1:
    # １つずつシミュレーション

    choice = [''] * n
    for i, com in enumerate(command):
        if com == 'AB':
            if c == 1:
                print('No')
                exit()
            if a == 0:
                choice[i] = 'A'
                a += 1
                b -= 1
            elif b == 0:
                choice[i] = 'B'
                a -= 1
                b += 1
        if com == 'BC':
            if a == 1:
                print('No')
                exit()
            if b == 0:
                choice[i] = 'B'
                b += 1
                c -= 1
            elif c == 0:
                choice[i] = 'C'
                b -= 1
                c += 1


        if com == 'AC':
            if b == 1:
                print('No')
                exit()
            if c == 0:
                choice[i] = 'C'
                c += 1
                a -= 1
            elif a == 0:
                choice[i] = 'A'
                c -= 1
                a += 1
    
    print('Yes')
    for ch in choice:
        print(ch)

Submission status: Accepted

Solution code:
import math


def main():
    N, A, B, C = map(int, input().split())
    s = [input() for _ in range(N)] + ['']

    # dp_abc = [None for _ in range(N+1)]
    # dp_history = [[] for _ in range(N+1)]
    #
    # dp_abc[0] = (A, B, C)

    history = []

    cnt = {
        'A': A,
        'B': B,
        'C': C,
    }

    for i in range(N):
        x, y = s[i][0], s[i][1]
        if cnt[x] > cnt[y]:
            cnt[x] -= 1
            cnt[y] += 1
            history.append(y)
        elif cnt[x] < cnt[y]:
            cnt[x] += 1
            cnt[y] -= 1
            history.append(x)
        else:
            if x in s[i + 1]:
                cnt[x] += 1
                cnt[y] -= 1
                history.append(x)
            else:
                cnt[x] -= 1
                cnt[y] += 1
                history.append(y)

        if cnt[x] < 0 or cnt[y] < 0:
            print('No')
            return

    print('Yes')
    for c in history:
        print(c)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = [0,0,0]
n,s[0],s[1],s[2] = map(int,input().split())
T = [0]*n
ABC = 'ABC'
for i in range(n):
    t = input()
    if t == 'AB':T[i] = 2
    if t == 'BC':T[i] = 0
    if t == 'AC':T[i] = 1
ans = ['' for _ in range(n)]
# if s.count(0) <= 1 and sum(s) >= 3:
#     for i,t in enumerate(T):
#         nxt = [0,1,2]
#         nxt.remove(t)
#         if s[nxt[0]] < s[nxt[1]]:
#             ans[i] = ABC[nxt[0]]
#             s[nxt[0]] += 1
#             s[nxt[1]] -= 1
#         else:
#             ans[i] = ABC[nxt[1]]
#             s[nxt[0]] -= 1
#             s[nxt[1]] += 1
#     print('Yes')
#     print('\n'.join(ans))
if sum(s) == 2:
    if s.count(0) == 1:
        now = s.index(0)
        i = 0
    else:
        if T[0] != s.index(2):
            now = T[0]
            i = 1
            nxt = [0,1,2]
            nxt.remove(T[0])
            nxt.remove(s.index(2))
            ans[0] = ABC[nxt[0]]
        else:
            print('No')
            exit(0)
    while i < n-1:
        if T[i] == T[i+1]:
            if T[i] == now:
                ans[i] = ABC[(now-1)%3]
                ans[i+1] = ABC[(now-2)%3]
            else:
                for j in range(3):
                    if j not in [T[i],now]:
                        ans[i+1] = ABC[j]
                ans[i] = ABC[now]
            i += 2
        elif T[i] == now:
            for j in range(3):
                if j not in [T[i+1],now]:ans[i] = ABC[j]
            ans[i+1] = ABC[T[i]]
            now = T[i+1]
            i += 2
        else:
            ans[i] = ABC[now]
            for j in range(3):
                if j not in [T[i],now]:
                    now = j
                    break
            i += 1
    if i == n-1:
        if T[i] == now:
            for j in range(3):
                if j not in [now,T[i]]:ans[i] = ABC[j]
        else:
            ans[i] = ABC[now]
    print('Yes')
    print('\n'.join(ans))
else:
    for i,t in enumerate(T):
        nxt = [0,1,2]
        nxt.remove(t)
        if s[nxt[0]] < s[nxt[1]]:
            ans[i] = ABC[nxt[0]]
            s[nxt[0]] += 1
            s[nxt[1]] -= 1
        else:
            ans[i] = ABC[nxt[1]]
            s[nxt[0]] -= 1
            s[nxt[1]] += 1
        if -1 in s:
            print('No')
            break
    else:
        print('Yes')
        print('\n'.join(ans))

Submission status: Accepted

Solution code:
N, A, B, C = map(int, input().split())
opts = []
for _ in range(N):
    opt = input()
    opts.append((ord(opt[0]) - ord("A"), ord(opt[1]) - ord("A")))

choices = []
nums = [A, B, C]
for i, opt in enumerate(opts):
    if nums[opt[0]] == 0 and nums[opt[1]] == 0:
        print("No")
        break
    if nums[opt[0]] == 1 and nums[opt[1]] == 1 and i < N - 1 and opt != opts[i + 1]:
        choice = (set(opt) & set(opts[i + 1])).pop()
    elif nums[opt[0]] >= nums[opt[1]]:
        choice = opt[1]
    else:
        choice = opt[0]
    choices.append(choice)
    nums[choice] += 1
    nums[choice ^ opt[0] ^ opt[1]] -= 1
else:
    print("Yes")
    for c in choices:
        print(chr(c + ord("A")))

Submission status: Accepted

Solution code:
import sys
def main():
    input = sys.stdin.readline
    N,A,B,C=map(int, input().split())
    S=[input().rstrip() for _ in range(N)]

    MX=2
    dp=[[[[None]*(MX+1) for _ in range(MX+1)] for __ in range(MX+1)] for ___ in range(N+4)]
    dp[0][min(MX,A)][min(MX,B)][min(MX,C)]=1
    for i,s in enumerate(S):
        ni=i+1
        for a in range(MX+1):
            for b in range(MX+1):
                for c in range(MX+1):
                    for j in range(2):
                        na,nb,nc=a,b,c
                        sel=None
                        if s=='AB':
                            if j==0:
                                if b==0: continue
                                na,nb=min(MX,na+1),nb-1
                                sel='A'
                            else:
                                if a==0: continue
                                na,nb=na-1,min(MX,nb+1)
                                sel='B'
                        elif s=='AC':
                            if j==0:
                                if c==0: continue
                                na,nc=min(MX,na+1),nc-1
                                sel='A'
                            else:
                                if a==0: continue
                                na,nc=na-1,min(MX,nc+1)
                                sel='C'
                        elif s=='BC':
                            if j==0:
                                if c==0: continue
                                nb,nc=min(MX,nb+1),nc-1
                                sel='B'
                            else:
                                if b==0: continue
                                nb,nc=nb-1,min(MX,nc+1)
                                sel='C'
                        if dp[i][a][b][c]: dp[ni][na][nb][nc]=(a,b,c,sel)

    flag=False
    ga,gb,gc=0,0,0
    for a in range(MX+1):
        for b in range(MX+1):
            for c in range(MX+1):
                if dp[N][a][b][c]:
                    flag=True
                    ga,gb,gc=a,b,c
    if flag==False:
        print('No')
        return
    print('Yes')
    ans=[]
    for i in range(N,0,-1):
        ga,gb,gc,sel=dp[i][ga][gb][gc]
        ans.append(sel)
    for a in reversed(ans):
        print(a)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

ok = True

s_l = []

ans_s = []

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

for i in range(n):
    s = s_l[i]
    if i < n-1:
        nex = s_l[i+1]
    else:
        nex = 'NONO'

    if s == 'AB':
        if a < b:
            a += 1
            b -= 1
            ans_s.append('A')
        elif a > b:
            a -= 1
            b += 1
            ans_s.append('B')
        elif nex == 'BC':
            a -= 1
            b += 1
            ans_s.append('B')
        else:
            a += 1
            b -= 1
            ans_s.append('A')
    elif s == 'AC':
        if a < c:
            a += 1
            c -= 1
            ans_s.append('A')
        elif a > c:
            a -= 1
            c += 1
            ans_s.append('C')
        elif nex == 'BC':
            a -= 1
            c += 1
            ans_s.append('C')
        else:
            a += 1
            c -= 1
            ans_s.append('A')
    else:
        if b < c:
            b += 1
            c -= 1
            ans_s.append('B')
        elif b > c:
            b -= 1
            c += 1
            ans_s.append('C')
        elif nex == 'AB':
            c -= 1
            b += 1
            ans_s.append('B')
        else:
            c += 1
            b -= 1
            ans_s.append('C')

    if a < 0 or b < 0 or c < 0:
        ok = False
        break

if ok:
    print('Yes')
    for s_e in ans_s:
        print(s_e)
else:
    print('No')

Submission status: Accepted

Solution code:
def mp():return map(int,input().split())
def lmp():return list(map(int,input().split()))


n,a,b,c = mp()
ans_ar = []
q = []
for i in range(n):
    s = input()
    q.append(s)
for i in range(n):
    s = q[i]
    if s == "AB":
        if a == b == 0:
            print("No")
            exit()
        elif a == b == 1 and i != n-1:
            if q[i+1] == "AC":
                a += 1
                b -= 1
                ans_ar.append("A")
            else:
                a -= 1
                b += 1
                ans_ar.append("B")
        elif a >= b:
            a -= 1
            b += 1
            ans_ar.append("B")
        else:
            a += 1
            b -= 1
            ans_ar.append("A")
    if s == "AC":
        if a == c == 0:
            print("No")
            exit()
        elif a == c == 1 and i != n - 1:
            if q[i + 1] == "AB":
                a += 1
                c -= 1
                ans_ar.append("A")
            else:
                a -= 1
                c += 1
                ans_ar.append("C")

        elif a >= c:
            a -= 1
            c += 1
            ans_ar.append("C")
        else:
            a += 1
            c -= 1
            ans_ar.append("A")
    if s == "BC":
        if c == b == 0:
            print("No")
            exit()
        elif c == b == 1 and i != n-1:
            if q[i+1] == "AC":
                c += 1
                b -= 1
                ans_ar.append("C")
            else:
                c -= 1
                b += 1
                ans_ar.append("B")

        elif c >= b:
            c -= 1
            b += 1
            ans_ar.append("B")
        else:
            c += 1
            b -= 1
            ans_ar.append("C")
print("Yes")
for i in ans_ar:
    print(i)

Submission status: Accepted

Solution code:
N, A, B, C = map(int, input().split())
dic = {'A':A,'B':B,'C':C}
S = [input() for _ in range(N)]

def solve(N,dic,S):
    ans = ['Yes']
    for i in range(N):
        if dic[S[i][0]]>dic[S[i][1]]:
            dic[S[i][0]]-=1
            dic[S[i][1]]+=1
            ans.append(S[i][1])
        elif dic[S[i][0]]<dic[S[i][1]] or dic[S[i][0]]>1:
            dic[S[i][1]]-=1
            dic[S[i][0]]+=1
            ans.append(S[i][0])
        elif dic[S[i][0]]==0:
            return ['No']
        elif i==N-1:
            dic[S[i][0]]+=1
            dic[S[i][1]]-=1
            ans.append(S[i][0])
        else:
            if S[i][0] in S[i+1]:
                dic[S[i][0]]+=1
                dic[S[i][1]]-=1
                ans.append(S[i][0])
            else:
                dic[S[i][1]]+=1
                dic[S[i][0]]-=1
                ans.append(S[i][1])
    return ans
print(*solve(N,dic,S),sep='\n')

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**8)
INF = float("inf")


def yes():
    print("Yes")  # type: str


def no():
    print("No")  # type: str


def solve(N: int, A: int, B: int, C: int, s: "List[str]"):
    ABC = [A, B, C]
    ans = []
    d = {"AB": (0, 1), "BC": (1, 2), "AC": (0, 2)}
    # print(ABC)
    for i in range(N):
        j, k = d[s[i]]
        if ABC[j] == ABC[k] == 0:
            no()
            return
        if ABC[j] == ABC[k] == 1:
            if i != N-1 and (j not in d[s[i+1]]):
                j, k = k, j
            ABC[j] += 1
            ABC[k] -= 1
            ans.append("ABC"[j])
        elif ABC[j] > ABC[k]:
            ABC[j] -= 1
            ABC[k] += 1
            ans.append("ABC"[k])
        else:
            ABC[j] += 1
            ABC[k] -= 1
            ans.append("ABC"[j])

    yes()
    for a in ans:
        print(a)
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    s = [next(tokens) for _ in range(N)]  # type: "List[str]"
    solve(N, A, B, C, s)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys,math,copy,queue,itertools,bisect,collections
from heapq import *
sys.setrecursionlimit(10**7)
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())
INF = 10**18
MOD = 10**9 + 7
DD = ((1,0),(-1,0),(0,1),(0,-1))

n,a,b,c = LI()
z = [a,b,c]
s = [sys.stdin.readline().rstrip() for _ in range(n)]
ans = []
for i in range(n):
    x = s[i]
    p = -1
    if x == 'AB': j=0;k=1;l=2
    elif x == 'AC': j=0;k=2;l=1
    elif x == 'BC': j=1;k=2;l=0
    if z[j] > z[k]:
        z[j] -= 1; z[k] += 1
        p = k
    else:
        z[j] += 1; z[k] -= 1
        p = j
    if (z[j] == 0 or z[k] == 0) and z[l] == 0:
        if i < n-1:
            y = s[i+1]
            if y == 'AB': j1=0;k1=1;l1=2
            elif y == 'AC': j1=0;k1=2;l1=1
            elif y == 'BC': j1=1;k1=2;l1=0
            if z[j1] == 0 and z[k1] == 0:
                if z[j] == 0:
                    z[j] += 2; z[k] -= 2
                    p = j
                else:
                    z[j] -= 2; z[k] += 2
                    p = k
    if min(z) < 0:
        print('No')
        exit(0)
    if p==0:ans.append('A')
    elif p==1:ans.append('B')
    else: ans.append('C')
print('Yes')
print(*ans,sep='\n')

Submission status: Accepted

Solution code:
n,a,b,c = map(int, input().split())
a = min(a, 4)
b = min(b, 4)
c = min(c, 4)

s = [input() for i in range(n)]

DP = [set() for i in range(n+1)]
DP[0].add((a,b,c))
for i in range(n):
    for x,y,z in DP[i]:
        if s[i] == "AB":
            nz = z

            nx = x+1
            ny = y-1
            if ny >= 0:
                DP[i+1].add((nx,ny,nz))
            nx = x-1
            ny = y+1
            if nx >= 0:
                DP[i+1].add((nx,ny,nz))
        elif s[i] == "BC":
            nx = x

            ny = y+1
            nz = z-1
            if nz >= 0:
                DP[i+1].add((nx,ny,nz))
            ny = y-1
            nz = z+1
            if ny >= 0:
                DP[i+1].add((nx,ny,nz))
        else:
            ny = y

            nz = z+1
            nx = x-1
            if nx>=0:
                DP[i+1].add((nx,ny,nz))
            nz = z-1
            nx = x+1
            if nz >=0:
                DP[i+1].add((nx,ny,nz))

if len(DP[-1]) == 0:
    print("No")
    exit()
ans = []
tmp = list(DP[-1])[0]
for i in range(n):
    x,y,z = tmp
    j = n-1-i
    if s[j] == "AB":
        nz = z

        nx = x+1
        ny = y-1
        if (nx,ny,nz) in DP[j]:
            tmp = (nx,ny,nz)
            ans.append("B")
            continue
        nx = x-1
        ny = y+1
        if (nx,ny,nz) in DP[j]:
            tmp = (nx,ny,nz)
            ans.append("A")
            continue
    elif s[j] == "BC":
        nx = x

        ny = y+1
        nz = z-1
        if (nx,ny,nz) in DP[j]:
            tmp = (nx,ny,nz)
            ans.append("C")
            continue
        ny = y-1
        nz = z+1
        if (nx,ny,nz) in DP[j]:
            tmp = (nx,ny,nz)
            ans.append("B")
            continue
    else:
        ny = y

        nz = z+1
        nx = x-1
        if (nx,ny,nz) in DP[j]:
            tmp = (nx,ny,nz)
            ans.append("A")
            continue
        nz = z-1
        nx = x+1
        if (nx,ny,nz) in DP[j]:
            tmp = (nx,ny,nz)
            ans.append("C")
            continue

ans = ans[::-1]
print("Yes")
print(*ans, sep="\n")

Submission status: Accepted

Solution code:
import sys

n,a,b,c = map(int,input().split())
d = {'A':a,'B':b,'C':c}

s = []
for _ in range(n):
	s.append(input())

ans = []
for i in range(n):

	if d[s[i][0]] <= 0 and d[s[i][1]] <= 0:
		print('No')
		sys.exit()
	if d[s[i][0]] < d[s[i][1]]:
		d[s[i][1]] -= 1
		d[s[i][0]] += 1
		ans.append(s[i][0])
	elif d[s[i][0]] > d[s[i][1]]:
		d[s[i][0]] -= 1
		d[s[i][1]] += 1
		ans.append(s[i][1])
	else:
		if i < n-1 and s[i+1].strip(s[i]) != '' and d[s[i+1].strip(s[i])] == 0:
			string = s[i+1].strip(s[i+1].strip(s[i]))
			d[string] += 1
			d[s[i].strip(string)] -= 1
			ans.append(s[i+1].strip(s[i+1].strip(s[i])))
		else:
			d[s[i][0]] += 1
			d[s[i][1]] -= 1
			ans.append(s[i][0])
print('Yes')
for i in range(len(ans)):
	print(ans[i])

Submission status: Accepted

Solution code:
import sys, copy, math, heapq, bisect
from itertools import accumulate
from collections import deque, defaultdict, Counter
sys.setrecursionlimit(500000)


N,A,B,C = map(int,input().split())
ABC = [A,B,C]
S = []
SS = ('A','B','C')
d = [0,0,0]
flag = True
for i in range(N):
    s = input()
    if s=="AB":
        S.append(2)
        d[0] += 1
        d[1] += 1
    elif s=="BC":
        S.append(0)
        d[1] += 1
        d[2] += 1
    else:
        S.append(1)
        d[2] += 1
        d[0] += 1
ans = []
for i in range(N):
    x,y = (S[i]+1)%3,(S[i]+2)%3
    if ABC[x]<ABC[y]:
        ans.append(SS[x])
        ABC[y] -= 1
        ABC[x] += 1
    elif ABC[x]>ABC[y]:
        ans.append(SS[y])
        ABC[y] += 1
        ABC[x] -= 1
    else:
        if i < N-1:
            if S[i+1]==y:
                ans.append(SS[x])
                ABC[y] -= 1
                ABC[x] += 1
            else:
                ans.append(SS[y])
                ABC[y] += 1
                ABC[x] -= 1
        else:
            ans.append(SS[y])
            ABC[y] += 1
            ABC[x] -= 1




    if ABC[0]<0 or ABC[1]<0 or ABC[2]<0:
        flag = False
        break


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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)

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

ans = []
def dfs(i,a,b,c):
  if i == n:
    print('Yes')
    for ai in ans:
      print(ai)
    exit()
  si = s[i]
  if si == 'AB':
    if a == 0 and b == 0:
      return
    elif a == 0 and b > 0:
      ans.append('A')
      dfs(i+1,a+1,b-1,c)
    elif a > 0 and b == 0:
      ans.append('B')
      dfs(i+1,a-1,b+1,c)
    else:
      ans.append('A')
      dfs(i+1,a+1,b-1,c)
      ans.pop()
      ans.append('B')
      dfs(i+1,a-1,b+1,c)
  elif si == 'BC':
    if b == 0 and c == 0:
      return
    elif b == 0 and c > 0:
      ans.append('B')
      dfs(i+1,a,b+1,c-1)
    elif b > 0 and c == 0:
      ans.append('C')
      dfs(i+1,a,b-1,c+1)
    else:
      ans.append('B')
      dfs(i+1,a,b+1,c-1)
      ans.pop()
      ans.append('C')
      dfs(i+1,a,b-1,c+1)
  else:
    if c == 0 and a == 0:
      return
    elif c ==0 and a > 0:
      ans.append('C')
      dfs(i+1,a-1,b,c+1)
    elif c > 0 and a == 0:
      ans.append('A')
      dfs(i+1,a+1,b,c-1)
    else:
      ans.append('C')
      dfs(i+1,a-1,b,c+1)
      ans.pop()
      ans.append('A')
      dfs(i+1,a+1,b,c-1)

dfs(0,a,b,c)
print('No')

Submission status: Accepted

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

DP = [[0] * 3 for _ in range(N + 1)]
DP[0] = [A, B, C]

L = [""] * N
R = {}
R[0] = "A"
R[1] = "B"
R[2] = "C"
A = []
B = []
for i in range(N):
  t = s[i]
  if t == "AB":
    x = 0
    y = 1
  if t == "BC":
    x = 1
    y = 2
  if t == "AC":
    x = 0
    y = 2
  if DP[i][y] != 0:
    DP[i + 1] = DP[i]
    DP[i + 1][x] += 1
    DP[i + 1][y] -= 1
    L[i] = R[x]
    if x == 0:
      A.append([i, y])
    else:
      B.append([i, y])
  elif DP[i][x] != 0:
    DP[i + 1] = DP[i]
    DP[i + 1][x] -= 1
    DP[i + 1][y] += 1
    L[i] = R[y]
  else:
    if x == 0:
      if len(B) == 0:
        print("No")
        exit()
      z = 1
      k, n = B.pop()
    if x == 1:
      if len(A) == 0:
        print("No")
        exit()
      z = 0
      k, n = A.pop()
    DP[i + 1] = DP[i]
    DP[i + 1][z] -= 2
    DP[i + 1][n] += 2
    if n == x:
      x, y = y, x
    DP[i + 1][x] += 1
    DP[i + 1][y] -= 1
    if DP[i + 1][z] < 0:
      print("No")
      exit()
    L[i] = R[x]
    L[k] = R[y]

print("Yes")
for i in L:
  print(i)