Problem p02692 (python)

Fixed submission:
n,a,b,c = map(int,input().split())
s = [str(input()) for i in range(n)]
lis = [a,b,c]
ans = ""
for i in range(n-1):
    if lis[ord(s[i][0]) - ord("A")] == 0 and lis[ord(s[i][1]) - ord("A")] == 0:
        print("No")
        exit()
    if s[i] == s[i+1]:
        if lis[ord(s[i][0])-ord("A")] == 0:
            ans += s[i][0]
            lis[ord(s[i][0])-ord("A")] += 1
            lis[ord(s[i][1])-ord("A")] -= 1
        else:
            ans += s[i][1]
            lis[ord(s[i][0])-ord("A")] -= 1
            lis[ord(s[i][1])-ord("A")] += 1
    else:
        if s[i] == "AB":
            if s[i+1] == "AC":
                if lis[1] == 0:
                    ans += "B"
                    lis[1] += 1
                    lis[0] -= 1
                else:
                    ans += "A"
                    lis[0] += 1
                    lis[1] -= 1
            else:
                if lis[0] == 0:
                    ans += "A"
                    lis[0] += 1
                    lis[1] -= 1
                else:
                    ans += "B"
                    lis[0] -= 1
                    lis[1] += 1
        elif s[i] == "AC":
            if s[i+1] == "AB":
                if lis[2] == 0:
                    ans += "C"
                    lis[2] += 1
                    lis[0] -= 1
                else:
                    ans += "A"
                    lis[0] += 1
                    lis[2] -= 1
            else:
                if lis[0] == 0:
                    ans += "A"
                    lis[0] += 1
                    lis[2] -= 1
                else:
                    ans += "C"
                    lis[0] -= 1
                    lis[2] += 1
        else:
            if s[i+1] == "AB":
                if lis[2] == 0:
                    ans += "C"
                    lis[2] += 1
                    lis[1] -= 1
                else:
                    ans += "B"
                    lis[2] -= 1
                    lis[1] += 1
            else:
                if lis[1] == 0:
                    ans += "B"
                    lis[1] += 1
                    lis[2] -= 1
                else:
                    ans += "C"
                    lis[2] += 1
                    lis[1] -= 1
if lis[ord(s[-1][0])-ord("A")] == 0:
    if lis[ord(s[-1][1])-ord("A")] == 0:
        print("No")
        exit()
    else:
        ans += s[-1][0]
else:
    ans += s[-1][1]
print("Yes")
for i in range(n):
    print(ans[i])

Buggy submission (verdict: Runtime Error):
n,a,b,c = map(int,input().split())
s = [str(input()) for i in range(n)]
lis = [a,b,c]
ans = ""
for i in range(n-1):
    if lis[ord(s[i][0]) - ord("A")] * lis[ord(s[i][1]) - ord("A")] == 0:
        print("No")
        exit()
    if s[i] == s[i+1]:
        if lis[ord(s[i][0])] == 0:
            ans += s[i][0]
            lis[ord(s[i][0])] += 1
            lis[ord(s[i][1])] -= 1
        else:
            ans += s[i][1]
            lis[ord(s[i][0])-ord("A")] -= 1
            lis[ord(s[i][1])-ord("A")] += 1
    else:
        if s[i] == "AB":
            if s[i+1] == "AC":
                if lis[1] == 0:
                    ans += "B"
                    lis[1] += 1
                    lis[0] -= 1
                else:
                    ans += "A"
                    lis[0] += 1
                    lis[1] -= 1
            else:
                if lis[0] == 0:
                    ans += "A"
                    lis[0] += 1
                    lis[1] -= 1
                else:
                    ans += "B"
                    lis[0] -= 1
                    lis[1] += 1
        elif s[i] == "AC":
            if s[i+1] == "AB":
                if lis[2] == 0:
                    ans += "C"
                    lis[2] += 1
                    lis[0] -= 1
                else:
                    ans += "A"
                    lis[0] += 1
                    lis[2] -= 1
            else:
                if lis[0] == 0:
                    ans += "A"
                    lis[0] += 1
                    lis[2] -= 1
                else:
                    ans += "C"
                    lis[0] -= 1
                    lis[2] += 1
        else:
            if s[i+1] == "AB":
                if lis[2] == 0:
                    ans += "C"
                    lis[2] += 1
                    lis[1] -= 1
                else:
                    ans += "B"
                    lis[2] -= 1
                    lis[1] += 1
            else:
                if lis[1] == 0:
                    ans += "B"
                    lis[1] += 1
                    lis[2] -= 1
                else:
                    ans += "C"
                    lis[2] += 1
                    lis[1] -= 1
if lis[ord(s[-1][0])-ord("A")] == 0:
    if lis[ord(s[-1][1])-ord("A")] == 0:
        print("No")
        exit()
    else:
        ans += s[-1][0]
else:
    ans += s[-1][1]
print("Yes")
for i in range(n):
    print(ans[i])

Buggy submission (verdict: Runtime Error):
import random
def answer(x):
    print("Yes")
    AB = ["B"] * x[0][0] + ["A"] * x[0][1]
    AC = ["C"] * x[1][0] + ["A"] * x[1][1]
    BC = ["C"] * x[2][0] + ["B"] * x[2][1]
    AB = random.shuffle(AB)
    AC = random.shuffle(AC)
    BC = random.shuffle(BC)
    for i in range(n):
        if s[i] == "AB":
            print(AB.pop(0))
        elif s[i] == "AC":
            print(AC.pop(0))
        else:
            print(BC.pop(0))
    exit()

n,a,b,c = map(int,input().split())
s = [str(input()) for i in range(n)]
cnt = [0 for i in range(3)]
for ss in s:
    if ss == "AB":
        a += 1
        b += 1
        cnt[0] += 1
    elif ss == "AC":
        a += 1
        c += 1
        cnt[1] += 1
    else:
        b += 1
        c += 1
        cnt[2] += 1
a,b,c = a//2,b//2,c//2
all = min(min(cnt),min(a,b,c))
a,b,c = a-all,b-all,c-all
for i in range(3):
    cnt[i] -= all
  # cnt=["AB","AC","BC"]

#"AB"=0
if cnt[0] == 0:
        ACdeA = min(cnt[1],a)
        BCdeB = min(cnt[2],b)
        ACdeC = cnt[1]-ACdeA
        BCdeC = cnt[2]-BCdeB
        if ACdeC + BCdeC <= c:
            answer([[all,0],[ACdeA,all+ACdeC],[all+BCdeB,BCdeC]])
if cnt[1] == 0:
        ABdeA = min(cnt[0],a)
        BCdeC = min(cnt[2],c)
        ABdeB = cnt[0]-ABdeA
        BCdeB = cnt[2]-BCdeC
        if ABdeB + BCdeB <= b:
            answer([[all+ABdeA,ABdeB],[0,all],[all+BCdeB,BCdeC]])
if cnt[2] == 0:
        ABdeB = min(cnt[0],b)
        ACdeC = min(cnt[1],c)
        ABdeA = cnt[0]-ABdeB
        ACdeA = cnt[1]-ACdeC
        if ABdeA + ACdeA <= a:
            answer([[all+ABdeA,ABdeB],[ACdeA,ACdeC+all],[all,0]])
if a == 0:
    ABdeB = cnt[0]
    ACdeC = cnt[1]
    b -= ABdeB
    c -= ACdeC
    if b >= 0 and c >= 0:
        BCdeB = min(b,cnt[2])
        BCdeC = cnt[2]-BCdeB
        if BCdeC <= c:
             answer([[all,ABdeB],[0,ACdeC+all],[all+BCdeB,BCdeC]])
    b += ABdeB
    c += ACdeC
if b == 0:
    ABdeA = cnt[0]
    BCdeC = cnt[2]
    a -= ABdeA
    c -= BCdeC
    if a >= 0 and c >= 0:
        ACdeA = min(a,cnt[1])
        ACdeC = cnt[1]-ACdeA
        if ACdeC <= c:
            answer([[all+ABdeA,0],[ACdeA,ACdeC+all],[all,BCdeC]])
    a += ABdeA
    c += BCdeC
if c == 0:
    ACdeA = cnt[1]
    BCdeB = cnt[2]
    a -= ACdeA
    b -= BCdeB
    if a >= 0 and b >= 0:
        ABdeA = min(a,cnt[0])
        ABdeB = cnt[0]-ABdeA
        if ABdeB <= b:
            answer([[all+ABdeA,ABdeB],[ACdeA,all],[BCdeB+all,0]])
print("No")