solution to spoj problems