#K62142. Sum of Absolute Differences

    ID: 31466 Type: Default 1000ms 256MiB

Sum of Absolute Differences

Sum of Absolute Differences

You are given a sequence of N integers. Your task is to compute the sum of the absolute differences between every pair of integers in the sequence.

Formally, for a sequence \(A = [A_1, A_2, \dots, A_N]\), you need to compute: \[ F(A)=\sum_{1\le i < j \le N} |A_i-A_j| \]

Note: If the sequence consists of a single element, the result is 0.

inputFormat

The input is read from standard input (stdin) and has the following format:

T
N_1 A_{1,1} A_{1,2} ... A_{1,N_1}
N_2 A_{2,1} A_{2,2} ... A_{2,N_2}
... 
N_T A_{T,1} A_{T,2} ... A_{T,N_T}

where:

  • T is the number of test cases.
  • For each test case, the first integer N indicates the number of elements in the sequence, followed by N integers.

outputFormat

For each test case, output the computed value F(A) on a new line. The output is written to standard output (stdout).

## sample
3
3 1 3 2
2 2 4
4 -1 -2 -3 -4
4

2 10

</p>