You are only looking at local minimum, instead of focussing on the global minimum.
Here is my explanation on another solution with an approach similar to yours. Hope it helps!
https://discuss.codechef.com/t/wa-in-the-problem-phcul/44226/4?u=shridharravi97