链接:
https://codeforces.com/problemset/problem/1540/A
题意:
给长度为n的数组a,ai表示到达i的最短距离,从1到i的权值为正,往后走的权值为负数,问怎样构造权值最低。
本题要让正权值最少,就只要构造一条正值的路即可,然后想办法多构造负权值的路。
代码如下:
#include<iostream>
#include<vector>
#include<cmath>
#include<map>
#include<algorithm>
#include<string>
#include<string.h>
#include<random>
using namespace std;
typedef long long ll;
ll d[100003];
ll sum[100003];
int main() {
int T;
cin >> T;
while (T--) {
memset(sum, 0, sizeof(sum));
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> d[i];
}
sort(d + 1, d + 1 + n);
ll ans = 0;
for (int i = 1; i <= n; i++) {
sum[i] = sum[i - 1] + d[i];
ans += 1ll * i * d[i] - sum[i];
}
cout << d[n] - ans;
cout << endl;
}
return 0;
}