Prateek loves candy

Coding Blocks IDE
Can someone tell me why it is giving TLE in some cases.
Could someone plz modify my code?