D. Almost Difference

Let's denote a function

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 ;
}

Educational Codeforces Round 34 (Rated for Div. 2) D - Almost Difference(高精度)的更多相关文章

  1. Educational Codeforces Round 34 (Rated for Div. 2) A B C D

    Educational Codeforces Round 34 (Rated for Div. 2) A Hungry Student Problem 题目链接: http://codeforces. ...

  2. Educational Codeforces Round 34 (Rated for Div. 2) C. Boxes Packing

    C. Boxes Packing time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  3. Educational Codeforces Round 34 (Rated for Div. 2)

    A. Hungry Student Problem time limit per test 1 second memory limit per test 256 megabytes input sta ...

  4. Educational Codeforces Round 34 (Rated for Div. 2) B题【打怪模拟】

    B. The Modcrab Vova is again playing some computer game, now an RPG. In the game Vova's character re ...

  5. Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块

    Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块 [Problem Description] ​ ...

  6. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  7. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  8. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  9. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

随机推荐

  1. linux 动态库 静态库 函数覆盖

    本文讨论了linux动态库  静态库中函数的覆盖问题. 测试目的: 同名函数,分别打成动态库libdync_lib.so与静态库libstatic_lib.a,并把libstatic_lib.a打到另 ...

  2. linux中如何检测设备驱动模块是否存在

    linux系统中的设备驱动是否安装好一般检查几个方面:1.系统日志.嵌入式系统多是直接dmesg一下,看有没有设备关键字相关的出错信息(通用系统可检查/var/log/messages文件).2.已加 ...

  3. 一款APP的交互文档从撰写到交付

    我第一份工作的设计总监是前百度设计师,34岁,一线设计12年:今年聊天说转了产品总监,如今39岁还活跃在行业中…… 我第二份工作的部门总监是前腾讯工程师,38岁,一线开发14年:2年前在Q群里跟我们说 ...

  4. csv中文乱码

    处理办法:https://jingyan.baidu.com/album/3c48dd3464b46ce10be3581f.html?picindex=2

  5. Laravel Tinker 使用笔记

    我们知道,Laravel Tinker 提供了命令行式的交互调试途径.使用极其方便直观. 使用: #php artisan tinker 要点: 命令要在一行上输入完成,回车执行.>>&g ...

  6. Vue.js 与 Laravel 分离

    首先表示折腾了十来天的php-laravel框架和vue的结合开发又偏前端实在是太大的阻碍,首先laravel的机制就是写完路由router再加载blade模板的.如果要在laravel工程里面加载一 ...

  7. HTTP 500.21 处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”

    问题:处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler” IIS7下部署网站出现错误,原因是先安装的II ...

  8. 2018.07.24 loj#107. 维护全序集(非旋treap)

    传送门 就是普通平衡树,可以拿来练非旋treap" role="presentation" style="position: relative;"&g ...

  9. UVa 12174 Shuffle (滑动窗口)

    题意:你正在使用的音乐播放器有一个所谓的乱序播放功能,即随机打乱歌曲的播放顺序.假设一共有s首歌, 则一开始会给这s首歌随机排序,全部播放完毕后再重新随机排序.继续播放,依次类推.注意,当s首歌播放完 ...

  10. python 实现排列组合

    1.python语言简单.方便,其内部可以快速实现排列组合算法,下面做简单介绍. 2.一个列表数据任意组合 2.1主要是利用自带的库 #_*_ coding:utf-8 _*_ #__author__ ...