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:
def f(s):
    if s == "A":
        return A
    elif s == "B":
        return B
    elif s == "C":
        return C


def inc(s):
    global A, B, C

    if s == "A":
        A += 1
    elif s == "B":
        B += 1
    elif s == "C":
        C += 1


def dec(s):
    global A, B, C

    if s == "A":
        A -= 1
    elif s == "B":
        B -= 1
    elif s == "C":
        C -= 1


N, A, B, C = map(int, input().split())

if A + B + C == 0:
    print("No")

elif A + B + C == 1:
    ans = []
    for _ in range(N):
        s = input()
        if f(s[0]) == f(s[1]) == 0:
            print("No")
            exit()
        else:
            if f(s[0]) == 1:
                ans.append(s[1])
                dec(s[0])
                inc(s[1])
            elif f(s[1]) == 1:
                ans.append(s[0])
                inc(s[0])
                dec(s[1])
    print("Yes")
    for i in range(N):
        print(ans[i])

elif A + B + C >= 2:
    s = []
    for i in range(N):
        s.append(input())

    # 共に0を選ぶことは初手でしか起こり得ない
    # 2回目以降は、0は1個以下
    if f(s[0][0]) == f(s[0][1]) == 0:
        print("No")
        exit()

    ans = []
    for i in range(len(s)):
        if f(s[i][0]) >= 1 and f(s[i][1]) == 0:
            ans.append(s[i][1])
            dec(s[i][0])
            inc(s[i][1])
        elif f(s[i][0]) == 0 and f(s[i][1]) >= 1:
            ans.append(s[i][0])
            inc(s[i][0])
            dec(s[i][1])
        # (1,1,0)の状態で(1,1)を選んだ時 and 最後の手ではない and 次に違う手が予定されている
        elif (A + B + C == 2) and (f(s[i][0]) == f(s[i][1]) == 1) and (i < N - 1):
            # s[i][0]を次に操作する場合
            if s[i][0] == s[i + 1][0] or s[i][0] == s[i + 1][1]:
                ans.append(s[i][0])
                inc(s[i][0])
                dec(s[i][1])
            # s[i][1]を次に操作する場合
            elif s[i][1] == s[i + 1][0] or s[i][1] == s[i + 1][1]:
                ans.append(s[i][1])
                dec(s[i][0])
                inc(s[i][1])
        else:
            if f(s[i][0]) <= f(s[i][1]):
                ans.append(s[i][0])
                inc(s[i][0])
                dec(s[i][1])
            else:
                ans.append(s[i][1])
                inc(s[i][1])
                dec(s[i][0])

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

Submission status: Accepted

Solution code:
def f(s):
    if s == "A":
        return A
    elif s == "B":
        return B
    elif s == "C":
        return C


def inc(s):
    global A, B, C

    if s == "A":
        A += 1
    elif s == "B":
        B += 1
    elif s == "C":
        C += 1


def dec(s):
    global A, B, C

    if s == "A":
        A -= 1
    elif s == "B":
        B -= 1
    elif s == "C":
        C -= 1


N, A, B, C = map(int, input().split())

if A + B + C == 0:
    print("No")

elif A + B + C == 1:
    ans = []
    for _ in range(N):
        s = input()
        if f(s[0]) == f(s[1]) == 0:
            print("No")
            exit()
        else:
            if f(s[0]) == 1:
                ans.append(s[1])
                dec(s[0])
                inc(s[1])
            elif f(s[1]) == 1:
                ans.append(s[0])
                inc(s[0])
                dec(s[1])
    print("Yes")
    for i in range(N):
        print(ans[i])

elif A + B + C >= 2:
    s = []
    for i in range(N):
        s.append(input())

    if f(s[0][0]) == f(s[0][1]) == 0:
        print("No")
        exit()

    ans = []
    for i in range(len(s)):
        if f(s[i][0]) >= 1 and f(s[i][1]) == 0:
            ans.append(s[i][1])
            dec(s[i][0])
            inc(s[i][1])
        elif f(s[i][0]) == 0 and f(s[i][1]) >= 1:
            ans.append(s[i][0])
            inc(s[i][0])
            dec(s[i][1])
        elif (A + B + C == 2) and (f(s[i][0]) == f(s[i][1]) == 1) and (i < N - 1) and (s[i] != s[i + 1]):
            if s[i][0] == s[i + 1][0] or s[i][0] == s[i + 1][1]:
                ans.append(s[i][0])
                inc(s[i][0])
                dec(s[i][1])
            elif s[i][1] == s[i + 1][0] or s[i][1] == s[i + 1][1]:
                ans.append(s[i][1])
                dec(s[i][0])
                inc(s[i][1])
        else:
            ###ここらへんちょっと改変###
            if f(s[i][0]) == 1:
                ans.append(s[i][0])
                inc(s[i][0])
                dec(s[i][1])
            else:
                ans.append(s[i][1])
                inc(s[i][1])
                dec(s[i][0])
                
    print("Yes")
    for i in range(N):
        print(ans[i])

Submission status: Accepted

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

if A + B + C == 0:
    print("No")

#########################

elif A + B + C == 1:
    ans = []
    for i in range(N):
        # (1,0,0)
        if A == 1:
            if S[i] == "BC":
                print("No")
                exit()
            elif S[i] == "AB":
                ans.append("B")
                A, B, C = 0, 1, 0
            elif S[i] == "AC":
                ans.append("C")
                A, B, C = 0, 0, 1
        # (0,1,0)
        elif B == 1:
            if S[i] == "AC":
                print("No")
                exit()
            elif S[i] == "AB":
                ans.append("A")
                A, B, C = 1, 0, 0
            elif S[i] == "BC":
                ans.append("C")
                A, B, C = 0, 0, 1
        # (0,0,1)
        elif C == 1:
            if S[i] == "AB":
                print("No")
                exit()
            elif S[i] == "BC":
                ans.append("B")
                A, B, C = 0, 1, 0
            elif S[i] == "AC":
                ans.append("A")
                A, B, C = 1, 0, 0

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

#########################

elif A + B + C >= 2:
    if S[0] == "AB" and A == B == 0:
        print("No")
    elif S[0] == "BC" and B == C == 0:
        print("No")
    elif S[0] == "AC" and A == C == 0:
        print("No")
    else:
        ans = []
        #########################
        if A + B + C >= 3:
            for i in range(N):
                if S[i] == "AB":
                    if A >= B:
                        A -= 1
                        B += 1
                        ans.append("B")
                    else:
                        A += 1
                        B -= 1
                        ans.append("A")
                elif S[i] == "AC":
                    if A >= C:
                        A -= 1
                        C += 1
                        ans.append("C")
                    else:
                        A += 1
                        C -= 1
                        ans.append("A")
                elif S[i] == "BC":
                    if B >= C:
                        B -= 1
                        C += 1
                        ans.append("C")
                    else:
                        B += 1
                        C -= 1
                        ans.append("B")
        #########################
        elif A + B + C == 2:
            for i in range(N):
                if A == B == 1 and S[i] == "AB" and i < N - 1 and S[i] != S[i + 1]:
                    if S[i + 1] == "AC":
                        ans.append("A")
                        A += 1
                        B -= 1
                    elif S[i + 1] == "BC":
                        ans.append("B")
                        A -= 1
                        B += 1
                elif A == C == 1 and S[i] == "AC" and i < N - 1 and S[i] != S[i + 1]:
                    if S[i + 1] == "AB":
                        ans.append("A")
                        A += 1
                        C -= 1
                    elif S[i + 1] == "BC":
                        ans.append("C")
                        A -= 1
                        C += 1
                elif B == C == 1 and S[i] == "BC" and i < N - 1 and S[i] != S[i + 1]:
                    if S[i + 1] == "AB":
                        ans.append("B")
                        B += 1
                        C -= 1
                    elif S[i + 1] == "AC":
                        ans.append("C")
                        B -= 1
                        C += 1
                else:
                    if S[i] == "AB":
                        if A >= B:
                            A -= 1
                            B += 1
                            ans.append("B")
                        else:
                            A += 1
                            B -= 1
                            ans.append("A")
                    elif S[i] == "AC":
                        if A >= C:
                            A -= 1
                            C += 1
                            ans.append("C")
                        else:
                            A += 1
                            C -= 1
                            ans.append("A")
                    else:
                        if B >= C:
                            B -= 1
                            C += 1
                            ans.append("C")
                        else:
                            B += 1
                            C -= 1
                            ans.append("B")
        print("Yes")
        for i in range(N):
            print(ans[i])

Submission status: Accepted

Solution code:
import sys
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def S(): return sys.stdin.readline().rstrip()


N,A,B,C = MI()
X = [S() for _ in range(N)]

if A+B+C == 0:
    print('No')

elif A+B+C == 1:
    ANS = []
    for i in range(N):
        if A == 1:
            if X[i] == 'BC':
                print('No')
                break
            elif X[i] == 'AB':
                ANS.append('B')
                A,B,C = 0,1,0
            else:
                ANS.append('C')
                A,B,C = 0,0,1
        elif B == 1:
            if X[i] == 'AC':
                print('No')
                break
            elif X[i] == 'AB':
                ANS.append('A')
                A,B,C = 1,0,0
            else:
                ANS.append('C')
                A,B,C = 0,0,1
        else:
            if X[i] == 'AB':
                print('No')
                break
            elif X[i] == 'BC':
                ANS.append('B')
                A,B,C = 0,1,0
            else:
                ANS.append('A')
                A,B,C = 1,0,0
    else:
        print('Yes')
        print(*ANS,sep='\n')

else:
    if X[0] == 'AB' and C == A+B+C:
        print('No')
    elif X[0] == 'BC' and A == A+B+C:
        print('No')
    elif X[0] == 'AC' and B == A+B+C:
        print('No')
    else:
        print('Yes')
        ANS = []
        if A+B+C >= 3:
            for i in range(N):
                if X[i] == 'AB':
                    if A > B:
                        A -= 1
                        B += 1
                        ANS.append('B')
                    else:
                        A += 1
                        B -= 1
                        ANS.append('A')
                elif X[i] == 'AC':
                    if A > C:
                        A -= 1
                        C += 1
                        ANS.append('C')
                    else:
                        A += 1
                        C -= 1
                        ANS.append('A')
                else:
                    if B > C:
                        B -= 1
                        C += 1
                        ANS.append('C')
                    else:
                        B += 1
                        C -= 1
                        ANS.append('B')
        else:
            for i in range(N):
                if A == B == 1 and X[i] == 'AB' and i < N-1:
                    if X[i+1] == 'AC':
                        ANS.append('A')
                        A += 1
                        B -= 1
                    else:
                        ANS.append('B')
                        A -= 1
                        B += 1
                elif A == C == 1 and X[i] == 'AC' and i < N-1:
                    if X[i+1] == 'AB':
                        ANS.append('A')
                        A += 1
                        C -= 1
                    else:
                        ANS.append('C')
                        A -= 1
                        C += 1
                elif B == C == 1 and X[i] == 'BC' and i < N-1:
                    if X[i+1] == 'AB':
                        ANS.append('B')
                        B += 1
                        C -= 1
                    else:
                        ANS.append('C')
                        B -= 1
                        C += 1
                else:
                    if X[i] == 'AB':
                        if A > B:
                            A -= 1
                            B += 1
                            ANS.append('B')
                        else:
                            A += 1
                            B -= 1
                            ANS.append('A')
                    elif X[i] == 'AC':
                        if A > C:
                            A -= 1
                            C += 1
                            ANS.append('C')
                        else:
                            A += 1
                            C -= 1
                            ANS.append('A')
                    else:
                        if B > C:
                            B -= 1
                            C += 1
                            ANS.append('C')
                        else:
                            B += 1
                            C -= 1
                            ANS.append('B')

        print(*ANS,sep='\n')

Submission status: Accepted

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

if A==B==C==0:
    print('No')
    exit()

if A+B+C == 1:
    ans = []
    for s in S:
        if (s=='AB' and A+B==0) or (s=='AC' and A+C==0) or (s=='BC' and B+C==0):
            print('No')
            exit()
        if s=='AB':
            if A<B:
                ans.append('A')
            else:
                ans.append('B')
            A,B = B,A
        if s=='AC':
            if A<C:
                ans.append('A')
            else:
                ans.append('C')
            A,C = C,A
        if s=='BC':
            if B<C:
                ans.append('B')
            else:
                ans.append('C')
            B,C = C,B
    print('Yes')
    print(*ans, sep='\n')

else:
    ans = []
    for i,s in enumerate(S):
        if (s=='AB' and A+B==0) or (s=='AC' and A+C==0) or (s=='BC' and B+C==0):
            print('No')
            exit()
        if s=='AB':
            if A==0:
                ans.append('A')
                A += 1
                B -= 1
            elif B==0:
                ans.append('B')
                B += 1
                A -= 1
            elif A==B==1:
                if i == N-1 or C > 0 or S[i+1]=='AB':
                    ans.append('A')
                    A += 1
                    B -= 1
                elif S[i+1]=='AC':
                    ans.append('A')
                    A += 1
                    B -= 1
                else:
                    ans.append('B')
                    B += 1
                    A -= 1
            elif A < B:
                ans.append('A')
                A += 1
                B -= 1
            else:
                ans.append('B')
                B += 1
                A -= 1
        if s=='AC':
            if A==0:
                ans.append('A')
                A += 1
                C -= 1
            elif C==0:
                ans.append('C')
                C += 1
                A -= 1
            elif A==C==1:
                if i == N-1 or B > 0 or S[i+1]=='AC':
                    ans.append('C')
                    C += 1
                    A -= 1
                elif S[i+1]=='AB':
                    ans.append('A')
                    A += 1
                    C -= 1
                else:
                    ans.append('C')
                    C += 1
                    A -= 1
            elif C < A:
                ans.append('C')
                C += 1
                A -= 1
            else:
                ans.append('A')
                A += 1
                C -= 1
        if s=='BC':
            if B==0:
                ans.append('B')
                B += 1
                C -= 1
            elif C==0:
                ans.append('C')
                C += 1
                B -= 1
            elif B==C==1:
                if i == N-1 or A > 0 or S[i+1]=='BC':
                    ans.append('B')
                    B += 1
                    C -= 1
                elif S[i+1]=='AB':
                    ans.append('B')
                    B += 1
                    C -= 1
                else:
                    ans.append('C')
                    C += 1
                    B -= 1
            elif B < C:
                ans.append('B')
                B += 1
                C -= 1
            else:
                ans.append('C')
                C += 1
                B -= 1
    print('Yes')
    print(*ans, sep='\n')

Submission status: Accepted

Solution code:
from sys import stdin
N,A, B, C = map(int, input().split())

S = stdin.read().split()


if (A and B or B and C or C and A) and A+B+C >= 3:
    ans = []
    ans.append('Yes')
    for s in S:
        if s == 'AB':
            if A < B:
                A += 1
                B -= 1
                ans.append('A')
            else:
                A -= 1
                B += 1
                ans.append('B')
        elif s == 'AC':
            if A < C:
                C -= 1
                A += 1
                ans.append('A')
            else:
                A -= 1
                C += 1
                ans.append('C')
        else:
            if B < C:
                C -= 1
                B += 1
                ans.append('B')
            else:
                B -= 1
                C += 1
                ans.append('C')
    print('\n'.join(ans))
    exit()

ans = []
for i, s in enumerate(S):
    if s == 'AB':
        if A==B==1 and C==0 and i < N-1:
            if S[i+1] == 'AC':
                A += 1
                B -= 1
                ans.append('A')
                continue
            elif S[i+1] == 'BC':
                A -= 1
                B += 1
                ans.append('B')
                continue
        if A < B:
            A += 1
            B -= 1
            ans.append('A')
        else:
            A -= 1
            B += 1
            ans.append('B')

    elif s == 'AC':
        if A==C==1 and B==0 and i < N-1:
            if S[i+1] == 'AB':
                A += 1
                C -= 1
                ans.append('A')
                continue
            elif S[i+1] == 'BC':
                A -= 1
                C += 1
                ans.append('C')
                continue
        if A < C:
            A += 1
            C -= 1
            ans.append('A')
        else:
            A -= 1
            C += 1
            ans.append('C')
    else:
        if B==C==1 and A==0 and i < N-1:
            if S[i+1] == 'AB':
                B += 1
                C -= 1
                ans.append('B')
                continue
            elif S[i+1] == 'AC':
                B -= 1
                C += 1
                ans.append('C')
                continue
        if B < C:
            B += 1
            C -= 1
            ans.append('B')
        else:
            B -= 1
            C += 1
            ans.append('C')

    if A < 0 or B < 0 or C < 0:
        print('No')
        exit()

print('Yes')
print('\n'.join(ans))

Submission status: Accepted

Solution code:
N,A,B,C = map(int,input().split())
C = {"A":A,"B":B,"C":C}
S = [input().strip() for _ in range(N)]
D = []
flag = 0
for i in range(N):
    a,b = S[i][0],S[i][1]
    if C[a]>C[b]:
        C[a] -= 1
        C[b] += 1
        D.append(b)
    elif C[b]>C[a]:
        C[b] -= 1
        C[a] += 1
        D.append(a)
    else:
        if C[a]>1:
            C[a] -= 1
            C[b] += 1
            D.append(b)
        elif C[a]==1:
            if i==N-1:
                D.append(b)
            elif S[i]==S[i+1]:
                C[a] -= 1
                C[b] += 1
                D.append(b)
            else:
                if a in S[i+1]:
                    C[a] += 1
                    C[b] -= 1
                    D.append(a)
                else:
                    C[a] -= 1
                    C[b] += 1
                    D.append(b)
        else:
            flag = 1
            break
    if C["A"]<0 or C["B"]<0 or C["C"]<0:
        flag = 1
        break
if flag==1:
    print("No")
else:
    print("Yes")
    for i in range(N):
        print(D[i])

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  #空白あり
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし


N,A,B,C = MI()
X = [S() for _ in range(N)]

if A+B+C == 0:
    print('No')

elif A+B+C == 1:
    ANS = []
    for i in range(N):
        if A == 1:
            if X[i] == 'BC':
                print('No')
                break
            elif X[i] == 'AB':
                ANS.append('B')
                A,B,C = 0,1,0
            else:
                ANS.append('C')
                A,B,C = 0,0,1
        elif B == 1:
            if X[i] == 'AC':
                print('No')
                break
            elif X[i] == 'AB':
                ANS.append('A')
                A,B,C = 1,0,0
            else:
                ANS.append('C')
                A,B,C = 0,0,1
        else:
            if X[i] == 'AB':
                print('No')
                break
            elif X[i] == 'BC':
                ANS.append('B')
                A,B,C = 0,1,0
            else:
                ANS.append('A')
                A,B,C = 1,0,0
    else:
        print('Yes')
        print(*ANS,sep='\n')

else:
    if X[0] == 'AB' and C == A+B+C:
        print('No')
    elif X[0] == 'BC' and A == A+B+C:
        print('No')
    elif X[0] == 'AC' and B == A+B+C:
        print('No')
    else:
        print('Yes')
        ANS = []
        if A+B+C >= 3:
            for i in range(N):
                if X[i] == 'AB':
                    if A > B:
                        A -= 1
                        B += 1
                        ANS.append('B')
                    else:
                        A += 1
                        B -= 1
                        ANS.append('A')
                elif X[i] == 'AC':
                    if A > C:
                        A -= 1
                        C += 1
                        ANS.append('C')
                    else:
                        A += 1
                        C -= 1
                        ANS.append('A')
                else:
                    if B > C:
                        B -= 1
                        C += 1
                        ANS.append('C')
                    else:
                        B += 1
                        C -= 1
                        ANS.append('B')
        else:
            for i in range(N):
                if A == B == 1 and X[i] == 'AB' and i < N-1:
                    if X[i+1] == 'AC':
                        ANS.append('A')
                        A += 1
                        B -= 1
                    else:
                        ANS.append('B')
                        A -= 1
                        B += 1
                elif A == C == 1 and X[i] == 'AC' and i < N-1:
                    if X[i+1] == 'AB':
                        ANS.append('A')
                        A += 1
                        C -= 1
                    else:
                        ANS.append('C')
                        A -= 1
                        C += 1
                elif B == C == 1 and X[i] == 'BC' and i < N-1:
                    if X[i+1] == 'AB':
                        ANS.append('B')
                        B += 1
                        C -= 1
                    else:
                        ANS.append('C')
                        B -= 1
                        C += 1
                else:
                    if X[i] == 'AB':
                        if A > B:
                            A -= 1
                            B += 1
                            ANS.append('B')
                        else:
                            A += 1
                            B -= 1
                            ANS.append('A')
                    elif X[i] == 'AC':
                        if A > C:
                            A -= 1
                            C += 1
                            ANS.append('C')
                        else:
                            A += 1
                            C -= 1
                            ANS.append('A')
                    else:
                        if B > C:
                            B -= 1
                            C += 1
                            ANS.append('C')
                        else:
                            B += 1
                            C -= 1
                            ANS.append('B')

        print(*ANS,sep='\n')

Submission status: Accepted

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

ans = []
for i in range(N):
  if IN[i] == "AB":
    if A== 0 and B == 0:
      print("No")
      exit()
    elif A == 1 and B ==1:
      if i == N-1: #最後ならどちらでもよい
        ans.append("A")
      else:
        if IN[i+1] == "AC": #次に来るのがACならAを補完
          A += 1; B -=1
          ans.append("A")
        elif IN[i+1] == "BC":#次に来るのがBCならBを補完
          A -=1; B += 1
          ans.append("B")
        else: #次がABならどっちでもよい
          A += 1; B-=1
          ans.append("A")
    else:
      if A>=B:
        A-=1;B+=1
        ans.append("B")
      else:
        A+=1;B-=1
        ans.append("A")
  
  elif IN[i] == "AC":
    if A== 0 and C == 0:
      print("No")
      exit()
    elif A == 1 and C ==1:
      if i == N-1: #最後ならどちらでもよい
        ans.append("A")
      else:
        if IN[i+1] == "AB": #次に来るのがABならAを補完
          A += 1; C -=1
          ans.append("A")
        elif IN[i+1] == "BC":#次に来るのがBCならCを補完
          A -=1; C += 1
          ans.append("C")
        else: #次がACならどっちでもよい
          A += 1; C-=1
          ans.append("A")
    else:
      if A>=C:
        A-=1;C+=1
        ans.append("C")
      else:
        A+=1;C-=1
        ans.append("A")
        
  else: #IN[i] == "BC":
    if B == 0 and C == 0:
      print("No")
      exit()
    elif B == 1 and C ==1:
      if i == N-1: #最後ならどちらでもよい
        ans.append("B")
      else:
        if IN[i+1] == "AB": #次に来るのがABならBを補完
          B += 1; C -=1
          ans.append("B")
        elif IN[i+1] == "AC":#次に来るのがACならCを補完
          B -=1; C += 1
          ans.append("C")
        else: #次がBCならどっちでもよい
          B += 1; C-=1
          ans.append("B")
    else:
      if B>=C:
        B-=1;C+=1
        ans.append("C")
      else:
        B+=1;C-=1
        ans.append("B")
print("Yes")
print(*ans,sep="\n")

Submission status: Accepted

Solution code:
n,a,b,c=map(int,input().split())
s=[input()for _ in range(n)]
if (a+b+c)==0:
  print("No")
if (a+b+c)==1 or (a+b+c)>=3:
  ans=[]
  for i in s:
    if i=="AB":
      if a<b:ans.append("A");a+=1;b+=-1;c+=0
      else:ans.append("B");a+=-1;b+=1;c+=0
    if i=="BC":
      if b<c:ans.append("B");a+=0;b+=1;c+=-1
      else:ans.append("C");a+=0;b+=-1;c+=1
    if i=="AC":
      if a<c:ans.append("A");a+=1;b+=0;c+=-1
      else:ans.append("C");a+=-1;b+=0;c+=1
    if min(a,b,c)<0:
      exit(print("No"))
  print("Yes")
  for i in ans:print(i)
if (a+b+c)==2:
  ans=[]
  for i in range(n-1):
    if s[i]=="AB":
      if a==b:
        if s[i+1]=="AC":ans.append("A");a+=1;b+=-1;c+=0
        else:ans.append("B");a+=-1;b+=1;c+=0
      elif a<b:ans.append("A");a+=1;b+=-1;c+=0
      else:ans.append("B");a+=-1;b+=1;c+=0
    if s[i]=="BC":
      if b==c:
        if s[i+1]=="AC":ans.append("C");a+=0;b+=-1;c+=1
        else:ans.append("B");a+=0;b+=1;c+=-1
      elif b<c:ans.append("B");a+=0;b+=1;c+=-1
      else:ans.append("C");a+=0;b+=-1;c+=1
    if s[i]=="AC":
      if a==c:
        if s[i+1]=="BC":ans.append("C");a+=-1;b+=0;c+=1
        else:ans.append("A");a+=1;b+=0;c+=-1
      elif a<c:ans.append("A");a+=1;b+=0;c+=-1
      else:ans.append("C");a+=-1;b+=0;c+=1
    if min(a,b,c)<0:
      exit(print("No"))
  for i in s[n-1:]:
    if i=="AB":
      if a<b:ans.append("A");a+=1;b+=-1;c+=0
      else:ans.append("B");a+=-1;b+=1;c+=0
    if i=="BC":
      if b<c:ans.append("B");a+=0;b+=1;c+=-1
      else:ans.append("C");a+=0;b+=-1;c+=1
    if i=="AC":
      if a<c:ans.append("A");a+=1;b+=0;c+=-1
      else:ans.append("C");a+=-1;b+=0;c+=1
    if min(a,b,c)<0:
      exit(print("No"))
  print("Yes")
  for i in ans:print(i)