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
n, W = map(int, input().split()) items = [tuple(map(int, input().split())) for _ in range(n)] # Sort items by value per weight in descending order sorted_items = sorted(items, key=lambda x: -(x[0] / x[1])) remaining = W total = 0.0 for v, w in sorted_items: if remaining <= 0: break if w <= remaining: total += v remaining -= w else: total += (v / w) * remaining remaining = 0 # Format the output to remove trailing zeros and decimal point if not needed formatted_total = "{0:.8f}".format(total) if '.' in formatted_total: formatted_total = formatted_total.rstrip('0').rstrip('.') print(formatted_total)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.