Example 4.2.5. Let $n \in \mathbb{N}$. Find the minimum value of the following expression
$$f(x)=|1+x|+|2+x|+\ldots+|n+x|, \quad(x \in \mathbb{R})$$
Solution. Denote $I_{1}=[-1,+\infty), I_{n+1}=(-\infty,-n]$ and $I_{k}=[-k,-k+1]$ for each $k \in\{2,3, \ldots, n\}$. If $x \in I_{1}$ then $f(x)=\sum_{i=1}^{n}(1+x) \geq \sum_{i=1}^{n}(i-1)=\frac{n(n-1)}{2}=f(-1)$. If $x \in I_{n}$ then $f(x)=\sum_{i=1}^{n}(-1-x) \geq \sum_{i=1}^{n}(-i+n)=\frac{n(n-1)}{2}=f(-n)$.
Suppose $x \in I_{k}$ with $1<k<n+1$, then
$$f(x)=-\sum_{i=1}^{k-1}(i+x)+\sum_{i=k}^{n}(i+x)$$
is a linear function of $x$, therefore
$$\min _{x \in I_{k}} f(x)=\min \{f(-k), f(-k+1)\}$$

This result, combined with the previous results, implies that
$$\min _{x \in \mathbb{R}} f(x)=\min \{f(-1), f(-2), \ldots, f(-n)\}$$

After a simple calculation, we have
$$f(-k)=(1+2+\ldots+(k-1))+(1+2+\ldots+(n-k))=\frac{1}{2}\left(k^{2}+(n-k)^{2}+n\right)$$
which implies that
$$\min _{x \in \mathbb{R}} f(x)=\min _{1 \leq k \leq n} \frac{k^{2}+(n-k)^{2}+n}{2}=\left\{\begin{array}{l}
m(m+1) \text { if } n=2 m(m \in \mathbb{N}) \\
(m+1)^{2} \text { if } n=2 m+1(m \in \mathbb{N})
\end{array}\right.$$