Please help with Snake Eating.(3 days spent ,no solution)

I have been trying the Snake Eating question for 3 days now.I’m still getting a TLE.Please help. I want to solve this question really bad.My latest solution getting TLE https://www.codechef.com/viewsolution/16575841

Probably ur second binary search fn was wrong, I updated it slightly to get AC

your solution:https://www.codechef.com/viewsolution/16582335

Sorry i used lower_bound,u could try it with ur fn too.