Problem p02692 (python)

Fixed submission:
import sys

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

def check(i, A, B, C):
    if A < 0 or B < 0 or C < 0:
        return False
    if Q[i] == 'AB':
        return not (A == 0 and B == 0)
    if Q[i] == 'AC':
        return not (A == 0 and C == 0)
    return not (B == 0 and C == 0)

L = []
for i in range(N):
    if i == N-1:
        if Q[i] == 'AB':
            if A == 0 and B == 0:
                print('No')
                sys.exit()
            elif A == 0:
                L.append('A')
                A += 1
                B -= 1
                if B < 0:
                    print('No')
                    sys.exit()
            else:
                L.append('B')
                A -= 1
                B += 1
                if A < 0:
                    print('No')
                    sys.exit()
        elif Q[i] == 'AC':
            if A == 0 and C == 0:
                print('No')
                sys.exit()
            elif A == 0:
                L.append('A')
                A += 1
                C -= 1
                if C < 0:
                    print('No')
                    sys.exit()
            else:
                L.append('C')
                A -= 1
                C += 1
                if A < 0:
                    print('No')
                    sys.exit()
        else:
            if B == 0 and C == 0:
                print('No')
                sys.exit()
            elif B == 0:
                L.append('B')
                B += 1
                C -= 1
                if C < 0:
                    print('No')
                    sys.exit()
            else:
                L.append('C')
                B -= 1
                C += 1
                if B < 0:
                    print('No')
                    sys.exit()
    else:
        if not check(i, A, B, C):
            print('No')
            sys.exit()
        if Q[i] == 'AB':
            if check(i+1, A+1, B-1, C):
                L.append('A')
                A += 1
                B -= 1
                if B < 0:
                    print('No')
                    sys.exit()
            elif check(i+1, A-1, B+1, C):
                L.append('B')
                A -= 1
                B += 1
                if A < 0:
                    print('No')
                    sys.exit()
            else:
                print('No')
                sys.exit()
        elif Q[i] == 'AC':
            if check(i+1, A+1, B, C-1):
                L.append('A')
                A += 1
                C -= 1
                if C < 0:
                    print('No')
                    sys.exit()
            elif check(i+1, A-1, B, C+1):
                L.append('C')
                A -= 1
                C += 1
                if A < 0:
                    print('No')
                    sys.exit()
            else:
                print('No')
                sys.exit()
        else:
            if check(i+1, A, B+1, C-1):
                L.append('B')
                B += 1
                C -= 1
                if C < 0:
                    print('No')
                    sys.exit()
            elif check(i+1, A, B-1, C+1):
                L.append('C')
                B -= 1
                C += 1
                if B < 0:
                    print('No')
                    sys.exit()
            else:
                print('No')
                sys.exit()


print('Yes')
for i in L:
    print(i)

Buggy submission (verdict: Runtime Error):
N, A, B, C = map(int,input().split())
Q = []
for _ in range(N):
    Q.append(input())

def check(i, A, B, C):
    if A < 0 or B < 0 or C < 0:
        return False
    if Q[i] == 'AB':
        return not (A == 0 and B == 0)
    if Q[i] == 'AC':
        return not (A == 0 and C == 0)
    return not (B == 0 and C == 0)

def solve(L, A, B, C):
    if len(L) == N:
        return L
    i = len(L)
    if i == N-1:
        if Q[i] == 'AB':
            if A == 0 and B == 0:
                return 'No'
            if A == 0:
                return solve(L + ['A'], A+1, B-1, C)
            return solve(L + ['B'], A-1, B+1, C)
        if Q[i] == 'AC':
            if A == 0 and C == 0:
                return 'No'
            if A == 0:
                return solve(L + ['A'], A+1, B, C-1)
            return solve(L + ['C'], A-1, B, C+1)
        if Q[i] == 'BC':
            if B == 0 and C == 0:
                return 'No'
            if B == 0:
                return solve(L + ['B'], A, B+1, C-1)
            return solve(L + ['C'], A, B-1, C+1)
    if not check(i, A, B, C):
        return 'No'
    if Q[i] == 'AB':
        if check(i+1, A+1, B-1, C):
            return solve(L + ['A'], A+1, B-1, C)
        if check(i+1, A-1, B+1, C):
            return solve(L + ['B'], A-1, B+1, C)
        return 'No'
    if Q[i] == 'AC':
        if check(i+1, A+1, B, C-1):
            return solve(L + ['A'], A+1, B, C-1)
        if check(i+1, A-1, B, C+1):
            return solve(L + ['C'], A-1, B, C+1)
        return 'No'
    if Q[i] == 'BC':
        if check(i+1, A, B+1, C-1):
            return solve(L + ['B'], A, B+1, C-1)
        if check(i+1, A, B-1, C+1):
            return solve(L + ['C'], A, B-1, C+1)
        return 'No'

ans = solve([], A, B, C)

if ans == 'No':
    print('No')
else:
    print('Yes')
    for i in ans:
        print(i)