Sum of squares and linear sum 
For which positive integer $n$ can we write $n=a_1+a_2+\dots+a_k$ (for some unfixed $k$ and positive integers $a_1,a_2,\ldots,a_k$) such that $\sum_{i=1}^k a_i^2 = \sum_{i=1}^k a_i + 2\sum_{i<j}a_ia_j$?

When $k=1$, the equation is $a_1^2=a_1$, so $a_1=1$ and $n=1$ is the only possibility.
When $k=2$, we get $a_1^2+a_2^2 = a_1+a_2+2a_1a_2$, or $(a_1-a_2)^2 = a_1+a_2$, hence $n$ must be a perfect square. For perfect square $n=r^2$, we can solve $a_1+a_2=r^2$ and $a_1-a_2=r$, which must have a solution because $r^2\equiv r\pmod 2$. So all perfect squares work.
 A: Partial answer.
If we write $m = \sum_i a_i^2$, then the identity $$\left(\sum_i a_i\right)^2 = \sum_i a_i^2 + 2\sum_{i < j} a_i a_j$$ allows us to translate our condition to $$ 2m = n^2 + n.$$ We know that $m \equiv n^2\mod 2$, hence a necessary condition is $4\mid n^2 - n$, which means $n\equiv 0, 1\mod 4$.
Now we can try the first several possibilities, as below ("-" means there is no solution):
1: 1
4: 3, 1
5: -
8: -
9: 6, 3
12: -
13: 9, 3, 1
16: 10, 6
17: 12, 2, 2, 1
20: 14, 3, 2, 1
21: 15, 1, 1, 1, 1, 1, 1
24: 17, 2, 2, 1, 1, 1
25: 15, 10

I did these calculations manually, so there are possibly errors.
My feeling is that for sufficiently large $n\equiv 0, 1\mod 4$, it is always possible to find a solution. However it seems hard to prove.

EDIT:
With a simple computer program, I verified that for all $n < 200$ with $n \equiv 0, 1\mod 4$, only $5, 8, 12$ cannot be obtained. I also corrected some errors in the calculations above.
Here is a brief idea of a proof. Firstly, we find the maximum integer $x$ such that $x^2 < \frac{n^2 + n}2$. For large $n$, this $x$ is about $\frac 1{\sqrt 2}n \sim 0.7 n$.
We take either $x$ or $x - 1$ (in case $x^2$ is too close to $\frac{n^2 + n}2$) as our first term $a_1$. This leaves us with a remaining sum of about $0.3n$ and a remaining sum of squares of about $1.4n$, which is the size of the difference between $x^2$ and $(x + 1)^2$.
Thus the problem becomes to find integers who sum up to $0.3n$ while their squares sum up to $1.4n$. It suffices to search for small values of $a_i$, say with $a_i\in [1, 6]$.
If we denote by $c_i$ the number of $a_i$ that are equal to $i$, then it becomes a group of two linear equations in $6$ variables:
\begin{eqnarray}
c_1 + 2c_2 + 3c_3 + 4c_4 + 5c_5 + 6c_6 &=& n - a_1 (\sim 0.3n)\\
c_1 + 4c_2 + 9c_3 + 16c_4 + 25c_5 + 36c_6 &=& \frac{n^2 + n}2 - a_1^2 (\sim 1.4n)\\
\end{eqnarray}
It should be possible to show that this system has non-negative integer solutions for sufficiently large $n$.
A: Here we prove an auxiliary claim for WhatsUp’s answer.
Let $S$ be the set of all pairs $(a,b)$ of natural numbers such that a system
$$\cases{
c_1 + 2c_2 + 3c_3 + 4c_4 + 5c_5 + 6c_6=x\\ 
c_1 + 4c_2 + 9c_3 + 16c_4 + 25c_5 + 36c_6=y}$$
has a solution in non-negative integers.
The set $S$ can be constructed recursively as follows. Put $(0,0)\in S$ and if $(x,y)\in S$ then put to $S$ pairs $(x+1,y+1)$, $(x+2,y+4)$, $(x+3,y+9)$, $(x+4,y+16)$, $(x+5,y+25)$, and $(x+6,y+36)$. If $(x,y)\in S$ then $x\le y \le 6x$. Moreover, $y-x=4c_2 + 6c_3 + 12c_4 + 20c_5 + 30c_6$, so $x$ and $y$ have the same parity.
Black points are pairs $(x,y)\in S$ for $0\le x, y\le 255$. Red points are pairs $(x,y)\not\in S$ for $x\le y \le 6x$ and $x,y$ of the same parity.

We claim that if $16\le x\le y\le 5x$ and $x$ and $y$ have the same parity then $(x,y)\in S$. We shall prove the claim by indution with respect to $x$. For $16\le x\le 20$ the claim can be checked directly, see the graph below. Assume that the claim is already proved for $x\ge 20$ and  $x+1\le y\le 5(x+1)$. If $x+1\le y-20$ then $(x-4,y-25)\in S$ by the induction hypoteses and so $(x+1,y)=(x-4,y-25)+(5,25)\in S$ by the construction of $S$. If $x+1>y-20$ then $(x,y-1)\in S$ by the induction hypoteses and so $(x+1,y)=(x,y-1)+(1,1)\in S$ by the construction of $S$.
