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:
import sys

N, A, B, C = map(int,input().split())
Q = []
for _ in range(N):
    Q.append(input())

def check(i, A, B, C):
    if A < 0 or B < 0 or C < 0:
        return False
    if Q[i] == 'AB':
        return not (A == 0 and B == 0)
    if Q[i] == 'AC':
        return not (A == 0 and C == 0)
    return not (B == 0 and C == 0)

L = []
for i in range(N):
    if i == N-1:
        if Q[i] == 'AB':
            if A == 0 and B == 0:
                print('No')
                sys.exit()
            elif A == 0:
                L.append('A')
                A += 1
                B -= 1
                if B < 0:
                    print('No')
                    sys.exit()
            else:
                L.append('B')
                A -= 1
                B += 1
                if A < 0:
                    print('No')
                    sys.exit()
        elif Q[i] == 'AC':
            if A == 0 and C == 0:
                print('No')
                sys.exit()
            elif A == 0:
                L.append('A')
                A += 1
                C -= 1
                if C < 0:
                    print('No')
                    sys.exit()
            else:
                L.append('C')
                A -= 1
                C += 1
                if A < 0:
                    print('No')
                    sys.exit()
        else:
            if B == 0 and C == 0:
                print('No')
                sys.exit()
            elif B == 0:
                L.append('B')
                B += 1
                C -= 1
                if C < 0:
                    print('No')
                    sys.exit()
            else:
                L.append('C')
                B -= 1
                C += 1
                if B < 0:
                    print('No')
                    sys.exit()
    else:
        if not check(i, A, B, C):
            print('No')
            sys.exit()
        if Q[i] == 'AB':
            if check(i+1, A+1, B-1, C):
                L.append('A')
                A += 1
                B -= 1
                if B < 0:
                    print('No')
                    sys.exit()
            elif check(i+1, A-1, B+1, C):
                L.append('B')
                A -= 1
                B += 1
                if A < 0:
                    print('No')
                    sys.exit()
            else:
                print('No')
                sys.exit()
        elif Q[i] == 'AC':
            if check(i+1, A+1, B, C-1):
                L.append('A')
                A += 1
                C -= 1
                if C < 0:
                    print('No')
                    sys.exit()
            elif check(i+1, A-1, B, C+1):
                L.append('C')
                A -= 1
                C += 1
                if A < 0:
                    print('No')
                    sys.exit()
            else:
                print('No')
                sys.exit()
        else:
            if check(i+1, A, B+1, C-1):
                L.append('B')
                B += 1
                C -= 1
                if C < 0:
                    print('No')
                    sys.exit()
            elif check(i+1, A, B-1, C+1):
                L.append('C')
                B -= 1
                C += 1
                if B < 0:
                    print('No')
                    sys.exit()
            else:
                print('No')
                sys.exit()


print('Yes')
for i in L:
    print(i)

Submission status: Accepted

Solution code:
n,a,b,c=map(int, input().split( ))
cnts = {"A":a,"B":b,"C":c}
ans=[]
s = [input() for _ in range(n)]+[""]
def rnd(ss,t):

    if cnts[ss[0]]>cnts[ss[1]]:
        cnts[ss[0]]-=1; cnts[ss[1]]+=1
        ans.append(ss[1])
    elif cnts[ss[1]]>cnts[ss[0]]:
        cnts[ss[0]]+=1; cnts[ss[1]]-=1
        ans.append(ss[0])
    else:
        if ss[0] in s[t]:
            cnts[ss[0]]+=1;cnts[ss[1]]-=1
            ans.append(ss[0])
        else:
            cnts[ss[1]]+=1;cnts[ss[0]]-=1
            ans.append(ss[1])

    for k in cnts:
        if cnts[k]<0:
            print("No")
            exit()
for i in range(n):
    rnd(s[i],i+1)
print("Yes")
for ai in ans:
    print(ai)

Submission status: Accepted

Solution code:
from heapq import heappush, heappop

N, A, B, C = [int(x) for x in input().split()]
S = [input() for i in range(N)]


sToIndex = {
    "AB": (0, 1),
    "AC": (0, 2),
    "BC": (1, 2),
}
heap = [(0, -min(A, B, C), (A, B, C))]
parent = {}
while heap:
    original = heappop(heap)
    negT, negMin, state = original

    if negT == -N:

        print("Yes")
        ans = []
        last = original
        while last in parent:
            nextLast = parent[last]
            diffIndex = None
            for i in range(3):
                if nextLast[2][i] + 1 == last[2][i]:
                    diffIndex = i
                    break
            ans.append("ABC"[diffIndex])
            last = nextLast
        print("\n".join(reversed(ans)))
        exit()

    index1, index2 = sToIndex[S[-negT]]

    nbr1 = list(state)
    nbr1[index1] += 1
    nbr1[index2] -= 1
    min1 = min(nbr1)
    if min1 >= 0:
        node = (negT - 1, -min1, tuple(nbr1))
        if node not in parent:
            heappush(heap, node)
            parent[node] = original

    nbr2 = list(state)
    nbr2[index1] -= 1
    nbr2[index2] += 1
    min2 = min(nbr2)
    if min2 >= 0:
        node = (negT - 1, -min2, tuple(nbr2))
        if node not in parent:
            heappush(heap, node)
            parent[node] = original

print("No")

Submission status: Accepted

Solution code:
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

# 合計は同じ、0,0の場所が選ばれたら終わり
# 次のものに渡しておく
N, A, B, C = lr()
S = [sr() for _ in range(N)]
answer = []
for i, s in enumerate(S):
    if s == 'AB':
        if A == B == 0:
            print('No'); exit()
        elif A == 0:
            answer.append('A')
            A += 1; B -= 1; continue
        elif B == 0 or i == N-1:
            answer.append('B')
            B += 1; A -= 1; continue
        ne = S[i+1]
        if 'A' in ne:
            answer.append('A')
            A += 1; B -= 1
        else:
            answer.append('B')
            B += 1; A -= 1
    elif s == 'BC':
        if C == B == 0:
            print('No'); exit()
        elif C == 0:
            answer.append('C')
            C += 1; B -= 1; continue
        elif B == 0 or i == N-1:
            answer.append('B')
            B += 1; C -= 1; continue
        ne = S[i+1]
        if 'C' in ne:
            answer.append('C')
            C += 1; B -= 1
        else:
            answer.append('B')
            B += 1; C -= 1
    elif s == 'AC':
        if A == C == 0:
            print('No'); exit()
        elif A == 0:
            answer.append('A')
            A += 1; C -= 1; continue
        elif C == 0 or i == N-1:
            answer.append('C')
            C += 1; A -= 1; continue
        ne = S[i+1]
        if 'A' in ne:
            answer.append('A')
            A += 1; C -= 1
        else:
            answer.append('C')
            C += 1; A -= 1

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

Submission status: Accepted

Solution code:


n, a, b, c = map(int, input().split())
ss = [None] * n
for i in range(n):
    ss[i] = input()

abc = {"A": a, "B": b, "C": c}
ans = [None] * n
# 3つある→ aをなくしにかかる
# 2つある→ bを1にしにかかる
# 0, 1, c→ c>=2のとき、
No = False
for i,s in enumerate(ss):
    zero = [i for i, item in abc.items() if item==0]
    one = [i for i, item in abc.items() if item==1]
    two = [i for i, item in abc.items() if item>=2]
#     print(s)
#     print(zero, one, two)
    if len(zero)==0:
        if s in ["AB", "AC"]:
            ans[i] = "A"
            abc["A"] += 1
            abc[s[1]] -= 1
        else:
            ans[i] = "C"
            abc["C"] += 1
            abc["B"] -= 1
    elif len(zero)==1 and len(two)>=1:
        z = zero[0]
        t = two[0]
        if z in s:
            # zに加える
            ans[i] = z
            abc[z] += 1
            abc[s.replace(z, "")] -= 1
        else:
            # twoから引く
            tmp = s.replace(t, "")
            ans[i] = tmp
            abc[tmp] += 1
            abc[t] -= 1
    elif len(zero)==1 and len(one)==2:
        z = zero[0]
        if s[0] in one and s[1] in one:
            # １手先を見て、
            ns = ss[i+1] if i+1<=n-1 else None
            if (ns is None) or (z not in ns):
                # 次も同じなのでどちらでもよい
                ans[i] = one[0]
                abc[one[0]] += 1
                abc[one[1]] -= 1
            else:
                # nsのzでないほうに加える
                tmp = ns.replace(z, "")
                ans[i] = tmp
                abc[tmp] += 1
                abc[s.replace(tmp, "")] -= 1
        else:
            # 0に加える
            z = zero[0]
            ans[i] = z
            abc[z] += 1
            abc[s.replace(z, "")] -= 1
    else:
        # 0が２つ以上
        assert len(zero) >= 2
        if len(zero)==3:
            No = True
            break
        elif s[0] in zero and s[1] in zero:
            No = True
            break
        else:
            # zに加える
            z = (set(s) & set(zero)).pop()
            ans[i] = z
            abc[z] += 1
            abc[s.replace(z, "")] -= 1
            
if No:
    print("No")
else:
    print("Yes")
    print("\n".join(ans))

Submission status: Accepted

Solution code:
import sys

n,a,b,c = map(int,input().split())
s_list = [input() for _ in range(n)]
print_list = []


for i,s in enumerate(s_list):
    if s == 'AB':
        if a == 0 and b == 0:  
            print('No')
            sys.exit()
        else:
            if a > b:
                print_list.append('B')
                a -= 1
                b += 1
            elif b > a:
                print_list.append('A')
                a += 1
                b -= 1
            else:
                if c == 0:
                    if i < len(s_list)-1 and s_list[i+1] == 'AC':
                        print_list.append('A')
                        a += 1
                        b -= 1
                    else:
                        print_list.append('B')
                        a -= 1
                        b += 1
                else:
                    print_list.append('B')
                    a -= 1
                    b += 1


    elif s == 'BC':
        if c == 0 and b == 0:  
            print('No')
            sys.exit()
        else:
            if c > b:
                print_list.append('B')
                c -= 1
                b += 1
            elif b > c:
                print_list.append('C')
                c += 1
                b -= 1
            else:
                if a == 0:
                    if i < len(s_list)-1 and s_list[i+1] == 'AB':
                        print_list.append('B')
                        c -= 1
                        b += 1
                    else:
                        print_list.append('C')
                        c += 1
                        b -= 1
                else:
                    print_list.append('B')
                    c -= 1
                    b += 1

    elif s == 'AC':
        if a == 0 and c == 0:  
            print('No')
            sys.exit()
        else:
            if a > c:
                print_list.append('C')
                a -= 1
                c += 1
            elif c > a:
                print_list.append('A')
                a += 1
                c -= 1

            else:
                if b == 0:
                    if i < len(s_list)-1 and s_list[i+1] == 'AB':
                        print_list.append('A')
                        c -= 1
                        a += 1
                    else:
                        print_list.append('C')
                        c += 1
                        a -= 1
                else:
                    print_list.append('C')
                    c += 1
                    a -= 1

print('Yes')
for p in print_list:
    print(p)

Submission status: Accepted

Solution code:
import sys

N,A,B,C = map(int,input().split())
s = [0]*N

for i in range(N):
    s[i] = input()

ans = [0]*N
for i in range(N):
    if s[i] == "AB":
        if A == 0 and B == 0:
            print("No")
            sys.exit()
        elif A == 0:
            ans[i] = "A"
            A += 1
            B -= 1
        elif B == 0:
            ans[i] = "B"
            B += 1
            A -= 1
        elif i < N-1 and s[i+1] == "BC":
            ans[i] = "B"
            B += 1
            A -= 1
        elif i < N-1 and s[i+1] == "AC":
            ans[i] = "A"
            A += 1
            B -= 1
        else:
            ans[i] = "A"
            A += 1
            B -= 1
    elif s[i] == "BC":
        if B == 0 and C == 0:
            print("No")
            sys.exit()
        elif B == 0:
            ans[i] = "B"
            B += 1
            C -= 1
        elif C == 0:
            ans[i] = "C"
            C += 1
            B -= 1
        elif i < N-1 and s[i+1] == "AB":
            ans[i] = "B"
            B += 1
            C -= 1
        elif i < N-1 and s[i+1] == "AC":
            ans[i] = "C"
            C += 1
            B -= 1
        else:
            ans[i] = "B"
            B += 1
            C -= 1
    elif s[i] == "AC":
        if A == 0 and C == 0:
            print("No")
            sys.exit()
        elif C == 0:
            ans[i] = "C"
            C += 1
            A -= 1
        elif A == 0:
            ans[i] = "A"
            A += 1
            C -= 1
        elif i < N-1 and s[i+1] == "AB":
            ans[i] = "A"
            A += 1
            C -= 1
        elif i < N-1 and s[i+1] == "BC":
            ans[i] = "C"
            C += 1
            A -= 1
        else:
            ans[i] = "C"
            C += 1
            A -= 1

print("Yes")
for i in range(N):
    print(ans[i])

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, a, b, c = LI()
ans = []
D = {'A':a, 'B':b, 'C':c}
sx = SR(n)
for i in range(n):
    s = sx[i]
    s1 = s[0]
    s2 = s[1]
    if D[s1] == D[s2] == 0:
        print('No')
        exit()
    elif D[s[0]] == D[s[1]] == 1 and i + 1 < n:
        if s1 in sx[i + 1]:
            D[s1] += 1
            D[s2] -= 1
            ans += [s1]
        else:
            D[s2] += 1
            D[s1] -= 1
            ans += [s2]
    elif D[s1] == 0:
        ans += [s1]
        D[s1] += 1
        D[s2] -= 1
    elif D[s2] == 0:
        ans += [s2]
        D[s1] -= 1
        D[s2] += 1
    else:
        if D[s1] > D[s2]:
            ans += [s2]
            D[s1] -= 1
            D[s2] += 1
        else:
            ans += [s1]
            D[s1] += 1
            D[s2] -= 1

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

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

def pow_k(x, n):
    ans = 1
    while n:
        if n % 2:
            ans *= x
        x *= x
        n >>= 1
    return ans

"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

n,a,b,c = m()

no = {"A":a,"B":b,"C":c}

ans = []

abc = ["A","B","C"]
fl = 0
for i in range(n):
    s = set(list(input()[:-1]))
    if i == 0:
        pre = s
        continue
    po = (pre & s)

    pre = list(pre)
    if no[pre[0]] == 0:
        no[pre[0]] += 1
        no[pre[1]] -= 1
        ans.append(pre[0])
    elif no[pre[1]] == 0:
        no[pre[0]] -= 1
        no[pre[1]] += 1
        ans.append(pre[1])
    else:
        if len(po) == 2:
            if no[pre[0]] >= no[pre[1]]:
                no[pre[0]] -= 1
                no[pre[1]] += 1
                ans.append(pre[1])
            else:
                no[pre[0]] += 1
                no[pre[1]] -= 1
                ans.append(pre[0])
        else:
            aaa = po.pop()
            if no[aaa] == 1:
                for j in pre:
                    if j == aaa:
                        no[j] += 1
                    else:
                        no[j] -= 1
                ans.append(aaa)
            else:
                if no[pre[0]] >= no[pre[1]]:
                    no[pre[0]] -= 1
                    no[pre[1]] += 1
                    ans.append(pre[1])
                else:
                    no[pre[0]] += 1
                    no[pre[1]] -= 1
                    ans.append(pre[0])



    for i in abc:
        if no[i] < 0:
            fl = 1
            break
    if fl:
        break

    pre = s
else:
    pre = list(pre)
    if no[pre[0]] == 0:
        no[pre[0]] += 1
        no[pre[1]] -= 1
        ans.append(pre[0])
    elif no[pre[1]] == 0:
        no[pre[0]] -= 1
        no[pre[1]] += 1
        ans.append(pre[1])
    else:
        ans.append(pre[1])
    for i in abc:
        if no[i] < 0:
            fl = 1
            break


if fl:
    print("No")
else:
    print("Yes")
    for i in ans:
        print(i)

Submission status: Accepted

Solution code:
n, a, b, c = map(int,input().split())
s = [input() for i in range(n)]
dic = {'A':a, 'B':b, 'C':c}
l = []
for i in range(n):
    temp1 = s[i][0]
    temp2 = s[i][1]
    if dic[temp1] == 0 and dic[temp2] == 0:
        print('No')
        exit()

    if dic[temp1] < dic[temp2]:
        dic[temp1] += 1
        dic[temp2] -= 1
        l.append(temp1)
    elif dic[temp1] == dic[temp2] and i != n - 1:
        n1, n2 = s[i + 1]
        if (n1, n2) == (temp1, temp2):
            dic[temp2] += 1
            dic[temp1] -= 1
            l.append(temp2)
        else:
            if temp1 == n1 or temp1 == n2:
                dic[temp1] += 1
                dic[temp2] -= 1
                l.append(temp1)
            else:
                dic[temp2] += 1
                dic[temp1] -= 1
                l.append(temp2)
    else:
        dic[temp2] += 1
        dic[temp1] -= 1
        l.append(temp2)
print('Yes')
print(*l, sep = '\n')

Submission status: Accepted

Solution code:
n, av, bv, cv = list(map(int, input().split()))
s = []
for i in range(n):
    s.append(input())

v = {'A':av, 'B':bv, 'C':cv}

selection = []
for i in range(n):
    if v[s[i][0]] == 0:
        selection.append(s[i][0])
        v[s[i][0]] += 1
        v[s[i][1]] -= 1
    elif v[s[i][1]] == 0:
        selection.append(s[i][1])
        v[s[i][0]] -= 1
        v[s[i][1]] += 1
    elif v[s[i][0]] == 1 and v[s[i][1]] == 1:
        if i == n-1:
            selection.append(s[i][0])
            v[s[i][0]] += 1
            v[s[i][1]] -= 1       
        else:
            if s[i+1][0] == s[i][0] or s[i+1][1] == s[i][0]:
                selection.append(s[i][0])
                v[s[i][0]] += 1
                v[s[i][1]] -= 1 
            else:
                selection.append(s[i][1])
                v[s[i][0]] -= 1
                v[s[i][1]] += 1
    else:
        if v[s[i][0]] > v[s[i][1]]:
            selection.append(s[i][1])
            v[s[i][0]] -= 1
            v[s[i][1]] += 1
        else:
            selection.append(s[i][0])
            v[s[i][0]] += 1
            v[s[i][1]] -= 1
    
    if v['A'] < 0 or v['B'] < 0 or v['C'] < 0:
        print('No')
        break
else:
    print('Yes')
    for i in range(n):
        print(selection[i])

Submission status: Accepted

Solution code:
def play():
    N, A, B, C = map(int, input().split())
    S = [input() for i in range(N)]
    p = []
    for i in range(N):
        s = S[i]
        if s == "AB":
            if A + B == 0:
                print("No")
                return 0
            elif A == 1 and B == 1 and i != N-1:
                t = S[i+1]
                if t == "AC":
                    A += 1
                    B -= 1
                    p.append("A")
                else:
                    A -= 1
                    B += 1
                    p.append("B")
            elif A >= B:
                A -= 1
                B += 1
                p.append('B')
            else:
                A += 1
                B -= 1
                p.append('A')
        if s == "AC":
            if A + C == 0:
                print("No")
                return 0
            elif A == 1 and C == 1 and i != N-1:
                t = S[i+1]
                if t == "AB":
                    A += 1
                    C -= 1
                    p.append("A")
                else:
                    A -= 1
                    C += 1
                    p.append("C")
            elif A >= C:
                A -= 1
                C += 1
                p.append('C')
            else:
                A += 1
                C -= 1
                p.append('A')
        if s == "BC":
            if B + C == 0:
                print("No")
                return 0
            elif B == 1 and C == 1 and i != N-1:
                t = S[i+1]
                if t == "AB":
                    B += 1
                    C -= 1
                    p.append("B")
                else:
                    B -= 1
                    C += 1
                    p.append("C")
            elif B >= C:
                B -= 1
                C += 1
                p.append('C')
            else:
                B += 1
                C -= 1
                p.append('B')
    print('Yes')
    for i in range(N):
        print(p[i])
    return 0


if __name__ == '__main__':
    play()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import sys
sys.setrecursionlimit(300000)

YES = "Yes"  # type: str
NO = "No"  # type: str

def solve(N: int, A: int, B: int, C: int, S: "List[str]"):
    L = 'ABC'
    ll = []
    def rec(i, a, b, c):
        #print(a, b, c, ll)
        if i == N:
            return ll
        s = S[i]
        vals = [a, b, c]
        if s == 'AB':
            u, v = 0, 1
        if s == 'BC':
            u, v = 1, 2
        if s == 'AC':
            u, v = 0, 2

        if vals[u] == 0 and vals[v] == 0:
            if len(ll) > 0:
                ll.pop()
            return False
        elif vals[u] == 1 and vals[v] == 1:
            ll.append(L[u])
            vals[u] += 1
            vals[v] -= 1
            tmp = rec(i + 1, vals[0], vals[1], vals[2])
            if tmp:
                return tmp
            else:
                ll.append(L[v])
                vals[u] -= 2
                vals[v] += 2
                return rec(i + 1, vals[0], vals[1], vals[2])
        elif vals[u] == 0 or (vals[v] > 1 and vals[u] == 1):
            ll.append(L[u])
            vals[u] += 1
            vals[v] -= 1
            return rec(i + 1, vals[0], vals[1], vals[2])
        else:
            ll.append(L[v])
            vals[u] -= 1
            vals[v] += 1
            return rec(i + 1, vals[0], vals[1], vals[2])

    ret = rec(0, A, B, C)
    if ret:
        print('Yes')
        for c in ret:
            print(c)
        '''
        vals = [A, B, C]
        for i in range(N):
            x = S[i]
            c = ret[i]
            for d in x:
                k = L.find(d)
                if d == c:
                    vals[k] += 1
                else:
                    vals[k] -= 1
            print(x, c)
            print(vals)
        '''
    else:
        print('No')

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

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



def s3(abc_dict):
    ans = []
    for _ in range(N):
        xy = input()
        x = xy[0] 
        y = xy[1]
        if abc_dict[x]==0 and abc_dict[y]==0:
            return False, []
        elif abc_dict[x]>=abc_dict[y]:
            abc_dict[x] -= 1
            abc_dict[y] += 1
            ans.append(y)
        else:
            abc_dict[y] -= 1
            abc_dict[x] += 1
            ans.append(x)
    return True, ans

def s2(abc_dict):

    ans = []
    flag = True
    q_list = []
    for _ in range(N):
        q_list.append(input())

    for i in range(N):
        xy = q_list[i]
        x = xy[0]
        y = xy[1]

        if (abc_dict[x]>=1 and abc_dict[y]==0):
            abc_dict[x]-=1
            abc_dict[y]+=1
            ans.append(y)
        elif (abc_dict[y]>=1 and abc_dict[x]==0):
            abc_dict[y]-=1
            abc_dict[x]+=1
            ans.append(x)
        elif  (abc_dict[x]==1 and abc_dict[y]==1):
            if i==N-1:
                ans.append(x)
            else:
                next_xy = q_list[i+1]
                if xy==next_xy: # 次も同じなのでどちらでもいい
                    abc_dict[x]+=1
                    abc_dict[y]-=1
                    ans.append(x)
                elif (x in next_xy):
                    abc_dict[x]+=1
                    abc_dict[y]-=1
                    ans.append(x)
                else: # 次にyが含まれる
                    abc_dict[y]+=1
                    abc_dict[x]-=1
                    ans.append(y)
        else:
            return False, []
    return True,ans

def s1(abc_dict):
    
    ans = []
    flag = True
    for _ in range(N):
        xy = input()
        x = xy[0] 
        y = xy[1]
        if abc_dict[x]==1:
            abc_dict[x]-=1
            abc_dict[y]+=1
            ans.append(y)
        elif abc_dict[y]==1:
            abc_dict[y]-=1
            abc_dict[x]+=1
            ans.append(x)
        else:
            flag = False
            return flag,[]
    return flag, ans
    
def s0(abc_dict):
    return False, []

abc_dict = {"A":A,"B":B,"C":C}
sumABC = A+B+C
if sumABC>=3:
    flag, ans = s3(abc_dict)
elif sumABC==2:
    flag, ans = s2(abc_dict)
elif sumABC==1:
    flag, ans = s1(abc_dict)
elif sumABC==0:
    flag, ans = False, []

if flag:
    print('Yes')
    for i in range(N):
        print(ans[i])
else:
    print("No")