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:
n,a,b,c = map(int,input().split())
s_list = []
for i in range(n):
    s = input()
    s_list.append(s)

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

move = []
if a+b+c == 1:
    for i in range(n):
        if s_list[i] == "AB":
            if a == 0:
                a+=1
                b-=1
                move.append("A")
            else:
                b+=1
                a-=1
                move.append("B")
        if s_list[i] == "BC":
            if b == 0:
                b+=1
                c-=1
                move.append("B")
            else:
                c+=1
                b-=1
                move.append("C")
        if s_list[i] == "AC":
            if a == 0:
                a+=1
                c-=1
                move.append("A")
            else:
                c+=1
                a-=1
                move.append("C")
        if min(a,b,c) < 0:
            print("No")
            exit()

    print("Yes")
    for i in move:
        print(i)            
    
else:
    for i in range(n):
        if a+b+c == 2:
            if i < n-1:
                s_next = s_list[i+1]
            if s_list[i] == "AB":
                if a == 1 and b == 1 and s_next != "AB":
                    if s_next == "BC":
                        b+=1
                        a-=1
                        move.append("B")
                    else:
                        a+=1
                        b-=1
                        move.append("A")
                elif a == 0:
                    a+=1
                    b-=1
                    move.append("A")
                elif b == 0:
                    b+=1
                    a-=1
                    move.append("B")
                else:
                    a+=1
                    b-=1
                    move.append("A")
            if s_list[i] == "BC":
                if b == 1 and c == 1 and s_next != "BC":
                    if s_next == "AB":
                        b+=1
                        c-=1
                        move.append("B")
                    else:
                        c+=1
                        b-=1
                        move.append("C")
                if b == 0:
                    b+=1
                    c-=1
                    move.append("B")
                elif c == 0:
                    c+=1
                    b-=1
                    move.append("C")
                else:
                    b+=1
                    c-=1
                    move.append("B")
            if s_list[i] == "AC":
                if a == 1 and c == 1 and s_next != "AC":
                    if s_next == "AB":
                        a+=1
                        c-=1
                        move.append("A")
                    else:
                        c+=1
                        a-=1
                        move.append("C")
                if a == 0:
                    a+=1
                    c-=1
                    move.append("A")
                elif c == 0:
                    c+=1
                    a-=1
                    move.append("C")
                else:
                    a+=1
                    c-=1
                    move.append("A")     
        else:       
            if s_list[i] == "AB":
                if a == 0:
                    a+=1
                    b-=1
                    move.append("A")
                elif b == 0:
                    b+=1
                    a-=1
                    move.append("B")
                else:
                    if a < b:
                        a+=1
                        b-=1
                        move.append("A")
                    else:
                        b+=1
                        a-=1
                        move.append("B")


            if s_list[i] == "BC":
                if b == 0:
                    b+=1
                    c-=1
                    move.append("B")
                elif c == 0:
                    c+=1
                    b-=1
                    move.append("C")
                else:
                    if b < c:
                        b+=1
                        c-=1
                        move.append("B")
                    else:
                        c+=1
                        b-=1
                        move.append("C")
                        
            if s_list[i] == "AC":
                if a == 0:
                    a+=1
                    c-=1
                    move.append("A")
                elif c == 0:
                    c+=1
                    a-=1
                    move.append("C")
                else:
                    if a < c:
                        a+=1
                        c-=1
                        move.append("A")
                    else:
                        c+=1
                        a-=1
                        move.append("C")
                    
        if min(a,b,c) < 0:
            print("No")
            exit()
    print("Yes")
    for i in move:
        print(i)

Submission status: Wrong Answer

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

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

move = []
if a+b+c == 1:
    for i in range(n):
        if s_list[i] == "AB":
            if a == 0:
                a+=1
                b-=1
                move.append("A")
            else:
                b+=1
                a-=1
                move.append("B")
        if s_list[i] == "BC":
            if b == 0:
                b+=1
                c-=1
                move.append("B")
            else:
                c+=1
                b-=1
                move.append("C")
        if s_list[i] == "AC":
            if a == 0:
                a+=1
                c-=1
                move.append("A")
            else:
                c+=1
                a-=1
                move.append("C")
        if min(a,b,c) < 0:
            print("No")
            exit()

    print("Yes")
    for i in move:
        print(i)            
    
else:
    for i in range(n):
        if a+b+c == 2:
            if i < n-1:
                s_next = s_list[i+1]
            if s_list[i] == "AB":
                if a == 1 and b == 1 and s_next != "AB":
                    if s_next == "BC":
                        b+=1
                        a-=1
                        move.append("B")
                    else:
                        a+=1
                        b-=1
                        move.append("A")
                elif a == 0:
                    a+=1
                    b-=1
                    move.append("A")
                elif b == 0:
                    b+=1
                    a-=1
                    move.append("B")
                else:
                    a+=1
                    b-=1
                    move.append("A")
            if s_list[i] == "BC":
                if b == 1 and c == 1 and s_next != "BC":
                    if s_next == "AB":
                        b+=1
                        c-=1
                        move.append("B")
                    else:
                        c+=1
                        b-=1
                        move.append("C")
                if b == 0:
                    b+=1
                    c-=1
                    move.append("B")
                elif c == 0:
                    c+=1
                    b-=1
                    move.append("C")
                else:
                    b+=1
                    c-=1
                    move.append("B")
            if s_list[i] == "AC":
                if a == 1 and c == 1 and s_next != "AC":
                    if s_next == "AB":
                        a+=1
                        c-=1
                        move.append("A")
                    else:
                        c+=1
                        a-=1
                        move.append("C")
                if a == 0:
                    a+=1
                    c-=1
                    move.append("A")
                elif c == 0:
                    c+=1
                    a-=1
                    move.append("C")
                else:
                    a+=1
                    c-=1
                    move.append("A")
        if s_list[i] == "AB":
            if a == 0:
                a+=1
                b-=1
                move.append("A")
            elif b == 0:
                b+=1
                a-=1
                move.append("B")
            else:
                if a < b:
                    a+=1
                    b-=1
                    move.append("A")
                else:
                    b+=1
                    a-=1
                    move.append("B")


        if s_list[i] == "BC":
            if b == 0:
                b+=1
                c-=1
                move.append("B")
            elif c == 0:
                c+=1
                b-=1
                move.append("C")
            else:
                if b < c:
                    b+=1
                    c-=1
                    move.append("B")
                else:
                    c+=1
                    b-=1
                    move.append("C")
                    
        if s_list[i] == "AC":
            if a == 0:
                a+=1
                c-=1
                move.append("A")
            elif c == 0:
                c+=1
                a-=1
                move.append("C")
            else:
                if a < c:
                    a+=1
                    c-=1
                    move.append("A")
                else:
                    c+=1
                    a-=1
                    move.append("C")
                    
        if min(a,b,c) < 0:
            print("No")
            exit()
    print("Yes")
    for i in move:
        print(i)

Submission status: Wrong Answer

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

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

move = []
if a+b+c == 1:
    for i in range(n):
        if s_list[i] == "AB":
            if a == 0:
                a+=1
                b-=1
                move.append("A")
            else:
                b+=1
                a-=1
                move.append("B")
        if s_list[i] == "BC":
            if b == 0:
                b+=1
                c-=1
                move.append("B")
            else:
                c+=1
                b-=1
                move.append("C")
        if s_list[i] == "AC":
            if a == 0:
                a+=1
                c-=1
                move.append("A")
            else:
                c+=1
                a-=1
                move.append("C")
        if min(a,b,c) < 0:
            print("No")
            exit()

    print("Yes")
    for i in move:
        print(i)            
    
else:
    for i in range(n):
        if a+b+c == 2:
            if i < n-1:
                s_next = s_list[i+1]
            if s_list[i] == "AB":
                if a == 1 and b == 1 and s_next != "AB":
                    if s_next == "BC":
                        b+=1
                        a-=1
                        move.append("B")
                    else:
                        a+=1
                        b-=1
                        move.append("A")
                elif a == 0:
                    a+=1
                    b-=1
                    move.append("A")
                elif b == 0:
                    b+=1
                    a-=1
                    move.append("B")
                else:
                    a+=1
                    b-=1
                    move.append("A")
            if s_list[i] == "BC":
                if b == 1 and c == 1 and s_next != "BC":
                    if s_next == "AB":
                        b+=1
                        c-=1
                        move.append("B")
                    else:
                        c+=1
                        b-=1
                        move.append("C")
                if b == 0:
                    b+=1
                    c-=1
                    move.append("B")
                elif c == 0:
                    c+=1
                    b-=1
                    move.append("C")
                else:
                    b+=1
                    c-=1
                    move.append("B")
            if s_list[i] == "AC":
                if a == 1 and c == 1 and s_next != "AC":
                    if s_next == "AB":
                        a+=1
                        c-=1
                        move.append("A")
                    else:
                        c+=1
                        a-=1
                        move.append("C")
                if a == 0:
                    a+=1
                    c-=1
                    move.append("A")
                elif c == 0:
                    c+=1
                    a-=1
                    move.append("C")
                else:
                    a+=1
                    c-=1
                    move.append("A")
        if s_list[i] == "AB":
            if a == 0:
                a+=1
                b-=1
                move.append("A")
            elif b == 0:
                b+=1
                a-=1
                move.append("B")
            else:
                a+=1
                b-=1
                move.append("A")

        if s_list[i] == "BC":
            if b == 0:
                b+=1
                c-=1
                move.append("B")
            elif c == 0:
                c+=1
                b-=1
                move.append("C")
            else:
                b+=1
                c-=1
                move.append("B")
        if s_list[i] == "AC":
            if a == 0:
                a+=1
                c-=1
                move.append("A")
            elif c == 0:
                c+=1
                a-=1
                move.append("C")
            else:
                a+=1
                c-=1
                move.append("A")
        if min(a,b,c) < 0:
            print("No")
            exit()
    print("Yes")
    for i in move:
        print(i)

Submission status: Wrong Answer

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

if sum(d.values()) == 0:
    print('No')
elif sum(d.values()) == 1:
    ans = []
    for s in S:
        if d[s[0]] == d[s[1]] == 0:
            print('No')
            exit()
        elif d[s[0]] > d[s[1]]:
            d[s[0]] -= 1
            d[s[1]] += 1
            ans.append(s[1])
        else:
            d[s[0]] += 1
            d[s[1]] -= 1
            ans.append(s[0])
    print('Yes')
    print(*ans, sep='\n')
else:
    ans = []
    for i, s in enumerate(S):
        if d[s[0]] == d[s[1]]:
            print('No')
            exit()
        elif d[s[0]] != 0 and d[s[1]] == 0:
            d[s[0]] -= 1
            d[s[1]] += 1
            ans.append(s[1])
        elif d[s[0]] == 0 and d[s[1]] != 0:
            d[s[0]] += 1
            d[s[1]] -= 1
            ans.append(s[0])
        elif i != N-1 and S[i] != S[i+1]:
            # elif sum(d.values()) == 2 and d[s[0]] == d[s[1]] == 1 and i != N-1 and S[i] != S[i+1]:
            if s[0] in S[i+1]:
                d[s[0]] += 1
                d[s[1]] -= 1
                ans.append(s[0])
            else:
                d[s[0]] -= 1
                d[s[1]] += 1
                ans.append(s[1])
        else:
            d[s[0]] -= 1
            d[s[1]] += 1
            ans.append(s[1])
    print('Yes')
    print(*ans, sep='\n')

Submission status: Wrong Answer

Solution code:

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

x = [A, B, C]
d = {"A": 0, "B": 1, "C": 2}
if sum(x) == 0:
    print("No")
else:
    flag = True
    ans = []
    for s in S:
        n, m = list(s)
        if x[d[n]] > x[d[m]]:
            x[d[n]] -= 1
            x[d[m]] += 1
            ans.append(m)
        elif x[d[n]] <= x[d[m]] and not (x[d[n]] == x[d[m]] == 0):
            x[d[n]] += 1
            x[d[m]] -= 1
            ans.append(n)
        else:
            flag = False
            break

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
if a+b+c==0:
    print("No")
elif a+b+c==1:
    d=[]
    for i in range(n):
        s=input()
        if s=="AB":
            if a+b==0:
                print("No")
                exit()
            else:
                if a==0:
                    d.append("A")
                else:
                    d.append("B")
                a,b=b,a
        if s=="BC":
            if c+b==0:
                print("No")
                exit()
            else:
                if b==0:
                    d.append("B")
                else:
                    d.append("C")
                c,b=b,c
        if s=="AC":
            if a+c==0:
                print("No")
                exit()
            else:
                if a==0:
                    d.append("A")
                else:
                    d.append("C")
                a,c=c,a 
    print("Yes")
    for i in d:
        print(i)
else:
    d=[]
    s=[input()for i in range(n)]+["AB"]
    for i in range(n):
        if s[i]=="AB":
            if a+b==0:
                print("No")
                exit()
            if a+b+c==2 and a==b==1 and s[i+1]!=s[i]:
                if s[i]=="AC":
                    d.append("A")
                    a+=1
                    b-=1
                else:
                    d.append("B")
                    b+=1
                    a-=1
            elif a==0:
                d.append("A")
                a+=1
                b-=1
            else:
                d.append("B")
                b+=1
                a-=1
        elif s[i]=="BC":
            if c+b==0:
                print("No")
                exit()
            if a+b+c==2 and c==b==1 and s[i+1]!=s[i]:
                if s[i]=="AB":
                    d.append("B")
                    c-=1
                    b+=1
                else:
                    d.append("C")
                    c+=1
                    b-=1
            elif b==0:
                d.append("B")
                b+=1
                c-=1
            else:
                d.append("C")
                c+=1
                b-=1
        elif s[i]=="AC":
            if c+a==0:
                print("No")
                exit()
            if a+b+c==2 and c==a==1 and s[i+1]!=s[i]:
                if s[i]=="AB":
                    d.append("A")
                    a+=1
                    c-=1
                else:
                    d.append("C")
                    c+=1
                    a-=1
            elif a==0:
                d.append("A")
                a+=1
                c-=1
            else:
                d.append("C")
                c+=1
                a-=1
    print("Yes")
    for i in d:
        print(i)

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
if a+b+c==0:
    print("No")
elif a+b+c==1:
    d=[]
    for i in range(n):
        s=input()
        if s=="AB":
            if a+b==0:
                print("No")
                exit()
            else:
                if a==0:
                    d.append("A")
                else:
                    d.append("B")
                a,b=b,a
        if s=="BC":
            if c+b==0:
                print("No")
                exit()
            else:
                if b==0:
                    d.append("B")
                else:
                    d.append("C")
                c,b=b,c
        if s=="AC":
            if a+c==0:
                print("No")
                exit()
            else:
                if a==0:
                    d.append("A")
                else:
                    d.append("C")
                a,c=c,a 
    print("Yes")
    for i in d:
        print(i)
else:
    d=[]
    s=[input()for i in range(n)]+["AB"]
    for i in range(n):
        if s[i]=="AB":
            if a+b==0:
                print("No")
                exit()
            if a+b+c==2 and a==b==1 and s[i+1]!=s[i]:
                if s[i]=="AC":
                    d.append("A")
                    a+=1
                    b-=1
                else:
                    d.append("B")
                    b+=1
                    a-=1
            elif a==0:
                d.append("A")
                a+=1
                b-=1
            else:
                d.append("B")
                b+=1
                a-=1
        if s=="BC":
            if c+b==0:
                print("No")
                exit()
            if a+b+c==2 and c==b==1 and s[i+1]!=s[i]:
                if s[i]=="AB":
                    d.append("B")
                    c-=1
                    b+=1
                else:
                    d.append("C")
                    c+=1
                    b-=1
            elif b==0:
                d.append("B")
                b+=1
                c-=1
            else:
                d.append("C")
                c+=1
                b-=1
        if s=="AC":
            if c+a==0:
                print("No")
                exit()
            if a+b+c==2 and c==a==1 and s[i+1]!=s[i]:
                if s[i]=="AB":
                    d.append("A")
                    a+=1
                    c-=1
                else:
                    d.append("C")
                    c+=1
                    a-=1
            elif a==0:
                d.append("A")
                a+=1
                c-=1
            else:
                d.append("C")
                c+=1
                a-=1
    print("Yes")
    for i in d:
        print(i)

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int, input().split())
dt = {'AB': (0, 1), 'AC': (0, 2), 'BC': (1, 2)}
Q = [dt[input()] for _ in range(N)]


class Game:
    def __init__(self, ABC):
        self.ABC = ABC
        self.ds = {0: 'A', 1: 'B', 2: 'C'}
        self.ans = []

    def add_q0(self, q0, q1):
        self.ans.append(self.ds[q0])
        self.ABC[q0] += 1
        self.ABC[q1] -= 1

    def add_q1(self, q0, q1):
        self.ans.append(self.ds[q1])
        self.ABC[q0] -= 1
        self.ABC[q1] += 1


def solve1(ABC):
    G = Game(ABC)
    for q0, q1 in Q:
        if G.ABC[q0] == G.ABC[q1] == 0:
            print('No')
            exit()
        elif G.ABC[q0] == 0:
            G.add_q0(q0, q1)
        else:
            G.add_q1(q0, q1)
    print('Yes')
    print(*G.ans, sep='\n')


def solve2(ABC):
    G = Game(ABC)
    for i, (q0, q1) in enumerate(Q):
        if G.ABC[q0] == G.ABC[q1] == 0:
            print('No')
            exit()
        elif G.ABC[q0] == 0:
            G.add_q0(q0, q1)
        elif G.ABC[q1] == 0:
            G.add_q1(q0, q1)
        elif G.ABC[q0] == G.ABC[q1] == 1 and i < N - 1:
            if q0 in Q[i + 1]:
                G.add_q0(q0, q1)
            else:
                G.add_q1(q0, q1)
        else:
            # どちらでもよい
            G.add_q0(q0, q1)
    print('Yes')
    print(*G.ans, sep='\n')


def solve3(ABC):
    G = Game(ABC)
    for q0, q1 in Q:
        if G.ABC[q0] == G.ABC[q1] == 0:
            print('No')
            exit()
        elif G.ABC[q0] == 0:
            G.add_q0(q0, q1)
        elif G.ABC[q1] == 0:
            G.add_q1(q0, q1)
        else:
            # どちらでもよい
            G.add_q0(q0, q1)
    print('Yes')
    print(*G.ans, sep='\n')


S = A + B + C
if S == 0:
    print('No')
elif S == 1:
    solve1([A, B, C])
elif S == 2:
    solve2([A, B, C])
else:
    solve3([A, B, C])

Submission status: Wrong Answer

Solution code:
n,a,b,c = list(map(int, input().split()))
s = [[0]*2 for _ in range(n)]
S = []
for i in range(n):
  X = input()
  S += X
  cnt = 0
  for x in X:
    if x == 'A':
      s[i][cnt] = 0
    elif x == 'B':
      s[i][cnt] = 1
    else:
      s[i][cnt] = 2
    cnt += 1

if (a + b + c) == 0:exit(print('No'))
abc = [a,b,c]

def con(x):
  if x == 0:
    return 'A'
  elif x == 1:
    return 'B'
  else:
    return 'C'

ans = [0] * n

def zero(xy):
  x = xy[0]
  y = xy[1]
  if abc[x] == 0 and abc[y] == 0:
    return -1,-1
  elif abc[x] == 0:
    return x,y
  elif abc[y] == 0:
    return y,x
  else:
    return 3,3

def main():
  global ans
  global abc
  
  for i in range(n):
    x,y = zero(s[i])
    if x == -1:exit(print('No'))
    if x == 3:
      if i < n-1:
        nx,ny = zero(s[i+1])
        if nx == s[i][0] or nx == s[i][1]:
          x = nx
          y = ny
        else:
          x = s[i][0]
          y = s[i][1]
      else:
        x = s[i][0]
        y = s[i][1]
    ans[i] = x
    abc[x] += 1
    abc[y] -= 1

main()
print('Yes')
for i in range(n):
  print(con(ans[i]))

Submission status: Wrong Answer

Solution code:
from collections import Counter
n,a,b,c = list(map(int, input().split()))
s = [[0]*2 for _ in range(n)]
S = []
for i in range(n):
  X = input()
  S += X
  cnt = 0
  for x in X:
    if x == 'A':
      s[i][cnt] = 0
    elif x == 'B':
      s[i][cnt] = 1
    else:
      s[i][cnt] = 2
    cnt += 1

if (a + b + c) == 0:exit(print('No'))
abc = [a,b,c]
x = s[0][0]
y = s[0][1]
if abc[x] == 0 and abc[y] == 0:
  exit(print('No'))

if (a + b + c) == 1:
  S = Counter(S)
  pre = -1
  for sn in S:
    if pre != -1:
      if pre != S[sn]:exit(print('No')) 
    pre = S[sn]

def con(x):
  if x == 0:
    return 'A'
  elif x == 1:
    return 'B'
  else:
    return 'C'

def sel(i):
  x = s[i][0]
  y = s[i][1]
  if abc[x] == 0:
    print(con(x))
    abc[x] += 1
    abc[y] -= 1
  elif abc[y] == 0:
    print(con(y))
    abc[y] += 1
    abc[x] -= 1
  elif i < n-1:
    nx = s[i+1][0]
    ny = s[i+1][1]
    if abc[nx] == 0:
      if nx == x:
        print(con(x))
        abc[x] += 1
        abc[y] -= 1
      elif nx == y:
        print(con(y))
        abc[y] += 1
        abc[x] -= 1 
    elif abc[ny] == 0:
      if ny == x:
        print(con(x))
        abc[x] += 1
        abc[y] -= 1
      elif ny == y:
        print(con(y))
        abc[y] += 1
        abc[x] -= 1              
      else:
        print(con(x))
        abc[x] += 1
        abc[y] -= 1
    else:
      print(con(x))
      abc[x] += 1
      abc[y] -= 1

print('Yes')
for i in range(n):
  sel(i)

Submission status: Wrong Answer

Solution code:
from collections import Counter
n,a,b,c = list(map(int, input().split()))
s = [[] for _ in range(n)]
S = []
for i in range(n):
  s[i] = input()
  S += s[i]


S = Counter(S)
cnt = 0
cnt += a - S['A']
cnt += b - S['B']
cnt += c - S['C']

if cnt < 0:exit(print('No'))
cnt = 0
for x in s[0]:
  if x == 'A':
    cnt += a
  elif x == 'B':
    cnt += b
  else:
    cnt += c
if cnt == 0:exit(print('No'))

sd = {}
sd['A'] = a
sd['B'] = b
sd['C'] = c
H = [x for x in sorted(sd.items(), key=lambda x: x[1])]
h = []
for i,n in H:
  if n < 2:
    for _ in range(2-n):
      h.append(i)
  else:
    h.append(i)

print('Yes')
for x,y in s:
  for i in h:
    if i in [x,y]:
      print(i)
      break