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:
from collections import defaultdict

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

if a + b + c == 0:
    print("No")
    exit(0)

ans = [None] * n
if a + b + c == 1:
    for i in range(n):
        if s[i] == 'AB':
            if a == 0 and b == 0:
                print('No')
                exit(0)
            if b == 0:
                b += 1
                a -= 1
                ans[i] = 'B'
            else:
                a += 1
                b -= 1
                ans[i] = 'A'
        elif s[i] == 'BC':
            if b == 0 and c == 0:
                print('No')
                exit(0)
            if c == 0:
                b -= 1
                c += 1
                ans[i] = 'C'
            else:
                b += 1
                c -= 1
                ans[i] = 'B'
        else:
            if a == 0 and c == 0:
                print('No')
                exit(0)
            if c == 0:
                a -= 1
                c += 1
                ans[i] = 'C'
            else:
                a += 1
                c -= 1
                ans[i] = 'A'

    print("Yes")
    for i in range(n):
        print(ans[i])
    exit(0)

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

print("Yes")
for i in range(n):
    if s[i] == 'AB':
        if a == 0:
            print('A')
            a += 1
            b -= 1
            continue
        elif b == 0:
            print('B')
            a -= 1
            b += 1
            continue
    elif s[i] == 'BC':
        if b == 0:
            b += 1
            c -= 1
            print('B')
            continue
        elif c == 0:
            b -= 1
            c += 1
            print('C')
            continue
    else:
        if a == 0:
            a += 1
            c -= 1
            print('A')
            continue
        elif c == 0:
            a -= 1
            c += 1
            print('C')
            continue
    
    if a + b + c == 2 and i < n - 1 and s[i] != s[i + 1]:
        if s[i] == 'AB' and a == 1 and b == 1:
            if s[i + 1] == 'AC':
                a += 1
                b -= 1
                print('A')
            else:
                a -= 1
                b += 1
                print('B')
        elif s[i] == 'BC' and b == 1 and c == 1:
            if s[i + 1] == 'AB':
                b += 1
                c -= 1
                print('B')
            else:
                b -= 1
                c += 1
                print('C')
        elif s[i] == 'AC' and a == 1 and c == 1:
            if s[i + 1] == 'AB':
                a += 1
                c -= 1
                print('A')
            else:
                a -= 1
                c += 1
                print('C')
    
    if s[i] == 'AB':
        if a < b:
            a += 1
            b -= 1
            print('A')
        else:
            a -= 1
            b += 1
            print('B')
    elif s[i] == 'BC':
        if b < c:
            b += 1
            c -= 1
            print('B')
        else:
            b -= 1
            c += 1
            print('C')
    else:
        if a < c:
            a += 1
            c -= 1
            print('A')
        else:
            a -= 1
            c += 1
            print('C')

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

if a + b + c == 0:
    print("No")
    exit(0)

ans = [None] * n
if a + b + c == 1:
    for i in range(n):
        if s[i] == 'AB':
            if a == 0 and b == 0:
                print('No')
                exit(0)
            if b == 0:
                b += 1
                a -= 1
                ans[i] = 'B'
            else:
                a += 1
                b -= 1
                ans[i] = 'A'
        elif s[i] == 'BC':
            if b == 0 and c == 0:
                print('No')
                exit(0)
            if c == 0:
                b -= 1
                c += 1
                ans[i] = 'C'
            else:
                b += 1
                c -= 1
                ans[i] = 'B'
        else:
            if a == 0 and c == 0:
                print('No')
                exit(0)
            if c == 0:
                a -= 1
                c += 1
                ans[i] = 'C'
            else:
                a += 1
                c -= 1
                ans[i] = 'A'

    print("Yes")
    for i in range(n):
        print(ans[i])
    exit(0)

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

print("Yes")
for i in range(n):
    if s[i] == 'AB':
        if a == 0:
            print('A')
            a += 1
            b -= 1
            continue
        elif b == 0:
            print('B')
            a -= 1
            b += 1
            continue
    elif s[i] == 'BC':
        if b == 0:
            b += 1
            c -= 1
            print('B')
            continue
        elif c == 0:
            b -= 1
            c += 1
            print('C')
            continue
    else:
        if a == 0:
            a += 1
            c -= 1
            print('A')
            continue
        elif c == 0:
            a -= 1
            c += 1
            print('C')
            continue
    
    if a + b + c == 2 and i < n - 1 and s[i] != s[i + 1]:
        if s[i] == 'AB' and a == 1 and b == 1:
            if s[i + 1] == 'AC':
                a += 1
                b -= 1
                print('A')
            else:
                a -= 1
                b += 1
                print('B')
        elif s[i] == 'BC' and b == 1 and c == 1:
            if s[i + 1] == 'AB':
                a -= 1
                b += 1
                print('B')
            else:
                a += 1
                b -= 1
                print('A')
        elif s[i] == 'AC' and a == 1 and c == 1:
            if s[i + 1] == 'AB':
                a += 1
                b -= 1
                print('A')
            else:
                a -= 1
                b += 1
                print('B')
    
    if s[i] == 'AB':
        if a < b:
            a += 1
            b -= 1
            print('A')
        else:
            a -= 1
            b += 1
            print('B')
    elif s[i] == 'BC':
        if b < c:
            b += 1
            c -= 1
            print('B')
        else:
            b -= 1
            c += 1
            print('C')
    else:
        if a < c:
            a += 1
            c -= 1
            print('A')
        else:
            a -= 1
            c += 1
            print('C')

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

if a + b + c == 0:
    print("No")
    exit(0)

ans = [None] * n
if a + b + c == 1:
    for i in range(n):
        if s[i] == 'AB':
            if a == 0 and b == 0:
                print('No')
                exit(0)
            if b == 0:
                b += 1
                a -= 1
                ans[i] = 'B'
            else:
                a += 1
                b -= 1
                ans[i] = 'A'
        elif s[i] == 'BC':
            if b == 0 and c == 0:
                print('No')
                exit(0)
            if c == 0:
                b -= 1
                c += 1
                ans[i] = 'C'
            else:
                b += 1
                c -= 1
                ans[i] = 'B'
        else:
            if c == 0:
                a -= 1
                c += 1
                ans[i] = 'C'
            else:
                a += 1
                c -= 1
                ans[i] = 'A'

    print("Yes")
    for i in range(n):
        print(ans[i])
    exit(0)

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

print("Yes")
for i in range(n):
    if s[i] == 'AB':
        if a == 0:
            print('A')
            a += 1
            b -= 1
            continue
        elif b == 0:
            print('B')
            a -= 1
            b += 1
            continue
    elif s[i] == 'BC':
        if b == 0:
            b += 1
            c -= 1
            print('B')
            continue
        elif c == 0:
            b -= 1
            c += 1
            print('C')
            continue
    else:
        if a == 0:
            a += 1
            c -= 1
            print('A')
            continue
        elif c == 0:
            a -= 1
            c += 1
            print('C')
            continue
    
    if a + b + c == 2 and i < n - 1 and s[i] != s[i + 1]:
        if s[i] == 'AB' and a == 1 and b == 1:
            if s[i + 1] == 'AC':
                a += 1
                b -= 1
                print('A')
            else:
                a -= 1
                b += 1
                print('B')
        elif s[i] == 'BC' and b == 1 and c == 1:
            if s[i + 1] == 'AB':
                a -= 1
                b += 1
                print('B')
            else:
                a += 1
                b -= 1
                print('A')
        elif s[i] == 'AC' and a == 1 and c == 1:
            if s[i + 1] == 'AB':
                a += 1
                b -= 1
                print('A')
            else:
                a -= 1
                b += 1
                print('B')
    
    if s[i] == 'AB':
        if a < b:
            a += 1
            b -= 1
            print('A')
        else:
            a -= 1
            b += 1
            print('B')
    elif s[i] == 'BC':
        if b < c:
            b += 1
            c -= 1
            print('B')
        else:
            b -= 1
            c += 1
            print('C')
    else:
        if a < c:
            a += 1
            c -= 1
            print('A')
        else:
            a -= 1
            c += 1
            print('C')

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

if a + b + c == 0:
    print("No")
    exit(0)

ans = [None] * n
if a + b + c == 1:
    for i in range(n):
        if s[i] == 'AB':
            if a == 0 and b == 0:
                print('No')
                exit(0)
            if b == 0:
                b += 1
                a -= 1
                ans[i] = 'B'
            else:
                a += 1
                b -= 1
                ans[i] = 'A'
        elif s[i] == 'BC':
            if b == 0 and c == 0:
                print('No')
                exit(0)
            if c == 0:
                b -= 1
                c += 1
                ans[i] = 'C'
            else:
                b += 1
                c -= 1
                ans[i] = 'B'
        else:
            if c == 0:
                a -= 1
                c += 1
                ans[i] = 'C'
            else:
                a += 1
                c -= 1
                ans[i] = 'A'

    print("Yes")
    for i in range(n):
        print(ans[i])
    exit(0)

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

print("Yes")
for i in range(n):
    if s[i] == 'AB':
        if a == 0:
            print('A')
            a += 1
            b -= 1
            continue
        elif b == 0:
            print('B')
            a -= 1
            b += 1
            continue
    elif s[i] == 'BC':
        if b == 0:
            b += 1
            c -= 1
            print('B')
            continue
        elif c == 0:
            b -= 1
            c += 1
            print('C')
            continue
    else:
        if a == 0:
            a += 1
            c -= 1
            print('A')
            continue
        elif c == 0:
            a -= 1
            c += 1
            print('C')
            continue
    
    if a + b + c == 2 and i < n - 1 and s[i] != s[i + 1]:
        if s[i] == 'AB' and a == 1 and b == 1:
            if s[i + 1] == 'AC':
                a += 1
                b -= 1
                print('A')
            else:
                a -= 1
                b += 1
                print('B')
        elif s[i] == 'BC' and b == 1 and c == 1:
            if s[i + 1] == 'AB':
                a -= 1
                b += 1
                print('B')
            else:
                a += 1
                b -= 1
                print('A')
        elif s[i] == 'AC' and a == 1 and c == 1:
            if s[i + 1] == 'AB':
                a += 1
                b -= 1
                print('A')
            else:
                a -= 1
                b += 1
                print('B')
    
    if s[i] == 'AB':
        if a == 0:
            a += 1
            b -= 1
            print('A')
        else:
            a -= 1
            b += 1
            print('B')
    elif s[i] == 'BC':
        if b == 0:
            b += 1
            c -= 1
            print('B')
        else:
            b -= 1
            c += 1
            print('C')
    else:
        if a == 0:
            a += 1
            c -= 1
            print('A')
        else:
            a -= 1
            c += 1
            print('C')

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

def main():
    N, A, B, C = map(int, input().split())
    s_list = [input() for i in range(N)]

    select_list = deque()
    select_list.append('Yes')
    for i, i_s in enumerate(s_list):
        if i_s == 'AB':
            if A == 0 and B == 0:
                select_list = ['No']
                break
            elif A == 1 and B == 1:
                if i+1 >= N:
                    A -= 1
                    B += 1
                    select_list.append('B')
                    continue
                if s_list[i+1] == 'AC':
                    A += 1
                    B -= 1
                    select_list.append('A')
                else:
                    A += 1
                    B -= 1
                    select_list.append('B')
            elif A > B:
                A -= 1
                B += 1
                select_list.append('B')
            else:
                A += 1
                B -= 1
                select_list.append('A')
        elif i_s == 'AC':
            if A == 0 and C == 0:
                select_list = ['No']
                break
            elif A == 1 and C == 1:
                if i+1 >= N:
                    A -= 1
                    C += 1
                    select_list.append('C')
                    continue
                if s_list[i+1] == 'AB':
                    A += 1
                    C -= 1
                    select_list.append('A')
                else:
                    A -= 1
                    C += 1
                    select_list.append('C')
            elif A > C:
                A -= 1
                C += 1
                select_list.append('C')
            else:
                A += 1
                C -= 1
                select_list.append('A')
        else:
            if B == 0 and C == 0:
                select_list = ['No']
                break
            elif B == 1 and C == 1:
                if i+1 >= N:
                    B -= 1
                    C += 1
                    select_list.append('C')
                    continue
                if s_list[i+1] == 'AB':
                    B += 1
                    C -= 1
                    select_list.append('B')
                else:
                    B -= 1
                    C += 1
                    select_list.append('C')
            elif B > C:
                B -= 1
                C += 1
                select_list.append('C')
            else:
                B += 1
                C -= 1
                select_list.append('B')

    for i in select_list:
        print(i)

    return

if __name__ == '__main__':
    main()
    sys.exit(0)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

def main():
    N, A, B, C = map(int, input().split())
    s_list = [input() for i in range(N)]

    select_list = deque()
    select_list.append('Yes')
    for i_s in s_list:
        if i_s == 'AB':
            if A == 0 and B == 0:
                select_list = ['No']
                break
            elif A > B:
                A -= 1
                B += 1
                select_list.append('B')
            else:
                A += 1
                B -= 1
                select_list.append('A')
        elif i_s == 'AC':
            if A == 0 and C == 0:
                select_list = ['No']
                break
            elif A > C:
                A -= 1
                C += 1
                select_list.append('C')
            else:
                A += 1
                C -= 1
                select_list.append('A')
        else:
            if B == 0 and C == 0:
                select_list = ['No']
                break
            elif B > C:
                B -= 1
                C += 1
                select_list.append('C')
            else:
                B += 1
                C -= 1
                select_list.append('B')

    for i in select_list:
        print(i)

    return

if __name__ == '__main__':
    main()
    sys.exit(0)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

def main():
    N, A, B, C = map(int, input().split())
    s_list = [input() for i in range(N)]

    select_list = deque()
    select_list.append('Yes')
    for i_s in s_list:
        if i_s == 'AB':
            if A == 0 and B == 0:
                select_list = ['No']
            elif A > B:
                A -= 1
                B += 1
                select_list.append('B')
            else:
                A += 1
                B -= 1
                select_list.append('A')
        elif i_s == 'AC':
            if A == 0 and C == 0:
                select_list = ['No']
            elif A > C:
                A -= 1
                C += 1
                select_list.append('C')
            else:
                A += 1
                C -= 1
                select_list.append('A')
        else:
            if B == 0 and C == 0:
                select_list = ['No']
            elif B > C:
                B -= 1
                C += 1
                select_list.append('C')
            else:
                B += 1
                C -= 1
                select_list.append('B')

    for i in select_list:
        print(i)

    return

if __name__ == '__main__':
    main()
    sys.exit(0)

Submission status: Wrong Answer

Solution code:
n,a,b,c = [int(j) for j in input().split()]
l = []
for i in range(n):
	s = input()
	l.append(s)
d = {"A":[0]*n, "B":[0]*n, "C":[0]*n}
# for j in l[-1]:
	# d[j].append(1)
for i in range(n-1, -1, -1):
	for j in l[i]:
		d[j][i] = 1
for i in range(n-2, -1, -1):
	for j in ["A", "B", "C"]:
		d[j][i]+=d[j][i+1]
ct = {"A":a, "B":b, "C":c}
ans = []
flag = True
# print(ct, d)
for i in range(n):
	# print(ct)
	if flag==False:
		break
	tmp = min([(d[j][i], ct[j]) for j in l[i]])
	# print(tmp, "tmp")

	tflag = True
	for j in l[i]:
		if (d[j][i], ct[j]) == tmp and tflag:
			ans.append(j)
			tflag = False
			ct[j]+=1
		else:
			# print(ct[j], j)
			ct[j]-=1
			if ct[j]<0:
				flag = False
				print("No")
				break

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

Submission status: Wrong Answer

Solution code:
def recur(A, B, C, events, prev):
    if len(events) == 0:
        return(prev)

    if events[0] == "AC":
        if C > 0:
            return recur(A + 1, B, C - 1, events[1:], prev + "A")
        if A > 0:
            return recur(A - 1, B, C + 1, events[1:], prev + "C")
    elif events[0] == "AB":
        if B > 0:
            return recur(A + 1, B - 1, C, events[1:], prev + "A")
        if A > 0:
            return recur(A - 1, B + 1, C, events[1:], prev + "B")
    else:
        if C > 0:
            return recur(A, B + 1, C, events[1:], prev + "B")
        if B > 0:
            return recur(A, B - 1, C, events[1:], prev + "C")
    
N, A, B, C = map(int, input().split())

events = []
for n in range(0, N): events.append(input())

ans = recur(A, B, C, events, "")

if not ans:
    print("No")
else:
    print("Yes")
    for c in ans:
        print(c)

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int, input().split())
if A + B + C == 0:
    print('No')
elif A + B + C == 1:
    res = []
    for _ in range(N):
        s = input()
        if s == 'AB':
            if A == 0 and B == 0:
                print('No')
                break
            elif A == 0:
                A += 1
                B -= 1
                res.append('A')
            else:
                A -= 1
                B += 1
                res.append('B')
        elif s == 'AC':
            if A == 0 and C == 0:
                print('No')
                break
            elif A == 0:
                A += 1
                C -= 1
                res.append('A')
            else:
                A -= 1
                C += 1
                res.append('C')
        else:
            if B == 0 and C == 0:
                print('No')
                break
            elif B == 0:
                B += 1
                C -= 1
                res.append('B')
            else:
                B -= 1
                C += 1
                res.append('C')
    else:
        print('Yes')
        print('\n'.join(res))
else:
    res = []
    s = input()
    if (s == 'AB' and A == 0 and B == 0) or (s == 'AC' and A == 0 and C == 0) or (s == 'BC' and B == 0 and C == 0):
        print('No')
    else:
        for _ in range(N - 1):
            next = input()
            if s == 'AB':
                if A == 0:
                    B -= 1
                    A += 1
                    res.append('A')
                elif B == 0:
                    B += 1
                    A -= 1
                    res.append('B')
                else:
                    if next == 'AB':
                        A += 1
                        B -= 1
                        res.append('A')
                    elif next == 'AC':
                        A += 1
                        B -= 1
                        res.append('A')
                    else:
                        A -= 1
                        B += 1
                        res.append('B')
            elif s == 'AC':
                if A == 0:
                    C -= 1
                    A += 1
                    res.append('A')
                elif C == 0:
                    C += 1
                    A -= 1
                    res.append('C')
                else:
                    if next == 'AC':
                        A += 1
                        C -= 1
                        res.append('C')
                    elif next == 'AB':
                        A += 1
                        C -= 1
                        res.append('A')
                    else:
                        A -= 1
                        C += 1
                        res.append('C')
            else:
                if C == 0:
                    B -= 1
                    C += 1
                    res.append('C')
                elif B == 0:
                    B += 1
                    C -= 1
                    res.append('B')
                else:
                    if next == 'CB':
                        C += 1
                        B -= 1
                        res.append('C')
                    elif next == 'AC':
                        C += 1
                        B -= 1
                        res.append('C')
                    else:
                        C -= 1
                        B += 1
                        res.append('B')
            s = next
        if s == 'AB':
            if A == 0:
                A += 1
                B -= 1
                res.append('A')
            else:
                A -= 1
                B += 1
                res.append('B')
        elif s == 'AC':
            if A == 0:
                A += 1
                C -= 1
                res.append('A')
            else:
                A -= 1
                C += 1
                res.append('C')
        else:
            if B == 0:
                B += 1
                C -= 1
                res.append('B')
            else:
                B -= 1
                C += 1
                res.append('C')
        print('Yes')
        print('\n'.join(res))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
abc = ["A","B","C"]

def calculate(x,y, q1,q2):
    flag = 0
    ans = ""
    if x == 1 and y == 1:
        if q1 == q2:
            y -= 1
            x += 1
            ans = abc[q1[0]]

        elif q2 != "":
            t = set(q1) & set(q2)
            s = set(q1) - t
            #print(int(list(t)[0]),int(list(s)[0]))
            if int(list(t)[0]) < int(list(s)[0]):
                y -= 1
                x += 1
                ans = abc[q1[0]]
            else:
                x -= 1
                y += 1
                ans = abc[q1[1]]

    elif x <= y:
        y -= 1
        x += 1
        #print(y)
        if y < 0:
            print("No")
            flag = 1
            return(x, y, ans, flag)
        ans = abc[q1[0]]
    else:
        x -= 1
        y += 1
        ans = ""
        #print(x)
        if x < 0:
            print("No")
            flag = 1
            return(x, y, ans, flag)
        ans = abc[q1[1]]
    return (x, y, ans, flag)

def main():
    N, A, B, C  =  [int(i) for i in input().split()]
    s = []
    for i in range(N):
        t = input()
        if t == "AB":
            s.append([0,1])
        if t == "AC":
            s.append([0,2])
        if t == "BC":
            s.append([1,2])
    #print(s)
    answers  = []
    for i, q1 in enumerate(s):
        ans = ""
        f = 0
        q2 = ""
        if i+1 != N:
            q2 = s[i+1]

        if q1 == [0,1]:
            #print(1)
            A, B, ans, f = calculate(A,B,q1,q2)
        if q1 == [1,2]:
            #print(2)
            B, C, ans,f  = calculate(B,C,q1,q2)
        if q1 == [0,2]:
            #print(3)
            A, C, ans,f  = calculate(A,C,q1,q2)
        answers.append(ans)
        if f == 1:
            return
    print("Yes")

    for a in answers:
        print(a)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
abc = ["A","B","C"]

def calculate(x,y, q1,q2):
    flag = 0
    ans = ""
    if x == 1 and y == 1:
        if q1 == q2:
            y -= 1
            x += 1
            #print(y)
            if y < 0:
                print("No")
                flag = 1
                return(x, y, ans, flag)
            ans = abc[q1[0]]

        elif q2 != "":
            t = set(q1) & set(q2)
            s = set(q1) - t
            #print(int(list(t)[0]),int(list(s)[0]))
            if int(list(t)[0]) < int(list(s)[0]):
                y -= 1
                x += 1
                ans = abc[q1[0]]
            else:
                x -= 1
                y += 1
                ans = abc[q1[1]]

    elif x <= y:
        y -= 1
        x += 1
        #print(y)
        if y < 0:
            print("No")
            flag = 1
            return(x, y, ans, flag)
        ans = abc[q1[0]]
    else:
        x -= 1
        y += 1
        ans = ""
        #print(x)
        if x < 0:
            print("No")
            flag = 1
            return(x, y, ans, flag)
        ans = abc[q1[1]]
    return (x, y, ans, flag)

def main():
    N, A, B, C  =  [int(i) for i in input().split()]
    s = []
    for i in range(N):
        t = input()
        if t == "AB":
            s.append([0,1])
        if t == "AC":
            s.append([0,2])
        if t == "BC":
            s.append([1,2])
    #print(s)
    answers  = []
    for i, q1 in enumerate(s):
        ans = ""
        f = 0
        q2 = ""
        if i+1 != N:
            q2 = s[i+1]

        if q1 == [0,1]:
            #print(1)
            A, B, ans, f = calculate(A,B,q1,q2)
        if q1 == [1,2]:
            #print(2)
            B, C, ans,f  = calculate(B,C,q1,q2)
        if q1 == [0,2]:
            #print(3)
            A, C, ans,f  = calculate(A,C,q1,q2)
        answers.append(ans)
        if f == 1:
            return
    print("Yes")

    for a in answers:
        print(a)
if __name__ == "__main__":
    main()