Problem p02692 (python)

Fixed submission:
n,a,b,c=map(int,input().split())
s=[]
for i in range(n):
    s.append(input())
s.append('EOF')

ans=[]
 
flag=True
 
for i in range(n):
    if s[i]=='AB':
        if a==0 and b==0:
            flag=False
            break
        if a==0 or (b!=0 and a==1 and s[i+1]=='AC'):
            ans.append('A')
            a+=1
            b-=1
        else:
            ans.append('B')
            a-=1
            b+=1
    elif s[i]=='AC':
        if a==0 and c==0:
            flag=False
            break
        if a==0 or (c!=0 and a==1 and s[i+1]=='AB'):
            ans.append('A')
            a+=1
            c-=1
        else:
            ans.append('C')
            a-=1
            c+=1
    else:
        if c==0 and b==0:
            flag=False
            break
        if c==0 or (b!=0 and c==1 and s[i+1]=='AC'):
            ans.append('C')
            c+=1
            b-=1
        else:
            ans.append('B')
            c-=1
            b+=1

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())
s=[]
for i in range(n):
    s.append(input())

ans=[]

flag=True

for i in range(n):
    if s[i]=='AB':
        if a==0 and b==0:
            flag=False
            break
        if a==1 and s[i+1]=='AC' or a==0:
            ans.append('A')
            a+=1
            b-=1
        else:
            ans.append('B')
            a-=1
            b+=1
    elif s[i]=='AC':
        if a==0 and c==0:
            flag=False
            break
        if a==1 and s[i+1]=='AB' or a==0:
            ans.append('A')
            a+=1
            c-=1
        else:
            ans.append('C')
            a-=1
            c+=1
    else:
        if c==0 and b==0:
            flag=False
            break
        if c==1 and s[i+1]=='AC' or c==0:
            ans.append('C')
            c+=1
            b-=1
        else:
            ans.append('B')
            c-=1
            b+=1
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())
s=[]
for i in range(n):
    s.append(input())

ans=[]

flag=True

for i in range(n):
    if s[i]=='AB':
        if a==0 and b==0:
            flag=False
            break
        if a==1 and b==1 and s[i+1]=='AC' or a==0:
            ans.append('A')
            a+=1
            b-=1
        else:
            ans.append('B')
            a-=1
            b+=1
    elif s[i]=='AC':
        if a==0 and c==0:
            flag=False
            break
        if a==1 and c==1 and s[i+1]=='AB' or a==0:
            ans.append('A')
            a+=1
            c-=1
        else:
            ans.append('C')
            a-=1
            c+=1
    else:
        if c==0 and b==0:
            flag=False
            break
        if c==1 and b==1 and s[i+1]=='AC' or c==0:
            ans.append('C')
            c+=1
            b-=1
        else:
            ans.append('B')
            c-=1
            b+=1
if flag:
    print("Yes")
    for i in ans:
        print(i)
else:
    print("No")

Buggy submission (verdict: Runtime Error):
n=0
s=[]
ans=[]

def dfs(a,b,c,i,root):
    if i==n:
        global ans
        ans=root.copy()
        return True
    l=False
    r=False
    if s[i]=='AB':
        if a>0:
            root.append('B')
            l=dfs(a-1,b+1,c,i+1,root)
            root.pop()
        if b>0:
            root.append('A')
            r=dfs(a+1,b-1,c,i+1,root)
            root.pop()
    elif s[i]=='AC':
        if a>0:
            root.append('C')
            l=dfs(a-1,b,c+1,i+1,root)
            root.pop()
        if c>0:
            root.append('A')
            r=dfs(a+1,b,c-1,i+1,root)
            root.pop()
    else:
        if b>0:
            root.append('C')
            l=dfs(a,b-1,c+1,i+1,root)
            root.pop()
        if c>0:
            root.append('B')
            r=dfs(a,b+1,c-1,i+1,root)
            root.pop()
    return l or r

n,a,b,c=map(int,input().split())
s=['']*n
for i in range(n):
    s[i]=input()
flag=dfs(a,b,c,0,[])

if flag:
    print("Yes")
    for t in ans:
        print(t)
else:
    print("No")

Buggy submission (verdict: Runtime Error):
n=0
s=[]
ans=[]

def dfs(a,b,c,i,root):
    if i==n:
        global ans
        ans=root.copy()
        return True
    l=False
    r=False
    if s[i]=='AB':
        if a>0:
            root.append('B')
            l=dfs(a-1,b+1,c,i+1,root)
            root.pop()
        if not l and b>0:
            root.append('A')
            r=dfs(a+1,b-1,c,i+1,root)
            root.pop()
    elif s[i]=='AC':
        if a>0:
            root.append('C')
            l=dfs(a-1,b,c+1,i+1,root)
            root.pop()
        if not l and c>0:
            root.append('A')
            r=dfs(a+1,b,c-1,i+1,root)
            root.pop()
    else:
        if b>0:
            root.append('C')
            l=dfs(a,b-1,c+1,i+1,root)
            root.pop()
        if not l and c>0:
            root.append('B')
            r=dfs(a,b+1,c-1,i+1,root)
            root.pop()
    return l or r

n,a,b,c=map(int,input().split())
s=['']*n
for i in range(n):
    s[i]=input()
flag=dfs(a,b,c,0,[])

if flag:
    print("Yes")
    for t in ans:
        print(t)
else:
    print("No")