forked from sureshmangs/Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1541C - Great Graphs.cpp
86 lines (59 loc) · 2.55 KB
/
1541C - Great Graphs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
Farmer John has a farm that consists of n pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.
Unfortunately, Farmer John lost the map of the farm. All he remembers is an array d, where di is the smallest amount of time it took the cows to reach the i-th pasture from pasture 1 using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory.
Input
The first line contains one integer t (1=t=104) — the number of test cases. Then t cases follow.
The first line of each test case contains a single integer n (1=n=105) — the number of pastures.
The second line of each test case contains n space separated integers d1,d2,…,dn (0=di=109) — the array d. It is guaranteed that d1=0.
It is guaranteed that the sum of n over all test cases does not exceed 105.
Output
For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory.
Example
inputCopy
3
3
0 2 3
2
0 1000000000
1
0
outputCopy
-3
0
0
Note
In the first test case, you can add roads
from pasture 1 to pasture 2 with a time of 2,
from pasture 2 to pasture 3 with a time of 1,
from pasture 3 to pasture 1 with a time of -3,
from pasture 3 to pasture 2 with a time of -1,
from pasture 2 to pasture 1 with a time of -2.
The total cost is 2+1+-3+-1+-2=-3.
In the second test case, you can add a road from pasture 1 to pasture 2 with cost 1000000000 and a road from pasture 2 to pasture 1 with cost -1000000000. The total cost is 1000000000+-1000000000=0.
In the third test case, you can't add any roads. The total cost is 0.
*/
#include<bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
vector <long long> v(n);
for (auto &x: v) cin >> x;
sort(v.begin(), v.end());
long long sum = v[n - 1];
vector <long long> neg(n);
neg[0] = 0;
for (int i = 1; i < n; i++) {
neg[i] = neg[i - 1] + i * (v[i] - v[i - 1]);
sum -= neg[i];
}
cout << sum << "\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
cin >> t;
while (t--) solve();
return 0;
}