Problem p02692 (python)

Fixed submission:
n,a,b,c=map(int,input().split())
#a+b+c>1のときは必ずYes(追記:入力例3が優しい)
#a+b+c==1のときは操作が限定される
#a+b+c==0のときはNo
#a+b+c>=3のときは大きい方を-1、小さい方を+1の貪欲でいける
#a+b+c=2のときは何手か先まで読む
s=[input()for _ in range(n)]
if s[0]=="AC" and a==c==0:print("No");exit()
if s[0]=="BC" and b==c==0:print("No");exit()
if s[0]=="AB" and a==b==0:print("No");exit()
if (a+b+c)==0:print("No");exit()
if (a+b+c)==1:
  ans=[]
  for i in s:
    if i=="AB":
      if a==1:ans.append("B");a,b=0,1
      elif b==1:ans.append("A");a,b=1,0
      else:print("No");exit()
    elif i=="AC":
      if a==1:ans.append("C");a,c=0,1
      elif c==1:ans.append("A");a,c=1,0
      else:print("No");exit()
    else:
      if b==1:ans.append("C");b,c=0,1
      elif c==1:ans.append("B");b,c=1,0
      else:print("No");exit()
  print("Yes")
  for i in ans:print(i)
  exit()
if (a+b+c)>=3:
  print("Yes")
  for i in s:
    if i=="AB":
      if a>=b:print("B");a-=1;b+=1
      else:print("A");a+=1;b-=1
    elif i=="BC":
      if b>=c:print("C");c+=1;b-=1
      else:print("B");b+=1;c-=1
    else:
      if a>=c:print("C");c+=1;a-=1
      else:print("A");a+=1;c-=1
  exit()
#a+b+c=2
#何手まで読むか
#ダメな操作の例は？
#110AB,020AC
#つまりNoになるのはその直後
#1手先まで読めばいい
print("Yes")
for i in range(n-1):
  if s[i]=="AB":
    if a==b==1:
      if s[i+1]=="AC":print("A");a+=1;b-=1
      else:print("B");b+=1;a-=1
    else:
      if a>=b:print("B");a-=1;b+=1
      else:print("A");a+=1;b-=1
  if s[i]=="BC":
    if b==c==1:
      if s[i+1]=="AB":print("B");b+=1;c-=1
      else:print("C");c+=1;b-=1
    else:
      if b>=c:print("C");c+=1;b-=1
      else:print("B");b+=1;c-=1
  if s[i]=="AC":
    if a==c==1:
      if s[i+1]=="BC":print("C");c+=1;a-=1
      else:print("A");a+=1;c-=1
    else:
      if a>=c:print("C");c+=1;a-=1
      else:print("A");a+=1;c-=1
if s[-1]=="AB":
  if a>=b:print("B");a-=1;b+=1
  else:print("A");a+=1;b-=1
elif s[-1]=="BC":
  if b>=c:print("C");c+=1;b-=1
  else:print("B");b+=1;c-=1
else:
  if a>=c:print("C");c+=1;a-=1
  else:print("A");a+=1;c-=1

Buggy submission (verdict: Runtime Error):
n,a,b,c=map(int,input().split())
s=[input()for _ in range(n)]
if (a+b+c)==0:
  print("No")
if (a+b+c)==1 or (a+b+c)>=3:
  ans=[]
  for i in s:
    if i=="AB":
      if a<b:ans.append("A");a+=1;b+=-1;c+=0
      else:ans.append("B");a+=-1;b+=1;c+=0
    if i=="BC":
      if b<c:ans.append("B");a+=0;b+=1;c+=-1
      else:ans.append("C");a+=0;b+=-1;c+=1
    if i=="AC":
      if a<c:ans.append("A");a+=1;b+=0;c+=-1
      else:ans.append("C");a+=-1;b+=0;c+=1
    if min(a,b,c)<0:
      exit(print("No"))
  print("Yes")
  for i in ans:print(i)
if (a+b+c)==2:
  ans=[]
  for i in range(n-1):
    if s[i]=="AB":
      if a==b:
        if s[i+1]=="AC":ans.append("A");a+=1;b+=-1;c+=0
        else:ans.append("B");a+=-1;b+=1;c+=0
      elif a<b:ans.append("A");a+=1;b+=-1;c+=0
      else:ans.append("B");a+=-1;b+=1;c+=0
    if s[i]=="BC":
      if b==c:
        if s[i+1]=="AC":ans.append("C");a+=0;b+=-1;c+=1
        else:ans.append("B");a+=0;b+=1;c+=-1
      elif b<c:ans.append("B");a+=0;b+=1;c+=-1
      else:ans.append("C");a+=0;b+=-1;c+=1
    if s[i]=="AC":
      if a==c:
        if s[i+1]=="BC":ans.append("C");a+=-1;b+=0;c+=1
        else:ans.append("A");a+=1;b+=0;c+=-1
      elif a<c:ans.append("A");a+=1;b+=0;c+=-1
      else:ans.append("C");a+=-1;b+=0;c+=1
      if min(a,b,c)<0:
      exit(print("No"))
  for i in s[n-1:]:
    if i=="AB":
      if a<b:ans.append("A");a+=1;b+=-1;c+=0
      else:ans.append("B");a+=-1;b+=1;c+=0
    if i=="BC":
      if b<c:ans.append("B");a+=0;b+=1;c+=-1
      else:ans.append("C");a+=0;b+=-1;c+=1
    if i=="AC":
      if a<c:ans.append("A");a+=1;b+=0;c+=-1
      else:ans.append("C");a+=-1;b+=0;c+=1
    if min(a,b,c)<0:
      exit(print("No"))
  print("Yes")
  for i in ans:print(i)