Problem p02683 (python)

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

# 持っているビスケットを叩き、1枚増やす
# ビスケット A枚を 1円に交換する
# 1円をビスケット B枚に交換する
def main():
    n, m, x = input_list()
    books = {}
    contents = {}
    for i in range(n):
        c = input_list()
        books[i] = c[0]
        contents[i] = c[1:m+1]

    if n == 1:
        if sum(contents[0]) >= x:
            print(books[0])
        else:
            print(-1)
        exit()

    ans = []
    for v in itertools.product([0, 1], repeat=n):
        understands = [0] * m
        ok = [False] * m
        a = 0
        for i, vi in enumerate(v):
            if vi == 0:
                continue
            for index, val in enumerate(contents[i]):
                understands[index] += val
                if understands[index] >= x:
                    ok[index] = True
            a += books[i]
        if all(ok):
            ans.append(a)

    if len(ans) > 0:
        print(min(ans))
    else:
        print(-1)
        # a = 0
        # for vi in v:
        #     for index, val in enumerate(contents[vi]):
        #         understands[index] += val
        #         if understands[index] >= x:
        #             ok[index] = True
        #     a += books[vi]
        # if all(ok):
        #     ans.append(a)


def input_list():
    return list(map(int, input().split()))


def input_list_str():
    return list(map(str, input().split()))


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import itertools


def main():
    n, m, x = input_list()

    costs = []
    contents = [[] for i in range(m)]
    contents_with_index = [[] for i in range(m)]
    for i in range(n):
        b = input_list()
        costs.append(b[0])
        for index, c in enumerate(b[1:]):
            contents[index].append(c)
            contents_with_index[index].append((str(i), c))

    for c in contents:
        if sum(c) < x:
            print(-1)
            exit()

    results = [[] for i in range(m)]
    for index, c in enumerate(contents_with_index):
        for i in range(1, m+1):
            for cv in itertools.combinations(c, i):
                s = sum([num[1] for num in cv])
                if s >= x:
                    results[index].append(','.join([num[0] for num in cv]))

    ans = set(results[0])
    for r in results:
        ans &= set(r)

    usable_cost = []
    for a in ans:
        usable_cost.append(sum(list(map(lambda x: costs[int(x)], a.split(',')))))

    print(min(usable_cost))


def input_list():
    return list(map(int, input().split()))


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import itertools


def main():
    n, m, x = input_list()

    costs = []
    contents = [[] for i in range(m)]
    contents_with_index = [[] for i in range(m)]
    for i in range(n):
        b = input_list()
        costs.append(b[0])
        for index, c in enumerate(b[1:]):
            contents[index].append(c)
            contents_with_index[index].append((str(i), c))

    for c in contents:
        if sum(c) < x:
            print(-1)
            exit()

    results = [[] for i in range(m)]
    for index, c in enumerate(contents_with_index):
        for i in range(1, m+1):
            for cv in itertools.combinations(c, i):
                s = sum([num[1] for num in cv])
                if s >= x:
                    results[index].append(','.join([num[0] for num in cv]))
    ans = set(results[0])
    for r in results:
        ans &= set(r)

    usable_cost = []
    for a in ans:
        usable_cost.append(sum(list(map(lambda x: costs[int(x)], a.split(',')))))

    print(min(usable_cost))


def has(v, l):
    for vl in l:
        if v == vl:
            return True
    return False

def input_list():
    return list(map(int, input().split()))


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import itertools


def main():
    n, m, x = input_list()

    costs = []
    contents = [[] for i in range(m)]
    contents_with_index = [[] for i in range(m)]
    for i in range(n):
        b = input_list()
        costs.append(b[0])
        for index, c in enumerate(b[1:]):
            contents[index].append(c)
            contents_with_index[index].append((i, c))

    for c in contents:
        if sum(c) < x:
            print(-1)
            exit()

    results = [[] for i in range(m)]
    for index, c in enumerate(contents_with_index):
        for i in range(2, m+1):
            for cv in itertools.combinations(c, i):
                s = sum([num[1] for num in cv])
                if s >= x:
                    results[index].append([num[0] for num in cv])
    ans = []
    for r in results[0]:
        is_continue = False
        for v in results:
            if has(r, v) is False:
                is_continue = True
                break
        if is_continue:
            continue

        ans.append(r)

    usable_cost = []
    for a in ans:
        usable_cost.append(sum(list(map(lambda x: costs[x], a))))

    print(min(usable_cost))


def has(v, l):
    for vl in l:
        if v == vl:
            return True
    return False

def input_list():
    return list(map(int, input().split()))


if __name__ == '__main__':
    main()