Problem p02692 (python)

Fixed submission:
n, a, b, c = map(int, input().split())
ans = []
prev = ''
for _ in range(n):
    s = input()
    if 'C' not in s:
        if a == b == 0 and ans:
            # キングクリムゾン発動！
            prev_x = ans.pop()
            if prev_x == prev[0]:
                prev_y = prev[1]
            else:
                prev_y = prev[0]
            ans.append(prev_y)
            if prev_x == 'A':
                a -= 2
            elif prev_x == 'B':
                b -= 2
            else:
                c -= 2
            if prev_y == 'A':
                a += 2
            elif prev_y == 'B':
                b += 2
            else:
                c += 2
        if a < b:
            a += 1
            b -= 1
            ans.append('A')
        else:
            a -= 1
            b += 1
            ans.append('B')

    elif 'B' not in s:
        if a == c == 0 and ans:
            # キングクリムゾン発動！
            prev_x = ans.pop()
            if prev_x == prev[0]:
                prev_y = prev[1]
            else:
                prev_y = prev[0]
            ans.append(prev_y)

            if prev_x == 'A':
                a -= 2
            elif prev_x == 'B':
                b -= 2
            else:
                c -= 2
            if prev_y == 'A':
                a += 2
            elif prev_y == 'B':
                b += 2
            else:
                c += 2
        if a < c:
            a += 1
            c -= 1
            ans.append('A')
        else:
            a -= 1
            c += 1
            ans.append('C')
    elif 'A' not in s:
        if b == c == 0 and ans:
            # キングクリムゾン発動！
            prev_x = ans.pop()
            if prev_x == prev[0]:
                prev_y = prev[1]
            else:
                prev_y = prev[0]
            ans.append(prev_y)
            if prev_x == 'A':
                a -= 2
            elif prev_x == 'B':
                b -= 2
            else:
                c -= 2
            if prev_y == 'A':
                a += 2
            elif prev_y == 'B':
                b += 2
            else:
                c += 2
        if b < c:
            b += 1
            c -= 1
            ans.append('B')
        else:
            b -= 1
            c += 1
            ans.append('C')
    prev = s
    if -1 in (a, b, c):
        print('No')
        break
else:
    print('Yes')
    for x in ans:
        print(x)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n, *abc = map(int, input().split())
s = [input()for _ in range(n)]
cond = {'AB': (0, 1), 'AC': (0, 2), 'BC': (1, 2)}
legend = ('A', 'B', 'C')
ans = []


def dfs(i):
    if -1 in abc:
        return 0
    elif i == n:
        print('Yes')
        print(*ans, sep='\n')
        exit()
        # return 0
    else:
        idx1, idx2 = cond[s[i]]
        abc[idx1] += 1
        abc[idx2] -= 1
        ans.append(legend[idx1])
        dfs(i+1)
        ans.pop()
        abc[idx1] -= 1
        abc[idx2] += 1

        abc[idx1] -= 1
        abc[idx2] += 1
        ans.append(legend[idx2])
        dfs(i+1)
        ans.pop()
        abc[idx1] += 1
        abc[idx2] -= 1


dfs(0)
print('No')