C. Do you want a date?
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Leha decided to move to a quiet town Vičkopolis, because he was tired by living in Bankopolis. Upon arrival he immediately began to expand his network of hacked computers. During the week Leha managed to get access to n computers throughout the town. Incidentally all the computers, which were hacked by Leha, lie on the same straight line, due to the reason that there is the only one straight street in Vičkopolis.

Let's denote the coordinate system on this street. Besides let's number all the hacked computers with integers from 1 to n. So the i-th hacked computer is located at the point xi. Moreover the coordinates of all computers are distinct.

Leha is determined to have a little rest after a hard week. Therefore he is going to invite his friend Noora to a restaurant. However the girl agrees to go on a date with the only one condition: Leha have to solve a simple task.

Leha should calculate a sum of F(a) for all a, where a is a non-empty subset of the set, that consists of all hacked computers. Formally, let's denote A the set of all integers from 1 to n. Noora asks the hacker to find value of the expression . Here F(a) is calculated as the maximum among the distances between all pairs of computers from the set a. Formally, . Since the required sum can be quite large Noora asks to find it modulo 109 + 7.

Though, Leha is too tired. Consequently he is not able to solve this task. Help the hacker to attend a date.

Input

The first line contains one integer n (1 ≤ n ≤ 3·105) denoting the number of hacked computers.

The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 109) denoting the coordinates of hacked computers. It is guaranteed that all xi are distinct.

Output

Print a single integer — the required sum modulo 109 + 7.

Examples
Input
2
4 7
Output
3
Input
3
4 3 1
Output
9
Note

There are three non-empty subsets in the first sample test:, and . The first and the second subset increase the sum by 0 and the third subset increases the sum by 7 - 4 = 3. In total the answer is 0 + 0 + 3 = 3.

There are seven non-empty subsets in the second sample test. Among them only the following subsets increase the answer: , , , . In total the sum is (4 - 3) + (4 - 1) + (3 - 1) + (4 - 1) = 9.

题意就是n 个数 组成的2^n个集合  问每个集合的最大值减去最小值得和对1e9取模

其实 这题就是去a[i]这个数要加多少次 减多少次

a[1] a[2] a[3] a[4] a[5].....a[n] 排序后

对于a[1]  需要减2^0+2^1+2^2.....2^(n-i-1)  就是相当于(i j)(开区间)有多少个数

加多少个a[i]也可以推  2^0+2^1+...+2^(i-2)

 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#include<string.h>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=0.0000000001;
const int N=+;
const ll mod=1e9+;
ll a[N];
ll b[N];
void init(){
b[]=;
for(int i=;i<=N;i++){
b[i]=(b[i-]*)%mod;
}
}
int main(){
int n;
init();
while(scanf("%d",&n)!=EOF){
for(int i=;i<=n;i++)scanf("%I64d",&a[i]);
sort(a+,a+n+);
ll ans=;
for(int i=;i<=n;i++){
ans=(ans-a[i]*((b[n-i]-))%mod+mod)%mod;
ans=(ans+a[i]*(b[i-]-)%mod)%mod;
}
cout<<ans<<endl;
}
}

CodeForces - 810C(规律)的更多相关文章

  1. Codeforces 810C Do you want a date?(数学,前缀和)

    C. Do you want a date? time limit per test:2 seconds memory limit per test:256 megabytes input:stand ...

  2. Integer Sequence Dividing CodeForces - 1102A (规律)

    You are given an integer sequence 1,2,…,n1,2,…,n. You have to divide it into two sets AAand BB in su ...

  3. 【codeforces 810C】Do you want a date?

    [题目链接]:http://codeforces.com/contest/810/problem/C [题意] 给你一个集合,它包含a[1],a[2]..a[n]这n个整数 让你求出这个集合的所有子集 ...

  4. CodeForces 22、23部分题解

    CodeForces 22A 找严格第二小的...注意只有一种情况,可以sort排序然后unique输出. int a[N]; int main() { int n; while(~scanf(&qu ...

  5. codeforces D. Queue 找规律+递推

    题目链接: http://codeforces.com/problemset/problem/353/D?mobile=true H. Queue time limit per test 1 seco ...

  6. Codeforces Gym 100015A Another Rock-Paper-Scissors Problem 找规律

    Another Rock-Paper-Scissors Problem 题目连接: http://codeforces.com/gym/100015/attachments Description S ...

  7. Codeforces Round #347 (Div. 2) C. International Olympiad 找规律

    题目链接: http://codeforces.com/contest/664/problem/C 题解: 这题最关键的规律在于一位的有1989-1998(9-8),两位的有1999-2098(99- ...

  8. Codeforces Round #327 (Div. 2) C. Median Smoothing 找规律

    C. Median Smoothing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/p ...

  9. Codeforces Gym 100114 A. Hanoi tower 找规律

    A. Hanoi tower Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descript ...

随机推荐

  1. 关于用友 U8-UAP二开的一些事

    这是关于一个刚刚接触用友U8的二次开发的一些小心得. 首先就是用友二开的论坛,http://u8dev.yonyou.com/ 当然这个论坛做得不怎么样,提出了好几个问题,都没有回复的. 以下是关于二 ...

  2. ES6 中set的用法

  3. vue03 axios

    4. 通过axios实现数据请求 vue.js默认没有提供ajax功能的. 所以使用vue的时候,一般都会使用axios的插件来实现ajax与后端服务器的数据交互. 注意,axios本质上就是java ...

  4. BZOJ 3489: A simple rmq problem KDtree

    Code: #include<bits/stdc++.h> #define maxn 200000 #define inf 100000000 #define mid ((l+r)> ...

  5. netperf使用指南

    1. 介绍: Netperf是由惠普公司开发的,测试网络栈.即测试不同类型的网络性能的benchmark工具,大多数网络类型TCP/UPD端对端的性能,得到网络上不同类型流量的性能参数.Netperf ...

  6. SWING界面

    import java.awt.FlowLayout;import javax.swing.*;import java.awt.Container; public class kk extends J ...

  7. office 2016最新安装及激活教程(KMS)【亲测有效】!!

    前言 博主的一个朋友,咳咳--你们懂得,想装office,于是我就上网找了一下激活的方法,亲测有效,而且也没有什么广告病毒之类的,还比较方便,所以传上来方便大家.好了,进入正题: 安装office 首 ...

  8. 利用定时器 1和定时器0控制led1和led2分别 2hz和0.5hz闪烁

    //利用定时器 1和定时器0控制led1和led2分别 2hz和0.5hz闪烁 #include<reg52.h> #define uchar unsigned char #define ...

  9. 清北学堂模拟赛d6t3 反击数

    分析:显然是一道数位dp题,不过需要一些奇怪的姿势.常规的数位dp能统计出一个区间内满足条件的数的个数,可是我们要求第k个,怎么办呢?转化为经典的二分问题,我们二分当前数的大小,看它是第几大的,就可以 ...

  10. 调度器Quartz的配置文件中的线程池设置

    在使用调度器Quartz来进行数据归档的时候,当我们开的定时任务很多的时候,就会出现一些定时任务不会被触发的现象,这就是线程阻塞.那到底什么叫线程阻塞呢? 线程阻塞,顾名思义就是说线程被阻塞了,没有按 ...