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 examA():
    S = SI()
    if S=="ARC":
        ans = "ABC"
    else:
        ans = "ARC"
    print(ans)
    return

def examB():
    N, K = LI()
    snuke = set(range(1,N+1))
    for _ in range(K):
        d = I()
        A = LI()
        for a in A:
            snuke.discard(a)
    ans = len(snuke)
    print(ans)
    return

def examC():
    N, M = LI()
    H = LI()
    good = set(range(1,N+1))
    for _ in range(M):
        a, b = LI()
        if H[a-1]>=H[b-1]:
            good.discard(b)
        if H[b-1]>=H[a-1]:
            good.discard(a)
    ans = len(good)
    print(ans)
    return

def examD():
    X = I()
    #print(1000**5-999**5)
    for a in range(-1000,1000):
        for b in range(-1000,1000):
            if a**5-b**5==X:
                print(a,b)
                return
    return

def examE():
    N = I()
    A = LI()
    B = [0]*N
    D = defaultdict(int)
    for i in range(N):
        B[i] = A[i]-i
        c = A[i]+i
        D[c] += 1
    #print(B)
    #print(D)
    ans = 0
    for b in B:
        ans += D[-b]
    print(ans)
    return

def examF():
    def Run_Length_Encoding(s):
        tmp, count, res = s[0], 1, []
        for i in range(1, len(s)):
            if tmp == s[i]:
                count += 1
            else:
                res.append((count, tmp))
                tmp = s[i]
                count = 1
        res.append((count, tmp))
        return res

    N, A, B, C = LI()
    S = [SI()for _ in range(N)]
    SS = Run_Length_Encoding(S)
    #print(SS)
    n = len(SS)
    ans = [""]*(N)
    def judge(a,b):
        if a<0 or b<0:
            return False
        return True
    def dfs(i,num,A,B,C):
        if i==n:
            if A>=0 and B>=0 and C>=0:
                print("Yes")
                for v in ans:
                    print(v)
                exit()
            return
        cnt, s = SS[i]
        if s=="AB":
            if A==0 and B==0:
                return
            for j in range(cnt//2 -1):
                if A==0:
                    ans[num] = "A"
                    num += 1
                    ans[num] = "B"
                else:
                    ans[num] = "B"
                    num += 1
                    ans[num] = "A"
            if cnt>=2:
                A += 1; B -= 1
                if judge(A, B):
                    ans[num] = "A"
                    dfs(i + 1, num + 1, A, B, C)
                A -= 2; B += 2
                if judge(A, B):
                    ans[num] = "B"
                    dfs(i + 1, num + 1, A, B, C)
                A += 1;
                B -= 2
            if cnt%2==0:
                dfs(i+1,num+1,A,B,C)
                return
            A += 1; B -= 1
            if judge(A,B):
                ans[num] = "A"
                dfs(i+1,num+1,A,B,C)
            A -= 2; B += 2
            if judge(A,B):
                ans[num] = "B"
                dfs(i+1,num+1,A,B,C)
            A += 1; B -= 2
        elif s=="BC":
            if B==0 and C==0:
                return
            for j in range(cnt//2 -1):
                if B==0:
                    ans[num] = "B"
                    num += 1
                    ans[num] = "C"
                else:
                    ans[num] = "C"
                    num += 1
                    ans[num] = "B"
            if cnt%2==0:

                dfs(i+1,num+1,A,B,C)
                return
            B += 1; C -= 1
            if judge(B,C):
                ans[num] = "B"
                dfs(i+1,num+1,A,B,C)
            B -= 2; C += 2
            if judge(B,C):
                ans[num] = "C"
                dfs(i+1,num+1,A,B,C)
            B += 1; C -= 2
        else:
            if A==0 and C==0:
                return
            for j in range(cnt//2 -1):
                if A==0:
                    ans[num] = "A"
                    num += 1
                    ans[num] = "C"
                else:
                    ans[num] = "C"
                    num += 1
                    ans[num] = "A"
            if cnt%2==0:
                dfs(i+1,num+1,A,B,C)
                return
            A += 1; C -= 1
            if judge(A, C):
                ans[num] = "A"
                dfs(i + 1,num+1,A,B,C)
            A -= 2; C += 2
            if judge(A, C):
                ans[num] = "C"
                dfs(i + 1,num+1,A,B,C)
            A += 1; C -= 2
        return
    dfs(0,0,A,B,C)
    print("No")
    return


def examF2():
    N, A, B, C = LI()
    S = [SI() for _ in range(N)]
    # print(SS)
    ans = [""] * N

    def judge(a, b):
        if a < 0 or b < 0:
            return False
        return True

    def dfs(i, A, B, C):
        if i == N:
            if A >= 0 and B >= 0 and C >= 0:
                print("Yes")
                for v in ans:
                    print(v)
                exit()
            return
        s = S[i]
        if s == "AB":
            if A == 0 and B == 0:
                return
            A += 1;
            B -= 1
            if judge(A, B):
                ans[i] = "A"
                dfs(i + 1, A, B, C)
            A -= 2;
            B += 2
            if judge(A, B):
                ans[i] = "B"
                dfs(i + 1, A, B, C)
            A += 1;
            B -= 1
        elif s == "BC":
            if B == 0 and C == 0:
                return
            B += 1;
            C -= 1
            if judge(B, C):
                ans[i] = "B"
                dfs(i + 1, A, B, C)
            B -= 2;
            C += 2
            if judge(B, C):
                ans[i] = "C"
                dfs(i + 1,A, B, C)
            B += 1;
            C -= 1
        else:
            if A == 0 and C == 0:
                return
            A += 1;
            C -= 1
            if judge(A, C):
                ans[i] = "A"
                dfs(i + 1, A, B, C)
            A -= 2;
            C += 2
            if judge(A, C):
                ans[i] = "C"
                dfs(i + 1, A, B, C)
            A += 1;
            C -= 1
        return

    dfs(0, A, B, C)
    print("No")
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(readline())
def LI(): return list(map(int,readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examF2()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline

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

    ans = []
    for i, op in enumerate(s):
        # print(op)
        if op == 'AB':
            if A == B == 0:
                print('No')
                return

            if A > B:
                A -= 1
                B += 1
                ans.append('B')
            else:
                if A == B and i < N - 1:
                    if s[i + 1] == 'AC' and A == 1:
                        A += 1
                        B -= 1
                        ans.append('A')
                        continue
                    elif s[i + 1] == 'BC' and B == 1:
                        A -= 1
                        B += 1
                        ans.append('B')
                        continue
                A += 1
                B -= 1
                ans.append('A')
        elif op == 'AC':
            if A == C == 0:
                print('No')
                return

            if A > C:
                A -= 1
                C += 1
                ans.append('C')
            else:
                if A == C and i < N - 1:
                    if s[i + 1] == 'AB' and A == 1:
                        A += 1
                        C -= 1
                        ans.append('A')
                        continue
                    elif s[i + 1] == 'BC' and C == 1:
                        A -= 1
                        C += 1
                        ans.append('C')
                        continue
                A += 1
                C -= 1
                ans.append('A')
        else:
            if B == C == 0:
                print('No')
                return

            if B > C:
                B -= 1
                C += 1
                ans.append('C')
            else:
                if B == C and i < N - 1:
                    if s[i + 1] == 'AB' and B == 1:
                        B += 1
                        C -= 1
                        ans.append('B')
                        continue
                    elif s[i + 1] == 'AC' and C == 1:
                        B -= 1
                        C += 1
                        ans.append('C')
                        continue
                B += 1
                C -= 1
                ans.append('B')
        # print(A, B, C)

    print('Yes')
    print(*ans, sep='\n')


if __name__ == '__main__':
    main()

# import sys
# input = sys.stdin.readline
# 
# sys.setrecursionlimit(10 ** 7)
# 
# (int(x)-1 for x in input().split())
# rstrip()
#
# def binary_search(*, ok, ng, func):
#     while abs(ok - ng) > 1:
#         mid = (ok + ng) // 2
#         if func(mid):
#             ok = mid
#         else:
#             ng = mid
#     return ok

Submission status: Accepted

Solution code:
import random
from collections import deque
import sys
sys.setrecursionlimit(10**7)

def input():
    return sys.stdin.readline().strip()

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

n,a,b,c = get_nums_l()
d = {
    "A": a,
    "B": b,
    "C": c
}
S = []
for _ in range(n):
    S.append(input())

def dfs(i, x, history):

    if d[S[i][1-x]] == 0:
        return False

    d[S[i][x]] += 1
    d[S[i][1-x]] -= 1

    history.append(S[i][x])

    if i == n-1:
        return True

    nex = random.randint(0, 1)

    ok = dfs(i+1, nex, history)
    if ok:
        return ok

    ok = dfs(i+1, 1-nex, history)

    if ok:
        return ok

    # バックトラック
    history.pop()
    d[S[i][x]] -= 1
    d[S[i][1-x]] += 1

    return False

history = deque()
ok = dfs(0, 0, history) or dfs(0, 1, history)

print("Yes" if ok else "No")
if ok:
    for s in history:
        print(s)

Submission status: Accepted

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

n, a, b, c = [int(x) for x in input().split()]
score = {}
score["A"] = a
score["B"] = b
score["C"] = c     
S = [input().rstrip() for _ in range(n)]
ans = []

for i in range(n - 1):
    s = S[i]
    next_s = S[i + 1]
    s1 = s[0]
    s2 = s[1]
    if score[s1] == 0 and score[s2] == 0:
        print("No")
        sys.exit()
    elif score[s1] == 0:
        ans.append(s1)
        score[s1] += 1
        score[s2] -= 1
    elif score[s2] == 0:
        ans.append(s2)
        score[s1] -= 1
        score[s2] += 1
    elif score[s1] == 1 and score[s2] == 1:
        if next_s == s:
            ans.append(s1)
            score[s1] += 1
            score[s2] -= 1
        elif s1 in next_s:
            ans.append(s1)
            score[s1] += 1
            score[s2] -= 1
        else:
            ans.append(s2)
            score[s2] += 1
            score[s1] -= 1
    else:
        if score[s1] >= score[s2]:
            ans.append(s2)
            score[s2] += 1
            score[s1] -= 1
        else:
            ans.append(s1)
            score[s1] += 1
            score[s2] -= 1
s = S[-1]
s1 = s[0]
s2 = s[1]

if score[s1] == 0 and score[s2] == 0:
        print("No")
        sys.exit()
elif score[s1] == 0:
    ans.append(s1)
    score[s1] += 1
    score[s2] -= 1
elif score[s2] == 0:
    ans.append(s2)
    score[s1] -= 1
    score[s2] += 1
else:
    ans.append(s1)
print("Yes")
for i in ans:
    print(i)

Submission status: Accepted

Solution code:
import sys
from math import log2,floor,ceil,sqrt
# import bisect
# from collections import deque

Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()
 
def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10 ** 18
MOD = 10**9+7

n,a,b,c = Ri()
ans  = []
flag = True
q = []
for i in range(n):
    temp = ri()
    q.append(temp)

ac = [INF]*n
ab = [INF]*n
bc = [INF]*n
nac = INF
nab = INF
nbc = INF
for i in range(n-1,-1,-1):
    ac[i] = nac
    ab[i] = nab
    bc[i] = nbc
    if q[i] == 'AC':
        nac = i
    elif q[i] == 'AB':
        nab = i
    else:
        nbc = i

for i in range(n):
    # temp = ri()
    temp = q[i]
    if temp == "AB":
        if a==0 and b==0:
            flag = False
        else:
            if a<b:
                # if a== 0 and b==1:

                a+=1
                b-=1
                ans.append("A")
            else:
                if a==1 and b==1 and c==0:
                    if ac[i] < bc[i]:
                        a+=1
                        b-=1
                        ans.append('A')
                    else:
                        b+=1
                        a-=1
                        ans.append('B')
                else:
                    a-=1
                    b+=1
                    ans.append("B")
    elif temp == "BC":
        # print(a,b,c)
        if c==0 and b==0:
            flag = False
        else:
            if c<b:
                # print("faf")
                c+=1
                b-=1
                ans.append("C")
                # print(ans)
            else:
                if c==1 and b==1 and a==0:
                    if ac[i] < ab[i]:
                        c+=1
                        b-=1
                        ans.append('C')
                    else:
                        b+=1
                        c-=1
                        ans.append('B')
                else:
                    c-=1
                    b+=1
                    ans.append("B")
    else:
        if a==0 and c==0:
            flag = False
        else:
            if a<c:
                a+=1
                c-=1
                ans.append("A")
            else:
                if a==1 and c==1 and b==0:
                    if ab[i] < bc[i]:
                        a+=1
                        c-=1
                        ans.append('A')
                    else:
                        c+=1
                        a-=1
                        ans.append('C')
                else:
                    a-=1
                    c+=1
                    ans.append("C")
if not flag : 
    No()
else:
    Yes()
    for i in range(n):
        print(ans[i])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
n,a,b,c=map(int,input().split())
s=[input() for i in range(n)]
ans=[]
for i in range(n-1):
  if s[i]=='AB':
    if a==0 and b==0:
      print('No')
      sys.exit()
    if a>b or (a==b and s[i+1]=='BC'):
      a-=1
      b+=1
      ans.append('B')
    else:
      a+=1
      b-=1
      ans.append('A')
  if s[i]=='BC':
    if b==0 and c==0:
      print('No')
      sys.exit()
    if b>c or (b==c and s[i+1]=='AC'):
      b-=1
      c+=1
      ans.append('C')
    else:
      b+=1
      c-=1
      ans.append('B')
  if s[i]=='AC':
    if a==0 and c==0:
      print('No')
      sys.exit()
    if a>c or (a==c and s[i+1]=='BC'):
      a-=1
      c+=1
      ans.append('C')
    else:
      a+=1
      c-=1
      ans.append('A')
for i in range(n-1,n):
  if s[i]=='AB':
    if a==0 and b==0:
      print('No')
      sys.exit()
    if a>=b:
      a-=1
      b+=1
      ans.append('B')
    else:
      a+=1
      b-=1
      ans.append('A')
  if s[i]=='BC':
    if b==0 and c==0:
      print('No')
      sys.exit()
    if b>=c:
      b-=1
      c+=1
      ans.append('C')
    else:
      b+=1
      c-=1
      ans.append('B')
  if s[i]=='AC':
    if a==0 and c==0:
      print('No')
      sys.exit()
    if a>=c:
      a-=1
      c+=1
      ans.append('C')
    else:
      a+=1
      c-=1
      ans.append('A')
print('Yes')
for i in range(n):
  print(ans[i])

Submission status: Accepted

Solution code:
d = {'A':0, 'B':1, 'C':2}
ABC = 'ABC'

def main():
    N, A, B, C = map(int, input().split())
    abc_nums = [A, B, C]
    S = []
    for i in range(N):
        s = input()
        flgs = []
        for j in range(2):
            flgs.append(d[s[j]])
        S.append(flgs)

    ans = []
    for i in range(N):
        if abc_nums[S[i][0]] == abc_nums[S[i][1]] == 0:
            print('No')
            return
        elif i == N-1:  # 最後だったらどちらでも
            if abc_nums[S[i][0]] <= abc_nums[S[i][1]]:
                abc_nums[S[i][0]] += 1
                ans.append(ABC[S[i][0]])
                abc_nums[S[i][1]] -= 1
            else:
                abc_nums[S[i][0]] -= 1
                abc_nums[S[i][1]] += 1
                ans.append(ABC[S[i][1]])
        elif abc_nums[S[i][0]] == abc_nums[S[i][1]]:    # 同じだったら次に出てくる方
            if S[i][0] == S[i+1][0] or S[i][0] == S[i+1][1]:
                abc_nums[S[i][0]] += 1
                ans.append(ABC[S[i][0]])
                abc_nums[S[i][1]] -= 1
            else:
                abc_nums[S[i][0]] -= 1
                abc_nums[S[i][1]] += 1
                ans.append(ABC[S[i][1]])
        elif abc_nums[S[i][0]] <= abc_nums[S[i][1]]:
            abc_nums[S[i][0]] += 1
            ans.append(ABC[S[i][0]])
            abc_nums[S[i][1]] -= 1
        else:
            abc_nums[S[i][0]] -= 1
            abc_nums[S[i][1]] += 1
            ans.append(ABC[S[i][1]])
    print('Yes')
    for i in range(N):
        print(ans[i])
    return
main()

Submission status: Accepted

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

    S=[input() for i in range(N)]
    ans =[]




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

        elif s =="BC":
            if C ==B ==0:
                print("No")
                return
            if C == B ==1 and A == 0 and i < N-1:
                if S[i+1] =="AC":
                    C+=1
                    B-=1
                    ans.append("C")
                else:
                    C-=1
                    B+=1
                    ans.append("B")
            elif C>B:
                C-=1
                B+=1
                ans.append("B")
            else:
                C+=1
                B-=1
                ans.append("C")

        elif s =="AC":
            if C ==A ==0:
                print("No")
                return
            if C == A ==1 and B == 0 and i < N-1:
                if S[i+1] =="AB":
                    A+=1
                    C-=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")


    print("Yes")
    print("\n".join(ans))







if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
n, a, b, c = map(int,input().split())
slist=[]
for i in range(n):
    slist.append(input())

if a==0 and b==0 and c==0:
    print("No")
elif a==0 and b==0 and slist[0]=="AB":
    print("No")
elif a==0 and c==0 and slist[0]=="AC":
    print("No")
elif b==0 and c==0 and slist[0]=="BC":
    print("No")
else:
    cup=[a, b, c]
    answer=""
    ng=0
    for i in range(n):
        now=slist[i]
        if now=="AB":
            mode1=2
        elif now=="AC":
            mode1=1
        else:
            mode1=0
        if i==n-1:
            mode2=3
        else:
            if slist[i+1]=="AB":
                mode2=2
            elif slist[i+1]=="AC":
                mode2=1
            else:
                mode2=0
        
        if mode2==3:
            if now=="AB":
                if cup[0]==0:
                    if cup[1]==0:
                        ng=1
                    else:
                        answer+="A"
                else:
                    answer+="B"
            elif now=="BC":
                if cup[1]==0:
                    if cup[2]==0:
                        ng=1
                    else:
                        answer+="B"
                else:
                    answer+="C"
            else:
                if cup[0]==0:
                    if cup[2]==0:
                        ng=1
                    else:
                        answer+="A"
                else:
                    answer+="C"
        
        elif mode1==mode2:
            if now=="AB":
                if cup[0]==0:
                    if cup[1]==0:
                        ng=1
                    else:
                        answer+="A"
                        cup[0]+=1
                        cup[1]-=1
                else:
                    answer+="B"
                    cup[1]+=1
                    cup[0]-=1
            elif now=="BC":
                if cup[1]==0:
                    if cup[2]==0:
                        ng=1
                    else:
                        answer+="B"
                        cup[1]+=1
                        cup[2]-=1
                else:
                    answer+="C"
                    cup[2]+=1
                    cup[1]-=1
            else:
                if cup[0]==0:
                    if cup[2]==0:
                        ng=1
                    else:
                        answer+="A"
                        cup[0]+=1
                        cup[2]-=1
                else:
                    answer+="C"
                    cup[2]+=1
                    cup[0]-=1
        
        else:
            if now=="AB" and cup[0]==0 and cup[1]==0:
                ng=1
            elif now=="BC" and cup[2]==0 and cup[1]==0:
                ng=1
            elif now=="AC" and cup[0]==0 and cup[2]==0:
                ng=1
            else:
                if now=="AB":
                    if cup[0]==0:
                        answer+="A"
                        cup[0]+=1
                        cup[1]-=1
                    elif cup[1]==0:
                        answer+="B"
                        cup[1]+=1
                        cup[0]-=1
                    else:
                        if slist[i+1]=="AC":
                            cup[0]+=1
                            cup[1]-=1
                            answer+="A"
                        else:
                            cup[1]+=1
                            cup[0]-=1
                            answer+="B"
                
                elif now=="BC":
                    if cup[1]==0:
                        answer+="B"
                        cup[1]+=1
                        cup[2]-=1
                    elif cup[2]==0:
                        answer+="C"
                        cup[2]+=1
                        cup[1]-=1
                    else:
                        if slist[i+1]=="AC":
                            cup[2]+=1
                            cup[1]-=1
                            answer+="C"
                        else:
                            cup[1]+=1
                            cup[2]-=1
                            answer+="B"
                
                else:
                    if cup[0]==0:
                        answer+="A"
                        cup[0]+=1
                        cup[2]-=1
                    elif cup[2]==0:
                        answer+="C"
                        cup[2]+=1
                        cup[0]-=1
                    else:
                        if slist[i+1]=="AB":
                            cup[0]+=1
                            cup[2]-=1
                            answer+="A"
                        else:
                            cup[2]+=1
                            cup[0]-=1
                            answer+="C"
                            
    if ng==1:
        print("No")
    else:
        print("Yes")
        for i in range(len(answer)):
            print(answer[i])

Submission status: Accepted

Solution code:
Arr=list(map(int,input().split()))
L=[]
for i in range(Arr[0]):
    S=input()
    if S=="AB":
        L.append([1,2])
    elif S=="BC":
        L.append([2,3])
    else:
        L.append([1,3])
D={}
D[1]="A"
D[2]="B"
D[3]="C"

if Arr[L[0][0]]+Arr[L[0][1]]==0:
    print("No")
else:
    ANS=["Yes"]
    for i in range(len(L)):
        if Arr[L[i][0]]==Arr[L[i][1]] and Arr[L[i][0]]==1:
            if i!=len(L)-1 and L[i]!=L[i+1]:
                if L[i][0] in L[i+1]:
                    ANS.append(D[L[i][0]])
                    Arr[L[i][0]]+=1
                    Arr[L[i][1]]-=1
                else:
                    ANS.append(D[L[i][1]])
                    Arr[L[i][1]]+=1
                    Arr[L[i][0]]-=1
            else:
                ANS.append(D[L[i][0]])
                Arr[L[i][0]]+=1
                Arr[L[i][1]]-=1
        elif Arr[L[i][0]]+Arr[L[i][1]]==0:
            print("No")
            exit()
        else:
            if Arr[L[i][0]]>=Arr[L[i][1]]:
                ANS.append(D[L[i][1]])
                Arr[L[i][1]]+=1
                Arr[L[i][0]]-=1
            else:
                ANS.append(D[L[i][0]])
                Arr[L[i][0]]+=1
                Arr[L[i][1]]-=1
    for j in range(len(ANS)):
        print(ANS[j])