Getting TLE in Box Of Chocolates(CHCBOX)

Problem link - CHCBOX Problem - CodeChef

solution - CodeChef: Practical coding for everyone

I tried using the same method provided in the editorial. Where am I wrong?