As In the problem we have to minimize the F( X ) = \sum_{i = 0}^{n-1} |X+i∗L−Si|

So finally $F( X ) = \sum_{i = 0}^{n-1} |X-(Si-i∗L)|$ $ F(x) = |x-a_1|+|x-a_2|.......+ |x-a_{n}|$ such that $a<=x <= b-n*l$ and all $a_i $ are in sorted order Is it not worth to check only 3 critical points $x = a_{floor(n/2)} ,a,b-n*l$ for the minimum value of $F(x)$ ? but i'm getting WA here is the [link][1] ![image][2]The problem is that you are not sorting the vector s and assigning snakes position in the order in which they come.

You have to assign the position in the order of their current position.

3 Likes