First solution to topcoder: 7321