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:
"""
AB,AC,BCの数をそれぞれカウントしておく。
引かれる用の変数と足される用の変数を用意しておく？

ABが出たら、Aにプラス1,Bにマイナス1する。
ACが出たら、Cにプラス1,Aにマイナス1する。
BCが出たら、Bにプラス1,Cにマイナス1する。

Cがマイナスになったら、"BCの回数”を上限にマイナス分をBから分けてもらう。
Bがマイナスになったら、"ABの回数"を上限にマイナス分をAから分けてもらう。
Aがマイナスになったら、"ACの回数"を上限にマイナス分をCから分けてもらう。
Cがマイナスになったら、"BCの回数”を上限にマイナス分をBから分けてもらう。

ここまでやって、どれか一つでも-があれば不可能。
もし、全部プラスになれば、行った操作を記録しているので、それをもとに復元を行う。

"""
import copy
from collections import Counter
N,A,B,C = map(int,input().split())
tmp = []
for i in range(N):
    s = input()
    tmp.append(s)
game = Counter(tmp)
if "AB" not in game:
    game["AB"] = 0
if "AC" not in game:
    game["AC"] = 0
if "BC" not in game:
    game["BC"] = 0

default = copy.copy(game)

A += game["AB"]
B -= game["AB"]
B += game["BC"]
C -= game["BC"]
C += game["AC"]
A -= game["AC"]

if C < 0:
    diff = 0 - C
    if diff > game["BC"]:
        C += diff
        B -= diff
        game["BC"] -= diff
    else:
        print("No")
        exit()
if B < 0:
    diff = 0 - B
    if diff > game["AB"]:
        B += diff
        A -= diff
        game["AB"] -= diff
    else:
        print("No")
        exit()
if A < 0:
    diff = 0 - A
    if diff > game["AC"]:
        A += diff
        C -= diff
        game["AC"] -= diff
    else:
        print("No")
        exit()
if C < 0:
    diff = 0 - C
    if diff > game["BC"]:
        C += diff
        B -= diff
        game["BC"] -= diff
    else:
        print("No")
        exit()

print("Yes")
for s in tmp:
    if s == "AB":
        if game[s]>0:
            print("A")
            game[s] -= 1
        else:
            print("B")
        
    if s == "BC":
        if game[s]>0:
            print("B")
            game[s] -= 1
        else:
            print("C")
    if s == "AC":
        if game[s]>0:
            print("C")
            game[s] -= 1
        else:
            print("A")

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"

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

if S >= 3:
    for i in range(N):
        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:
import sys

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

s = []
for _ in range(n):
	s.append(input())

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

	if d[s[i][0]] <= 0 and d[s[i][1]] <= 0:
		print('No')
		sys.exit()
	if d[s[i][0]] < d[s[i][1]]:
		d[s[i][1]] -= 1
		d[s[i][0]] += 1
		ans.append(s[i][0])
	elif d[s[i][0]] > d[s[i][1]]:
		d[s[i][0]] -= 1
		d[s[i][1]] += 1
		ans.append(s[i][1])
	else:
		if i < n-1 and s[i+1].strip(s[i]) != '' and d[s[i+1].strip(s[i])] == 0:
			d[s[i+1].strip(s[i+1].strip(s[i]))] += 1
			ans.append(s[i+1].strip(s[i+1].strip(s[i])))
		else:
			d[s[i][0]] += 1
			ans.append(s[i][0])
print('Yes')
for i in range(len(ans)):
	print(ans[i])

Submission status: Wrong Answer

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

d = {"A": A, "B": B, "C": C}

order = []
for i_s in s:
    letter_1, letter_2 = list(i_s)
    larger = letter_1 if d[letter_1] > d[letter_2] else letter_2
    smaller = letter_2 if letter_1 == larger else letter_1
    d[larger] -= 1
    if d[larger] < 0:
        print("No")
        break
    d[smaller] += 1
    order.append(smaller)
else:
    print("Yes")
    for o in order:
        print(o)

Submission status: Wrong Answer

Solution code:
import random
def answer(x):
    print("Yes")
    AB = ["B"] * x[0][0] + ["A"] * x[0][1]
    AC = ["C"] * x[1][0] + ["A"] * x[1][1]
    BC = ["C"] * x[2][0] + ["B"] * x[2][1]
    random.shuffle(AB)
    random.shuffle(AC)
    random.shuffle(BC)
    for i in range(n):
        if s[i] == "AB":
            print(AB.pop(0))
        elif s[i] == "AC":
            print(AC.pop(0))
        else:
            print(BC.pop(0))
    exit()

n,a,b,c = map(int,input().split())
s = [str(input()) for i in range(n)]
cnt = [0 for i in range(3)]
for ss in s:
    if ss == "AB":
        a += 1
        b += 1
        cnt[0] += 1
    elif ss == "AC":
        a += 1
        c += 1
        cnt[1] += 1
    else:
        b += 1
        c += 1
        cnt[2] += 1
a,b,c = a//2,b//2,c//2
all = min(min(cnt),min(a,b,c))
a,b,c = a-all,b-all,c-all
for i in range(3):
    cnt[i] -= all
  # cnt=["AB","AC","BC"]

#"AB"=0
if cnt[0] == 0:
        ACdeA = min(cnt[1],a)
        BCdeB = min(cnt[2],b)
        ACdeC = cnt[1]-ACdeA
        BCdeC = cnt[2]-BCdeB
        if ACdeC + BCdeC <= c:
            answer([[all,0],[ACdeA,all+ACdeC],[all+BCdeB,BCdeC]])
if cnt[1] == 0:
        ABdeA = min(cnt[0],a)
        BCdeC = min(cnt[2],c)
        ABdeB = cnt[0]-ABdeA
        BCdeB = cnt[2]-BCdeC
        if ABdeB + BCdeB <= b:
            answer([[all+ABdeA,ABdeB],[0,all],[all+BCdeB,BCdeC]])
if cnt[2] == 0:
        ABdeB = min(cnt[0],b)
        ACdeC = min(cnt[1],c)
        ABdeA = cnt[0]-ABdeB
        ACdeA = cnt[1]-ACdeC
        if ABdeA + ACdeA <= a:
            answer([[all+ABdeA,ABdeB],[ACdeA,ACdeC+all],[all,0]])
if a == 0:
    ABdeB = cnt[0]
    ACdeC = cnt[1]
    b -= ABdeB
    c -= ACdeC
    if b >= 0 and c >= 0:
        BCdeB = min(b,cnt[2])
        BCdeC = cnt[2]-BCdeB
        if BCdeC <= c:
             answer([[all,ABdeB],[0,ACdeC+all],[all+BCdeB,BCdeC]])
    b += ABdeB
    c += ACdeC
if b == 0:
    ABdeA = cnt[0]
    BCdeC = cnt[2]
    a -= ABdeA
    c -= BCdeC
    if a >= 0 and c >= 0:
        ACdeA = min(a,cnt[1])
        ACdeC = cnt[1]-ACdeA
        if ACdeC <= c:
            answer([[all+ABdeA,0],[ACdeA,ACdeC+all],[all,BCdeC]])
    a += ABdeA
    c += BCdeC
if c == 0:
    ACdeA = cnt[1]
    BCdeB = cnt[2]
    a -= ACdeA
    b -= BCdeB
    if a >= 0 and b >= 0:
        ABdeA = min(a,cnt[0])
        ABdeB = cnt[0]-ABdeA
        if ABdeB <= b:
            answer([[all+ABdeA,ABdeB],[ACdeA,all],[BCdeB+all,0]])
print("No")

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]
                print(YES)

                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
                                a -= 1
                                print("B")
                            else:
                                b -= 1
                                a += 1
                                print("A")
                            tmp = [a,b,c]
                            continue

                    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:
# -*- coding: utf-8 -*-
abc = ["A","B","C"]

def calculate(x,y, b, e):
    flag = 0
    ans = ""
    if x <= y:
        y -= 1
        x += 1
        #print(y)
        if y < 0:
            print("No")
            flag = 1
            return(x, y, ans, flag)
        ans = abc[b]
    else:
        x -= 1
        y += 1
        ans = ""
        #print(x)
        if x < 0:
            print("No")
            flag = 1
            return(x, y, ans, flag)
        ans = abc[e]
    return (x, y, ans, flag)

def main():
    N, A, B, C  =  [int(i) for i in input().split()]
    s = []
    for i in range(N):
        t = input()
        if t == "AB":
            s.append([0,1])
        if t == "AC":
            s.append([0,2])
        if t == "BC":
            s.append([1,2])
    #print(s)
    answers  = []
    for ss in s:
        ans = ""
        f = 0
        if ss == [0,1]:
            #print(1)
            A, B, ans, f = calculate(A,B,0,1)
        if ss == [1,2]:
            #print(2)
            B, C, ans,f  = calculate(B,C,1,2)
        if ss == [0,2]:
            #print(3)
            A, C, ans,f  = calculate(A,C,0,1)
        answers.append(ans)
        if f == 1:
            return

    for a in answers:
        print(a)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N,A,B,C=map(int,input().split())
move=[]
judge=True
for i in range(N):
    s=input()
    if(s=="AB"):
        if(A==0 and B==0):
            judge=False
            print("No")
        elif(A>=B):
            A-=1
            B+=1
            move.append("B")
        else:
            A+=1
            B-=1
            move.append("A")
    if(s=="AC"):
        if(A==0 and C==0):
            judge=False
            print("No")
        elif(A>=C):
            A-=1
            C+=1
            move.append("C")
        else:
            A+=1
            C-=1
            move.append("A")
    if(s=="BC"):
        if(B==0 and C==0):
            judge=False
            print("No")
        elif(B>=C):
            B-=1
            C+=1
            move.append("C")
        else:
            B+=1
            C-=1
            move.append("B")
if(judge):
    print("Yes")
    for i in range(N):
        print(move[i])

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]
                print(YES)

                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]
                            continue

                    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:
n,a,b,c=map(int,input().split())
ab=[0]*n
ac=[0]*n
bc=[0]*n
AB=0
BC=0
AC=0
ip=[0]*n
for i in range(n):
	ip[i]=input()
	if ip[i]=="AB":
		AB+=1
	elif ip[i]=="BC":
		BC+=1
	else:
		AC+=1
	ab[i]=AB
	bc[i]=BC
	ac[i]=AC
res=[0]*n
ans="Yes"
for i in range(n):
	AB=(ab[n-1]-ab[i])*2-(a+b)
	BC=(bc[n-1]-bc[i])*2-(b+c)
	AC=(ac[n-1]-ac[i])*2-(a+c)
	if ip[i]=="AB":
		if AC>BC:
			res[i]="A"
			a+=1
			b-=1
		else:
			res[i]="B"
			a-=1
			b+=1
	if ip[i]=="BC":
		if AB>AC:
			res[i]="B"
			b+=1
			c-=1
		else:
			res[i]="C"
			b-=1
			c+=1
	if ip[i]=="AC":
		if AB>BC:
			res[i]="A"
			a+=1
			c-=1
		else:
			res[i]="C"
			a-=1
			c+=1
	if a<0 or b<0 or c<0:
		ans="No"
		break
if ans=="Yes":
	print(ans)
	for i in range(n):
		print(res[i])
else:
	print(ans)

Submission status: Wrong Answer

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

op = []
dq = deque([[A,B,C,0,op]])
while dq:
    A,B,C,i,op = dq.pop()
    if min(A,B,C) < 0:
        continue
    if i == N:
        break
    s = S[i]
    if s == 'AB':
        if A != 0:
            dq.append([A-1,B+1,C,i+1,op+['A']])
        if B != 0:
            dq.append([A+1,B-1,C,i+1,op+['B']])
    if s == 'AC':
        if A != 0:
            dq.append([A-1,B,C+1,i+1,op+['A']])
        if C != 0:
            dq.append([A+1,B-1,C-1,i+1,op+['B']])
    if s == 'BC':
        if B != 0:
            dq.append([A-1,B-1,C+1,i+1,op+['B']])
        if C != 0:
            dq.append([A+1,B+1,C-1,i+1,op+['C']])


if i == N:
    print('Yes')
    for p in op:
        print(p)
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
            else:
                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:
N, A, B, C = map(int, input().split())

choice = []
for _ in range(N):
    s = input()
    if s == "AB":
        if A == 0 and B == 0:
            print("No")
            break
        else:
            if A >= B:
                A -= 1
                B += 1
                choice.append("B")
            else:
                B -= 1
                A += 1
                choice.append("A")
                
    if s == "BC":
        if B == 0 and C == 0:
            print("No")
            break
        else:
            if B >= B:
                B -= 1
                C += 1
                choice.append("C")

            else:
                C -= 1
                B += 1
                choice.append("B")

    if s == "AC":
        if A == 0 and C == 0:
            print("No")
            break
        else:
            if A >= C:
                A -= 1
                C += 1
                choice.append("C")
            else:
                C -= 1
                A += 1
                choice.append("A")
else:
    for s in choice:
        print(s)