Prateek Loves Candy

Why this problem not submitted it will gave correct output.

This code is giving tle

your approach is not correct
you need to call the check function every time

see this code

Modified Code