Problem p02692 (python)

Fixed submission:
import sys
sys.setrecursionlimit(2147483647)
INF=float('inf')
MOD=10**9+7

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

s=[input() for _ in range(n)]

#不可能な場合
if a+b+c==0:
    print("No")
    exit()
elif a+b+c==1:
    result=[]
    for order in s:
        if "AB" == order:
            if c==1:
                print("No")
                exit()
            elif a==1:
                a-=1
                b+=1
                result.append("B")
            else:
                b-=1
                a+=1
                result.append("A")
        elif "AC" == order:
            if b==1:
                print("No")
                exit()
            elif a==1:
                a-=1
                c+=1
                result.append("C")
            else:
                c-=1
                a+=1
                result.append("A")
        elif "BC" == order:
            if a==1:
                print("No")
                exit()
            elif b==1:
                b-=1
                c+=1
                result.append("C")
            else:
                c-=1
                b+=1
                result.append("B")
    print("Yes")
    for item in result:
        print(item)
    exit()

else:
    result=[]
    for index,order in enumerate(s):
        if "AB" == order:
            if a==0 and b==0:
                print("No")
                exit()
            elif a==1 and b==1:
                if index <n-1:
                    if s[index+1]=="AB":
                        result.append("B")
                        a-=1
                        b+=1
                    elif s[index+1]=="AC":
                        result.append("A")
                        a+=1
                        b-=1
                    elif s[index+1]=="BC":
                        result.append("B")
                        a-=1
                        b+=1
                else:
                    a-=1
                    b+=1
                    result.append("B")
            else:
                if a<b:
                    a+=1
                    b-=1
                    result.append("A")
                else:
                    a-=1
                    b+=1
                    result.append("B")
                    
#ここっからまた変わるよ
        elif "AC" == order:
            if a==0 and c==0:
                print("No")
                exit()
            elif a==1 and c==1:
                if index <n-1:
                    if s[index+1]=="AB":
                        result.append("A")
                        a+=1
                        c-=1
                    elif s[index+1]=="AC":
                        result.append("A")
                        a+=1
                        c-=1
                    elif s[index+1]=="BC":
                        result.append("C")
                        a-=1
                        c+=1
                else:
                    a-=1
                    c+=1
                    result.append("C")
            else:
                if a<c:
                    a+=1
                    c-=1
                    result.append("A")
                else:
                    a-=1
                    c+=1
                    result.append("C")

#ここっからまた変わるよ
        elif "BC" == order:
            if b==0 and c==0:
                print("No")
                exit()
            elif b==1 and c==1:
                if index <n-1:
                    if s[index+1]=="AB":
                        result.append("B")
                        b+=1
                        c-=1
                    elif s[index+1]=="AC":
                        result.append("C")
                        c+=1
                        b-=1
                    elif s[index+1]=="BC":
                        result.append("C")
                        b-=1
                        c+=1
                else:
                    c-=1
                    b+=1
                    result.append("B")
            else:
                if b<c:
                    b+=1
                    c-=1
                    result.append("B")
                else:
                    b-=1
                    c+=1
                    result.append("C")

        
    print("Yes")
    for item in result:
        print(item)
    exit()

Buggy submission (verdict: Runtime Error):
n,a,b,c=map(int,input().split())

s=[input() for _ in range(n)]

#不可能な場合
if a+b+c==0:
    print("No")
    exit()

result=[]

abc=[a,b,c]
order=[]

def fukasa(order,abc,i):
    #print(order,abc,i)
    global n,s
    if -1 in abc:
        return 0

    if i== n:
        print("Yes")
        for item in order:
            print(item)
        exit()
    else:
        if s[i]=="AB":
            for index,item in enumerate([[1,-1,0],[-1,1,0]]):
                fukasa(order+[s[i][index]],[abc[0]+item[0],abc[1]+item[1],abc[2]+item[2]],i+1)
        if s[i]=="AC":
            for index,item in enumerate([[1,0,-1],[-1,0,1]]):
                fukasa(order+[s[i][index]],[abc[0]+item[0],abc[1]+item[1],abc[2]+item[2]],i+1)
        if s[i]=="BC":
            for index,item in enumerate([[0,1,-1],[0,-1,1]]):
                fukasa(order+[s[i][index]],[abc[0]+item[0],abc[1]+item[1],abc[2]+item[2]],i+1)

fukasa([],[a,b,c],0)
print("No")