Variant of the knapsack problem and box stacking
This is a good dynamic programming exercise.  Here's a hint.


   Let $f(i)$ denote the maximum height that can be achieved if you have the $i$th box at the bottom.  Can you find a recursive relation to let you compute $f$ in a recursive fashion?


Think about that one.  If you get stuck, here's another hint:


   Oops, you'll probably discover that doesn't quite seem to work.  What additional information would we like to know about the box at the bottom, to enable you to find a recursive relation?  You might need to add an extra parameter to $f$, so it becomes $f(i,\text{something})$ -- what should you put as the "something"?


If you can answer those hints, you should be very close to finding a dynamic programming algorithm for the problem.