Problem p02692 (python)

Fixed submission:
MOD = 10 ** 9 + 7
INF = 10 ** 11
import sys
sys.setrecursionlimit(100000000)

def main():
    N,A,B,C = map(int,input().split())
    S = [input() for _ in range(N)]

    ans = []
    for i in range(N):
        s = S[i]
        if s == 'AB':
            if A > 1 and B > 1:
                if A > B:
                    A -= 1
                    B += 1
                    ans.append('B')
                else:
                    B -= 1
                    A += 1
                    ans.append('A')
            elif A == 1 and B > 1:
                B -= 1
                A += 1
                ans.append('A')
            elif A > 1  and B == 1:
                A -= 1
                B += 1
                ans.append('B')
            elif A == 1 and B == 1:
                if i + 1 < N:
                    if S[i + 1] == 'AB':
                        A += 1
                        B -= 1
                        ans.append('A')
                    elif S[i + 1] == 'BC':
                        B += 1
                        A -= 1
                        ans.append('B')
                    else:
                        A += 1
                        B -= 1
                        ans.append('A')
                else:
                    ans.append('A')
            elif A == 0 and B == 0:
                print('No')
                return
            elif A == 0:
                A += 1
                B -= 1
                ans.append('A')
            elif B == 0:
                B += 1
                A -= 1
                ans.append('B')
        
        elif s == 'BC':
            if C > 1 and B > 1:
                if C > B:
                    C -= 1
                    B += 1
                    ans.append('B')
                else:
                    B -= 1
                    C += 1
                    ans.append('C')
            elif C == 1 and B > 1:
                B -= 1
                C += 1
                ans.append('C')
            elif C > 1  and B == 1:
                C -= 1
                B += 1
                ans.append('B')
            elif C == 1 and B == 1:
                if i + 1 < 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')
                    else:
                        C += 1
                        B -= 1
                        ans.append('C')
                else:
                    ans.append('B')
            elif C == 0 and B == 0:
                print('No')
                return
            elif C == 0:
                C += 1
                B -= 1
                ans.append('C')
            elif B == 0:
                B += 1
                C -= 1
                ans.append('B')
        
        elif s == 'AC':
            if A > 1 and C > 1:
                if A > C:
                    A -= 1
                    C += 1
                    ans.append('C')
                else:
                    C -= 1
                    A += 1
                    ans.append('A')
            elif A == 1 and C > 1:
                C -= 1
                A += 1
                ans.append('A')
            elif A > 1  and C == 1:
                A -= 1
                C += 1
                ans.append('C')
            elif A == 1 and C == 1:
                if i + 1 < N:
                    if S[i + 1] == 'AB':
                        A += 1
                        C -= 1
                        ans.append('A')
                    elif S[i + 1] == 'BC':
                        C += 1
                        A -= 1
                        ans.append('C')
                    else:
                        A += 1
                        C -= 1
                        ans.append('A')
                else:
                    ans.append('A')
            elif A == 0 and C == 0:
                print('No')
                return
            elif A == 0:
                A += 1
                C -= 1
                ans.append('A')
            elif C == 0:
                C += 1
                A -= 1
                ans.append('C')

    print('Yes')
    print(*ans,sep = '\n')
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
MOD = 10 ** 9 + 7
INF = 10 ** 11
import sys
sys.setrecursionlimit(100000000)

def main():
    N,A,B,C = map(int,input().split())
    choice = [input() for _ in range(N)]
    ab = 0
    bc = 0
    ca = 0
    for s in choice:
        if s == 'AB':
            ab += 1
        elif s == 'BC':
            bc += 1
        else:
            ca += 1
    
    ans = [[0,0] for _ in range(3)]
    if A > 0 or B > 0:
        ans[0][0] += ab//2
        ans[0][1] += ab//2
        ab %= 2
    else:
        if bc > 0 and C > 0:
            bc -= 1
            C -= 1
            B += 1
            ans[1][0] += 1
        elif ca > 0 and C > 0:
            ca -= 1
            C -= 1
            A += 1
            ans[2][0] += 1
        else:
            print('No')
            return
        ans[0][0] += ab//2
        ans[0][1] += ab//2
        ab %= 2
        
    if B > 0 or C > 0:
        ans[1][0] += bc//2
        ans[1][1] += bc//2
        bc %= 2
    else:
        if ab > 0 and A > 0:
            A -= 1
            B += 1
            ab -= 1
            ans[0][1] += 1
        elif ca > 0 and A > 0:
            A -= 1
            C += 1
            ca -= 1
            ans[2][0] += 1
        else:
            print('No')
            return
        ans[1][0] += bc//2
        ans[1][1] += bc//2
        bc %= 2
                
    if C > 0 or A > 0:
        ans[2][0] += ca//2
        ans[2][1] += ca//2
        ca %= 2
    else:
        if cb > 0 and B > 0:
            B -= 1
            C += 1
            cb -= 1
            ans[1][1] += 1
        elif ab > 0 and B > 0:
            B -= 1
            A += 1
            ab -= 1
            ans[0][0] += 1
        else:
            print('No')
            return
        ans[1][0] += bc//2
        ans[1][1] += bc//2
        bc %= 2
    
    if ab > 0 and bc > 0 and ca > 0:
        if A > 0 or B > 0 or C > 0:
            ans[0][1] += 1
            ans[1][1] += 1
            ans[2][1] += 1
        else:
            print('No')
            return
    elif ab > 0 and bc > 0:
        if A > 0:
            ans[0][1] += 1
            ans[1][1] += 1
        elif C > 0:
            ans[1][0] += 1
            ans[0][0] += 1
        elif B > 1:
            ans[0][0] += 1
            ans[1][1] += 1
        else:
            print('No')
            return
    elif bc > 0 and ca > 0:
        if A > 0:
            ans[2][0] += 1
            ans[1][0] += 1
        elif B > 0:
            ans[1][1] += 1
            ans[2][0] += 1
        elif C > 1:
            ans[1][0] += 1
            ans[2][1] += 1
        else:
            print('No')
            return
    elif ca > 0 and ab > 0:
        if C > 0:
            ans[2][0] += 1
            ans[0][1] += 1
        elif B > 0:
            ans[1][0] += 1
            ans[2][0] += 1
        elif A > 1:
            ans[0][1] += 1
            ans[2][1] += 1
        else:
            print('No')
            return
    
    elif ab > 0:
        if A > 0:
            ans[0][1] += 1
        elif B > 0:
            ans[0][0] += 1
        else:
            print('No')
            return
    
    elif bc > 0:
        if B > 0:
            ans[1][1] += 1
        elif C > 0:
            ans[1][0] += 1
        else:
            print('No')
            return
    
    elif ca > 0:
        if A > 0:
            ans[2][1] += 1
        elif C > 0:
            ans[2][0] += 1
        else:
            print('No')
            return
    

    print('Yes')
    for s in choice:
        if s == 'AB':
            if ans[0][0] > 0:
                print('A')
                ans[0][0] -= 1
            elif ans[0][1] > 0:
                print('B')
        elif s == 'BC':
            if ans[1][0] > 0:
                print('B')
                ans[1][0] -= 1
            else:
                print('C')
        else:
            if ans[2][0] > 0:
                print('C')
                ans[2][0] -= 1
            else:
                print('A')

if __name__ == '__main__':
    main()