Hey guys, can someone please help me figure out why I am getting TLE in Challenge problem from this long?

Link: https://www.codechef.com/viewsolution/19245152

I really dont know what can be the problem, and I did try using all sorts of “flush” and what not . Any help is appreciated, thanks