思路:
每个点对答案的贡献= a[i]*(i-1)-sum[i-1]-cnt[a[i]-1]+cnt[a[i]+1]
注意爆long long
#include<bits/stdc++.h>
#define PI acos(-1.0)
using namespace std;
typedef long long ll;
const int MAX_N=2e5+5;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;
int a[MAX_N];
ll sum[MAX_N];
map<int,int> mp;
int main(void){
int n;
cin >> n;
memset(sum,0,sizeof sum);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<=n;i++) sum[i]=sum[i-1]+1LL*a[i];
long double ans=0.0;
mp[a[1]]++;
for(int i=2;i<=n;i++){
long double res=(long double)(i-1)*a[i]-sum[i-1];
res-=mp[a[i]-1];
res+=mp[a[i]+1];
mp[a[i]]++;
ans+=res;
// cout << res << endl;
}
printf("%.0Lf\n",ans+0.1);
return 0;
}