UVa 11039 - Building designing

11039 - Building designing

 * http://acm.uva.es/p/v110/11039.html

Summary
This is a simple problem that can be solved with a greedy algorithm.

Explanation
First, sort the red and the blue independently. Then try the two scenarios - with a red as the base, and blue as the base and alternatively puts the biggest block that can fit on top of the previous piece. Take the higher of the buildings.

Input
2 5 7 -2 6 9 -3 8 11 -9 2 5 18 17 -15 4

Output
2 5