Problem p02837 (python)

Fixed submission:
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
import itertools
import queue
from collections import deque
from fractions import Fraction


def ternary (n, k, length):
    if n == 0:
        nums = ['0' for i in range(length)]
        nums = ''.join(nums)
        return nums
    nums = ''
    while n:
        n, r = divmod(n, k)
        nums += str(r)
    nums = nums[::-1]
    nums = nums.zfill(length)
    return nums

def main():
    num = int(input())
    say_count = []
    say_data = []
    for i in range(num):
        aaa = int(input())
        say_count.append(aaa)
        data = [list(map(int, input().split())) for i in range(aaa)]
        say_data.append(data)


    ans = 0
    for i in range(2 ** num):
        bin_data = list(map(int, ternary(i, 2, num)))
        kari_ans = bin_data.count(1)

        people_flg = [-1 for i in range(num)]
        dame_flg = 0
        for j in range(num):
            if people_flg[j] == -1:
                people_flg[j] = bin_data[j]
            else:
                if people_flg[j] != bin_data[j]:
                    dame_flg = 1

            if people_flg[j] == 0:
                continue

            for k in range(say_count[j]):
                people, flg = say_data[j][k]
                people -= 1
                if people_flg[people] == -1:
                    people_flg[people] = flg
                else:
                    if people_flg[people] != flg:
                        dame_flg = 1

            if dame_flg:
                break
        if dame_flg:
            continue
        ans = max(ans, kari_ans)

    print(ans)






if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
import itertools
import queue
from collections import deque
from fractions import Fraction


def ternary (n, k, length):
    if n == 0:
        nums = ['0' for i in range(length)]
        nums = ''.join(nums)
        return nums
    nums = ''
    while n:
        n, r = divmod(n, k)
        nums += str(r)
    nums = nums[::-1]
    nums = nums.zfill(length)
    return nums

def main():
    num = int(input())
    say_count = []
    say_data = []
    for i in range(num):
        aaa = int(input())
        say_count.append(aaa)
        data = [list(map(int, input().split())) for i in range(aaa)]
        say_data.append(data)


    ans = 0
    for i in range(2 ** num):
        bin_data = list(map(int, ternary(i, 2, num)))
        kari_ans = bin_data.count(1)

        people_flg = [-1 for i in range(num)]
        dame_flg = 0
        for j in range(num):
            if people_flg[j] == -1:
                people_flg[j] = bin_data[j]
            else:
                if people_flg[j] != bin_data[j]:
                    dame_flg = 1

            if people_flg[j] == 0:
                continue

            for k in range(say_count[j]):
                people, flg = [say_dataj][k]
                people -= 1
                if people_flg[people] == -1:
                    people_flg[people] = flg
                else:
                    if people_flg[people] != flg:
                        dame_flg = 1

            if dame_flg:
                break
        if dame_flg:
            continue
        ans = max(ans, kari_ans)

    print(ans)






if __name__ == '__main__':
    main()