Problem p02692 (python)

Fixed submission:
var = [0]*3
n, var[0], var[1], var[2] = list(map(int, input().split()))
choice = [input() for i in range(n)]
selVar = []
addVar = 0
res = []
flag = True

for i in range(len(choice)):
    if choice[i] == 'AB':
        selVar = [0,1]
    elif choice[i] == 'BC':
        selVar = [1,2]
    else:
        selVar = [0,2]
    if var[0]+var[1]+var[2] == 0:
        flag = False
        break
    elif var[0]+var[1]+var[2] == 1:
        if var[selVar[0]] == 0 and var[selVar[1]] == 0:
            flag = False
            break
        elif var[selVar[0]] == 0:
            var[selVar[0]] += 1
            var[selVar[1]] -= 1
            addVar = selVar[0]
        else:
            var[selVar[0]] -= 1
            var[selVar[1]] += 1
            addVar = selVar[1]
    elif var[0]+var[1]+var[2] >= 2:
        if var[selVar[0]] == 0 and var[selVar[1]] == 0:
            flag = False
            break
        elif var[selVar[0]] == 0:
            var[selVar[0]] += 1
            var[selVar[1]] -= 1
            addVar = selVar[0]
        elif var[selVar[1]] == 0:
            var[selVar[0]] -= 1
            var[selVar[1]] += 1
            addVar = selVar[1]
        elif var[0]+var[1]+var[2] == 2:
            if i < len(choice) - 1:
                if choice[i] != choice[i+1]:
                    commonLet = set(list(choice[i])) & set(list(choice[i+1]))
                    if 'A' in commonLet:
                        var[0] += 1
                        addVar = 0
                        if selVar[0] == 0:
                            var[selVar[1]] -= 1
                        else:
                            var[selVar[0]] -= 1
                    if 'B' in commonLet:
                        var[1] += 1
                        addVar = 1
                        if selVar[0] == 1:
                            var[selVar[1]] -= 1
                        else:
                            var[selVar[0]] -= 1
                    if 'C' in commonLet:
                        var[2] += 1
                        addVar = 2
                        if selVar[0] == 2:
                            var[selVar[1]] -= 1
                        else:
                            var[selVar[0]] -= 1
                else:
                    var[selVar[0]] -= 1
                    var[selVar[1]] += 1
                    addVar = selVar[1]
            else:
                var[selVar[0]] -= 1
                var[selVar[1]] += 1
                addVar = selVar[1]
        else:
            if var[selVar[0]] < var[selVar[1]]:
                var[selVar[0]] += 1
                var[selVar[1]] -= 1
                addVar = selVar[0]
            else:
                var[selVar[0]] -= 1
                var[selVar[1]] += 1
                addVar = selVar[1]
    if addVar == 0:
        res.append('A')
    elif addVar == 1:
        res.append('B')
    else:
        res.append('C')

if not flag:
    print('No')
else:
    print('Yes')
    for re in res:
        print(re)

Buggy submission (verdict: Runtime Error):
import math
import sys

sys.setrecursionlimit(10**6)

flag = False

def returnDepth(node):
    return math.floor(math.log2(node + 1))

def childNode(node):
    depth = returnDepth(node)
    column = node - 2**depth + 1
    leftChildNode = node + 2**depth + column
    return [leftChildNode, leftChildNode + 1]

def parentNode(node):
    depth = returnDepth(node)
    leftSide = 2**depth - 1
    return int(leftSide - 2**(depth - 1) + math.floor((node - leftSide) / 2))

def sgn(num):
    if num % 2 == 0:
        return 1
    else:
        return -1

def DFS(node):
    global flag
    if flag:
        return 0
    childs = childNode(node)
    depth = returnDepth(node)
    parent = parentNode(node)
    if node > 0:
        route[node] = route[parent]
        if route[parent] != '':
            route[node] += '\n'
        if choice[depth - 1] == 'AB':
            tree[node] = [tree[parent][0] + sgn(node), tree[parent][1] - sgn(node), tree[parent][2]]
            if node % 2 == 0:
                route[node] += 'A'
            else:
                route[node] += 'B'
        elif choice[depth - 1] == 'BC':
            tree[node] = [tree[parent][0], tree[parent][1] + sgn(node), tree[parent][2] - sgn(node)]
            if node % 2 == 0:
                route[node] += 'B'
            else:
                route[node] += 'C'
        elif choice[depth - 1] == 'AC':
            tree[node] = [tree[parent][0] + sgn(node), tree[parent][1], tree[parent][2] - sgn(node)]
            if node % 2 == 0:
                route[node] += 'A'
            else:
                route[node] += 'C'

    if tree[node][0] >= 0 and tree[node][1] >= 0 and tree[node][2] >= 0:
        if childs[0] < len(tree):
            DFS(childs[0])
            DFS(childs[1])
        else:
            print('Yes')
            print(route[node])
            flag = True
    else:
        tree[node] = []

n, a, b, c = list(map(int, input().split()))
choice = [input() for i in range(n)]

tree = [[]] * (2**(n + 1) - 1)
route = [''] * (2**(n + 1) - 1)

tree[0] = [a,b,c]

DFS(0)

if not flag:
    print('No')

Buggy submission (verdict: Runtime Error):
import math

flag = False

def returnDepth(node):
    return math.floor(math.log2(node + 1))

def childNode(node):
    depth = returnDepth(node)
    column = node - 2**depth + 1
    leftChildNode = node + 2**depth + column
    return [leftChildNode, leftChildNode + 1]

def parentNode(node):
    depth = returnDepth(node)
    leftSide = 2**depth - 1
    return int(leftSide - 2**(depth - 1) + math.floor((node - leftSide) / 2))

def sgn(num):
    if num % 2 == 0:
        return 1
    else:
        return -1

def DFS(node):
    global flag
    if flag:
        return 0
    childs = childNode(node)
    depth = returnDepth(node)
    parent = parentNode(node)

    if node > 0:
        route[node] = route[parent]
        if route[parent] != '':
            route[node] += '\n'
        if choice[depth - 1] == 'AB':
            tree[node] = [tree[parent][0] + sgn(node), tree[parent][1] - sgn(node), tree[parent][2]]
            if node % 2 == 0:
                route[node] += 'A'
            else:
                route[node] += 'B'
        elif choice[depth - 1] == 'BC':
            tree[node] = [tree[parent][0], tree[parent][1] + sgn(node), tree[parent][2] - sgn(node)]
            if node % 2 == 0:
                route[node] += 'B'
            else:
                route[node] += 'C'
        elif choice[depth - 1] == 'AC':
            tree[node] = [tree[parent][0] + sgn(node), tree[parent][1], tree[parent][2] - sgn(node)]
            if node % 2 == 0:
                route[node] += 'A'
            else:
                route[node] += 'C'

    if tree[node][0] >= 0 and tree[node][1] >= 0 and tree[node][2] >= 0:
        if childs[0] < len(tree):
            DFS(childs[0])
            DFS(childs[1])
        else:
            print(route[node])
            flag = True
    else:
        tree[node] = []

n, a, b, c = list(map(int, input().split()))
choice = [input() for i in range(n)]

tree = [[]] * (2**(n + 1) - 1)
route = [''] * (2**(n + 1) - 1)

tree[0] = [a,b,c]

DFS(0)

if not flag:
    print('No')