getting TLE in rrstone

I am getting Time Limit Exceeded for my answer.Please do help.

http://www.codechef.com/viewsolution/3844699

simple answer is, you solution complexity is O(N*T) - too slow

how to reduce the time complexity in the above solution???