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

import sys
from collections import defaultdict


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


def psum(pair):
    if pair == 'AB':
        return a + b
    elif pair == 'AC':
        return a + c
    elif pair == 'BC':
        return b + c

def select(pair, npair):
    global a, b, c
    added = None
    if pair == 'AB':
        if a < b:
            a += 1
            b -= 1
            added = 'A'
        elif a > b:
            a -= 1
            b += 1
            added = 'B'            
        else:
            # a == b
            if a + b == 1 and c == 0 and npair:
                if npair == 'AB':
                    a += 1
                    b -= 1
                    added = 'A'
                elif npair == 'AC':
                    a += 1
                    b -= 1
                    added = 'A'                    
                elif npair == 'BC':
                    a -= 1
                    b += 1
                    added = 'B'                    
            else:
                a += 1
                b -= 1
                added = 'A'
            
    elif pair == 'AC':
        if a < c:
            a += 1
            c -= 1
            added = 'A'
        elif a > c:
            a -= 1
            c += 1
            added = 'C'            
        else:
            # a == c
            if a + c == 1 and b == 0 and npair:
                if npair == 'AB':
                    a += 1
                    c -= 1
                    added = 'A'                    
                elif npair == 'AC':
                    a += 1
                    c -= 1
                    added = 'A'                    
                elif npair == 'BC':
                    a -= 1
                    c += 1
                    added = 'C'
            else:
                a += 1
                c -= 1
                added = 'A'                
        
    elif pair == 'BC':
        if b < c:
            b += 1
            c -= 1
            added = 'B'            
        elif b > c:
            b -= 1
            c += 1
            added = 'C'            
        else:
            # b == c
            if b + c == 1 and a == 0 and npair:
                if npair == 'AB':
                    b += 1
                    c -= 1
                    added = 'B'                    
                elif npair == 'AC':
                    b -= 1
                    c += 1
                    added = 'C'                    
                elif npair == 'BC':
                    b += 1
                    c -= 1
                    added = 'B'
            else:
                b += 1
                c -= 1
                added = 'B'                
    return added

    
ans = []
if a + b + c == 0:
    print('No')
else:
    for i, s in enumerate(ss):
        if psum(s) == 0:
            print('No')
            exit()
        else:
            ns = None
            if i < len(ss) - 1:
                ns = ss[i]
            ans.append(select(s, ns))
        #print(a, b, c)
        
print('Yes')
for l in ans:
    print(l)

Submission status: Wrong Answer

Solution code:
import sys
n,a,b,c=map(int,input().split())
slist=[]
for i in range(n):
    slist.append(input())
output=[]
ynflag=True

def abc1(s):
    global a
    global b
    global c
    global output
    global ynflag

    
    if s == "AB":
        if a ==1:
            a -=1
            b +=1
            output.append("B")
            return True
        elif b == 1:
            a +=1
            b -=1
            output.append("A")
            return True
        else:
            return False
    if s == "AC":
        if a ==1:
            a -=1
            c +=1
            output.append("C")
            return True
        elif c == 1:
            a +=1
            c -=1
            output.append("A")
            return True
        else:
            return False
    if s == "BC":
        if b ==1:
            b -=1
            c +=1
            output.append("C")
            return True
        elif c == 1:
            b +=1
            c -=1
            output.append("B")
            return True
        else:
            return False

def abc2(i):
    global a
    global b
    global c
    global output
    global slist
    s=slist[i]
    
    if i == len(slist)-1:
        if s =="AB":
            if a > b:
                output.append("B")
                return True
            else:
                output.append("A")
                return True
        elif s =="AC":
            if a > c:
                output.append("C")
                return True
            else:
                output.append("A")
                return True
        else:
            if b > c:
                output.append("C")
                return True
            else:
                output.append("B")
                return True
    
    elif s == "AB":
        if a + b == 0:
            return False
        elif a == 1 and b == 1 and c == 0:
            if slist[i+1] == "BC":
                a -=1
                b +=1
                output.append("B")
                return True
            else:
                a +=1
                b -=1
                output.append("A")
                return True
        else:
            if a >=b:
                a -=1
                b +=1
                output.append("B")
                return True
            else:
                a +=1
                b -=1
                output.append("A")
                return True
    elif s == "AC":
        if a + c == 0:
            return False
        elif a == 1 and c == 1 and b == 0:
            if slist[i+1] == "BC":
                a -=1
                c +=1
                output.append("C")
                return True
            else:
                a +=1
                c -=1
                output.append("A")
                return True
        else:
            if a >=c:
                a -=1
                c +=1
                output.append("C")
                return True
            else:
                a +=1
                c -=1
                output.append("A")
                return True
    else:
        if b + c == 0:
            return False
        elif b == 1 and c == 1 and a == 0:
            if slist[i+1] == "AC":
                b -=1
                c +=1
                output.append("C")
                return True
            else:
                b +=1
                c -=1
                output.append("B")
                return True
        else:
            if b >=c:
                b -=1
                c +=1
                output.append("C")
                return True
            else:
                b +=1
                c -=1
                output.append("B")
                return True

if a == 0 and b == 0 and c == 0:
    print("No")
    sys.exit()

else:
    if a+b+c == 1:
        for s in slist:
            ynflag=abc1(s)
            if not ynflag:
                print("No")
                sys.exit()
    else:
        for i in range(n):
            ynflag=abc2(i)
            if not ynflag:
                print("No")
                sys.exit()
    print("Yes")
    for out in output:
        print(out)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def main():
    N, A, B, C = map(int, readline().split())
    s = read().decode().split()
    s.append('AB')
    ans = []
    for i in range(N):
        if s[i] == 'AB':
            if A == 0 and B == 0:
                print('No')
                break
            else:
                if A < B:
                    A += 1
                    B -= 1
                    ans.append('A')
                elif A > B:
                    A -= 1
                    B += 1
                    ans.append('B')
                else:
                    if s[i+1] == 'AB' or s[i+1] == 'AC':
                        A += 1
                        B -= 1
                        ans.append('A')
                    else:
                        A -= 1
                        B += 1
                        ans.append('B')
        if s[i] == 'AC':
            if A == 0 and C == 0:
                print('No')
                break
        else:
            if A < C:
                A += 1
                C -= 1
                ans.append('A')
            elif A > C:
                A -= 1
                C += 1
                ans.append('C')
            else:
                if s[i + 1] == 'AB' or s[i + 1] == 'AC':
                    A += 1
                    C -= 1
                    ans.append('A')
                else:
                    A -= 1
                    C += 1
                    ans.append('C')
        if s[i] == 'BC':
            if C == 0 and B == 0:
                print('No')
                break
        else:
            if C < B:
                C += 1
                B -= 1
                ans.append('C')
            elif C > B:
                C -= 1
                B += 1
                ans.append('B')
            else:
                if s[i + 1] == 'AB' or s[i + 1] == 'BC':
                    B += 1
                    C -= 1
                    ans.append('B')
                else:
                    B -= 1
                    C += 1
                    ans.append('C')
    else:
        print('Yes')
        print('\n'.join(ans))
    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3

import sys
from collections import defaultdict

def psum(a, b, c, pair):
    if pair == 'AB':
        return a + b
    elif pair == 'AC':
        return a + c
    elif pair == 'BC':
        return b + c

def select(a, b, c, pair, npair):
    added = None
    if pair == 'AB':
        if a < b:
            a += 1
            b -= 1
            added = 'A'
        elif a > b:
            a -= 1
            b += 1
            added = 'B'            
        else:
            # a == b
            if a + b == 1 and c == 0 and npair:
                if npair == 'AB':
                    a += 1
                    b -= 1
                    added = 'A'
                elif npair == 'AC':
                    a += 1
                    b -= 1
                    added = 'A'                    
                elif npair == 'BC':
                    a -= 1
                    b += 1
                    added = 'B'                    
            else:
                a += 1
                b -= 1
                added = 'A'
            
    elif pair == 'AC':
        if a < c:
            a += 1
            c -= 1
            added = 'A'
        elif a > c:
            a -= 1
            c += 1
            added = 'C'            
        else:
            # a == c
            if a + c == 1 and b == 0 and npair:
                if npair == 'AB':
                    a += 1
                    c -= 1
                    added = 'A'                    
                elif npair == 'AC':
                    a += 1
                    c -= 1
                    added = 'A'                    
                elif npair == 'BC':
                    a -= 1
                    c += 1
                    added = 'C'
            else:
                a += 1
                c -= 1
                added = 'A'                
        
    elif pair == 'BC':
        if b < c:
            b += 1
            c -= 1
            added = 'B'            
        elif b > c:
            b -= 1
            c += 1
            added = 'C'            
        else:
            # b == c
            if b + c == 1 and a == 0 and npair:
                if npair == 'AB':
                    b += 1
                    c -= 1
                    added = 'B'                    
                elif npair == 'AC':
                    b -= 1
                    c += 1
                    added = 'C'                    
                elif npair == 'BC':
                    b += 1
                    c -= 1
                    added = 'B'
            else:
                b += 1
                c -= 1
                added = 'B'                
    return added

(n, a, b, c) = tuple(map(int, input().split()))
ss = []
for i in range(n):
    ss.append(input())

ans = []
if a + b + c == 0:
    print('No')
else:
    for i, s in enumerate(ss):
        if psum(a, b, c, s) == 0:
            print('No')
            exit()
        else:
            ns = None
            if i < len(ss) - 1:
                ns = ss[i]
            ans.append(select(a, b, c, s, ns))
        
        
print('Yes')
for l in ans:
    print(l)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(1000000)


def recur1(a, b, c, ls: [], ans: []):
    s = ls[0]
    if s == "AB":
        if a == 0 and b == 0:
            return False, []
        elif a == 0:
            ans.append("A")
            if len(ls) == 1:
                return True, ans
            return recur1(a + 1, b - 1, c, ls[1:], ans)
        else:
            ans.append("B")
            if len(ls) == 1:
                return True, ans
            return recur1(a - 1, b + 1, c, ls[1:], ans)
    elif s == "AC":
        if a == 0 and c == 0:
            return False, []
        elif a == 0:
            ans.append("A")
            if len(ls) == 1:
                return True, ans
            return recur1(a + 1, b, c - 1, ls[1:], ans)
        else:
            ans.append("C")
            if len(ls) == 1:
                return True, ans
            return recur1(a - 1, b, c + 1, ls[1:], ans)
    else:
        if b == 0 and c == 0:
            return False, []
        elif b == 0:
            ans.append("B")
            if len(ls) == 1:
                return True, ans
            return recur1(a, b + 1, c - 1, ls[1:], ans)
        else:
            ans.append("C")
            if len(ls) == 1:
                return True, ans
            return recur1(a, b - 1, c + 1, ls[1:], ans)


def recur2(a, b, c, ls: [], ans: []):
    s = ls[0]
    ln = len(ls)
    if s == "AB":
        if a == 0 and b == 0:
            return False, []
        elif a == 0:
            ans.append("A")
            if ln == 1:
                return True, ans
            return recur2(a + 1, b - 1, c, ls[1:], ans)
        elif b == 0:
            ans.append("B")
            if ln == 1:
                return True, ans
            return recur2(a - 1, b + 1, c, ls[1:], ans)
        else:
            if ln == 1:
                ans.append("A")
                return True, ans
            nex = ls[1]
            if nex == "BC":
                ans.append("B")
                return recur2(a - 1, b + 1, c, ls[1:], ans)
            elif nex == "AC":
                ans.append("A")
                return recur2(a + 1, b - 1, c, ls[1:], ans)
            else:
                ans.append("A")
                return recur2(a + 1, b - 1, c, ls[1:], ans)
    elif s == "AC":
        if a == 0 and c == 0:
            return False, []
        elif a == 0:
            ans.append("A")
            if ln == 1:
                return True, ans
            return recur2(a + 1, b, c - 1, ls[1:], ans)
        elif c == 0:
            ans.append("C")
            if ln == 1:
                return True, ans
            return recur2(a - 1, b, c + 1, ls[1:], ans)
        else:
            if ln == 1:
                ans.append("A")
                return True, ans
            nex = ls[1]
            if nex == "AB":
                ans.append("A")
                return recur2(a + 1, b, c - 1, ls[1:], ans)
            elif nex == "BC":
                ans.append("C")
                return recur2(a - 1, b, c + 1, ls[1:], ans)
            else:
                ans.append("C")
                return recur2(a - 1, b, c + 1, ls[1:], ans)
    else:
        if c == 0 and b == 0:
            return False, []
        elif c == 0:
            ans.append("C")
            if ln == 1:
                return True, ans
            return recur2(a, b - 1, c + 1, ls[1:], ans)
        elif b == 0:
            ans.append("B")
            if ln == 1:
                return True, ans
            return recur2(a, b + 1, c - 1, ls[1:], ans)
        else:
            if ln == 1:
                ans.append("C")
                return True, ans
            nex = ls[1]
            if nex == "AB":
                ans.append("B")
                return recur2(a, b + 1, c - 1, ls[1:], ans)
            elif nex == "AC":
                ans.append("C")
                return recur2(a, b - 1, c + 1, ls[1:], ans)
            else:
                ans.append("C")
                return recur2(a, b - 1, c + 1, ls[1:], ans)


def recur3(a, b, c, ls: [], ans: []):
    s = ls[0]
    ln = len(ls)
    if s == "AB":
        if a == 0 and b == 0:
            return False, []
        else:
            if a >= b:
                ans.append("B")
                if ln == 1:
                    return True, ans
                return recur3(a - 1, b + 1, c, ls[1:], ans)
            else:
                ans.append("A")
                if ln == 1:
                    return True, ans
                return recur3(a + 1, b - 1, c, ls[1:], ans)
    elif s == "BC":
        if b == 0 and c == 0:
            return False, []
        else:
            if b >= c:
                ans.append("C")
                if ln == 1:
                    return True, ans
                return recur3(a, b - 1, c+1, ls[1:], ans)
            else:
                ans.append("B")
                if ln == 1:
                    return True, ans
                return recur3(a, b + 1, c-1, ls[1:], ans)
    else:
        if a == 0 and c == 0:
            return False, []
        else:
            if a >= c:
                ans.append("C")
                if ln == 1:
                    return True, ans
                return recur3(a - 1, b, c+1, ls[1:], ans)
            else:
                ans.append("A")
                if ln == 1:
                    return True, ans
                return recur3(a + 1, b, c-1, ls[1:], ans)



def main():
    n, a, b, c = map(int, input().split())
    s_ls = []
    for i in range(n):
        s_ls.append(input())

    res = True
    ans = []
    if a + b + c == 0:
        print("No")
    elif a + b + c == 1:
        res, ans = recur1(a, b, c, s_ls, ans)
    elif a + b + c == 2:
        res, ans = recur2(a, b, c, s_ls, ans)
    else:
        res, ans = recur3(a, b, c, s_ls, ans)

    if res:
        print("Yes")
        for x in ans:
            print(x)
    else:
        print("No")


main()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(1000000)


def recur1(a, b, c, ls: [], ans: []):
    s = ls[0]
    if s == "AB":
        if a == 0 and b == 0:
            return False, []
        elif a == 0:
            ans.append("A")
            if len(ls) == 1:
                return True, ans
            ans_1 = ans[:]
            return recur1(a + 1, b - 1, c, ls[1:], ans_1)
        else:
            ans.append("B")
            if len(ls) == 1:
                return True, ans
            ans_1 = ans[:]
            return recur1(a - 1, b + 1, c, ls[1:], ans_1)
    elif s == "AC":
        if a == 0 and c == 0:
            return False, []
        elif a == 0:
            ans.append("A")
            if len(ls) == 1:
                return True, ans
            ans_1 = ans[:]
            return recur1(a + 1, b, c - 1, ls[1:], ans_1)
        else:
            ans.append("C")
            if len(ls) == 1:
                return True, ans
            ans_1 = ans[:]
            return recur1(a - 1, b, c + 1, ls[1:], ans_1)
    else:
        if b == 0 and c == 0:
            return False, []
        elif b == 0:
            ans.append("B")
            if len(ls) == 1:
                return True, ans
            ans_1 = ans[:]
            return recur1(a, b + 1, c - 1, ls[1:], ans_1)
        else:
            ans.append("C")
            if len(ls) == 1:
                return True, ans
            ans_1 = ans[:]
            return recur1(a, b - 1, c + 1, ls[1:], ans_1)


def recur2(a, b, c, ls: [], ans: []):
    s = ls[0]
    ln = len(ls)
    if s == "AB":
        if a == 0 and b == 0:
            return False, []
        elif a == 0:
            ans.append("A")
            if ln == 1:
                return True, ans
            ans_1 = ans[:]
            return recur2(a + 1, b - 1, c, ls[1:], ans_1)
        elif b == 0:
            ans.append("B")
            if ln == 1:
                return True, ans
            ans_1 = ans[:]
            return recur2(a - 1, b + 1, c, ls[1:], ans_1)
        else:
            if ln == 1:
                ans.append("A")
                return True, ans
            nex = ls[1]
            if nex == "BC":
                ans.append("B")
                return recur2(a - 1, b + 1, c, ls[1:], ans)
            elif nex == "AC":
                ans.append("A")
                return recur2(a + 1, b - 1, c, ls[1:], ans)
            else:
                ans.append("A")
                return recur2(a + 1, b - 1, c, ls[1:], ans)
    elif s == "AC":
        if a == 0 and c == 0:
            return False, []
        elif a == 0:
            ans.append("A")
            if ln == 1:
                return True, ans
            ans_1 = ans[:]
            return recur2(a + 1, b, c - 1, ls[1:], ans_1)
        elif c == 0:
            ans.append("C")
            if ln == 1:
                return True, ans
            ans_1 = ans[:]
            return recur2(a - 1, b, c + 1, ls[1:], ans_1)
        else:
            if ln == 1:
                ans.append("A")
                return True, ans
            nex = ls[1]
            if nex == "AB":
                ans.append("A")
                return recur2(a + 1, b, c - 1, ls[1:], ans)
            elif nex == "BC":
                ans.append("C")
                return recur2(a - 1, b, c + 1, ls[1:], ans)
            else:
                ans.append("C")
                return recur2(a - 1, b, c + 1, ls[1:], ans)
    else:
        if c == 0 and b == 0:
            return False, []
        elif c == 0:
            ans.append("C")
            if ln == 1:
                return True, ans
            ans_1 = ans[:]
            return recur2(a, b - 1, c + 1, ls[1:], ans_1)
        elif b == 0:
            ans.append("B")
            if ln == 1:
                return True, ans
            ans_1 = ans[:]
            return recur2(a, b + 1, c - 1, ls[1:], ans_1)
        else:
            if ln == 1:
                ans.append("C")
                return True, ans
            nex = ls[1]
            if nex == "AB":
                ans.append("B")
                return recur2(a, b + 1, c - 1, ls[1:], ans)
            elif nex == "AC":
                ans.append("C")
                return recur2(a, b - 1, c + 1, ls[1:], ans)
            else:
                ans.append("C")
                return recur2(a, b - 1, c + 1, ls[1:], ans)


def recur3(a, b, c, ls: [], ans: []):
    s = ls[0]
    ln = len(ls)
    if s == "AB":
        if a == 0 and b == 0:
            return False, []
        else:
            if a >= b:
                ans.append("B")
                if ln == 1:
                    return True, ans
                return recur3(a - 1, b + 1, c, ls[1:], ans)
            else:
                ans.append("A")
                if ln == 1:
                    return True, ans
                return recur3(a + 1, b - 1, c, ls[1:], ans)
    elif s == "BC":
        if b == 0 and c == 0:
            return False, []
        else:
            if b >= c:
                ans.append("C")
                if ln == 1:
                    return True, ans
                return recur3(a, b - 1, c+1, ls[1:], ans)
            else:
                ans.append("B")
                if ln == 1:
                    return True, ans
                return recur3(a, b + 1, c-1, ls[1:], ans)
    else:
        if a == 0 and c == 0:
            return False, []
        else:
            if a >= c:
                ans.append("C")
                if ln == 1:
                    return True, ans
                return recur3(a - 1, b, c+1, ls[1:], ans)
            else:
                ans.append("A")
                if ln == 1:
                    return True, ans
                return recur3(a + 1, b, c-1, ls[1:], ans)



def main():
    n, a, b, c = map(int, input().split())
    s_ls = []
    for i in range(n):
        s_ls.append(input())

    res = True
    ans = []
    if a + b + c == 0:
        print("No")
    elif a + b + c == 1:
        res, ans = recur1(a, b, c, s_ls, ans)
    elif a + b + c == 2:
        res, ans = recur2(a, b, c, s_ls, ans)
    else:
        res, ans = recur3(a, b, c, s_ls, ans)

    if res:
        print("Yes")
        for x in ans:
            print(x)
    else:
        print("No")


main()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
sysread = sys.stdin.readline
sys.setrecursionlimit(10**7)
def run():
    pri = []
    N, A, B, C = map(int, sysread().split())
    vals = {'A':A, 'B':B, 'C':C}
    S = [sysread() for _ in range(N)]

    pri = []
    for s in S:
        if vals[s[0]] <= vals[s[1]]:
            vals[s[0]] += 1
            vals[s[1]] -= 1
            pri.append(s[0])
        else:
            vals[s[0]] -= 1
            vals[s[1]] += 1
            pri.append(s[1])
        if not check_vals(vals):
            print('No')
            return None
    print('Yes')
    for st in pri:
        print(st)

def check_vals(vals):
    cnt = 0
    over = 0
    for v in vals.values():
        if v < 0:
            return False
    return True

if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
from heapq import *
import sys
from collections import *
from itertools import *
from decimal import *
import copy
from bisect import *
import math
sys.setrecursionlimit(4100000)
def gcd(a,b):
    if(a%b==0):return(b)
    return (gcd(b,a%b))
input=lambda :sys.stdin.readline().rstrip()
N,A,B,C=map(int,input().split())
S=[input() for i in range(N)]
idleA=10**20
idleB=10**10
idleC=1
stack=[[0,A,B,C]]
lst={}
dp=[defaultdict(str) for i in range(N+1)]
def main(a,b,c):
    return(a*idleA+b*idleB+c)

while stack:
    #print(stack)
    n,a,b,c=stack.pop()
    if n==N:
        stack.append([n,a,b,c])
        break
    s=S[n]
    if s=="AB":
        if a>0:
            Q=main(a-1,b+1,c)
            if not dp[n+1][Q]:
                dp[n+1][Q]="B"
                stack.append([n+1,a-1,b+1,c])
        if b>0:
            Q=main(a+1,b-1,c)
            if not dp[n+1][Q]:
                dp[n+1][Q]="A"
                stack.append([n+1,a+1,b-1,c])
    elif s=="BC":
        if b>0:
            Q=main(a,b-1,c+1)
            if not dp[n+1][Q]:
                dp[n+1][Q]="C"
                stack.append([n+1,a,b-1,c+1])
        if c>0:
            Q=main(a,b+1,c-1)
            if not dp[n+1][Q]:
                dp[n+1][Q]="B"
                stack.append([n+1,a,b+1,c-1])
    elif s=="AC":
        if a>0:
            Q=main(a-1,b,c+1)
            if not dp[n+1][Q]:
                dp[n+1][Q]="C"
                stack.append([n+1,a-1,b,c+1])
        if c>0:
            Q=main(a+1,b,c-1)
            if not dp[n+1][Q]:
                dp[n+1][Q]="A"
                stack.append([n+1,a+1,b,c-1])

if stack:
    print("Yes")
    lst=[]
    n,a,b,c=stack.pop()

    lst.append([dp[n][main(a,b,c)],a,b,c])
    #print(lst)
    for n in range(N-1)[::-1]:
        s=S[n+1]
        X,a,b,c=lst[-1]
        if s=="AB":
            if a>0:
                Q=main(a-1,b+1,c)
                if dp[n+1][Q]:
                    lst.append([dp[n+1][Q],a-1,b+1,c])
                    continue
            if b>0:
                Q=main(a+1,b-1,c)
                if dp[n+1][Q]:
                    lst.append([dp[n+1][Q],a+1,b-1,c])
                    continue
        elif s=="BC":
            if b>0:
                Q=main(a,b-1,c+1)
                if dp[n+1][Q]:
                    lst.append([dp[n+1][Q],a,b-1,c+1])
                    continue
            if c>0:
                Q=main(a,b+1,c-1)
                if dp[n+1][Q]:
                    lst.append([dp[n+1][Q],a,b+1,c-1])
                    continue
        elif s=="AC":
            if a>0:
                Q=main(a-1,b,c+1)
                if dp[n+1][Q]:
                    lst.append([dp[n+1][Q],a-1,b,c+1])
                    continue
            if c>0:
                Q=main(a+1,b,c-1)
                if dp[n+1][Q]:
                    lst.append([dp[n+1][Q],a+1,b,c-1])
                    continue
        #print("a")
    for X,a,b,c in list(reversed(lst)):
        print(X)
else:
    print("No")

Submission status: Wrong Answer

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

n, a, b, c = map(int, input().split())
s = [input() for _ in range(n)]

a = min(a, 1)
b = min(b, 1)
c = min(c, 1)

ans = []

d = {"A": a, "B": b, "C": c}


def dfs(i):
    if min(d.values()) < 0:
        return

    if i == n:
        print("Yes")
        print(*ans, sep="\n")
        exit()

    k1, k2 = s[i]

    d[k1] += 1
    d[k2] -= 1
    ans.append(k1)
    dfs(i+1)
    d[k1] -= 1
    d[k2] += 1
    ans.pop()

    d[k2] += 1
    d[k1] -= 1
    ans.append(k2)
    dfs(i+1)
    d[k2] -= 1
    d[k1] += 1
    ans.pop()


dfs(0)

print("No")