$\huge \color{orange}{成仙之路->}$ $\huge \color{purple}{算法基础课题解}$
证明:
$\ \ \ \ \ |x_1-x|+|x_2-x|+…+|x_{n-1}-x|+|x_n-x|$
$=(|x_1-x|+|x_n-x|)+(|x_2-x|+|x_{n-1}-x|)+…$
$\geqslant (x_n-x_1)+(x_{n-1}-x_2)+…$
完整代码
#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
int n;
int a[N];
int main()
{
cin>>n;
for(int i=0;i<n;i++) cin>>a[i];
sort(a,a+n); //排序
int res=0;
for(int i=0;i<n;i++) res+=a[--n]-a[i];
cout<<res<<endl;
return 0;
}