Codeforces Round #728 (Div. 2) C

题目来源 https://codeforces.com/contest/1541/problem/C

一.题目内容

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

二.解题思路

意思是需要构造一个道路图能通过所有草场,从一个草场到另一个草场都需成本,求最小成本

先将d[i]排序 保证最小成本则一定有大的d[i]指向所有小于该d[i]的成本 但最小d[i]需先向大的d[i]走才能指向小d[i] 最小走到最大的过程无论发生多少步都相当于 +a[n]-a[1];

三. 

#include<bits/stdc++.h>
using namespace std;
long long  a[100009];
int main()
{   long long  n,m,num,t,k;
    scanf("%lld",&m);
    while (m--)
    {  num=0;
        scanf("%lld",&n);
        for (int i = 1; i <=n ; ++i) {
            scanf("%lld",&a[i]);
        }sort(a+1,a+1+n);
        if (n<=2)printf("0\n");
        else {
                for (int i = 0; i <n/2 ; ++i) {
                    num+=(a[n-i]-a[i+1])*(n-2*i-1);
                }num-=(a[n]-a[1]);
            printf("%lld\n",-num);
        }
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值