Problem p02692 (python)

Fixed submission:
n, a, b, c = map(int, input().split())
s = [input() for _ in range(n)]
if s[0] == 'AB'and a == 0 and b == 0:
    print('No')
elif s[0] == 'BC' and b == 0 and c == 0:
    print('No')
elif s[0] == 'AC' and a == 0 and c == 0:
    print('No')
elif a + b + c >= 2:
    print('Yes')
    ans = []
    for i in range(n):
        if i < n - 1:
            if a == b == 1 and c == 0 and s[i] == 'AB':
                if s[i + 1] == 'AB' or s[i + 1] == 'AC':
                    ans.append('A')
                    a += 1
                    b -= 1
                else:
                    ans.append('B')
                    a -= 1
                    b += 1
                continue
            elif b == c == 1 and a == 0 and s[i] == 'BC':
                if s[i + 1] == 'AB' or s[i + 1] == 'BC':
                    ans.append('B')
                    c -= 1
                    b += 1
                else:
                    ans.append('C')
                    c += 1
                    b -= 1
                continue
            elif a == c == 1 and b == 0 and s[i] == 'AC':
                if s[i + 1] == 'AB' or s[i + 1] == 'AC':
                    ans.append('A')
                    a += 1
                    c -= 1
                else:
                    ans.append('C')
                    a -= 1
                    c += 1
                continue
        if s[i] == 'AB':
            if a <= b:
                ans.append('A')
                a += 1
                b -= 1
            else:
                ans.append('B')
                a -= 1
                b += 1
        elif s[i] == 'BC':
            if b <= c:
                ans.append('B')
                b += 1
                c -= 1
            else:
                ans.append('C')
                b -= 1
                c += 1
        else:
            if a <= c:
                ans.append('A')
                a += 1
                c -= 1
            else:
                ans.append('C')
                a -= 1
                c += 1
    for c in ans:
        print(c)
elif a == b == c == 0:
    print('No')
else:
    ans = []
    if a == 1:
        t = 0
    elif b == 1:
        t = 1
    else:
        t = 2
    for i in range(n):
        if s[i] == 'AB' and t == 2 or s[i] == 'BC' and t == 0 or s[i] == 'AC' and t == 1:
            print('No')
            exit(0)
        elif s[i] == 'AB':
            if t == 0:
                ans.append('B')
                t = 1
            else:
                ans.append('A')
                t = 0
        elif s[i] == 'BC':
            if t == 1:
                ans.append('C')
                t = 2
            else:
                ans.append('B')
                t = 1
        else:
            if t == 0:
                ans.append('C')
                t = 2
            else:
                ans.append('A')
                t = 0
    print('Yes')
    for c in ans:
        print(c)

Buggy submission (verdict: Runtime Error):
n, a, b, c = map(int, input().split())
s = [input() for _ in range(n)]
if s[0] == 'AB'and a == 0 and b == 0:
    print('No')
elif s[0] == 'BC' and b == 0 and c == 0:
    print('No')
elif s[0] == 'AC' and a == 0 and c == 0:
    print('No')
elif a + b + c >= 2:
    exit(1)
    print('Yes')
    ans = []
    for i in range(n):
        if i < n - 1:
            if a == b == 1 and c == 0 and s[i] == 'AB':
                if s[i + 1] == 'AB' or s[i + 1] == 'AC':
                    ans.append('A')
                    a += 1
                    b -= 1
                else:
                    ans.append('B')
                    a -= 1
                    b += 1
                continue
            elif b == c == 1 and a == 0 and s[i] == 'BC':
                if s[i + 1] == 'AB' or s[i + 1] == 'BC':
                    ans.append('B')
                    c -= 1
                    b += 1
                else:
                    ans.append('C')
                    c += 1
                    b -= 1
                continue
            elif a == c == 1 and b == 0 and s[i] == 'AC':
                if s[i + 1] == 'AB' or s[i + 1] == 'AC':
                    ans.append('A')
                    a += 1
                    c -= 1
                else:
                    ans.append('C')
                    a -= 1
                    c += 1
                continue
        if s[i] == 'AB':
            if a <= b:
                ans.append('A')
                a += 1
                b -= 1
            else:
                ans.append('B')
                a -= 1
                b += 1
        elif s[i] == 'BC':
            if b <= c:
                ans.append('B')
                b += 1
                c -= 1
            else:
                ans.append('C')
                b -= 1
                c += 1
        else:
            if a <= c:
                ans.append('A')
                a += 1
                c -= 1
            else:
                ans.append('C')
                a -= 1
                c += 1
    for c in ans:
        print(c)
else:
    ans = []
    if a == 1:
        t = 0
    elif b == 1:
        t = 1
    else:
        t = 2
    for i in range(n):
        if s[i] == 'AB' and t == 2 or s[i] == 'BC' and t == 0 or s[i] == 'AC' and t == 1:
            print('No')
            exit(0)
        elif s[i] == 'AB':
            if t == 0:
                ans.append('B')
            else:
                ans.append('A')
        elif s[i] == 'BC':
            if t == 1:
                ans.append('C')
            else:
                ans.append('B')
        else:
            if t == 0:
                ans.append('C')
            else:
                ans.append('A')
    print('Yes')
    for c in ans:
        print(c)

Buggy submission (verdict: Runtime Error):
n, a, b, c = map(int, input().split())
s = [input() for _ in range(n)]
if s[0] == 'AB'and a == 0 and b == 0:
    print('No')
elif s[0] == 'BC' and b == 0 and c == 0:
    print('No')
elif s[0] == 'AC' and a == 0 and c == 0:
    print('No')
elif a + b + c >= 2:
    exit(1)
    print('Yes')
    ans = []
    for i in range(n):
        if i < n - 1:
            if a == b == 1 and c == 0 and s[i] == 'AB':
                if s[i + 1] == 'AB' or s[i + 1] == 'AC':
                    ans.append('A')
                    a += 1
                    b -= 1
                else:
                    ans.append('B')
                    a -= 1
                    b += 1
                continue
            elif b == c == 1 and a == 0 and s[i] == 'BC':
                if s[i + 1] == 'AB' or s[i + 1] == 'BC':
                    ans.append('B')
                    c -= 1
                    b += 1
                else:
                    ans.append('C')
                    c += 1
                    b -= 1
                continue
            elif a == c == 1 and b == 0 and s[i] == 'AC':
                if s[i + 1] == 'AB' or s[i + 1] == 'AC':
                    ans.append('A')
                    a += 1
                    c -= 1
                else:
                    ans.append('C')
                    a -= 1
                    c += 1
                continue
        if s[i] == 'AB':
            if a <= b:
                ans.append('A')
                a += 1
                b -= 1
            else:
                ans.append('B')
                a -= 1
                b += 1
        elif s[i] == 'BC':
            if b <= c:
                ans.append('B')
                b += 1
                c -= 1
            else:
                ans.append('C')
                b -= 1
                c += 1
        else:
            if a <= c:
                ans.append('A')
                a += 1
                c -= 1
            else:
                ans.append('C')
                a -= 1
                c += 1
    for c in ans:
        print(c)
else:
    exit(1)
    ans = []
    if a == 1:
        t = 0
    elif b == 1:
        t = 1
    else:
        t = 2
    for i in range(n):
        if s[i] == 'AB' and t == 2 or s[i] == 'BC' and t == 0 or s[i] == 'AC' and t == 1:
            print('No')
            exit(0)
        elif s[i] == 'AB':
            if t == 0:
                ans.append('B')
            else:
                ans.append('A')
        elif s[i] == 'BC':
            if t == 1:
                ans.append('C')
            else:
                ans.append('B')
        else:
            if t == 0:
                ans.append('C')
            else:
                ans.append('A')
    print('Yes')
    for c in ans:
        print(c)