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:
#!python3

# input
N, A, B, C = list(map(int, input().split()))
S = [input() for _ in range(N)]


def main():
    l = [A, B, C]
    d = {"AB": (0, 1, 2), "AC": (0, 2, 1), "BC": (1, 2, 0)}
    dl = ["A", "B", "C"]
    ans = []
    for n in range(N):
        i, j, k = d[S[n]]
        if l[i] < l[j]:
            l[i] += 1
            l[j] -= 1
            ans.append(dl[i])
        elif l[i] > l[j]:
            l[i] -= 1
            l[j] += 1
            ans.append(dl[j])
        elif l[i] == 0:
            print("No")
            return
        elif S[n] == S[n + 1]:
            l[i] += 1
            l[j] -= 1
            ans.append(dl[i])
        else:
            if i == d[S[n + 1]][0] or i == d[S[n + 1]][1]:
                i, j = j, i
            if l[i] < l[k]:
                l[i] -= 1
                l[j] += 1
                ans.append(dl[j])
            else:
                l[i] += 1
                l[j] -= 1
                ans.append(dl[i])
        
    print("Yes")
    print("\n".join(ans))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int, input().split())
S = []
for _ in range(N):
    S.append(input())
ans = []
for s in S:
    if s == "AB":
        if A >= B:
            A -= 1
            B += 1
            ans.append("B")
        else:
            B -= 1
            A += 1
            ans.append("A")
    elif s == "AC":
        if A >= C:
            A -= 1
            C += 1
            ans.append("C")
        else:
            C -= 1
            A += 1
            ans.append("A")
    elif s == "BC":
        if B >= C:
            B -= 1
            C += 1
            ans.append("C")
        else:
            C -= 1
            B += 1
            ans.append("B")

if not all(map(lambda x: x > 0, (A, B, C))):
    print("No")
else:
    print("Yes")
    for a in ans:
        print(a)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(700000)

def s_in():
    return input()

def n_in():
    return int(input())

def l_in():
    return list(map(int, input().split()))

n,a,b,c=l_in()
S=[s_in() for _ in range(n)]

res = []

for s in S:
    if s == 'AB':
        if a == 0 and b == 0:
            break
        if a > b:
            a -= 1
            b += 1
            res.append('B')
        else:
            a += 1
            b -= 1
            res.append('A')
    if s == 'AC':
        if a == 0 and c == 0:
            break
        if a > c:
            a -= 1
            c += 1
            res.append('C')
        else:
            a += 1
            c -= 1
            res.append('A')
            
    if s == 'BC':
        if c == 0 and b == 0:
            break
        if b > c:
            b -= 1
            c += 1
            res.append('C')
            
        else:
            b += 1
            c -= 1
            res.append('B')
else:
    print('Yes')
    [print(r) for r in res]
    exit()

print('No')

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline

N, A, B, C = map(int, readline().split())
s = list(map(str, read().split()))
S = ''.join(s).translate(str.maketrans({'A': '0', 'B': '1', 'C': '2'}))
v = [A, B, C]
answer = []
for i, j in zip(S[::2], S[1::2]):
    i = int(i)
    j = int(j)
    if v[i] >= v[j]:
        answer.append(j)
        v[i] -= 1
        v[j] += 1
    else:
        answer.append(i)
        v[i] += 1
        v[j] -= 1

if any(i < 0 for i in v):
    print('No')
else:
    print('Yes')
    ans = []
    for i in answer:
        if i == 0:
            ans.append('A')
        elif i == 1:
            ans.append('B')
        else:
            ans.append('C')
    print('\n'.join(ans))

Submission status: Wrong Answer

Solution code:
n, *L = map(int, input().split())
t = "ABC"
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 = []
for i, s in enumerate(S):
    u, v, w = f(s)
    if L[u] == 0 and L[v] == 0:
        print("No")
        exit()
    if L[u] == 0:
        A.append(t[u])
        L[u] += 1
        L[v] -= 1
    elif L[v] == 0:
        A.append(t[v])
        L[v] += 1
        L[u] -= 1
    elif L[u] > L[v]:
        A.append(t[v])
        L[v] += 1
        L[u] -= 1
    elif L[v] > L[u]:
        A.append(t[u])
        L[u] += 1
        L[v] -= 1
    else:
        if i == n-1:
            A.append(t[u])
            L[u] += 1
            L[v] -= 1
            break
        ns = S[i+1]
        for j, l in enumerate(t):
            if l in s and l in ns:
                A.append(l)
                L[j] += 1
                if u == j:
                    L[v] -= 1
                else:
                    L[u] -= 1
print("Yes")
print("\n".join(A))

Submission status: Wrong Answer

Solution code:
"""
AB,AC,BCの数をそれぞれカウントしておく。
引かれる用の変数と足される用の変数を用意しておく？

ABが出たら、Aにプラス1,Bにマイナス1する。
ACが出たら、Cにプラス1,Aにマイナス1する。
BCが出たら、Bにプラス1,Cにマイナス1する。

Cがマイナスになったら、"BCの回数”を上限にマイナス分をBから分けてもらう。
Bがマイナスになったら、"ABの回数"を上限にマイナス分をAから分けてもらう。
Aがマイナスになったら、"ACの回数"を上限にマイナス分をCから分けてもらう。
Cがマイナスになったら、"BCの回数”を上限にマイナス分をBから分けてもらう。

ここまでやって、どれか一つでも-があれば不可能。
もし、全部プラスになれば、行った操作を記録しているので、それをもとに復元を行う。

"""
import copy
from collections import Counter
N,A,B,C = map(int,input().split())
tmp = []
for i in range(N):
    s = input()
    tmp.append(s)
game = Counter(tmp)
if "AB" not in game:
    game["AB"] = 0
if "AC" not in game:
    game["AC"] = 0
if "BC" not in game:
    game["BC"] = 0

default = copy.copy(game)

A += game["AB"]
B -= game["AB"]
B += game["BC"]
C -= game["BC"]
C += game["AC"]
A -= game["AC"]

if C < 0:
    diff = 0 - C
    if diff > game["BC"]:
        C += diff
        B -= diff
        game["BC"] -= diff
    else:
        print("No")
        exit()
if B < 0:
    diff = 0 - B
    if diff > game["AB"]:
        B += diff
        A -= diff
        game["AB"] -= diff
    else:
        print("No")
        exit()
if A < 0:
    diff = 0 - A
    if diff > game["AC"]:
        A += diff
        C -= diff
        game["AC"] -= diff
    else:
        print("No")
        exit()
if C < 0:
    diff = 0 - C
    if diff > game["BC"]:
        C += diff
        B -= diff
        game["BC"] -= diff
    else:
        print("No")
        exit()

print("Yes")
for s in tmp:
    if s == "AB":
        if default[s]>0:
            print("A")
            default[s] -= 1
        else:
            print("B")
        
    if s == "BC":
        if default[s]>0:
            print("B")
            default[s] -= 1
        else:
            print("C")
    if s == "AC":
        if default[s]>0:
            print("C")
            default[s] -= 1
        else:
            print("A")

Submission status: Wrong Answer

Solution code:
#import sys
#input = sys.stdin.readline
def one(D, S):
    d = {"A":0, "B":1, "C":2}
    ANS = []
    for a, b in S:
        x = d[a]
        y = d[b]
        if D[x] == 0 and D[y] == 0:
            print("No")
            return
        if D[x] == 0:
            D[x] += 1
            D[y] -= 1
            ANS.append(a)
        else:
            D[x] -= 1
            D[y] += 1
            ANS.append(b)
    print("\n".join(ANS))
            
def two(D,S,N):
    d = {"A":0, "B":1, "C":2}
    e = {0:"A", 1:"B",2:"C"}
    ANS = []
    for i in range(N):
        a, b = S[i]
        x = d[a]
        y = d[b]
        if D[x] == 0 and D[y] == 0:
            print("No")
            return
        if D[x] == 0:
            D[x] += 1
            D[y] -= 1
            ANS.append(a)
        elif D[y] == 0: 
            D[x] -= 1
            D[y] += 1
            ANS.append(b)
        else: #(1,1)
            if i+1 > N-1:
                D[x] += 1
                D[y] -= 1
                ANS.append(a)
                continue
            if S[i] == S[i+1]:
                D[x] += 1
                D[y] -= 1
                ANS.append(a)
                continue
            if not a in S[i+1]:
                x,y = y, x
            D[x] += 1
            D[y] -= 1
            ANS.append(a)
                
    print("\n".join(ANS))
    
def main():
    N, A, B, C = map( int, input().split())
    S = [ tuple( input()) for _ in range(N)]
    d = {"A":0, "B":1, "C":2}
    D = [A,B,C]
    ANS = []
    if sum(D) == 1:
        one(D, S)
        return
    if sum(D) == 2:
        two(D,S,N)
        return
    for a, b in S:
        x = d[a]
        y = d[b]
        if D[x] == 0 and D[y] == 0:
            print("No")
            return
        if D[x] == 0:
            D[x] += 1
            D[y] -= 1
            ANS.append(a)
        elif D[x] == 1:
            if D[y] == 0:
                D[x] -= 1
                D[y] += 1
                ANS.append(b)
            else:
                D[x] += 1
                D[y] -= 1
                ANS.append(a)
        else:
            D[x] -= 1
            D[y] += 1
            ANS.append(b)

    print("Yes")
    print("\n".join(ANS))
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline
from collections import defaultdict
from heapq import heappop, heappush

from decimal import *

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 累乗 #####
# pow(x, y, z) -> x**y % z

##### 割り算の切り上げ #####
# tmp = -(-4 // 3)

##### dict の for文 #####
# for k, v in d.items():
#     print(k, v)

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	N,A,B,C = inputMap()
	stat = [0,0,0]
	Q = []
	for i in range(N):
		s = inputStr()
		Q.append(s)
		if s == "AB":
			stat[0] += 1
			stat[1] += 1
		elif s == "AC":
			stat[0] += 1
			stat[2] += 1
		elif s == "BC":
			stat[1] += 1
			stat[2] += 1

	delA = False
	delB = False
	delC = False
	if stat[0] <= A:
		delA = True
	if stat[1] <= B:
		delB = True
	if stat[2] <= C:
		delC = True

	#print(stat)
	#print("{} {} {}".format(delA,delB,delC))

	hon = "Yes"
	ans = []
	for s in Q:
		#print(i)
		#print("{} {} {}".format(A,B,C))
		if s == "AB":
			if delA == True:
				B += 1
				A -= 1
				ans.append("B")
				continue
			if delB == True:
				B -= 1
				A += 1
				ans.append("A")
				continue
			if A < B:
				B -= 1
				A += 1
				ans.append("A")
			else:
				B += 1
				A -= 1
				ans.append("B")
			if A < 0 or B < 0:
				hon = "No"
				break
		elif s == "AC":
			if delA == True:
				C += 1
				A -= 1
				ans.append("C")
				continue
			if delC == True:
				C -= 1
				A += 1
				ans.append("A")
				continue
			if A <= C:
				C -= 1
				A += 1
				ans.append("A")
			else:
				C += 1
				A -= 1
				ans.append("C")
			if A < 0 or C < 0:
				hon = "No"
				break
		elif s == "BC":
			if delB == True:
				C += 1
				B -= 1
				ans.append("C")
				continue
			if delC == True:
				C -= 1
				B += 1
				ans.append("B")
				continue
			if B <= C:
				C -= 1
				B += 1
				ans.append("B")
			else:
				C += 1
				B -= 1
				ans.append("C")
			if C < 0 or B < 0:
				hon = "No"
				break

	if hon == "No":
		print(hon)
	else:
		print(hon)
		for i in ans:
			print(i)


#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
	# comb = Combination(n+r-1, mod)
	# return comb(n+r-1, r)
    return combination(n+r-1, r, mod)


class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# dfs のサンプル
def dfs(graph,parent,counter,edge):
    stk = []
    stk.append(edge)
    while len(stk) > 0:
        p = stk.pop()
        for e in graph[p]:
            if parent[p] == e:
                continue
            else:
                parent[e] = p
                counter[e] += counter[p]
                stk.append(e)

if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
import bisect
import collections
import sys

sys.setrecursionlimit(100000)
input = sys.stdin.readline
ACMOD = 1000000007
INF = 1 << 62


def lmi():
    return list(map(int, input().split()))


def llmi(n):
    return [lmi() for _ in range(n)]


N, A, B, C = lmi()
S = [input().strip() for _ in range(N)]

a = A
b = B
total = A+B+C
ans = []
for s in S:
    c = total - a - b
    if s == "AB":
        if a == 0 and b == 0:
            print("No")
            exit()
        if a > b:
            a -= 1
            b += 1
            ans.append('B')
        else:
            a += 1
            b -= 1
            ans.append('A')
    if s == "AC":
        if a == 0 and c == 0:
            print("No")
            exit()
        if a > c:
            a -= 1
            ans.append('C')
        else:
            a += 1
            ans.append('A')
    if s == "BC":
        if b == 0 and c == 0:
            print("No")
            exit()
        if b > c:
            b -= 1
            ans.append('C')
        else:
            b += 1
            ans.append('B')
    # print(a,b,c)

# print(ans, a, b, c)
print('\n'.join(['Yes'] + ans))

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,input().split())
ans = "Yes"
ansl = []
for i in range(n):
  s = str(input())
  if(s=="AB"):
    if(a>b):
      ansl.append("B")
      b+=1
      a-=1
    else:
      ansl.append("A")
      a+=1
      b-=1
  elif(s=="AC"):
    if(a>c):
      ansl.append("C")
      c+=1
      a-=1
    else:
      ansl.append("A")
      a+=1
      c-=1
  else:
    if(b>c):
      ansl.append("C")
      c+=1
      b-=1
    else:
      ansl.append("B")
      b+=1
      c-=1  
  if(a<0)or(b<0)or(c<0):
    ans = "No"
    break
if(ans == "Yes"):
  print(ans)
  for i in range(n):
    print(ansl[i])
else:
  print(ans)

Submission status: Wrong Answer

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

print("No")

Submission status: Wrong Answer

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




if a==0 and  b==0:
    if s[0] == "AB":
        print("No")
        exit()
elif a==0 and  c==0:
    if s[0] == "AC":
        print("No")
        exit()
elif b==0 and  c==0:
    if s[0] == "BC":
        print("No")
        exit()

print("Yes")
for i in range(n):
    if s[i] == "AB":
        if a >= b:
            a -= 1
            b += 1
            print("B")
        else:
            a += 1
            b -= 1
            print("A")
    elif s[i] == "AC":
        if a >= c:
            a -= 1
            c += 1
            print("C")
        else:
            a += 1
            c -= 1
            print("A")
    else:
        if b >= c:
            b -= 1
            c += 1
            print("C")
        else:
            b += 1
            c -= 1
            print("B")

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline
from collections import defaultdict
from heapq import heappop, heappush

from decimal import *

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 累乗 #####
# pow(x, y, z) -> x**y % z

##### 割り算の切り上げ #####
# tmp = -(-4 // 3)

##### dict の for文 #####
# for k, v in d.items():
#     print(k, v)

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	N,A,B,C = inputMap()
	stat = [0,0,0]
	Q = []
	for i in range(N):
		s = inputStr()
		Q.append(s)
		if s == "AB":
			stat[0] += 1
			stat[1] += 1
		elif s == "AC":
			stat[0] += 1
			stat[2] += 1
		elif s == "BC":
			stat[1] += 1
			stat[2] += 1

	delA = False
	delB = False
	delC = False
	if stat[0] <= A:
		delA = True
	if stat[1] <= B:
		delB = True
	if stat[2] <= C:
		delC = True

	#print(stat)
	#print("{} {} {}".format(delA,delB,delC))

	hon = "Yes"
	ans = []
	for s in Q:
		#print(i)
		#print("{} {} {}".format(A,B,C))
		if s == "AB":
			if delA == True:
				B += 1
				A -= 1
				ans.append("B")
				continue
			if delB == True:
				B -= 1
				A += 1
				ans.append("A")
				continue
			if A <= B:
				B -= 1
				A += 1
				ans.append("A")
			else:
				B += 1
				A -= 1
				ans.append("B")
			if A < 0 or B < 0:
				hon = "No"
				break
		elif s == "AC":
			if delA == True:
				C += 1
				A -= 1
				ans.append("C")
				continue
			if delC == True:
				C -= 1
				A += 1
				ans.append("A")
				continue
			if A <= C:
				C -= 1
				A += 1
				ans.append("A")
			else:
				C += 1
				A -= 1
				ans.append("C")
			if A < 0 or C < 0:
				hon = "No"
				break
		elif s == "BC":
			if delB == True:
				C += 1
				B -= 1
				ans.append("C")
				continue
			if delC == True:
				C -= 1
				B += 1
				ans.append("B")
				continue
			if B < C:
				C -= 1
				B += 1
				ans.append("B")
			else:
				C += 1
				B -= 1
				ans.append("C")
			if C < 0 or B < 0:
				hon = "No"
				break

	if hon == "No":
		print(hon)
	else:
		print(hon)
		for i in ans:
			print(i)


#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
	# comb = Combination(n+r-1, mod)
	# return comb(n+r-1, r)
    return combination(n+r-1, r, mod)


class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# dfs のサンプル
def dfs(graph,parent,counter,edge):
    stk = []
    stk.append(edge)
    while len(stk) > 0:
        p = stk.pop()
        for e in graph[p]:
            if parent[p] == e:
                continue
            else:
                parent[e] = p
                counter[e] += counter[p]
                stk.append(e)

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 = []
ans = []
for i in range(n):
    s.append(input().rstrip("\n"))
def check(x, y):
    if x + y == 0:
        print("No")
        sys.exit()

for si in s:
    if si == "AB":
        check(a, b)
        if a > b:
            ans.append('B')
            a -= 1
            b += 1
        else:
            ans.append('A')
            b -= 1
            a += 1
    if si == "AC":
        check(a, c)
        if a > c:
            ans.append('C')
            a -= 1
            c += 1
        else:
            ans.append('A')
            c -= 1
            a += 1
    if si == "BC":
        check(b, c)
        if b > c:
            ans.append('C')
            b -= 1
            c += 1
        else:
            ans.append('B')
            c -= 1
            b += 1
print("Yes")
for item in ans:
    print(item)