Solved SPOJ question