basic = list(map(int, input().split())) N = basic[0] items = [] for _ in range(basic[1]): items.append(list(map(int, input().split()))) for i in range(len(items)): if items[i][2]: items[items[i][2] - 1].append(items[i][:]) items[i] = [] dp = [0] * (N + 1) for itm in items: ...