欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1540A.Great Graphs
Great Graphs
Farmer John has a farm that consists of 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 , where is the smallest amount of time it took the cows to reach the -th pasture from pasture 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 () — the number of test cases. Then cases follow.
The first line of each test case contains a single integer () — the number of pastures.
The second line of each test case contains space separated integers () — the array . It is guaranteed that .
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory.
Note
In the first test case, you can add roads
- from pasture to pasture with a time of ,
- from pasture to pasture with a time of ,
- from pasture to pasture with a time of ,
- from pasture to pasture with a time of ,
- from pasture to pasture with a time of .
The total cost is .
In the second test case, you can add a road from pasture to pasture with cost and a road from pasture to pasture with cost . The total cost is .
In the third test case, you can't add any roads. The total cost is .
Samples
3
3
0 2 3
2
0 1000000000
1
0
-3
0
0
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |