Wrong answer in SPOJ

I implemented the logic with a few modifications. The solution is able to pass sample test cases but in the hidden test cases it is showing wrong answer. The code is https://ide.codingblocks.com/s/307385. Can someone please help ?
The problem link is https://www.spoj.com/problems/BAISED/