Let $n \geqslant 3$ and $a_{1}, \ldots, a_{n}$ be real numbers such that $\sum a_{i}^{2}=1$. Determine the maximum value that

$$
\sum_{i=2}^{n+1}\left|a_{i}-a_{i-1}\right|
$$

can take.
Answer: $2 \sqrt{n}$ if $n$ is even and $2 \sqrt{n-1}$ if $n$ is odd.

We start with the case where $n$ is even. From the triangle inequality and the arithmetic-quadratic inequality:

$$
\sum_{i=2}^{n+1}\left|a_{i}-a_{i-1}\right| \leqslant 2 \sum\left|a_{i}\right| \leqslant 2 \sqrt{n} \sqrt{\sum a_{i}^{2}}=2 \sqrt{n}
$$

On the other hand, equality holds for $a_{2 k+1}=-\frac{1}{\sqrt{n}}$ and $a_{2 k}=\frac{1}{\sqrt{n}}$: we have $\sum_{i=2}^{n+1}\left|a_{i}-a_{i-1}\right|=n \frac{2}{\sqrt{n}}=2 \sqrt{n}$, and $\sum a_{i}^{2}=\frac{n}{n}=1$.

Now suppose $n$ is odd, we take the indices of the sequence $\bmod n$, which defines $a_{i}$ for all integers $i$. Let's prove that there exists $i$ such that $a_{i-1} \leqslant a_{i} \leqslant a_{i+1}$ or $a_{i-1} \geqslant a_{i} \geqslant a_{i+1}$. Suppose by contradiction that this is not the case, we always have $a_{i-1} \neq a_{i}$, and thus either $a_{i-1}>a_{i}$ (and in this case, $a_{i}a_{i+1}$). In particular, if $a_{1}a_{2 k+3}$, which poses a problem for the $k$ such that $n=2 k+1$, since $a_{2 k+2}>a_{2 k+3}$ becomes $a_{1}>a_{2}$, which is contradictory. If $a_{1}>a_{2}$, we show by induction that $a_{2 k+1}>a_{2 k+2}$ and $a_{2 k+2}<a_{2 k+3}$, which poses a problem for the $k$ such that $n=2 k+1$, since $a_{2 k+2}<a_{2 k+3}$ becomes $a_{1}<a_{2}$, which is contradictory.

We can then group the terms $\left|a_{i-1}-a_{i}\right|$ and $a_{i}-a_{i+1} \mid$ into $\left|a_{i-1}-a_{i+1}\right|$. We have then reduced to the same inequality as at the beginning, but for $n-1$. The previous case ensures that $\sum_{i=2}^{n+1} \mid a_{i-1}-$ $a_{i} \mid \leqslant 2 \sqrt{n-1}$. Conversely, we obtain equality by setting $a_{2 k-1}=-\frac{1}{\sqrt{n}}$ and $a_{2 k}=\frac{1}{\sqrt{n}}$, for $1 \leqslant k \leqslant \frac{n-1}{2}$, and $a_{n}=0$.