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())
arr=[int(0) for i in range(N)]
for i in range(N):
    arr[i]=input()

#print(arr)
arr1=[]
flag=0
for i in range(N):
    if arr[i]=="AB":
        if A==0 and B==0:
            print("No")
            flag+=1
            break
        else:    
            if A>B:
                A-=1
                B+=1
                arr1.append("B")
            elif B>A:
                A+=1
                B-=1    
                arr1.append("A")
            else:    
                if i<N-1 and A==1 and B==1 and C==0  and arr[i+1]=="AC":
                    arr1.append("A")
                    A+=1
                    B-=1
                elif i<N-1 and A==1 and B==1 and C==0  and arr[i+1]=="BC":
                    B+=1
                    A-=1
                    arr1.append("B")       
                else:
                    arr1.append("B")
                    A-=1
                    B+=1    

    elif arr[i]=="AC":
        if A==0 and C==0:
            print("No")
            flag+=1
            break
        else:    
            if A>C:
                A-=1
                C+=1
                arr1.append("C")

            elif A<C:
                A+=1
                C-=1    
                arr1.append("A")
            else:
                if i<N-1 and A==1 and C==1 and B==0 and arr[i+1]=="AB":
                    arr1.append("A")
                    A+=1
                    C-=1
                elif i<N-1 and A==1 and C==1 and B==0 and arr[i+1]=="CB":
                    C+=1
                    A-=1
                    arr1.append("C")       
                else:
                    arr1.append("C")
                    A-=1
                    C+=1
                        
    else:    
        if B==0 and C==0:
            print("No")
            flag+=1
            break
        else:    
            if B>C:
                B-=1
                C+=1
                arr1.append("C")

            elif C>B:
                B+=1
                C-=1    
                arr1.append("B")
            else:
                if i!=N-1 and B==1 and C==1 and A==0  and arr[i+1]=="BA":
                    arr1.append("B")
                    B+=1
                    C-=1
                elif i!=N-1 and B==1 and C==1 and A==0 and arr[i+1]=="CA":
                    C+=1
                    B-=1
                    arr1.append("C")       
                else:
                    arr1.append("C")
                    B-=1
                    C+=1    

if flag==0 and A>=0 and B>=0 and C>=0:
    print("Yes")
    for i in arr1:
        print(i)

Submission status: Wrong Answer

Solution code:
import numpy as np
N,a,b,c = map(int, input().split())
point = np.array([a,b,c])
result = np.array([''],dtype=object)
for n in range(1,N+1):
    s = str(input())
    if s =='AB':
        exp1 = np.array([1,-1,0]) + point
        exp2 = np.array([-1,1,0]) + point
        expr1 = result.astype(object) + np.array(['A'],dtype=object)
        expr2 = result.astype(object) + np.array(['B'],dtype=object) 
    elif s =='AC':
        exp1 = np.array([1,0,-1]) + point
        exp2 = np.array([-1,0,1]) + point
        expr1 = result.astype(object) + np.array(['A'],dtype=object) 
        expr2 = result.astype(object) + np.array(['C'],dtype=object) 
    elif s =='BC':
        exp1 = np.array([0,1,-1]) + point
        exp2 = np.array([0,-1,1]) + point
        expr1 = result.astype(object) + np.array(['B'],dtype=object) 
        expr2 = result.astype(object) + np.array(['C'],dtype=object)
    else:
        exit()
    point = np.append(exp1, exp2, axis=0).reshape(2**n,3)
    result = np.append(expr1,expr2,axis=0).reshape(2**n,1)
if True in list(np.all(point > 0, axis=1)):
    indexN = list(np.all(point > 0, axis=1)).index(True)
    print("Yes")
    for i in range(len(result[indexN,0])):
        print(result[indexN,0][i])
else:
    print("No")

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

from typing import List


def ab(a: int, b: int, c: int) -> List[int]:
    if a >= b:
        a -= 1
        b += 1
        X = 'B'
    else:
        a += 1
        b -= 1
        X = 'A'
    return [X, a, b, c]


def ac(a: int, b: int, c: int) -> List[int]:
    if a >= c:
        a -= 1
        c += 1
        X = 'C'
    else:
        a += 1
        c -= 1
        X = 'A'
    return [X, a, b, c]


def bc(a: int, b: int, c: int) -> List[int]:
    if b >= c:
        b -= 1
        c += 1
        X = 'C'
    else:
        b += 1
        c -= 1
        X = 'B'
    return [X, a, b, c]


def main():
    N, A, B, C = map(int, input().split())
    S = []
    [S.append(input()) for _ in range(N)]
    if A + B + C == 0:
        print('No')
        return

    judge: bool = True
    X = []
    for s in S:
        if s == 'AB':
            x, A, B, C = ab(A, B, C)
            X.append(x)

        elif s == 'AC':
            x, A, B, C = ac(A, B, C)
            X.append(x)
        else:
            x, A, B, C = bc(A, B, C)
            X.append(x)
        if A < 0 or B < 0 or C < 0:
            judge = False
            break

    if judge:
        print('Yes')
        [print(x) for x in X]
    else:
        print('No')


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:









N,A,B,C=map(int,input().split())
arr=[int(0) for i in range(N)]
for i in range(N):
    arr[i]=input()

#print(arr)
arr1=[]
flag=0
for i in range(N):
    if arr[i]=="AB":
        if A==0 and B==0:
            print("No")
            flag+=1
            break
        else:    
            if A>B:
                A-=1
                B+=1
                arr1.append("B")
            elif B>A:
                A+=1
                B-=1    
                arr1.append("A")
            else:    
                if i!=N-1  and arr[i+1]=="AC":
                    arr1.append("A")
                    A+=1
                    B-=1
                elif i!=N-1 and arr[i+1]=="BC":
                    B+=1
                    A-=1
                    arr1.append("B")       
                else:
                    arr1.append("B")
                    A-=1
                    B+=1    

    elif arr[i]=="AC":
        if A==0 and C==0:
            print("No")
            flag+=1
            break
        else:    
            if A>C:
                A-=1
                C+=1
                arr1.append("C")

            elif A<C:
                A+=1
                C-=1    
                arr1.append("A")
            else:
                if i!=N-1 and arr[i+1]=="AB":
                    arr1.append("A")
                    A+=1
                    C-=1
                elif i!=N-1 and arr[i+1]=="CB":
                    C+=1
                    A-=1
                    arr1.append("C")       
                else:
                    arr1.append("C")
                    A-=1
                    C+=1
                        
    else:    
        if B==0 and C==0:
            print("No")
            flag+=1
            break
        else:    
            if B>C:
                B-=1
                C+=1
                arr1.append("C")

            elif C>B:
                B+=1
                C-=1    
                arr1.append("B")
            else:
                if i!=N-1 and arr[i+1]=="BA":
                    arr1.append("B")
                    B+=1
                    C-=1
                elif i!=N-1 and arr[i+1]=="CA":
                    C+=1
                    B-=1
                    arr1.append("C")       
                else:
                    arr1.append("C")
                    B-=1
                    C+=1    


if flag==0 and A>=0 and B>=0 and C>=0:
    print("Yes")
    for i in arr1:
        print(i)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

from typing import List


def ab(a: int, b: int, c: int) -> List[int]:
    if a >= b:
        a -= 1
        b += 1
        print('B')
    else:
        a += 1
        b -= 1
        print('A')
    return [a, b, c]


def ac(a: int, b: int, c: int) -> List[int]:
    if a >= c:
        a -= 1
        c += 1
        print('C')
    else:
        a += 1
        c -= 1
        print('A')
    return [a, b, c]


def bc(a: int, b: int, c: int) -> List[int]:
    if b >= c:
        b -= 1
        c += 1
        print('C')
    else:
        b += 1
        c -= 1
        print('B')
    return [a, b, c]


def main():
    N, A, B, C = map(int, input().split())
    S = []
    [S.append(input()) for _ in range(N)]
    if A + B + C == 0:
        print('No')
        return

    print('Yes')
    for s in S:
        if s == 'AB':
            A, B, C = ab(A, B, C)
        elif s == 'AC':
            A, B, C = ac(A, B, C)
        else:
            A, B, C = bc(A, B, C)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
N,A,B,C = map(int,input().split())
Q = []
ans = []
f = 0
def dfs(f,a,b,c):
    if f == N:return True
    if Q[f] == 'AB':
        if a+b == 0:return False
        if a>0 and dfs(f+1,a-1,b+1,c):
            ans.append('A')
            return True
        if b>0 and dfs(f+1,a+1,b-1,c):
            ans.append('B')
            return True
        
    return False
    if Q[f] == 'BC':
        if c+b == 0:return False
        if b>0 and dfs(f+1,a,b-1,c+1):
            ans.append('B')
            return True
        if c>0 and dfs(f+1,a,b+1,c-1):
            ans.append('C')
            return True
        
    return False
    if Q[f] == 'AC':
        if a+c == 0:return False
        if a>0 and dfs(f+1,a-1,b,c+1):
            ans.append('A')
            return True
        if c>0 and dfs(f+1,a+1,b,c-1):
            ans.append('C')
            return True
        
    return False
    

for i in range(N):
    s = input()
    Q.append(s)
dfs(0,A,B,C)
# print(ans)
if len(ans) == N:
    print('Yes')
    ans = ans[::-1]
    for i in ans:
        print(i)
else:
    print('No')

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
N,A,B,C = map(int,input().split())
Q = []
ans = []
f = 0
def dfs(f,a,b,c):
    if f == N:return True
    if Q[f] == 'AB':
        if a+b == 0:return False
        if a>0 and dfs(f+1,a-1,b+1,c):
            ans.append('A')
            return True
        if b>0 and dfs(f+1,a+1,b-1,c):
            ans.append('B')
            return True
        
        return False
    if Q[f] == 'BC':
        if c+b == 0:return False
        if b>0 and dfs(f+1,a,b-1,c+1):
            ans.append('B')
            return True
        if c>0 and dfs(f+1,a,b+1,c-1):
            ans.append('C')
            return True
        
        return False
    if Q[f] == 'AC':
        if a+c == 0:return False
        if a>0 and dfs(f+1,a-1,b,c+1):
            ans.append('A')
            return True
        if c>0 and dfs(f+1,a+1,b,c-1):
            ans.append('C')
            return True
        
        return False
    

for i in range(N):
    s = input()
    Q.append(s)
dfs(0,A,B,C)
# print(ans)
if len(ans) == N:
    print('Yes')
    ans = ans[::-1]
    for i in ans:
        print(i)
else:
    print('No')

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
N,A,B,C = map(int,input().split())
Q = []
ans = []
f = 0
def dfs(f,a,b,c):
    if f == N:return True
    if Q[f] == 'AB':
        if a>0 and dfs(f+1,a-1,b+1,c):
            ans.append('A')
            return True
        if b>0 and dfs(f+1,a+1,b-1,c):
            ans.append('B')
            return True
        
        return False
    if Q[f] == 'BC':
        if b>0 and dfs(f+1,a,b-1,c+1):
            ans.append('B')
            return True
        if c>0 and dfs(f+1,a,b+1,c-1):
            ans.append('C')
            return True
        
        return False
    if Q[f] == 'AC':
        if a>0 and dfs(f+1,a-1,b,c+1):
            ans.append('A')
            return True
        if c>0 and dfs(f+1,a+1,b,c-1):
            ans.append('C')
            return True
        
        return False
    

for i in range(N):
    s = input()
    Q.append(s)
dfs(0,A,B,C)
# print(ans)
if len(ans) == N:
    print('Yes')
    for i in ans:
        print(i)
else:
    print('No')

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
N,A,B,C = map(int,input().split())
Q = []
ans = []
f = 0
def dfs(f,a,b,c):
    if f == N:return True
    if Q[f] == 'AB':
        if a>0 and dfs(f+1,a-1,b+1,c):
            ans.append('A')
            return True
        if b>0 and dfs(f+1,a+1,b-1,c):
            ans.append('B')
            return True
        
        return False
    if Q[f] == 'BC':
        if b>0 and dfs(f+1,a,b-1,c+1):
            ans.append('B')
            return True
        if c>0 and dfs(f+1,a,b+1,c-1):
            ans.append('C')
            return True
        
        return False
    if Q[f] == 'AC':
        if a>0 and dfs(f+1,a-1,b,c+1):
            ans.append('A')
            return True
        if c>0 and dfs(f+1,a+1,b,c-1):
            ans.append('C')
            return True
        
        return False
    

for i in range(N):
    s = input()
    Q.append(s)
dfs(0,A,B,C)
# print(ans)
if len(ans) == N:
    print('Yes')
    ans = ans[::-1]
    for i in ans:
        print(i)
else:
    print('No')

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,input().split())
clear=1
ans=""
memo=""
non=0
for _ in range(n):
    s=input()
    if s=="AB":
        if (a==0) and (b==0) and (memo!=""):
            if memo.strip(ans[-1])=="A":
                a=0
                b=1
                c-=1
                ans=ans[:-1]+"AB"
            else:
                a=1
                b=0
                c-=1
                ans=ans[:-1]+"BA"
        elif a>b:
            a-=1
            b+=1
            ans+="B"
        else:
            a+=1
            b-=1
            ans+="A"
        if a+b==1:
            non=1
        else:
            non=0
    elif s=="BC":
        if (b==0) and (b==0) and (memo!=""):
            if memo.strip(ans[-1])=="B":
                b=0
                c=1
                a-=1
                ans=ans[:-1]+"BC"
            else:
                b=1
                c=0
                a-=1
                ans=ans[:-1]+"CB"
        elif b>c:
            b-=1
            c+=1
            ans+="C"
        else:
            b+=1
            c-=1
            ans+="B"
        if b+c==1:
            non=1
        else:
            non=0
    else:
        if (a==0) and (c==0) and (memo!=""):
            if memo.strip(ans[-1])=="A":
                a=0
                c=1
                b-=1
                ans=ans[:-1]+"AC"
            else:
                a=1
                c=0
                b-=1
                ans=ans[:-1]+"CA"
        elif c>a:
            c-=1
            a+=1
            ans+="A"
        else:
            a-=1
            c+=1
            ans+="C"
        if a+c==1:
            non=1
        else:
            non=0
    if (a<0) or (b<0) or (c<0):
        clear=0
    if non==0:
        memo=s
    else:
        memo=""
if clear==1:
    print("Yes")
    for i in ans:
        print(i)
else:
    print("No")

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ANS = ['']*N
for i in range(N):
    if S[i]=='AB':
        if A==0 and B==0:
            print('No')
            exit()
        if i != N-1 and A==B and S[i+1]!='AB':
            if S[i+1]=='AC':
                ANS[i] = 'A'
                A += 1
                B -= 1
            elif S[i+1]=='BC':
                ANS[i] = 'B'
                B += 1
                A -= 1

        if A<B:
            ANS[i] = 'A'
            A += 1
            B -= 1
        else:
            ANS[i] = 'B'
            A -= 1
            B += 1
            

    elif S[i]=='AC':
        if A==0 and C==0:
            print('No')
            exit()
        if i != N-1 and A==C and S[i+1]!='AC':
            if S[i+1]=='AB':
                ANS[i] = 'A'
                A += 1
                C -= 1
            elif S[i+1]=='BC':
                ANS[i] = 'C'
                C += 1
                A -= 1


        if A<C:
            ANS[i] = 'A'
            A += 1
            C -= 1
        else:
            ANS[i] = 'C'
            A -= 1
            C += 1


    elif S[i]=='BC':
        if C==0 and B==0:
            print('No')
            exit()
        if i != N-1 and C==B and S[i+1]!='BC':
            if S[i+1]=='AC':
                ANS[i] = 'C'
                C += 1
                B -= 1
            elif S[i+1]=='AB':
                ANS[i] = 'B'
                B += 1
                C -= 1

        if C<B:
            ANS[i] = 'C'
            C += 1
            B -= 1
        else:
            ANS[i] = 'B'
            C -= 1
            B += 1
if A<0 or B<0 or C<0:
    print('No')
    exit()

print('Yes')
for a in ANS:
    print(a)

Submission status: Wrong Answer

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:
    choice=[]
    for si in s:
        if si=="AB":
            if c==1:
                print("No")
                exit()
            elif a==1:
                a-=1
                b+=1
                choice.append("B")
            else:
                a+=1
                b-=1
                choice.append("A")
        elif si=="AC":
            if b==1:
                print("No")
                exit()
            elif a==1:
                a-=1
                c+=1
                choice.append("C")
            else:
                a+=1
                c-=1
                choice.append("A")
        else:
            if a==1:
                print("No")
                exit()
            elif b==1:
                b-=1
                c+=1
                choice.append("C")
            else:
                b+=1
                c-=1
                choice.append("B")
    print("Yes")
    for ch in choice:
        print(ch)
else:
    choice=[]
    if s[0]=="AB":
        if a==0 and b==0:
            print("No")
            exit()
    elif s[0]=="AC":
        if a==0 and c==0:
            print("No")
            exit()
    else:
        if b==0 and c==0:
            print("No")
            exit()
    for i in range(n):
        if a+b+c>=3:
            if s[i]=="AB":
                if a>=b:
                    a-=1
                    b+=1
                    choice.append("B")
                else:
                    a+=1
                    b-=1
                    choice.append("A")
            elif s[i]=="AC":
                if a>=c:
                    a-=1
                    c+=1
                    choice.append("C")
                else:
                    a+=1
                    c-=1
                    choice.append("A")
            else:
                if b>=c:
                    b-=1
                    c+=1
                    choice.append("C")
                else:
                    b+=1
                    c-=1
                    choice.append("B")
        else:
            if i<n-1:
                if s[i]=="AB":
                    if a==b:
                        if s[i+1]=="AB" or s[i+1]=="BC":
                            a-=1
                            b+=1
                            choice.append("B")
                        else:
                            a+=1
                            b-=1
                            choice.append("A")
                    elif b>a:
                        a+=1
                        b-=1
                        choice.append("A")
                    else:
                        a=-1
                        b+=1
                        choice.append("B")
                elif s[i]=="AC":
                    if a==c:
                        if s[i+1]=="AC" or s[i+1]=="BC":
                            a-=1
                            c+=1
                            choice.append("C")
                        else:
                            a+=1
                            c-=1
                            choice.append("A")
                    elif c>a:
                        a+=1
                        c-=1
                        choice.append("A")
                    else:
                        a-=1
                        c+=1
                        choice.append("C")
                else:
                    if b==c:
                        if s[i+1]=="BC" or s[i+1]=="AC":
                            b-=1
                            c+=1
                            choice.append("C")
                        else:
                            b+=1
                            c-=1
                            choice.append("B")
                    elif b>c:
                        b-=1
                        c+=1
                        choice.append("C")
                    else:
                        b+=1
                        c-=1
                        choice.append("B")
            else:
                if s[i]=="AB":
                    if a>=b:
                        a-=1
                        b+=1
                        choice.append("B")
                    else:
                        a+=1
                        b-=1
                        choice.append("A")
                elif s[i]=="AC":
                    if a>=c:
                        a-=1
                        c+=1
                        choice.append("C")
                    else:
                        a+=1
                        c-=1
                        choice.append("A")
                else:
                    if b>=c:
                        b-=1
                        c+=1
                        choice.append("C")
                    else:
                        b+=1
                        c-=1
                        choice.append("B")
    print("Yes")
    #print(a,b,c)
    for ch in choice:
        print(ch)

Submission status: Wrong Answer

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

flag = True

for x in range(len(S)):
    if S[x] == "AB":
        if A == 0 and B == 0:
            flag = False
            break
        if A == 0:
            A += 1
            B -= 1
            add.append("A")
        elif B == 0:
            B += 1
            A -= 1
            add.append("B")
        elif x + 1 < len(S) and A + B + C == 2 and S[x + 1] != "AB":
            if S[x + 1] == "AC":
                A += 1
                B -= 1
                add.append("A")
            else:
                B += 1
                A -= 1
                add.append("B")
        else:
            A += 1
            B -= 1
            add.append("A")
    elif S[x] == "AC":
        if A == 0 and C == 0:
            flag = False
            break
        if A == 0:
            A += 1
            C -= 1
            add.append("A")
        elif C == 0:
            C += 1
            A -= 1
            add.append("C")
        elif x + 1 < len(S) and A + B + C == 2 and S[x + 1] != "AC":
            if S[x + 1] == "BC":
                C += 1
                A -= 1
                add.append("C")
            else:
                A += 1
                C -= 1
                add.append("A")
        else:
            A += 1
            C -= 1
            add.append("A")
    else:
        if B == 0 and C == 0:
            flag = False
            break
        if B == 0:
            B += 1
            C -= 1
            add.append("B")
        elif C == 0:
            C += 1
            B -= 1
            add.append("C")
        elif x + 1 < len(S) and A + B + C == 2 and S[x + 1] != "BC":
            if S[x + 1] == "AB":
                B += 1
                C -= 1
                add.append("B")
            else:
                C += 1
                B -= 1
                add.append("C")
        else:
            B += 1
            C -= 1
            add.append("B")

if flag is True and A >= 0 and B >= 0 and C >= 0:
    print("Yes")
    for y in add:
        print(y)
else:
    print("No")

Submission status: Wrong Answer

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

ANS = ['']*N
for i in range(N):
    if S[i]=='AB':
        if A==0 and B==0:
            print('No')
            exit()
        
        if A<B:
            ANS[i] = 'A'
            A += 1
            B -= 1
        else:
            ANS[i] = 'B'
            A -= 1
            B += 1
            

    elif S[i]=='AC':
        if A==0 and C==0:
            print('No')
            exit()
        
        if A<C:
            ANS[i] = 'A'
            A += 1
            C -= 1
        else:
            ANS[i] = 'C'
            A -= 1
            C += 1


    elif S[i]=='BC':
        if C==0 and B==0:
            print('No')
            exit()
        
        if C<B:
            ANS[i] = 'C'
            C += 1
            B -= 1
        else:
            ANS[i] = 'B'
            C -= 1
            B += 1
if A<0 or B<0 or C<0:
    print('No')
    exit()

print('Yes')
for a in ANS:
    print(a)

Submission status: Wrong Answer

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

flag = True

for x in range(len(S)):
    if S[x] == "AB":
        if A == 0 and B == 0:
            flag = False
            break
        if x + 1 < len(S):
            if S[x + 1] == "AC":
                A += 1
                B -= 1
                add.append("A")
            else:
                B += 1
                A -= 1
                add.append("B")
        elif A > B:
            B += 1
            A -= 1
            add.append("B")
        else:
            A += 1
            B -= 1
            add.append("A")
    elif S[x] == "AC":
        if A == 0 and C == 0:
            flag = False
            break
        if x + 1 < len(S):
            if S[x + 1] == "BC":
                C += 1
                A -= 1
                add.append("C")
            else:
                A += 1
                C -= 1
                add.append("A")
        elif A > C:
            C += 1
            A -= 1
            add.append("C")
        else:
            A += 1
            C -= 1
            add.append("A")
    else:
        if B == 0 and C == 0:
            flag = False
            break
        if x + 1 < len(S):
            if S[x + 1] == "AB":
                B += 1
                C -= 1
                add.append("B")
            else:
                C += 1
                B -= 1
                add.append("C")
        elif B > C:
            C += 1
            B -= 1
            add.append("C")
        else:
            B += 1
            C -= 1
            add.append("B")

if flag is True and A >= 0 and B >= 0 and C >= 0:
    print("Yes")
    for y in add:
        print(y)
else:
    print("No")

Submission status: Wrong Answer

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

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

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

s1 = 0
s2 = 0

abc = [a,b,c]
moji = ['A','B','C']

def checkmoji(s):
    if si[i] == "AB":
        s1 = 0
        s2 = 1
    elif si[i] == 'AC':
        s1 = 0
        s2 = 2
    elif si[i] == 'BC':
        s1 = 1
        s2 = 2
    return s1,s2

ans = []

for i in range(n):
    s1,s2 = checkmoji(si[i])
    if abc[s1] <= 0 and abc[s2] <= 0:
        print('No')
        exit()
    if abc[s1] > abc[s2]:
        abc[s1] -= 1
        abc[s2] += 1
        ans.append(moji[s2])
    elif abc[s2] > abc[s1]:
        abc[s1] += 1
        abc[s2] -= 1
        ans.append(moji[s1])
    else:
        if i == n-1:
            ans.append(moji[s1])
            break
        s1_a,s2_a = checkmoji(si[i+1])
        if s1 != s1_a and s1 != s2_a:
            abc[s1] -= 1
            abc[s2] += 1
            ans.append(moji[s2])
        elif s2 != s1_a and s2 != s2_a:
            abc[s1] += 1
            abc[s2] -= 1
            ans.append(moji[s1])
        else:
            abc[s1] += 1
            abc[s2] -= 1
            ans.append(moji[s1])
print('Yes')
for i in range(len(ans)):
    print(ans[i])