Solution code for SPOJ from Python.