You have $N$ items that you want to put them into a knapsack of capacity $W$. Item $i$…
- User
You have items that you want to put them into a knapsack of capacity . Item () has weight and value for the weight.
Assistant
pythonimport sys def main(): n, w = map(int, sys.stdin.readline().split()) items = [] for _ in range(n): v_i, w_i = map(int, sys.stdin.readline().split()) if w_i == 0: ratio = float('inf') else: ratio = v_i / w_i items.append((-ratio, v_i, w_i)) # Negative for ascending sort items.sort() remaining = w total = 0.0 for ratio, v, weight in items: if remaining <= 0: break ratio = -ratio take = min(weight, remaining) total += v * (take / weight) remaining -= take print("{0:.10g}".format(total)) if __name__ == "__main__": main()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.