ABSP1 - abs(a-b) I

Recently Mr. Kopa Samsu is learning programming. On a very renowned online judge, he found a problem:

You are given an array of N numbers in non-decreasing order. You have to answer the summation of the absolute difference of all distinct pairs in the given array.

Do you know what distinct pair means? Suppose you have an array of 3 elements: 3 5 6

Then all the distinct pairs are:

3 5
3 6
5 6

For this problem, Mr. Kopa Samsu implemented an algorithm to find the summation of the absolute difference of all distinct pairs. His algorithm was:

int ABS(int a[], int n)
{
    int sum = 0;
    for (int i = 1; i <= n ;i++)
    {
        for (int j = i + 1; j <= n; j++)
        {
            sum += abs(a[i] - a[j]);
        }
    }
}

After a great hard work, he finished the code. But alas!!! Frustration came around him when he submitted his code, the judge gave the verdict “TLE” (Time Limit Exceeded). “How can I get rid of TLE?” he thought a lot but couldn't find any way. Then suddenly, he remembered about you that you (his friend) is very good at programming. So, he came to you seeking your help. Can you help him solving this problem?

Input

The input data set starts with an integer T (T <= 1000), the number of test cases. Then every test case starts with a integer N (N <= 10000), the number of elements in the array. After that, the next line contains N integers A[i], where 1 <= i <= N and 1 <= A[i] <= 1000000000 & A[i] <= A[i+1].

Output

Every test case should output an integer “X”, where X is the summation of the absolute difference of all the distinct pair.

Example

Input:
3
3
1 2 3
3
1 4 5
3
2 4 6

Output:
4
8
8

Problem Set: S.M. Shaheen Sha, Raihat Zaman Neloy

Data Set & Solution: Raihat Zamane Neloy


Added by:Raihat Zaman Neloy
Date:2014-01-25
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64

hide comments
2014-02-13 21:18:49 The Alchemist
nyc prob on arrays <3
2014-02-10 18:13:07 Mukund Kumar
Nice problem...AC in one go.. :)
2014-02-07 19:24:16 anurag garg
use long long int
costed me 2 WA
2014-02-02 12:25:47 ritz
it is a Non-decreasing order.. its obvious from the problem statement..
2014-02-01 23:29:26 candide
@Ashwini if the input is 1 1 2 2 3 3 the expected answer is 16. Duplicates don't break the rule.
2014-01-31 13:38:13 Ashwini
what should all the pairs be for case like
1 1 2 2 3 3
2014-01-31 04:04:20 Hector Monteo
@surendra

duplicate items should be taken into account.

Last edit: 2014-01-31 04:36:56
2014-01-31 02:36:13 ­Surendra
Hi Héctor Montero,
What about duplicate elements ?
2014-01-31 01:32:12 Hector Monteo
@Surendra

they ask you to do is this:

for n = 3
1 2 3

Pairs:
1 2
1 3

2 3

sum = abs(2 - 1) + abs(3 - 1) + abs(3 - 2) = 4.

Last edit: 2014-01-31 02:36:36
2014-01-31 00:44:20 ­Surendra
WA.... !!

Can someone please explain the problem well ?
The problem statement is very confusing.

Now a days, New problem setters are wasting the time of problem-solvers.

Last edit: 2014-01-31 00:48:29
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.