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.
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
Edge cases
Take remaining/weight of it — the only fractional take, always the last.
Everything fits; answer is the total value.