题库练习 Great Graphs
← 上一题 下一题 →

A14272 | Great Graphs

时间限制1s
内存限制256MB
通过 / 提交0/0

题目描述

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 $d_i$ 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.

输入格式

The first line contains one integer $t$ ( $1 \leq t \leq 10^4$ ) — the number of test cases. Then $t$ cases follow.

The first line of each test case contains a single integer $n$ ( $1 \leq n \leq 10^5$ ) — the number of pastures.

The second line of each test case contains $n$ space separated integers $d_1, d_2, \ldots, d_n$ ( $0 \leq d_i \leq 10^9$ ) — the array $d$ . It is guaranteed that $d_1 = 0$ .

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$ .

输出格式

For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory.

输入输出样例

输入 #1
3
3
0 2 3
2
0 1000000000
1
0
输出 #1
-3
0
0
C++ 编辑器
输入
输出