from fractions import Fraction candidates = [] for y in range(7): x = min((14 - y) // 2, 18 - 3 * y) if x >= 0: assert 2 * x + y <= 14 and x + 3 * y <= 18 candidates.append((5 * x + 7 * y, x, y)) profit, x, y = max(candidates) assert Fraction(profit) <= Fraction(274, 5) result = {"x": x, "y": y, "profit": profit} import json print(json.dumps(result, ensure_ascii=False, sort_keys=True))