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):
        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 == 1:
                ans.append("C")
                C += 1
                B -= 1
            elif B == 1:
                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:
# -*- coding: utf-8 -*-
"""
@author: H_Hoshigi
"""
def main():
    N, A, B, C = map(int, input().split())
    S_lsit = []
    for i in range(N):
        S_lsit.append(input())
    x, y, z = 0, 0, 0
    for S in S_lsit:
        if S == "AB":
            x += 1
        elif S == "BC":
            y += 1
        else:
            z += 1
    a_ac, b_ac, c_ac = -1, -1, -1
    for a in range(x+1):
        for b in range(y+1):
            for c in range(z+1):
                if (A -2*a +x +2*c -z) >= 0 \
                    and (B -2*b +y +2*a -x) >= 0 \
                    and (C -2*c +z +2*b -y) >= 0:
                    a_ac, b_ac, c_ac = a, b, c
                    break
                    break
                    break
    print([a_ac, b_ac, c_ac])
    if a_ac==-1 or b_ac==-1 or c_ac==-1:
        print("No")
    else:
        print("Yes")
        for S in S_lsit:
            if S == "AB":
                if a_ac >= 1 and A >= 1:
                    print("B")
                    A -= 1
                    B += 1
                    a_ac -= 1
                else:
                    print("A")
                    A += 1
                    B -= 1
            elif S == "BC":
                if b_ac >= 1 and B >= 1:
                    print("C")
                    B -= 1
                    C += 1
                    b_ac -= 1
                else:
                    print("B")
                    B += 1
                    C -= 1
            else:
                if c_ac >= 1 and C >= 1:
                    print("A")
                    C -= 1
                    A += 1
                    c_ac -= 1
                else:
                    print("C")
                    C += 1
                    B -= 1
    return

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"

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 == 1:
                ans.append("C")
                C += 1
                B -= 1
            elif B == 1:
                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:


def solve(a,b,c):
    ans = []
    # if (int(a!=0) + int(b!=0) + int(c!=0)) == 3 or (int(a!=0) + int(b!=0) + int(c!=0)) == 2:
    #     ans.append("Yes")
    # print(a, b, c)
    for i in range(n):
        if a<0 or b<0 or c<0:
            print("No")
            exit()
        if s[i] == "AB":
            if b>a:
                ans.append("A")
                b-=1
                a+=1
            elif a>b:
                ans.append("B")
                b+=1
                a-=1
            elif b==a:
                if a == b == 0:
                    print("No")
                    exit()
                move = checknext(i)
                if move == "BC":
                    ans.append("B")
                    b+=1
                    a-=1
                elif move == "AC":
                    ans.append("A")
                    a+=1
                    b-=1
        if s[i] == "BC":
            if c>b:
                ans.append("B")
                c-=1
                b+=1
            elif b>c:
                ans.append("C")
                c+=1
                b-=1
            elif c==b:
                if b == c == 0:
                    print("No")
                    exit()
                move = checknext(i)
                if move == "AC":
                    ans.append("C")
                    c+=1
                    b-=1
                elif move == "AB":
                    ans.append("B")
                    b+=1
                    c-=1
        if s[i] == "AC":
            if c>a:
                ans.append("A")
                c-=1
                a+=1
            elif a>c:
                ans.append("C")
                c+=1
                a-=1
            elif c==a:
                if a == c == 0:
                    print("No")
                    exit()
                move = checknext(i)
                if move == "BC":
                    ans.append("C")
                    c+=1
                    a-=1
                if move == "AB":
                    ans.append("A")
                    a+=1
                    c-=1
    print("Yes")
    for a in ans:
        print(a)
    return

def checknext(i):
    curr_move = s[i]
    while curr_move == s[i]:
        if i+1==n:
            if curr_move == "AB":
                return "BC"
            else:
                return "AB"
        i = i+1
        curr_move = s[i]
    return curr_move




n, a, b, c = [int(s) for s in input().split(" ")]
s = []
for i in range(n):
    s.append(input())
solve(a,b,c)

Submission status: Wrong Answer

Solution code:
def main():
    n,a,b,c=map(int,input().split())
    ans=[]
    for i in range(n):
        s=input()
        if s=="AB":
                if a>b:
                    a-=1
                    b+=1
                    ans.append("B")
                else:
                    a+=1
                    b-=1
                    ans.append("A")
    
        elif s=="BC":
                if c>b:
                    c-=1
                    b+=1
                    ans.append("B")
                else:
                    c+=1
                    b-=1
                    ans.append("C")
                
        elif s=="AC":
                if c>a:
                    c-=1
                    a+=1
                    ans.append("A")
                else:
                    c+=1
                    a-=1
                    ans.append("C")

        if min(a,min(b,c))<0:
            print('No')
            exit()
    else:
        print("Yes")
        for i in ans:
            print(i)

if __name__=="__main__":
    main()

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 a*b==1 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 a*c == 1 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 b*c == 1 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:
n, a, b, c = map(int, input().split())
dic = {}
dic['A'] = a
dic['B'] = b
dic['C'] = c
ok = True
ans = ''
while n > 0:
  n -= 1
  s = str(input())
  if dic[s[0]] > dic[s[1]]:
    dic[s[0]] -= 1
    dic[s[1]] += 1
    ans += s[1]
    ans += '\n'
  else:
    dic[s[1]] -= 1
    dic[s[0]] += 1
    ans += s[0]
    ans += '\n'
  if dic[s[0]] < 0 or dic[s[1]] < 0:
    ok = False
if ok == True:
  print('Yes')
  print(ans)
else:
  print('No')

Submission status: Wrong Answer

Solution code:
import sys
def input() : return sys.stdin.readline().strip()

def main():
    N, A, B, C = map(int, input().split())
    s = tuple(input() for _ in range(N))
    d = {}
    d["A"] = A
    d["B"] = B
    d["C"] = C
    ans = []
    for i in range(N-1):
        l1, r1 = s[i]
        l2, r2 = s[i+1]

        if l1 == l2 and r1 == r2:
            temp = [d[l1], d[r1]]
            max_val = max(temp)
            max_index = temp.index(max_val)
            
            if max_index == 0:
                ans.append(r1)
                d[l1] -= 1
                d[r1] += 1
            else:
                ans.append(l1)
                d[l1] += 1
                d[r1] -= 1
        else:
            for c in [l1, r1]:
                if [l1, r1, l2, r2].count(c) == 1:
                    d[c] -= 1
                else:
                    ans.append(c)
                    d[c] += 1

    l1, r1 = s[N-1]
    temp = [d[l1], d[r1]]
    max_val = max(temp)
    max_index = temp.index(max_val)
            
    if max_index == 0:
        ans.append(r1)
        d[l1] -= 1
        d[r1] += 1
    else:
        ans.append(l1)
        d[l1] += 1
        d[r1] -= 1

    if any([d[c] < 0 for c in "ABC"]):
            print("No")
    else:
        print("Yes")
        print(*ans, sep="\n")

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
n,a,b,c=map(int,input().split())
s=[]
for i in range(n):
    sub=input().rstrip()
    s.append(sub)
if a+b+c==0:
    print('No')
    sys.exit()
if a+b+c==1:
    com=[]
    for i in s:
        if i=='AB':
            if a==0 and b==0:
                print('No')
                sys.exit()
            else:
                if a>b:
                    com.append('B')
                else:
                    com.append('A')
                a,b=b,a
        elif i=='AC':
            if a==0 and c==0:
                print('No')
                sys.exit()
            else:
                if a>c:
                    com.append('C')
                else:
                    com.append('A')
                a,c=c,a
        elif i=='BC':
            if b==0 and c==0:
                print('No')
                sys.exit()
            else:
                if b>c:
                    com.append('C')
                else:
                    com.append('B')
                b,c=c,b
    for i in com:
        print(com)
if (s[0]=='AB' and a**2+b**2==0) or (s[0]=='AC' and a**2+c**2==0) or (s[0]=='BC' and c**2+b**2==0):
    print('No')
else:
    print('Yes')
    for i in range(n):
        check=True
        if s[i]=='AB':
            at=a+1
            bt=b-1
            ct=c
            if bt<0:
                check=False
            if i<n-1:
                if (s[i+1]=='AB' and at**2+bt**2==0) or (s[i+1]=='AC' and at**2+ct**2==0) or (s[i+1]=='BC' and ct**2+bt**2==0):
                    check=False
            if check:
                a,b,c=at,bt,ct
                print('A')
            else:
                a-=1
                b+=1
                print('B')
        elif s[i]=='AC':
            at=a+1
            bt=b
            ct=c-1
            if ct<0:
                check=False
            if i<n-1:
                if (s[i+1]=='AB' and at**2+bt**2==0) or (s[i+1]=='AC' and at**2+ct**2==0) or (s[i+1]=='BC' and ct**2+bt**2==0):
                    check=False
            if check:
                a,b,c=at,bt,ct
                print('A')
            else:
                a-=1
                c+=1
                print('C')
        elif s[i]=='BC':
            at=a
            bt=b+1
            ct=c-1
            if ct<0:
                check=False
            if i<n-1:
                if (s[i+1]=='AB' and at**2+bt**2==0) or (s[i+1]=='AC' and at**2+ct**2==0) or (s[i+1]=='BC' and ct**2+bt**2==0):
                    check=False
            if check:
                a,b,c=at,bt,ct
                print('B')
            else:
                b-=1
                c+=1
                print('C')

Submission status: Wrong Answer

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

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

DP = [set() for i in range(n+1)]
DP[0].add((a,b,c))
for i in range(n):
    for x,y,z in DP[i]:
        if s[i] == "AB":
            nz = z

            nx = x+1
            ny = y-1
            if ny >= 0:
                DP[i+1].add((nx,ny,nz))
            nx = x-1
            ny = y+1
            if nx >= 0:
                DP[i+1].add((nx,ny,nz))
        elif s[i] == "BC":
            nx = x

            ny = y+1
            nz = z-1
            if nz >= 0:
                DP[i+1].add((nx,ny,nz))
            ny = y-1
            nz = z+1
            if ny >= 0:
                DP[i+1].add((nx,ny,nz))
        else:
            ny = y

            nz = z+1
            nx = x-1
            if nx>=0:
                DP[i+1].add((nx,ny,nz))
            nz = z-1
            nx = x+1
            if nz >=0:
                DP[i+1].add((nx,ny,nz))

if len(DP[-1]) == 0:
    print("No")
    exit()
ans = []
tmp = list(DP[-1])[0]
for i in range(n):
    x,y,z = tmp
    j = n-1-i
    if s[j] == "AB":
        nz = z

        nx = x+1
        ny = y-1
        if (nx,ny,nz) in DP[j]:
            tmp = (nx,ny,nz)
            ans.append("B")
            continue
        nx = x-1
        ny = y+1
        if (nx,ny,nz) in DP[j]:
            tmp = (nx,ny,nz)
            ans.append("A")
            continue
    elif s[j] == "BC":
        nx = x

        ny = y+1
        nz = z-1
        if (nx,ny,nz) in DP[j]:
            tmp = (nx,ny,nz)
            ans.append("C")
            continue
        ny = y-1
        nz = z+1
        if (nx,ny,nz) in DP[j]:
            tmp = (nx,ny,nz)
            ans.append("B")
            continue
    else:
        ny = y

        nz = z+1
        nx = x-1
        if (nx,ny,nz) in DP[j]:
            tmp = (nx,ny,nz)
            ans.append("A")
            continue
        nz = z-1
        nx = x+1
        if (nx,ny,nz) in DP[j]:
            tmp = (nx,ny,nz)
            ans.append("C")
            continue

ans = ans[::-1]
print(*ans, sep="\n")

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:
			string = s[i+1].strip(s[i+1].strip(s[i]))
			d[string] += 1
			d[s[i].strip(string)] -= 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, *L = map(int, input().split())
def f(s):
    if s == "AB":
        u, v, w = 0, 1, 2
    elif s == "BC":
        u, v, w = 1, 2, 0
    else:
        u, v, w = 0, 2, 1
    return u, v, w
S = [input() for _ in range(n)]
A = []
ab, ac, bc = L[0]+L[1], L[0]+L[2], L[1]+L[2]
K = [ab, ac, bc]
T = ["AB", "AC", "BC"]
for i, s in enumerate(S):
    idx = T.index(s)
    if idx == 0:
        u, v = 1, 2
    elif idx == 1:
        u, v = 0, 2
    else:
        u, v = 0, 1
    if K[idx] <= 0 or K[u] + K[v] <= 0:
        print("No")
        exit()
    if K[u] < K[v]:
        K[u] += 1
        K[v] -= 1
        for t in "ABC":
            if t in T[u] and t in T[idx]:
                A.append(t)
                break
    elif K[v] < K[u]:
        K[v] += 1
        K[u] -= 1
        for t in "ABC":
            if t in T[v] and t in T[idx]:
                A.append(t)
                break
    else:
        if i == n-1:
            break
        ns = S[i+1]
        ni = T.index(ns)
        if ni == u:
            K[u] += 1
            K[v] -= 1
            for t in "ABC":
                if t in T[u] and t in T[idx]:
                    A.append(t)
                    break
        else:
            K[v] += 1
            K[u] -= 1
            for t in "ABC":
                if t in T[v] and t in T[idx]:
                    A.append(t)
                    break
print("Yes")
print("\n".join(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[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 i < N-1:
            if S[i+1]==x:
                ans.append(SS[x])
                ABC[y] -= 1
                ABC[x] += 1
            else:
                ans.append(SS[y])
                ABC[y] += 1
                ABC[x] -= 1
        else:
            ans.append(SS[y])
            ABC[y] += 1
            ABC[x] -= 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:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def main():
    N, A, B, C = map(int, readline().split())
    s = read().decode().split()
    s.append('AB')
    ans = []
    for i in range(N):
        if s[i] == 'AB':
            if A == 0 and B == 0:
                print('No')
                break
            else:
                if A < B:
                    A += 1
                    B -= 1
                    ans.append('A')
                elif A > B:
                    A -= 1
                    B += 1
                    ans.append('B')
                else:
                    if s[i+1] == 'AB' or s[i+1] == 'AC':
                        A += 1
                        B -= 1
                        ans.append('A')
                    else:
                        A -= 1
                        B += 1
                        ans.append('B')
        if s[i] == 'AC':
            if A == 0 and C == 0:
                print('No')
                break
            else:
                if A < C:
                    A += 1
                    C -= 1
                    ans.append('A')
                elif A > C:
                    A -= 1
                    C += 1
                    ans.append('C')
                else:
                    if s[i + 1] == 'AB' or s[i + 1] == 'AC':
                        A += 1
                        B -= 1
                        ans.append('A')
                    else:
                        A -= 1
                        C += 1
                        ans.append('C')
        if s[i] == 'BC':
            if C == 0 and B == 0:
                print('No')
                break
            else:
                if C < B:
                    C += 1
                    B -= 1
                    ans.append('C')
                elif C > B:
                    C -= 1
                    B += 1
                    ans.append('B')
                else:
                    if s[i + 1] == 'AB' or s[i + 1] == 'BC':
                        B += 1
                        C -= 1
                        ans.append('B')
                    else:
                        B -= 1
                        C += 1
                        ans.append('C')
    else:
        print('Yes')
        print('\n'.join(ans))
    return

if __name__ == '__main__':
    main()

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 i < N-1:
            if S[i+1]==x:
                ans.append(SS[x])
                ABC[y] -= 1
                ABC[x] += 1
            else:
                ans.append(SS[y])
                ABC[y] += 1
                ABC[x] -= 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:
import sys
sys.setrecursionlimit(20000000)

N, A, B, C = map(int, input().split())
dic = {
    "A": A,
    "B": B,
    "C": C
}
ans = [0] * N
def part_solve(dic, depth):
    while depth < N:
        key1, key2 = inputs[depth]
        if dic[key1] < dic[key2]:
            dic[key1] += 1
            dic[key2] -= 1
            ans[depth] = key1
        elif dic[key1] > dic[key2]:
            dic[key2] += 1
            dic[key1] -= 1
            ans[depth] = key2
        else:
            if not dic[key1]:
                return False
            else:
                key3 = "ABC".replace(key1, "").replace(key2, "")
                return part_solve({
                    key1: dic[key1] + 1,
                    key2: dic[key2] - 1,
                    key3: dic[key3]
                }, depth) or part_solve({
                    key1: dic[key1] - 1,
                    key2: dic[key2] + 1,
                    key3: dic[key3]
                }, depth)
        depth += 1
    return True

inputs = tuple(tuple(input()) for _ in range(N))
if part_solve(dic, 0):
    print("Yes")
    print(*ans, sep="\n")
else:
    print("No")