Need some optimization LEAFEAT iarcs judge

my code is TLE’ing for a few cases and I am unable to optimize it further…

any suggestions?

My Code

You can check the solution here -

um… ive done the very same thing, the problem is pretty easy, i just dont know why its not running under the time constraints…