Problem:


Score : 600 points

Problem Statement

There is a game that involves three variables, denoted A, B, and C.

As the game progresses, there will be N events where you are asked to make a choice.
Each of these choices is represented by a string s_i. If s_i is AB, you must add 1 to A or B then subtract 1 from the other; if s_i is AC, you must add 1 to A or C then subtract 1 from the other; if s_i is BC, you must add 1 to B or C then subtract 1 from the other.

After each choice, none of A, B, and C should be negative.

Determine whether it is possible to make N choices under this condition. If it is possible, also give one such way to make the choices.

Constraints

1 \leq N \leq 10^5

0 \leq A,B,C \leq 10^9

N, A, B, C are integers.

s_i is AB, AC, or BC.

Input

Input is given from Standard Input in the following format:

N A B C
s_1
s_2
:
s_N

Output

If it is possible to make N choices under the condition, print Yes; otherwise, print No.

Also, in the former case, show one such way to make the choices in the subsequent N lines. The (i+1)-th line should contain the name of the variable (A, B, or C) to which you add 1 in the i-th choice.

Sample Input 1

2 1 3 0
AB
AC

Sample Output 1

Yes
A
C

You can successfully make two choices, as follows:

In the first choice, add 1 to A and subtract 1 from B. A becomes 2, and B becomes 2.

In the second choice, add 1 to C and subtract 1 from A. C becomes 1, and A becomes 1.

Sample Input 2

3 1 0 0
AB
BC
AB

Sample Output 2

No

Sample Input 3

1 0 9 0
AC

Sample Output 3

No

Sample Input 4

8 6 9 1
AC
BC
AB
BC
AC
BC
AB
AB

Sample Output 4

Yes
C
B
B
C
C
B
A
A



Sample input:
2 1 3 0
AB
AC


Sample output:
Yes
A
C

Submission status: Wrong Answer

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

sum = a + b + c
lst = []
if sum != 2:
    for _ in range(n):
        s = input()
        if s == "AB":
            if a < b:
                a += 1
                b -= 1
                lst.append("A")
            else:
                a -= 1
                b += 1
                if a < 0:
                    print("No")
                    break
                lst.append("B")
        elif s == "AC":
            if a < c:
                a += 1
                c -= 1
                lst.append("A")
            else:
                a -= 1
                c += 1
                if a < 0:
                    print("No")
                    break
                lst.append("C")
        else:
            if c < b:
                c += 1
                b -= 1
                lst.append("C")
            else:
                c -= 1
                b += 1
                if c < 0:
                    print("No")
                    break
                lst.append("B")
    else:
        print("Yes")
        for alpha in lst:
            print(alpha)
else:
    s = input()
    flag = False
    flag2 = True
    for _ in range(n - 1):
        if s == "AB":
            if a == b == 0:
                print("No")
                flag2 = False
                break
            elif a != b:
                if a > b:
                    lst.append("B")
                    a -= 1
                    b += 1
                else:
                    lst.append("A")
                    a += 1
                    b -= 1
                s = input()
                flag = True
            else:
                s = input()
                flag = False
                if s == "AB":
                    lst.append("A")
                    a = 2
                    b = 0
                elif s == "AC":
                    lst.append("A")
                    a = 2
                    b = 0
                else:
                    lst.append("B")
                    b = 2
                    a = 0
                    
        elif s == "AC":
            if a == c == 0:
                print("No")
                flag2 = False
                break
            elif a != c:
                if a > c:
                    lst.append("C")
                    a -= 1
                    c += 1
                else:
                    lst.append("A")
                    a += 1
                    c -= 1
                s = input()
                flag = True
            else:
                s = input()
                flag = False
                if s == "AC":
                    lst.append("A")
                    a = 2
                    c = 0
                elif s == "AB":
                    lst.append("A")
                    a = 2
                    c = 0
                else:
                    lst.append("C")
                    c = 2
                    a = 0
                    
        else:
            if b == c == 0:
                print("No")
                flag2 = False
                break
            elif b != c:
                if b > c:
                    lst.append("C")
                    b -= 1
                    c += 1
                else:
                    lst.append("B")
                    b += 1
                    c -= 1
                s = input()
                flag = True
            else:
                s = input()
                flag = False
                if s == "BC":
                    lst.append("B")
                    b = 2
                    c = 0
                elif s == "AB":
                    lst.append("B")
                    b = 2
                    c = 0
                else:
                    lst.append("C")
                    c = 2
                    b = 0
    else:
        if flag:
            if s == "AB":
                if a == b == 0:
                    print("No")
                    flag2 = False
                if a < b:
                    lst.append("A")
                else:
                    lst.append("B")
            elif s == "AC":
                if a == c == 0:
                    print("No")
                    flag2 = False
                if a < c:
                    lst.append("A")
                else:
                    lst.append("C")
            else:
                if b == c == 0:
                    print("No")
                    flag2 = False
                if b < c:
                    lst.append("B")
                else:
                    lst.append("C")
    if flag2:
        print("Yes")
        for alpha in lst:
            print(alpha)

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:
n,a,b,c = map(int,input().split())
flag = True
ans = []
for i in range(n):
    s = input()
    if s == "AB":
        if a == 0 and b == 0:
            flag = False
        if 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:
            flag = False
        if b >= c:
            b -= 1
            c += 1
            ans.append("C")
        else:
            b += 1
            c -= 1
            ans.append("B")
    if s == "AC":
        if a == 0 and c == 0:
            flag = False
        if a >= c:
            a -= 1
            c += 1
            ans.append("C")
        else:
            a += 1
            c -= 1
            ans.append("A")
if flag:
    print("Yes")
    for i in ans:
        print(i)
else:
    print("No")

Submission status: Wrong Answer

Solution code:
N,A,B,C = map(int,input().split())
q = [input() for i in range(N)]
ans = []
possible = True
for i in range(N):
    s = q[i]
    if s=='AB':
        if B>1:
            A += 1
            B -= 1
            ans.append('A')
        else:
            A -= 1
            B += 1
            ans.append('B')
            if A<0:
                possible = False
    if s=='AC':
        if C>1:
            A += 1
            C -= 1
            ans.append('A')
        else:
            A -= 1
            C += 1
            ans.append('C')
            if A<0:
                possible = False
    else:
        if B==0 and C==0:
            possible = False
        if B>=C:
            B -= 1
            C += 1
            ans.append('C')
        else:
            B += 1
            C -= 1
            ans.append('B')

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

Submission status: Wrong Answer

Solution code:
N,A,B,C = map(int,input().split())
ans_list= []
chk = 0
for i in range(N):
    s = input()
    if chk == 1:
        if s == "AC":
            A += 1
            B -= 1
            ans_list.append(["A"])
            chk = 0
        elif s == "BC":
            A -= 1
            B += 1
            ans_list.append(["B"])
            chk = 0
        else:
            A += 1
            B -= 1
            ans_list.append(["A"])
            chk = 0
    elif chk == 2:
        if s == "AB":
            A += 1
            C -= 1
            ans_list.append(["A"])
            chk = 0
        elif s == "BC":
            A -= 1
            C += 1
            ans_list.append(["C"])
            chk = 0
        else:
            A += 1
            C -= 1
            ans_list.append(["A"])
    elif chk == 3:
        if s == "AB":
            B += 1
            C -= 1
            ans_list.append(["B"])
            chk = 0
        elif s == "AC":
            B -= 1
            C += 1
            ans_list.append(["C"])
            chk = 0
        else:
            B += 1
            C -= 1
            ans_list.append(["B"])
    if s == "AB" and A == 0 and B == 0:
        print("No")
        break
    elif s == "BC" and B == 0 and C == 0:
        print("No")
        break
    elif s == "AC" and A == 0 and C == 0:
        print("No")
        break
    else:
        if s == "AB":
            if A == 0:
                A += 1
                B -= 1
                ans_list.append(["A"])
            elif B == 0:
                A -= 1
                B += 1
                ans_list.append(["B"])
            elif A == 1 and B != 1 :
                A += 1
                B -= 1
                ans_list.append(["A"])
            elif B == 1 and A != 1:
                A -= 1
                B += 1
                ans_list.append(["B"])
            else:
                chk = 1
        elif s == "AC":
            if A == 0:
                A += 1
                C -= 1
                ans_list.append(["A"])
            elif C == 0:
                A -= 1
                C += 1
                ans_list.append(["C"])
            elif A == 1 and B != 1 :
                A += 1
                C -= 1
                ans_list.append(["A"])
            elif B == 1 and A != 1:
                A -= 1
                C += 1
                ans_list.append(["C"])
            else:
                chk = 2
        else:
            if B == 0:
                B += 1
                C -= 1
                ans_list.append(["B"])
            elif C == 0:
                B -= 1
                C += 1
                ans_list.append(["C"])
            elif B == 1 and C != 1 :
                B += 1
                C -= 1
                ans_list.append(["B"])
            elif C == 1 and B != 1:
                B -= 1
                C += 1
                ans_list.append(["C"])
            else:
                chk = 3
if chk == 1:
        A += 1
        B -= 1
        ans_list.append(["A"])
elif chk == 2:
        A += 1
        C -= 1
        ans_list.append(["A"])
elif chk == 3:
        B += 1
        C -= 1
        ans_list.append(["B"])
if len(ans_list) == N:
    print("Yes")
    for i in range(N):
        print(*ans_list[i])

Submission status: Wrong Answer

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

L = []
ans = "Yes"
for s in S:
    if s == "AB":
        if A < B:
            A += 1
            B -= 1
            L.append("A")
        else:
            A -= 1
            B += 1
            L.append("B")
    elif s == "BC":
        if B < C:
            B += 1
            C -= 1
            L.append("B")
        else:
            B -= 1
            C += 1
            L.append("C")
    else:
        if A < C:
            A += 1
            C -= 1
            L.append("A")
        else:
            A -= 1
            C += 1
            L.append("C")
    if A*B*C < 0:
        ans = "No"
            
print(ans)
if ans == "Yes":
    for l in L:
        print(l)

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:
            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:
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:
        if i + 1 < n:
            if s1 in sx[i + 1]:
                D[s1] += 1
                D[s2] -= 1
                ans += [s1]
            else:
                ans += [s2]
                D[s2] += 1
                D[s1] -= 1
    else:
        if D[s1] == 0:
            ans += [s2]
        else:
            ans += [s1]

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

Submission status: Wrong Answer

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

ans = []
for s in S:
    if s=='AB':
        if A==0 and B==0:
            print('No')
            exit()
        if A==0:
            ans.append('A')
            A += 1
            B -= 1
        elif B==0:
            ans.append('B')
            B += 1
            A -= 1
        else:
            if A>B:
                ans.append('B')
                B += 1
                A -= 1
            else:
                ans.append('A')
                A += 1
                B -= 1
    elif s=='AC':
        if A==0 and C==0:
            print('No')
            exit()
        if A==0:
            ans.append('A')
            A += 1
            C -= 1
        elif B==0:
            ans.append('C')
            C += 1
            A -= 1
        else:
            if A>C:
                ans.append('C')
                C += 1
                A -= 1
            else:
                ans.append('A')
                A += 1
                C -= 1
    else:
        if C==0 and B==0:
            print('No')
            exit()
        if C==0:
            ans.append('C')
            C += 1
            B -= 1
        elif B==0:
            ans.append('B')
            B += 1
            C -= 1
        else:
            if C>B:
                ans.append('B')
                B += 1
                C -= 1
            else:
                ans.append('C')
                C += 1
                B -= 1
print('Yes')
print(*ans, sep='\n')

Submission status: Wrong Answer

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

ans = []
arr = [A, B, C]
for s in Ss:
    if s == 'AB':
        v1 = 0
        v2 = 1
    elif s == 'AC':
        v1 = 0
        v2 = 2
    else:
        v1 = 1
        v2 = 2
    if arr[v1] == arr[v2] == 0:
        print('No')
        exit()
    if arr[v1] < arr[v2]:
        ans.append(chr(ord('A')+v1))
        arr[v1] += 1
        arr[v2] -= 1
    else:
        ans.append(chr(ord('A')+v2))
        arr[v1] -= 1
        arr[v2] += 1
print('Yes')
for a in ans:
    print(a)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)

def game(cmds,A,B,C):
    if len(cmds)==0:
        if A>=0 and B>=0 and C>=0:
            return True,[]
        else:
            return False,[]
    cmd=cmds.pop(0)
    if cmd=="AB":
        if A<B:
            suc,his= game(cmds,A+1,B-1,C)
            if suc:
                return suc,his+["A"]
            else:
                return False,[]
        elif A>B:
            suc,his= game(cmds,A-1,B+1,C)
            if suc:
                return suc,his+["B"]
            else:
                return False,[]
        else:
            if A==0 and B==0:
                return False,[]
            g1= game(cmds,A-1,B+1,C)
            if g1[0]:
                return g1[0],g1[1]+["B"]
            g2=game(cmds,A+1,B-1,C)
            if g2[0]:
                return g2[0],g2[1]+["A"]
            else:
                return False,[]

    elif cmd=="AC":
        if A<C:
            suc,his= game(cmds,A+1,B,C-1)
            if suc:
                return suc,his+["A"]
            else:
                return False,[]
        elif A>C:
            suc,his= game(cmds,A-1,B,C+1)
            if suc:
                return suc,his+["C"]
            else:
                return False,[]
        else:
            if A==0 and C==0:
                return False,[]
            g1= game(cmds,A-1,B,C+1)
            if g1[0]:
                return g1[0],g1[1]+["C"]
            g2=game(cmds,A+1,B,C-1)
            if g2[0]:
                return g2[0],g2[1]+["A"]
            else:
                return False,[]
    else:
        if C<B:
            suc,his= game(cmds,A,B-1,C+1)
            if suc:
                return suc,his+["C"]
            else:
                return False,[]
        elif C>B:
            suc,his= game(cmds,A,B+1,C-1)
            if suc:
                return suc,his+["B"]
            else:
                return False,[]
        else:
            if C==0 and B==0:
                return False,[]
            g1= game(cmds,A,B+1,C-1)
            if g1[0]:
                return g1[0],g1[1]+["B"]
            g2=game(cmds,A,B-1,C+1)
            if g2[0]:
                return g2[0],g2[1]+["C"]
            else:
                return False,[]


N,A,B,C=[int(i) for i in input().split(" ")]
cmds=[]
for i in range(N):
    cmds.append(input())

suc,his=game(cmds,A,B,C)
if suc:
    print("Yes")
    for h in his[::-1]:
        print(h)
else:
    print("No")

Submission status: Wrong Answer

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
for i in range(n):
    temp = ri()
    if temp == "AB":
        if a==0 and b==0:
            flag = False
        else:
            if a<b:
                a+=1
                b-=1
                ans.append("A")
            else:
                a-=1
                b+=1
                ans.append("B")
    elif temp == "BC":
        if c==0 and b==0:
            flag = False
        else:
            if c<b:
                c+=1
                b-=1
                ans.append("C")
            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:
                a-=1
                c+=1
                ans.append("C")
if not flag : 
    No()
else:
    Yes()
    for i in range(n):
        print(ans[i])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n,a,b,c = map(int,input().split())
s = [input().rstrip() for i in range(n)]
def mthd(a,b,c,s):
  print("Yes")
  for i in range(n):
    if s[i] == "AB":
      if a >= b:
        print("B")
        a -= 1
        b += 1
      else:
        print("A")
        a += 1
        b -= 1
    if s[i] == "AC":
      if a >= c:
        print("C")
        a -= 1
        c += 1
      else:
        print("A")
        a += 1
        c -= 1
    if s[i] == "BC":
      if c >= b:
        print("B")
        c -= 1
        b += 1
      else:
        print("C")
        c += 1
        b -= 1
if a+b+c >= 3 and [a,b,c].count(0) <= 1:
  mthd(a,b,c,s)
else:
  if s[0] == "AB" and a+b == 0:
    print("No")
  elif s[0] == "BC" and b+c == 0:
    print("No")
  elif s[0] == "AC" and a+c == 0:
    print("No")
  elif a+b+c >= 3:
    mthd(a,b,c,s)
  elif a+b+c == 1:
    ans = []
    for i in range(n):
      if s[i] == "AB":
        if a == 1:
          ans.append("B")
        elif b == 1:
          ans.append("A")
        else:
          print("No")
          exit()
        a,b = b,a
      if s[i] == "BC":
        if c == 1:
          ans.append("B")
        elif b == 1:
          ans.append("C")
        else:
          print("No")
          exit()
        c,b = b,c
      if s[i] == "AC":
        if c == 1:
          ans.append("A")
        elif a == 1:
          ans.append("C")
        else:
          print("No")
          exit()
        c,a = a,c
    print("Yes",*ans,sep="\n")
  elif a+b+c == 2:
    print("Yes")
    for i in range(n-1):
      if s[i] == "AB":
        if a>b or s[i+1] != "AC":
          print("B")
          a -= 1
          b += 1
        elif a<b or s[i+1] == "AC":
          print("A")
          a += 1
          b -= 1
      elif s[i] == "AC":
        if a>c or s[i+1] != "AB":
          print("C")
          a -= 1
          c += 1
        elif a<c or s[i+1] == "AB":
          print("A")
          a += 1
          c -= 1
      elif s[i] == "BC":
        if c>b or s[i+1] != "AC":
          print("B")
          c -= 1
          b += 1
        elif c<b or s[i+1] == "AC":
          print("C")
          c += 1
          b -= 1
    if s[-1] == "AB":
      if b > 0:
        print("A")
      else:
        print("B")
    if s[-1] == "BC":
      if b > 0:
        print("C")
      else:
        print("B")
    if s[-1] == "AC":
      if c > 0:
        print("A")
      else:
        print("C")

Submission status: Wrong Answer

Solution code:
import sys
n, a, b, c = map(int, input().split())
s = [input() for _ in range(n)]
score = {"A":a, "B":b, "C":c}

result = []
for g in s:
    x, y = list(g)
    if score[x] < score[y]:
        score[y] -= 1
        score[x] += 1
        result.append(x)
    else:
        score[x] -= 1
        score[y] += 1
        result.append(y)
    if score[x] < 0 or score[y] < 0:
        print("No")
        sys.exit()
print("Yes")
for i in result:
    print(i)

Submission status: Wrong Answer

Solution code:
s = [0,0,0]
n,s[0],s[1],s[2] = map(int,input().split())
T = [0]*n
ABC = 'ABC'
for i in range(n):
    t = input()
    if t == 'AB':T[i] = 2
    if t == 'BC':T[i] = 0
    if t == 'AC':T[i] = 1
ans = ['' for _ in range(n)]
if s.count(0) <= 1 and sum(s) >= 3:
    for i,t in enumerate(T):
        nxt = [0,1,2]
        nxt.remove(t)
        if s[nxt[0]] < s[nxt[1]]:
            ans[i] = ABC[nxt[0]]
            s[nxt[0]] += 1
            s[nxt[1]] -= 1
        else:
            ans[i] = ABC[nxt[1]]
            s[nxt[0]] -= 1
            s[nxt[1]] += 1
    print('Yes')
    print('\n'.join(ans))
elif s.count(0) == 1:
    now = s.index(0)
    i = 0
    while i < n-1:
        if T[i] == T[i+1]:
            if T[i] == now:
                ans[i] = ABC[(now-1)%3]
                ans[i+1] = ABC[(now-2)%3]
            else:
                for j in range(3):
                    if j not in [T[i],now]:
                        ans[i+1] = ABC[j]
                ans[i] = ABC[now]
            i += 2
        elif T[i] == now:
            for j in range(3):
                if j not in [T[i+1],now]:ans[i] = ABC[j]
            ans[i+1] = ABC[T[i]]
            now = T[i+1]
            i += 2
        else:
            ans[i] = ABC[now]
            for j in range(3):
                if j not in [T[i],now]:
                    now = j
                    break
            i += 1
    if i == n-1:
        ans[i] = ABC[(T[i]-1)%3]
    print('Yes')
    print('\n'.join(ans))
else:
    for i,t in enumerate(T):
        nxt = [0,1,2]
        nxt.remove(t)
        if s[nxt[0]] < s[nxt[1]]:
            ans[i] = ABC[nxt[0]]
            s[nxt[0]] += 1
            s[nxt[1]] -= 1
        else:
            ans[i] = ABC[nxt[1]]
            s[nxt[0]] -= 1
            s[nxt[1]] += 1
        if -1 in s:
            print('No')
            break
    else:
        print('Yes')
        print('\n'.join(ans))