Problem p02692 (python)

Fixed submission:
import sys
input = sys.stdin.readline
n,a,b,c = map(int,input().strip().split())
cc = []
for i in range(n):
  ff = input().strip()
  cc.append((ff))
  
chk = True
ans = []
for num,q in enumerate(cc):
  if q[0] == 'A' and q[1] == 'B':
    m = max(a,b)
    if m < 1:
      chk = False
      break
    if a == 1 and b == 1:
      if num == n-1:
        ans.append('A')
      else:
        if cc[num+1][0] == 'A':
          a += 1
          b -= 1
          ans.append('A')
        else:
          a -= 1
          b += 1
          ans.append('B')       
    elif m == a:
      a -= 1
      b += 1
      ans.append('B')
    else:
      a += 1
      b -= 1
      ans.append('A')      
  if q[0] == 'A' and q[1] == 'C':
    m = max(a,c)
    if m < 1:
      chk = False
      break
    if c == 1 and a == 1:
      if num == n-1:
        ans.append('A')
      else:
        if cc[num+1][0] == 'A':
          a += 1
          c -= 1
          ans.append('A')
        else:
          a -= 1
          c += 1
          ans.append('C')       
    elif m == a:
      a -= 1
      c += 1
      ans.append('C')       
    else:
      a += 1
      c -= 1
      ans.append('A')       
  if q[0] == 'B' and q[1] == 'C':
    m = max(b,c)
    if m < 1:
      chk = False
      break
    if c == 1 and b == 1:
      if num == n-1:
        ans.append('B')
      else:      
        if cc[num+1][1] == 'C':
          c += 1
          b -= 1
          ans.append('C')
        else:
          c -= 1
          b += 1
          ans.append('B')       
    elif m == b:
      b -= 1
      c += 1
      ans.append('C')
    else:
      b += 1
      c -= 1
      ans.append('B')
      
if not chk:
  print('No')
else:
  print('Yes')
  print(*ans,sep='\n')

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
n,a,b,c = map(int,input().strip().split())
cc = []
for i in range(n):
  ff = input().strip()
  cc.append((ff))
print(cc,cc[0][0],cc[1],cc[1][0],cc[1][1])  
chk = True
ans = []
for num,q in enumerate(cc):
  if q[0] == 'A' and q[1] == 'B':
    m = max(a,b)
    if m < 1:
      chk = False
      break
    if a == 1 and b == 1:
      if cc[min(n-1,num+1)][0] == 'A':
        a += 1
        b -= 1
        ans.append('A')
      else:
        a -= 1
        b += 1
        ans.append('B')       
    elif m == a:
      a -= 1
      b += 1
      ans.append('B')
    else:
      a += 1
      b -= 1
      ans.append('A')      
  if q[0] == 'A' and q[1] == 'C':
    m = max(a,c)
    if m < 1:
      chk = False
      break
    if c == 1 and a == 1:
      if cc[min(n-1,num+1)][0] == 'A':
        a += 1
        c -= 1
        ans.append('A')
      else:
        a -= 1
        c += 1
        ans.append('C')       
    if m == a:
      a -= 1
      c += 1
      ans.append('C')       
    else:
      a += 1
      c -= 1
      ans.append('A')       
  if q[0] == 'B' and q[1] == 'C':
    m = max(b,c)
    if m < 1:
      chk = False
      break
    if c == 1 and b == 1:
      if cc[min(n-1,num+1)][1] == 'C':
        c += 1
        b -= 1
        ans.append('C')
      else:
        c -= 1
        b += 1
        ans.append('B')       
    if m == b:
      b -= 1
      c += 1
      ans.append('C')
    else:
      b += 1
      c -= 1
      ans.append('B')
      
if not chk:
  print('No')
else:
  print('Yes')
  print(*ans,sep='\n')

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
n,a,b,c = map(int,input().strip().split())
cc = []
for i in range(n):
  ff = input().strip()
  cc.append((ff))
  
chk = True
ans = []
for num,q in enumerate(cc):
  if q[0] == 'A' and q[1] == 'B':
    m = max(a,b)
    if m < 1:
      chk = False
      break
    if a == b and b == 1:
      if q[num+1][0] == 'A':
        a += 1
        b -= 1
        ans.append('A')
      else:
        a -= 1
        b += 1
        ans.append('B')       
    elif m == a:
      a -= 1
      b += 1
      ans.append('B')
    else:
      a += 1
      b -= 1
      ans.append('A')      
  if q[0] == 'A' and q[1] == 'C':
    m = max(a,c)
    if m < 1:
      chk = False
      break
    if a == c and a == 1:
      if q[num+1][0] == 'A':
        a += 1
        b -= 1
        ans.append('A')
      else:
        a -= 1
        c += 1
        ans.append('C')       
    if m == a:
      a -= 1
      c += 1
      ans.append('C')       
    else:
      a += 1
      c -= 1
      ans.append('A')       
  if q[0] == 'B' and q[1] == 'C':
    m = max(b,c)
    if m < 1:
      chk = False
      break
    if c == b and b == 1:
      if q[num+1][1] == 'C':
        c += 1
        b -= 1
        ans.append('C')
      else:
        c -= 1
        b += 1
        ans.append('B')       
    if m == b:
      b -= 1
      c += 1
      ans.append('C')
    else:
      b += 1
      c -= 1
      ans.append('B')
      
if not chk:
  print('No')
else:
  print('Yes')
  print(*ans,sep='\n')