You might see Geometric Optimisation and Perl for a similar problem (though stated more generically than yours). And I want to say this is a kind of "knapsack problem" if you want to do more googling, but I vaguely remember doing that before and getting corrected that it's more of a "fooblesquznitz problem" (FSVO fooblesquznitz which I can't remember at the moment).