SPOJ problem solution