Need help in bookshelf problem

I have try to solve this zco problem but answer is displaying wrong.
I try to find a testcase where it doesnot work but i didnot get it.

please help me in finding in finding the error

link: https://www.codechef.com/viewsolution/83257539