4. The numbers $a_{1}, a_{2}, \ldots, a_{20}$ satisfy the conditions:

$$
\begin{aligned}
& a_{1} \geq a_{2} \geq \ldots \geq a_{20} \geq 0 \\
& a_{1}+a_{2}=20 \\
& a_{3}+a_{4}+\ldots+a_{20} \leq 20
\end{aligned}
$$

What is the maximum value of the expression:

$$
a_{1}^{2}+a_{2}^{2}+\ldots+a_{20}^{2}
$$

For which values of $a_{1}, a_{2}, \ldots, a_{20}$ is the maximum value achieved?
Solution. From the condition we have:

$$
a_{1}+a_{2}+a_{3}+a_{4}+\ldots+a_{20} \leq 40 \quad \text { and } \quad a_{1}=20-a_{2}
$$

then, we get:

$$
\begin{aligned}
a_{1}^{2}+a_{2}^{2}+\ldots+a_{20}^{2} & =\left(20-a_{2}\right)^{2}+a_{2}^{2}+\ldots+a_{20}^{2} \\
& =400-40 a_{2}+a_{2}^{2}+a_{2}^{3}+\ldots+a_{20}^{2}
\end{aligned}
$$

$$
\begin{aligned}
& \leq 400-\left(a_{1}+a_{2}+a_{3}+a_{4}+\ldots+a_{20}\right) a_{2}+a_{2}^{2}+\ldots+a_{20}^{2} \\
& =400-a_{1} a_{2}-a_{3} a_{2}-a_{4} a_{2}-\ldots-a_{20} a_{2}+a_{2}^{2}+\ldots+a_{20}^{2} \\
& =400+\left(a_{2}^{2}-a_{1} a_{2}\right)+\left(a_{3}^{2}-a_{3} a_{2}\right)+\ldots+\left(a_{20}^{2}-a_{20} a_{2}\right) \\
& =400+a_{2}\left(a_{2}-a_{1}\right)+a_{3}\left(a_{3}-a_{2}\right)+\ldots+a_{20}\left(a_{20}-a_{2}\right)
\end{aligned}
$$

From the condition we have:

$$
a_{2}-a_{1} \leq 0, \quad a_{3}-a_{2} \leq 0, \ldots, a_{20}-a_{2} \leq 0
$$

then:

$$
a_{1}^{2}+a_{2}^{2}+\ldots+a_{20}^{2} \leq 400
$$

and the equality holds if and only if

$$
\begin{aligned}
& a_{3}+a_{4}+\ldots+a_{20}=20, \text { and } \\
& a_{2}\left(a_{2}-a_{1}\right)=0, a_{3}\left(a_{3}-a_{2}\right)=0, \ldots, a_{20}\left(a_{20}-a_{2}\right)=0
\end{aligned}
$$

We consider two cases in which this equality is achieved. We have, $a_{2}=0$, from which it follows $a_{1}=20, a_{3}=a_{4}=\ldots=a_{20}=0$ or $a_{2}=a_{1}=10$, from which it follows $a_{3}=a_{4}=10, a_{5}=a_{6}=\ldots=a_{20}=0$. Moreover, we consider equivalent every case of permutation of the indices $3,4, \ldots, 20$.