Source: a generalization of problem #5 of the 2000 CMO.
Suppose $n\ge 2, A,B > 0$ and that the real numbers $a_1,\cdots, a_n$ satisfy $a_1\ge a_2\ge \cdots \ge a_{n} \ge 0, a_1 + a_2 \leq A$ and $a_3 + a_4+\cdots + a_n \leq B.$ Note that for any $A,B > 0$ such a sequence exists (we can just take $a_1 = A$ and the rest of the $a_i$'s to be zero). Find the maximum possible value of $a_1^2+\cdots + a_n^2$, or show that no maximum exists. If the maximum exists, find all possible sequences that achieve the maximum, or show that for some values of A and B there are uncountably many sequences that achieve the maximum.
As a rough upper bound, we have $a_1^2 + \cdots + a_n^2 \leq (a_1^2 +\cdots + a_n)^2 \leq (A+B)^2,$ so the supremum of $a_1^2+\cdots + a_n^2$ over all sequences satisfying the constraints is surely finite. If the set $\mathcal{A}$ of sequences $(a_i)$ satisfying the constraints forms a compact subset of $\mathbb{R}^n$, then since the function $f(a_1,\cdots, a_n) = a_1^2+\cdots + a_n^2$ is continuous on $\mathbb{R}^n$, it achieves a maximum on $\mathcal{A}$ by a generalization of the EVT for single variable functions. But this is actually indeed the case, since if $(x_k)$ converges to $(a_k)$ in $\mathcal{A}$ where $x_k=(x_{k1},x_{k2},\cdots, x_{kn})$, then $\lim\limits_{k\to\infty} x_{kj} =a_{j}$ for all $1\leq j\leq n$ and so by limit properties, we see that $a_1 = \lim\limits_{k\to\infty} x_{k1} \ge \lim\limits_{k\to\infty} x_{k2} = a_2$ and similarly we can show the rest of the $a_i$'s are decreasing. Also $a_1 + a_2\leq A $ and $a_3+a_4+\cdots + a_n\leq B$. Since $\mathcal{A}\subseteq [0,\max\{A,B\}]^n$ it is bounded, so by the Heine-Borel theorem, $\mathcal{A}$ is compact in $\mathbb{R}^n$ (under the Euclidean metric), as required.
We have $0\leq a_1 \leq A - a_2$ so $a_1^2 \leq (A-a_2)^2.$ So $a_1^2+\cdots + a_n^2 \leq A^2 - 2A a_2 + 2a_2^2 + a_3^2+\cdots + a_n^2.$ However, due to the generalization, unlike in the official solution to the original problem, we might not have $a_1 + \cdots + a_n \leq 2A (1),$ though this holds if $B\leq A.$ Note that the maximum is attained from above, but if $B < A$, then $a_1^2+\cdots + a_n^2 < A^2 - (a_1+\cdots + a_n)a_2 + 2a_2^2 +a_3^2 + \cdots + a_n^2$. Assume (1) holds and $B=A$ for now. Then $a_1^2+\cdots + a_n^2 \leq A^2 - (a_1+\cdots + a_n)a_2 + 2a_2^2 +a_3^2 + \cdots + a_n^2 = A^2 + (a_2-a_1)a_2+\cdots + (a_n-a_1)a_n \leq A^2,$ with equality if and only if $a_1 = A-a_2, a_1+\cdots + a_n = 2A$ and there exists some $i\ge 1$ so that $a_j = a_1$ for $1\leq j\leq i$ and $a_j = 0$ for $j > i$. Indeed, if the latter three conditions hold, we have equality, and if we have equality, then it is easy to see that the first two conditions hold. To see why the latter condition must hold, observe that if all the $a_i$'s are positive, then $(a_2-a_1)a_2+\cdots + (a_n-a_1)a_n = 0\Rightarrow a_i = a_1$ for all i. Otherwise, take the smallest $j$ so that $a_j = 0$ to be $i+1$. Note that for all larger indices $k > j, a_k=0$ and for all smaller indices $k < j, a_k > 0.$ Hence $(a_2-a_1)a_2+\cdots + (a_j-a_1)a_j + (a_{j+1} - a_1)a_{j+1}+\cdots + (a_n-a_1)a_n = 0\Rightarrow (a_2-a_1)a_2+\cdots + (a_j-a_1)a_j = 0\Rightarrow a_k = a_1$ for $1\leq k\leq j.$ So either $i=1$ and $a_1 = A$ while $a_i=0$ for $i>1$ or $i\ge 2$ and $a_1 = a_2 = A/2$. Now for $B=A$ we get $a_1 = a_2 =a_3=a4= A/2$ while the other $a_i$'s are 0.
Alternatively, I think it might be possible to use Lagrange multipliers, but I'd prefer a more elementary approach if there is one.