import json from itertools import product costs = [3, 5] values = [4, 5] budget = 5 # 穷举每项选或不选的二进制向量,只对可行组合比较收益。 feasible = [] for choice in product((0, 1), repeat=len(costs)): cost = sum(flag * amount for flag, amount in zip(choice, costs)) if cost <= budget: value = sum(flag * amount for flag, amount in zip(choice, values)) feasible.append((value, cost, choice)) value, cost, choice = max(feasible, key=lambda row: row[0]) assert choice == (0, 1) and cost == budget and value == 5 result = {"choice": list(choice), "cost": cost, "value": value, "feasible_count": len(feasible)} print("LAB_RESULT " + json.dumps(result, ensure_ascii=False))