Getting TLE on ASSIGN (spoj), help!

I am getting TLE on ASSIGN and I don’t know how to optimize my code further. please, help!