Perfect subarray(google kick start round c)

i have used the optimal solution as mentioned in editorial.
but my solution only passed the test set 1.
how to optimise it?
i have used python

question link:

link to my solution:

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

thanks in advance