Problem p02692 (python)

Fixed submission:
n,a,b,c = map(int,input().split())

sl = []
for _ in range(n):
    s = input()
    sl.append(s)

yusen = []
before = sl[0]
co = 0
for s in sl:
    if before == s:
        co += 1
    else:
        target = list(set(list(s)) & set(list(before)))[0]
        for i in range(co):
            yusen.append(target)
        before = s
        co = 1
else:
    for i in range(co):
        yusen.append("C")




ansl = []
for i in range(n):
    s = sl[i]
    if s == "AB":
        if a > b:
            ansl.append("B")
            a -= 1
            b += 1
        elif a < b:
            ansl.append("A")
            a += 1
            b -= 1
        else:
            if n-1 == i:
                ansl.append("A")
                a += 1
                b -= 1
            elif sl[i+1] == "AC":
                ansl.append("A")
                a += 1
                b -= 1
            else:
                ansl.append("B")
                a -= 1
                b += 1
    if s == "BC":
        if b > c:
            ansl.append("C")
            b -= 1
            c += 1
        elif b < c:
            ansl.append("B")
            b += 1
            c -= 1
        else:
            if n-1 == i:
                ansl.append("B")
                b += 1
                c -= 1
            elif  sl[i+1] == "AB":
                ansl.append("B")
                b += 1
                c -= 1
            else:
                ansl.append("C")
                b -= 1
                c += 1
    if s == "AC":
        if c > a:
            ansl.append("A")
            c -= 1
            a += 1
        elif c < a:
            ansl.append("C")
            c += 1
            a -= 1
        else:
            if n-1 == i:
                ansl.append("C")
                c += 1
                a -= 1
            elif sl[i+1] == "BC":
                ansl.append("C")
                c += 1
                a -= 1
            else:
                ansl.append("A")
                c -= 1
                a += 1
    if a < 0 or b < 0 or c < 0:
        print("No")
        exit()

print("Yes")
for a in ansl:
    print(a)

Buggy submission (verdict: Runtime Error):
n,a,b,c = map(int,input().split())

sl = []
for _ in range(n):
    s = input()
    sl.append(s)

yusen = []
before = sl[0]
co = 0
for s in sl:
    if before == s:
        co += 1
    else:
        target = list(set(list(s)) & set(list(before)))[0]
        for i in range(co):
            yusen.append(target)
        before = s
        co = 1
else:
    for i in range(co):
        yusen.append("C")




ansl = []
for i in range(n):
    s = sl[i]
    if s == "AB":
        if a > b:
            ansl.append("B")
            a -= 1
            b += 1
        elif a < b:
            ansl.append("A")
            a += 1
            b -= 1
        else:
            if n == i - 1:
                ansl.append("A")
                a += 1
                b -= 1
            elif sl[i+1] == "AC":
                ansl.append("A")
                a += 1
                b -= 1
            else:
                ansl.append("B")
                a -= 1
                b += 1
    if s == "BC":
        if b > c:
            ansl.append("C")
            b -= 1
            c += 1
        elif b < c:
            ansl.append("B")
            b += 1
            c -= 1
        else:
            if n == i - 1:
                ansl.append("B")
                b += 1
                c -= 1
            elif  sl[i+1] == "AB":
                ansl.append("B")
                b += 1
                c -= 1
            else:
                ansl.append("C")
                b -= 1
                c += 1
    if s == "AC":
        if c > a:
            ansl.append("A")
            c -= 1
            a += 1
        elif c < a:
            ansl.append("C")
            c += 1
            a -= 1
        else:
            if n == i - 1:
                ansl.append("C")
                c += 1
                a -= 1
            elif sl[i+1] == "BC":
                ansl.append("C")
                c += 1
                a -= 1
            else:
                ansl.append("A")
                c -= 1
                a += 1
    if a < 0 or b < 0 or c < 0:
        print("No")
        exit()

print("Yes")
for a in ansl:
    print(a)

Buggy submission (verdict: Runtime Error):
n,a,b,c = map(int,input().split())

sl = []
for _ in range(n):
    s = input()
    sl.append(s)

yusen = []
before = sl[0]
co = 0
for s in sl:
    if before == s:
        co += 1
    else:
        target = list(set(list(s)) & set(list(before)))[0]
        for i in range(co):
            yusen.append(target)
        before = s
        co = 1
else:
    for i in range(co):
        yusen.append("C")




ansl = []
for i in range(n):
    s = sl[i]
    if s == "AB":
        if a > b:
            ansl.append("B")
            a -= 1
            b += 1
        elif a < b:
            ansl.append("A")
            a += 1
            b -= 1
        else:
            if i - 1 != n and sl[i+1] == "AC":
                ansl.append("A")
                a += 1
                b -= 1
            else:
                ansl.append("B")
                a -= 1
                b += 1
    if s == "BC":
        if b > c:
            ansl.append("C")
            b -= 1
            c += 1
        elif b < c:
            ansl.append("B")
            b += 1
            c -= 1
        else:
            if i - 1 != n and sl[i+1] == "AB":
                ansl.append("B")
                b += 1
                c -= 1
            else:
                ansl.append("C")
                b -= 1
                c += 1
    if s == "AC":
        if c > a:
            ansl.append("A")
            c -= 1
            a += 1
        elif c < a:
            ansl.append("C")
            c += 1
            a -= 1
        else:
            if i - 1 != n and sl[i+1] == "BC":
                ansl.append("C")
                c += 1
                a -= 1
            else:
                ansl.append("A")
                c -= 1
                a += 1
    if a < 0 or b < 0 or c < 0:
        print("No")
        exit()

print("Yes")
for a in ansl:
    print(a)

Buggy submission (verdict: Runtime Error):
n,a,b,c = map(int,input().split())

sl = []
for _ in range(n):
    s = input()
    sl.append(s)

yusen = []
before = sl[0]
co = 0
for s in sl:
    if before == s:
        co += 1
    else:
        target = list(set(list(s)) & set(list(before)))[0]
        for i in range(co):
            yusen.append(target)
        before = s
        co = 1
else:
    for i in range(co):
        yusen.append("C")




ansl = []
for i in range(n):
    s = sl[i]
    if s == "AB":
        if a > b:
            ansl.append("B")
            a -= 1
            b += 1
        elif a < b:
            ansl.append("A")
            a += 1
            b -= 1
        else:
            if sl[i+1] == "AC":
                ansl.append("A")
                a += 1
                b -= 1
            else:
                ansl.append("B")
                a -= 1
                b += 1
    if s == "BC":
        if b > c:
            ansl.append("C")
            b -= 1
            c += 1
        elif b < c:
            ansl.append("B")
            b += 1
            c -= 1
        else:
            if sl[i+1] == "AB":
                ansl.append("B")
                b += 1
                c -= 1
            else:
                ansl.append("C")
                b -= 1
                c += 1
    if s == "AC":
        if c > a:
            ansl.append("A")
            c -= 1
            a += 1
        elif c < a:
            ansl.append("C")
            c += 1
            a -= 1
        else:
            if sl[i+1] == "BC":
                ansl.append("C")
                c += 1
                a -= 1
            else:
                ansl.append("A")
                c -= 1
                a += 1
    if a < 0 or b < 0 or c < 0:
        print("No")
        exit()

print("Yes")
for a in ansl:
    print(a)