Problem p02692 (python)

Fixed submission:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    import math

    #inf = 10**17
    #mod = 10**9 + 7

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

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    import math
    #from math import gcd

    #inf = 10**17
    #mod = 10**9 + 7

    n,a,b,c = map(int, input().split())
    l = []
    for i in range(n):
        s = input().rstrip()
        l.append(s)
    if a==0 and b==0 and c==0:
        print('No')
        exit()
    if a==0 and b==0:
        if l[0] == 'AB':
            print('No')
            exit()
    if c==0 and b==0:
        if l[0] == 'BC':
            print('No')
            exit()
    if a==0 and c==0:
        if l[0] == 'AC':
            print('No')
            exit()
    print('Yes')
    res = []
    boll = 0
    for i in range(n):
        if i == 0:
            if s[i] == 'AB':
                if a==0:
                    b -= 1
                    a += 1
                    boll = 0
                    res.append('A')
                else:
                    a -= 1
                    b += 1
                    boll = 1
                    res.append('B')
            if s[i] == 'BC':
                if c==0:
                    b -= 1
                    c += 1
                    boll = 2
                    res.append('C')
                else:
                    c -= 1
                    b += 1
                    boll = 1
                    res.append('B')
            if s[i] == 'AC':
                if a==0:
                    c -= 1
                    a += 1
                    boll = 0
                    res.append('A')
                else:
                    a -= 1
                    c += 1
                    boll = 2
                    res.append('C')
        else:
            if s[i] == 'AB':
                if boll==1:
                    b -= 1
                    a += 1
                    boll = 0
                    res.append('A')
                else:
                    a -= 1
                    b += 1
                    boll = 1
                    res.append('B')
            if s[i] == 'BC':
                if boll==1:
                    b -= 1
                    c += 1
                    boll = 2
                    res.append('C')
                else:
                    c -= 1
                    b += 1
                    boll = 1
                    res.append('B')
            if s[i] == 'AC':
                if boll==2:
                    c -= 1
                    a += 1
                    boll = 0
                    res.append('A')
                else:
                    a -= 1
                    c += 1
                    boll = 2
                    res.append('C')
    for i in res:
        print(i)

if __name__ == '__main__':
    main()