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 def main(): data = list(map(int, sys.stdin.read().split())) n = data[0] W = data[1] items = [] index = 2 for _ in range(n): v = data[index] w = data[index + 1] ratio = v / w items.append((ratio, v, w)) index += 2 items.sort(reverse=True, key=lambda x: x[0]) total = 0.0 remaining = W for ratio, v, w in items: if remaining <= 0: break take = min(w, remaining) total += take * ratio remaining -= take print("{0:.8f}".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.