Fractional Knapsack
Items have value and weight; capacity is W. You may take fractions of items. Maximize total value.
Intuition
Because items are divisible, there's no packing subtlety: every kilogram of capacity should carry the densest value available. Sort by value/weight and pour items in, topping off with a fraction of the last one.
Fractions are what make greedy legal here. Because you may take part of an item, filling the bag with the best value-per-unit-weight first is provably optimal — there's never a reason to leave a denser item behind. The instant items become indivisible (0/1 knapsack), that argument collapses and you need DP. Spotting whether splitting is allowed decides the entire approach.
Approach
Sort by value density
value ÷ weight is the worth of one unit of capacity spent on that item.
Take greedily
Take whole items while they fit; when the next doesn't, take exactly the fraction that fills the sack and stop.
Why fractions make greedy exact
With divisibility, swapping any low-density mass for unused high-density mass strictly improves — an optimum must follow density order. (The 0/1 version breaks this and needs DP.)
Solution & live demo
Common pitfalls
Sorting by value instead of value-to-weight ratio
items.sort(key=lambda x: x[0], reverse=True)
items.sort(key=lambda x: x[0] / x[1], reverse=True)
A high-value item can be so heavy that it crowds out several lighter items worth more in total. What the bag is really spending is capacity, so the quantity to maximise is value per unit of weight.
Skipping an item that doesn't fit whole
if W >= w:
total += v; W -= w
else:
continueelse:
total += v * W / w
breakThis is the difference between the fractional problem and 0/1. Once an item doesn't fit entirely, you take the portion that does — and since the bag is then exactly full, no later item can be added, so you stop.
Integer division on the partial take
total += v * W // w
total += v * W / w
The whole point of the fractional variant is that the answer is generally not an integer. Floor division silently discards the remainder of the last, partially-taken item.
Edge cases
Take remaining/weight of it — the only fractional take, always the last.
Everything fits; answer is the total value.