Solved SPOJ problem