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-08-19 14:35:05 rohith
i have 2 doubts what is the output for the inputs
1) 1
2) 1 1 1 2 2
Thanks in advance :)

Last edit: 2014-08-19 14:35:25
2014-07-30 12:39:36 Master_Card
Weird question...
same algo gives Ac after Wa
2014-07-19 22:56:35 nikhil_nihal
one of the best problems, i solved.
2014-07-08 06:28:49 Sahil Dua
The best problem in arrays, I have ever solved. Thought for half an hour, made an algo and AC in 1st go.
Take your copy & pen and assume a test case of atleast 4 elements and try to make up an O(n) solution!

Last edit: 2014-07-08 06:29:08
2014-07-03 21:32:03 Anubhav Balodhi
good problem on arrays, (C)carefully...
2014-07-01 13:24:27 Ranjan Kumar Singh
5 line code!!!!
2014-06-23 20:49:30 Archit Jain
easy just observe how they cancel out each other and pattern so formed
2014-06-18 18:13:24 Counter Strike 1.6
O(n) in java is giving TLE .... Don't know why ....
2014-06-17 20:47:47 L
enjoyed solving it!!!!!
0.10secs with complexity O(n)
2014-06-11 22:52:38 Puneet Gupta
Easy!
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.