Roy Xue's Codejam solution :)