Problem list
Log in
#r03225 · Online Judge > GoldOJ

Sum of Pairwise Distances

Silver V 2.0 s 1024 MB

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
Source
Online Judge > GoldOJ
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code