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: Accepted

Solution code:
def main():
    N, A, B, C = map(int, input().split())
    S = [input() for _ in range(N)]
    res = []
    if A + B + C == 2:
        for i in range(N):
            s = S[i]
            if s == "AB":
                if A == 0 and B == 0:
                    print("No")
                    return 1
                if A > B:
                    A -= 1
                    B += 1
                    res.append("B")
                elif A < B:
                    A += 1
                    B -= 1
                    res.append("A")
                else:
                    if i == N - 1:
                        res.append("A")
                        break
                    elif S[i + 1] == "BC":
                        A -= 1
                        B += 1
                        res.append("B")
                    else:
                        A += 1
                        B -= 1
                        res.append("A")
            if s == "AC":
                if A == 0 and C == 0:
                    print("No")
                    return 1
                if A > C:
                    A -= 1
                    C += 1
                    res.append("C")
                elif A < C:
                    A += 1
                    C -= 1
                    res.append("A")
                else:
                    if i == N - 1:
                        res.append("A")
                        break
                    elif S[i + 1] == "BC":
                        A -= 1
                        C += 1
                        res.append("C")
                    else:
                        A += 1
                        C -= 1
                        res.append("A")
            if s == "BC":
                if B == 0 and C == 0:
                    print("No")
                    return 1
                if B > C:
                    B -= 1
                    C += 1
                    res.append("C")
                elif B < C:
                    B += 1
                    C -= 1
                    res.append("B")
                else:
                    if i == N - 1:
                        res.append("B")
                        break
                    elif S[i + 1] == "AC":
                        B -= 1
                        C += 1
                        res.append("C")
                    else:
                        B += 1
                        C -= 1
                        res.append("B")
        print("Yes")
        print("\n".join(map(str, res)))
        return 0
    if A == 0 and B == 0:
        if S[0] == "AB":
            print("No")
            return 1
    if A == 0 and C == 0:
        if S[0] == "AC":
            print("No")
            return 1
    if C == 0 and B == 0:
        if S[0] == "BC":
            print("No")
            return 1

    for s in S:
        if s == "AB":
            if A == 0 and B == 0:
                print("No")
                return 1
            if A >= B:
                A -= 1
                B += 1
                res.append("B")
            else:
                A += 1
                B -= 1
                res.append("A")
        elif s == "AC":
            if A == 0 and C == 0:
                print("No")
                return 1
            if A >= C:
                A -= 1
                C += 1
                res.append("C")
            else:
                A += 1
                C -= 1
                res.append("A")

        elif s == "BC":
            if B == 0 and C == 0:
                print("No")
                return 1
            if B >= C:
                B -= 1
                C += 1
                res.append("C")
            else:
                B += 1
                C -= 1
                res.append("B")
        else:
            print("ERROR")
    print("Yes")
    print("\n".join(map(str, res)))
    return 0


main()

Submission status: Accepted

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

cur = dict()
cur['A'], cur['B'], cur['C'] = A, B, C

f = True
ans = []
for i, ss in enumerate(s):
    if cur[ss[0]] == 1 and cur[ss[1]] == 1 and sorted(list(cur.values())) == [0, 1, 1] and i < len(s) - 1:
        if ss[0] in s[i + 1]:
            cur[ss[0]] += 1
            cur[ss[1]] -= 1
            ans.append(ss[0])
        elif ss[1] in s[i + 1]:
            cur[ss[1]] += 1
            cur[ss[0]] -= 1
            ans.append(ss[1])

    else:
        if cur[ss[0]] >= cur[ss[1]]:
            cur[ss[0]] -= 1
            cur[ss[1]] += 1
            ans.append(ss[1])
        else:
            cur[ss[0]] += 1
            cur[ss[1]] -= 1
            ans.append(ss[0])

    if -1 in list(cur.values()):
        f = False
        break

if f:
    print('Yes')
    for a in ans:
        print(a)
else:
    print('No')

Submission status: Accepted

Solution code:
n, a, b, c = map(int, input().split())
variables = [a, b, c]
alph = ["A", "B", "C"]
s = [[] for _ in range(n)]
for i in range(n):
	u, v = map(lambda x: alph.index(x) , list(input()))
	s[i] = [u, v]
# print(s)

can_do = True
ans = []
for i in range(n):
	u, v = s[i][0], s[i][1]
	if variables[u] + variables[v] == 0:
		can_do = False
	elif variables[v] == 0:
			variables[u] -= 1
			variables[v] += 1
			ans.append(alph[v])
	elif variables[u] == 0:
			variables[v] -= 1
			variables[u] += 1
			ans.append(alph[u])
	else:
		# both are > 0
		if i == n-1:
			# do whatever you want
			variables[u] -= 1
			variables[v] += 1
			ans.append(alph[v])
		else:
			# need to think about next step
			if variables[u] > 0 and variables[v] > 0:
				next_u, next_v = s[i+1][0], s[i+1][1]
				if variables[next_u] + variables[next_v] >= 2:
					# can subsctact and whatever
					if variables[u] < variables[v]:
						u, v = v, u
					variables[u] -= 1
					variables[v] += 1
					ans.append(alph[v])
				else:
					# need to sponsor
					common = list( {u, v}.intersection({next_u, next_v}) )[0]
					# print("common", common)
					not_common = u + v - common
					variables[common] += 1
					variables[not_common] -= 1
					ans.append(alph[common])

if can_do:
	print("Yes")
	for item in ans:
		print(item)
else:
	print("No")

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    def ng():
        print("No")
        exit()

    n,a,b,c=MI()
    ss=[SI() for _ in range(n)]
    ans=[]
    for i in range(n):
        s=ss[i]
        if s=="AB":
            if (a,b)==(0,0):ng()
            elif (a,b)==(0,1):
                ans.append("A")
                a,b=a+1,b-1
            elif (a,b)==(1,0):
                ans.append("B")
                a,b=a-1,b+1
            elif (a,b,c)==(1,1,0):
                if i==n-1:
                    ans.append("A")
                else:
                    if ss[i+1]=="AC":
                        ans.append("A")
                        a,b=a+1,b-1
                    else:
                        ans.append("B")
                        a, b = a - 1, b + 1
            else:
                if a<b:
                    ans.append("A")
                    a, b = a + 1, b - 1
                else:
                    ans.append("B")
                    a, b = a - 1, b + 1
        elif s=="BC":
            if (b,c)==(0,0):ng()
            elif (b,c)==(0,1):
                ans.append("B")
                b,c=b+1,c-1
            elif (b,c)==(1,0):
                ans.append("C")
                b,c=b-1,c+1
            elif (a,b,c)==(0,1,1):
                if i==n-1:
                    ans.append("B")
                else:
                    if ss[i+1]=="AC":
                        ans.append("C")
                        b, c = b - 1, c + 1
                    else:
                        ans.append("B")
                        b, c = b + 1, c - 1
            else:
                if c<b:
                    ans.append("C")
                    b, c = b - 1, c + 1
                else:
                    ans.append("B")
                    b, c = b + 1, c - 1
        else:
            if (a,c)==(0,0):ng()
            elif (a,c)==(0,1):
                ans.append("A")
                a,c=a+1,c-1
            elif (a,c)==(1,0):
                ans.append("C")
                a,c=a-1,c+1
            elif (a,b,c)==(1,0,1):
                if i==n-1:
                    ans.append("A")
                else:
                    if ss[i+1]=="AB":
                        ans.append("A")
                        a, c = a + 1, c - 1
                    else:
                        ans.append("C")
                        a, c = a - 1, c + 1
            else:
                if a<c:
                    ans.append("A")
                    a, c = a + 1, c - 1
                else:
                    ans.append("C")
                    a, c = a - 1, c + 1
    print("Yes")
    for s in ans:
        print(s)

main()

Submission status: Accepted

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

if A+B+C!=2:
    ans=[]
    for i in range(N):
        s=S[i]
        if s=="AB":
            if A>B:
                ans.append("B")
                A-=1
                B+=1
            else:
                ans.append("A")
                A+=1
                B-=1
        elif s=="BC":
            if B>C:
                ans.append("C")
                B-=1
                C+=1
            else:
                ans.append("B")
                B+=1
                C-=1
        else:
            if A>C:
                ans.append("C")
                A-=1
                C+=1
            else:
                ans.append("A")
                A+=1
                C-=1
        if min(A,B,C)<0:
            print("No")
            exit()

    print("Yes")
    for i in range(N):
        print(ans[i])
else:
    ans=[]
    for i in range(N):
        s=S[i]
        if s=="AB":
            if A>B:
                ans.append("B")
                A-=1
                B+=1
            elif A==B and i!=N-1:
                ns=S[i+1]
                if ns=="AC":
                    ans.append("A")
                    A+=1
                    B-=1
                elif ns=="BC":
                    ans.append("B")
                    A-=1
                    B+=1
                else:
                    ans.append("A")
                    A+=1
                    B-=1
            else:
                ans.append("A")
                A+=1
                B-=1
        elif s=="BC":
            if B>C:
                ans.append("C")
                B-=1
                C+=1
            elif C==B and i!=N-1:
                ns=S[i+1]
                if ns=="AC":
                    ans.append("C")
                    C+=1
                    B-=1
                elif ns=="AB":
                    ans.append("B")
                    C-=1
                    B+=1
                else:
                    ans.append("C")
                    C+=1
                    B-=1
            else:
                ans.append("B")
                B+=1
                C-=1
        else:
            if A>C:
                ans.append("C")
                A-=1
                C+=1
            elif A==C and i!=N-1:
                ns=S[i+1]
                if ns=="BC":
                    ans.append("C")
                    C+=1
                    A-=1
                elif ns=="AB":
                    ans.append("A")
                    A+=1
                    C-=1
                else:
                    ans.append("A")
                    A+=1
                    C-=1
            else:
                ans.append("A")
                A+=1
                C-=1
        if min(A,B,C)<0:
            print("No")
            exit()
    print("Yes")
    for i in range(N):
        print(ans[i])

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N, A, B, C = map(int, input().split())
res = [""] * N
ab = []
ac = []
bc = []
def no():
  print("No")
  exit(0)
for i in range(N):
  s = input()[: -1]
  if s == "AB":
    ab.append(i)
    A -= 1
    B -= 1
    if A < 0:
      if len(ac):
        A += 2
        res[ac.pop()] = "A"
      elif len(ab):
        A += 2
        res[ab.pop()] = "A"
    if B < 0:
      if len(bc):
        B += 2
        res[bc.pop()] = "B"
      elif len(ab):
        B += 2
        res[ab.pop()] = "B"

  if s == "AC":
    ac.append(i)
    A -= 1
    C -= 1
    if A < 0:
      if len(ab):
        A += 2
        res[ab.pop()] = "A"
      elif len(ac):
        A += 2
        res[ac.pop()] = "A"
    if C < 0:
      if len(bc):
        C += 2
        res[bc.pop()] = "C"
      elif len(ac):
        C += 2
        res[ac.pop()] = "C"

  if s == "BC":
    bc.append(i)
    B -= 1
    C -= 1
    if B < 0:
      if len(ab):
        B += 2
        res[ab.pop()] = "B"
      elif len(bc):
        B += 2
        res[bc.pop()] = "B"
    if C < 0:
      if len(ac):
        C += 2
        res[ac.pop()] = "C"
      elif len(bc):
        C += 2
        res[bc.pop()] = "C"

  if min(A, B, C) < 0: no()

for x in ab: res[x] = "A"
for x in ac: res[x] = "A"
for x in bc: res[x] = "B"
print("Yes")
for r in res: print(r)

Submission status: Accepted

Solution code:
# coding: utf-8
N,A,B,C=map(int,input().split())
flg=True
M=[]
SS=[]
for i in range(N):
    SS.append(input())
    
for i in range(N):
    S=SS[i]
    if S=="AB":
        if A==0 and B==0:
            flg=False
            break
        elif A==0:
            m="A"
        elif B==0:
            m="B"
        elif A==1 and B==1:
            if i+1<=N-1:
                if SS[i+1]=="AC":
                    m="A"
                elif SS[i+1]=="BC":
                    m="B"
                else:
                    m="A"
        elif A>=B:
            m="B"
        elif B>A:
            m="A"
            
        M.append(m)
        if m=="A":
            A+=1
            B-=1
        if m=="B":
            A-=1
            B+=1
################
    elif S=="AC":
        if A==0 and C==0:
            flg=False
            break
        elif A==0:
            m="A"
        elif C==0:
            m="C"
        elif A==1 and C==1:
            if i+1<=N-1:
                if SS[i+1]=="AB":
                    m="A"
                elif SS[i+1]=="BC":
                    m="C"
                else:
                    m="A"
        elif A>=C:
            m="C"
        elif C>A:
            m="A"
            
        M.append(m)
        if m=="A":
            A+=1
            C-=1
        if m=="C":
            A-=1
            C+=1
################
    elif S=="BC":
        if B==0 and C==0:
            flg=False
            break
        elif B==0:
            m="B"
        elif C==0:
            m="C"
        elif B==1 and C==1:
            if i+1<=N-1:
                if SS[i+1]=="AB":
                    m="B"
                elif SS[i+1]=="AC":
                    m="C"
                else:
                    m="B"
        elif B>=C:
            m="C"
        elif C>B:
            m="B"
            
        M.append(m)
        if m=="B":
            B+=1
            C-=1
        if m=="C":
            B-=1
            C+=1

if flg:
    print("Yes")
    for i in range(N):
        print(M[i])
else:
    print("No")

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10**6)
buff_readline = sys.stdin.buffer.readline
readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(buff_readline())


def read_int_n():
    return list(map(int, buff_readline().split()))


def read_float():
    return float(buff_readline())


def read_float_n():
    return list(map(float, buff_readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()

def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A, B, C, S):
    D = [A, B, C]
    ans = []

    def f(s):
        t = []
        if 'A' in s:
            t.append(0)
        if 'B' in s:
            t.append(1)
        if 'C' in s:
            t.append(2)
        return t
    for i, s in enumerate(S):
        t = f(s)
        if D[t[0]] == 0 and D[t[1]] == 0:
            print('No')
            return
        else:
            if D[t[0]] == D[t[1]] == 1 and i != N-1:
                ns = S[i+1]
                if s == ns:
                    ans.append(t[1])
                    D[t[0]] -= 1
                    D[t[1]] += 1
                else:
                    n = set(t) & set(f(ns))
                    m = set(t) - n
                    n = n.pop()
                    m = m.pop()
                    ans.append(n)
                    D[n] += 1
                    D[m] -= 1


            elif D[t[0]] > D[t[1]]:
                ans.append(t[1])
                D[t[0]] -= 1
                D[t[1]] += 1
            else:
                ans.append(t[0])
                D[t[0]] += 1
                D[t[1]] -= 1

    print('Yes')
    for a in ans:
        if a == 0:
            print('A')
        elif a == 1:
            print('B')
        else:
            print('C')



def main():
    N, A, B, C = read_int_n()
    S = [read_str() for _ in range(N)]
    (slv(N, A, B, C, S))


    # N = 10**5
    # A = random.randint(1, 10**9)
    # B = random.randint(1, 10**9)
    # C = random.randint(1, 10**9)
    # S = random.choices(['AB', 'AC', 'BC'], k=N)
    # (slv(N, A, B, C, S))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,A,B,C=map(int,input().split())
S=[input() for i in range(N)]
ans=[]
X=A+B+C
if X==1:
    for i in range(N):
        if S[i]=="AB":
            if A>0:
                A-=1
                B+=1
                ans.append("B")
            elif B>0:
                A+=1
                B-=1
                ans.append("A")
            else:
                print("No")
                exit()
        elif S[i]=="BC":
            if B>0:
                B-=1
                C+=1
                ans.append("C")
            elif C>0:
                B+=1
                C-=1
                ans.append("B")
            else:
                print("No")
                exit()
        else:
            if C>0:
                C-=1
                A+=1
                ans.append("A")
            elif A>0:
                C+=1
                A-=1
                ans.append("C")
            else:
                print("No")
                exit()
    print("Yes")
    for i in range(N):
        print(ans[i])
    exit()
if X==2:
    for i in range(N):
        if S[i]=="AB":
            if A==0 and B==0:
                print("No")
                exit()
            elif A==1 and B==1:
                if i<N-1 and S[i+1]=="AC":
                    ans.append("A")
                    A+=1
                    B-=1
                else:
                    ans.append("B")
                    B+=1
                    A-=1
            elif A==0:
                ans.append("A")
                A+=1
                B-=1
            else:
                ans.append("B")
                B+=1
                A-=1
        elif S[i]=="BC":
            if B==0 and C==0:
                print("No")
                exit()
            elif B==1 and C==1:
                if i<N-1 and S[i+1]=="AB":
                    ans.append("B")
                    B+=1
                    C-=1
                else:
                    ans.append("C")
                    C+=1
                    B-=1
            elif B==0:
                ans.append("B")
                B+=1
                C-=1
            else:
                ans.append("C")
                C+=1
                B-=1
        else:
            if C==0 and A==0:
                print("No")
                exit()
            elif C==1 and A==1:
                if i<N-1 and S[i+1]=="BC":
                    ans.append("C")
                    C+=1
                    A-=1
                else:
                    ans.append("A")
                    A+=1
                    C-=1
            elif C==0:
                ans.append("C")
                C+=1
                A-=1
            else:
                ans.append("A")
                A+=1
                C-=1
    print("Yes")
    for i in range(N):
        print(ans[i])
    exit()
for i in range(N):
    if S[i]=="AB":
        if A==0 and B==0:
            print("No")
            exit()
        elif A+1==X:
            ans.append("B")
            A-=1
            B+=1
        elif B+1==X:
            ans.append("A")
            A+=1
            B-=1
        elif A==0:
            ans.append("A")
            A+=1
            B-=1
        elif B==0:
            ans.append("B")
            A-=1
            B+=1
        else:
            ans.append("A")
            A+=1
            B-=1
    elif S[i]=="BC":
        if B==0 and C==0:
            print("No")
            exit()
        elif B+1==X:
            ans.append("C")
            B-=1
            C+=1
        elif C+1==X:
            ans.append("B")
            B+=1
            C-=1
        elif B==0:
            ans.append("B")
            B+=1
            C-=1
        elif C==0:
            ans.append("C")
            B-=1
            C+=1
        else:
            ans.append("B")
            B+=1
            C-=1
    else:
        if C==0 and A==0:
            print("No")
            exit()
        elif C+1==X:
            ans.append("A")
            C-=1
            A+=1
        elif A+1==X:
            ans.append("C")
            C+=1
            A-=1
        elif C==0:
            ans.append("C")
            C+=1
            A-=1
        elif A==0:
            ans.append("A")
            C-=1
            A+=1
        else:
            ans.append("C")
            C+=1
            A-=1
print("Yes")
for i in range(N):
    print(ans[i])

Submission status: Accepted

Solution code:
n,a,b,c = map(int, input().split())
s = [input() for i in range(n)]
ans = []
if a+b+c == 2:
  s.append("AB")
  for k,i in enumerate(s[:-1]):
    NEXT = s[k+1]
    if i == "AB":
      if a > b:
        a-=1
        b+=1
        ans.append("B")
      elif a==b:
        if NEXT == "AB" or NEXT == "AC":
          a+=1
          b-=1
          ans.append("A")
        else:
          a-=1
          b+=1
          ans.append("B")
      else:
        a+=1
        b-=1
        ans.append("A")
    elif i == "AC":
      if a > c:
        a-= 1
        c+=1
        ans.append("C")
      elif a == c:
        if NEXT == "AB" or NEXT == "AC":
          a+=1
          c-=1
          ans.append("A")
        else:
          a-=1
          c+=1
          ans.append("C")
      else:
        a+=1
        c-=1
        ans.append("A")
    else:
      if b > c:
        b-=1
        c+=1
        ans.append("C")
      elif b==c:
        if NEXT == "AB" or NEXT == "BC":
          b+=1
          c-=1
          ans.append("B")
        else:
          b-=1
          c+=1
          ans.append("C")
      else:
        b+=1
        c-=1
        ans.append("B")
    if a == -1 or b == -1 or c == -1:
      print("No")
      exit()
else:
  for i in s:
    if i == "AB":
      if a > b:
        a-=1
        b+=1
        ans.append("B")
      else:
        a+=1
        b-=1
        ans.append("A")
    elif i == "AC":
      if a > c:
        a-= 1
        c+=1
        ans.append("C")
      else:
        a+=1
        c-=1
        ans.append("A")
    else:
      if b > c:
        b-=1
        c+=1
        ans.append("C")
      else:
        b+=1
        c-=1
        ans.append("B")
    if a == -1 or b == -1 or c == -1:
      print("No")
      exit()
print("Yes")
for i in ans:
  print(i)

Submission status: Accepted

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

def solve(n,a,b,c,S):
    Ans=[]
    for i,s in enumerate(S):
        if s=='AB':
            if a==0 and b==0:
                return 'No'
            if a==1 and b==1:
                if i==n-1:
                    Ans.append('A')
                else:
                    if S[i+1]=='AB':
                        a+=1; b-=1
                        Ans.append('A')
                    elif S[i+1]=='BC':
                        a-=1; b+=1
                        Ans.append('B')
                    else:
                        a+=1; b-=1
                        Ans.append('A')
            elif a>b:
                a-=1; b+=1
                Ans.append('B')
            else:
                a+=1; b-=1
                Ans.append('A')
        if s=='BC':
            if b==0 and c==0:
                return 'No'
            if b==1 and c==1:
                if i==n-1:
                    Ans.append('B')
                else:
                    if S[i+1]=='BC':
                        b+=1; c-=1
                        Ans.append('B')
                    elif S[i+1]=='AC':
                        b-=1; c+=1
                        Ans.append('C')
                    else:
                        b+=1; c-=1
                        Ans.append('B')
            elif b>c:
                b-=1; c+=1
                Ans.append('C')
            else:
                b+=1; c-=1
                Ans.append('B')
        if s=='AC':
            if c==0 and a==0:
                return 'No'
            if c==1 and a==1:
                if i==n-1:
                    Ans.append('C')
                else:
                    if S[i+1]=='AC':
                        c+=1; a-=1
                        Ans.append('C')
                    elif S[i+1]=='AB':
                        c-=1; a+=1
                        Ans.append('A')
                    else:
                        c+=1; a-=1
                        Ans.append('C')
            elif c>a:
                c-=1; a+=1
                Ans.append('A')
            else:
                c+=1; a-=1
                Ans.append('C')
    return Ans

def main():
    n,a,b,c=map(int,input().split())
    S=[input().strip() for _ in range(n)]
    Ans=solve(n,a,b,c,S)
    if Ans=='No':
        print(Ans)
    else:
        print('Yes')
        print(*Ans,sep='\n')
    
if __name__=='__main__':
    main()