版权声明:写得不好,随便转载,但请注明出处,感激不尽 https://blog.csdn.net/xyc1719/article/details/87903504
【简要题意】求序列中任意两个数的差之和。n<=5e5
【分析】排序扫一遍,over
【code】
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=5e5+1000;
int n;LL a[maxn],s,ans;
inline void read(LL &x){
x=0;LL fl=1;char tmp=getchar();
while(tmp<'0'||tmp>'9'){if(tmp=='-')fl=-fl;tmp=getchar();}
while(tmp>='0'&&tmp<='9') x=(x<<1)+(x<<3)+tmp-'0',tmp=getchar();
x=x*fl;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++) read(a[i]); sort(a+1,a+n+1);
for(int i=1;i<=n;i++) ans=ans+(i-1)*a[i]-s,s+=a[i];
cout<<(ans<<1)<<endl;
return 0;
}