Solution:
Classic binary search problem. We only consider the positive part. The negative part can be similar.
Let's consider to push boxes to . We binary search with to find the largest so that .
If there is a box at . Which means . Then we plus one to the basic result (base_ans++
). No need for other things because push all boxes to here can't be the best solution.
If there is no box at . Which means . Then for all boxes between 0 and , we push it to . And then we binary search with the number of boxes between 0 and (Which is ). Find the smallest so that . So the result of pushing all boxes to is .
Enumerate . We pick the maximum of results.
Code

Tips:
Convert the negative part to the positive. And run the solution again. It can make the implementation easier.