Problem:


Score : 400 points

Problem Statement

You are given N integers; the i-th of them is A_i.
Find the maximum possible sum of the absolute differences between the adjacent elements after arranging these integers in a row in any order you like.

Constraints

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print the maximum possible sum of the absolute differences between the adjacent elements after arranging the given integers in a row in any order you like.

Sample Input 1

5
6
8
1
2
3

Sample Output 1

21

When the integers are arranged as 3,8,1,6,2, the sum of the absolute differences between the adjacent elements is |3 - 8| + |8 - 1| + |1 - 6| + |6 - 2| = 21. This is the maximum possible sum.

Sample Input 2

6
3
1
4
1
5
9

Sample Output 2

25

Sample Input 3

3
5
5
1

Sample Output 3

8



Sample input:
5
6
8
1
2
3


Sample output:
21

Submission status: Accepted

Solution code:
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [long(input_raw[0])]
    A = []
    for i in range(N):
        input_raw = read_func().strip().split()
        A.append(long(input_raw[0]))

    A.sort()

    B = deque([])
    is_back = False
    is_append_left = True
    is_append_left_back = False

    for i in range(N):
##        if len(B) == 0:
##            B.append(A[i])
##        else:
            if is_back == False:
                x = A[i/2]
                if is_append_left == True:
                    B.appendleft(x)
                    is_append_left = False
                else:
                    B.append(x)
                    is_append_left = True

            else:
                x = A[len(A) - 1 - i/2]
                if is_append_left_back == True:
                    B.appendleft(x)
                    is_append_left_back = False
                else:
                    B.append(x)
                    is_append_left_back = True

##            if abs(B[0] - x) > abs(B[len(B) - 1] - x):
##                B.appendleft(x)
##            elif abs(B[0] - x) < abs(B[len(B) - 1] - x):
##                B.append(x)
##            else:
##                if is_back == False:
##                    if B[0] <= B[len(B) - 1]:
##                        B.append(x)
##                    else:
##                        B.appendleft(x)
##                else:
##                    if B[0] >= B[len(B) - 1]:
##                        B.append(x)
##                    else:
##                        B.appendleft(x)
            if is_back == False:
                is_back = True
            else:
                is_back = False
    score1 = 0L
    for i in range(N -1):
        score1 += abs(B[i] - B[i + 1])


    A.sort(reverse = True)

    B = deque([])
    is_back = False
    is_append_left = True
    is_append_left_back = False
    for i in range(N):
##        if len(B) == 0:
##            B.append(A[i])
##        else:
            if is_back == False:
                x = A[i/2]
                if is_append_left == True:
                    B.appendleft(x)
                    is_append_left = False
                else:
                    B.append(x)
                    is_append_left = True

            else:
                x = A[len(A) - 1 - i/2]
                if is_append_left_back == True:
                    B.appendleft(x)
                    is_append_left_back = False
                else:
                    B.append(x)
                    is_append_left_back = True

##            if abs(B[0] - x) > abs(B[len(B) - 1] - x):
##                B.appendleft(x)
##            elif abs(B[0] - x) < abs(B[len(B) - 1] - x):
##                B.append(x)
##            else:
##                if is_back == True:
##                    if B[0] < B[len(B) - 1]:
##                        B.append(x)
##                    else:
##                        B.appendleft(x)
##                else:
##                    if B[0] > B[len(B) - 1]:
##                        B.append(x)
##                    else:
##                        B.appendleft(x)
            if is_back == False:
                is_back = True
            else:
                is_back = False
    score2 = 0L
    for i in range(N -1):
        score2 += abs(B[i] - B[i + 1])

    print max(score1, score2)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
A=[int(input()) for _ in range(N)]

A.sort(reverse=True)
if len(A)%2==0:
  print(A[len(A)//2-1]-A[len(A)//2]+2*sum(A[:len(A)//2-1])-2*sum(A[len(A)//2+1:]))
else:
  tmp1=-A[len(A)//2]-A[len(A)//2+1]+2*sum(A[:len(A)//2])-2*sum(A[len(A)//2+2:])
  tmp2=A[len(A)//2-1]+A[len(A)//2]+2*sum(A[:len(A)//2-1])-2*sum(A[len(A)//2+1:])
  print(tmp1 if tmp1>tmp2 else tmp2)

Submission status: Accepted

Solution code:
N = int(input())
a_list = [int(input()) for _ in range(N)]

a_list.sort(reverse=True)

half = N // 2

ans1 = 0
ans2 = 0
for i in range(half):
    if i == half - 1:
        ans1 += a_list[i]
        ans1 -= a_list[-(i + 1)]
    else:
        ans1 += 2 * a_list[i]
        ans1 -= 2 * a_list[-(i + 1)]
ans2 = ans1
if N % 2 != 0:
    ans1 += a_list[half - 1]
    ans1 -= a_list[- (half + 1)]
    ans2 += a_list[half]
    ans2 -= a_list[- half]

print(max(ans1, ans2))

Submission status: Accepted

Solution code:
# coding:utf-8

import sys
from collections import Counter, deque

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return input()


N = I()
A = [I() for _ in range(N)]

A.sort()
B = deque()

state = 1
for i in range(N // 2):
    if state:
        B.append(A[i])
        B.appendleft(A[-(i + 1)])
    else:
        B.append(A[-(i + 1)])
        B.appendleft(A[i])
    state ^= 1
if N % 2:
    median = A[N // 2]
    if abs(B[0] - median) < abs(B[-1] - median):
        B.append(median)
    else:
        B.appendleft(median)

ans = 0
for i in range(N - 1):
    ans += abs(B[i] - B[i + 1])
print(ans)

Submission status: Accepted

Solution code:
# coding:utf-8

import sys
from collections import Counter, deque

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return input()


N = I()
A = [I() for _ in range(N)]

A.sort()
B = deque()

state = 1
for i in range(N // 2):
    if state:
        B.append(A[i])
        B.appendleft(A[-(i + 1)])
    else:
        B.append(A[-(i + 1)])
        B.appendleft(A[i])
    state ^= 1
if N % 2:
    C = deque(B)
    B.append(A[N // 2])
    C.appendleft(A[N // 2])
else:
    C = A[:]

ans1 = 0
ans2 = 0
for i in range(N - 1):
    ans1 += abs(B[i] - B[i + 1])
    ans2 += abs(C[i] - C[i + 1])

print(max(ans1, ans2))

Submission status: Accepted

Solution code:
from itertools import accumulate


class CoeffTable:
    def __init__(self, n):
        self.n = n
        if n == 2:
            (self.p2, self.p1, self.m1, self.m2) = (0, 1, 1, 0)
        if n % 2 == 0:
            self.p1 = self.m1 = 1
            self.p2 = self.m2 = (n - 1) // 2
        else:
            self.p1 = 0
            self.m1 = 2
            self.p2 = (n - 1) // 2
            self.m2 = (n - 2) // 2
    def reverse(self):
        (self.p2, self.p1, self.m1, self.m2) = (
            self.m2,
            self.m1,
            self.p1,
            self.p2,
        )

    def cumsum(self):
        return list(accumulate([self.p2, self.p1, self.m1, self.m2]))



def main(n, seq):
    seq = sorted(seq, reverse=True)
    coeff = CoeffTable(n)
    cm = coeff.cumsum()
    ans1 = (
        2 * sum(seq[0 : cm[0]])
        + 1 * sum(seq[cm[0] : cm[1]])
        - 1 * sum(seq[cm[1] : cm[2]])
        - 2 * sum(seq[cm[2] : cm[3]])
    )
    coeff.reverse()
    rcm = coeff.cumsum()
    ans2 = (
        2 * sum(seq[0 : rcm[0]])
        + 1 * sum(seq[rcm[0] : rcm[1]])
        - 1 * sum(seq[rcm[1] : rcm[2]])
        - 2 * sum(seq[rcm[2] : rcm[3]])
    )
    ans = max(ans1, ans2)
    print(ans)
    return


if __name__ == "__main__":
    n = int(input())
    sample = []
    for _ in range(n):
        sample.append(int(input()))
    main(n, sample)

Submission status: Accepted

Solution code:
N = int(input())
A = []
for i in range(N):
    a = int(input())
    A.append(a)

A = sorted(A)

P1 = [0 for _ in range(N)]
for i in range(N):
    if i == 0:
        P1[0] = 1
    elif i%2 == 1:
        if i == N-1:
            P1[i] = -1
        else:
            P1[i] = -2
    else:
        if i == N - 1:
            P1[i] = 1
        else:
            P1[i] = 2

P1 = sorted(P1)

P2 = [0 for _ in range(N)]
for i in range(N):
    if i == 0:
        P2[0] = -1
    elif i%2 == 1:
        if i == N-1:
            P2[i] = 1
        else:
            P2[i] = 2
    else:
        if i == N - 1:
            P2[i] = -1
        else:
            P2[i] = -2

P2 = sorted(P2)

ans1 = 0
ans2 = 0
for i in range(N):
    ans1 += P1[i]*A[i]
    ans2 += P2[i]*A[i]

ans = max(ans1,ans2)

print(ans)

Submission status: Accepted

Solution code:
from collections import deque
n = int(input())

A = []
for _ in range(n):
    a = int(input())
    A.append(a)
A_ = deque()

l = 0
r = len(A)-1

A = sorted(A)

for _ in range(n):
    if len(A_) == 0:
        A_.append(A[r])
        r-=1
        continue

    todo = "ll"
    bestVal = abs(A[l]-A_[0])
    if(abs(A[l]-A_[-1]) > bestVal):
        bestVal = abs(A[l]-A_[-1])
        todo = "lr"
    if(abs(A[r]-A_[-1]) > bestVal):
        bestVal = abs(A[r]-A_[-1])
        todo = "rr"
    if(abs(A[r]-A_[0]) > bestVal):
        bestVal = abs(A[r]-A_[0])
        todo = "rl"

    if todo == "ll":
        A_.appendleft(A[l])
        l+=1
    if todo == "lr":
        A_.append(A[l])
        l+=1
    if todo == "rr":
        A_.append(A[r])
        r-=1
    if todo == "rl":
        A_.appendleft(A[r])
        r-=1
ans = 0
for i in range(len(A_)-1):
    ans += abs(A_[i+1]-A_[i])
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
As = []
for i in range(N):
    As.append(int(input()))
As.sort()
if N%2 == 0:
    s = 0
    for i in range(N):
        if i < N/2:
            s -= 2*As[i]
        else:
            s += 2*As[i]
    s = s + As[int(N/2)-1] -As[int(N/2)]
    print(s)
else:
    s1, s2 = 0, 0
    for i in range(N):
        if i < N//2 -1:
            s1 -= 2*As[i]
        elif N//2-1 <= i <=N//2 :
            s1 -= As[i]
        else:
            s1 +=  2*As[i]
        if i < N//2:
            s2 -= 2*As[i]
        elif N//2 <= i <= N//2 +1:
            s2 += As[i]
        else:
            s2 += 2*As[i]
    print(max(s1, s2))

Submission status: Accepted

Solution code:

N=input()
N=int(N)

As=[]
for i in range(N):
	As.append(int(input()))


As.sort()

l=[]
l.append(As[N//2])
#if(N%2==0):
s=0
b=0
for i in range(N-1):
	if(i%2==1):
		l.append(As[-b-1])
		b+=1
	else:
		l.append(As[s])
		s+=1
ans=0
for i in range(1,N):
	ans+=abs(l[i]-l[i-1])
#print(l)
#print(ans)

l2=[]
l2.append(As[(N-1)//2])
#if(N%2==0):
s2=0
b2=0
for i in range(N-1):
	if(i%2==0):
		l2.append(As[-b2-1])
		b2+=1
	else:
		l2.append(As[s2])
		s2+=1
ans2=0
for i in range(1,N):
	ans2+=abs(l2[i]-l2[i-1])
#print(l2)
#print(ans2)
print(max(ans,ans2))

Submission status: Accepted

Solution code:
n = int(input())
a = sorted(int(input()) for _ in range(n))
if n == 2:
    print(abs(a[0] - a[1]))
elif n%2 == 1:
    r1 = a[n//2] + a[n//2+1] + sum(a[n//2+2:])*2 - sum(a[:n//2])*2
    r2 = sum(a[n//2+1:])*2 - sum(a[:n//2-1])*2 - a[n//2-1] - a[n//2]
    print(max(r1, r2))
else:
    r1 = sum(a[n//2+1:])*2 + a[n//2] - sum(a[:n//2-1])*2 - a[n//2-1]
    print(r1)

Submission status: Accepted

Solution code:
from collections import *
a = sorted(int(input()) for _ in range(int(input())))
q = deque(a)
rq = deque([q.popleft()])
while q:
    rq.appendleft(q.pop())
    if q: 
        rq.append(q.pop())
    if q:
        rq.appendleft(q.popleft())
    if q: 
        rq.append(q.popleft())
r1 = sum(abs(i-j) for i, j in zip(rq, list(rq)[1:]))

q = deque(a[::-1])
rq = deque([q.popleft()])
while q:
    rq.appendleft(q.pop())
    if q: 
        rq.append(q.pop())
    if q:
        rq.appendleft(q.popleft())
    if q: 
        rq.append(q.popleft())
r2 = sum(abs(i-j) for i, j in zip(rq, list(rq)[1:]))
print(max(r1, r2))

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
a = sorted(a)
b = abs###
# min, max
p,q = a[0],a[-1]
res = b(p-q)
for i in range(n//2-1):
    # i+1, n-2-i
    res += b(p-a[n-2-i])
    res += b(q-a[i+1])
    p,q = a[i+1],a[n-2-i]
if n%2==1:
    c = a[n//2]
    res += max(b(p-c),b(q-c))
print(res)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**13
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n = I()
    a = sorted([I() for _ in range(n)])
    if n % 2 == 0:
        k = n // 2
        b = a[:k-1]
        c = a[k+1:]
        t = [0] * n
        t[0] = a[k]
        t[-1] = a[k-1]
        for i in range(k-1):
            t[i*2+1] = b[i]
            t[i*2+2] = c[i]
        r = 0
        for i in range(n-1):
            r += abs(t[i]-t[i+1])

        return r

    k = n // 2 + 1
    b = a[:k-1]
    c = a[k+1:]
    t = [0] * n
    t[0] = a[k]
    t[-1] = a[k-1]
    t[-2] = b[-1]
    for i in range(k-2):
        t[i*2+1] = b[i]
        t[i*2+2] = c[i]
    r = 0
    for i in range(n-1):
        r += abs(t[i]-t[i+1])

    b = a[:k-2]
    c = a[k:]
    t = [0] * n
    t[0] = a[k-1]
    t[-1] = a[k-2]
    t[-2] = c[-1]
    for i in range(k-2):
        t[i*2+1] = c[i]
        t[i*2+2] = b[i]
    tr = 0
    for i in range(n-1):
        tr += abs(t[i]-t[i+1])

    if r < tr:
        r = tr

    return r


print(main())

Submission status: Accepted

Solution code:
import itertools
import numpy as np
data_num = int(input()) 
data = [int(input()) for i in range(data_num)] 
#data_num=5
#data=[6,8,1,2,3]

data.sort()
npdata=np.array(data)

parts1=[1]
parts2=[2 for i in range(data_num-2)]
parts1.extend(parts2)
parts1.append(1)
npparts1=np.array(parts1)

parts0=[(-1)**(i+1) for i in range(data_num)]
npparts0=np.array(parts0)

y=npparts1*npparts0

y=y.tolist()
y.sort()

y=np.array(y)
z1=sum(data*y)

parts01=[(-1)**i for i in range(data_num)]
npparts0=np.array(parts01)

y=npparts1*npparts0

y=y.tolist()
y.sort()
y=np.array(y)
z2=sum(data*y)

print(max(z1,z2))

Submission status: Accepted

Solution code:
n = int(input())
A = [int(input()) for _ in range(n)]
A.sort()
if n%2==1:
    a = 2*sum(A[n-n//2:]) - sum(A[n-n//2-2:n-n//2]) - 2*sum(A[:n-n//2-2])
    b = -2*sum(A[:n//2]) + sum(A[n//2:n//2+2]) + 2*sum(A[n//2+2:])
    print(max(a,b))
else: print(-2*sum(A[:(n-2)//2])-A[(n-2)//2]+A[(n-2)//2+1]+2*sum(A[(n-2)//2+2:]))

Submission status: Accepted

Solution code:
n=int(input());A=[int(input()) for _ in range(n)];A.sort()
if n%2==1:print(max(2*sum(A[n-n//2:])-sum(A[n-n//2-2:n-n//2])-2*sum(A[:n-n//2-2]),-2*sum(A[:n//2])+sum(A[n//2:n//2+2])+2*sum(A[n//2+2:])))
else:print(-2*sum(A[:(n-2)//2])-A[(n-2)//2]+A[(n-2)//2+1]+2*sum(A[(n-2)//2+2:]))

Submission status: Accepted

Solution code:
n = int(input())
A = [int(input()) for _ in range(n)]
A.sort()
if n%2==1:print(max(2*sum(A[n-n//2:])-sum(A[n-n//2-2:n-n//2])-2*sum(A[:n-n//2-2]),-2*sum(A[:n//2])+sum(A[n//2:n//2+2])+2*sum(A[n//2+2:])))
else:print(-2*sum(A[:(n-2)//2])-A[(n-2)//2]+A[(n-2)//2+1]+2*sum(A[(n-2)//2+2:]))

Submission status: Accepted

Solution code:
a=[]
N = int(input())
for i in range(N):
    a.append(int(input()))
a=sorted(a)

if(len(a)==2):
    print(abs(a[0]-a[1]))
elif(len(a)==3):
    print(max(a[2] - a[1] + a[2] - a[0], a[1]-a[0] + a[2]-a[0]))
else:
    #----------------------------------------------------------------------
    def append2big(id1, id2):
        global bBeg
        global bEnd
        global s
        if (a[id1] - bBeg + a[id2] - bEnd < a[id2] - bBeg + a[id1] - bEnd):
            id1, id2 = id2, id1
        s+=a[id1] - bBeg + a[id2] - bEnd
        bBeg = a[id1]
        bEnd = a[id2]
    def append2small(id1, id2):
        global bBeg
        global bEnd
        global s
        if (bBeg - a[id1] + bEnd - a[id2] < bBeg - a[id2] + bEnd - a[id1]):
            id1, id2 = id2, id1
        s+=bBeg - a[id1] + bEnd - a[id2]
        bBeg = a[id1]
        bEnd = a[id2]
    def append1small(id):
        global bBeg
        global bEnd
        global s
        if (bBeg - a[id] > bEnd - a[id]):
            s+=bBeg-a[id]
            bBeg = a[id]
        else:
            s+=bEnd-a[id]
            bEnd = a[id]
    def append1big(id):
        global bBeg
        global bEnd
        global s
        if (a[id] - bBeg > a[id] - bEnd):
            s+=a[id]-bBeg
            bBeg = a[id]
        else:
            s+=a[id]-bEnd
            bEnd = a[id]
    # ----------------------------------------------------------------------
    s = a[N-2]-a[0] + a[N-1] - a[0]
    bBeg = a[N-2]
    bEnd = a[N-1]
    l = 0
    r = N-2
    cur = 0
    while(l<r-1):
        if(cur==0):
            if(l+2<r):
                append2small(l+1, l+2)
                l+=2
            elif(l+1<r):
                append1small(l+1)
                l+=1
            cur = 1-cur
        else:
            if(r-2>l):
                append2big(r-1, r-2)
                r-=2
            elif(r-1>l):
                append1big(r-1)
                r-=1
            cur = 1- cur
    r1 = s
    #-----case 2------
    s = a[N - 1] - a[0] + a[N - 1] - a[1]
    bBeg = a[0]
    bEnd = a[1]
    l = 1
    r = N - 1
    cur = 1
    while (l < r - 1):
        if (cur == 0):
            if (l + 2 < r):
                append2small(l + 1, l + 2)
                l += 2
            elif (l + 1 < r):
                append1small(l + 1)
                l += 1
            cur = 1 - cur
        else:
            if (r - 2 > l):
                append2big(r - 1, r - 2)
                r -= 2
            elif (r - 1 > l):
                append1big(r - 1)
                r -= 1
            cur = 1 - cur
    r2 = s

    print(max(r1, r2))

Submission status: Accepted

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

N = int(input())
a = [int(input()) for i in range(N)]
a.sort()

sum1 = a[N-1] - a[0]
sum2 = sum1
i = 0
while True:
    if i+1 == N-i-1:
        break
    sum1 += a[N-i-1] - a[i+1]
    if i+1 == N-i-2:
        break
    sum1 += a[N-i-2] - a[i]
    i += 1

i = 0
while True:
    if i == N-i-2:
        break
    sum2 += a[N-i-2] - a[i]
    if i+1 == N-i-2:
        break
    sum2 += a[N-i-1] - a[i+1]
    i += 1

max = sum1 if sum1 >= sum2 else sum2
print(max)

Submission status: Accepted

Solution code:
n = int(input())
a = [0] * n
for i in range(n):
  a[i] = int(input())

a.sort()

c0 = [0] * n
for i in range(n-1):
  if i%2:
    c0[i] += 1
    c0[i+1] += -1
  else:
    c0[i+1] += 1
    c0[i] += -1
    
c0.sort()

s0 = sum(i*j for i, j in zip(a, c0))
s1 = sum(-i*j for i, j in zip(a, c0[::-1]))

print(max(s0, s1))

Submission status: Accepted

Solution code:
N=int(input())
A=[]
for i in range(N):
    A.append(int(input()))
A.sort()
z=N//2
if not N%2==0:
    An=sum(A[z+2:])*2 - sum(A[:z-1])*2 - A[z-1] + A[z+1] + max(-A[z-1]+A[z], -A[z] + A[z+1])
else:
    An=sum(A[z+1:])*2-sum(A[:z-1])*2-A[z-1]+A[z]
print(An)

Submission status: Accepted

Solution code:
n = int(input())
lst = [int(input()) for _ in range(n)]
lst.sort()
center = n // 2
if n % 2:
  higher = lst[center + 1:]
  lower = lst[:center]
  ans = (sum(higher) - sum(lower)) * 2 - higher[0] + lower[-1] + max(lst[center] - lower[-1], higher[0] - lst[center])
else:
  higher = lst[center:]
  lower = lst[:center]
  ans = (sum(higher) - sum(lower)) * 2 - higher[0] + lower[-1]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for k in range(N)]
A = sorted(A)
k = N//2
if N%2 == 1:
    print(max(-2*sum(A[:k])+sum(A[k:k+2])+2*sum(A[k+2:]),-2*sum(A[:k-1])-sum(A[k-1:k+1])+2*sum(A[k+1:])))
else:
    print(-2*sum(A[:k-1])-A[k-1]+A[k]+2*sum(A[k+1:]))

Submission status: Accepted

Solution code:
n = int(input())
x = [int(input()) for i in range(n)]
x.sort()
if n%2:
	a = 2*sum(x[n-n//2:])-sum(x[n-n//2-2:n-n//2])-2*sum(x[:n-n//2-2])
	a2 = -2*sum(x[:n//2])+sum(x[n//2:n//2+2])+2*sum(x[n//2+2:])
	print(max(a,a2))
else: print(-2*sum(x[:(n-2)//2])-x[(n-2)//2]+x[(n-2)//2+1]+2*sum(x[(n-2)//2+2:]))

Submission status: Accepted

Solution code:
n = input()
arr = []
for i in range(n):
    arr.append(input())
arr.sort()
if n % 2 == 0:
    arr1 = arr[0:n/2]
    arr2 = arr[n/2:n]
    n1 = arr1[-1]
    n2 = arr2[0]
    arr1.remove(n1)
    arr2.remove(n2)
    print (sum(arr2)-sum(arr1)) * 2 + n2 - n1
else:
    arr1 = arr[0:n/2]
    arr2 = arr[n/2+1:n]
    n1 = arr1[-1]
    n2 = arr2[0]
    n3 = arr[n/2]
    arr1.remove(n1)
    arr2.remove(n2)
    print (sum(arr2)-sum(arr1)) * 2 + n2 - n1 + max(n2-n3,n3-n1)

Submission status: Accepted

Solution code:
n=int(input())
data=[int(input()) for i in range(n)]
data.sort()


ans=0
head=0
tail=n-1

tmp=n//2
for i in range(n-1):
    if(i%2==0):
        ans+=abs(data[tmp] - data[head])
        tmp=head
        head+=1
    else:
        ans+=abs(data[tmp] - data[tail])
        tmp=tail
        tail-=1

if(i%2==1):
    tmp_ans=ans
    ans=0
    head=0
    tail=n-1

    tmp=n//2
    for i in range(n-1):
        if(i%2==1):
            ans+=abs(data[tmp] - data[head])
            tmp=head
            head+=1
        else:
            ans+=abs(data[tmp] - data[tail])
            tmp=tail
            tail-=1
    ans=max([tmp_ans,ans])

print(ans)

Submission status: Accepted

Solution code:
all_num = int(input())
nums = []
for i in range(all_num):
    nums.append( int(input()) )
	
nums = sorted(nums)

sort_nums = []
loop_num = int(all_num/2)
for i in range( loop_num ):
    if i % 2 == 0:
        sort_nums.append(nums[i])
        sort_nums.insert(0,nums[-(i+1)])
    else:
        sort_nums.insert(0,nums[i])
        sort_nums.append(nums[-(i+1)])
    
if len( nums ) % 2 == 1:
    center_index = int(len(nums)/2)
    center_num = nums[center_index]
    if abs(center_num - sort_nums[0]) > abs(center_num - sort_nums[-1]):
        sort_nums.insert(0,center_num)
    else :
        sort_nums.append(center_num)


total = 0
for i in range( len(sort_nums)-1 ):
    total = total + abs(sort_nums[i] - sort_nums[i+1])

print(total)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for x in range(N)]
A.sort()

a = [1, -1]
listA = [0 for x in range(N)]
listB = [0 for x in range(N)]
for i in range(1, N):
    listA[i] += a[i % 2]
    # ビット反転　~
    listA[i - 1] += a[~(i % 2)]

    listB[i - 1] += a[i % 2]
    listB[i] += a[~(i % 2)]

listA.sort()
listB.sort()
listA = [x * y for (x, y) in zip(listA, A)]
listB = [x * y for (x, y) in zip(listB, A)]

ans = max(sum(listA), sum(listB))

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

L = []
for i in range(N):
    n = int(input())
    L.append(n)
    
L = sorted(L)
ans = []

if N % 2 == 0:
    A1 = L[0: N //2]
    A2 = L[N // 2:]
    ans.append(2 * sum(A2[1:]) + A2[0] - (2 * sum(A1[:-1]) + A1[-1]))
    
    A1 = L[0: N // 2]
    A2 = L[N // 2:]
    ans.append(2 * sum(A2[1:]) + A2[0] - (2 * sum(A1[:-1]) + A1[-1]))
    
    print(max(ans))
    
else:
    A1 = L[0: N // 2 + 1]
    A2 = L[N // 2 + 1:]
    ans.append(2 * sum(A2) - (2 * sum(A1[0: -2]) + A1[-1] + A1[-2]))
    
    A1 = L[0: N // 2]
    A2 = L[N // 2:]
    
    ans.append((2 * sum(A2[2:]) + A2[0] + A2[1]) - 2 * sum(A1))
    print(max(ans))

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
def solve():
    n = int(input())
    a = sorted(int(readline()) for _ in range(n))
    c0 = n // 2 - 1
    c1 = n // 2
    c2 = n // 2 + 1
    ans = 0
    if n & 1:
        ans = sum(a[c2 + 1:]) * 2 - sum(a[:c0]) * 2 - a[c0] + a[c2] + max(-a[c0] + a[c1], - a[c1] + a[c2])
    else:
        ans = sum(a[c1 + 1:]) * 2 - sum(a[:c0]) * 2 - a[c0] + a[c1]
    return ans
print(solve())