Complted topcoder problem 12171