Problem:


Score : 500 points

Problem Statement

For two sequences S and T of length N consisting of 0 and 1, let us define f(S, T) as follows:

Consider repeating the following operation on S so that S will be equal to T. f(S, T) is the minimum possible total cost of those operations.

Change S_i (from 0 to 1 or vice versa). The cost of this operation is D \times C_i, where D is the number of integers j such that S_j \neq T_j (1 \leq j \leq N) just before this change.

There are 2^N \times (2^N - 1) pairs (S, T) of different sequences of length N consisting of 0 and 1. Compute the sum of f(S, T) over all of those pairs, modulo (10^9+7).

Constraints

1 \leq N \leq 2 \times 10^5

1 \leq C_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
C_1 C_2 \cdots C_N

Output

Print the sum of f(S, T), modulo (10^9+7).

Sample Input 1

1
1000000000

Sample Output 1

999999993

There are two pairs (S, T) of different sequences of length 2 consisting of 0 and 1, as follows:

S = (0), T = (1): by changing S_1 to 1, we can have S = T at the cost of 1000000000, so f(S, T) = 1000000000.

S = (1), T = (0): by changing S_1 to 0, we can have S = T at the cost of 1000000000, so f(S, T) = 1000000000.

The sum of these is 2000000000, and we should print it modulo (10^9+7), that is, 999999993.

Sample Input 2

2
5 8

Sample Output 2

124

There are 12 pairs (S, T) of different sequences of length 3 consisting of 0 and 1, which include:

S = (0, 1), T = (1, 0)

In this case, if we first change S_1 to 1 then change S_2 to 0, the total cost is 5 \times 2 + 8 = 18. We cannot have S = T at a smaller cost, so f(S, T) = 18.

Sample Input 3

5
52 67 72 25 79

Sample Output 3

269312



Sample input:
1
1000000000


Sample output:
999999993

Submission status: Accepted

Solution code:
N=int(input())
C=list(map(int,input().split()))
C.sort()

mod=10**9+7


ans=0
for i in range(N):
    ans+=C[i]*(N-i+1)
    ans%=mod
    
p=pow(4,N-1,mod)
print((ans*p)%mod)

Submission status: Accepted

Solution code:
N = int(input())
C = list(map(int, input().split()))
C = reversed(sorted(C))

MOD = 10 ** 9 + 7
pow = 4 ** (N - 1) % MOD

ans = 0
for i, c in zip(range(N), C):
    ans += (i + 2) * c % MOD
    ans %= MOD

print(ans * pow % MOD)

Submission status: Accepted

Solution code:
n = int(input())
c = sorted(list(map(int, input().split())), reverse=True)
p = 10**9+7
print(sum([(i+2) * c[i] for i in range(n)]) * pow(4, n-1, p) % p)

Submission status: Accepted

Solution code:
MOD=10**9+7
MAXN=4*(10**5)

two_power=[1 for _ in range(MAXN+10)]
for i in range(1, MAXN+10):
    two_power[i]=(two_power[i-1]*2)%MOD

n=int(input())
c=list(map(int, input().split()))
c.sort(reverse=True)
ans=0
x=two_power[2*n-1]
d=two_power[2*n-2]
for i in range(n):
    ans+=(x+i*d)*c[i]
print(ans%MOD)

Submission status: Accepted

Solution code:
N = int(input())
C = [int(c) for c in input().split()]
C.sort()
MOD = 10**9+7
p2 = [1]
for i in range(2*N+10):
  p2 += [p2[-1]*2%MOD]

ans = 0
for i in range(N):
  m = (p2[N-1-i]+(N-i-1)*p2[N-i-2])*C[i]
#  print(m)
  m = m*p2[N+i]%MOD
  ans += m
print(ans%MOD)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np

MOD = 10 ** 9 + 7

N = int(readline())
A = np.array(read().split(),np.int64)

A.sort()
x = (A * np.arange(N+1,1,-1) % MOD).sum() % MOD
x *= pow(2, N+N-2, MOD); x %= MOD
print(x)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
c = np.array(input().split(),dtype=int)
mod = 10**9+7

c = sorted(c)[::-1]
#4^(n-1)を求める
e4 = 1
k = n-1
inst = 4
while k > 0:
    if k%2 == 1:
        e4 = (e4*inst)%mod
    k = k//2
    inst = (inst**2)%mod

ans = 0
for i in range(n):
    ans = (ans+c[i]*(i+2))%mod
ans = (ans*e4)%mod

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
*C, = map(int, input().split())
C.sort(reverse=True)

mod = 10**9+7

ans = 0
for i,c in enumerate(C, start=1):
    ans += (c*(i+1))
ans *= (4**(N-1))
ans %= mod
print(ans)

Submission status: Accepted

Solution code:
def main():
    mod=1000000007
    n=int(input())
    C=list(map(int,input().split()))
    C.sort(reverse=True)
    ans=0
    for i,c in enumerate(C):
        ans+=c*(i+2)
        ans%=mod
    ans*=pow(4,n-1,mod)
    ans%=mod
    print(ans)
    
if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
import math
n = int(input())
c = sorted(list(map(int, input().split())),reverse=True)
a = 0
for i in range(n):
  a += c[i]*(i+2)
  a = a%(10**9+7)
a = (a*pow(2,2*n-2,10**9+7))%(10**9+7)
print(a)

Submission status: Accepted

Solution code:
n = int(input())
c = list(map(int, input().split()))
mod = 10**9+7
c.sort(reverse=True)
ans = 0
for k, num in enumerate(c):
    ans += (k+2)*num
    ans %= mod

for _ in range(2*n-2):
    ans *= 2
    ans %= mod

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
a.sort(reverse=True)
mod = 10 ** 9 + 7
ans = 1
for _ in range(2*n-2):
  ans *= 2
  ans %= mod
cef = [i+2 for i in range(n)]
for i in range(n):
  a[i] *= cef[i]
ans *= sum(a)
ans %= mod
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
C = list(map(int,input().split()))
C.sort(reverse=True)
MOD = 10**9+7
if N==1:
    print((C[0]*2)%MOD)
    exit()

ans = 0
d = pow(2,N-2,MOD)
base = pow(2,N-1,MOD)
for i,c in enumerate(C):
    ans += c * (base + i*d)
ans *= pow(2,N,MOD)
ans %= MOD
print(ans)

Submission status: Accepted

Solution code:
MOD = 10**9+7
n = int(input())
S = [1]*n
D = [1]*(n+1)
for i in range(n):
  D[i+1] = D[i]*2 % MOD
for i in range(n-1):
  S[i+1] = (S[i]*2 + D[i+1]) % MOD
T = S[::-1]
C = sorted(list(map(int, input().split())))
ans = 0
for i in range(n):
  ans += C[i]*((D[n-(i+1)]+T[i])*D[i]) % MOD
  ans %= MOD
ans = ans*D[n-1]%MOD
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
f_i = stdin

mod = 10 ** 9 + 7

N = int(f_i.readline())

C = map(int, f_i.readline().split())
C = sorted(C)

x = (C_i * i % mod for i, C_i in zip(range(N + 1, 1, -1), C))

ans = pow(4, N - 1, mod) * sum(x) % mod

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8

import itertools
import sys

N = int(input())
C = [int(x) for x in input().split()]

# Cを昇順にソートしておく
C.sort()

# （C_iの個数×C_iのコスト）の総和×2**Nを求める
sum = 0
for i in range(N):
	sum += (N + 1 - i) * C[i]
sum *= 2 ** (N - 2)
sum *= 2 ** N

# 10**9 + 7で割った余りを求めて出力
print(int(sum % (10 ** 9 + 7)))

Submission status: Accepted

Solution code:
def power(n, r, mod=10**9+7):
    if r == 0: return 1
    if r%2 == 0:
        return power(n*n % mod, r//2, mod) % mod
    if r%2 == 1:
        return n * power(n, r-1, mod) % mod


N = int(input())
C = list(map(int, input().split()))
C.sort()
MOD = 10**9 + 7

ans = 0
for i, c in enumerate(C, 1):
  ans = (ans + (N-i+2) * c) % MOD

ans *= power(4, N-1)
ans %= MOD
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
c=sorted(list(map(int,input().split())),reverse=True)
ans=0
mod=10**9+7
for i in range(1,n+1):
  ans+=c[i-1]*(i+1)%mod
print(4**(n-1)*ans%mod)

Submission status: Accepted

Solution code:
mod=10**9+7
n=int(input())
arr=list(map(int,input().split()))
arr=sorted(arr)
ans=0
total=pow(2,2*n-2,mod)
for i in range(n):
  ans+=total*(n-i+1)*arr[i]
  ans%=mod
print(int(ans))

Submission status: Accepted

Solution code:
mod=10**9+7
n=int(input())
arr=list(map(int,input().split()))
arr=sorted(arr)
ans=0
for i in range(n):
  ans+=pow(2,2*n-2,mod)*(n-i+1)*arr[i]
  ans%=mod
print(int(ans))

Submission status: Accepted

Solution code:
mod=10**9+7
n=int(input())
arr=list(map(int,input().split()))
arr=sorted(arr)
ans=0
if n==1:
  print((2*arr[0])%mod)
else:
  table=[1]
  for _ in range(n):
    tmp=table[-1]*2
    tmp%=mod
    table.append(tmp)
  for i in range(n):
    if i==n-1:    
      ans+=(table[i-1]*(n-i+1)*arr[i])%mod
    else:
      ans+=(table[i]*(n-i+1)*table[n-i-2]*arr[i])%mod
  print((ans*table[n])%mod)

Submission status: Accepted

Solution code:
N = int(input())
C = [0] + sorted(list(map(int, input().split())))
MOD = 10 ** 9 + 7

ans = 0
for i in range(1, N + 1):
    l, r = i - 1, N - i
    ans += C[i] * pow(2, l, MOD) * (pow(2, max(0, r - 1), MOD) * r % MOD + pow(2, r, MOD)) % MOD

print(ans * pow(2, N, MOD) % MOD)

Submission status: Accepted

Solution code:
n = int(input())
c = [int(i) for i in input().split()]
f = 0
c.sort()
c.reverse()
for i in range(n):
    f = f + c[i]*(i+2)
print(f*(4**(n-1))%(10**9+7))

Submission status: Accepted

Solution code:
n = int(input())
M = 10**9 + 7
a = list(map(int, input().split()))
a.sort(reverse=True)
# print(np.sum((np.array(range(2, 2 + n)) * 2**((n - 1) * 2))[::-1] * a) % M)

def mod(a):
    if a >= M:
        a %= M
    return a

def powmod(a, n):
    if n == 0:
        return 1
    if n % 2:
        return mod(a * powmod(a, n - 1))
    else:
        return mod(powmod(a, n // 2)**2)

result = 0
for i, v in enumerate([mod(i * powmod(2, (n - 1) * 2)) for i in range(2, 2 + n)]):
    result = mod(result + mod(v * a[i]))
print(result)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys

N=input()
C=map(int, sys.stdin.readline().split())
C.sort()

mod=10**9+7
D=[]
d0=pow(2,N-1,mod)

#D：係数の配列の作成
for i in range(N):
    if i==0:
        D.append(d0)
    else:
        next=(2*D[-1]-d0)*pow(2,mod-2,mod)+d0
        D.append(next%mod)

#Ci*Diを最小にするため、最小のCiに最大のDiを掛けるため、Dは配列を逆順にしたものを足し合わせる
#Dもmodを取っているので、Dを降順ソートすると順番が崩れる
ans=0
for c,d in zip(C,D[::-1]):
    ans+=c*d
    ans%=mod

ans=ans*pow(2,N,mod)

print ans%mod

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys

N=input()
C=map(int, sys.stdin.readline().split())
C.sort()

mod=10**9+7
D=[]
d0=pow(2,N-1,mod)

#D：係数の配列の作成
for i in range(N):
    if i==0:
        D.append(d0)
    else:
        next=(2*D[-1]-d0)*pow(2,mod-2,mod)+d0
        D.append(next%mod)

#Ci*Diを最小にするため、最小のCiに最大のDiを掛けるため、Dは配列を逆順にしたものを足し合わせる
#modを取っているので、Dを降順ソートすると順番が崩れる
ans=0
for c,d in zip(C,D[::-1]):
    ans+=c*d
    ans%=mod

ans=ans*pow(2,N,mod)

print ans%mod

Submission status: Accepted

Solution code:
N = int(input())
C = list(map(int,input().split()))

mod = 10**9 + 7

C = sorted(C)[::-1]

ans = 0

for n in range(N):
    ans += pow(4,N-1,mod)*(n+2)*C[n]
    ans %= mod
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
C = list(map(int,input().split()))
for i in range(N):
    C[i] *= -1
C.sort()
for i in range(N):
    C[i] *= -1
mod = 10**9 + 7
ans = 0
for i in range(N):
    ans += C[i]*(i+2)
ans = ans*(4**(N-1))
ans = ans % mod
print(ans)

Submission status: Accepted

Solution code:
#import sys
#sys.setrecursionlimit(10**5)
MOD = 10**9+7

# input
N = int(input())
C = list(map(int, input().split()))
    
# process
C.sort(reverse=True)
ans = 0

if N == 1:
    ans = C[0]*2**N
else:
    for i in range(N):
        ans += C[i]*(2+i)

    ans *= 4**(N-1)

# output
print(ans%MOD)

Submission status: Accepted

Solution code:
import sys


n = int(input())
c = sorted([int(_) for _ in input().split()], reverse=True)
mod = 10**9+7
ans = 0
if n == 1:
    ans += c[0] * 2
    ans %= mod
    print(ans)
    sys.exit()
for i in range(n):
    ans += (c[i] * (pow(2, n-1, mod) + pow(2, n-2, mod) * i)) % mod
    ans %= mod
ans *= pow(2, n, mod)
ans %= mod
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
C=sorted(list(map(int,input().split())),reverse=True)
mod=10**9+7
def mpow(x,n):
    num=1
    while n > 0:
        if n&1:
            num=num*x%mod
        x=x*x%mod
        n=n>>1
    return(num)
count=0
for i, c in enumerate(C, 2):
    count += c * (i)
    count %= mod
print(count * mpow(2, N-1)*mpow(2, N-1) % mod)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10000000)
# input = sys.stdin.readline
from bisect import *
from collections import *
from heapq import *
# import functools
# import itertools
# import math
INF = 500000
mod = 10**9+7

N = int(input())
C = [int(x) for x in input().split()]
C.sort(reverse = 1)
x = 0
for i, c in enumerate(C, 1):
    x += c * (i+1)
    x %= mod
print(x * pow(4, N-1, mod) % mod)

Submission status: Accepted

Solution code:
inpl = lambda: list(map(int,input().split()))
MOD = 10**9+7
N = int(input())
C = inpl()
C.sort()
ans = 0
for i in range(N):
    ans += (N+1-i)*C[i]
    ans %= MOD
ans *= pow(2,2*N-2,MOD)
ans %= MOD
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
c = sorted(list(map(int, input().split())),reverse=True)
mod = 10**9 + 7
ans = 0
two = [1]
for i in range(n*2):
  two += [(two[i]*2) % mod]

for i in range(n):
  ans += (i+2) * c[i]
  ans %= mod
ans *= two[2*n - 2]
print(ans%mod)

Submission status: Accepted

Solution code:
N = int(input())
C = [int(i) for i in input().split()]
mod = 10 ** 9 + 7

C.sort()

ret = 0
P = pow(4, N - 1, mod)
for i in range(N) :
  ret += C[i] * P * (N - i + 1) % mod
  ret %= mod

print(ret)

Submission status: Accepted

Solution code:
N = int(input())
C = [int(i) for i in input().split()]
mod = 10 ** 9 + 7

C.sort()

ret = 0
for i in range(N) :
  rest = N - i - 1
  if rest == 0 :
    ret += C[i] * pow(4, i, mod) * pow(2, N - i, mod) * pow(2, rest, mod) % mod
  else :
    ret += C[i] * pow(4, i, mod) * pow(2, N - i, mod) * (rest + 2) * pow(2, rest - 1, mod) % mod
  ret %= mod

print(ret)

Submission status: Accepted

Solution code:
# inverse x^(-1)
def inv(x):
    global mod
    return pow(x,mod-2,mod)

mod=10**9+7
N = int(input())
C = [0]
C.extend(sorted(list(map(int,input().split())), reverse=True)) # 1-indexed, 降順

# print(C)
# C[i]が最後からj番目に操作される（コストが C[i]*j 掛かる）のは，c[1]~C[i-1]にj-1個操作すべきもの，すなわち，C[i]以上のものがあるとき。
# C[i+1]~C[N]の選び方は自由。
# ⇒ combi(i-1, j-1)*2**(N-i) を掛ける。
# 総和を計算すると，結局，2**N * Σ[i=1->N](2**(N-i)*Ci*(2**(i-2)*(i-1) + 2**(i-1))) になる

pow2 = [1]
for k in range(1, N+1):    
    pow2.append(pow2[-1] * 2 % mod)

# print(pow2)

ans=0
for i in range(1, N+1):
    temp = pow2[N - i] * C[i] * ((0 if i == 1 else (pow2[i - 2] * (i - 1))) + pow2[i - 1]) % mod
    ans = (ans + temp) % mod

ans = ans * pow2[N] % mod

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
import sys
import numpy as np
import collections
from functools import cmp_to_key
import heapq
sys.setrecursionlimit(100000)

##  input functions for me
def rsa(sep = ''):
    if sep == '' :
        return input().split() 
    else: return input().split(sep)
def rip(sep = ''):
    if sep == '' :
        return map(int, input().split()) 
    else: return map(int, input().split(sep))
def ria(sep = ''): 
    return list(rip(sep))
def ri(): return int(input())
def rd(): return float(input())
def rs(): return input()
##
class mint:
    mod = int(1e9 + 7)
    def __init__(self, v = 0):
        if not ((v >= 0) and (v < mint.mod)):
            v %= mint.mod
            if v < 0: v += mint.mod
        self.V = v
    def __add__(self, other):
        v = self.V + (other.V if isinstance(other, mint) else other)
        return mint(v)
    def __sub__(self, other):
        v = self.V - (other.V if isinstance(other, mint) else other)
        return mint(v)
    def __mul__(self, other):
        v = self.V * (other.V if isinstance(other, mint) else other)
        return mint(v)
    def __floordiv__(self, other):
        v = self.V * mint.inv((other.V if isinstance(other, mint) else other))
        return mint(v)
    def __truediv__(self, other):
        v = self.V * mint.inv((other.V if isinstance(other, mint) else other))
        return mint(v)
    
    def __eq__(self, other):
        return self.V == (other.V if isinstance(other, mint) else mint(other).V)
    def __ne__(self, other):
        return self.V != (other.V if isinstance(other, mint) else other)
    def __int__(self): return self.V
    # right operand
    def __radd__(self, other):
        v = (other.V if isinstance(other, mint) else other) + self.V
        return mint(v)
    def __rsub__(self, other):
        v = (other.V if isinstance(other, mint) else other) - self.V
        return mint(v)
    def __rmul__(self, other):
        v = (other.V if isinstance(other, mint) else other) * self.V
        return mint(v)
    def __rfloordiv__(self, other):
        v = (other.V if isinstance(other, mint) else other) * mint.inv(self.V)
        return mint(v)
    def __rtruediv__(self, other):
        v = (other.V if isinstance(other, mint) else other) * mint.inv(self.V)
        return mint(v)

    @staticmethod
    def inv(x):
        a, _, _ = mint.extGCD(x, mint.mod)
        return (a + mint.mod) % mint.mod
    @staticmethod
    def extGCD(x, y):
        r0 = x
        r1 = y
        a0 = 1
        a1 = 0
        b0 = 0
        b1 = 1
        while(r1 > 0):
            q1 = r0 // r1
            r2 = r0 % r1
            a2 = a0 - q1 * a1
            b2 = b0 - q1 * b1
            r0 = r1; r1 = r2
            a0 = a1; a1 = a2
            b0 = b1; b1 = b2
        c = r0
        a = a0
        b = b0       
        return a, b, c
    @staticmethod
    def pow(x, k):
        x = x.V if isinstance(x, mint) else x
        return pow(x, k, mint.mod)

    
    def __str__(self):
        return str(self.V)
    def __repr__(self):
        return str(self.V)

def main():
    N = ri()
    A = ria()
    A = sorted(A)
    A = list(reversed(A))
    Pow2 = [mint(0)] * (N + 1)
    Pow2[0] = mint(1)
    for i in range(1,N+1): Pow2[i] = Pow2[i-1] * 2

    tot = mint(0)
    for i in range(N):
        tot += mint(A[i]) * Pow2[N-1]
        if i > 0: tot += mint(A[i]) * i * Pow2[N-2]
    tot *= Pow2[N]
    print(tot)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[] for _ in range(num)]
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

#################

N = I()
C = III()
C.sort()

ans = 0
for i in range(N):
    ans += pow(4,N-1,mod)*C[i]*(N-i+1)
    ans %= mod

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import sys
sys.setrecursionlimit(300000)

MOD = 1000000007  # type: int

def solve(N: int, C: "List[int]"):
    pows = [1]
    for i in range(N):
        pows.append(pows[i] * 2 % MOD)

    C.sort(reverse=True)
    ret = 0
    for i, v in enumerate(C):
        if i > 0:
            ret += (pows[i] + pows[i - 1] * i) * (pows[N - i - 1]) * v
        else:
            ret += pows[i] * (pows[N - i - 1]) * v
    ret = ret * pows[N] % MOD
    print(ret)
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    C = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, C)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
c = list(map(int, input().split()))

MOD = 10 ** 9 + 7

m = 4 ** (n-1) % MOD

c.sort(reverse=True)

ans = 0
for i in range(n):
    ans = (ans + c[i] * (i+2) * m) % MOD

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

N = int(input())
C = list(map(int, input().split()))
ans = 0
MOD = 10**9+7

C.sort()

for i in range(N):
    left = pow(2, 2*i, MOD)
    
    if i==N-1:
        right = 1
    else:
        right = pow(2, 2*N-2*i-3, MOD)*(N-i+1)

    ans += left*right*2*C[i]
    ans %= MOD

print(ans)

Submission status: Accepted

Solution code:
#E
n=int(input())
C=sorted(list(map(int,input().split())))
mod=10**9+7

pow2=[1]
for i in range(n+5): #2の累乗(mod 10**9+7)のテーブル作成
    pow2.append(pow2[i]*2%mod)

sum=0
for i in range(n):
    l=i #その桁より左側の桁数(関係ないやつ)
    r=n-i #その桁より右側の個数(その桁自身も含む、1の個数を数えるやつ)
    if r==1:
        ans=pow2[l]
        ans=ans*C[i]%mod
        sum=(sum+ans)%mod
    else:
        ans=pow2[r-1]
        ans=(ans+pow2[r-2]*(r-1)%mod)%mod
        ans=ans*pow2[l]%mod
        ans=ans*C[i]%mod
        sum=(sum+ans)%mod
print((sum*pow2[n]%mod))

Submission status: Accepted

Solution code:
import collections as c
ip = lambda : map(int, input().split())
mod = 10**9+7
# ##############
N = int(input())
l = list(ip())
l.sort()

ans = 0
f = 1
for i in range(N):
    ans = (ans + l[i]*(1+N-i)%mod)%mod
    if i>0:
        f=f*4%mod
ans = ans*f%mod
print(ans)

Submission status: Accepted

Solution code:
mod = 10 ** 9 + 7

N, *C = map(int, open(0).read().split())

C.sort(reverse=True)
print(pow(4, N - 1, mod) * (sum(i * c for i, c in enumerate(C, 2)) % mod) % mod)

Submission status: Accepted

Solution code:
mod = 10 ** 9 + 7

N, *C = map(int, open(0).read().split())

C.sort(reverse=True)
print(pow(4, N - 1, mod) * sum(i * c for i, c in enumerate(C, 2)) % mod)

Submission status: Accepted

Solution code:
#T=(0,0,...,0)
#に変換するためのコストの2^N倍
N=int(input())
c=sorted([int(i) for i in input().split()])
mod=10**9+7
ans=0
for k in range(N):
    a1=pow(2,(N-2)%(mod-1),mod)
    a2=(N-k+1)
    tmp=(a1*a2)%mod
    ans+=(c[k]*tmp)%mod
    ans%=mod
    #print(k,tmp)
ans=ans*pow(2,N,mod)
ans%=mod
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
C = list(map(int, input().split()))

MOD = 10 ** 9 + 7

C.sort()
C = C[::-1]

a0 = pow(2, N - 1, MOD)
d = pow(2, N - 2, MOD) if N - 2 >= 0 else 0
answer = 0
for i in range(N):
  answer = (answer + (a0 + d * i) * C[i]) % MOD
answer = (answer * pow(2, N, MOD)) % MOD
print(int(answer))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
mod = 10**9+7

n=int(input())
c=list(map(int,input().split()))
c.sort(reverse=True)

lpow = [False] * (n+5)
lpow[0] = 1
def pow(n,p):
    if p<0:return 0
    if lpow[p]:
        return lpow[p]
    else:
        if p % 2 == 0:
            ret = (pow(n,p//2)**2) % mod
        else:
            ret = ((pow(n,p//2)**2)*n) % mod
        lpow[p] = ret
        return ret

res = 0
for i in range(n):
    res += (c[i]*pow(4,n-1)*(i+2))% mod
    res %= mod
print(res)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

num = int(input())
costs = sorted(list(map(int, input().split())), reverse = True)

total = 0
for i in range(1, num+1):
    total += (i + 1) * costs[i-1]
ans = (total * (4 ** (num - 1))) % (10 ** 9 + 7)
print(ans)

Submission status: Accepted

Solution code:
import sys

MOD = 10 ** 9 + 7

def make_power_2_table(n=10**6, mod=MOD):
    power = [None] * (n + 1)
    power[0] = 1
    for i in range(n):
        power[i+1] = power[i] * 2 % mod
    return power

power_2 = make_power_2_table(2*10**5)
inv_2 = pow(2, MOD-2, MOD)

n, *c = map(int, sys.stdin.read().split())
c.sort()

def main():
    res = 0
    for i in range(n):
        sum_d = (power_2[n-i-1] + power_2[n-(i+1)] * inv_2 % MOD * (n-(i+1)) % MOD) % MOD
        l_comb = power_2[i]
        res += c[i] * l_comb % MOD * sum_d % MOD
        res %= MOD

    return res * power_2[n] % MOD

if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Accepted

Solution code:
import sys

MOD = 10 ** 9 + 7

def make_power_2_table(n=10**6, mod=MOD):
    power = [None] * (n + 1)
    power[0] = 1
    for i in range(n):
        power[i+1] = power[i] * 2 % mod
    return power

power_2 = make_power_2_table(2*10**5)
inv_2 = pow(2, MOD-2, MOD)

n, *c = map(int, sys.stdin.read().split())
c.sort()

def main():
    r_diffs = (power_2[n] + power_2[n] * n % MOD * inv_2 % MOD) % MOD
    res = 0
    for i in range(n):
        r_diffs = (r_diffs - power_2[n-i-1]) % MOD * inv_2 % MOD
        l_comb = power_2[i]
        res += c[i] * l_comb % MOD * r_diffs % MOD
        res %= MOD

    return res * power_2[n] % MOD

if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Accepted

Solution code:
N=int(input())
C=sorted(map(int,input().split()))
mod=10**9+7
ANS=0
for i in range(1,N+1):
    ANS=ANS+C[-i]*(i+1)*pow(2,N*2-2,mod)
    ANS%=mod
print(ANS)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys

MOD = 1000000007  # type: int


def solve(N: int, C: "List[int]"):
    if N == 1:
        print(2*C[0]%MOD)
        return
    
    start = pow(2,N-1,MOD)
    distance = pow(2,N-2,MOD)

    count = [0]*N
    for i in range(N):
        count[i] = (start+distance*i)%MOD
    answer = 0
    C.sort(reverse=True)
    for i in range(N):
        answer += C[i]*count[i]
        answer%=MOD
    answer*=pow(2,N,MOD)
    answer%=MOD
    print(answer)


    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    C = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, C)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = sorted([int(a) for a in input().split()])[::-1]
P = 10**9+7
print(sum([(i+2) * A[i] for i in range(N)]) * pow(4, N-1, P) % P)

Submission status: Accepted

Solution code:
n = int(input())
c = list(map(int, input().split()))
c.sort()

MOD = 10 ** 9 + 7
ans = 0

for i in range(n):
    ans += c[i] *(2 + (n - (i+1))) *  pow(4, n-1, MOD)
    ans %= MOD

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

def fermat(x, y, MOD): return x * pow(y, MOD-2, MOD) % MOD

N = INT()
A = LIST()

# 大きい数ほど少なく使うのが最適なので降順ソートしておく
A.sort(reverse=1)
# 各要素が変更のために何回使われるか数える
cnts = [0] * N
cnts[0] = pow(4, N-1, MOD)
add = fermat(cnts[0], 2, MOD)
for i in range(1, N):
    cnts[i] = cnts[i-1] + add
    cnts[i] %= MOD

ans = 0
for i in range(N):
    ans += A[i] * cnts[i]
    ans %= MOD
print(ans * 2 % MOD)

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7


N = int(input())
C = list(map(int, input().split()))
C.sort(reverse=True)

mod4 = [0] * (N + 1)
mod4[0] = 1
for i in range(N):
    mod4[i + 1] = (mod4[i] * 4) % MOD

ans = 0
N4 = mod4[N - 1]
for i, c in enumerate(C):
    j = i + 1
    ans += N4 * (j + 1) * c
    ans %= MOD

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
p=[int(i) for i in input().split()]
mod = 10**9+7
p.sort()
ans = 0
for i,c in enumerate(p):
    mult = (pow(2,n-1-i,mod)+(n-1-i)*pow(2,n-2-i,mod))%mod if i+1!=len(p) else 1
    ans = (ans + ( (pow(2,i,mod)*c)%mod ) * mult )%mod
ans = (ans*pow(2,n,mod))%mod
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int, input().split()))
mod = 10**9 + 7
tot = 0
l.sort(reverse=True)
for i,x in enumerate(l,2):
  tot += x*i
  tot %= mod
print(tot*pow(2,n*2-2,mod)%mod)

Submission status: Accepted

Solution code:
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N = int(sys.stdin.buffer.readline())
C = list(map(int, sys.stdin.buffer.readline().split()))
C.sort(reverse=True)

assert pow(2, MOD - 2, MOD) == 500000004


def div2(a):
    return a % MOD * 500000004 % MOD


pow2 = [1]
for _ in range(N):
    pow2.append(pow2[-1] * 2 % MOD)


# (1+x)^k = kC0 * x^0 + kC1 * x^1 + kC2 * x^2 + ... + kCk * x^k
# (1+x)^k * x = kC0 * x^1 + kC1 * x^2 + kC2 * x^3 + ... + kCk * x^(k+1)
# 微分して x=1 を代入すると、上から1番目からk番目になる通り数 * D の総和になる
# kC0 * 1 + kC1 * 2 + kC2 * 3 + ... + kCk * (k+1)
def df(k):
    # a(x) = (1+x)^k * x として、a'(1) を返す。
    # https://mathtrain.jp/composite
    # http://w3e.kanazawa-it.ac.jp/math/category/bibun/keisan/diff-gh.html
    # f(x) = 1+x
    # g(x) = x^k
    # h(x) = x
    # g(f(x)) * h(x) = (1+x)^k * x
    # f'(x) = 1
    # g'(x) = kx^(k-1)
    # h'(x) = 1
    # g'(f(x)) = g'(f(x)) * f'(x) = k(1+x)^(k-1)
    # (g(f(x)) * h(x))' = g'(f(x)) * h(x) + g(f(x)) * h'(x)
    #                   = k(1+x)^(k-1) + (1+x)^k
    # (g(f(1)) * h(1))' = k * 2^(k-1) + 2^k
    #                   = k * 2^k / 2 + 2^k
    #                   = (k + 2) / 2 * 2^k
    return div2((k + 2) * pow2[k])


ans = 0
for k in range(N):
    # k までの選び方 * 上から何番目か * C[k] * k より後ろの選び方
    ans += df(k) * C[k] * pow2[N - k - 1]
    ans %= MOD
print(ans * pow2[N] % MOD)

Submission status: Accepted

Solution code:
import os
import sys

# from libs.debug import debug

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N = int(sys.stdin.buffer.readline())
C = list(map(int, sys.stdin.buffer.readline().split()))
C.sort(reverse=True)


def div_mod(a, b, mod):
    return a * pow(b, mod - 2, mod) % mod


# (1+x)^k = kC0 * x^0 + kC1 * x^1 + kC2 * x^2 + ... + kCk * x^k
# (1+x)^k * x = kC0 * x^1 + kC1 * x^2 + kC2 * x^3 + ... + kCk * x^(k+1)
# 微分して x=1 を代入すると、上から1番目からk番目になる通り数 * D の総和になる
# kC0 * 1 + kC1 * 2 + kC2 * 3 + ... + kCk * (k+1)
def df(k):
    # (1+x)^x * x を微分した値を返す。
    # https://mathtrain.jp/composite
    # http://w3e.kanazawa-it.ac.jp/math/category/bibun/keisan/diff-gh.html
    # f(x) = 1+x
    # g(x) = x^k
    # h(x) = x
    # g(f(x)) * h(x) = (1+x)^k * x
    # f'(x) = 1
    # g'(x) = kx^(k-1)
    # h'(x) = 1
    # g'(f(x)) = g'(f(x)) * f'(x) = k(1+x)^(k-1)
    # (g(f(x)) * h(x))' = g'(f(x)) * h(x) + g(f(x)) * h'(x)
    #                   = k(1+x)^(k-1) + (1+x)^k
    # (g(f(1)) * h(1))' = k * 2^(k-1) + 2^k
    #                   = k * 2^k / 2 + 2^k
    #                   = (k + 2) / 2 * 2^k
    return div_mod((k + 2) * pow(2, k, MOD), 2, MOD)


ans = 0
for k in range(N):
    # k までの選び方 * 上から何番目か * C[k] * k より後ろの選び方
    ans += df(k) * C[k] * pow(2, N - k - 1, MOD)
    ans %= MOD
print(ans * pow(2, N, MOD) % MOD)

Submission status: Accepted

Solution code:
n = int(input())
c = list(map(int, input().split()))
c.sort(reverse=True)

MOD = 10**9+7
two = [0]*(n+1)
for i in range(n+1):
    two[i] = pow(2, i, MOD)

cost = 0
for i in range(n):
    current_one_num = two[i]
    left_one_num = 0
    if i != 0:
        left_one_num = two[i-1] * i % MOD
    right_num = two[n-1-i]
    one_num = (current_one_num + left_one_num) * right_num % MOD
    current_cost = one_num * c[i] % MOD
    cost = (cost + current_cost) % MOD

num = two[n]
ans = cost * num % MOD
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
mod=10**9+7
c=list(map(int,input().split()))
c.sort()

ans=0
cnt=pow(4,N-1,mod)
for i in range(N):
    ans +=cnt*2*c[i] + cnt*(N-1-i)*c[i]
    ans%=mod
    
print(ans)

Submission status: Accepted

Solution code:
def p_e():
    N = int(input())
    *C, = map(int, input().split())
    C.sort()
    mod = 10 ** 9 + 7
    ans = 0
    for i in range(N):
        ans += C[N - i - 1] * (i + 2)
        ans %= mod
    print((4 ** (N - 1)) * ans % mod)
    
p_e()

Submission status: Accepted

Solution code:
n=int(input())
mod=10**9+7
c=list(map(int,input().split()))
c.sort()
ans=0
for i in range(n):
    ans+=c[i]*(n-i+1)
    ans%=mod
print((ans*pow(4,n-1,mod))%mod)