Problem p02690 (python)

Fixed submission:
MOD = 10 ** 9 + 7
INF = 10 ** 11
import sys
sys.setrecursionlimit(100000000)
from bisect import bisect_right,bisect_left

def main():
    X = int(input())
    for i in range(1000):
        for j in range(1000):
            p = pow(i,5)
            q = pow(j,5)
            if p + q == X:
                print(i,-j)
                return
            elif p - q == X:
                print(i,j)
                return
            elif q - p == X:
                print(j,i)
                return
                
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
MOD = 10 ** 9 + 7
INF = 10 ** 11
import sys
sys.setrecursionlimit(100000000)

def main():
    N,A,B,C = map(int,input().split())
    choice = [input() for _ in range(N)]
    ab = 0
    bc = 0
    ca = 0
    for s in choice:
        if s == 'AB':
            ab += 1
        elif s == 'BC':
            bc += 1
        else:
            ca += 1
    
    ans = [[0,0] for _ in range(3)]
    if A > 0 or B > 0:
        ans[0][0] += ab//2
        ans[0][1] += ab//2
        ab %= 2
    else:
        if bc > 0 and C > 0:
            bc -= 1
            C -= 1
            B += 1
            ans[1][0] += 1
        elif ca > 0 and C > 0:
            ca -= 1
            C -= 1
            A += 1
            ans[2][0] += 1
        else:
            print('No')
            return
        ans[0][0] += ab//2
        ans[0][1] += ab//2
        ab %= 2
        
    if B > 0 or C > 0:
        ans[1][0] += bc//2
        ans[1][1] += bc//2
        bc %= 2
    else:
        if ab > 0 and A > 0:
            A -= 1
            B += 1
            ab -= 1
            ans[0][1] += 1
        elif ca > 0 and A > 0:
            A -= 1
            C += 1
            ca -= 1
            ans[2][0] += 1
        else:
            print('No')
            return
        ans[1][0] += bc//2
        ans[1][1] += bc//2
        bc %= 2
                
    if C > 0 or A > 0:
        ans[2][0] += ca//2
        ans[2][1] += ca//2
        ca %= 2
    else:
        if bc > 0 and B > 0:
            B -= 1
            C += 1
            bc -= 1
            ans[1][1] += 1
        elif ab > 0 and B > 0:
            B -= 1
            A += 1
            ab -= 1
            ans[0][0] += 1
        else:
            print('No')
            return
        ans[1][0] += bc//2
        ans[1][1] += bc//2
        bc %= 2
    
    if ab > 0 and bc > 0 and ca > 0:
        if A > 0 or B > 0 or C > 0:
            ans[0][1] += 1
            ans[1][1] += 1
            ans[2][1] += 1
        else:
            print('No')
            return
    elif ab > 0 and bc > 0:
        if A > 0:
            ans[0][1] += 1
            ans[1][1] += 1
        elif C > 0:
            ans[1][0] += 1
            ans[0][0] += 1
        elif B > 1:
            ans[0][0] += 1
            ans[1][1] += 1
        else:
            print('No')
            return
    elif bc > 0 and ca > 0:
        if A > 0:
            ans[2][0] += 1
            ans[1][0] += 1
        elif B > 0:
            ans[1][1] += 1
            ans[2][0] += 1
        elif C > 1:
            ans[1][0] += 1
            ans[2][1] += 1
        else:
            print('No')
            return
    elif ca > 0 and ab > 0:
        if C > 0:
            ans[2][0] += 1
            ans[0][1] += 1
        elif B > 0:
            ans[1][0] += 1
            ans[2][0] += 1
        elif A > 1:
            ans[0][1] += 1
            ans[2][1] += 1
        else:
            print('No')
            return
    
    elif ab > 0:
        if A > 0:
            ans[0][1] += 1
        elif B > 0:
            ans[0][0] += 1
        else:
            print('No')
            return
    
    elif bc > 0:
        if B > 0:
            ans[1][1] += 1
        elif C > 0:
            ans[1][0] += 1
        else:
            print('No')
            return
    
    elif ca > 0:
        if A > 0:
            ans[2][1] += 1
        elif C > 0:
            ans[2][0] += 1
        else:
            print('No')
            return
    

    print('Yes')
    for s in choice:
        if s == 'AB':
            if ans[0][0] > 0:
                print('A')
                ans[0][0] -= 1
            else:
                print('B')
        elif s == 'BC':
            if ans[1][0] > 0:
                print('B')
                ans[1][0] -= 1
            else:
                print('C')
        else:
            if ans[2][0] > 0:
                print('C')
                ans[2][0] -= 1
            else:
                print('A')

if __name__ == '__main__':
    main()