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
import sys n, W = map(int, sys.stdin.readline().split()) items = [] for _ in range(n): v, w = map(int, sys.stdin.readline().split()) items.append((v, w)) items.sort(key=lambda x: x[0] / x[1], reverse=True) total = 0.0 remaining = W for v, w in items: if remaining <= 0: break if w <= remaining: total += v remaining -= w else: total += (v / w) * remaining remaining = 0 print("{0:.10g}".format(total))
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.