Solution for Code Chef Problem 'TLG' (http://www.codechef.com/problems/TLG)