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. Canvas游戏计算机图形教程

    TechbrooD   主站 WOW 登录   注册 0首页 1简介 1.1WWW 技术变迁和生态 1.2WWW 学习建议 1.3WWW 互联网基础知识 1.4WWW Web 1.5 WWW Web ...

  2. linux下安装php php-fpm(转载)

    centos安装php php-fpm 1.下载php源码包http://www.php.net/downloads.php2 .安装phptar -xvf php-5.5.13.tar.bz2cd ...

  3. python创建独立虚拟工作环境方法

    前言: python的组件非常之多,有时这个项目依赖m个组件,有时那个项目依赖n个组件,时间一长很容易导致系统python环境的臃肿不堪,由此便有了virtualenv.virtualenvwrapp ...

  4. 嵌入式的SQL程序设计

    嵌入式的SQL程序设计 sql语句大全之嵌入式SQL 2017-01-18 16:00 来源:未知   嵌入式SQL 为了更好的理解嵌入式SQL,本节利用一个具体例子来说明.嵌入式SQL允许程序连接数 ...

  5. laravel中的old()函数

    1.控制器 2.模板

  6. 如何处理好前后端分离的 API 问题(转载自知乎)

    9 个月前 API 都搞不好,还怎么当程序员?如果 API 设计只是后台的活,为什么还需要前端工程师. 作为一个程序员,我讨厌那些没有文档的库.我们就好像在操纵一个黑盒一样,预期不了它的正常行为是什么 ...

  7. sql删除重复记录

    DELETE E FROM t E where E.id> ( SELECT MIN(X.id) FROM t X WHERE X.name = E.name );

  8. 为什么const对象只能调用const成员函数,而不能调用非const成员函数?

    在c++中,我们可以用const来定义一个const对象,const对象是不可以调用类中的非const成员函数,这是为什么呢?下面是我总结的一些原理. 假设有一个类,名字为test代码如下: clas ...

  9. 流程控制 if while for 已及数字类型 进制转换

    一:if 语句 语法一:ifif 条件: code1 code1 code1 语法二:if ...else ... if 条件: code1 code1 code1else: code1 code1 ...

  10. #include 和 #import 的区别, @class 的含义

    #import 和 #include  会包含这个类的所有信息,包括实体变量和方法 而#include比起 #import的好处不会引起重复包含 @class是用来做类引用的 @class就是告诉编译 ...