Solution to a top coder problem.