Problem p02692 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
N,A,B,C = map(int,input().split())
Q = []
ans = []
f = 0
def dfs(f,a,b,c):
    if f == N:return True
    if Q[f] == 'AB':
        if a+b == 0:return False
        if b>0 and dfs(f+1,a+1,b-1,c):
            ans.append('A')
            return True
        if a>0 and dfs(f+1,a-1,b+1,c):
            ans.append('B')
            return True
        
        return False
    if Q[f] == 'BC':
        if c+b == 0:return False
        if c>0 and dfs(f+1,a,b+1,c-1):
            ans.append('B')
            return True
        if b>0 and dfs(f+1,a,b-1,c+1):
            ans.append('C')
            return True
        
        return False
    if Q[f] == 'AC':
        if a+c == 0:return False
        if c>0 and dfs(f+1,a+1,b,c-1):
            ans.append('A')
            return True
        if a>0 and dfs(f+1,a-1,b,c+1):
            ans.append('C')
            return True
        
        return False
    

for i in range(N):
    s = input()
    Q.append(s)
dfs(0,A,B,C)
# print(ans)
if len(ans) == N:
    print('Yes')
    ans = ans[::-1]
    for i in ans:
        print(i)
else:
    print('No')

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**5)
N,A,B,C = map(int,input().split())
Q = []
ans = []
f = 0
def dfs(f,a,b,c):
    if f == N:return True
    if Q[f] == 'AB':
        if a>0 and dfs(f+1,a-1,b+1,c):
            ans.append('A')
            return True
        if b>0 and dfs(f+1,a+1,b-1,c):
            ans.append('B')
            return True
        
        return False
    if Q[f] == 'BC':
        if b>0 and dfs(f+1,a,b-1,c+1):
            ans.append('B')
            return True
        if c>0 and dfs(f+1,a,b+1,c-1):
            ans.append('C')
            return True
        
        return False
    if Q[f] == 'AC':
        if a>0 and dfs(f+1,a-1,b,c+1):
            ans.append('A')
            return True
        if c>0 and dfs(f+1,a+1,b,c-1):
            ans.append('C')
            return True
        
        return False
    

for i in range(N):
    s = input()
    Q.append(s)
dfs(0,A,B,C)
# print(ans)
if len(ans) == N:
    print('Yes')
    ans = ans[::-1]
    for i in ans:
        print(i)
else:
    print('No')

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**5)
N,A,B,C = map(int,input().split())
Q = []
ans = []
f = 0
def dfs(f,a,b,c):
    if f == N-1:return True
    if Q[f] == 'AB':
        if a>0:
            dfs(f+1,a-1,b+1,c)
            ans.append('A')
        if b>0:
            dfs(f+1,a+1,b-1,c)
            ans.append('B')
        
        return False
    if Q[f] == 'BC':
        if b>0:
            dfs(f+1,a,b-1,c+1)
            ans.append('B')
        if c>0:
            dfs(f+1,a,b+1,c-1)
            ans.append('C')
        
        return False
    if Q[f] == 'AC':
        if a>0:
            dfs(f+1,a-1,b,c+1)
            ans.append('A')
        if c>0:
            dfs(f+1,a+1,b,c-1)
            ans.append('C')
        
        return False
    

for i in range(N):
    s = input()
    Q.append(s)
dfs(0,A,B,C)

if len(ans) == N:
    print('Yes')
    ans = ans[::-1]
    for i in ans:
        print(i)
else:
    print('No')