Problem p02692 (python)

Fixed submission:
'''
自宅用PCでの解答
'''
import math
#import numpy as np
import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7
dir = [(-1,0),(0,-1),(1,0),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"

def main():
    n,a,b,c = map(int,ipt().split())
    s = a+b+c
    q = [input() for i in range(n)]
    ans = []

    for i,qi in enumerate(q):
        if qi == "AB":
            if a == b == 0:
                print("No")
                exit()
            elif a == 0:
                ans.append("A")
                a += 1
                b -= 1
            elif b == 0:
                ans.append("B")
                a -= 1
                b += 1
            elif i == n-1:
                ans.append("A")
            elif a == b == 1:
                if q[i+1] == "BC":
                    ans.append("B")
                    b += 1
                    a -= 1
                else:
                    ans.append("A")
                    b -= 1
                    a += 1
            else:
                if a > b:
                    ans.append("B")
                    b += 1
                    a -= 1
                else:
                    ans.append("A")
                    a += 1
                    b -= 1
        elif qi == "BC":
            if c == b == 0:
                print("No")
                exit()
            elif c == 0:
                ans.append("C")
                c += 1
                b -= 1
            elif b == 0:
                ans.append("B")
                c -= 1
                b += 1
            elif i == n-1:
                ans.append("C")
            elif c == b == 1:
                if q[i+1] == "AB":
                    ans.append("B")
                    b += 1
                    c -= 1
                else:
                    ans.append("C")
                    b -= 1
                    c += 1
            else:
                if c > b:
                    ans.append("B")
                    b += 1
                    c -= 1
                else:
                    ans.append("C")
                    c += 1
                    b -= 1
        else:
            if a == c == 0:
                print("No")
                exit()
            elif a == 0:
                ans.append("A")
                a += 1
                c -= 1
            elif c == 0:
                ans.append("C")
                a -= 1
                c += 1
            elif i == n-1:
                ans.append("A")
            elif a == c == 1:
                if q[i+1] == "BC":
                    ans.append("C")
                    c += 1
                    a -= 1
                else:
                    ans.append("A")
                    c -= 1
                    a += 1
            else:
                if a > c:
                    ans.append("C")
                    c += 1
                    a -= 1
                else:
                    ans.append("A")
                    a += 1
                    c -= 1

    print("Yes")
    for i in ans:
        print(i)
    return None

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import numpy as np
import math
n, a, b, c = map(int,input().split())

ans = []
s = input()
f = True
for i in range(n):
    if not i == n-1:
        ns = input()
    if s == "AB": 
        if a == 0 and b == 0:
            print("No")
            f = False
            break
        elif ns == "AC" and c == 0 and a == 1:
            if b == 0:
                print("No")
                f = False
                break
            a += 1
            b -= 1
            ans.append("A")
        elif ns == "BC" and c == 0 and b == 1:
            if a == 0:
                print("No")
                f = False
                break
            b += 1
            a -= 1
            ans.append("B")
        elif a > b:
            a -= 1
            b += 1
            ans.append("B")
        else:
            a += 1
            b -= 1
            ans.append("A")
    if s == "BC": 
        if c == 0 and b == 0:
            print("No")
            f = False
            break
        elif ns == "AC" and a == 0 and c == 1:
            if b == 0:
                print("No")
                f = False
                break
            c += 1
            b -= 1
            ans.append("C")
        elif ns == "AB" and a == 0 and b == 1:
            if c == 0:
                print("No")
                f = False
                break
            b += 1
            c -= 1
            ans.append("B")
        elif c > b:
            c -= 1
            b += 1
            ans.append("B")
        else:
            c += 1
            b -= 1
            ans.append("C")
    if s == "AC": 
        if a == 0 and c == 0:
            print("No")
            f = False
            break
        elif ns == "BC" and b == 0 and c == 1:
            if a == 0:
                print("No")
                f = False
                break
            c += 1
            a -= 1
            ans.append("C")
        elif ns == "AB" and b == 0 and a == 1:
            if c == 0:
                print("No")
                f = False
                break
            a += 1
            c -= 1
            ans.append("A")
        elif c > a:
            c -= 1
            a += 1
            ans.append("A")
        else:
            c += 1
            a -= 1
            ans.append("C")
    s = ns

if f:
    print("Yes")
    for i in ans:
        print(i)

Buggy submission (verdict: Runtime Error):
import numpy as np
import math
n, a, b, c = map(int,input().split())
s = a+b+c

ans = []
s = input()
f = True
for i in range(n):
    if not i == n-1:
        ns = input()
    if s == "AB": 
        if a == 0 and b == 0:
            print("No")
            f = False
            break
        elif ns == "AC" and c == 0 and a == 1:
            if b == 0:
                print("No")
                f = False
                break
            a += 1
            b -= 1
            ans.append("A")
        elif ns == "BC" and c == 0 and b == 1:
            if a == 0:
                print("No")
                f = False
                break
            b += 1
            a -= 1
            ans.append("B")
        elif a > b:
            a -= 1
            b += 1
            ans.append("B")
        else:
            a += 1
            b -= 1
            ans.append("A")
    if s == "BC": 
        if c == 0 and b == 0:
            print("No")
            f = False
            break
        elif ns == "AC" and a == 0 and c == 1:
            if b == 0:
                print("No")
                f = False
                break
            c += 1
            b -= 1
            ans.append("C")
        elif ns == "AB" and a == 0 and b == 1:
            if c == 0:
                print("No")
                f = False
                break
            b += 1
            c -= 1
            ans.append("B")
        elif c > b:
            c -= 1
            b += 1
            ans.append("B")
        else:
            c += 1
            b -= 1
            ans.append("C")
    if s == "AC": 
        if a == 0 and c == 0:
            print("No")
            f = False
            break
        elif ns == "BC" and b == 0 and c == 1:
            if a == 0:
                print("No")
                f = False
                break
            c += 1
            a -= 1
            ans.append("C")
        elif ns == "AB" and b == 0 and a == 1:
            if c == 0:
                print("No")
                f = False
                break
            a += 1
            c -= 1
            ans.append("A")
        elif c > b:
            c -= 1
            a += 1
            ans.append("A")
        else:
            c += 1
            a -= 1
            ans.append("C")
    s = ns

if f:
    print("Yes")
    for i in ans:
        print(i)