Solution for SPOJ problems.