Suppose we have a set of $n$ positive integers, $s_1 \leq s_2 \leq s_3 \leq \cdots \leq s_n$. Given two positive integers $a, b$, which number $x$ minimizes the following sum?
$$\sum_{s_i < x} a(x - s_i) + \sum_{s_i > x} b(s_i -x)$$
I've only figured out that if $a=b$, then the answer is the median of the set. However, I can't figure out what happens if $a \neq b$.
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$.