Problem p02692 (python)

Fixed submission:
n,A,B,C = map(int,input().split())
flag = True
ans = []
s = []
for i in range(n):
    s.append(input())
s.append("")
for i in range(n):
    if s[i] == "AB":
        if A == 0 and B == 0:
            flag = False
        elif A == 1 and B == 1:
            if s[i+1] == "BC":
                A -= 1
                B += 1
                ans.append("B")
            else:
                B -= 1
                A += 1
                ans.append("A")
        elif A >= B:
            A -= 1
            B += 1
            ans.append("B")
        else:
            B -= 1
            A += 1
            ans.append("A")
    if s[i] == "AC":
        if A == 0 and C == 0:
            flag = False
        elif A == 1 and C == 1:
            if s[i+1] == "BC":
                A -= 1
                C += 1
                ans.append("C")
            else:
                C -= 1
                A += 1
                ans.append("A")
        elif A >= C:
            A -= 1
            C += 1
            ans.append("C")
        else:
            C -= 1
            A += 1
            ans.append("A")
    if s[i] == "BC":
        if B == 0 and C == 0:
            flag = False
        elif B == 1 and C == 1:
            if s[i+1] == "AC":
                B -= 1
                C += 1
                ans.append("C")
            else:
                C -= 1
                B += 1
                ans.append("B")
        elif B >= C:
            B -= 1
            C += 1
            ans.append("C")
        else:
            C -= 1
            B += 1
            ans.append("B")
if flag:
    print("Yes")
    for i in ans:
        print(i)
else:
    print("No")

Buggy submission (verdict: Runtime Error):
def transfer(a,b,B):
    a -= 1
    b += 1
    ans.append(B)
n,A,B,C = map(int,input().split())
flag = True
ans = []
s = []
for i in range(n):
    s.append(input())
for i in range(n):
    if s[i] == "AB":
        if A == 0 and B == 0:
            flag = False
        elif A == 1 and B == 1:
            if s[i+1] == "BC":
                transfer(A,B,"B")
            else:
                transfer(B,A,"A")
        elif A >= B:
            transfer(A,B,"B")
        else:
            transfer(B,A,"A")
    if s[i] == "AC":
        if A == 0 and C == 0:
            flag = False
        elif A == 1 and C == 1:
            if s[i+1] == "BC":
                transfer(A,C,"C")
            else:
                transfer(C,A,"A")
        elif A >= C:
            transfer(A,C,"C")
        else:
            transfer(C,A,"A")
    if s[i] == "BC":
        if B == 0 and C == 0:
            flag = False
        elif B == 1 and C == 1:
            if s[i+1] == "AC":
                transfer(B,C,"C")
            else:
                transfer(C,B,"B")
        elif B >= C:
            transfer(B,C,"C")
        else:
            transfer(C,B,"B")
if flag:
    print("Yes")
    for i in ans:
        print(i)
else:
    print("No")

Buggy submission (verdict: Runtime Error):
n,a,b,c = map(int,input().split())
flag = True
ans = []
s = []
for i in range(n):
    s.append(input())
for i in range(n):
    if s[i] == "AB":
        if a == 0 and b == 0:
            flag = False
        if a > b:
            a -= 1
            b += 1
            ans.append("B")
        if b < a:
            a += 1
            b -= 1
            ans.append("A")
        if a == b:
            if s[i+1] == "AC":
                a += 1
                b -= 1
                ans.append("A")
            else:
                a -= 1
                b += 1
                ans.append("B")
    if s[i] == "BC":
        if b == 0 and c == 0:
            flag = False
        if b > c:
            b -= 1
            c += 1
            ans.append("C")
        if b < c:
            b += 1
            c -= 1
            ans.append("B")
        if b == c:
            if s[i+1] == "AB":
                b += 1
                c -= 1
                ans.append("B")
            else:
                c += 1
                b -= 1
                ans.append("B")
    if s[i] == "AC":
        if a == 0 and c == 0:
            flag = False
        if a > c:
            a -= 1
            c += 1
            ans.append("C")
        if c > a:
            a += 1
            c -= 1
            ans.append("A")
        if c == a:
            if s[i+1] == "AB"
                a += 1
                c -= 1
                ans.append("A")
            else:
                c += 1
                a -= 1
                ans.append("C")
if flag:
    print("Yes")
    for i in ans:
        print(i)
else:
    print("No")