Solution for Google Code Jam