Problem p02689 (python)

Fixed submission:
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N,M=MI()
    H=LI()
    adj=[[]for _ in range(N)]
    for i in range(M):
        a,b=MI()
        a-=1
        b-=1
        adj[a].append(b)
        adj[b].append(a)
        
    used=[0]*N
    Hi=[[0,0]for _ in range(N)]
    for i in range(N):
        Hi[i][0]=H[i]
        Hi[i][1]=i
        
    Hi.sort(reverse=True)
    
    ans=0
    for i in range(N):
        v=Hi[i][1]
        HH=Hi[i][0]
        if used[v]==0:
            temph=0
            used[v]=1
            for nv in adj[v]:
                temph=max(H[nv],temph)
                used[nv]=1
            if HH>temph:
                ans+=1
                
    print(ans)
        
    

main()

Buggy submission (verdict: Runtime Error):
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N,A,B,C=MI()
    S=[]
    SS=A+B+C
    for i in range(N):
        s=input()
        S.append(s)
    
    S.append("AB")
    S.append("AB")#適当に入れとけ
    
    ans=[]
    flag=1
    if SS<1:
        flag=-1
    elif SS>=3 or SS==1:
        for i in range(N):
            s=S[i]
            if s=="AB":
                if A<B:
                    A+=1
                    B-=1
                    ans.append("A")
                elif B>A:
                    A-=1
                    B+=1
                    ans.append("B")
                else:#同数
                    s2=S[i+1]
                    if s2=="AC":
                        A+=1
                        B-=1
                        ans.append("A")
                    else:
                        A-=1
                        B+=1
                        ans.append("B")
                        
                    
            elif s=="AC":
                if A<C:
                    A+=1
                    C-=1
                    ans.append("A")
                elif A>C:
                    A-=1
                    C+=1
                    ans.append("C")
                else:#同数
                    s2=S[i+1]
                    if s2=="AB":
                        A+=1
                        C-=1
                        ans.append("A")
                    else:
                        A-=1
                        C+=1
                        ans.append("C")
                    
            else:#BC
                if C<B:
                    C+=1
                    B-=1
                    ans.append("C")
                elif C>B:
                    C-=1
                    B+=1
                    ans.append("B")
                else:#同数
                    s2=S[i+1]
                    if s2=="AC":
                        C+=1
                        B-=1
                        ans.append("C")
                    else:
                        C-=1
                        B+=1
                        ans.append("B")
                
   
        
                
            if A<0 or B<0 or C<0:
                flag=-1
                break
                
        
        
    if flag==-1:
        print("No")
    else:
        print("Yes")
        for i in range(N):
            print(ans[i])
            
            
        
    
    

main()