Problem p02692 (python)

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


def solve(x):
    d = {"A": 0, "B": 1, "C": 2}
    ans = []
    for i in range(N):
        u, v = X[i]
        if x[d[u]] == x[d[v]] == 0:
            return

        if x[d[u]] < x[d[v]]:
            x[d[u]] += 1
            x[d[v]] -= 1
            ans.append(u)
        elif x[d[u]] > x[d[v]]:
            x[d[u]] -= 1
            x[d[v]] += 1
            ans.append(v)
        elif i < N - 1 and X[i] != X[i + 1]:
            # x[d[u]] == x[d[v]] >= 1
            if u in X[i + 1]:
                x[d[u]] += 1
                x[d[v]] -= 1
                ans.append(u)
            else:
                x[d[u]] -= 1
                x[d[v]] += 1
                ans.append(v)
        else:
            x[d[u]] += 1
            x[d[v]] -= 1
            ans.append(u)

        if x[d[u]] < 0 or x[d[v]] < 0:
            return

    return ans

ans = solve([A, B, C])
if ans:
    print("Yes")
    print(*ans, sep="\n")
else:
    print("No")

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


def solve(x):
    d = {"A": 0, "B": 1, "C": 2}
    ans = []
    for i in range(N):
        u, v = X[i]
        if x[d[u]] == x[d[v]] == 0:
            return

        if x[d[u]] < x[d[v]]:
            x[d[u]] += 1
            x[d[v]] -= 1
            ans.append(u)
        elif x[d[u]] > x[d[v]]:
            x[d[u]] -= 1
            x[d[v]] += 1
            ans.append(v)
        else:
            # x[d[u]] == x[d[v]] >= 1
            if i < N - 1 and X[i] != X[i + 1]:
                x, y = X[i + 1]
                if u == x or u == y:
                    x[d[u]] += 1
                    x[d[v]] -= 1
                    ans.append(u)
                else:
                    x[d[u]] -= 1
                    x[d[v]] += 1
                    ans.append(v)
            else:
                x[d[u]] += 1
                x[d[v]] -= 1
                ans.append(u)

    return ans

ans = solve([A, B, C])
if ans:
    print("Yes")
    print(*ans, sep="\n")
else:
    print("No")

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


def solve(x):
    d = {"A": 0, "B": 1, "C": 2}
    ans = []
    for i in range(N):
        u, v = X[i]
        if x[d[u]] == x[d[v]] == 0:
            return

        if x[d[u]] < x[d[v]]:
            x[d[u]] += 1
            x[d[v]] -= 1
            ans.append(u)
        elif x[d[u]] > x[d[v]]:
            x[d[u]] -= 1
            x[d[v]] += 1
            ans.append(v)
        else:
            # x[d[u]] == x[d[v]] >= 1
            if i < N - 1 and X[i] != X[i + 1]:
                x, y = X[i + 1]
                if u == x:
                    x[d[u]] += 1
                    x[d[v]] -= 1
                    ans.append(u)
                else:
                    x[d[u]] -= 1
                    x[d[v]] += 1
                    ans.append(v)
            else:
                x[d[u]] += 1
                x[d[v]] -= 1
                ans.append(u)

    return ans

ans = solve([A, B, C])
if ans:
    print("Yes")
    print(*ans, sep="\n")
else:
    print("No")

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


def solve(x):
    d = {"A": 0, "B": 1, "C": 2}
    ans = []
    for i in range(N):
        u, v = X[i]
        if x[d[u]] == x[d[v]] == 0:
            return

        if x[d[u]] < x[d[v]]:
            x[d[u]] += 1
            x[d[v]] -= 1
            ans.append(u)
        elif x[d[u]] > x[d[v]]:
            x[d[u]] -= 1
            x[d[v]] += 1
            ans.append(v)
        else:
            # x[d[u]] == x[d[v]] >= 1
            if i < N - 1 and X[i] != X[i + 1]:
                x, y = X[i + 1]
                if u == x:
                    x[d[u]] += 1
                    x[d[v]] += 1
                    ans.append(u)
                else:
                    x[d[u]] -= 1
                    x[d[v]] += 1
                    ans.append(v)
            else:
                x[d[u]] += 1
                x[d[v]] += 1
                ans.append(u)

    return ans

ans = solve([A, B, C])
if ans:
    print("Yes")
    print(*ans, sep="\n")
else:
    print("No")