Buggy python submission (verdict: Time Limit Exceeded):
import dataclasses
n, a, b, c = list(map(int, input().split(" ")))


@dataclasses.dataclass
class Data:
    history: str
    a: int
    b: int
    c: int

    def get_tuple(self):
        return ("", self.a, self.b, self.c)

    def check(self):
        return self.a >= 0 and self.b >= 0 and self.c >= 0

now = [Data("", a, b, c)]
for i in range(n):
    string = input()
    new = []
    tpls = set()
    if string == "AB":
        for j in now:
            cand = Data(j.history + "A", j.a + 1, j.b - 1, j.c)
            tp = cand.get_tuple()
            if cand.check() and tp not in tpls:
                new.append(cand)
                tpls.add(tp)

            cand = Data(j.history + "B", j.a - 1, j.b + 1, j.c)
            tp = cand.get_tuple()
            if cand.check() and tp not in tpls:
                new.append(cand)
                tpls.add(tp)

    elif string == "AC":
        for j in now:
            cand = Data(j.history + "A", j.a + 1, j.b, j.c - 1)
            tp = cand.get_tuple()
            if cand.check() and tp not in tpls:
                new.append(cand)
                tpls.add(tp)

            cand = Data(j.history + "C", j.a - 1, j.b, j.c + 1)
            tp = cand.get_tuple()
            if cand.check() and tp not in tpls:
                new.append(cand)
                tpls.add(tp)

    elif string == "BC":
        for j in now:
            cand = Data(j.history + "B", j.a, j.b + 1, j.c - 1)
            tp = cand.get_tuple()
            if cand.check() and tp not in tpls:
                new.append(cand)
                tpls.add(tp)

            cand = Data(j.history + "C", j.a, j.b - 1, j.c + 1)
            tp = cand.get_tuple()
            if cand.check() and tp not in tpls:
                new.append(cand)
                tpls.add(tp)
    if not new:
        print("No")
        break
    now = new
else:
    print("Yes")
    for i in now[0].history:
        print(i)


Fixed submission:
import dataclasses
n, a, b, c = list(map(int, input().split(" ")))
m = {"A": a, "B": b, "C": c}
h = ""
if sum(m.values()) == 0:
    print("No")
elif sum(m.values()) == 1:
    for i in range(n):
        x, y = input()
        if m[x] == 0 and m[y] == 0:
            print("No")
            break
        elif m[x] == 0:
            m[x] += 1
            m[y] -= 1
            h += x
        else:
            m[x] -= 1
            m[y] += 1
            h += y
    else:
        print("Yes")
        [print(i) for i in h]
else:
    inp = input()
    x, y = inp
    if m[x] == 0 and m[y] == 0:
        print("No")
    else:
        for i in range(n-1):
            next_inp = input()
            x, y = inp
            if m[x] == 0:
                m[x] += 1
                m[y] -= 1
                h += x
            elif m[y] == 0:
                m[x] -= 1
                m[y] += 1
                h += y
            elif m[x] == 1 and m[y] == 1:
                if x in next_inp:
                    m[x] += 1
                    m[y] -= 1
                    h += x
                else:
                    m[x] -= 1
                    m[y] += 1
                    h += y
            else:
                if m[x] < m[y]:
                    m[x] += 1
                    m[y] -= 1
                    h += x
                else:
                    m[x] -= 1
                    m[y] += 1
                    h += y
            inp = next_inp
        x, y = inp
        h += x if m[x] == 0 else y
        print("Yes")
        [print(i) for i in h]