赞
踩
n ~n~ n 个点,给你一个数组 d [ i ] ~d[i]~ d[i] ,表示第 i ~i~ i 个点到第一个点的最短距离。要求你加边,然后使得所有边权和最小,可以加负边。


#include <bits/stdc++.h> using namespace std; const int N = 100005; int t, n, d[N]; int main() { scanf("%d", &t); for (; t; --t){ scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &d[i]); sort(d + 1, d + n + 1); long long ans = 0, sum = 0; for (int i = 1; i <= n; ++i){ ans -= 1ll * d[i] * (i - 1) - sum; sum += d[i]; } ans += d[n]; printf("%lld\n", ans); } return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。