Problem p02692 (python)

Fixed submission:
import sys
def main():
    input = sys.stdin.readline
    N,A,B,C=map(int, input().split())
    S=[input().rstrip() for _ in range(N)]

    MX=2
    dp=[[[[None]*(MX+1) for _ in range(MX+1)] for __ in range(MX+1)] for ___ in range(N+4)]
    dp[0][min(MX,A)][min(MX,B)][min(MX,C)]=1
    for i,s in enumerate(S):
        ni=i+1
        for a in range(MX+1):
            for b in range(MX+1):
                for c in range(MX+1):
                    for j in range(2):
                        na,nb,nc=a,b,c
                        sel=None
                        if s=='AB':
                            if j==0:
                                if b==0: continue
                                na,nb=min(MX,na+1),nb-1
                                sel='A'
                            else:
                                if a==0: continue
                                na,nb=na-1,min(MX,nb+1)
                                sel='B'
                        elif s=='AC':
                            if j==0:
                                if c==0: continue
                                na,nc=min(MX,na+1),nc-1
                                sel='A'
                            else:
                                if a==0: continue
                                na,nc=na-1,min(MX,nc+1)
                                sel='C'
                        elif s=='BC':
                            if j==0:
                                if c==0: continue
                                nb,nc=min(MX,nb+1),nc-1
                                sel='B'
                            else:
                                if b==0: continue
                                nb,nc=nb-1,min(MX,nc+1)
                                sel='C'
                        if dp[i][a][b][c]: dp[ni][na][nb][nc]=(a,b,c,sel)

    flag=False
    ga,gb,gc=0,0,0
    for a in range(MX+1):
        for b in range(MX+1):
            for c in range(MX+1):
                if dp[N][a][b][c]:
                    flag=True
                    ga,gb,gc=a,b,c
    if flag==False:
        print('No')
        return
    print('Yes')
    ans=[]
    for i in range(N,0,-1):
        ga,gb,gc,sel=dp[i][ga][gb][gc]
        ans.append(sel)
    for a in reversed(ans):
        print(a)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
def main():
    input = sys.stdin.readline
    N,A,B,C=map(int, input().split())
    S=[input().rstrip() for _ in range(N)]

    MX=3
    dp=[[[[None]*MX+1 for _ in range(MX+1)] for __ in range(MX+1)] for ___ in range(N+4)]
    dp[0][min(MX,A)][min(MX,B)][min(MX,C)]=1
    for i,s in enumerate(S):
        ni=i+1
        for a in range(MX+1):
            for b in range(MX+1):
                for c in range(MX+1):
                    for j in range(2):
                        na,nb,nc=a,b,c
                        sel=None
                        if s=='AB':
                            if j==0:
                                if b==0: continue
                                na,nb=min(MX,na+1),nb-1
                                sel='A'
                            else:
                                if a==0: continue
                                na,nb=na-1,min(MX,nb+1)
                                sel='B'
                        elif s=='AC':
                            if j==0:
                                if c==0: continue
                                na,nc=min(MX,na+1),nc-1
                                sel='A'
                            else:
                                if a==0: continue
                                na,nc=na-1,min(MX,nc+1)
                                sel='C'
                        elif s=='BC':
                            if j==0:
                                if c==0: continue
                                nb,nc=min(MX,nb+1),nc-1
                                sel='B'
                            else:
                                if b==0: continue
                                nb,nc=nb-1,min(MX,nc+1)
                                sel='C'
                        if dp[i][a][b][c]: dp[ni][na][nb][nc]=(a,b,c,sel)

    flag=False
    ga,gb,gc=0,0,0
    for a in range(MX+1):
        for b in range(MX+1):
            for c in range(MX+1):
                if dp[N][a][b][c]:
                    flag=True
                    ga,gb,gc=a,b,c
    if flag==False:
        print('No')
        return
    print('Yes')
    ans=[]
    for i in range(N,0,-1):
        ga,gb,gc,sel=dp[i][ga][gb][gc]
        ans.append(sel)
    for a in reversed(ans):
        print(a)

if __name__ == '__main__':
    main()