Problem p02692 (python)

Fixed submission:
from bisect import bisect_left, bisect_right

INF = float("inf")
n, *s = map(int, input().split())
toidx = {"AB": (0, 1), "BC": (1, 2), "AC": (0, 2)}
toalp = lambda x: chr(ord("A") + x)
m = sum(s)
q = [toidx[input()] for _ in range(n)]

def solve():
    res = []
    if m == 0 or (not s[q[0][0]] and not s[q[0][1]]):
        print("No")
        return
    elif m == 1:
        for i, j in q:
            if not s[i] and not s[j]:
                print("No")
                return
            elif s[i]:
                s[j] += 1; s[i] -= 1
                res.append(toalp(j))
            elif s[j]:
                s[i] += 1; s[j] -= 1
                res.append(toalp(i))
    elif m == 2:
        d = {(0, 1): [], (0, 2): [], (1, 2): []}
        for i in range(n): d[q[i]].append(i)
        for key in d.keys(): d[key].append(INF)
        for idx, (i, j) in enumerate(q):
            if s[i] and s[j]:
                k = [x for x in range(3) if i != x and j != x][0]
                key1 = tuple(sorted([i, k]))
                key2 = tuple(sorted([j, k]))
                nik = d[key1][bisect_left(d[key1], idx)]
                njk = d[key2][bisect_left(d[key2], idx)]
                if nik <= njk:
                    s[i] += 1; s[j] -= 1
                    res.append(toalp(i))
                else: 
                    s[j] += 1; s[i] -= 1
                    res.append(toalp(j))
            elif s[i]:
                s[j] += 1; s[i] -= 1
                res.append(toalp(j))
            elif s[j]:
                s[i] += 1; s[j] -= 1
                res.append(toalp(i))
    else:
        for i, j in q:
            if s[i] >= s[j]:
                s[j] += 1; s[i] -= 1
                res.append(toalp(j))
            else:
                s[i] += 1; s[j] -= 1
                res.append(toalp(i))
    print("Yes")
    print(*res, sep="\n")

solve()

Buggy submission (verdict: Runtime Error):
from bisect import bisect_left, bisect_right

INF = float("inf")
n, *s = map(int, input().split())
toidx = {"AB": (0, 1), "BC": (1, 2), "AC": (0, 2)}
toalp = lambda x: chr(ord("A") + x)
m = sum(s)
q = [toidx[input()] for _ in range(n)]

def solve():
    res = []
    if m == 0 or (not s[q[0][0]] and not s[q[0][1]]):
        print("No")
        return
    elif m == 1:
        for i, j in q:
            if not s[i] and not s[j]:
                print("No")
                return
            elif s[i]:
                s[j] += 1; s[i] -= 1
                res.append(toalp(j))
            elif s[j]:
                s[i] += 1; s[j] -= 1
                res.append(toalp(i))
    elif m == 2:
        d = {(0, 1): [], (0, 2): [], (1, 2): []}
        for i in range(n): d[q[i]].append(i)
        for key in d.keys(): d[key].append(INF)
        for idx, (i, j) in enumerate(q):
            if s[i] and s[j]:
                k = [x for x in range(3) if i != x and j != x][0]
                nik = d[(i, k)][bisect_left(d[(i, k)], idx)]
                njk = d[(j, k)][bisect_left(d[(j, k)], idx)]
                if nik <= njk:
                    s[i] += 1; s[j] -= 1
                    res.append(toalp(i))
                else: 
                    s[j] += 1; s[i] -= 1
                    res.append(toalp(j))
            elif s[i]:
                s[j] += 1; s[i] -= 1
                res.append(toalp(j))
            elif s[j]:
                s[i] += 1; s[j] -= 1
                res.append(toalp(i))
    else:
        for i, j in q:
            if s[i] >= s[j]:
                s[j] += 1; s[i] -= 1
                res.append(toalp(j))
            else:
                s[i] += 1; s[j] -= 1
                res.append(toalp(i))
    print("Yes")
    print(*res, sep="\n")

solve()