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())
ans_list= []
chk = 0
count = 1
for i in range(N):
    s = input()
    if chk == 1:
        if s == "AC":
            num = count
            for j in reversed(range(count)):
                if num%2 == 1:
                    A += 1
                    B -= 1
                    ans_list.append(["A"])
                else:
                    A -= 1
                    B += 1
                    ans_list.append(["B"])
                num -= 1
            chk = 0
            count = 1
        elif s == "BC":
            num = count
            for j in reversed(range(count)):
                if num%2 == 1:
                    A -= 1
                    B += 1
                    ans_list.append(["B"])
                else:
                    A += 1
                    B -= 1
                    ans_list.append(["A"])
                num -= 1
            chk = 0
            count = 1
        else:
            count += 1
            continue
    elif chk == 2:
        if s == "AB":
            num = count
            for j in reversed(range(count)):
                if num%2 == 1:
                    A += 1
                    C -= 1
                    ans_list.append(["A"])
                else:
                    A -= 1
                    C += 1
                    ans_list.append(["C"])
                num -= 1
            chk = 0
            count = 1
        elif s == "BC":
            num = count
            for j in reversed(range(count)):
                if num%2 == 1:
                    A -= 1
                    C += 1
                    ans_list.append(["C"])
                else:
                    A += 1
                    C -= 1
                    ans_list.append(["A"])
                num -= 1
            chk = 0
            count = 1
        else:
            count += 1
            continue
    elif chk == 3:
        if s == "AB":
            num = count
            for j in reversed(range(count)):
                if num%2 == 1:
                    B += 1
                    C -= 1
                    ans_list.append(["B"])
                else:
                    B -= 1
                    C += 1
                    ans_list.append(["C"])
                num -= 1
            chk = 0
            count = 1
        elif s == "AC":
            num = count
            for j in reversed(range(count)):
                if num%2 == 0:
                    B -= 1
                    C += 1
                    ans_list.append(["C"])
                else:
                    B += 1
                    C -= 1
                    ans_list.append(["B"])
                num -= 1
            chk = 0
            count = 1
        else:
            count += 1
            continue
    if s == "AB" and A == 0 and B == 0:
        print("No")
        break
    elif s == "BC" and B == 0 and C == 0:
        print("No")
        break
    elif s == "AC" and A == 0 and C == 0:
        print("No")
        break
    else:
        if s == "AB":
            if A == 0:
                A += 1
                B -= 1
                ans_list.append(["A"])
            elif B == 0:
                A -= 1
                B += 1
                ans_list.append(["B"])
            elif A == 1 and B != 1 :
                A += 1
                B -= 1
                ans_list.append(["A"])
            elif B == 1 and A != 1:
                A -= 1
                B += 1
                ans_list.append(["B"])
            elif A == 1 and B == 1:
                chk = 1
            else:
                A += 1
                B -= 1
                ans_list.append(["A"])
        elif s == "AC":
            if A == 0:
                A += 1
                C -= 1
                ans_list.append(["A"])
            elif C == 0:
                A -= 1
                C += 1
                ans_list.append(["C"])
            elif A == 1 and C != 1 :
                A += 1
                C -= 1
                ans_list.append(["A"])
            elif C == 1 and A != 1:
                A -= 1
                C += 1
                ans_list.append(["C"])
            elif A == 1 and C == 1:
                chk = 2
            else:
                A += 1
                C -= 1
                ans_list.append(["A"])
        else:
            if B == 0:
                B += 1
                C -= 1
                ans_list.append(["B"])
            elif C == 0:
                B -= 1
                C += 1
                ans_list.append(["C"])
            elif B == 1 and C != 1 :
                B += 1
                C -= 1
                ans_list.append(["B"])
            elif C == 1 and B != 1:
                B -= 1
                C += 1
                ans_list.append(["C"])
            elif B == 1 and C == 1:
                chk = 3
            else:
                B += 1
                C -= 1
                ans_list.append(["B"])
if chk == 1:
    for i in range(count):
        if i%2 == 0:
            A += 1
            B -= 1
            ans_list.append(["A"])
        else:
            A -= 1
            B += 1
            ans_list.append(["B"])
elif chk == 2:
    for i in range(count):
        if i%2 == 0:
            A += 1
            C -= 1
            ans_list.append(["A"])
        else:
            A -= 1
            C += 1
            ans_list.append(["C"])
elif chk == 3:
    for i in range(count):
        if i%2 == 0:
            B += 1
            C -= 1
            ans_list.append(["B"])
        else:
            B -= 1
            C += 1
            ans_list.append(["C"])
if len(ans_list) == N:
    print("Yes")
    for i in range(N):
        print(*ans_list[i])

Submission status: Wrong Answer

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

# limited = A <= ##{i: A} - ##{i: -A}
if min(A, B, C) >= 1:
    parab, parbc, parac = 0, 0, 0
    print("Yes")
    for i in range(N):
        s = input()
        if s == "AB":
            if parab == 0:
                print("A")
                parab = 1
            else:
                print("B")
                parab = 0
        if s == "BC":
            if parbc == 0:
                print("B")
                parbc = 1
            else:
                print("C")
                parbc = 0
        if s == "AC":
            if parac == 0:
                print("A")
                parac = 1
            else:
                print("C")
                parac = 0

Submission status: Wrong Answer

Solution code:
import sys

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

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

	if d[s[0]] <= 0 and d[s[1]] <= 0:
		print('No')
		sys.exit()
	if d[s[0]] < d[s[1]]:
		d[s[1]] -= 1
		d[s[0]] += 1
		ans.append(s[0])
	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[1])
print('Yes')
for i in range(len(ans)):
	print(ans[i])

Submission status: Wrong Answer

Solution code:

N, a, b, c, *S = open(0).read().split()
N, a, b, c = map(int, (N, a, b, c))

flg = a*b*c==0 and (a*b==1 or a*c==1 or b*c == 1)

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

        ans.append('B' if a > b else 'A')
        if a> b:
            a -=1
            b += 1
        else:
            a += 1
            b -= 1

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

        ans.append('C' if a > c else 'A')
        if a> c:
            a -=1
            c += 1
        else:
            a += 1
            c -= 1

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

        ans.append('C' if b > c else 'B')
        if b> c:
            b -=1
            c += 1
        else:
            b += 1
            c -= 1

    if a < 0 or b < 0 or c < 0:
        print('No')
        exit()

print('Yes')
[print(r) for r in ans]

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
YES = "Yes"  # type: str
NO = "No"  # type: str

def cCount(s,c):
    return 1 if s[0]==c or s[1] == c else 0

def solve(n: int, A: int, B: int, C: int, s: "List[str]"):
    # S = {}
    # S[(A,B,C)] = ""
    # for i in range(n):
    #     N = {}
    #     d = s[i]
    #     for a,b,c in S:
    #         path = S[(a,b,c)]
    #         if d == "AB":
    #             if b >= n-i:
    #                 N[(a+1,b-1,c)] = path + "A"
    #                 continue
    #             if a >= n-i:
    #                 N[(a-1,b+1,c)] = path + "B"
    #                 continue
    #             if b > 0:
    #                 # A+1,B-1
    #                 N[(min(n,a+1),b-1,c)] = path + "A"
    #             if a > 0:
    #                 N[(a-1,min(n,b+1),c)] = path + "B"
    #         elif d == "BC":
    #             if b >= n - i:
    #                 N[(a, b - 1, c + 1)] = path + "C"
    #                 continue
    #             if c >= n - i:
    #                 N[(a, b+1, c-1)] = path + "B"
    #                 continue
    #             if c > 0:
    #                 # b +1,c -1
    #                 N[(a, min(n,b + 1), c-1)] = path + "B"
    #             if b > 0:
    #                 N[(a, b -1, min(n,c+1))] = path + "C"
    #         else:
    #             if c >= n - i:
    #                 N[(a + 1, b, c-1)] = path + "A"
    #                 continue
    #             if a >= n - i:
    #                 N[(a - 1, b, c+1)] = path + "C"
    #                 continue
    #             if c > 0:
    #                 N[(min(n,a+1),b,c-1)] = path + "A"
    #             if a > 0:
    #                 N[(a-1,b,min(n,c+1))] = path + "C"
    #     if len(N) == 0:
    #         print(NO)
    #         exit()
    #     S = N
    # print(YES)
    # for a,b,c in S:
    #     p = S[(a,b,c)]
    #     for i in p:
    #         print(i)
    #     break
    S = [[0,0,0] for i in range(n)]
    prev = [0,0,0]
    for i in range(n):
        command = s[i]
        prev = S[-1-i] = [prev[0]+cCount(command,"A"),prev[1]+cCount(command,"B"),prev[2]+cCount(command,"C")]
    path = []

    for i in range(n):
        if A < 0 or B < 0 or C < 0:
            print(NO)
            exit()
        command = s[i]
        if command == "AB":
            if S[i][0] <= A:
                path.append("B")
                B+=1
                A-=1
                continue
            if S[i][1] <= B:
                path.append("A")
                A+=1
                B-=1
                continue
            if S[i][0] - A >= S[i][1] - B:
                path.append("A")
                A+=1
                B-=1
            else:
                path.append("B")
                A-=1
                B+=1
        elif command == "BC":
            if S[i][1] <= B:
                path.append("C")
                B -= 1
                C += 1
                continue
            if S[i][2] <= C:
                path.append("B")
                B += 1
                C -= 1
                continue
            if S[i][1] - B >= S[i][2] - C:
                path.append("B")
                B += 1
                C -= 1
            else:
                path.append("C")
                B -= 1
                C += 1
        else:
            if S[i][0] <= A:
                path.append("C")
                C += 1
                A -= 1
                continue
            if S[i][2] <= C:
                path.append("A")
                A += 1
                C -= 1
                continue
            if S[i][0] - A >= S[i][2] - C:
                path.append("A")
                A += 1
                C -= 1
            else:
                path.append("C")
                A -= 1
                C +=1
    if A < 0 or B < 0 or C < 0:
        print(NO)
        exit()
    print(YES)
    for i in path:
        print(i)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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
N, A, B, C = map(int, input().split())
d=dict()
d["A"] = A
d["B"] = B
d["C"] = C
h = {"AB":"C","BC":"A","AC":"B"}
S = []
for _ in range(N):
    S.append(input().strip())
if A+B+C == 0:
    print("No")
elif A+B+C == 1:
    R = []
    for k in range(N):
        s = S[k]
        for i in range(2):
            if d[s[i]] == 1:
                R.append(s[1-i])
                d[s[i]] = 0
                d[s[1-i]] = 1
                break
        else:
            print("No")
            break
    else:
        print("Yes")
        for i in range(N):
            print(R[i])
else:
    R = []
    if d[S[0][0]] == 0 and d[S[0][1]] == 0:
        print("No")
        sys.exit()
    for i in range(N-1):
        s = S[i]
        if d[s[0]] == 1 and d[s[1]] == 1 and d[h[s]] == 0:
            if S[i+1] == s:
                d[s[0]] += 1
                d[s[1]] -= 1
                R.append(s[0])
            else:
                for j in range(2):
                    if s[j] not in S[i+1]:
                        d[s[j]] -= 1
                        d[s[j]] += 1
                        R.append(s[j])
                        break
        elif d[s[0]] >= d[s[1]]:
            d[s[0]] -= 1
            d[s[1]] += 1
            R.append(s[1])
        else:
            d[s[0]] += 1
            d[s[1]] -= 1
            R.append(s[0])
    for i in range(2):
        if d[S[N-1][i]] > 0:
            R.append(S[N-1][1-i])
            break
    print("Yes")
    for i in range(N):
        print(R[i])

Submission status: Wrong Answer

Solution code:
n,a,b,c  = map(int, input().split())
Ss = [input() for i in range(n)]
ans = []
flg = True
for i,S in enumerate(Ss):
    if S == "AB":
        if a == b == 0:
            flg = False
        elif a == b == 1 and i < len(Ss)-1:
            if Ss[i+1] == "AC":
                a += 1
                b -= 1
                ans.append("A")
            else:
                b += 1
                a -= 1
                ans.append("B")
        elif a < b:
            a += 1
            b -= 1
            ans.append("A")
        else:
            a -= 1
            b += 1
            ans.append("B")
    elif S == "AC":
        if a == c == 0:
            flg = False
        elif a == c == 1 and i<len(Ss)-1:
            if Ss[i+1] == "BC":
                a -= 1
                c += 1
                ans.append("C")
            else:
                a += 1
                c -= 1
                ans.append("A")

        if a < c:
            a += 1
            c -= 1
            ans.append("A")
        else:
            a -= 1
            c += 1
            ans.append("C")
    elif S == "BC":
        if b == c == 0:
            flg = False
        elif b == c == 1 and i < len(Ss)-1:
            if Ss[i+1] == "AC":
                c += 1
                b -= 1
                ans.append("C")
            else:
                c -= 1
                b += 1
                ans.append("B")
        elif b < c:
            b += 1
            c -= 1
            ans.append("B")
        else:
            b -= 1
            c += 1
            ans.append("C")
            
if flg: 
    print("Yes")
    print(*ans)
else:
    print("No")

Submission status: Wrong Answer

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


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


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


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

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


def main():

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

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

SL = []
nonZero = 0
nonZero += A>0
nonZero += B>0
nonZero += C>0

for i in range(N):
    s = input()
    SL.append(s)


def op2(f, t):
    global A, B, C
    if f=="A":
        A -=1
    elif f=="B":
        B -=1
    elif f=="C":
        C -=1
    if t=="A":
        A +=1
    elif t=="B":
        B +=1
    elif t=="C":
        C +=1

def op(f, t):
    global A, B, C
    if f=="A":
        A -=1
    elif f=="B":
        B -=1
    elif f=="C":
        C -=1
    if t=="A":
        A +=1
    elif t=="B":
        B +=1
    elif t=="C":
        C +=1
    print(t)

def show():
    print("Yes")
    global A, B, C
    for s in SL:
        # print("before: ", A, B, C)
        if s=="AB":
            if A > B:
                op("A", "B")
            else: op("B", "A")
        elif s=="BC":
            if B >C:
                op("B", "C")
            else: op("C", "B")
        elif s=="AC":
            if A >C:
                op("A", "C")
            else: op("C", "A")
        
        # print("after: ", A, B, C)
        if min(A, B, C ) <=-1:
            print("oops")
            exit()


def fight():
    global A, B, C
    pA = A
    pB = B
    pC = C
    for s in SL:
        if s=="AB":
            if A > B:
                op2("A", "B")
            else: op2("B", "A")
        elif s=="BC":
            if B >C:
                op2("B", "C")
            else: op2("C", "B")
        elif s=="AC":
            if A >C:
                op2("A", "C")
            else: op2("C", "A")
        if min(A, B, C) <= -1:
            print("No")
            exit()
    else:
        A = pA
        B = pB
        C = pC
        show()



# main


fight()


# if nonZero >=2:
#     show()

# elif nonZero == 1:
#     if max(A, B, C) >= 2:
#         s = SL[0]
#         if A!=0:
#             if s == "BC":
#                 print("No")
#                 exit()
#         if B!=0:
#             if s == "AC":
#                 print("No")
#                 exit()
#         if C!=0:
#             if s == "AB":
#                 print("No")
#                 exit()
#         show()
#     else:
#         fight()
# else:
#     print("No")

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int, input().split())
S = []
for _ in range(N):
    S.append(input())
ans = []
for s in S:
    if s == "AB":
        if A >= B:
            A -= 1
            B += 1
            ans.append("B")
        else:
            B -= 1
            A += 1
            ans.append("A")
    elif s == "AC":
        if A >= C:
            A -= 1
            C += 1
            ans.append("C")
        else:
            C -= 1
            A += 1
            ans.append("A")
    elif s == "BC":
        if B >= C:
            B -= 1
            C += 1
            ans.append("C")
        else:
            C -= 1
            B += 1
            ans.append("B")

    if not all(map(lambda x: x >= 0, (A, B, C))):
        print("No")
        break
else:
    print("Yes")
    for a in ans:
        print(a)

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[2] += 1
    elif s=="BC":
        S.append(0)
        d[0] += 1
    else:
        S.append(1)
        d[1] += 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
            ABC[y] -= 1
            ABC[x] += 1
        else:
            ans.append(SS[x])
            #ABC[y] -= 1
            #ABC[x] += 1
            ABC[y] += 1
            ABC[x] -= 1
    d[S[i]] -= 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:
i = list(map(int, input().split()))
n = i[0]
a = i[1]
b = i[2]
c = i[3]
ans_list = []
ab_next = "0"
bc_next = "0"
ca_next = "0"
flg = 1
for num in range(n):
    s = input()
    if s == "AB":
        if ab_next == "A":
            a += 1
            b -= 1
            ans_list.append("A")
            ab_next == "B"
        elif ab_next == "B":
            a -= 1
            b += 1
            ans_list.append("B")
            ab_next == "A"
        else:
            if a>b :
                a -= 1
                b += 1
                ans_list.append("B")
                ab_next == "A"
            else:
                a += 1
                b -= 1
                ans_list.append("A")
                ab_next == "B"
    elif s == "BC":
        if bc_next == "B":
            b += 1
            c -= 1
            ans_list.append("B")
            bc_next == "C"
        elif bc_next == "C":
            b -= 1
            c += 1
            ans_list.append("C")
            bc_next == "B"
        else:
            if b>c:
                b -= 1
                c += 1
                ans_list.append("C")
                bc_next == "B"
            else:
                b += 1
                c -= 1
                ans_list.append("B")
                bc_next == "C"
    else:
        if ca_next == "C":
            c += 1
            a -= 1
            ans_list.append("C")
            ca_next == "A"
        elif ca_next == "A":
            c -= 1
            a += 1
            ans_list.append("A")
            ca_next == "C"
        else:
            if c>a:
                c -= 1
                a += 1
                ans_list.append("A")
                ca_next == "C"
            else:
                c += 1
                a -= 1
                ans_list.append("C")
                ca_next == "A"
    if a<0 or b<0 or c<0:
        flg = 0
        break
        
if flg == 1:
    print("Yes")
    for ans in ans_list:
        print(ans)
else:
    print("No")

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict

read = sys.stdin.read
readline = sys.stdin.readline

N, A, B, C = map(int, readline().split())
S = list(map(str, read().split()))
v = defaultdict(int)
v['A'] = A
v['B'] = B
v['C'] = C
answer = []
stock = []
for idx, s in enumerate(S):
    i = s[0]
    j = s[1]
    if v[i] == v[j]:
        stock.append((i, j, idx))
        answer.append('stock')
        continue
    if v[i] > v[j]:
        answer.append(j)
        v[i] -= 1
        v[j] += 1
    else:
        answer.append(i)
        v[i] += 1
        v[j] -= 1
    for key, cnt in v.items():
        if cnt >= 0:
            continue
        for i, j, idx_ in stock:
            if key == i:
                if v[j] == 0:
                    print('No')
                    exit()
                v[i] += 1
                v[j] -= 1
                stock.remove((i, j, idx_))
                answer[idx_] = i
                break
            elif key == j:
                if v[i] == 0:
                    print('No')
                    exit()
                v[j] += 1
                v[i] -= 1
                stock.remove((i, j, idx_))
                answer[idx_] = j
                break
        else:
            print('No')
            exit()

print('Yes')
for i, j, idx in stock:
    if v[i] > v[j]:
        answer[idx] = j
        v[j] += 1
        v[i] -= 1
    else:
        answer[idx] = i
        v[i] += 1
        v[j] -= 1
print('\n'.join(answer))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

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

convs = dict(zip(['AB','AC','BC'], [0, 1, 2]))
chs = 'ABC'
Xss = [[0,1], [0,2], [1,2]]

for S in range(8):
#    print()
    Xss = [[0,1], [0,2], [1,2]]
    for i in range(3):
        if (S>>i)&1:
            Xss[i] = Xss[i][::-1]
#    print(bin(S)[2:].zfill(3), Xss)

    anss = []
    Ks = list([A, B, C])
    for Ss in Sss:
        no = convs[Ss]
        x, y = Xss[no]
#        print('no:', no, '/ x:', x, '/ y:', y)
        anss.append(chs[x])
        Ks[x] += 1
        Ks[y] -= 1
        if Ks[x] < 0 or Ks[y] < 0:
            break
        Xss[no] = Xss[no][::-1]
    else:
        print('Yes')
        print('\n'.join(anss))
        break
else:
    print('No')

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,input().split())
a_count,b_count,c_count = 0,0,0
S = ['' for _ in range(n)]
for i in range(n):
    s = input()
    if s == 'AB' or s=='BA':
        S[i]='AB'
        a_count +=1; b_count += 1
    elif s =='BC' or s=='CB':
        S[i]='BC'
        b_count += 1; c_count += 1
    else:
        S[i]='CA'
        a_count += 1; c_count += 1

ap = a-a_count; bp = b-b_count; cp = c-c_count
ans = []
flag = 1
for i in range(n):
    s = S[i]
    if s=='AB':
        if a==0 and b==0:
            flag = 0; break
        elif a==0: #chose B   引く方
            a+=1;b-=1;ap+=2;ans.append('A')
        elif b==0: #chose A
            b+=1;a-=1;bp+=2;ans.append('B')
        else:
            if ap<bp: #chose B
                a+=1;b-=1;ap+=2;ans.append('A')
            else: #chose A
                b+=1;a-=1;bp+=2;ans.append('B')
                
    elif s=='BC':
        if b==0 and c==0:
            flag = 0; break
        elif b==0: #chose C
            b+=1;c-=1;bp+=2;ans.append('B')
        elif c==0: #chose B
            c+=1;b-=1;cp+=2;ans.append('C')
        else:
            if bp<cp: #chose C
                b+=1;c-=1;bp+=2;ans.append('B')
            else: #chose B
                c+=1;b-=1;cp+=2;ans.append('C')
                    
                    
                    
                    
                    
    else:
        if a==0 and c==0:
            flag = 0; break
        elif a==0: #chose C
            a+=1;c-=1;ap+=2;ans.append('A')
        elif b==0: #chose A
            c+=1;a-=1;cp+=2;ans.append('C')
        else:
            if ap<cp: #chose C
                a+=1;c-=1;ap+=2;ans.append('A')
            else: #chose A
                c+=1;a-=1;cp+=2;ans.append('C')
                    
if flag ==0:
    print('No')
else:
    print('Yes')
    for i in range(n):
        print(ans[i])