I recently got stuck with the following problem:
Consider 18 integer numbers in segment $[10; 20]$. Find the maximum possible value of $$ \sum_{i=1}^{18} {a_i^2} $$ given that $$\sum_{i=1}^{18} {a_i}=237$$
I don't really know how to approach this problem. The thing is, solving it like constraint optimization problem I got the solution $a_1=a_2=...=a_n=\frac{237}{18} \approx13.2$
But we need to solve it in integers. And just taking numbers around $13.2$ doesn't make sense to me. Any ideas?
The idea is to suppose all of the $a_i$ are fixed except for two of them, and see what happens if you add $1$ to one and subtract $1$ from the other. So say for instance $a_1 = x$, $a_2 = y$, and all the other $a_i$ are fixed. Then adding $1$ to $x$ and subtracting $1$ from $y$ gives the same sum, but what happens to the sum of squares? $$(x+1)^2 + (y-1)^2 - (x^2 + y^2)$$ is the amount by which the sum of squares increases or decreases. But this is $$2x + 1 - 2y + 1 = 2(x-y+1).$$ So if $x \ge y$, this means the sum of squares will increase if we add $1$ to $x$ and subtract $1$ from $y$. Repeated application of this logic means that the sum of squares is actually maximized when $x$ is as large as possible and $y$ is as small as possible. Then we repeat this logic for the other terms in the sum; it follows that the maximum is attained when we can make as many of the $a_i$ as large as possible. Since the $a_i$ are restricted to $[10; 20]$ we want the largest $m$ such that $20 m + 10(18 - m) \le 237$, or $m = 5$. This means $5$ of the $a_i$ are equal to $20$, $12$ of them are $10$, and the last one makes up the remainder, which has value $17$, and the sum of squares is $3489$.
A slightly different way to look at the second part of the solution is to note that since each of the $a_i$ must be at least $10$, we can create an auxiliary sequence $b_1, b_2, \ldots, b_{18}$ with $b_i = a_i - 10$, hence $b_i \in [0; 10]$ and the constraint on them is $$\sum_{i=1}^{18} b_i = 57.$$ Then the same reasoning as before gives five $b_i$ equal to $10$, and one $b_i$ equals $7$.