in reply to Nesting 2

Sounds like a variation of the knapsack problem to me. Ask google about it and search CPAN for modules

P.S., where's the Perl in this problem?