in reply to Nested Loops vs Good programming
Recursion is your friend. By understanding both scoped variables and how to package and pass your current values forward in the recursive call, you can have both your current virtually unlimited level, and all previously detected superior values at any moment in time.
Be aware that recursion is memory intensive, so this ability gives you extreme flexibility, at several costs. First, it becomes a memory hog for depth-wise structures which may leave you "out of memory". Second, you still have the reponsibility to eventually leave the recursion in an orderly fashion (usually by return at the end of each level).
It is not good practice to step out of recursion before unwrapping all the depth you have accumulated. It is better to pass out your constructed structure or build an external structure if you need values outside the recursion.