Problem p02692 (python)

Fixed submission:
N,A,B,C = map(int,input().split())
valid = True
array = []
for i in range(N):
    s = input()
    array.append(s)
array2 = []
for i in range(N):
    if array[i] == "AB":
        if A == B == 0:
            valid = False
            break
        elif i != N-1 and A == B and array[i+1] == "AC":
            A += 1
            B -= 1
            array2.append("A")
        elif i != N-1 and A == B and array[i+1] == "BC":
            A -= 1
            B += 1
            array2.append("B")
        elif A <= B:
            A += 1
            B -= 1
            array2.append("A")
        else:
            A -= 1
            B += 1
            array2.append("B")
    elif array[i] == "AC":
        if A == C == 0:
            valid = False
            break
        elif i != N-1 and A == C and array[i+1] == "BC":
            A -= 1
            C += 1
            array2.append("C")
        elif i != N-1 and A == C and array[i+1] == "AC":
            A += 1
            C -= 1
            array2.append("A")
        elif A <= C:
            A += 1
            C -= 1
            array2.append("A")
        else:
            A -= 1
            C += 1
            array2.append("C")
    else:
        if B == C == 0:
            valid = False
            break
        elif i != N-1 and B == C and array[i+1] == "AC":
            B -= 1
            C += 1
            array2.append("C")
        elif i != N-1 and B == C and array[i+1] == "AB":
            B += 1
            C -= 1
            array2.append("B")
        elif B <= C:
            B += 1
            C -= 1
            array2.append("B")
        else:
            B -= 1
            C += 1
            array2.append("C")
if valid:
    print("Yes")
    for i in range(len(array2)):
        print(array2[i])
else:
    print("No")

Buggy submission (verdict: Runtime Error):
N,A,B,C = map(int,input().split())
valid = True
array = []
for i in range(N):
    s = input()
    array.append(s)
array2 = []
for i in range(N):
    if array[i] == "AB":
        if A == B == 0:
            valid = False
            break
        elif i != N-1 and A == B and array[i+1] == "AC":
            A += 1
            B -= 1
            array2.append("A")
        elif i != N-1 and A == B and array[i+1] == "BC":
            A -= 1
            B += 1
            array2.append("B")
        elif A <= B:
            A += 1
            B -= 1
            array2.append("A")
        else:
            A -= 1
            B += 1
            array2.append("B")
    elif array[i] == "AC":
        if A == C == 0:
            valid = False
            break
        elif i != N-1 and A == C and array[i+1] == "BC":
            A -= 1
            C += 1
            array2.append("C")
        elif i != N-1 and A == C and array[i+1] == "AC":
            A += 1
            C -= 1
            array2.append("A")
        elif A <= C:
            A += 1
            C -= 1
            array2.append("A")
        else:
            A -= 1
            C += 1
            array2.append("C")
    else:
        if B == C == 0:
            valid = False
            break
        elif i != N-1 and B == C and arrya[i+1] == "AC":
            B -= 1
            C += 1
            array2.append("C")
        elif i != N-1 and B == C and array[i+1] == "AB":
            B += 1
            C -= 1
            array2.append("B")
        elif B <= C:
            B += 1
            C -= 1
            array2.append("B")
        else:
            B -= 1
            C += 1
            array2.append("C")
if valid:
    print("Yes")
    for i in range(len(array2)):
        print(array2[i])
else:
    print("No")