Given $n$ integers, compute $\sum_{(i,j)} |a_i - a_j|$ over all ordered pairs of positions (each unordered pair counted twice). Sort and use prefix sums.
Input
Line 1: $n$ ($1 \le n \le 3000$). Line 2: $n$ integers in $[-10^9, 10^9]$.
Output
Print the total sum of absolute differences over all ordered pairs.
Example 1
Input
4 1 3 5 7
Output
40
Discussion
Threads about this problem. Spoilers are collapsed; wrap hints in
||double bars||.No discussion yet. Start a thread if something here needs explaining.