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.

代码:

#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
const int N=7*1e6+10;
typedef long long ll;
ll a[N],b[N];
void gg(){
b[0]=1;
for(int i=1;i<=N;i++)
b[i]=(b[i-1]*2)%mod;
}
int main(){
gg();
int n;
ll ans;
while(~scanf("%d",&n)){
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+1+n);
ans=0;
for(int i=0;i<=n;i++){
ans=(ans-a[i]*(b[n-i]-1)%mod+mod)%mod;
ans=(ans+a[i]*(b[i-1]-1)%mod)%mod;
}
printf("%lld\n",ans);
}
return 0;
}

Codeforces 810 C. Do you want a date?的更多相关文章

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

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

  2. codeforces 810 D. Glad to see you!(二分+互动的输入方式)

    题目链接:http://codeforces.com/contest/810/problem/D 题意:两个人玩一场游戏要猜出Noora选的f种菜的任意两种.一个人猜点另一个人回答 TAK如果 ,(x ...

  3. Codeforces 810 B. Summer sell-off

    B. Summer sell-off   time limit per test 1 second memory limit per test 256 megabytes input standard ...

  4. Codeforces 810 A.Straight «A»

    A. Straight «A»   time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  5. CF 810 D. Glad to see you!

    codeforces 810 D. Glad to see you! http://codeforces.com/contest/810/problem/D 题意 大小为k的集合,元素的范围都在[1, ...

  6. 2016-2017 ACM-ICPC, Asia Tsukuba Regional Contest D Hidden Anagrams

    题目链接:http://codeforces.com/gym/101158/attachments /* * @Author: lyucheng * @Date: 2017-10-21 12:20:0 ...

  7. 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 ...

  8. 【组合 数学】codeforces C. Do you want a date?

    codeforces.com/contest/810/problem/C [题意] 给定一个集合A,求 , 输入: [思路] 基数为n的集合有2^n-1个非空子集. 首先n个数要从小到大排序,枚举最后 ...

  9. codeforces 515A.Drazil and Date 解题报告

    题目链接:http://codeforces.com/problemset/problem/515/A 题目意思:问能否从 (0, 0) 出发,恰好走 s 步,到达该位置(a, b). 首先容易知道, ...

随机推荐

  1. [剑指Offer] 28.数组中出现次数超过一半的数字

    [思路]将每个数字都存入map中作为key值,将它们出现的次数作为value值,当value超过一半时则返回其key值. class Solution { public: int MoreThanHa ...

  2. Str 函数

    Str 函数 Visual Studio 2005 返回数字的 String 表示形式.     Public Shared Function Str(ByVal Number As Object) ...

  3. [NOIP2018 TG D2T1]旅行

    题目大意:$NOIP\;TG\;D2T1$ 题解:一棵树的很简单,第一个点一定是$1$,只需要对每个节点,找最小的没有访问过的节点访问即可,我写的是$O(n\log_2n)$. 考虑基环树的部分,一个 ...

  4. volatile的原理分析

    前言:Volatile作为一个多线程开发中的强有力的轻量级的线程协助工具,在实际编程中随处可见,它比synchronized更加轻量和方便,消耗的资源更少,了解Volatile对后面了解多线程有很重要 ...

  5. 微信小程序,设置所有标签样式

    page, view, scroll-view, swiper, movable-area, cover-view, text, icon, rich-text, progress, button, ...

  6. Dubbo入门介绍---搭建一个最简单的Demo框架

    Dubbo入门---搭建一个最简单的Demo框架 置顶 2017年04月17日 19:10:44 是Guava不是瓜娃 阅读数:320947 标签: dubbozookeeper 更多 个人分类: D ...

  7. getActionBar为null的解决以及ActionBar的Back键

    http://blog.csdn.net/lincyang/article/details/46286895

  8. Drupal7导入语言包

    下载语言包 然后把包存放在profiles\standard\translations目录下,安装. 如果出现如下错误 1.错误有MySql数据库默认引擎innoDB引起,只要mysql\bin\my ...

  9. 【BZOJ】1692: [Usaco2007 Dec]队列变换

    [算法]字符串hash [题解] 显然如果字母互不相同,贪心取是正确的. 如果存在字母相同,那么就换成比较后缀和前缀嘛. 但是要注意,不是后缀和前缀相同就能直接跳跃,每次必须只推一位. 取模的哈希比自 ...

  10. NYOJ 349 Sorting It All Out (拓扑排序 )

    题目链接 描述 An ascending sorted sequence of distinct values is one in which some form of a less-than ope ...