<s>[INST] How to prove this identity nicely? 
Show that$$\sum_{1\le i<j\le n}\left((x_j-x_i)-(x_j-x_i)^2\right)=\left(\sum_{i=1}^n{x_i}\right)^2-n\sum_{i=1}^n{x_i^2}-\sum_{i=1}^n{(n-2i+1)x_i}\\=-n\sum_{i=1}^n\left(x_i-\frac1n\sum_{j=1}^n{x_j}+\frac{n-2i+1}{2n}\right)^2+\frac1{4n}{\sum_{i=1}^n(n-2i+1)^2}.$$

I wonder how this identity comes up with the first and second recipes? Can anyone explain this in detail? I got this identity when I was reading the problem here. Thanks.
 [/INST] $\def\peq{\mathrel{\phantom{=}}{}}$For the first identity, because$$
\sum_{i < j} (x_j - x_i) = \sum_{k = 1}^n x_k \left( \sum_{l < k} 1 + \sum_{l > k} (-1) \right) = \sum_{k = 1}^n (2k - n - 1) x_k,
$$\begin{gather*}
\sum_{i < j} (x_j - x_i)^2 = \sum_{k = 1}^n x_k^2 \left( \sum_{l < k} 1 + \sum_{l > k} 1 \right) - 2 \sum_{i < j} x_i x_j\\
= (n - 1) \sum_{k = 1}^n x_k^2 - \left( \left( \sum_{k = 1}^n x_k \right)^2 - \sum_{k = 1}^n x_k^2 \right) = n \sum_{k = 1}^n x_k^2 - \left( \sum_{k = 1}^n x_k \right)^2,
\end{gather*}
then$$
\sum_{i < j} ((x_j - x_i) - (x_j - x_i)^2) = \left( \sum_{k = 1}^n x_k \right)^2 - n \sum_{k = 1}^n x_k^2 - \sum_{k = 1}^n (n - 2k + 1) x_k.
$$
For the second identity, denoting $\bar{x} = \dfrac{1}{n} \sum\limits_{k = 1}^n x_k$,\begin{align*}
&\peq -n \sum_{k = 1}^n \left( x_k - \bar{x} + \frac{1}{2n} (n - 2k + 1) \right)^2 + \frac{1}{4n} \sum_{k = 1}^n (n - 2k + 1)^2\\
&= -\sum_{k = 1}^n \left( n(x_k - \bar{x})^2 + (n - 2k + 1)(x_k - \bar{x}) + \frac{1}{4n} (n - 2k + 1)^2 \right) + \frac{1}{4n} \sum_{k = 1}^n (n - 2k + 1)^2\\
&= -\sum_{k = 1}^n \left( n(x_k - \bar{x})^2 + (n - 2k + 1)(x_k - \bar{x}) \right)\\
&= -n \sum_{k = 1}^n (x_k - \bar{x})^2 - \sum_{k = 1}^n (n - 2k + 1) x_k + \bar{x} \sum_{k = 1}^n (n - 2k + 1)\\
&= -n \left( \sum_{k = 1}^n x_k^2 - n\bar{x}^2 \right) - \sum_{k = 1}^n (n - 2k + 1) x_k + 0\\
&= \left( \sum_{k = 1}^n x_k \right)^2 - n \sum_{k = 1}^n x_k^2 - \sum_{k = 1}^n (n - 2k + 1) x_k.
\end{align*}
</s>