First solution to topcoder: 10329