Fractional knapsack

Bags of sand have different values. You can pour out any amount. Which portions give your knapsack the greatest value?

Divide each bag’s value by its weight. Take sand with the greatest value per unit of weight first.

If cheaper sand is packed while dearer sand remains, swapping equal weights increases the value.

Sorting n bags takes O(n log n). Packing takes O(n).