Problem p02692 (python)

Fixed submission:
N,A,B,C = list(map(int,input().split()))

S = [input() for i in range(N)]

if A+B+C==0:
    print("No")
elif A+B+C==1:
    ans = []
    flag = True
    for i in range(N):
        if S[i]=="AB":
            if A==0 and B==0:
                print("No")
                flag = False
                break
            else:
                if A==0:
                    ans.append("A")
                else:
                    ans.append("B")
                A,B = B,A
        elif S[i]=="BC":
            if B==0 and C==0:
                print("No")
                flag = False
                break
            else:
                if B==0:
                    ans.append("B")
                else:
                    ans.append("C")
                B,C = C,B
        elif S[i]=="AC":
            if A==0 and C==0:
                print("No")
                flag = False
                break
            else:
                if A==0:
                    ans.append("A")
                else:
                    ans.append("C")
                A,C = C,A
    if flag:
        print("Yes")
        for a in ans:
            print(a)
elif A+B+C>=2:
    ans = []
    flag = True
    for i in range(N):
        if S[i]=="AB":
            if A==0 and B==0:
                print("No")
                flag = False
                break
            else:
                if B==0:
                    B += 1
                    A -= 1
                    ans.append("B")
                elif A==0:
                    B -= 1
                    A += 1
                    ans.append("A")
                elif A==1 and B==1:
                    if i<N-1:
                        if S[i+1]=="AB":
                            A+=1
                            B-=1
                            ans.append("A")
                        elif S[i+1]=="BC":
                            A-=1
                            B+=1
                            ans.append("B")
                        elif S[i+1]=="AC":
                            A+=1
                            B-=1
                            ans.append("A")
                    else:
                        A+=1
                        B-=1
                        ans.append("A")
                else:
                    if A<=B:
                        A+=1
                        B-=1
                        ans.append("A")
                    else:
                        A-=1
                        B+=1
                        ans.append("B")
        elif S[i]=="BC":
            if B==0 and C==0:
                print("No")
                flag = False
                break
            else:
                if B==0:
                    B += 1
                    C -= 1
                    ans.append("B")
                elif C==0:
                    B -= 1
                    C += 1
                    ans.append("C")
                elif B==1 and C==1:
                    if i<N-1:
                        if S[i+1]=="AB":
                            C-=1
                            B+=1
                            ans.append("B")
                        elif S[i+1]=="BC":
                            B+=1
                            C-=1
                            ans.append("B")
                        elif S[i+1]=="AC":
                            B-=1
                            C+=1
                            ans.append("C")
                    else:
                        B+=1
                        C-=1
                        ans.append("B")
                else:
                    if B<=C:
                        B+=1
                        C-=1
                        ans.append("B")
                    else:
                        B-=1
                        C+=1
                        ans.append("C")
        if S[i]=="AC":
            if A==0 and C==0:
                print("No")
                flag = False
                break
            else:
                if C==0:
                    C += 1
                    A -= 1
                    ans.append("C")
                elif A==0:
                    C -= 1
                    A += 1
                    ans.append("A")
                elif A==1 and C==1:
                    if i<N-1:
                        if S[i+1]=="AB":
                            A+=1
                            C-=1
                            ans.append("A")
                        elif S[i+1]=="BC":
                            A-=1
                            C+=1
                            ans.append("C")
                        elif S[i+1]=="AC":
                            A+=1
                            C-=1
                            ans.append("A")
                    else:
                        A+=1
                        C-=1
                        ans.append("A")
                else:
                    if A<=C:
                        A+=1
                        C-=1
                        ans.append("A")
                    else:
                        A-=1
                        C+=1
                        ans.append("C")
    if flag:
        print("Yes")
        for a in ans:
            print(a)

Buggy submission (verdict: Runtime Error):
N,A,B,C = list(map(int,input().split()))

S = [input() for i in range(N)]

if A+B+C==0:
    print("No")
elif A+B+C==1:
    ans = []
    flag = True
    for i in range(N):
        if S[i]=="AB":
            if A==0 and B==0:
                print("No")
                flag = False
                break
            else:
                if A==0:
                    ans.append("A")
                else:
                    ans.append("B")
                A,B = B,A
        elif S[i]=="BC":
            if B==0 and C==0:
                print("No")
                flag = False
                break
            else:
                if B==0:
                    ans.append("B")
                else:
                    ans.append("C")
                B,C = C,B
        elif S[i]=="AC":
            if A==0 and C==0:
                print("No")
                flag = False
                break
            else:
                if A==0:
                    ans.append("A")
                else:
                    ans.append("C")
                A,C = C,A
    if flag:
        print("Yes")
        for a in ans:
            print(a)
elif A+B+C>=2:
    ans = []
    flag = True
    for i in range(N):
        if S[i]=="AB":
            if A==0 and B==0:
                print("No")
                flag = False
                break
            else:
                if B==0:
                    B += 1
                    A -= 1
                    ans.append("B")
                elif A==0:
                    B -= 1
                    A += 1
                    ans.append("A")
                elif A==1 and B==1:
                    if i<N:
                        if S[i+1]=="AB":
                            A+=1
                            B-=1
                            ans.append("A")
                        elif S[i+1]=="BC":
                            A-=1
                            B+=1
                            ans.append("B")
                        elif S[i+1]=="AC":
                            A+=1
                            B-=1
                            ans.append("A")
                    else:
                        A+=1
                        B-=1
                        ans.append("A")
                else:
                    if A<=B:
                        A+=1
                        B-=1
                        ans.append("A")
                    else:
                        A-=1
                        B+=1
                        ans.append("B")
        elif S[i]=="BC":
            if B==0 and C==0:
                print("No")
                flag = False
                break
            else:
                if B==0:
                    B += 1
                    C -= 1
                    ans.append("B")
                elif C==0:
                    B -= 1
                    C += 1
                    ans.append("C")
                elif B==1 and C==1:
                    if i<N:
                        if S[i+1]=="AB":
                            C-=1
                            B+=1
                            ans.append("B")
                        elif S[i+1]=="BC":
                            B+=1
                            C-=1
                            ans.append("B")
                        elif S[i+1]=="AC":
                            B-=1
                            C+=1
                            ans.append("C")
                    else:
                        B+=1
                        C-=1
                        ans.append("B")
                else:
                    if B<=C:
                        B+=1
                        C-=1
                        ans.append("B")
                    else:
                        B-=1
                        C+=1
                        ans.append("C")
        if S[i]=="AC":
            if A==0 and C==0:
                print("No")
                flag = False
                break
            else:
                if C==0:
                    C += 1
                    A -= 1
                    ans.append("C")
                elif A==0:
                    C -= 1
                    A += 1
                    ans.append("A")
                elif A==1 and C==1:
                    if i<N:
                        if S[i+1]=="AB":
                            A+=1
                            C-=1
                            ans.append("A")
                        elif S[i+1]=="BC":
                            A-=1
                            C+=1
                            ans.append("C")
                        elif S[i+1]=="AC":
                            A+=1
                            C-=1
                            ans.append("A")
                    else:
                        A+=1
                        C-=1
                        ans.append("A")
                else:
                    if A<=C:
                        A+=1
                        C-=1
                        ans.append("A")
                    else:
                        A-=1
                        C+=1
                        ans.append("C")
    if flag:
        print("Yes")
        for a in ans:
            print(a)