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:
def ans(N, A, B, C, s):
    add = []
    for i in range(N):
        # print(A, B, C)
        if s[i] == "AB":
            if A==0 and B==0:
                return False
            else:
                if A <= B:
                    A += 1
                    B -= 1
                    add.append("A")
                else:
                    B += 1
                    A -= 1
                    add.append("B")
        elif s[i] == "AC":
            if A==0 and C==0:
                return False
            else:
                if A <= C:
                    A += 1
                    C -= 1
                    add.append("A")
                else:
                    C += 1
                    A -= 1
                    add.append("C")
        elif s[i] == "BC":
            if C==0 and B==0:
                return False
            else:
                if B <= C:
                    B += 1
                    C -= 1
                    add.append("B")
                else:
                    B -= 1
                    C += 1
                    add.append("C")
    return add



N, A, B, C = map(int, input().split())
s = []
for i in range(N):
    s.append(input())
a = ans(N, A, B, C, s)
if not a:
    print("No")
else:
    print("Yes")
    for i in a:
        print(i)

Submission status: Wrong Answer

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



if [A,B,C].count(0)==2:
    print("No")
    exit()
else:
    print("Yes")
S=[0]*N
for i in range(N):
    s=input()
    S[i]=s

for i in range(N):  
    if S[i]=="AB":
        if A==1 and B==1:
            if i+1<N:
                if S[i+1]=="BC" and C==0:
                    B+=1
                    A-=1
                    print("B")
                    continue
                elif S[i+1]=="AC" and C==0:
                    A+=1
                    B-=1
                    print("A")
                    continue
            A+=1
            B-=1
            print("A")
        elif A>B:
            A-=1
            B+=1
            print("B")
        elif B>A:
            A+=1
            B-=1
            print("A")
        else:
            A+=1
            B-=1
            print("A")



    if S[i]=="AC":
        if A==1 and C==1:
            if i+1<N:
                if S[i+1]=="BC" and B==0:
                    C+=1
                    A-=1
                    print("C")
                    continue
                elif S[i+1]=="AB" and B==0:
                    A+=1
                    C-=1
                    print("A")
                    continue
            A+=1
            C-=1
            print("A")
        elif A>C:
            A-=1
            C+=1
            print("C")
        elif C>A:
            A+=1
            C-=1
            print("A")
        else:
            A+=1
            C-=1
            print("A")

    if S[i]=="BC":
        if B==1 and C==1:
            if i+1<N:
                if S[i+1]=="AC" and A==0:
                    C+=1
                    B-=1
                    print("C")
                    continue
                elif S[i+1]=="AB" and A==0:
                    B+=1
                    C-=1
                    print("B")
                    continue
            B+=1
            C-=1
            print("B")
        elif B>C:
            B-=1
            C+=1
            print("C")
        elif C>B:
            B+=1
            C-=1
            print("B")
        else:
            B+=1
            C-=1
            print("B")

    #print(A,B,C)

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,input().split())
ss = [input()for i in range(n)]
ok = True
ans = []
cnt = [[0]*3 for i in range(n)]
for i in range(n-1,-1,-1):
    s = ss[i]
    p = s[0]
    q = s[1]
    cnt[i][ord(p) - ord('A')] +=1
    cnt[i][ord(q) - ord('A')] +=1
    if i == 0:continue
    cnt[i-1][0] = cnt[i][0]
    cnt[i-1][1] = cnt[i][1]
    cnt[i-1][2] = cnt[i][2]

    
for i in range(n):
    s = ss[i]
    if s == 'AB':
        if a == 0 and b == 0:
            ok = False
            break
        
        if a == 0:
            ans.append('A')
            a += 1
            b -= 1
        elif b == 0:
            ans.append('B')
            b += 1
            a -= 1
        elif a < b:
            ans.append('A')
            a += 1
            b -= 1
        elif a > b:
            ans.append('B')
            b += 1
            a -= 1
            
        else:
            if cnt[i][0] > cnt[i][1]:
                a += 1
                b -= 1
                ans.append('A')
            else:
                ans.append('B')
                a -= 1
                b += 1
    elif s == 'AC':
        if a == 0 and c == 0:
            ok = False
            break
        if a == 0:
            ans.append('A')
            a += 1
            c -= 1
        elif c == 0:
            ans.append('C')
            c += 1
            a -= 1
        elif a > c:
            ans.append('C')
            c += 1
            a -= 1
        elif a < c:
            ans.append('A')
            c -= 1
            a += 1
        else:
            if cnt[i][0] > cnt[i][2]:
                a += 1
                c -= 1
                ans.append('A')
            else:
                ans.append('C')
                a -= 1
                c += 1
    else:
        if c == 0 and b == 0:
            ok = False
            break
        
        if b == 0:
            ans.append('B')
            c -= 1
            b += 1
        elif c == 0:
            ans.append('C')
            c += 1
            b -= 1
        elif b > c:
            ans.append('C')
            c += 1
            b -= 1
        elif b < c:
            ans.append('B')
            b += 1
            c -= 1
        else:
            if cnt[i][2] > cnt[i][1]:
                c += 1
                b -= 1
                ans.append('C')
            else:
                ans.append('B')
                c -= 1
                b += 1
if ok:
    print('Yes')
    for x in ans:
        print(x)
else:
    print('No')

Submission status: Wrong Answer

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

import sys
# import math
# from string import ascii_lowercase, ascii_upper_case, ascii_letters, digits, hexdigits
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from operator import itemgetter              # itemgetter(1), itemgetter('key')
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product, permutations  # product(iter, repeat=n), permutations(iter[,r])
# from itertools import combinations, combinations_with_replacement
# from itertools import accumulate             # accumulate(iter[, f])
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict (e.g. list is not allowed)
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from copy import deepcopy                    # to copy multi-dimentional matrix without reference
# from fractions import gcd                    # for Python 3.4 (previous contest @AtCoder)


def main():
    mod = 1000000007                # 10^9+7
    inf = float('inf')              # sys.float_info.max = 1.79...e+308
    # inf = 2 ** 64 - 1               # (for fast JIT compile in PyPy) 1.84...e+19
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    

    
    def simulation(a, b, c):
        result = []
        for com in commands:
            if com == 'AB':
                if a == 0 and b == 0:
                    return False, result
                if a < b:
                    result.append('A')
                    a += 1
                    b -= 1
                else:
                    result.append('B')
                    b += 1
                    a -= 1
            elif com == 'BC':
                if b == 0 and c == 0:
                    return False, result
                if b < c:
                    result.append('B')
                    b += 1
                    c -= 1
                else:
                    result.append('C')
                    c += 1
                    b -= 1            
            else:
                if c == 0 and a == 0:
                    return False, result
                if c < a:
                    result.append('C')
                    c += 1
                    a -= 1
                else:
                    result.append('A')
                    a += 1
                    c -= 1
        return True, result    

    
    n, a, b, c  = mi()
    t = [a, b, c]
    commands = [input() for _ in range(n)]
    if t.count(0) == 3:
        print('No')
    elif t.count(0) == 2:
        predicate, result = simulation(a, b, c)
        if predicate:
            print('Yes')
            print(*result, sep='\n')
        else:
            print('No')
    else:
        predicate, result = simulation(a, b, c)
        if predicate:
            print('Yes')
            print(*result, sep='\n')
        else:
            print('No')

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

YES = "Yes"  # type: str
NO = "No"  # type: str


def solve(N: int, A: int, B: int, C: int, s: "List[str]"):
    if A ==0 and B ==0 and C ==0:
        print(NO)
        return 

    def yes():
        tmp = [A,B,C]
        print(YES)
        for i in range(N):
            a = tmp[0]
            b = tmp[1]
            c = tmp[2]
            if s[i] == "AB":
                if a<=b:
                    a += 1
                    b -= 1
                    print("A")
                else:
                    a -=1
                    b += 1
                    print("B")
            elif s[i] == "AC":
                if a<=c:
                    a += 1
                    c -= 1
                    print('A')
                else:
                    a -=1
                    c += 1
                    print('C')
            else:
                if b<=c:
                    b += 1
                    c -= 1
                    print('B')

                else:
                    b -=1
                    c += 1
                    print('C')
            tmp = [a,b,c]
        return 

    if A+B+C>2 and [A,B,C].count(0) <= 1:
        yes()
    else:
        if A+B+C>2:
            if C>0:
                if s[0] == "AB":
                    print(NO)
                else:
                    yes()
            elif A>0:
                if s[0] == "BC":
                    print(NO)
                else:
                    yes()
            else: 
                if s[0] == "AC":
                    print(NO)
                else:
                    yes()
        else:
            if [A,B,C].count(0) == 2:
                tmp = [A,B,C]
                for i in range(N):
                    a = tmp[0]
                    b = tmp[1]
                    c = tmp[2]
                    if s[i] == "AB":
                        if a<=b:
                            a += 1
                            b -= 1
                        else:
                            a -=1
                            b += 1
                    elif s[i] == "AC":
                        if a<=c:
                            a += 1
                            c -= 1
                        else:
                            a -=1
                            c += 1
                    else:
                        if b<=c:
                            b += 1
                            c -= 1
                        else:
                            b -=1
                            c += 1
                    if a < 0 or b<0 or c<0:
                        print(NO)
                        return
                    tmp = [a,b,c]
                yes()
            elif [A,B,C].count(0) == 1:
                tmp = [A,B,C]
                for i in range(N):
                    a = tmp[0]
                    b = tmp[1]
                    c = tmp[2]
                    if s[i] == "AB" and a==1 and b == 1:
                        if i+1<N:
                            if s[i+1] == "BC":
                                b += 1
                                c -= 1
                                print("B")
                            else:
                                b -= 1
                                c += 1
                                print("C")
                            tmp = [a,b,c]

                    if s[i] == "BC" and b==1 and c == 1:
                        if i+1<N:
                            if s[i+1] == "AC":
                                b -= 1
                                c += 1
                                print("C")
                            else:
                                b += 1
                                c -= 1
                                print("B")
                            tmp = [a,b,c]
                            continue
                    if s[i] == "AC" and a==1 and c == 1:
                        if i+1<N:
                            if s[i+1] == "BC":
                                c += 1
                                a -= 1
                                print("C")
                            else:
                                c -= 1
                                a += 1
                                print("A")     
                            tmp = [a,b,c]
                            continue               

                    if s[i] == "AB":
                        if a<=b:
                            a += 1
                            b -= 1
                            print("A")
                        else:
                            a -=1
                            b += 1
                            print("B")
                    elif s[i] == "AC":
                        if a<=c:
                            a += 1
                            c -= 1
                            print('A')
                        else:
                            a -=1
                            c += 1
                            print('C')
                    else:
                        if b<=c:
                            b += 1
                            c -= 1
                            print('B')

                        else:
                            b -=1
                            c += 1
                            print('C')
                    tmp = [a,b,c]
        return 
    return


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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
def main():
    N, A, B, C = (int(i) for i in input().split())
    S = [input() for i in range(N)]
    c = {"A": 0, "B": 0, "C": 0}
    ans = ["x" for _ in range(N)]
    now = [A, B, C]
    for i, s in enumerate(S):
        c[s[0]] += 1
        c[s[1]] += 1
    less = [True]*3
    if c["A"] > A:
        less[0] = False
    if c["B"] > B:
        less[1] = False
    if c["C"] > C:
        less[2] = False
    for i, s in enumerate(S):
        if s == "AB":
            if now[0] == 0 and now[1] == 0:
                return print("No")
            elif now[0] == 0:
                now[0] += 1
                now[1] -= 1
                ans[i] = "A"
            elif now[1] == 0:
                now[1] += 1
                now[0] -= 1
                ans[i] = "B"
            elif now[0] < now[1]:
                now[0] += 1
                now[1] -= 1
                ans[i] = "A"
            else:
                now[1] += 1
                now[0] -= 1
                ans[i] = "B"
        elif s == "AC":
            if now[0] == 0 and now[2] == 0:
                return print("No")
            elif now[0] == 0:
                now[0] += 1
                now[2] -= 1
                ans[i] = "A"
            elif now[2] == 0:
                now[2] += 1
                now[0] -= 1
                ans[i] = "C"
            elif now[0] < now[2]:
                now[0] += 1
                now[2] -= 1
                ans[i] = "A"
            else:
                now[2] += 1
                now[0] -= 1
                ans[i] = "C"
        elif s == "BC":
            if now[2] == 0 and now[1] == 0:
                return print("No")
            elif now[2] == 0:
                now[2] += 1
                now[1] -= 1
                ans[i] = "C"
            elif now[1] == 0:
                now[1] += 1
                now[2] -= 1
                ans[i] = "B"
            elif now[2] < now[1]:
                now[2] += 1
                now[1] -= 1
                ans[i] = "C"
            else:
                now[1] += 1
                now[2] -= 1
                ans[i] = "B"
    print("Yes")
    print(*ans, sep="\n")


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,A,B,C = map(int,(input().split()))
s = [[] for _ in range(N+1)]
for i in range(N):
    s[i] = input()
s[N] = "NOTHING"
print(s)

flag = 0
ans = []
S = A+B+C

if S >= 3:
    for i in range(N):
        print(A,B,C)
        if s[i] == "AB":
            if A+B == 0:
                flag = 1
                break
            if A >= B:
                ans.append("B")
                B += 1
                A -= 1
            else:
                ans.append("A")
                A += 1
                B -= 1

        if s[i] == "AC":
            if A+C == 0:
                flag = 1
                break
            if A >= C:
                ans.append("C")
                C += 1
                A -= 1
            else:
                ans.append("A")
                A += 1
                C -= 1

        if s[i] == "BC":
            if B+C == 0:
                flag = 1
                break
            if B >= C:
                ans.append("C")
                C += 1
                B -= 1
            else:
                ans.append("B")
                B += 1
                C -= 1
    if flag == 1:
        print("No")
    else:
        print("Yes")
        for i in ans:
            print(i)

else:
    for i in range(N):
        print(A,B,C)
        if s[i] == "AB":
            if A+B == 0:
                flag = 1
                break
            if B == 0:
                ans.append("B")
                B += 1
                A -= 1
            if A == 0:
                ans.append("A")
                A += 1
                B -= 1
            if s[i+1] == "AC":
                ans.append("B")
                B += 1
                A -= 1
            else:
                ans.append("A")
                A += 1
                B -= 1

        if s[i] == "AC":
            if A+C == 0:
                flag = 1
                break
            if C == 0:
                ans.append("C")
                C += 1
                A -= 1
            if A == 0:
                ans.append("A")
                A += 1
                C -= 1
            if s[i+1] == "BC":
                ans.append("C")
                C += 1
                A -= 1
            else:
                ans.append("A")
                A += 1
                C -= 1

        if s[i] == "BC":
            if B+C == 0:
                flag = 1
                break
            if C == 1:
                ans.append("C")
                C += 1
                B -= 1
            if B == 1:
                ans.append("B")
                B += 1
                C -= 1
            if s[i+1] == "AC":
                ans.append("C")
                C += 1
                B -= 1
            else:
                ans.append("b")
                B += 1
                C -= 1

    if flag == 1:
        print("No")
    else:
        print("Yes")
        for i in ans:
            print(i)

Submission status: Wrong Answer

Solution code:
from sys import stdin, setrecursionlimit

setrecursionlimit(10 ** 9)


def input():
    return stdin.readline().strip()


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


def dfs(index, hp):
    if index == N+ 1:
        return ''

    s1, s2 = strings[index-1]
    if hp[s1] > 0:
        hp2 = hp.copy()
        hp2[s1] -= 1
        hp2[s2] += 1
        ret = dfs(index + 1, hp2)
        if ret != False:
            return  s2 + ret
    if hp[s2] > 0:
        hp2 = hp.copy()
        hp2[s2] -= 1
        hp2[s1] += 1
        ret = dfs(index + 1, hp2)
        if ret != False:
            return s1 + ret

    return False


d = {'A': A, 'B': B, 'C': C}
ans = dfs(1, d)
if ans == False:
    print('No')
else:
    for c in ans:
        print(c)

Submission status: Wrong Answer

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


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


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

Submission status: Wrong Answer

Solution code:
MOD = 10 ** 9 + 7
INF = 10 ** 11
import sys
sys.setrecursionlimit(100000000)

def main():
    N,A,B,C = map(int,input().split())
    choice = [input() for _ in range(N)]
    ab = 0
    bc = 0
    ca = 0
    for s in choice:
        if s == 'AB':
            ab += 1
        elif s == 'BC':
            bc += 1
        else:
            ca += 1
    
    ans = [[0,0] for _ in range(3)]
    if A > 0 or B > 0:
        ans[0][0] += ab//2
        ans[0][1] += ab//2
        ab %= 2
    else:
        if bc > 0 and C > 0:
            bc -= 1
            C -= 1
            B += 1
            ans[1][0] += 1
        elif ca > 0 and C > 0:
            ca -= 1
            C -= 1
            A += 1
            ans[2][0] += 1
        else:
            print('No')
            return
        ans[0][0] += ab//2
        ans[0][1] += ab//2
        ab %= 2
        
    if B > 0 or C > 0:
        ans[1][0] += bc//2
        ans[1][1] += bc//2
        bc %= 2
    else:
        if ab > 0 and A > 0:
            A -= 1
            B += 1
            ab -= 1
            ans[0][1] += 1
        elif ca > 0 and A > 0:
            A -= 1
            C += 1
            ca -= 1
            ans[2][0] += 1
        else:
            print('No')
            return
        ans[1][0] += bc//2
        ans[1][1] += bc//2
        bc %= 2
                
    if C > 0 or A > 0:
        ans[2][0] += ca//2
        ans[2][1] += ca//2
        ca %= 2
    else:
        if bc > 0 and B > 0:
            B -= 1
            C += 1
            bc -= 1
            ans[1][1] += 1
        elif ab > 0 and B > 0:
            B -= 1
            A += 1
            ab -= 1
            ans[0][0] += 1
        else:
            print('No')
            return
        ans[1][0] += bc//2
        ans[1][1] += bc//2
        bc %= 2
    
    if ab > 0 and bc > 0 and ca > 0:
        if A > 0 or B > 0 or C > 0:
            ans[0][1] += 1
            ans[1][1] += 1
            ans[2][1] += 1
        else:
            print('No')
            return
    elif ab > 0 and bc > 0:
        if A > 0:
            ans[0][1] += 1
            ans[1][1] += 1
        elif C > 0:
            ans[1][0] += 1
            ans[0][0] += 1
        elif B > 1:
            ans[0][0] += 1
            ans[1][1] += 1
        else:
            print('No')
            return
    elif bc > 0 and ca > 0:
        if A > 0:
            ans[2][0] += 1
            ans[1][0] += 1
        elif B > 0:
            ans[1][1] += 1
            ans[2][0] += 1
        elif C > 1:
            ans[1][0] += 1
            ans[2][1] += 1
        else:
            print('No')
            return
    elif ca > 0 and ab > 0:
        if C > 0:
            ans[2][0] += 1
            ans[0][1] += 1
        elif B > 0:
            ans[1][0] += 1
            ans[2][0] += 1
        elif A > 1:
            ans[0][1] += 1
            ans[2][1] += 1
        else:
            print('No')
            return
    
    elif ab > 0:
        if A > 0:
            ans[0][1] += 1
        elif B > 0:
            ans[0][0] += 1
        else:
            print('No')
            return
    
    elif bc > 0:
        if B > 0:
            ans[1][1] += 1
        elif C > 0:
            ans[1][0] += 1
        else:
            print('No')
            return
    
    elif ca > 0:
        if A > 0:
            ans[2][1] += 1
        elif C > 0:
            ans[2][0] += 1
        else:
            print('No')
            return
    

    print('Yes')
    for s in choice:
        if s == 'AB':
            if ans[0][0] > 0:
                print('A')
                ans[0][0] -= 1
            elif ans[0][1] > 0:
                print('B')
        elif s == 'BC':
            if ans[1][0] > 0:
                print('B')
                ans[1][0] -= 1
            else:
                print('C')
        else:
            if ans[2][0] > 0:
                print('C')
                ans[2][0] -= 1
            else:
                print('A')

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from itertools import product
from copy import deepcopy
N, Ax, Bx, Cx = map(int, input().split())
if Ax + Bx + Cx <= 1:
    print('No')
    exit()

S = [input() for _ in range(N)]

def calc(ab, ac, bc):
    A, B, C = deepcopy(Ax), deepcopy(Bx), deepcopy(Cx)
    ans = []
    for s in S:
        if s == 'AB':
            if A - ab > B:
                A -= 1
                B += 1
                ans.append('B')
            else:
                A += 1
                B -= 1
                ans.append('A')

        if s == 'AC':
            if A - ac > C:
                A -= 1
                C += 1
                ans.append('C')
            else:
                A += 1
                C -= 1
                ans.append('A')

        if s == 'BC':
            if B - bc > C:
                B -= 1
                C += 1
                ans.append('C')
            else:
                B += 1
                C -= 1
                ans.append('B')

        if A < 0 or B < 0 or C < 0:
            return False
    return ans


for pattern in product([0, 1, -1], repeat=3):
    pattern = list(pattern)
    ab, ac, bc = pattern

    ans = calc(ab, ac, bc)
    if ans:
        print('Yes')
        print(*ans, sep="\n")
        exit()

print('No')