First solution. Notice that we have
(a1−n)2+⋯+(an−n)2=(a12+⋯+an2)−2n(a1+⋯+an)+n3≤n3+1−2n3+n3=1.
Therefore, there are two cases:
The first case is when a1=a2=⋯=an=n. This is a solution since it satisfies both inequalities.
The second case is when there exists 1≤i0≤n such that ∣ai0−n∣=1 and ai=n for all 1≤i≤n with i=i0.
If ai0=n−1, the first inequality becomes n2−1=a1+⋯+an≥n2 which is impossible.
If ai0=n+1, the second inequality becomes n3+2n+1=a12+⋯+an2≤n3+1 which is also impossible.
Hence, the only solution to both inequalities is given by a1=a2=⋯=an=n.
Second solution. We have by Cauchy-Schwartz inequality
(n2+1)2>n4+n≥n(a12+⋯+an2)≥(a1+⋯+an)2≥n4.
We deduce that
a1+⋯+an=n2
and
n3+1≥a12+⋯+an2≥n3
and the right hand side equality occurs if and only if a1=⋯=an=n.
Now, assume, looking for a contradiction, that a12+⋯+an2=n3+1. Since a number and all his powers have the same parity, we deduce that
n≡n2≡a1+⋯+an≡a12+⋯+an2≡n3+1≡n+1mod2
which is impossible.
Hence, the only solution to both inequalities is given by a1=a2=⋯=an=n.