TLE Error on larger cases

Kindly help me solve the TLE Error with a more optimized solution. I am getting the right answer for smaller test cases.

Optimized Code