solution to spoj questions