Problem p02692 (python)

Fixed submission:
N,A,B,C=map(int,input().split())
S=[input() for i in range(N)]
ans=[]
X=A+B+C
if X==1:
    for i in range(N):
        if S[i]=="AB":
            if A>0:
                A-=1
                B+=1
                ans.append("B")
            elif B>0:
                A+=1
                B-=1
                ans.append("A")
            else:
                print("No")
                exit()
        elif S[i]=="BC":
            if B>0:
                B-=1
                C+=1
                ans.append("C")
            elif C>0:
                B+=1
                C-=1
                ans.append("B")
            else:
                print("No")
                exit()
        else:
            if C>0:
                C-=1
                A+=1
                ans.append("A")
            elif A>0:
                C+=1
                A-=1
                ans.append("C")
            else:
                print("No")
                exit()
    print("Yes")
    for i in range(N):
        print(ans[i])
    exit()
if X==2:
    for i in range(N):
        if S[i]=="AB":
            if A==0 and B==0:
                print("No")
                exit()
            elif A==1 and B==1:
                if i<N-1 and S[i+1]=="AC":
                    ans.append("A")
                    A+=1
                    B-=1
                else:
                    ans.append("B")
                    B+=1
                    A-=1
            elif A==0:
                ans.append("A")
                A+=1
                B-=1
            else:
                ans.append("B")
                B+=1
                A-=1
        elif S[i]=="BC":
            if B==0 and C==0:
                print("No")
                exit()
            elif B==1 and C==1:
                if i<N-1 and S[i+1]=="AB":
                    ans.append("B")
                    B+=1
                    C-=1
                else:
                    ans.append("C")
                    C+=1
                    B-=1
            elif B==0:
                ans.append("B")
                B+=1
                C-=1
            else:
                ans.append("C")
                C+=1
                B-=1
        else:
            if C==0 and A==0:
                print("No")
                exit()
            elif C==1 and A==1:
                if i<N-1 and S[i+1]=="BC":
                    ans.append("C")
                    C+=1
                    A-=1
                else:
                    ans.append("A")
                    A+=1
                    C-=1
            elif C==0:
                ans.append("C")
                C+=1
                A-=1
            else:
                ans.append("A")
                A+=1
                C-=1
    print("Yes")
    for i in range(N):
        print(ans[i])
    exit()
for i in range(N):
    if S[i]=="AB":
        if A==0 and B==0:
            print("No")
            exit()
        elif A+1==X:
            ans.append("B")
            A-=1
            B+=1
        elif B+1==X:
            ans.append("A")
            A+=1
            B-=1
        elif A==0:
            ans.append("A")
            A+=1
            B-=1
        elif B==0:
            ans.append("B")
            A-=1
            B+=1
        else:
            ans.append("A")
            A+=1
            B-=1
    elif S[i]=="BC":
        if B==0 and C==0:
            print("No")
            exit()
        elif B+1==X:
            ans.append("C")
            B-=1
            C+=1
        elif C+1==X:
            ans.append("B")
            B+=1
            C-=1
        elif B==0:
            ans.append("B")
            B+=1
            C-=1
        elif C==0:
            ans.append("C")
            B-=1
            C+=1
        else:
            ans.append("B")
            B+=1
            C-=1
    else:
        if C==0 and A==0:
            print("No")
            exit()
        elif C+1==X:
            ans.append("A")
            C-=1
            A+=1
        elif A+1==X:
            ans.append("C")
            C+=1
            A-=1
        elif C==0:
            ans.append("C")
            C+=1
            A-=1
        elif A==0:
            ans.append("A")
            C-=1
            A+=1
        else:
            ans.append("C")
            C+=1
            A-=1
print("Yes")
for i in range(N):
    print(ans[i])

Buggy submission (verdict: Runtime Error):
N,A,B,C=map(int,input().split())
S=[input() for i in range(N)]
ans=[]
X=A+B+C
if X==1:
    #TLE
    j=0
    for i in range(10**15):
        j+=i*i*i
if X==2:
    #RE
    P=[0,1,2]
    Y=P[10]
if X==1:
    for i in range(N):
        if S[i]=="AB":
            if A>0:
                A-=1
                B+=1
                ans.append("B")
            elif B>0:
                A+=1
                B-=1
                ans.append("A")
            else:
                print("No")
                exit()
        elif S[i]=="BC":
            if B>0:
                B-=1
                C+=1
                ans.append("C")
            elif C>0:
                B+=1
                C-=1
                ans.append("B")
            else:
                print("No")
                exit()
        else:
            if C>0:
                C-=1
                A+=1
                ans.append("A")
            elif A>0:
                C+=1
                A-=1
                ans.append("C")
            else:
                print("No")
                exit()
    print("Yes")
    for i in range(N):
        print(ans[i])
    exit()
if X==2:
    for i in range(N):
        if S[i]=="AB":
            if A==0 and B==0:
                print("No")
                exit()
            elif A==1 and B==1:
                if i<N-1 and S[i+1]=="AC":
                    ans.append("A")
                    A+=1
                    B-=1
                else:
                    ans.append("B")
                    B+=1
                    A-=1
            elif A==0:
                ans.append("A")
                A+=1
                B-=1
            else:
                ans.append("B")
                B+=1
                A-=1
        elif S[i]=="BC":
            if B==0 and C==0:
                print("No")
                exit()
            elif B==1 and C==1:
                if i<N-1 and S[i+1]=="AB":
                    ans.append("B")
                    B+=1
                    C-=1
                else:
                    ans.append("C")
                    C+=1
                    B-=1
            elif B==0:
                ans.append("B")
                B+=1
                C-=1
            else:
                ans.append("C")
                C+=1
                B-=1
        else:
            if C==0 and A==0:
                print("No")
                exit()
            elif C==1 and A==1:
                if i<N-1 and S[i+1]=="BC":
                    ans.append("C")
                    C+=1
                    A-=1
                else:
                    ans.append("A")
                    A+=1
                    C-=1
            elif C==0:
                ans.append("C")
                C+=1
                A-=1
            else:
                ans.append("A")
                A+=1
                C-=1
    print("Yes")
    for i in range(N):
        print(ans[i])
    exit()
for i in range(N):
    if S[i]=="AB":
        if A==0 and B==0:
            print("No")
            exit()
        elif A+1==X:
            ans.append("B")
            A-=1
            B+=1
        elif B+1==X:
            ans.append("A")
            A+=1
            B-=1
        elif A==0:
            ans.append("A")
            A+=1
            B-=1
        elif B==0:
            ans.append("B")
            A-=1
            B+=1
        else:
            ans.append("A")
            A+=1
            B-=1
    elif S[i]=="BC":
        if B==0 and C==0:
            print("No")
            exit()
        elif B+1==X:
            ans.append("C")
            B-=1
            C+=1
        elif C+1==X:
            ans.append("B")
            B+=1
            C-=1
        elif B==0:
            ans.append("B")
            B+=1
            C-=1
        elif C==0:
            ans.append("C")
            B-=1
            C+=1
        else:
            ans.append("B")
            B+=1
            C-=1
    else:
        if C==0 and A==0:
            print("No")
            exit()
        elif C+1==X:
            ans.append("A")
            C-=1
            A+=1
        elif A+1==X:
            ans.append("C")
            C+=1
            A-=1
        elif C==0:
            ans.append("C")
            C+=1
            A-=1
        elif A==0:
            ans.append("A")
            C-=1
            A+=1
        else:
            ans.append("C")
            C+=1
            A-=1
print("Yes")
for i in range(N):
    print(ans[i])