Simplify the sum of differences Given $(x_k)_{1 \leq k \leq n}$ a family of real numbers, how can I compute the most efficiently the sum of the differences that is :
$$\sum\limits_{1\leq i < j \leq n} (x_j - x_i) $$
I obtained 
$$\sum\limits_{i=1}^{n-1} \sum\limits_{j=i+1}^{n} x_j +\sum\limits_{i=1}^{n-1}i x_i - n \sum\limits_{i=1}^{n-1}x_i$$
but I was wondering if there are any other more elegant solutions.
 A: $$\begin{align}
S=\sum_{1\le i<j\le n}(x_j-x_i)
&=\sum_{i=1}^{n-1}\sum_{j=i+1}^nx_j-x_i\\
&=\sum_{i=1}^{n-1}\sum_{j=i+1}^nx_j-\sum_{i=1}^{n-1}\sum_{j=i+1}^n x_i\\
&=\sum_{j=2}^n\sum_{i=1}^{j-1}x_j-\sum_{i=1}^n(n-i)x_i\\
&=\sum_{j=2}^n (j-1)x_j-\sum_{i=1}^n(n-i)x_i\\
&=\sum_{i=1}^n (i-1)x_i-\sum_{i=1}^n(n-i)x_i\\
&=\color{red}{\sum_{i=1}^n \big(2i-n-1\big)\; x_i}\end{align}$$
If $n$ is odd ($n=2m-1$), then
$$\begin{align}
S&=\sum_{i=1}^{2m-1}(2i-2m)\ x_i\\
&=\sum_{i=1}^{m-1}-2(\underbrace{m-i}_r)\ x_i+0\cdot x_m+\sum_{i=m+1}^{2m-1}2(\underbrace{i-m}_s)\ x_i\\
&=\sum_{r=1}^{m-1}-2r\ x_{m-r}+\sum_{s=1}^{m-1}2s\ x_{m+s}\\
&=\color{orange}{\sum_{k=1}^{m-1}2k \ \big(x_{m+k}-x_{m-k}\big)}
\end{align}$$
e.g. if $n=5$ ($m=3$), then $S=2(x_4-x_2)+4(x_5-x_1)$.
If $n$ is even ($n=2m$), then
$$\begin{align}
S&=\sum_{i=1}^{2m}(2i-2m-1)\ x_i\\
&=\sum_{i=1}^m(2i-2m-1)\ x_i+\sum_{i=m+1}^{2m} (2i-2m-1)\ x_i\\
&=\sum_{s=1}^m \overbrace{-(2s-1)\ x_{m+1-s}}^{s=m+1-i}
+\sum_{r=1}^m \overbrace{ (2r-1)\ x_{m+r}}^{r=i-m}\\
&=\color{orange}{\sum_{k=1}^m \big(2k-1\big)\ \big(\ x_{m+k}-x_{m+1-k}\ \big)}\end{align}$$
e.g. if $n=6$ ($m=3$), then $S=1\ (x_4-x_3)+3\ (x_5-x_2)+5\ (x_6-x_1)$.
