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: Wrong Answer

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

if n%2==1:
    ans1 = 2*sum(A[n//2+2:]) + sum(A[2:n//2+2]) -2*sum(A[:2])
    ans2 = 2*sum(A[n//2+1:]) - sum(A[n//2-1:n//2+1]) -2*sum(A[:n//2-1])
    print(max(ans1,ans2))
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: Wrong Answer

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(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: Wrong Answer

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(a[1]-a[0] + a[2]-a[0])
else:
    s = a[N-2]-a[0] + a[N-1] - a[0]
    bBeg = a[N-2]
    bEnd = a[N-1]
    #----------------------------------------------------------------------
    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]
    # ----------------------------------------------------------------------
    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
    print(s)

Submission status: Wrong Answer

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(a[1]-a[0] + a[2]-a[0])
else:
    #b = [a[N-2], a[0], a[N-1]]
    s = a[N-2]-a[0] + a[N-1] - a[0]
    bBeg = a[N-2]
    bEnd = a[N-1]
    l = 1
    r = N-3
    cur = 0
    while(l<=r):
        if(cur < 2):
            if(bBeg - a[l] > bEnd - a[l]):    #b[0] - a[l] > b[len(b)-1] - a[l]
                s += bBeg - a[l]
                bBeg = a[l] #b.insert(0, a[l])
            else:
                s += bEnd - a[l]
                bEnd = a[l] #b.append(a[l])
            l+=1
            cur+=1
        else:
            if(a[r] - bBeg > a[r] - bEnd):#(a[r] - b[0] > a[r] - b[len(b)-1]):
                s += a[r] - bBeg
                bBeg = a[r] #b.insert(0, a[r])
            else:
                s += a[r] - bEnd
                bEnd = a[r] #b.append(a[r])
            r-=1
            cur+=1;
            if(cur == 4):
                cur=0
    #for i in range(N-1):
    #    s += abs(b[i] - b[i+1])
    print(s)

Submission status: Wrong Answer

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(a[1]-a[0] + a[2]-a[0])
else:
    b = [a[N-2], a[0], a[N-1]]
    l = 1
    r = N-3
    cur = 0
    while(l<=r):
        if(cur < 2):
            if(b[0] - a[l] > b[len(b)-1] - a[l]):
                b.insert(0, a[l])
            else:
                b.append(a[l])
            l+=1
            cur+=1
        else:
            if(a[r] - b[0] > a[r] - b[len(b)-1]):
                b.insert(0, a[r])
            else:
                b.append(a[r])
            r-=1
            cur+=1;
            if(cur == 4):
                cur=0
    s=0
    for i in range(N-1):
        s += abs(b[i] - b[i+1])
    print(s)

Submission status: Wrong Answer

Solution code:
answer_list= []

n = int(input())
emp_list = [ int(input(i)) for i in range(n)]
emp_list.sort()

def min_list(the_list):
    if len(the_list) > 0:
        x = the_list.pop(0)
        return x
    else:
        pass

def max_list(the_list):
    if len(the_list) > 0:
        x = the_list.pop()
        return x
    else:
        pass

m = min_list(emp_list)
answer_list.append(m)

while emp_list != 0:
    ma = max_list(emp_list)
    answer_list.append(ma)
    if len(emp_list) == 0:
        break
    else:
        mb = max_list(emp_list)
        answer_list.insert(0,mb)
    if emp_list == 0:
        break
    else:
        na = min_list(emp_list)
        answer_list.append(na)
    if emp_list == 0:
        break
    else:
        nb = min_list(emp_list)
        answer_list.insert(0,nb)
print(answer_list)

Submission status: Wrong Answer

Solution code:
emp_list =[]
answer_list =[]

n = int(input())
for i in range(n):
    i = int(input())
    emp_list.append(i)
emp_list.sort()

def min_list(the_list):
    if len(the_list) > 0:
        x = the_list.pop(0)
        return x
    else:
        pass

def max_list(the_list):
    if len(the_list) > 0:
        x = the_list.pop()
        return x
    else:
        pass

m = min_list(emp_list)
answer_list.append(m)

while emp_list != 0:
    ma = max_list(emp_list)
    answer_list.append(ma)
    if len(emp_list) == 0:
        break
    else:
        mb = max_list(emp_list)
        answer_list.insert(0,mb)
    if emp_list == 0:
        break
    else:
        na = min_list(emp_list)
        answer_list.append(na)
    if emp_list == 0:
        break
    else:
        nb = min_list(emp_list)
        answer_list.insert(0,nb)
print(answer_list)

Submission status: Wrong Answer

Solution code:
emp_list =[]
answer_list =[]

n = int(input())
for i in range(n):
    i = int(input())
    emp_list.append(i)

def min_list(the_list):
    the_list.sort()
    if len(the_list) > 0:
        x = the_list.pop(0)
        return x
    else:
        pass

def max_list(the_list):
    the_list.sort()
    if len(the_list) > 0:
        x = the_list.pop()
        return x
    else:
        pass

m = min_list(emp_list)
answer_list.append(m)

while emp_list != 0:
    ma = max_list(emp_list)
    answer_list.append(ma)
    if len(emp_list) == 0:
        break
    else:
        mb = max_list(emp_list)
        answer_list.insert(0,mb)
    if emp_list == 0:
        break
    else:
        na = min_list(emp_list)
        answer_list.append(na)
    if emp_list == 0:
        break
    else:
        nb = min_list(emp_list)
        answer_list.insert(0,nb)
print(answer_list)

Submission status: Wrong Answer

Solution code:
emp_list =[]
answer_list =[]

n = int(input())
for i in range(n):
    i = int(input())
    emp_list.append(i)

def min_list(the_list):
    the_list.sort()
    if len(the_list) > 0:
        x = the_list.pop(0)
        return x
    else:
        pass

def max_list(the_list):
    the_list.sort()
    if len(the_list) > 0:
        x = the_list.pop()
        return x
    else:
        pass

m = min_list(emp_list)
answer_list.append(m)

while emp_list != 0:
    ma = max_list(emp_list)
    answer_list.append(ma)
    if len(emp_list) == 0:
        break
    elif emp_list != 0:
        mb = max_list(emp_list)
        answer_list.insert(0,mb)
    elif emp_list == 0:
        break
    if len(emp_list) != 0:
        na = min_list(emp_list)
        answer_list.append(na)
    elif emp_list == 0:
        break
    if emp_list != 0:
        nb = min_list(emp_list)
        answer_list.insert(0,nb)
    elif emp_list == 0:
        break
print(answer_list)

Submission status: Wrong Answer

Solution code:
emp_list =[]
answer_list =[]

n = int(input())

if 2 < n < 10**5:
    for i in range(n):
        i = int(input())
        emp_list.append(i)

def min_list(the_list):
    the_list.sort()
    if len(the_list) > 0:
        x = the_list.pop(0)
        return x
    else:
        pass

def max_list(the_list):
    the_list.sort()
    if len(the_list) > 0:
        x = the_list.pop()
        return x
    else:
        pass

m = min_list(emp_list)
answer_list.append(m)

while emp_list != 0:
    ma = max_list(emp_list)
    answer_list.append(ma)
    if len(emp_list) == 0:
        break
    elif emp_list != 0:
        mb = max_list(emp_list)
        answer_list.insert(0,mb)
    elif emp_list == 0:
        break

    if len(emp_list) != 0:
        na = min_list(emp_list)
        answer_list.append(na)
    elif emp_list == 0:
        break

    if emp_list != 0:
        nb = min_list(emp_list)
        answer_list.insert(0,nb)
    elif emp_list == 0:
        break

print(answer_list)

Submission status: Wrong Answer

Solution code:
emp_list =[]
answer_list =[]

n = int(input())

if 2 < n < 10**5:
    for i in range(n):
        i = int(input())
        emp_list.append(i)

#最小値を抜き出す関数
def min_list(the_list):
    the_list.sort()
    if len(the_list) > 0:
        x = the_list.pop(0)
        return x
    else:
        pass

#最大値を抜き出す関数
def max_list(the_list):
    the_list.sort()
    if len(the_list) > 0:
        x = the_list.pop()
        return x
    else:
        pass

m = min_list(emp_list)

answer_list.append(m)
#残りを最挿入していく。
while emp_list != 0:
    ma = max_list(emp_list)
    answer_list.append(ma)
    if len(emp_list) == 0:
        break
    elif emp_list != 0:
        mb = max_list(emp_list)
        answer_list.insert(0,mb)
    elif emp_list == 0:
        break

    if len(emp_list) != 0:
        na = min_list(emp_list)
        answer_list.append(na)
    elif emp_list == 0:
        break

    if emp_list != 0:
        nb = min_list(emp_list)
        answer_list.insert(0,nb)
    elif emp_list == 0:
        break

print(answer_list)

Submission status: Wrong Answer

Solution code:
n = int(input())
listx = [int(input()) for i in range(n)]

listx.sort()
listz=[]

z=listx.pop(-1)
listz.append(z)

z=listx.pop(0)
listz.append(z)

while len(listx)>0:
    max2 = abs(listz[0]-listx[0])
    min2 = abs(listx[-1]-listz[-1])

    if max2 > min2:
        z = listx.pop(0)
        listz.insert(0,z)
    else:
        z = listx.pop(-1)
        listz.append(z)

ans=0

for i in range(len(listz)-1):
    ans=ans+abs(listz[i]-listz[i+1])

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

def main(sorted_list):
    if len(sorted_list) == 2:
        print(sorted_list.pop()- sorted_list.popleft())
        return
    if len(sorted_list) == 3:
        print(max(sorted_list[1]+sorted_list[2]-sorted_list[0]*2, -sorted_list[1]-sorted_list[0]+sorted_list[2]*2))
        return
    max_num = sorted_list.pop()
    left = sorted_list.popleft()
    right = sorted_list.popleft()
    ans = 2*max_num - left - right
    status = 'max'

    while sorted_list:
        if status == 'max':
            num = sorted_list.pop()
            status = 'min'
            if left < right:
                ans += num - left
                left = num
            else:
                ans += num - right
                right = num
        else:
            num = sorted_list.popleft()
            status = 'max'
            if left < right:
                ans += right - num
                right = num
            else:
                ans += left - num
                left = num
    print(ans)


if __name__ == '__main__':
    n = int(input())
    sample = []
    for _ in range(n):
        sample.append(int(input()))
    sorted_list = deque(sorted(sample))
    main(sorted_list)

Submission status: Wrong Answer

Solution code:
import itertools
a=0
print(a)

Submission status: Wrong Answer

Solution code:
from statistics import median

n = int(input())
data = []
for i in range(n):
    data.append(int(input()))

med = median(data)

data = map(lambda x: x - med,data)
data = map(lambda x: abs(x),data)
data = list(data)
data.sort()

ans = 0
ans += sum(data[:2]+2*data[2:])
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
x = [int(input()) for i in range(n)]
x.sort()
def f(z):
	m = n+n%2-1
	y,a = [0]*m,0
	y[m//2] = z[0]
	for i in range(1,m//2+1):
		if i%2: y[m//2-i-1] = z[i]
		else: y[m//2+i] = z[i]
	for i in range(1,m//2+1):
		if i%2: y[m//2-i] = z[-i]
		else: y[m//2+i-1] = z[-i]
	for i in range(m-1): a+=abs(y[i]-y[i+1])
	if n%2-1: a+=max(abs(y[0]-z[n//2]),abs(y[-1]-z[n//2]))
	return a
print(max(f(x),f(x[::-1])))

Submission status: Wrong Answer

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

# 隣あう要素の差の絶対値を足し合わせる
def sum_diff(ary):
    re = 0
    pre_n = ary.pop(0)
    for n in ary:
        re += abs(n - pre_n)
        pre_n = n
    return re

n = int(input())
num = []
for i in range(n):
    m = int(input())
    num.append(m)

# num配列の順列生成
# a = itertools.permutations(num)
a = itertools.combinations(num, n)

# permutationオブジェクトをsetにしてtupleの重複排除、
# 更にmapで各要素をlist化
b = map(list, a)

c = map(sum_diff, b)
print(max(c))

Submission status: Wrong Answer

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

n = int(stdin.readline().rstrip())
a = [int(stdin.readline().rstrip()) for i in range(n)]
a_sort = sorted(a)
a_i = a_sort.pop(0)
a_j = 0
a_min = a_i
ans = 0

for i in range(n-2):
    if i%2 == 0:
        a_j = a_i
        a_i = a_sort.pop(-1)
        ans += abs(a_i - a_j)
        print(abs(a_i - a_j))
    elif i%2 == 1:
        a_j = a_i
        a_i = a_sort.pop(0)
        ans += abs(a_i - a_j)
        print(abs(a_i - a_j))

a_j = a_min
a_i = a_sort[0]
ans += abs(a_i - a_j)

print(ans)

Submission status: Wrong Answer

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:
    if loop_num % 2 == 1:
        center_index = int(len(nums)/2)
        sort_nums.append(nums[center_index])
    else :
        center_index = int(len(nums)/2)
        sort_nums.insert(0,nums[center_index])


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

print(total)

Submission status: Wrong Answer

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:
    if loop_num % 2 == 1:
        center_index = int(len(nums)/2)
        sort_nums.append(nums[center_index])
    else :
        center_index = int(len(nums)/2)
        sort_nums.insert(0,nums[center_index])


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

print( sort_nums )
print(total)

Submission status: Wrong Answer

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(len(nums)/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)
    sort_nums.append(nums[center_index])

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


print(total)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[]
for i in range(N):
    A.append(int(input()))
A.sort()
ans=0
ans+=A[-1]-A[0]
ans+=A[-2]-A[0]
A.remove(A[0])
while not A==[]:
    if len(A)<=2:
        break
    ans+=A[-1]-A[0]
    ans+=A[-2]-A[1]
    A.remove(A[-1])
    A.remove(A[-1])
    if len(A)<=2:
        break
    ans+=A[-1]-A[0]
    ans+=A[-2]-A[1]
    A.remove(A[0])
    A.remove(A[0])
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[]
for i in range(N):
    A.append(int(input()))
A.sort()
ans=0
ans+=abs(A[0]-A[-1])
ans+=abs(A[0]-A[-2])
A.remove(A[0])
while not A==[]:
    if len(A)<=2:
        break
    ans+=abs(A[-1]-A[0])
    ans+=abs(A[-2]-A[1])
    A.remove(A[-1])
    A.remove(A[-1])
    if len(A)<=2:
        break
    ans+=abs(A[0]-A[-1])
    ans+=abs(A[1]-A[-2])
    A.remove(A[0])
    A.remove(A[0])
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[]
for i in range(N):
    A.append(int(input()))
B=[]
S=len(A)
while not A==[]:
    if not S%2==0:
        B.append(min(A))
        A.remove(min(A))
        if A==[]:
            break
        
        B.insert(0,max(A))
        A.remove(max(A))
        if A==[]:
            break
        B.append(max(A))
        A.remove(max(A))
    else:
        B.append(max(A))
        A.remove(max(A))
        if A==[]:
            break
        B.append(min(A))
        A.remove(min(A))
        if A==[]:
            break
        B.insert(0,min(A))
        A.remove(min(A))
B.append(B[-1])
Ans=0
for i in range(N):
    Ans+=abs(B[i]-B[i+1])
print(Ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = [0] * n
for i in range(n):
    l[i] = int(input())
l.sort(reverse=True)
ans1 = 0
ans2 = 0
if n % 2 == 1:
    for i in range((n-3)//2):
        ans1 += 2 * l[i]
    ans1 += l[(n-3)//2] + l[(n-3)//2+1]
    for i in range((n-3)//2+2, n):
        ans1 -= 2 * l[i]

    for j in range((n-1)//2):
        ans2 += 2 * l[j]
    ans2 -= l[(n-1)//2] + l[(n-1)//2+1]
    for j in range((n-1)//2+2, n):
        ans2 -= 2 * l[i]

else:
    for i in range(((n-2)//2)):
        ans1 += 2 * l[i]
    ans1 += l[(n-2)//2]
    ans1 -= l[(n-2)//2+1]
    for i in range((n-2)//2+2, n):
        ans1 -= 2 * l[i]

ans = max(ans1, ans2)
print(ans)

Submission status: Wrong Answer

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

if N % 2 == 0:
    print(2 * (sum(A[N // 2:]) - sum(A[:N // 2])) - (A[N // 2] - A[N // -1]))
else:
    print(2 * (sum(A[N // 2 + 1:]) - sum(A[:N // 2])) - min(A[N // 2] - A[N // 2 - 1], A[N // 2 + 1] - A[N // 2]))

Submission status: Wrong Answer

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

if N % 2 == 0:
    ans = 2 * (sum(A[N // 2:]) - sum(A[:N // 2])) - (A[N // 2] - A[N // -1])
else:
    ans = 2 * (sum(A[N // +1:]) - sum(A[:N // 2])) - min(A[N // 2] - A[N // 2 - 1], A[N // 2 + 1] - A[N // 2])

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = []

for _ in range(N):
    A.append(int(input()))

A.sort()

i = 0
j = N-1
M = 0

while i < j:
    M += A[j] - A[i]
    i += 1
    j -= 1

i = 1
j = N-1

while i < j:
    M += A[j] - A[i]
    i += 1
    j -= 1

print(M)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = []

for _ in range(N):
    A.append(int(input()))

A.sort()

i = 0
j = N-1
M = 0

while i < j:
    M += A[j] - A[i]
    i += 1
    j -= 1

i = 1
j = N-1

while i < j:
    M += A[j] - A[i]
    i += 1
    j -= 1

Submission status: Wrong Answer

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


def main():
    from collections import deque

    n = int(input())
    a = sorted([int(input()) for _ in range(n)])
    center = n // 2
    former_a = a[:center]
    latter_a = a[center:][::-1]
    new_a = [0] * n

    for i in range(len(latter_a)):
        new_a[2 * i] = latter_a[i]

    for j in range(len(former_a)):
        new_a[2 * j + 1] = former_a[j]

    diff = abs(new_a[0] - new_a[1])
    d = deque()
    d.append(new_a[0])
    d.append(new_a[1])

    for k in new_a[2:]:
        left = abs(k - d[0])
        right = abs(k - d[-1])

        if left > right:
            d.appendleft(k)
            diff += left
        else:
            d.append(k)
            diff += right

    print(diff)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# 1 2 3 4
# 4 1 3 2

# 1 2 3 4 5
# 2 5 1 4 3

import sys
from functools import reduce


def calindex(N):
    if N == 1:
        return [0]
    elif N == 2:
        return [0, 1]
    elif N > 2:
        result = [0]
        tmpCount = 0
        reverseFlag = True
        nonReverseCount = 0
        reverseCount = 0
        for i in range(1, N):
            if tmpCount >= 2:
                reverseFlag = not reverseFlag
                tmpCount = 0
            if reverseFlag:
                index = N - i + nonReverseCount
                reverseCount += 1
            else:
                index = i - reverseCount
                nonReverseCount += 1
            if tmpCount == 0:
                result.insert(0, index)
            elif tmpCount == 1:
                result.append(index)
            tmpCount += 1
        return result


def main(A):
    indexmap = calindex(len(A))
    result = [A[i] for i in indexmap]
    return sum([abs(result[i] - result[i+1]) for i in range(len(result) - 1)])

if __name__ == "__main__":
    N = int(sys.stdin.readline())
    A = [int(sys.stdin.readline()) for x in range(N)]
    print(main(sorted(A)))

Submission status: Wrong Answer

Solution code:
import itertools
n=int(input())
string_list=[input() for i in range(n)]

# max min

int_list = [int(s) for s in string_list]
int_list.sort()
big = int_list[int(n)//2:]
min = int_list[:int(n)//2]
# sort
ans = 0
for i, v in enumerate(list(itertools.permutations(big))):
    for _, u in enumerate(list(itertools.permutations(min))):
        ans = sum(v)*2 - sum(u)*2
        if n%2==0:
            ans += u[-1] - v[0]
        else:
            ans -= sum(v[:2])

        print(ans)

Submission status: Wrong Answer

Solution code:
x =input()
x = int(x)
y = []
for i in range(x):
    z = input()
    z = int(z)
    y.append(z)
y = sorted(y)
sum = float(0)
left =[]
right = []
center = y[0]
for i in range(int(x/2)):
    left.append(y[2*i+1])
    if x <= 2*i+2: break
    right.append(y[2*i+2])
b = len(left)
prev = y[0]
for i in range(b):
    if not left: break
    a =left.pop()
    sum += abs(prev - a)
    prev = a
    if not left : break
    a= left.pop(0)
    sum += abs(prev - a)
    prev = a
b = len(right)
prev = y[0]
for i in range(b):
    if not right: break
    a =right.pop()
    sum+= abs(prev-a)
    prev = a
    if not right : break
    a= right.pop(0)
    sum+= abs(prev-a)
    prev = a
print(int(sum))

Submission status: Wrong Answer

Solution code:
import itertools

n=int(input())
string_list=[input() for i in range(n)]

# max min

int_list = [int(s) for s in string_list]
int_list = sorted(int_list)
int_list.reverse()
big = int_list[:len(int_list)//2]
min = sorted(int_list)[:(len(big) if len(int_list) % 2 == 0 else len(big)+1)]

# sort
result = 0
for i, v in enumerate(list(itertools.permutations(big))):
    for _, u in enumerate(list(itertools.permutations(min))):
        result_array = []
        result_buf = 0
        for i, (bigger, minner) in enumerate(itertools.zip_longest(v, u)):
            result_array.append(minner)
            if bigger != None:
                result_array.append(bigger)
                try:
                    result_buf += (bigger - minner) + (bigger - u[i+1])
                except IndexError:
                    result_buf += bigger - minner
                    break

        if result_buf > result:
            result = result_buf

print(result)

Submission status: Wrong Answer

Solution code:
5
6
8
1
2
3

Submission status: Wrong Answer

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

PLUS = A[int((N+1)//2):]
MINUS = A[:int((N+1)//2)]
#print(PLUS, MINUS)
ans = sum(PLUS)*2 - sum(MINUS)*2
if N%2==0:
    ans += MINUS[-1] - PLUS[0]
else:
    ans += sum(MINUS[-2:])
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
N = input()
A = [int(input()) for i in range(N)]
coef = [2] * N
coef[0] = 1
coef[-1] = 1
coef1 = [i if i % 2 == 0 else i * -1 for i in coef]
coef2 = [i if i % 2 != 0 else i * -1 for i in coef]

A = sorted(A)
coef1 = sorted(coef1)
coef2 = sorted(coef2)
res1 = 0
for i in range(N):
	res1 += A[i] * coef1[i]
res2 = 0
for i in range(N):
	res2 += A[i] * coef2[i]
print max(res1, res2)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = []
for i in range(n):
    A.append(int(input()))
    
A.sort()
B = []
left = 0
right = 0
res = 0
for i in range(n):
    if(i==0):
        tmp = A.pop(0)
        left = tmp
        right = tmp
    elif(i==n-1):
        if(i%2==0):
            tmp = A.pop()
            res += abs(left-tmp)
        else:
            tmp = A.pop()
            if(abs(right-tmp)>abs(left-tmp)):
                res += abs(right-tmp)
            else:
                res += abs(left-tmp)
    elif(not(i%4==0 or (i+1)%4==0)):
        if(i%2==1):
            tmp = A.pop()
            res += abs(right-tmp)
            right = tmp
        else:
            tmp = A.pop()
            res += abs(left-tmp)
            left = tmp
    else:
        if(i%2==1):
            tmp = A.pop(0)
            res += abs(right-tmp)
            right = tmp
        else:
            tmp = A.pop(0)
            res += abs(left-tmp)
            left = tmp

print(res)

Submission status: Wrong Answer

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

B = sorted(A)
n = (N-1)//2
m = N//2
a = B[-1][0]+B[-2][0]-2*B[0][0]
b = B[-1][0]+B[-2][0]-2*B[0][0]
if N == 2:
    print([1][0]-B[0][0])
if N == 3:
    a = B[1][0]+B[2][0]-2*B[0][0]
    b = 2*B[2][0]-B[0][0]-B[1][0]
    print(max(a,b))
if N == 4:
    a = 2*B[-1][0]+B[-2][0]-2*B[0][0]-B[1][0]
    b = 2*B[-1][0]-2*B[0][0]-B[1][0]+B[-2][0]
    print(max(a,b))
if N == 5:
    a = 2*B[-1][0]+2*B[-2][0]-2*B[0][0]-B[1][0]-B[2][0]
    b = 2*B[-1][0]+B[-2][0]+B[-3][0]-2*B[0][0]-2*B[1][0]
    print(max(a,b))
if (N%2 ==1) and (N >=6):
    for i in range(1,n-1):
        a = a + B[-i][0] - B[i][0] + B[-i-2][0] - B[i][0]
    for j in range(1,n):
        b = b + B[-i][0] - B[i][0] + B[-i-2][0] - B[i][0]
    a = a + B[-(n-1)][0]-B[n-1][0]+B[-(n)][0]-B[n][0]
    c = b + B[-n+1][0] - B[n][0]
    print(max(a,b,c))
if (N%2 ==0) and (N>=6):
    for i in range(1,m-1):
        a = a + B[-i][0] - B[i][0] + B[-i-2][0] - B[i][0]
    print(a + B[-(m-1)][0] - B[m-1][0])

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
A.sort()
l,l_1,l_2 = [],[],[]
if len(A) % 2 == 0:  
  for i in range(len(A)):
    if i <= len(A)/2 - 2:
      l.append(-2)
    elif i == len(A)/2 -1:
      l.append(-1)
    elif i == len(A)/2:
      l.append(1)
    else:
      l.append(2)
  ans_d = [A[i]*l[i] for i in range(len(A))]
  print(sum(ans_d))
if len(A) % 2 == 1:
  for i in range(len(A)):
    if i <= int(len(A)/2 -2):
      l_1.append(2)
      l_2.append(-2)
    elif int(len(A)/2 -1) <= i <= int(len(A)/2):
      l_1.append(1)
      l_2.append(-1)
    else:
      l_1.append(-2)
      l_2.append(2)
  l_1.sort()
  l_2.sort()
  ans_d1 = [A[i]*l_1[i] for i in range(len(A))]
  ans_d2 = [A[i]*l_2[i] for i in range(len(A))]
  print(max(sum(ans_d1),sum(ans_d2)))