Problem p02692 (python)

Fixed submission:

N, a, b, c, *S = open(0).read().split()
N, a, b, c = map(int, (N, a, b, c))

flg = a*b*c == 0 and a+b+c == 2

ans = []
for i, s in enumerate(S):
    if s == 'AB':
        if flg and a*b == 1 and i < N-1:
            s_ = S[i+1]
            if s_ == 'AC':
                ans.append('A')
                a += 1
                b -= 1
                continue
            elif s_ == 'BC':
                ans.append('B')
                a -= 1
                b += 1
                continue

        ans.append('B' if a > b else 'A')
        if a > b:
            a -= 1
            b += 1
        else:
            a += 1
            b -= 1

    elif s == 'AC':
        if flg and a*c == 1 and i < N-1:
            s_ = S[i+1]
            if s_ == 'AB':
                ans.append('A')
                a += 1
                c -= 1
                continue
            elif s_ == 'BC':
                ans.append('C')
                a -= 1
                c += 1
                continue

        ans.append('C' if a > c else 'A')
        if a > c:
            a -= 1
            c += 1
        else:
            a += 1
            c -= 1

    elif s == 'BC':
        if flg and b*c == 1 and i < N-1:
            s_ = S[i+1]
            if s_ == 'AB':
                ans.append('B')
                b += 1
                c -= 1
                continue
            elif s_ == 'AC':
                ans.append('C')
                b -= 1
                c += 1
                continue

        ans.append('C' if b > c else 'B')
        if b > c:
            b -= 1
            c += 1
        else:
            b += 1
            c -= 1

    if a < 0 or b < 0 or c < 0:
        print('No')
        exit()


print('Yes')
[print(r) for r in ans]

Buggy submission (verdict: Runtime Error):

N, a, b, c, *S = open(0).read().split()
N, a, b, c = map(int, (N, a, b, c))

flg = a*b*c == 0 and a+b+c=2

ans = []
for i, s in enumerate(S):
    if s == 'AB':
        if flg and a*b==1 and i < N-1:
            s_ = S[i+1]
            if s_ == 'AC':
                ans.append('A')
                a += 1
                b -= 1
                continue
            elif s_ == 'BC':
                ans.append('B')
                a -= 1
                b += 1
                continue

        ans.append('B' if a > b else 'A')
        if a > b:
            a -= 1
            b += 1
        else:
            a += 1
            b -= 1

    elif s == 'AC':
        if flg and a*c == 1 and i < N-1:
            s_ = S[i+1]
            if s_ == 'AB':
                ans.append('A')
                a += 1
                c -= 1
                continue
            elif s_ == 'BC':
                ans.append('C')
                a -= 1
                c += 1
                continue

        ans.append('C' if a > c else 'A')
        if a > c:
            a -= 1
            c += 1
        else:
            a += 1
            c -= 1

    elif s == 'BC':
        if flg and b*c == 1 and i < N-1:
            s_ = S[i+1]
            if s_ == 'AB':
                ans.append('B')
                b += 1
                c -= 1
                continue
            elif s_ == 'AC':
                ans.append('C')
                b -= 1
                c += 1
                continue

        ans.append('C' if b > c else 'B')
        if b > c:
            b -= 1
            c += 1
        else:
            b += 1
            c -= 1

    if a < 0 or b < 0 or c < 0:
        print('No')
        exit()


print('Yes')
[print(r) for r in ans]