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

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int, input().split(' '))
# print(n,a,b,c)
left=[a,b,c]
ans=[]
prev=(-1,-1)
for i in range(0,n):
    s=input()
    # print(s)
    x = {'A':0,'B':1,'C':2}[s[0]]
    y = {'A':0,'B':1,'C':2}[s[1]]
    if left[x]==left[y]==0:
        x0,y0=prev
        if x0!=-1:
            left[x0]-=1
            left[y0]+=1
            if left[x0]==0:
                ans=[]
                break
            left[x0]-=1
            left[y0]+=1
        else:
            ans=[]
            break
    if left[x]>left[y]:
        x,y=y,x
    left[x]+=1
    left[y]-=1
    ans.append("ABC"[x])
    prev=(x,y)

if len(ans)==0:
    print("No")
else:
    print("Yes")
    for x in ans:
        print(x)

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int, input().split(' '))
# print(n,a,b,c)
left=[a,b,c]
ans=[]
for i in range(0,n):
    s=input()
    # print(s)
    x = {'A':0,'B':1,'C':2}[s[0]]
    y = {'A':0,'B':1,'C':2}[s[1]]
    if left[x]==left[y]==0:
        ans=[]
        break
    if left[x]>left[y]:
        x,y=y,x
    left[x]+=1
    left[y]-=1
    ans.append("ABC"[x])

if len(ans)==0:
    print("No")
else:
    print("Yes")
    for x in ans:
        print(x)

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int, input().split(' '))
# print(n,a,b,c)
left=[a,b,c]
ans=[]
for i in range(0,n):
    s=input()
    # print(s)
    x = {'A':0,'B':1,'C':2}[s[0]]
    y = {'A':0,'B':1,'C':2}[s[1]]
    if left[x]==left[y]==0:
        ans=[]
        break
    if left[x]>left[y]:
        x,y=y,x
    left[x]+=1
    left[y]-=1
    ans.append("ABC"[x])

if len(ans)==0:
    print("NO")
else:
    print("YES")
    for x in ans:
        print(x)

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int, input().split())
d = {}
d["A"] = A
d["B"] = B
d["C"] = C
l = [input() for _ in range(N)]
v = A+B+C

def solve():
    res = []
    if v==0:
        return []
    elif v==1:
        for i in l:
            a, b = i
            if d[a]==0 and d[b]==0:
                return []
            elif d[a]>d[b]:
                d[a] -= 1
                d[b] += 1
                res.append(b)
            else:
                d[a] += 1
                d[b] -= 1
                res.append(a)
    else:
        a, b = l[0]
        if d[a]==0 and d[b]==0:
            return []
        else:
            for i in l:
                a, b = i
                if d[a]>d[b]:
                    d[a] -= 1
                    d[b] += 1
                    res.append(b)
                else:
                    d[a] += 1
                    d[b] -= 1
                    res.append(a)
    return res


r = solve()
if r:
    print("Yes")
    for i in r:
        print(i)
else:
    print("No")

Submission status: Wrong Answer

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


def solve():
    ans = []
    for i in range(N-1):
        # print(d)
        t = set(l[i])&set(l[i+1])
        if len(t)==1:
            u = (set(l[i])-t).pop()
            s = t.pop()

            if d[s]>1:
                d[s]-=1
                d[u]+=1
                # print(u)
                ans.append(u)
            elif d[u]>1:
                d[s]+=1
                d[u]-=1
                ans.append(s)
            else:
                return []
        else:
            u = l[i][0]
            s = l[i][1]
            if d[u]>d[s] and d[u]>1:
                d[u]-=1
                d[s]+=1
                ans.append(s)
            elif d[u]<=d[s] and d[s]>1:
                d[s]-=1
                d[u]+=1
                ans.append(u)
            else:
                return []

    # print(d)
    u = l[N-1][0]
    s = l[N-1][1]
    if d[u]>d[s] and d[u]>1:
        d[u]-=1
        d[s]+=1
        ans.append(s)
    elif d[u]<=d[s] and d[s]>1:
        d[s]-=1
        d[u]+=1
        ans.append(u)
    else:
        return []
    
    return ans


result = solve()
if result:
    print("Yes")
    for i in result:
        print(i)
else:
    print("No")

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int, input().split())
s1 = input()
ans=[]
s2='?'
for i in range(n):
    if i!=n-1:
        s2 = input()
    if s1=='AB':
        if a>b:
            a-=1
            b+=1
            ans.append('B')
        elif a<b:
            a+=1
            ans.append('A')
            b-=1
        else:
            if s2=='AC':
                a+=1
                ans.append('A')
                b-=1
            else:
                a-=1
                b+=1
                ans.append('B')
    if s1=='AC':
        if a>c:
            a-=1
            c+=1
            ans.append('C')
        elif a<c:
            a+=1
            ans.append('A')
            c-=1
        else:
            if s2=='AB':
                a+=1
                ans.append('A')
                b-=1
            else:
                a-=1
                b+=1
                ans.append('B')
    if s1=='BC':
        if c>b:
            c-=1
            b+=1
            ans.append('B')
        elif c<b:
            c+=1
            ans.append('C')
            b-=1
        else:
            if s2=='AC':
                c+=1
                ans.append('C')
                b-=1
            else:
                a-=1
                b+=1
                ans.append('B')
    s1=s2
    if a<0 or b<0 or c<0:
        print('No')
        exit()
print('Yes')
for i in ans:
    print(i)

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int, input().split())
ABC_list = [int(a),int(b),int(c)]
Ans_list = []
No_flag = 0
for i in range(n):
    ABC = input()
    if ABC == 'AB':
        if ABC_list[0] == 0 and ABC_list[1] == 0:
            No_flag = 1
            break
        if ABC_list[0] >= ABC_list[1]:
            ABC_list[0] = ABC_list[0] - 1
            ABC_list[1] = ABC_list[1] + 1
            Ans_list = Ans_list + ['B']
        else:
            ABC_list[1] = ABC_list[1] - 1
            ABC_list[0] = ABC_list[0] + 1
            Ans_list = Ans_list + ['A']
    if ABC == 'AC':
        if ABC_list[0] == 0 and ABC_list[2] == 0:
            No_flag = 1
            break
        if ABC_list[0] >= ABC_list[2]:
            ABC_list[0] = ABC_list[0] - 1
            ABC_list[2] = ABC_list[2] + 1
            Ans_list = Ans_list + ['C']
        else:
            ABC_list[2] = ABC_list[2] - 1
            ABC_list[0] = ABC_list[0] + 1
            Ans_list = Ans_list + ['A']
    if ABC == 'BC':
        if ABC_list[1] == 0 and ABC_list[2] == 0:
            No_flag = 1
            break
        if ABC_list[1] >= ABC_list[2]:
            ABC_list[1] = ABC_list[1] - 1
            ABC_list[2] = ABC_list[2] + 1
            Ans_list = Ans_list + ['C']
        else:
            ABC_list[2] = ABC_list[2] - 1
            ABC_list[1] = ABC_list[1] + 1
            Ans_list = Ans_list + ['B']
        
if No_flag == 1:
    print('No')
else:
    print('Yes')
    for i in range(len(Ans_list)):
        print(Ans_list[i])

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int, input().split())
ABC_list = [int(a),int(b),int(c)]
Ans_list = []
No_flag = 0
for i in range(n):
    ABC = input()
    if ABC == 'AB':
        if ABC_list[0] == 0 and ABC_list[1] == 0:
            No_flag = 1
            break
        if ABC_list[0] >= ABC_list[1]:
            ABC_list[0] = ABC_list[0] - 1
            ABC_list[1] = ABC_list[1] + 1
            Ans_list = Ans_list + ['A']
        else:
            ABC_list[1] = ABC_list[1] - 1
            ABC_list[0] = ABC_list[0] + 1
            Ans_list = Ans_list + ['B']
    if ABC == 'AC':
        if ABC_list[0] == 0 and ABC_list[2] == 0:
            No_flag = 1
            break
        if ABC_list[0] >= ABC_list[2]:
            ABC_list[0] = ABC_list[0] - 1
            ABC_list[2] = ABC_list[2] + 1
            Ans_list = Ans_list + ['A']
        else:
            ABC_list[2] = ABC_list[2] - 1
            ABC_list[0] = ABC_list[0] + 1
            Ans_list = Ans_list + ['C']
    if ABC == 'BC':
        if ABC_list[1] == 0 and ABC_list[2] == 0:
            No_flag = 1
            break
        if ABC_list[1] >= ABC_list[2]:
            ABC_list[1] = ABC_list[1] - 1
            ABC_list[2] = ABC_list[2] + 1
            Ans_list = Ans_list + ['B']
        else:
            ABC_list[2] = ABC_list[2] - 1
            ABC_list[1] = ABC_list[1] + 1
            Ans_list = Ans_list + ['C']
        
if No_flag == 1:
    print('No')
else:
    print('Yes')
    for i in range(len(Ans_list)):
        print(Ans_list[i])

Submission status: Wrong Answer

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


def update(pair):
    global A, B, C
    c = ''
    if pair == 'AB':
        if A <= B:
            A += 1
            B -= 1
            c = 'A'
        else:
            A -= 1
            B += 1
            c = 'B'
    if pair == 'AC':
        if A <= C:
            A += 1
            C -= 1
            c = 'A'
        else:
            A -= 1
            C += 1
            c = 'C'
    if pair == 'BC':
        if B <= C:
            B += 1
            C -= 1
            c = 'B'
        else:
            B -= 1
            C += 1
            c = 'C'
    if A < 0 or B < 0 or C < 0:
        return 'No'
    else:
        return c


L = []
for n in range(N):

    choice = update(S[n])
    if choice == 'No':
        print('No')
        exit()
    else:
        L.append(choice)

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

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int, input().split())
ans = []
number = dict()
number['A'] = a
number['B'] = b
number['C'] = c
for i in range(n):
    s = input()
    s_1, s_2 = s[0], s[1]
    if number[s_1] == 0 and number[s_2] == 0:
        print("No")
        exit()
    else:
        if number[s_1] > number[s_2]:
            number[s_1] -= 1
            number[s_2] += 1
            ans.append(s_2)
        else:
            number[s_2] -= 1
            number[s_1] += 1
            ans.append(s_1)
print("Yes")
for i in range(n):
    print(ans[i])

Submission status: Wrong Answer

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

q = [(a, b, c, "")]
visited = {}

for _ in range(n):
    x = input()
    l = len(q)
    for _ in range(l):
        a, b, c, steps = q.pop(0)
        if x == "AB":
            if max(a, b) > 0:
                if a > b and visited.get((a-1, b+1, c, steps+"B"), 1):
                    q.append((a-1, b+1, c, steps+"B"))
                    visited[q[-1]] = 0
                elif b > a and visited.get((a+1, b-1, c, steps+"A"), 1):
                    q.append((a+1, b-1, c, steps+"A"))
                    visited[q[-1]] = 0
                else:
                    if visited.get((a-1, b+1, c, steps+"B"), 1):
                        q.append((a-1, b+1, c, steps+"B"))
                        visited[q[-1]] = 0
                    if visited.get((a+1, b-1, c, steps+"A"), 1):
                        q.append((a+1, b-1, c, steps+"A"))
                        visited[q[-1]] = 0
        elif x == "AC":
            if max(a, c) > 0:
                if a > c and visited.get((a-1, b, c+1, steps+"C"), 1):
                    q.append((a-1, b, c+1, steps+"C"))
                    visited[q[-1]] = 0
                elif c > a and visited.get((a+1, b, c-1, steps+"A"), 1):
                    q.append((a+1, b, c-1, steps+"A"))
                    visited[q[-1]] = 0
                else:
                    if visited.get((a-1, b, c+1, steps+"C"), 1):
                        q.append((a-1, b, c+1, steps+"C"))
                        visited[q[-1]] = 0
                    if visited.get((a+1, b, c-1, steps+"A"), 1):
                        q.append((a+1, b, c-1, steps+"A"))
                        visited[q[-1]] = 0
        elif x == "BC":
            if max(b, c) > 0:
                if b > c and visited.get((a, b-1, c+1, steps+"C"), 1):
                    q.append((a, b-1, c+1, steps+"C"))
                    visited[q[-1]] = 0
                elif c > b and visited.get((a, b+1, c-1, steps+"B"), 1):
                    q.append((a, b+1, c-1, steps+"B"))
                    visited[q[-1]] = 0
                else:
                    if visited.get((a, b+1, c-1, steps+"B"), 1):
                        q.append((a, b+1, c-1, steps+"B"))
                        visited[q[-1]] = 0
                    if visited.get((a, b-1, c+1, steps+"C"), 1):
                        q.append((a, b-1, c+1, steps+"C"))
                        visited[q[-1]] = 0

for i in q[::-1]:
    if len(i[-1])==n:
        if all(i[:-1]):
            print("Yes")
            for char in i[-1]:
                print(char)
            break
else: print("No")

Submission status: Wrong Answer

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

q = [(a, b, c, "")]
visited = {}

for _ in range(n):
    x = input()
    l = len(q)
    for _ in range(l):
        a, b, c, steps = q.pop(0)
        if x == "AB":
            if max(a, b) > 0:
                if a > b and visited.get((a-1, b+1, c, steps+"B"), 1):
                    q.append((a-1, b+1, c, steps+"B"))
                    visited[q[-1]] = 0
                elif b > a and visited.get((a+1, b-1, c, steps+"B"), 1):
                    q.append((a+1, b-1, c, steps+"A"))
                    visited[q[-1]] = 0
                else:
                    if visited.get((a-1, b+1, c, steps+"B"), 1):
                        q.append((a-1, b+1, c, steps+"B"))
                        visited[q[-1]] = 0
                    if visited.get((a+1, b-1, c, steps+"A"), 1):
                        q.append((a+1, b-1, c, steps+"A"))
                        visited[q[-1]] = 0
        elif x == "AC":
            if max(a, c) > 0:
                if a > c and visited.get((a-1, b, c+1, steps+"C"), 1):
                    q.append((a-1, b, c+1, steps+"C"))
                    visited[q[-1]] = 0
                elif c > a and visited.get((a+1, b, c-1, steps+"A"), 1):
                    q.append((a+1, b, c-1, steps+"A"))
                    visited[q[-1]] = 0
                else:
                    if visited.get((a-1, b, c+1, steps+"C"), 1):
                        q.append((a-1, b, c+1, steps+"C"))
                        visited[q[-1]] = 0
                    if visited.get((a+1, b, c-1, steps+"A"), 1):
                        q.append((a+1, b, c-1, steps+"A"))
                        visited[q[-1]] = 0
        elif x == "BC":
            if max(b, c) > 0:
                if b > c and visited.get((a, b-1, c+1, steps+"C"), 1):
                    q.append((a, b-1, c+1, steps+"C"))
                    visited[q[-1]] = 0
                elif c > b and visited.get((a, b+1, c-1, steps+"B"), 1):
                    q.append((a, b+1, c-1, steps+"B"))
                    visited[q[-1]] = 0
                else:
                    if visited.get((a, b+1, c-1, steps+"B"), 1):
                        q.append((a, b+1, c-1, steps+"B"))
                        visited[q[-1]] = 0
                    if visited.get((a, b-1, c+1, steps+"C"), 1):
                        q.append((a, b-1, c+1, steps+"C"))
                        visited[q[-1]] = 0

for i in q[::-1]:
    if len(i[-1])==n:
        if all(i[:-1]):
            print("Yes")
            for char in i[-1]:
                print(char)
            break
else: print("No")

Submission status: Wrong Answer

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

q = [(a, b, c, "")]

for _ in range(n):
    x = input()
    l = len(q)
    for _ in range(l):
        a, b, c, steps = q.pop(0)
        if x == "AB":
            if max(a, b) > 0:
                if a > b:
                    q.append((a-1, b+1, c, steps+"B"))
                elif b > a:
                    q.append((a+1, b-1, c, steps+"A"))
                else:
                    q.append((a-1, b+1, c, steps+"B"))
                    q.append((a+1, b-1, c, steps+"A"))
        elif x == "AC":
            if max(a, c) > 0:
                if a > c:
                    q.append((a-1, b, c+1, steps+"C"))
                elif c > a:
                    q.append((a+1, b, c-1, steps+"A"))
                else:
                    q.append((a-1, b, c+1, steps+"C"))
                    q.append((a+1, b, c-1, steps+"A"))
        elif x == "BC":
            if max(b, c) > 0:
                if b > c:
                    q.append((a, b-1, c+1, steps+"C"))
                elif c > b:
                    q.append((a, b+1, c-1, steps+"B"))
                else:
                    q.append((a, b+1, c-1, steps+"B"))
                    q.append((a, b-1, c+1, steps+"C"))

for i in q[::-1]:
    if len(i[-1])==n:
        if all(i[:-1]):
            print("Yes")
            for char in i[-1]:
                print(char)
            break
else: print("No")

Submission status: Wrong Answer

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

q = [(a, b, c, "")]

for _ in range(n):
    x = input()
    l = len(q)
    for _ in range(l):
        a, b, c, steps = q.pop(0)
        if x == "AB":
            if max(a, b) > 0:
                if a > b:
                    q.append((a-1, b+1, c, steps+"B"))
                elif b > a:
                    q.append((a+1, b-1, c, steps+"A"))
                else:
                    q.append((a-1, b+1, c, steps+"B"))
                    q.append((a+1, b-1, c, steps+"A"))
        elif x == "AC":
            if max(a, c) > 0:
                if a > c:
                    q.append((a-1, b, c+1, steps+"C"))
                elif c > a:
                    q.append((a+1, b, c-1, steps+"A"))
                else:
                    q.append((a-1, b, c+1, steps+"C"))
                    q.append((a+1, b, c-1, steps+"A"))
        elif x == "BC":
            if max(b, c) > 0:
                if b > c:
                    q.append((a, b-1, c+1, steps+"C"))
                elif c > b:
                    q.append((a, b+1, c-1, steps+"B"))
                else:
                    q.append((a, b+1, c-1, steps+"B"))
                    q.append((a, b-1, c+1, steps+"C"))

for i in q[::-1]:
    if len(i[-1])==n:
        if all(i[:-1]):
            print("Yes")
            for char in i[-1]:
                print(char)
else: print("No")

Submission status: Wrong Answer

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

q = [(a, b, c, "")]

for _ in range(n):
    x = input()
    l = len(q)
    for _ in range(l):
        a, b, c, steps = q.pop(0)
        if x == "AB":
            if max(a, b) > 0:
                if a > b:
                    q.append((a-1, b+1, c, steps+"B"))
                elif b > a:
                    q.append((a+1, b-1, c, steps+"A"))
                else:
                    q.append((a-1, b+1, c, steps+"B"))
                    q.append((a+1, b-1, c, steps+"A"))
        elif x == "AC":
            if max(a, c) > 0:
                if a > c:
                    q.append((a-1, b, c+1, steps+"C"))
                elif c > a:
                    q.append((a+1, b, c-1, steps+"A"))
                else:
                    q.append((a-1, b, c+1, steps+"C"))
                    q.append((a+1, b, c-1, steps+"A"))
        elif x == "BC":
            if max(b, c) > 0:
                if b > c:
                    q.append((a, b-1, c+1, steps+"C"))
                elif c > b:
                    q.append((a, b+1, c-1, steps+"B"))
                else:
                    q.append((a, b+1, c-1, steps+"B"))
                    q.append((a, b-1, c+1, steps+"C"))

if len(q) > 0:
    i = q[-1]
    if all(i[:-1]) and len(i[-1])==n:
        print("Yes")
        for char in i[-1]:
            print(char)
    else: print("No")
else: print("No")

Submission status: Wrong Answer

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

q = [(a, b, c, "")]

for _ in range(n):
    x = input()
    l = len(q)
    for _ in range(l):
        a, b, c, steps = q.pop(0)
        if x == "AB":
            if max(a, b) > 0:
                if a > b:
                    q.append((a-1, b+1, c, steps+"B"))
                elif b > a:
                    q.append((a+1, b-1, c, steps+"A"))
                else:
                    q.append((a-1, b+1, c, steps+"B"))
                    q.append((a+1, b-1, c, steps+"A"))
        elif x == "AC":
            if max(a, c) > 0:
                if a > c:
                    q.append((a-1, b, c+1, steps+"C"))
                elif c > a:
                    q.append((a+1, b, c-1, steps+"A"))
                else:
                    q.append((a-1, b, c+1, steps+"C"))
                    q.append((a+1, b, c-1, steps+"A"))
        elif x == "BC":
            if max(b, c) > 0:
                if b > c:
                    q.append((a, b-1, c+1, steps+"C"))
                elif c > b:
                    q.append((a, b+1, c-1, steps+"B"))
                else:
                    q.append((a, b+1, c-1, steps+"B"))
                    q.append((a, b-1, c+1, steps+"C"))

for i in q[::-1]:
    if all(i[:-1]) and len(i[-1])==n:
        print("Yes")
        for char in i[-1]:
            print(char)
        break
else: print("No")

Submission status: Wrong Answer

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

q = [(a, b, c, "")]

for _ in range(n):
    x = input()
    l = len(q)
    for _ in range(l):
        a, b, c, steps = q.pop(0)
        if x == "AB":
            if max(a, b) > 0:
                if a > b:
                    q.append((a-1, b+1, c, steps+"B"))
                elif b > a:
                    q.append((a+1, b-1, c, steps+"A"))
                else:
                    q.append((a-1, b+1, c, steps+"B"))
                    q.append((a+1, b-1, c, steps+"A"))
        elif x == "AC":
            if max(a, c) > 0:
                if a > c:
                    q.append((a-1, b, c+1, steps+"C"))
                elif c > a:
                    q.append((a+1, b, c-1, steps+"A"))
                else:
                    q.append((a-1, b, c+1, steps+"C"))
                    q.append((a+1, b, c-1, steps+"A"))
        elif x == "BC":
            if max(b, c) > 0:
                if b > c:
                    q.append((a, b-1, c+1, steps+"C"))
                elif c > b:
                    q.append((a, b+1, c-1, steps+"B"))
                else:
                    q.append((a, b+1, c-1, steps+"B"))
                    q.append((a, b-1, c+1, steps+"C"))

for i in q:
    if all(i[:-1]) and len(i[-1])==n:
        print("Yes")
        for char in i[-1]:
            print(char)
        break
else: print("No")

Submission status: Wrong Answer

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

q = [(a, b, c, "")]

for _ in range(n):
    x = input()
    q_copy = q[:]
    while len(q_copy) > 0:
        a, b, c, steps = q_copy.pop(0)
        if x == "AB":
            if max(a, b) > 0:
                if a > b:
                    q.append((a-1, b+1, c, steps+"B"))
                elif b > a:
                    q.append((a+1, b-1, c, steps+"A"))
                else:
                    q.append((a-1, b+1, c, steps+"B"))
                    q.append((a+1, b-1, c, steps+"A"))
        elif x == "AC":
            if max(a, c) > 0:
                if a > c:
                    q.append((a-1, b, c+1, steps+"C"))
                elif c > a:
                    q.append((a+1, b, c-1, steps+"A"))
                else:
                    q.append((a-1, b, c+1, steps+"C"))
                    q.append((a+1, b, c-1, steps+"A"))
        elif x == "BC":
            if max(b, c) > 0:
                if b > c:
                    q.append((a, b-1, c+1, steps+"C"))
                elif c > b:
                    q.append((a, b+1, c-1, steps+"B"))
                else:
                    q.append((a, b+1, c-1, steps+"B"))
                    q.append((a, b-1, c+1, steps+"C"))

for i in q:
    if all(i[:-1]) and len(i[-1])==n:
        print("Yes")
        for char in i[-1]:
            print(char)
        break
else: print("No")