Problem p02692 (python)

Fixed submission:
# -*- coding: utf-8 -*-
import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N,A,B,C=map(int,input().split())
    n=[A,B,C]
    d={'AB':0,'BC':1,'AC':2}
    before=[[],[],[]]
    ans=[-1]*N
    for i in range(N):
        s=input()
        if n[d[s]]==0 and n[(d[s]+1)%3]==0:
            if before[d[s]] and before[(d[s]+1)%3]:
                if before[d[s]][-1][0]>before[(d[s]+1)%3][-1][0]:
                    k,l=before[d[s]].pop()
                    n[d[s]]+=2
                    n[l]-=2
                    ans[k]=d[s]
                else:
                    k,l=before[(d[s]+1)%3].pop()
                    n[(d[s]+1)%3]+=2
                    n[l]-=2
                    ans[k]=(d[s]+1)%3
            elif before[d[s]] or before[(d[s]+1)%3]:
                if before[d[s]]:
                    k,l=before[d[s]].pop()
                    n[d[s]]+=2
                    n[l]-=2
                    ans[k]=d[s]
                elif before[(d[s]+1)%3]:
                    k,l=before[(d[s]+1)%3].pop()
                    n[(d[s]+1)%3]+=2
                    n[l]-=2
                    ans[k]=(d[s]+1)%3
            else:
                print('No')
                exit()
        if n[d[s]]==0:
            ans[i]=d[s]
            n[d[s]]+=1
            n[(d[s]+1)%3]-=1
        elif n[(d[s]+1)%3]==0:
            ans[i]=(d[s]+1)%3
            n[d[s]]-=1
            n[(d[s]+1)%3]+=1
        else:
            if n[d[s]]<n[(d[s]+1)%3]:
                ans[i]=d[s]
                n[d[s]]+=1
                n[(d[s]+1)%3]-=1
                before[(d[s]+1)%3].append((i,d[s]))
            else:
                ans[i]=(d[s]+1)%3
                n[d[s]]-=1
                n[(d[s]+1)%3]+=1
                before[d[s]].append((i,(d[s]+1)%3))
    print('Yes')
    d2=['A','B','C']
    for a in ans:
        print(d2[a])

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N,A,B,C=map(int,input().split())
    n=[A,B,C]
    d={'AB':0,'BC':1,'AC':2}
    before=[-1,-1,-1]
    ans=[-1]*N
    for i in range(N):
        s=input()
        if n[d[s]]==0 and n[(d[s]+1)%3]==0:
            if before[d[s]]!=-1:
                n[d[s]]+=1
                n[(d[s]-1)%3]-=1
                before[d[s]]=-1
                ans[before[d[s]]]=(d[s]-1)%3
            print('No')
            exit()
        if n[d[s]]==0:
            ans[i]=d[s]
            n[d[s]]+=1
            n[(d[s]+1)%3]-=1
        elif n[(d[s]+1)%3]==0:
            ans[i]=(d[s]+1)%3
            n[d[s]]-=1
            n[(d[s]+1)%3]+=1
        else:
            ans[i]=d[s]
            before[(d[s]+1)%3]=i
    print('Yes')
    d2=['A','B','C']
    for a,b in ans:
        print(d2[a])
                
                    

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N,A,B,C=map(int,input().split())
    n=[A,B,C]
    d={'AB':0,'BC':1,'AC':2}
    c=[deque() for _ in range(3)]
    ans=[-1]*N
    sousa=[]
    for i in range(N):
        s=input()
        sousa.append(d[s])
        if n[d[s]]==0 and n[(d[s]+1)%3]==0:
            print('No')
            exit()
        elif n[d[s]]==0:
            ans[i]=d[s]
            n[d[s]]+=1
            n[(d[s]+1)%3]-=1
        elif n[(d[s]+1)%3]==0:
            ans[i]=(d[s]+1)%3
            n[d[s]]-=1
            n[(d[s]+1)%3]+=1
        else:
            c[d[s]].append(i)
        if len(c[d[s]])>=2:
            if n[(d[s]+2)%3]>=1:
                if n[d[s]]>=1:
                    k=c[d[s]].popleft()
                    ans[k]=(d[s]+1)%3
                    k=c[d[s]].popleft()
                    ans[k]=d[s]%3
                else:
                    k=c[d[s]].popleft()
                    ans[k]=d[s]%3
                    k=c[d[s]].popleft()
                    ans[k]=(d[s]+1)%3
        if len(c[0])>=1 and len(c[1])>=1 and len(c[2])>=1:
            for j in range(3):
                if n[j]>=1:
                    start=j
                    break
            for j in range(start,3+start):
                j%=3
                k=c[j].popleft()
                ans[k]=(j+1)%3
    n=[A,B,C]
    print('Yes')
    d2=['A','B','C']
    raise Exception
    for x,y in zip(ans,sousa):
        if x!=-1:
            n[x]+=1
            if x==y:
                n[(x+1)%3]-=1
            else:
                n[(x+2)%3]-=1
            print(d2[x])
        else:
            if n[y]>=n[(y+1)%3]:
                n[y]-=1
                n[(y+1)%3]+=1
                print(d2[(y+1)%3])
            elif n[(y+1)%3]>=1:
                n[y]+=1
                n[(y+1)%3]-=1
                print(d2[y])
            else:
                raise Exception
                
                    

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N,A,B,C=map(int,input().split())
    n=[A,B,C]
    d={'AB':0,'BC':1,'AC':2}
    c=[deque() for _ in range(3)]
    ans=[-1]*N
    sousa=[]
    for i in range(N):
        s=input()
        sousa.append(d[s])
        if n[d[s]]==0 and n[(d[s]+1)%3]==0:
            raise Exception
            print('No')
            exit()
        elif n[d[s]]==0:
            ans[i]=d[s]
            n[d[s]]+=1
            n[(d[s]+1)%3]-=1
        elif n[(d[s]+1)%3]==0:
            ans[i]=(d[s]+1)%3
            n[d[s]]-=1
            n[(d[s]+1)%3]+=1
        else:
            c[d[s]].append(i)
        if len(c[d[s]])>=2:
            if n[(d[s]+2)%3]>=1:
                if n[d[s]]>=1:
                    k=c[d[s]].popleft()
                    ans[k]=(d[s]+1)%3
                    k=c[d[s]].popleft()
                    ans[k]=d[s]%3
                else:
                    k=c[d[s]].popleft()
                    ans[k]=d[s]%3
                    k=c[d[s]].popleft()
                    ans[k]=(d[s]+1)%3
        if len(c[0])>=1 and len(c[1])>=1 and len(c[2])>=1:
            for j in range(3):
                if n[j]>=1:
                    start=j
                    break
            for j in range(start,3+start):
                j%=3
                k=c[j].popleft()
                ans[k]=(j+1)%3
    n=[A,B,C]
    print('Yes')
    d2=['A','B','C']
    for x,y in zip(ans,sousa):
        if x!=-1:
            n[x]+=1
            if x==y:
                n[(x+1)%3]-=1
            else:
                n[(x-1)%3]-=1
            print(d2[x])
        else:
            if n[y]>=n[(y+1)%3]:
                n[y]-=1
                n[(y+1)%3]+=1
                print(d2[(y+1)%3])
            elif n[(y+1)%3]>=1:
                n[y]+=1
                n[(y+1)%3]-=1
                print(d2[y])
            else:
                raise Exception
                
                    

if __name__ == '__main__':
    main()