Problem p02692 (python)

Fixed submission:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    def ng():
        print("No")
        exit()

    n,a,b,c=MI()
    ss=[SI() for _ in range(n)]
    ans=[]
    for i in range(n):
        s=ss[i]
        if s=="AB":
            if (a,b)==(0,0):ng()
            elif (a,b)==(0,1):
                ans.append("A")
                a,b=a+1,b-1
            elif (a,b)==(1,0):
                ans.append("B")
                a,b=a-1,b+1
            elif (a,b,c)==(1,1,0):
                if i==n-1:
                    ans.append("A")
                else:
                    if ss[i+1]=="AC":
                        ans.append("A")
                        a,b=a+1,b-1
                    else:
                        ans.append("B")
                        a, b = a - 1, b + 1
            else:
                if a<b:
                    ans.append("A")
                    a, b = a + 1, b - 1
                else:
                    ans.append("B")
                    a, b = a - 1, b + 1
        elif s=="BC":
            if (b,c)==(0,0):ng()
            elif (b,c)==(0,1):
                ans.append("B")
                b,c=b+1,c-1
            elif (b,c)==(1,0):
                ans.append("C")
                b,c=b-1,c+1
            elif (a,b,c)==(0,1,1):
                if i==n-1:
                    ans.append("B")
                else:
                    if ss[i+1]=="AC":
                        ans.append("C")
                        b, c = b - 1, c + 1
                    else:
                        ans.append("B")
                        b, c = b + 1, c - 1
            else:
                if c<b:
                    ans.append("C")
                    b, c = b - 1, c + 1
                else:
                    ans.append("B")
                    b, c = b + 1, c - 1
        else:
            if (a,c)==(0,0):ng()
            elif (a,c)==(0,1):
                ans.append("A")
                a,c=a+1,c-1
            elif (a,c)==(1,0):
                ans.append("C")
                a,c=a-1,c+1
            elif (a,b,c)==(1,0,1):
                if i==n-1:
                    ans.append("A")
                else:
                    if ss[i+1]=="AB":
                        ans.append("A")
                        a, c = a + 1, c - 1
                    else:
                        ans.append("C")
                        a, c = a - 1, c + 1
            else:
                if a<c:
                    ans.append("A")
                    a, c = a + 1, c - 1
                else:
                    ans.append("C")
                    a, c = a - 1, c + 1
    print("Yes")
    for s in ans:
        print(s)

main()

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    def ng():
        print("No")
        exit()

    n,a,b,c=MI()
    ss=[SI() for _ in range(n)]
    ans=[]
    for i in range(n):
        s=ss[i]
        if s=="AB":
            if (a,b)==(0,0):ng()
            elif (a,b)==(0,1):
                ans.append("A")
                a,b=a+1,b-1
            elif (a,b)==(1,0):
                ans.append("B")
                a,b=a-1,b+1
            elif (a,b,c)==(1,1,0):
                if i==n-1:
                    ans.append("A")
                else:
                    if s[i+1]=="AC":
                        ans.append("A")
                        a,b=a+1,b-1
                    else:
                        ans.append("B")
                        a, b = a - 1, b + 1
            else:
                if a<b:
                    ans.append("A")
                    a, b = a + 1, b - 1
                else:
                    ans.append("B")
                    a, b = a - 1, b + 1
        elif s=="BC":
            if (b,c)==(0,0):ng()
            elif (b,c)==(0,1):
                ans.append("B")
                b,c=b+1,c-1
            elif (b,c)==(1,0):
                ans.append("C")
                b,c=b-1,c+1
            elif (a,b,c)==(0,1,1):
                if i==n-1:
                    ans.append("B")
                else:
                    if s[i+1]=="AC":
                        ans.append("C")
                        b, c = b - 1, c + 1
                    else:
                        ans.append("B")
                        b, c = b + 1, c - 1
            else:
                if c<b:
                    ans.append("C")
                    b, c = b - 1, c + 1
                else:
                    ans.append("B")
                    b, c = b + 1, c - 1
        else:
            if (a,c)==(0,0):ng()
            elif (a,c)==(0,1):
                ans.append("A")
                a,c=a+1,c-1
            elif (a,c)==(1,0):
                ans.append("C")
                a,c=a-1,c+1
            elif (a,b,c)==(1,0,1):
                if i==n-1:
                    ans.append("A")
                else:
                    if s[i+1]=="AB":
                        ans.append("A")
                        a, c = a + 1, c - 1
                    else:
                        ans.append("C")
                        a, c = a - 1, c + 1
            else:
                if a<c:
                    ans.append("A")
                    a, c = a + 1, c - 1
                else:
                    ans.append("C")
                    a, c = a - 1, c + 1
    print("Yes")
    for s in ans:
        print(s)

main()