Approach hintTry it first, then revealHide
Keep the median sheep in place and pack the others around it, so the j-th sheep moves to the median's position shifted by j - mid. Sum the distances with 64-bit arithmetic.
Keep the median sheep in place and pack the others around it, so the j-th sheep moves to the median's position shifted by j - mid. Sum the distances with 64-bit arithmetic.