Which number minimizes this sum?

Let's call the sum f(x).

If $x<s_1$ then all $s_i$ are greater than x, so f is the sum of n terms each of which has slope -b. So the slope of f in this region is -nb.

As x increases, it becomes greater than $s_1$. In the region $(s_1, s_2)$ f is the sum of 1 term with slope a and (n-1) terms with slope -b. So f has slope a - (n-1)b = a + b - nb.

And in general, if x lies between $s_i$ and $s_{i+1}$ then at this point f has slope ka - (n-k)b = k(a+b) - nb. Eventually x is greater than $s_n$, and the slope of f becomes na.

To minimise f you just need to find the value of k at which the slope of f changes from negative to positive (remember that a and b are both positive values).


Consider any interval $(s_k, s_{k+1})$ where $s_k < s_{k+1}$. For any $x$ in this interval, there are $k$ elements less than $x$, and $n-k$ elements greater than $x$. So long as we remain in this interval, increasing $x$ by some small amount $\Delta x$ increases the left-hand sum by $ak\Delta x$, but decreases the right-hand sum by $b(n-k)\Delta x$.

Keeping that principle in mind: If there exists an $k$ such that

$$ ak = b(n-k) $$

then the minimal values of $x$ are any of those within the interval $[s_k, s_{k+1}]$ (which may contain only one value, if $s_k = s_{k+1}$).

Otherwise, we find the unique positive $k$ such that

$$ a(k-1) < b(n-k+1) $$

but

$$ ak > b(n-k) $$

Such an $k$ is guaranteed to exist for positive integers $a$ and $b$ (why?), and then the minimum is reached at $x = s_k$.