Problem:


Score : 600 points

Problem Statement

There is a game that involves three variables, denoted A, B, and C.

As the game progresses, there will be N events where you are asked to make a choice.
Each of these choices is represented by a string s_i. If s_i is AB, you must add 1 to A or B then subtract 1 from the other; if s_i is AC, you must add 1 to A or C then subtract 1 from the other; if s_i is BC, you must add 1 to B or C then subtract 1 from the other.

After each choice, none of A, B, and C should be negative.

Determine whether it is possible to make N choices under this condition. If it is possible, also give one such way to make the choices.

Constraints

1 \leq N \leq 10^5

0 \leq A,B,C \leq 10^9

N, A, B, C are integers.

s_i is AB, AC, or BC.

Input

Input is given from Standard Input in the following format:

N A B C
s_1
s_2
:
s_N

Output

If it is possible to make N choices under the condition, print Yes; otherwise, print No.

Also, in the former case, show one such way to make the choices in the subsequent N lines. The (i+1)-th line should contain the name of the variable (A, B, or C) to which you add 1 in the i-th choice.

Sample Input 1

2 1 3 0
AB
AC

Sample Output 1

Yes
A
C

You can successfully make two choices, as follows:

In the first choice, add 1 to A and subtract 1 from B. A becomes 2, and B becomes 2.

In the second choice, add 1 to C and subtract 1 from A. C becomes 1, and A becomes 1.

Sample Input 2

3 1 0 0
AB
BC
AB

Sample Output 2

No

Sample Input 3

1 0 9 0
AC

Sample Output 3

No

Sample Input 4

8 6 9 1
AC
BC
AB
BC
AC
BC
AB
AB

Sample Output 4

Yes
C
B
B
C
C
B
A
A



Sample input:
2 1 3 0
AB
AC


Sample output:
Yes
A
C

Submission status: Wrong Answer

Solution code:
N,A,B,C = map(int,(input().split()))
s = [[] 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
            elif A == 0:
                ans.append("A")
                A += 1
                B -= 1
            else:
                if s[i+1] == "AC":
                    ans.append("A")
                    A += 1
                    B -= 1
                else:
                    ans.append("A")
                    B += 1
                    A -= 1

        if s[i] == "AC":
            if A+C <= 0:
                flag = 1
                break
            if C == 0:
                ans.append("C")
                C += 1
                A -= 1
            elif A == 0:
                ans.append("A")
                A += 1
                C -= 1
            else:
                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 == 0:
                ans.append("C")
                C += 1
                B -= 1
            elif B == 0:
                ans.append("B")
                B += 1
                C -= 1
            else:
                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:
n, a, b, c = map(int, input().split())
s = [input() for _ in range(n)]

d = {"AB": 0, "AC": 0, "BC": 0}

for e in s:
    d[e] += 1

da = d["AB"] + d["AC"]
db = d["AB"] + d["BC"]
dc = d["AC"] + d["BC"]

aa = a + da
bb = b + db
cc = c + dc

amx = aa // 2
bmx = bb // 2
cmx = cc // 2

ab = "AB"
bc = "BC"
ac = "AC"

if amx >= bmx >= cmx:
    li = [ac, ab, bc]
    mx = [amx, bmx, cmx]
    dd = [0, 1, 2]
elif amx >= cmx >= bmx:
    li = [ab, ac, bc]
    mx = [amx, cmx, bmx]
    dd = [0, 2, 1]
elif bmx >= amx >= cmx:
    li = [bc, ab, ac]
    mx = [bmx, amx, cmx]
    dd = [1, 0, 2]
elif bmx >= cmx >= amx:
    li = [ab, bc, ac]
    mx = [bmx, cmx, amx]
    dd = [1, 2, 0]
elif cmx >= amx >= bmx:
    li = [bc, ac, ab]
    mx = [cmx, amx, bmx]
    dd = [2, 0, 1]
else:
    li = [ac, bc, ab]
    mx = [cmx, bmx, amx]
    dd = [2, 1, 0]

cnt = {ab: [0] * 3, ac: [0] * 3, bc: [0] * 3}
int2c = ["A", "B", "C"]
for key in li:
    val = d[key]

    while val:
        val -= 1
        i = 0
        while mx[i] == 0 or int2c[dd[i]] not in key:
            i += 1
            if i == 3:
                break
        if i == 3:
            print("No")
            exit()
        mx[i] -= 1

        for e in key:
            if e != int2c[dd[i]]:
                cnt[key][int2c.index(e)] += 1

sm = 0
for v in cnt.values():
    sm += sum(v)

if sm != n:
    print("No")
    exit()

print("Yes")
for e in s:
    cnts = cnt[e]
    if cnts[0] > 0:
        print("A")
        cnt[e][0] -= 1
    elif cnts[1] > 0:
        print("B")
        cnt[e][1] -= 1
    else:
        print("C")
        cnt[e][2] -= 1

Submission status: Wrong Answer

Solution code:
inpl = lambda: list(map(int,input().split()))
N, A, B, C = inpl()
ans = []
stack = []
command = [ input() for _ in range(N) ]
pool = [(0, None, A, B, C)]
while pool:
    i, r, a, b, c = pool.pop()
    X = [a,b,c]
    while len(ans) >= max(i,1):
        ans.pop()
    if r is not None:
        ans.append('ABC'[r])
    if i >= N:
        print('Yes')
        for i in range(N):
            print(ans[i])
        exit()
    c = command[i]
    p = ord(c[0]) - ord('A')
    q = ord(c[1]) - ord('A')
    if X[p] == X[q] == 0:
        continue
    elif X[p] == X[q] == 1:
        Y = [X[0],X[1],X[2]]
        X[p] += 1; X[q] -= 1
        Y[p] -= 1; Y[q] += 1
        pool.append((i+1, p, X[0], X[1], X[2]))
        pool.append((i+1, q, Y[0], Y[1], Y[2]))
    elif X[p] <= X[q]:
        pool.append((i+1, p, X[0],X[1],X[2]))
    else:
        pool.append((i+1, q, X[0],X[1],X[2]))
else:
    print('No')

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
lisa=[a]+[None]*n
lisb=[b]+[None]*n
lisc=[c]+[None]*n
lis=[0]*n
for i in range(n):
    lis[i]=input()
t=[None]*n
for i in range(n):
    if i!=0:
        if lis[i-1] == lis[i]:
            if t[i-1] == lis[i][0]:
                t[i] = lis[i][1]
            else:
                t[i] = lis[i][0]
            lisa[i+1]=lisa[i-1]
            lisb[i+1]=lisb[i-1]
            lisc[i+1]=lisc[i-1]
            continue
    if lis[i]=="AB":
        if lisa[i]==0 and lisb[i]==0:
            print("No")
            exit()
        elif lisa[i]>=lisb[i]:
            t[i]="B"
            lisa[i+1]=lisa[i]-1
            lisb[i+1]=lisb[i]+1
            lisc[i+1]=lisc[i]
        else:
            t[i]="A"
            lisa[i+1]=lisa[i]+1
            lisb[i+1]=lisb[i]-1
            lisc[i+1]=lisc[i]
    elif lis[i]=="BC":
        if lisb[i]==0 and lisc[i]==0:
            print("No")
            exit()
        elif lisb[i]>=lisc[i]:
            t[i]="C"
            lisb[i+1]=lisb[i]-1
            lisc[i+1]=lisc[i]+1
            lisa[i+1]=lisa[i]
        else:
            t[i]="B"
            lisb[i+1]=lisb[i]+1
            lisc[i+1]=lisc[i]-1
            lisa[i+1]=lisa[i]
    else:
        if lisa[i]==0 and lisc[i]==0:
            print("No")
            exit()
        elif lisa[i]>=lisc[i]:
            t[i]="C"
            lisa[i+1]=lisa[i]-1
            lisc[i+1]=lisc[i]+1
            lisb[i+1]=lisb[i]
        else:
            t[i]="A"
            lisa[i+1]=lisa[i]+1
            lisc[i+1]=lisc[i]-1
            lisb[i+1]=lisb[i]
print("Yes")
for i in t:
    print(i)

Submission status: Wrong Answer

Solution code:
def change(d,ans,X,Y):
    x = {'A': 0, 'B': 1, 'C': 2}
    a = x[X]
    b = x[Y]
    if d[a]>0:
        d[a]-=1
        d[b]+=1
        ans.append(Y)
    else:
        d[b]-=1
        d[a]+=1
        ans.append(X)
    return d,ans


n,a,b,c = map(int, input().split())
d = [a,b,c]
q = [input() for i in range(n)]
x = {'A': 0, 'B': 1, 'C': 2}

if d.count(0)==3:
    print('No')
elif d.count(0)==2:
    ans = []
    for i in range(n):
        d,ans = change(d,ans,q[i][0],q[i][1])
        if d[0]<0 or d[1]<0 or d[2]<0:
            print('No')
            exit()
    print('Yes')
    print('\n'.join(ans))
elif d.count(0)==1:
    ans = []
    flag = 0
    for i in range(n-1):
        if flag:
            flag = 0
            continue
        s = x[q[i][0]]
        t = x[q[i][1]]
        if d[s]==0 or d[t]==0:
            d,ans = change(d,ans,q[i][0],q[i][1])
        else:
            flag = 1
            if q[i]==q[i+1]:
                ans.append(q[i][0])
                ans.append(q[i][1])
            elif q[i][0] in q[i+1]:
                ans.append(q[i][0])
                d,ans = change(d,ans,q[i][1],q[i+1].replace(q[i][0],''))
            else:
                ans.append(q[i][1])
                d,ans = change(d,ans,q[i][0],q[i+1].replace(q[i][1],''))
    if len(ans)!=n:
        s = x[q[-1][0]]
        t = x[q[-1][1]]
        if d[s]>0:
            ans.append(q[-1][1])
        else:
            ans.append(q[-1][0])
    print('Yes')
    print('\n'.join(ans))
else:
    ans = []
    flag = 0
    for i in range(n-1):
        s = x[q[i][0]]
        t = x[q[i][1]]
        if flag:
            flag = 0
            continue
        if d[0]>0 and d[1]>0 and d[2]>0:
            ans.append(q[i][0])
            d[s]+=1
            d[t]-=1
            continue
        if d[s]==0 or d[t]==0:
            d,ans = change(d,ans,q[i][0],q[i][1])
        else:
            flag = 1
            if q[i]==q[i+1]:
                ans.append(q[i][0])
                ans.append(q[i][1])
            elif q[i][0] in q[i+1]:
                ans.append(q[i][0])
                d,ans = change(d,ans,q[i][1],q[i+1].replace(q[i][0],''))
            else:
                ans.append(q[i][1])
                d,ans = change(d,ans,q[i][0],q[i+1].replace(q[i][1],''))
    if len(ans)!=n:
        s = x[q[-1][0]]
        t = x[q[-1][1]]
        if d[s]>0:
            ans.append(q[-1][1])
        else:
            ans.append(q[-1][0])
    print('Yes')
    print('\n'.join(ans))

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
            elif A == 0:
                ans.append("A")
                A += 1
                B -= 1
            else:
                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
            elif A == 0:
                ans.append("A")
                A += 1
                C -= 1
            else:
                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 == 0:
                ans.append("C")
                C += 1
                B -= 1
            elif B == 0:
                ans.append("B")
                B += 1
                C -= 1
            else:
                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:
if __name__ == '__main__':
    from sys import stdin
    # from collections import defaultdict
    # from collections import deque
    # from collections import Counter
    import math
    # import random
    input = stdin.readline


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

    count_ss = {'AB':0, 'AC':0, 'BC':0}
    ss = ['']*n
    for i in range(n):
        ss[i] = input()[:-1]
        count_ss[ss[i]] += 1

    need_a = math.ceil(-1*((a-n+count_ss['BC']))/2)
    need_b = math.ceil(-1*((b-n+count_ss['AC']))/2)
    need_c = math.ceil(-1*((c-n+count_ss['AB']))/2)
    if need_a < 0:
        need_a = 0
    if need_b < 0:
        need_b = 0
    if need_c < 0:
        need_c = 0
    for i in range(count_ss['AB']+1):
        ac_a = need_a-i
        if ac_a > count_ss['AC']:
            continue
        ac_c = count_ss['AC']-ac_a
        bc_c = need_c-ac_c
        if bc_c > count_ss['BC']:
            continue
        if count_ss['BC'] - bc_c + i > need_b:
            break
    else:
        print('No')
        exit()
    bc_b = count_ss['BC']-bc_c
    ab_a = i
    print('Yes')
    for s in ss:
        if s == 'AB':
            if ab_a > 0:
                print('A')
                ab_a -= 1
            else:
                print('B')
        if s == 'BC':
            if bc_b > 0:
                print('B')
                bc_b -= 1
            else:
                print('C')
        if s == 'AC':
            if ac_a > 0:
                print('A')
                ac_a -= 1
            else:
                print('C')

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,input().split())
ans = []
ab_counter=0
ac_counter=0
bc_counter=0
for i in range(n):
    s = input()
    if s=="AB":
        if a<=0 and b<=0:
            print("No")
            exit()
        if a>b:
            ans.append("B")
            ab_counter += 1
            b+=1
            a-=1
        else:
            ans.append("A")
            ab_counter += 1
            a+=1
            b-=1
    if s=="AC":
        if a<=0 and b<=0:
            print("No")
            exit()
        if a>c:
            ans.append("C")
            ac_counter += 1
            c+=1
            a-=1
        else:
            ans.append("A")
            ac_counter += 1
            a+=1
            b-=1
    if s=="BC":
        if a<=0 and b<=0:
            print("No")
            exit()
        if c>b:
            ans.append("B")
            bc_counter += 1
            b+=1
            c-=1
        else:
            ans.append("C")
            bc_counter += 1
            c+=1
            b-=1
print("Yes")
print("\n".join(ans))

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")
                            elif s[i+1] == "AC":
                                b -= 1
                                a += 1
                                print("A")
                            else:
                                if a<=b:
                                    a += 1
                                    b -= 1
                                    print("A")
                                else:
                                    a -=1
                                    b += 1
                                    print("B")                                    
                            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")
                            elif s[i+1] == "AB":
                                b += 1
                                c -= 1
                                print("B")
                            else:
                                if b<=c:
                                    b += 1
                                    c -= 1
                                    print('B')

                                else:
                                    b -=1
                                    c += 1
                                    print('C')
                            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")
                            elif s[i+1] == "AB":
                                c -= 1
                                a += 1
                                print("A")    
                            else:
                                if a<=c:
                                    a += 1
                                    c -= 1
                                    print('A')
                                else:
                                    a -=1
                                    c += 1
                                    print('C')
                            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())
ans = []
ab_counter=0
ac_counter=0
bc_counter=0
for i in range(n):
    s = input()
    if s=="AB":
        if a<=0 and b<=0:
            if bc_counter >= 1 and c >= 2:
                b+=1
                c-=1
                bc_counter -= 1
            elif ac_counter >= 1 and c >= 2:
                a+=1
                c-=1
                ac_counter -= 1
            if a<=0 and b<=0:
                print("No")
                exit()
        if a>b:
            ans.append("B")
            ab_counter += 1
            b+=1
            a-=1
        else:
            ans.append("A")
            ab_counter += 1
            a+=1
            b-=1
    if s=="AC":
        if bc_counter >= 1 and b >= 2:
            c+=1
            b-=1
            bc_counter -= 1
        elif ab_counter >= 1 and b >= 2:
            a+=1
            b-=1
            ac_counter -= 1
        if a<=0 and b<=0:
            print("No")
            exit()
        if a>c:
            ans.append("C")
            ac_counter += 1
            c+=1
            a-=1
        else:
            ans.append("A")
            ac_counter += 1
            a+=1
            b-=1
    if s=="BC":
        if ac_counter >= 1 and a >= 2:
            c+=1
            a-=1
            ac_counter -= 1
        elif ab_counter >= 1 and a >= 2:
            b+=1
            a-=1
            bc_counter -= 1
        if a<=0 and b<=0:
            print("No")
            exit()
        if c>b:
            ans.append("B")
            bc_counter += 1
            b+=1
            c-=1
        else:
            ans.append("C")
            bc_counter += 1
            c+=1
            b-=1
print("Yes")
print("\n".join(ans))

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    from sys import stdin
    # from collections import defaultdict
    # from collections import deque
    # from collections import Counter
    import math
    # import random
    input = stdin.readline


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

    count_ss = {'AB':0, 'AC':0, 'BC':0}
    ss = ['']*n
    for i in range(n):
        ss[i] = input()[:-1]
        count_ss[ss[i]] += 1

    need_a = math.ceil(-1*((a-n+count_ss['BC']))/2)
    need_b = math.ceil(-1*((b-n+count_ss['AC']))/2)
    need_c = math.ceil(-1*((c-n+count_ss['AB']))/2)
    if need_a < 0:
        need_a = 0
    if need_b < 0:
        need_b = 0
    if need_c < 0:
        need_c = 0
    if need_a+need_b+need_c > n:
        print('No')
        exit()
    for i in range(count_ss['AB']+1):
        ac_a = need_a-i
        if ac_a > count_ss['AC']:
            continue
        ac_c = count_ss['AC']-ac_a
        bc_c = need_c-ac_c
        if bc_c > count_ss['BC']:
            continue
        if count_ss['BC'] - bc_c + i > need_b:
            break
    else:
        print('No')
    bc_b = count_ss['BC']-bc_c
    ab_a = i
    print('Yes')
    for s in ss:
        if s == 'AB':
            if ab_a > 0:
                print('A')
                ab_a -= 1
            else:
                print('B')
        if s == 'BC':
            if bc_b > 0:
                print('B')
                bc_b -= 1
            else:
                print('C')
        if s == 'AC':
            if ac_a > 0:
                print('A')
                ac_a -= 1
            else:
                print('C')

Submission status: Wrong Answer

Solution code:
# coding: utf-8
s = input().split()

n = int(s[0])
abc = [int(s[1]), int(s[2]), int(s[3])]

res = ''

for i in range(n):
    s = input()
    s1 = ord(s[0]) - ord('A')
    s2 = ord(s[1]) - ord('A')
    
    if abc[s1] > abc[s2]:
        abc[s1] -= 1
        abc[s2] += 1
        res += chr(s2 + ord('A')) + '\n'
    else:
        abc[s1] += 1
        abc[s2] -= 1
        res += chr(s1 + ord('A')) + '\n'
        
    for j in range(3):
        if abc[j] < 0:
            print('No')
            res = ''
            break
        
if not res == '':
    print('Yes')
    print(res)

Submission status: Wrong Answer

Solution code:
def mp():return map(int,input().split())
def lmp():return list(map(int,input().split()))


n,a,b,c = mp()
ans_ar = []
q = []
for i in range(n):
    s = input()
    q.append(s)
for i in range(n):
    s = q[i]
    if s == "AB":
        if a == b == 0:
            print("No")
            exit()
        elif a == b == 1 and i != n-1:
            if q[i+1] == "AC":
                a += 1
                b -= 1
                ans_ar.append("A")
            else:
                a -= 1
                b += 1
                ans_ar.append("B")
        elif a >= b:
            a -= 1
            b += 1
            ans_ar.append("B")
        else:
            a += 1
            b -= 1
            ans_ar.append("A")
    if s == "AC":
        if a == c == 0:
            print("No")
            exit()
        elif a == c == 1 and i != n - 1:
            if q[i + 1] == "AB":
                a += 1
                c -= 1
                ans_ar.append("A")
            else:
                a -= 1
                c += 1
                ans_ar.append("C")

        elif a >= c:
            a -= 1
            c += 1
            ans_ar.append("C")
        else:
            a += 1
            c -= 1
            ans_ar.append("A")
    if s == "BC":
        if c == b == 0:
            print("No")
            exit()
        elif c == b == 1 and i != n-1:
            if q[i+1] == "AC":
                c += 1
                b -= 1
                ans_ar.append("C")
            else:
                c -= 1
                b += 1
                ans_ar.append("B")

        elif c >= b:
            c -= 1
            b += 1
            ans_ar.append("B")
        else:
            c += 1
            b -= 1
            ans_ar.append("C")
for i in ans_ar:
    print(i)