#include #include #include using namespace std; bool cmp1(int a,int b)//int为数组数据类型 { return a>b;//降序排列 //return a 0; test--){ int sum = 0; int i, n; scanf("%d", &n); for(i = 0; i < n; i++){ scanf("%d", &a[i]); } sort(a,a+n, cmp1); long long val = 0, tmp = 0; for(i = 1; i < n; i++){ tmp += a[0]-a[i]; } val += tmp; // printf("i=%d tmp=%lld val=%lld\n", 0, tmp, val); for(i = 1; i < n; i++){ long long d = a[i-1] - a[i]; tmp -= (n-i)*d; val += tmp; // printf("i=%d tmp=%lld val=%lld\n", i, tmp, val); } printf("%lld\n", val); } }