Codeforces Round #728 (Div. 2)C. Great Graphs
这题说给一个数组, 任意两个数字都可以建一个路, 求路的负数最大是多少.
这题就画一画, 比如 0 2 5 7 10, 那么可以建立任何两个数字之间的路, 和是0. 那么5->0, 7->(0,2,), 10->(0,2,5), 都是答案要的负数路. 很简单的模式.
#include "bits/stdc++.h"
using namespace std;
const auto fr = [](){
std::ios_base::sync_with_stdio(0); std::cin.tie(0);
std::cout << std::fixed << std::setprecision(12);
return 1;
}();
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v);
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.first << ", " << p.second << ")"; };
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) {
cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";
}
template<typename A, typename B> istream& operator>>(istream& cin, pair<A, B> &p) {
cin >> p.first;
return cin >> p.second;
}
// vars:
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using vi = std::vector<int>;
using vl = std::vector<ll>;
using vvi = std::vector<vi>;
using vvl = std::vector<vl>;
using pii = std::pair<int,int>;
using pil = std::pair<int,ll>;
using pli = std::pair<ll,int>;
using pll = std::pair<ll,ll>;
using vpii = std::vector<pii>;
using vvpii = std::vector<vpii>;
int main() {
fr;
int T;
cin >> T;
while (T --)
{
int N;
cin >> N;
ll A[N + 1]={};
for (int i = 1; i <= N; i++)
{
cin >> A[i];
}
sort(A+1, A+N+1);
if(N < 3)
{
cout << 0 << endl;
continue;
}
ll res = 0;
ll pre[N + 2]={};
for (int i = 1; i <= N; i++)
{
pre[i + 1] = pre[i] + A[i];
}
for (int i = 3; i <= N; i++)
{
res -= ((i - 2) * A[i] - pre[i - 1] );
}
cout << res << endl;
}
return 0;
}