My codes for problem on SPOJ