CF1540A Great Graphs- Codeforces Round #728 (Div. 1)

原文链接Problem - 1540A - Codeforces

A. Great Graphs

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Farmer John has a farm that consists of nn 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 dd, where didi is the smallest amount of time it took the cows to reach the ii-th pasture from pasture 11 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 tt (1≤t≤1041≤t≤104) — the number of test cases. Then tt cases follow.

The first line of each test case contains a single integer nn (1≤n≤1051≤n≤105) — the number of pastures.

The second line of each test case contains nn space separated integers d1,d2,…,dnd1,d2,…,dn (0≤di≤1090≤di≤109) — the array dd. It is guaranteed that d1=0d1=0.

It is guaranteed that the sum of nn over all test cases does not exceed 105105.

Output

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

Example

input

Copy

3
3
0 2 3
2
0 1000000000
1
0

output

Copy

-3
0
0

Note

In the first test case, you can add roads

  • from pasture 11 to pasture 22 with a time of 22,
  • from pasture 22 to pasture 33 with a time of 11,
  • from pasture 33 to pasture 11 with a time of −3−3,
  • from pasture 33 to pasture 22 with a time of −1−1,
  • from pasture 22 to pasture 11 with a time of −2−2.

The total cost is 2+1+−3+−1+−2=−32+1+−3+−1+−2=−3.

In the second test case, you can add a road from pasture 11 to pasture 22 with cost 10000000001000000000 and a road from pasture 22 to pasture 11 with cost −1000000000−1000000000. The total cost is 1000000000+−1000000000=01000000000+−1000000000=0.

In the third test case, you can't add any roads. The total cost is 00.

--------------------------------------------------------------------------------------------------------------------------------

首先题目说了,是有向边,并且要求1到每个点的最短路径是给出的,不能比这大,也不能比这小。必须正好满足这一条件才行。可以存在负权边,是反向的。

我们把到1号节点的点排个序,每两个节点之间建一条边,就可以保证最短路径满足题意。

# include<iostream>
# include<algorithm>
# include<iomanip>

using namespace std;
typedef long long int ll;


ll a[100000+10];

int main()
{

    int t;
    cin>>t;

    while(t--)
    {
        ll  n;
        cin>>n;
        ll sum=0;

        for(ll i=1; i<=n; i++)
        {
            cin>>a[i];

        }

        sort(a+1,a+1+n);

        for(ll i=1; i<=n; i++)
        {
            sum+=a[i]-a[i-1];
            sum-=((a[i]-a[i-1])*(ll)(n-i+1)*(ll)(i-1));

        }

        cout<<sum<<'\n';


    }


    return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秦三码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值