7. Given $a_{1}, a_{2}, \cdots, a_{n}$ are pairwise distinct real numbers, find the minimum value of the function defined by $f(x)=\sum_{i=1}^{n}\left|x-a_{i}\right|$. Here, $x$ is a real number. (1969 Polish Mathematical Olympiad)
7. First, note that when $a < b$,
$$x - a + \left| x - b \right| = \left\{\begin{array}{l}
a + b - 2x, \quad x \leqslant a \\
-a + b, \quad a \leqslant x \leqslant b \\
2x - a - b, \quad x \geqslant b
\end{array}\right.$$

Therefore, at each point in the interval $[a, b]$, the sum $|x - a| + |x - b|$ reaches its minimum value. Based on this principle, solve the problem.

Without loss of generality, assume $a_{1}, a_{2}, \cdots, a_{n}$ form an increasing sequence $a_{1} < a_{2} < \cdots < a_{n}$.
When $n = 2m \left(m \in \mathbf{N}^{*}\right)$,
$$f(x) = \sum_{i=1}^{n} \left| x - a_{i} \right|$$

The right-hand side can be divided into $m$ groups, i.e.,
$$\begin{aligned}
f(x) = & \sum_{i=1}^{n} \left| x - a_{i} \right| = \sum_{i=1}^{m} \left( \left| x - a_{i} \right| + \left| x - a_{2m+1-i} \right| \right) = \\
& \sum_{i=1}^{m} \left( \left| x - a_{i} \right| + \left| x - a_{n+1-i} \right| \right)
\end{aligned}$$

The sum $y_{i} = \left| x - a_{i} \right| + \left| x - a_{n+1-i} \right| (i=1,2, \cdots, m)$ is constant on the interval $\left[ a_{i}, a_{n+1-i} \right]$, and this constant is its minimum value. Since each interval contains the next interval $\left[ a_{i+1}, a_{n+1-(i+1)} \right]$, all intervals have a common part, which is the interval $\left[ a_{m}, a_{m+1} \right]$:

On the interval $\left[ a_{m}, a_{m+1} \right]$, the sum $y_{i} = \left| x - a_{i} \right| + \left| x - a_{n+1-i} \right|$ achieves its minimum value at every point, and thus $f(x)$ achieves its minimum value at every point on the interval $\left[ a_{m}, a_{m+1} \right]$. To calculate this value, set $x = a_{m}$ or $x = a_{m+1}$ in equation (2). This value is equal to $-a_{1} - a_{2} - \cdots - a_{m} + a_{m+1} + \cdots + a_{n}$

When $n = 2m + 1 (m \in \mathbf{N})$, the right-hand side of equation (1) can be rewritten as
$$\begin{aligned}
f(x) = & \sum_{i=1}^{n} \left| x - a_{i} \right| = \sum_{i=1}^{m} \left( \left| x - a_{i} \right| + \left| x - a_{2m+2-i} \right| \right) = \\
& \sum_{i=1}^{m} \left( \left| x - a_{i} \right| + \left| x - a_{n+1-i} \right| \right) + \left| x - a_{m+1} \right|
\end{aligned}$$

Similar to the case when $n$ is even, it is not difficult to verify that when $x = a_{m+1}$, each $y_{i} = \left| x - a_{i} \right| + \left| x - a_{n+1-i} \right|$ reaches its minimum value. At this point, $\left| x - a_{m+1} \right| = 0$, so the last two terms on the right-hand side of equation (3) also reach their minimum value. Therefore, when $x = a_{m+1}$, $f(x)$ reaches its minimum value. According to equation (3), this minimum value is
$$\text{equal to } -a_{1} - a_{2} - \cdots - a_{m} + a_{m+2} + a_{m+3} + \cdots + a_{n}$$