反正又是一个半小时没做出来。。。

先排序,然后求和,第i个和第j个,f(a)=a[j]-a[i]=a[i]*(2^(j-i-1))因为从j到i之间有j-i-1个数(存在或者不存在有两种情况)

又有a[i+k]-a[i]=a[n]+a[n-1]+...+a[n-k]-a[k+1]-...-a[1]

Codeforces Round #415 (Div. 2)C_iosCodeforces Round #415 (Div. 2)C_i++_02
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cassert>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;

const double g=10.0,eps=1e-9;
const int N=300000+10,maxn=16,inf=9999999;

ll a[N],sum[N],p[N];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n;
    cin>>n;
    p[0]=1;
    for(ll i=1;i<=n;i++)
    {
        p[i]=p[i-1]<<1;
        p[i]%=mod;
    }
    sum[0]=0;
    for(ll i=1;i<=n;i++)cin>>a[i];
    sort(a+1,a+1+n);
    for(ll i=1;i<=n;i++)sum[i]=(sum[i-1]+a[i])%mod;
    ll ans=0;
    for(ll i=0;i<n-1;i++)
    {
        ans+=(p[i]*(sum[n]+mod-sum[i+1]+mod-sum[n-i-1])%mod)%mod;
        ans%=mod;
    }
    cout<<ans<<endl;
    return 0;
}

/*
a[i]=sum[n]-sum[n-i-1]-sum[i]
*/
View Code