Let $n$ be a positive integer and let $k_{1}, k_{2}, k_{3}, \ldots, k_{n}$ be any permutation of the integers from 1 to $n$. What is the maximum value of the $n$-term expression

$$
\left(1-k_{1}\right)^{2}+\left(2-k_{2}\right)^{2}+\left(3-k_{3}\right)^{2}+\ldots+\left(n-k_{n}\right)^{2}
$$
Solution. Expanding and rearranging the parentheses:

$$
\sum_{i=1}^{n}\left(i-k_{i}\right)^{2}=\sum_{i=1}^{n} i^{2}+k_{i}^{2}-2 i k_{i}=2 \cdot \sum_{i=1}^{n} i^{2}-2 \sum_{i=1}^{n} i \cdot k_{i}
$$

The value of the expression is maximized when the

$$
K=\sum_{i=1}^{n} i \cdot k_{i}
$$

sum is minimized. First, we show that this follows when $k_{i}=n+1-i$. For this, consider an arbitrary sequence $k_{1}, k_{2}, k_{3}, \ldots, k_{n}$, and assume that for some $i<j$, $k_{i}<k_{j}$. Swap $k_{i}$ and $k_{j}$, i.e., let $k_{i}^{\prime}=k_{j}$ and $k_{j}^{\prime}=k_{i}$; the order of the other elements remains unchanged. Let the sum obtained by the swap be denoted by $K^{\prime}$; then

$$
\begin{aligned}
K^{\prime}-K & =\left(i \cdot k_{i}^{\prime}+j \cdot k_{j}^{\prime}\right)-\left(i \cdot k_{i}+j \cdot k_{j}\right)=\left(i \cdot k_{j}+j \cdot k_{i}\right)-\left(i \cdot k_{i}+j \cdot k_{j}\right)= \\
& =(i-j)\left(k_{j}-k_{i}\right)<0
\end{aligned}
$$

thus $K^{\prime}<K$. By a series of such swaps, we can reach from any order to the one where the numbers are in decreasing order, and the value of the sum corresponding to this is smaller than the initial value.

It remains to calculate the minimal value of this sum, and the maximal value of the original expression:

$$
\begin{aligned}
& 2 \cdot \sum_{i=1}^{n} i^{2}-2 \sum_{i=1}^{n} i \cdot(n+1-i)=4 \cdot \sum_{i=1}^{n} i^{2}-(2 n+2) \sum_{i=1}^{n} i= \\
= & 4 \cdot \frac{n(n+1)(2 n+1)}{6}-(2 n+2) \cdot \frac{n(n+1)}{2}=\frac{n(n+1)(n-1)}{3} .
\end{aligned}
$$