Problem p02692 (python)

Fixed submission:
n,a,b,c=map(int,input().split())
ans=[]
ks = [input() for _ in range(n)]
for i in range(n):
  k = ks[0]
  if k=="AB":
    if a==0 and b==0:
      print("No")
      quit()
    else:
      if a>b:
        a -= 1
        b+=1
        ans.append("B")
      else:
        if a == 1 and b == 1 and len(ks) > 1:
          if ks[1] == "AC":
            b-=1
            a+=1
            ans.append("A")
          else:
            a -= 1
            b+=1
            ans.append("B")
        else: 
          b-=1
          a+=1
          ans.append("A")
  if k=="BC":
    if c==0 and b==0:
      print("No")
      quit()
    else:
      if c>b:
        c -= 1
        b+=1
        ans.append("B")
      else:
        if c == 1 and b == 1 and len(ks) > 1:
          if ks[1] == "AC":
            b-=1
            c+=1
            ans.append("C")
          else:
            c -= 1
            b+=1
            ans.append("B")
        else: 
          b-=1
          c+=1
          ans.append("C")
  if k=="AC":
    if a==0 and c==0:
      print("No")
      quit()
    else:
      if a>c:
        a -= 1
        c+=1
        ans.append("C")
      else:
        if a == 1 and c == 1 and len(ks) > 1:
          if ks[1] == "BC":
            a-=1
            c+=1
            ans.append("C")
          else:
            c -= 1
            a+=1
            ans.append("A")
        else: 
          c-=1
          a+=1
          ans.append("A")
  del(ks[0])
print("Yes")
for i in ans:
  print(i)

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

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