my @order = sort {
$Costs[$a]+$Trims[$a]*$Costs[$b] <=> $Costs[$b]+$Trims[$b]*$Costs[
+$a]
} 0..$#Costs;
I'm pretty sure that this is the optimal solution, but I'm too lazy to write down the complete mathematical proof.
The basic idea is that the terms represent the general cost difference of swapping the first and the second filter¹!
So by sorting you get a solution which can't be improved by a pairwise swapping and any permutation can be represented by a sequence of pairwise swaps.
¹) see restructured formula for c in Re^2: Evolving a faster filter? (combinatorics) and you will notice that these are the first elements and the rest is fix.
UPDATE:
basic transformations lead to a simpler solution:
my @order = sort {
$Costs[$a]/(1-$Trims[$a]) <=> $Costs[$b]/(1-$Trims[$b])
} 0..$#Costs;
which can be precalculated to save calculation time
my @order = sort {
$weight[$a] <=> $weight[$b]
} 0..$#Costs;
(the edge case of a division by zero must be handled as practically infinity) |