Clear questions and runnable code get the best and fastest answer 

PerlMonks 
Re^3: Evolving a faster filter? (optimal solution?)by LanX (Abbot) 
on Jan 04, 2013 at 21:22 UTC ( #1011721=note: print w/ replies, xml )  Need Help?? 
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.
Cheers Rolf ¹) 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:
which can be precalculated to save calculation time
(the edge case of a division by zero must be handled as practically infinity)
In Section
Seekers of Perl Wisdom

