TLE in TRAINSET ! How can i reduce time taken for this code?

https://www.codechef.com/viewsolution/33618904