Solutions for spoj's problems