This is basic Linear Programming. You should look at a solver for these types of problems. In the Microsoft world - the Solver Foundation will solve this. You can also play with the Excel solver to see it done interactively.
Lot's of open source solutions too. If you prefer to do the code yourself- and need a general solution, use array math - you will be solving a system of equations of inequalities. That way you can handle N constraints and Y variables.