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

def str_to_int(ch):
	if ch =='A':
		return 0
	if ch =='B':
		return 1
	if ch =='C':
		return 2
def int_to_str(ch):
	if ch ==0:
		return 'A'
	if ch ==1:
		return 'B'
	if ch ==2:
		return 'C'

N, A, B, C = list(map(int, input().split()))
ABC =[0, 0, 0]
ABC[0] = A
ABC[1] = B
ABC[2] = C

ops = []
for _ in range(N):
	l =list(map(str_to_int, list(input())))
	ops.append(l)

if sum(ABC) == 0:
	print("No")
	sys.exit(0)

ans = []
for idx, op in enumerate(ops):
	if ABC[op[0]] == 0 and ABC[op[1]] == 0:
		print("No")
		sys.exit(0)
	elif ABC[op[1]] == 0:
		inc = op[0]
		dec = op[1]
	elif ABC[op[0]] == 0:
		dec = op[0]
		inc = op[1]
	elif idx +1 < len(ops) and op[0] in ops[idx+1]:
		dec = op[1]
		inc = op[0]
	else:
		inc = op[0]
		dec = op[1]
	ans.append(inc)
	ABC[dec]-=1
	ABC[inc]+=1
print("Yes")
for i in map(int_to_str, ans):
	print(i)
sys.exit(0)

Submission status: Wrong Answer

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


def calc(x, y):
    if y == 1:
        return (x - 1, y + 1)
    else:
        return (x + 1, y - 1)


strs = []
for i in range(n):
    strs.append(str(input()))

answers = []
for s in strs:
    old_a = a
    old_b = b
    old_c = c

    if s == 'AB':
        (a, b) = calc(a, b)
    elif s == 'AC':
        (a, c) = calc(a, c)
    elif s == 'BC':
        (b, c) = calc(b, c)

    if a > old_a:
        answers.append('A')
    elif b > old_b:
        answers.append('B')
    elif c > old_c:
        answers.append('C')

    if a <= 0 or b <= 0 or c <= 0:
        print('No')
        quit()

print('Yes')
for ans in answers:
    print(ans)

Submission status: Wrong Answer

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


def calc(x, y):
    if y == 1:
        return (x - 1, y + 1)
    else:
        return (x + 1, y - 1)


strs = []
for i in range(n):
    strs.append(str(input()))

answers = []
for s in strs:
    print(s)
    old_a = a
    old_b = b
    old_c = c

    if s == 'AB':
        (a, b) = calc(a, b)
    elif s == 'AC':
        (a, c) = calc(a, c)
    elif s == 'BC':
        (b, c) = calc(b, c)

    if a > old_a:
        answers.append('A')
    elif b > old_b:
        answers.append('B')
    elif c > old_c:
        answers.append('C')

    if a <= 0 or b <= 0 or c <= 0:
        print('No')
        quit()

print('Yes')
for ans in answers:
    print(ans)

Submission status: Wrong Answer

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


def calc(x, y):
    if y == 1:
        return (x - 1, y + 1)
    else:
        return (x + 1, y - 1)


answers = []
for i in range(n):
    strs = list(map(str, input().split()))

    for s in strs:
        old_a = a
        old_b = b
        old_c = c

        if s == 'AB':
            (a, b) = calc(a, b)
        elif s == 'AC':
            (a, c) = calc(a, c)
        elif s == 'BC':
            (b, c) = calc(b, c)

        if a > old_a:
            answers.append('A')
        elif b > old_b:
            answers.append('B')
        elif c > old_c:
            answers.append('C')

        if a <= 0 or b <= 0 or c <= 0:
            print('No')
            quit()


print('Yes')
for ans in answers:
    print(ans)

Submission status: Wrong Answer

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


def calc(x, y):
    if x > y:
        return (x - 1, y + 1)
    else:
        return (x + 1, y - 1)


answers = []
for i in range(n):
    strs = list(map(str, input().split()))

    for s in strs:
        old_a = a
        old_b = b
        old_c = c

        if s == 'AB':
            (a, b) = calc(a, b)
        elif s == 'AC':
            (a, c) = calc(a, c)
        elif s == 'BC':
            (b, c) = calc(b, c)

        if a > old_a:
            answers.append('A')
        elif b > old_b:
            answers.append('B')
        elif c > old_c:
            answers.append('C')

        if a <= 0 or b <= 0 or c <= 0:
            print('No')
            quit()


print('Yes')
for ans in answers:
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N, A, B, C = map(int, input().split())
S = [input() for _ in range(N)]
d = Counter(S)
ans = []
for s in S:
    if s == "AB":
        if A == 0:
            A += 1
            B -= 1
            ans.append("A")
        elif B == 0:
            A -= 1
            B += 1
            ans.append("B")
        else:
            if d["BC"] < d["AC"]:
                A += 1
                B -= 1
                ans.append("A")
            else:
                A -= 1
                B += 1
                ans.append("B")
        d["AB"] -= 1
        if A < 0 or B < 0:
            print("No")
            exit()
    elif s == "BC":
        if B == 0:
            B += 1
            C -= 1
            ans.append("B")
        elif C == 0:
            B -= 1
            C += 1
            ans.append("C")
        else:
            if d["AB"] < d["AC"]:
                B += 1
                C -= 1
                ans.append("B")
            else:
                B -= 1
                C += 1
                ans.append("C")
        d["BC"] -=1
        if B < 0 or C < 0:
            print("No")
            exit()
    else:
        if C == 0:
            C += 1
            A -= 1
            ans.append("C")
        elif A == 0:
            C -= 1
            A += 1
            ans.append("A")
        else:
            if d["AB"] < d["BC"]:
                A += 1
                C -= 1
                ans.append("A")
            else:
                A -= 1
                C += 1
                ans.append("C")
        d["AC"] -= 1
        if A < 0 or C < 0:
            print("No")
            exit()
print("Yes")
for x in ans:
    print(x)

Submission status: Wrong Answer

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


def calc(x, y):
    if x > y:
        return (x - 1, y + 1)
    else:
        return (x + 1, y + 1)


answers = []
for i in range(n):
    strs = list(map(str, input().split()))

    for s in strs:
        old_a = a
        old_b = b
        old_c = c

        if s == 'AB':
            (a, b) = calc(a, b)
        elif s == 'AC':
            (a, c) = calc(a, c)
        elif s == 'BC':
            (b, c) = calc(b, c)

        if a > old_a:
            answers.append('A')
        elif b > old_b:
            answers.append('B')
        elif c > old_c:
            answers.append('C')

        if a <= 0 or b <= 0 or c <= 0:
            print('No')
            quit()


print('Yes')
for ans in answers:
    print(ans)

Submission status: Wrong Answer

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


def calc(x, y):
    if x - 1 >= 0:
        return (x - 1, y + 1)
    else:
        return (x + 1, y - 1)


answers = []
for i in range(n):
    strs = list(map(str, input().split()))

    for s in strs:
        old_a = a
        old_b = b
        old_c = c

        if s == 'AB':
            (a, b) = calc(a, b)
        elif s == 'AC':
            (a, c) = calc(a, c)
        elif s == 'BC':
            (b, c) = calc(b, c)

        if a > old_a:
            answers.append('A')
        elif b > old_b:
            answers.append('B')
        elif c > old_c:
            answers.append('C')

        if a <= 0 or b <= 0 or c <= 0:
            print('No')
            quit()


print('Yes')
for ans in answers:
    print(ans)

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int, input().split())
S = [input() for _ in range(N)]
ans = []
for s in S:
    if s == "AB":
        if A < B:
            A += 1
            B -= 1
            ans.append("A")
        else:
            A -= 1
            B += 1
            ans.append("B")
        if A < 0 or B < 0:
            print("No")
            exit()
    elif s == "BC":
        if B < C:
            B += 1
            C -= 1
            ans.append("B")
        else:
            B -= 1
            C += 1
            ans.append("C")
        if B < 0 or C < 0:
            print("No")
            exit()
    else:
        if A < C:
            A += 1
            C -= 1
            ans.append("A")
        else:
            A -= 1
            C += 1
            ans.append("C")
        if A < 0 or C < 0:
            print("No")
            exit()
print("Yes")
for x in ans:
    print(x)

Submission status: Wrong Answer

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


def calc(x, y):
    if x - 1 >= 0:
        return (x - 1, y + 1)
    elif y - 1 >= 0:
        return (x + 1, y - 1)


answers = []
for i in range(n):
    s = list(map(str, input().split()))
    old_a = a
    old_b = b
    old_c = c

    if s == 'AB':
        a, b = calc(a, b)
    elif s == 'AC':
        a, c = calc(a, c)
    elif s == 'BC':
        b, c = calc(b, c)

    if a > old_a:
        answers.append('A')
    elif b > old_b:
        answers.append('B')
    elif c > old_c:
        answers.append('C')

    if a <= 0 and b <= 0 and c <= 0:
        print('No')
        quit()


print('Yes')
for ans in answers:
    print(ans)

Submission status: Wrong Answer

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

# process
V = {"A":A, "B":B, "C":C}
S.append("XX")
op = ""
for i in range(N):
    s0, s1 = S[i]
    if V[s0] + V[s1] == 0:
        break
    
    x, y = s0, s1
    if V[s1] == 0 or (s0 != 0 and s1 in S[i+1]):
        x, y = s1, s0

    V[x] += 1
    V[y] -= 1
    op += x

# output
if len(op) != N:
    print("No")
else:
    print("Yes")
    [print(x) for x in op]

Submission status: Wrong Answer

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


def calc(x, y):
    if x - 1 >= 0:
        return (x - 1, y)
    elif y - 1 >= 0:
        return (x, y - 1)


answers = []
for i in range(n):
    s = list(map(str, input().split()))
    old_a = a
    old_b = b
    old_c = c

    if s == 'AB':
        a, b = calc(a, b)
    elif s == 'AC':
        a, c = calc(a, c)
    elif s == 'BC':
        b, c = calc(b, c)

    if a > old_a:
        answers.append('A')
    elif b > old_b:
        answers.append('B')
    elif c > old_c:
        answers.append('C')

    if a <= 0 and b <= 0 and c <= 0:
        print('No')
        quit()


print('Yes')
for ans in answers:
    print(ans)

Submission status: Wrong Answer

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


def calc(x, y):
    if x - 1 >= 0:
        return (x - 1, y)
    elif y - 1 >= 0:
        return (x, y - 1)


answers = []
for i in range(n):
    s = list(map(str, input().split()))
    old_a = a
    old_b = b
    old_c = c

    if s == 'AB':
        a, b = calc(a, b)
    elif s == 'AC':
        a, c = calc(a, c)
    elif s == 'BC':
        b, c = calc(b, c)

    if a > old_a:
        answers.append('A')
    elif b > old_b:
        answers.append('B')
    elif c > old_c:
        answers.append('C')

    if a >= 0 and b >= 0 and c >= 0:
        print('No')
        quit()


print('Yes')
for ans in answers:
    print(ans)

Submission status: Wrong Answer

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

# process
V = {"A":A, "B":B, "C":C}
S.append("XX")
op = ""
for i in range(N):
    s0, s1 = S[i]
    if V[s0] + V[s1] == 0:
        break
    
    x, y = s0, s1
    if V[s1] == 0 or (i < N-1 and s1 in S[i+1]):
        x, y = s1, s0

    V[x] += 1
    V[y] -= 1
    op += x

# output
if len(op) != N:
    print("No")
else:
    print("Yes")
    [print(x) for x in op]

Submission status: Wrong Answer

Solution code:
n,a,b,c = list(map(int,input().split()))
d = [0]*n
s = []
f = [a,b,c]
if a == b == c == 0:
    print("No")
    exit()

for i in range(n):
    ipt = input()
    ipt = ipt.replace('A','0')
    ipt = ipt.replace('B','1')
    ipt = ipt.replace('C','2')
    s.append(list(ipt))

for i in range(n):
    add = -1
    a,b = list(map(int,s[i]))
    if f[a]>0 and f[b]>0:
        if f[a] >= f[b]:
            f[a] -= 1
            f[b] += 1
            add = b
        else:
            f[a] += 1
            f[b] -= 1
            add = a
    elif f[a]>0:
        f[a] -= 1
        f[b] += 1
        add = b
    elif f[b] > 0:
        f[a] += 1
        f[b] -= 1
        add = a
    else:
        print("No")
        exit()
    d[i] = add
print("Yes")
for i in range(n):
    print(chr(65+d[i]))

Submission status: Wrong Answer

Solution code:
N, *ABC = map(int, input().split())
s = [input() for _ in range(N)]

yes_flag = True
ans_list = []

if sum(ABC) == 0:
    yes_flag = False

for i in range(N):
    if not yes_flag:
        break
    u = ord(s[i][0]) - 65
    v = ord(s[i][1]) - 65
    if ABC[u] + ABC[v] == 0:
        yes_flag = False
    elif ABC[u] + ABC[v] == 1:
        if ABC[u] == 1:
            ans_list += [s[i][0]]
            ABC[u] -= 1
            ABC[v] += 1
        else:
            ans_list += [s[i][1]]
            ABC[v] -= 1
            ABC[u] += 1
    elif ABC[u] + ABC[v] == 2:
        # 両方1の場合だけケアが必要
        if ABC[u] * ABC[v] == 1:
            if i == N-1:
                if ABC[u] > 0:
                    ans_list += [s[i][0]]
                else:
                    ans_list += [s[i][1]]
            else:
                if s[i][1] in s[i+1]:
                    ans_list += [s[i][0]]
                    ABC[u] -= 1
                    ABC[v] += 1
                else:
                    ans_list += [s[i][1]]
                    ABC[v] -= 1
                    ABC[u] += 1
        else:
            if ABC[u] > 1:
                ans_list += [s[i][0]]
                ABC[u] -= 1
                ABC[v] += 1
            else:
                ans_list += [s[i][1]]
                ABC[v] -= 1
                ABC[u] += 1
    else:
        if ABC[u] > 1:
            ans_list += [s[i][0]]
            ABC[u] -= 1
            ABC[v] += 1
        else:
            ans_list += [s[i][1]]
            ABC[v] -= 1
            ABC[u] += 1

if yes_flag:
    print("Yes")
    print(*ans_list)
else:
    print("No")

Submission status: Wrong Answer

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

# process
a, b, c = A, B, C
op = ""
for i, s in enumerate(S):
    s0 = s[0]
    s1 = s[1]
    if V[s0] + V[s1] == 0:
        break
    
    x, y = s0, s1
    if V[s1] == 0 or (i < N-1 and s1 in S[i+1]):
        x, y = s1, s0

    V[x] += 1
    V[y] -= 1
    op += x

    # print("{} {} {}".format(s, V, op))

# output
if len(op) != N:
    print("No")
else:
    print("Yes")
    [print(x) for x in op]

Submission status: Wrong Answer

Solution code:
import sys

def str_to_int(ch):
	if ch =='A':
		return 0
	if ch =='B':
		return 1
	if ch =='C':
		return 2
def int_to_str(ch):
	if ch ==0:
		return 'A'
	if ch ==1:
		return 'B'
	if ch ==2:
		return 'C'

N, A, B, C = list(map(int, input().split()))
ABC =[0, 0, 0]
ABC[0] = A
ABC[1] = B
ABC[2] = C

Ss = []
for _ in range(N):
	l =list(map(str_to_int, list(input())))
	Ss.append(l)

if sum(ABC) == 0:
	print("No")
	sys.exit(0)

ans = []
for idx, S in enumerate(Ss):
	if ABC[S[0]] == 0 and ABC[S[1]] == 0:
		print("No")
		sys.exit(0)
	elif ABC[S[1]] == 0:
		inc = S[0]
		dec = S[1]
	elif ABC[S[0]] == 0:
		dec = S[0]
		inc = S[1]
	elif idx +1 < len(Ss) and S[0] in Ss[idx+1]:
		dec = S[0]
		inc = S[1]
	else:
		inc = S[1]
		dec = S[0]
	ans.append(inc)
	ABC[dec]-=1
	ABC[inc]+=1
print("Yes")
for i in map(int_to_str, ans):
	print(i)
sys.exit(0)

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter
def str_to_int(ch):
	if ch =='A':
		return 0
	if ch =='B':
		return 1
	if ch =='C':
		return 2
def int_to_str(ch):
	if ch ==0:
		return 'A'
	if ch ==1:
		return 'B'
	if ch ==2:
		return 'C'
N, A, B, C = map(int, input().split())
ABC =[0, 0, 0]
ABC[0] = A
ABC[1] = B
ABC[2] = C

Ss = []
for _ in range(N):
	l =list(map(str_to_int, list(input())))
	Ss.append(l)


if sum(ABC) == 0:
	print("No")
	sys.exit(0)
if sum(ABC) == 1:
	# 一位
	ans = []
	for S in Ss:
		if ABC[S[0]] > 0 and ABC[S[1]] == 0:
			ans.append(S[1])
			ABC[S[0]]-=1
			ABC[S[1]]+=1
		elif ABC[S[0]] == 0 and ABC[S[1]] > 0:
			ans.append(S[0])
			ABC[S[0]]+=1
			ABC[S[1]]-=1
		elif ABC[S[0]] == 0 and ABC[S[1]] == 0:
			print("No")
			sys.exit(0)
	print("Yes")
	for i in map(int_to_str, ans):
		print(i)
	sys.exit(0)
if sum(ABC)>=2:
	ans = []
	for idx, S in enumerate(Ss):
		if ABC[S[0]] == 0 and ABC[S[1]] == 0:
			print("No")
			sys.exit(0)
		elif ABC[S[1]] == 0:
			n = S[0]
			m = S[1]
		elif ABC[S[0]] == 0:
			m = S[0]
			n = S[1]
		elif idx +1 < len(Ss) and S[0] in Ss[idx+1]:
			m = S[0]
			n = S[1]
		else:
			n = S[1]
			m = S[0]
		ans.append(m)
		ABC[n]-=1
		ABC[m]+=1
	print("Yes")
	for i in map(int_to_str, ans):
		print(i)
	sys.exit(0)

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter
def str_to_int(ch):
	if ch =='A':
		return 0
	if ch =='B':
		return 1
	if ch =='C':
		return 2
def int_to_str(ch):
	if ch ==0:
		return 'A'
	if ch ==1:
		return 'B'
	if ch ==2:
		return 'C'
N, A, B, C = map(int, input().split())
ABC =[0, 0, 0]
ABC[0] = A
ABC[1] = B
ABC[2] = C

Ss = []
for _ in range(N):
	l =list(map(str_to_int, list(input())))
	Ss.append(l)


if sum(ABC) == 0:
	print("No")
	sys.exit(0)
if sum(ABC) == 1:
	# 一位
	ans = []
	for S in Ss:
		if ABC[S[0]] > 0 and ABC[S[1]] == 0:
			ans.append(S[1])
			ABC[S[0]]-=1
			ABC[S[1]]+=1
		elif ABC[S[0]] == 0 and ABC[S[1]] > 0:
			ans.append(S[0])
			ABC[S[0]]+=1
			ABC[S[1]]-=1
		elif ABC[S[0]] == 0 and ABC[S[1]] == 0:
			print("No")
			sys.exit(0)
	print("Yes")
	for i in map(int_to_str, ans):
		print(i)
	sys.exit(0)
if sum(ABC)>=2:
	ans = []
	for idx, S in enumerate(Ss):
		if ABC[S[0]]==1 and ABC[S[1]]==1:
			if S == Ss[idx+1]:
				if ABC[S[0]] > ABC[S[1]]:
					n = S[0]
					m = S[1]
				else:
					n = S[1]
					m = S[0]

			elif S[0] == Ss[idx+1][0] or S[0] == Ss[idx+1][1]:
				n = S[0]
				m = S[1]
			elif S[1] == Ss[idx+1][0] or S[1] == Ss[idx+1][1]:
				n = S[1]
				m = S[0]
			else:
				raise Exception()

			ans.append(m)
			ABC[S[0]]-=n
			ABC[S[1]]+=m
		elif ABC[S[0]] == 0 and ABC[S[1]] == 0:
			print("No")
			sys.exit(0)
		elif ABC[S[0]]>=ABC[S[1]]:
			ans.append(S[1])
			ABC[S[0]]-=1
			ABC[S[1]]+=1
		elif ABC[S[0]]<ABC[S[1]]:
			ans.append(S[0])
			ABC[S[0]]+=1
			ABC[S[1]]-=1
	print("Yes")
	for i in map(int_to_str, ans):
		print(i)
	sys.exit(0)
	if ABC[Ss[0][0]] +  ABC[Ss[0][1]] == 0:
		print("No")
		sys.exit(0)

if sum(ABC)>2:
	ans = []
	for idx, S in enumerate(Ss):

		if ABC[S[0]]>=ABC[S[1]]:
			ans.append(S[1])
			ABC[S[0]]-=1
			ABC[S[1]]+=1
		elif ABC[S[0]]<ABC[S[1]]:
			ans.append(S[0])
			ABC[S[0]]+=1
			ABC[S[1]]-=1
	print("Yes")
	for i in map(int_to_str, ans):
		print(i)
	sys.exit(0)

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter
def str_to_int(ch):
	if ch =='A':
		return 0
	if ch =='B':
		return 1
	if ch =='C':
		return 2
def int_to_str(ch):
	if ch ==0:
		return 'A'
	if ch ==1:
		return 'B'
	if ch ==2:
		return 'C'
N, A, B, C = map(int, input().split())
ABC =[0, 0, 0]
ABC[0] = A
ABC[1] = B
ABC[2] = C

Ss = []
for _ in range(N):
	l =list(map(str_to_int, list(input())))
	Ss.append(l)


if sum(ABC) == 0:
	print("No")
	sys.exit(0)
if sum(ABC) == 1:
	# 一位
	ans = []
	for S in Ss:
		if ABC[S[0]] > 0 and ABC[S[1]] == 0:
			ans.append(S[1])
			ABC[S[0]]-=1
			ABC[S[1]]+=1
		elif ABC[S[0]] == 0 and ABC[S[1]] > 0:
			ans.append(S[0])
			ABC[S[0]]+=1
			ABC[S[1]]-=1
		elif ABC[S[0]] == 0 and ABC[S[1]] == 0:
			print("No")
			sys.exit(0)
	print("Yes")
	for i in map(int_to_str, ans):
		print(i)
	sys.exit(0)
if sum(ABC)>=2:
	ans = []
	for idx, S in enumerate(Ss):
		
		if ABC[S[0]]==1 and ABC[S[1]]==1:
			if S[0] == Ss[idx+1][0] or S[0] == Ss[idx+1][1]:
				n = S[0]
				m = S[1]
			elif S[1] == Ss[idx+1][0] or S[1] == Ss[idx+1][1]:
				n = S[1]
				m = S[0]
			else:
				raise Exception()

			ans.append(m)
			ABC[S[0]]-=n
			ABC[S[1]]+=m
		elif ABC[S[0]] == 0 and ABC[S[1]] == 0:
			print("No")
			sys.exit(0)
		elif ABC[S[0]]>=ABC[S[1]]:
			ans.append(S[1])
			ABC[S[0]]-=1
			ABC[S[1]]+=1
		elif ABC[S[0]]<ABC[S[1]]:
			ans.append(S[0])
			ABC[S[0]]+=1
			ABC[S[1]]-=1
	print("Yes")
	for i in map(int_to_str, ans):
		print(i)
	sys.exit(0)
	if ABC[Ss[0][0]] +  ABC[Ss[0][1]] == 0:
		print("No")
		sys.exit(0)

if sum(ABC)>2:
	ans = []
	for idx, S in enumerate(Ss):

		if ABC[S[0]]>=ABC[S[1]]:
			ans.append(S[1])
			ABC[S[0]]-=1
			ABC[S[1]]+=1
		elif ABC[S[0]]<ABC[S[1]]:
			ans.append(S[0])
			ABC[S[0]]+=1
			ABC[S[1]]-=1
	print("Yes")
	for i in map(int_to_str, ans):
		print(i)
	sys.exit(0)

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
s=list(input() for i in range(n))+["XX"]
ans=["Yes"]

if a+b+c==0:
  del ans[:]
  ans+=["No"]
  
if a+b+c==1:
  for j in range(n):
    
    if s[j]=="AB":
      if c==1:
        del ans[:]
        ans+=["No"]
        break        
      elif a==1:
        a-=1
        b+=1
        ans+=["B"]
      elif b==1:
        a+=1
        b-=1
        ans+=["A"]

    elif s[j]=="AC":
      if b==1:
        del ans[:]
        ans+=["No"]
        break       
      elif a==1:
        a-=1
        c+=1
        ans+=["C"]
      elif c==1:
        a+=1
        c-=1
        ans+=["A"] 

    elif s[j]=="BC":
      if a==1:
        del ans[:]
        ans+=["No"]
        break
      elif b==1:
        b-=1
        c+=1
        ans+="C"
      elif c==1:
        b+=1
        c-=1
        ans+=["B"]  

if a+b+c==2:
  for j in range(n):
    
    if s[j]=="AB":
      if a==2:
        a-=1
        b+=1
        ans+=["B"]
      elif b==2:
        a+=1
        b-=1
        ans+=["A"]
      elif c==2:
        del ans[:]
        ans+=["No"]
        break     
      elif a==1 and b==1:
        if s[j+1]=="AB" or "AC" or "XX":
          a+=1
          b-=1
          ans+=["A"]
        if s[j+1]=="BC":
          a-=1
          b+=1
          ans+=["B"]
      elif a==1 and c==1:
        a-=1
        b+=1
        ans+=["B"]
      elif b==1 and c==1:
        a+=1
        b-=1
        ans+=["A"]
    
    elif s[j]=="AC":
      if a==2:
        a-=1
        c+=1
        ans+=["C"]
      elif c==2:
        a+=1
        c-=1
        ans+=["A"] 
      elif b==2:
        del ans[:]
        ans+=["No"]
        break     
      elif a==1 and c==1:
        if s[j+1]=="AB" or "AC" or "XX":
          a+=1
          c-=1
          ans+=["A"]
        if s[j+1]=="BC":
          a-=1
          c+=1
          ans+=["C"]
      elif a==1 and b==1:
        a-=1
        c+=1
        ans+=["C"]
      elif b==1 and c==1:
        a+=1
        c-=1
        ans+=["A"]

    elif s[j]=="BC":
      if c==2:
        c-=1
        b+=1
        ans+=["B"]
      elif b==2:
        c+=1
        b-=1
        ans+=["C"] 
      elif a==2:
        del ans[:]
        ans+=["No"]
        break     
      elif c==1 and b==1:
        if s[j+1]=="AB" or "BC" or "XX":
          b+=1
          c-=1
          ans+=["B"]
        if s[j+1]=="AC":
          b-=1
          c+=1
          ans+=["C"]
      elif a==1 and c==1:
        c-=1
        b+=1
        ans+=["B"]
      elif b==1 and a==1:
        c+=1
        b-=1
        ans+=["C"]        

if a+b+c>=3:
  if s[0]=="AB" and a+b==0 or s[0]=="AC" and a+c==0 or s[0]=="BC" and b+c==0:
    del ans[:]
    ans+=["No"]
  
  else:
    for j in range(n):
      if s[j]=="AB":
        if a>b:
          a-=1
          b+=1
          ans+=["B"]
        elif a<=b:
          a+=1
          b-=1
          ans+=["A"]
        
      elif s[j]=="AC":
        if a>c:
          a-=1
          c+=1
          ans+=["C"]
        elif a<=c:
          a+=1
          c-=1
          ans+=["A"]     

      elif s[j]=="BC":
        if b>c:
          b-=1
          c+=1
          ans+=["C"]
        elif b<=c:
          b+=1
          c-=1
          ans+=["B"]

for an in ans:
  print(an)

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
s=list(input() for i in range(n))+["XX"]
ans=["Yes"]
if a+b+c==0:
  del ans[:]
  ans+=["No"]
  
if a+b+c==1:
  for j in range(n):
    
    if s[j]=="AB":
      if c==1:
        del ans[:]
        ans+=["No"]
        break        
      elif a==1:
        a-=1
        b+=1
        ans+=["B"]
      elif b==1:
        a+=1
        b-=1
        ans+=["A"]

    if s[j]=="AC":
      if b==1:
        del ans[:]
        ans+=["No"]
        break       
      elif a==1:
        a-=1
        c+=1
        ans+=["C"]
      elif c==1:
        a+=1
        c-=1
        ans+=["A"] 

    if s[j]=="BC":
      if a==1:
        del ans[:]
        ans+=["No"]
        break
      elif b==1:
        b-=1
        c+=1
        ans+="C"
      elif c==1:
        b+=1
        c-=1
        ans+=["B"]  

if a+b+c==2:
  for j in range(n):
    
    if s[j]=="AB":
      if a==2:
        a-=1
        b+=1
        ans+=["B"]
      elif b==2:
        a+=1
        b-=1
        ans+=["A"]
      elif c==2:
        del ans[:]
        ans+=["No"]
        break     
      elif a==1 and b==1:
        if s[j+1]=="AB" or "AC" or "XX":
          a+=1
          b-=1
          ans+=["A"]
        if s[j+1]=="BC":
          a-=1
          b+=1
          ans+=["B"]
      elif a==1 and c==1:
        a-=1
        b+=1
        ans+=["B"]
      elif b==1 and c==1:
        a+=1
        b-=1
        ans+=["A"]
    
    if s[j]=="AC":
      if a==2:
        a-=1
        c+=1
        ans+=["C"]
      elif c==2:
        a+=1
        c-=1
        ans+=["A"] 
      elif b==2:
        del ans[:]
        ans+=["No"]
        break     
      elif a==1 and c==1:
        if s[j+1]=="AB" or "AC" or "XX":
          a+=1
          c-=1
          ans+=["A"]
        if s[j+1]=="BC":
          a-=1
          c+=1
          ans+=["C"]
      elif a==1 and b==1:
        a-=1
        c+=1
        ans+=["C"]
      elif b==1 and c==1:
        a+=1
        c-=1
        ans+=["A"]

    if s[j]=="BC":
      if c==2:
        c-=1
        b+=1
        ans+=["B"]
      elif b==2:
        c+=1
        b-=1
        ans+=["C"] 
      elif a==2:
        del ans[:]
        ans+=["No"]
        break     
      elif c==1 and b==1:
        if s[j+1]=="AB" or "BC" or "XX":
          b+=1
          c-=1
          ans+=["B"]
        if s[j+1]=="AC":
          b-=1
          c+=1
          ans+=["C"]
      elif a==1 and c==1:
        c-=1
        b+=1
        ans+=["B"]
      elif b==1 and a==1:
        c+=1
        b-=1
        ans+=["C"]        

if a+b+c>=3:
  if s[0]=="AB" and a+b==0 or s[0]=="AC" and a+c==0 or s[0]=="BC" and b+c==0:
    del ans[:]
    ans+=["No"]
  
  else:
    for j in range(n):
      if s[j]=="AB":
        if a>b:
          a-=1
          b+=1
          ans+=["B"]
        elif a<=b:
          a+=1
          b-=1
          ans+=["A"]
        
      if s[j]=="AC":
        if a>c:
          a-=1
          c+=1
          ans+=["C"]
        elif a<=c:
          a+=1
          c-=1
          ans+=["A"]     

      if s[j]=="BC":
        if b>c:
          b-=1
          c+=1
          ans+=["C"]
        elif b<=c:
          b+=1
          c-=1
          ans+=["B"]

for an in ans:
  print(an)

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
s=list(input() for i in range(n))+["XX"]
ans=["Yes"]
if a+b+c==0:
  del ans[:]
  ans+=["No"]
  
if a+b+c==1:
  for j in range(n):
    
    if s[j]=="AB":
      if c==1:
        del ans[:]
        ans+=["No"]
        break        
      elif a==1:
        a-=1
        b+=1
        ans+="B"
      elif b==1:
        a+=1
        b-=1
        ans+="A"

    if s[j]=="AC":
      if b==1:
        del ans[:]
        ans+=["No"]
        break       
      elif a==1:
        a-=1
        c+=1
        ans+="C"
      elif c==1:
        a+=1
        c-=1
        ans+="A" 

    if s[j]=="BC":
      if a==1:
        del ans[:]
        ans+=["No"]
        break
      elif b==1:
        b-=1
        c+=1
        ans+="C"
      elif c==1:
        b+=1
        c-=1
        ans+="B"  

if a+b+c==2:
  for j in range(n):
    
    if s[j]=="AB":
      if a==2:
        a-=1
        b+=1
        ans+="B"
      elif b==2:
        a+=1
        b-=1
        ans+="A" 
      elif c==2:
        del ans[:]
        ans+=["No"]
        break     
      elif a==1 and b==1:
        if s[j+1]=="AB" or "AC" or "XX":
          a+=1
          b-=1
          ans+="A"
        if s[j+1]=="BC":
          a-=1
          b+=1
          ans+="B"
      elif a==1 and c==1:
        a-=1
        b+=1
        ans+="B"
      elif b==1 and c==1:
        a+=1
        b-=1
        ans+="A"
    
    if s[j]=="AC":
      if a==2:
        a-=1
        c+=1
        ans+="C"
      elif c==2:
        a+=1
        c-=1
        ans+="A" 
      elif b==2:
        del ans[:]
        ans+=["No"]
        break     
      elif a==1 and c==1:
        if s[j+1]=="AB" or "AC" or "XX":
          a+=1
          c-=1
          ans+="A"
        if s[j+1]=="BC":
          a-=1
          c+=1
          ans+="C"
      elif a==1 and b==1:
        a-=1
        c+=1
        ans+="C"
      elif b==1 and c==1:
        a+=1
        c-=1
        ans+="A"

    if s[j]=="BC":
      if c==2:
        c-=1
        b+=1
        ans+="B"
      elif b==2:
        c+=1
        b-=1
        ans+="C" 
      elif a==2:
        del ans[:]
        ans+=["No"]
        break     
      elif c==1 and b==1:
        if s[j+1]=="AB" or "BC" or "XX":
          b+=1
          c-=1
          ans+="B"
        if s[j+1]=="AC":
          b-=1
          c+=1
          ans+="C"
      elif a==1 and c==1:
        c-=1
        b+=1
        ans+="B"
      elif b==1 and a==1:
        c+=1
        b-=1
        ans+="C"        

if a+b+c>=3:
  if s[0]=="AB" and a+b==0 or s[0]=="AC" and a+c==0 or s[0]=="BC" and b+c==0:
    del ans[:]
    ans+=["No"]
  
  else:
    for j in range(n):
      if s[j]=="AB":
        if a>b:
          a-=1
          b+=1
          ans+="B"
        elif a<=b:
          a+=1
          b-=1
          ans+="A"
        
      if s[j]=="AC":
        if a>c:
          a-=1
          c+=1
          ans+="C"
        elif a<=c:
          a+=1
          c-=1
          ans+="A"     

      if s[j]=="BC":
        if b>c:
          b-=1
          c+=1
          ans+="C"
        elif b<=c:
          b+=1
          c-=1
          ans+="B"
for an in ans:
  print(an)