Educational Codeforces Round 34 (Rated for Div. 2) D - Almost Difference(高精度)

时间:2021-10-04 06:06:31

D. Almost Difference

Let's denote a function

Educational Codeforces Round 34 (Rated for Div. 2) D - Almost Difference(高精度)

You are given an array a consisting of n integers. You have to calculate the sum of d(ai, aj) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.

Input

The first line contains one integer n (1 ≤ n ≤ 200000) — the number of elements in a.

The second line contains n integers a1a2, ..., an (1 ≤ ai ≤ 109) — elements of the array.

Output

Print one integer — the sum of d(ai, aj) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.

Examples

input

5
1 2 3 1 3

output

4

input

4
6 6 5 5

output

0

input

4
6 6 4 4

output

-8

Note

In the first example:

  1. d(a1, a2) = 0;
  2. d(a1, a3) = 2;
  3. d(a1, a4) = 0;
  4. d(a1, a5) = 2;
  5. d(a2, a3) = 0;
  6. d(a2, a4) = 0;
  7. d(a2, a5) = 0;
  8. d(a3, a4) =  - 2;
  9. d(a3, a5) = 0;
  10. d(a4, a5) = 2.

哇,好不容易写到第四题,突然弹出消息说这题爆long long,然后就懵逼了,看了下状态AC的全是Python。赛后发现Hacker在疯狂Hack C++,血赚场?

怎么全世界都会long double,不过瞄到qls也被Hack了,窝q(小纠结.JPG)

#include <bits/stdc++.h>
using namespace std;
map<double,double>a;
int main()
{
int n;
scanf("%d",&n);
long double ans=;
for (int i=;i<n;i++)
{
double x;scanf("%lf",&x);
a[x]++;
ans= ans+ a[x+]-a[x-]+x*(i+-n+i);
}
cout << fixed << setprecision() << ans << endl;
return ;
}

q巨赛后补题代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=;
const ll BASE=1000000000000000000LL;
int a[MAXN];
int main()
{
int n;
scanf("%d",&n);
ll high=,low=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
low+=1LL*(*(i-)-(n-))*a[i];
while(low>=BASE)low-=BASE,high++;
while(low<)low+=BASE,high--;
}
map<int,int> mp;
for(int i=;i<=n;i++)
{
low-=mp[a[i]-],low+=mp[a[i]+];
while(low>=BASE)low-=BASE,high++;
while(low<)low+=BASE,high--;
mp[a[i]]++;
}
if(high>=- && high<=)printf("%lld\n",high*BASE+low);
else if(high>)printf("%lld%018lld\n",high,low);
else printf("%lld%018lld\n",high+(low>),(low>)*BASE-low);
return ;
}