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())
lis=[]
for i in range(N):
    lis.append(int(input()))
lis.sort()
res=[lis.pop(0)]
while True:
    res.append(lis.pop(-1))
    if len(lis)==0:break
    res.insert(0,lis.pop(-1))
    if len(lis)==0:break
    res.insert(0,lis.pop(0))
    if len(lis)==0:break
    res.append(lis.pop(0))
    if len(lis)==0:break

    res.insert(0,lis.pop(-1))
    if len(lis)==0:break        
    res.append(lis.pop(-1))
    if len(lis)==0:break
    res.append(lis.pop(0))
    if len(lis)==0:break
    res.insert(0,lis.pop(0))
    if len(lis)==0:break

n=0
for i in range(N-1):
    n+=abs(res[i]-res[i+1])
print(res,n)

Submission status: Wrong Answer

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

def diff(L):
    total=0
    for i in range(len(L)-1):
        total+=abs(L[i+1]-L[i])
    return total
for i in range(N):
    A=input()
    L.append(int(A))
L.sort()
if len(L)>3:
    length=len(L)
    SL=L[:int(length/2)]
    LL=L[int(length/2):]
    print(SL)
    print(LL)
    result=[]
    result.append(SL[-1])
    SL.pop(-1)
    last=LL[0]
    LL.pop(0)
    while True:
        try:
            result.append(LL[-1])
            LL.pop(-1)
            result.append(SL[0])
            SL.pop(0)
        except:
            break

    result.append(last)


    print(result)
    print(diff(result))    
elif len(L)==3:
    all=[diff([L[0],L[1],L[2]]),diff([L[0],L[2],L[1]]),diff([L[1],L[0],L[2]]),diff([L[1],L[2],L[0]]),diff([L[2],L[0],L[1]]),diff([L[2],L[1],L[0]])]
    print(max(all))
elif len(L)==2:
    print(abs(L[0]-L[1]))

Submission status: Wrong Answer

Solution code:
n = int(input())

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

a.sort()

ans = 0

if n % 2 == 1:
    b = a[0]*2
    a.pop(0)
    c = 0
    for i in range(n//2):
        if i % 2 == 0:
            c += a.pop(-1)
            c += a.pop(-1)
            ans += abs(b-c)
            b = c
            c = 0
        else:
            c += a.pop(0)
            c += a.pop(0)
            ans += abs(b-c)
            b = c
            c = 0
elif n == 2:
    ans = abs(a[0]-a[1])
else:
    b = a[0]*2
    a.pop(0)
    c = 0
    d = 0
    e = 0
    for i in range(n//2-1):
        if i == n//2 -2:
            d = a[0]
            e = a[-1]
        if i % 2 == 0:
            c += a.pop(-1)
            c += a.pop(-1)
            ans += abs(b-c)
            b = c
            c = 0
        else:
            c += a.pop(0)
            c += a.pop(0)
            ans += abs(b-c)
            b = c
            c = 0
    ans += max(abs(a[0]-d),abs(a[0]-e))

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = []
flag=0
ans=0
anslist=[]
nlen=len(A)
setting=0
for i in range(N):
    A.append(int(input()))
A.sort()
if N%2==0:
    anslist.append(A[-1])
    del A[-1]
else:
    anslist.append(A[0])
    del A[0]
    setting=1
try:
    if N%2==0:
        while True:
            if flag==0:
                for i in range(2):
                    anslist.append(A[0])
                    anslist.reverse()
                    del A[0]
                flag=1
            else:
                for i in range(2):
                    anslist.append(A[-1])
                    anslist.reverse()
                    del A[-1]
                flag=0
    else:
        while True:
            if flag!=0:
                for i in range(2):
                    anslist.append(A[0])
                    anslist.reverse()
                    del A[0]
                flag=1
            else:
                for i in range(2):
                    anslist.append(A[-1])
                    anslist.reverse()
                    del A[-1]
                flag=0


except Exception as e:
    #print (anslist)
    flag=0
    try:
        while True:
            ans=abs(anslist[flag]-anslist[flag+1])+ans
            flag=flag+1
    except Exception as e:
        print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(0 for i in range(n))
b = list(0 for i in range(n))

if n%2==0:
    cent  = int(n/2)
else:
    cent = int((n-1)/2)

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

for i in range(n):
    if i==0:
        b[cent] = a[i]
    elif i%4==1:
        if i == n-1:
            b[0] = a[n-1-int(i/2)]
        else:
            b[cent-int((i+1)/2)] = a[n-1-int(i/2)]
            b[cent+int((i+1)/2)] = a[n-1-int((i+1)/2)]
    elif i%4==3:
        if i == n-1:
            b[0] = a[int(i/2)]
        else:
            b[cent-int((i+1)/2)] = a[int(i/2)]
            b[cent+int((i+1)/2)] = a[int((i+1)/2)]    

sum = 0
for i in range(1,n):
    sum += abs(b[i]-b[i-1])
print(sum)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

a.sort()
a = [i+1 for i in range(999)]
n = 999
ans = 0

if n % 2 == 1:
    b = a[0]*2
    a.pop(0)
    c = 0
    for i in range(n//2):
        if i % 2 == 0:
            c += a.pop(-1)
            c += a.pop(-1)
            ans += abs(b-c)
            b = c
            c = 0
        else:
            c += a.pop(0)
            c += a.pop(0)
            ans += abs(b-c)
            b = c
            c = 0
elif n == 2:
    ans = abs(a[0]-a[1])
else:
    b = a[0]*2
    a.pop(0)
    c = 0
    d = 0
    e = 0
    for i in range(n//2-1):
        if i == n//2 -2:
            d = a[0]
            e = a[-1]
        if i % 2 == 0:
            c += a.pop(-1)
            c += a.pop(-1)
            ans += abs(b-c)
            b = c
            c = 0
        else:
            c += a.pop(0)
            c += a.pop(0)
            ans += abs(b-c)
            b = c
            c = 0
    ans += max(abs(a[0]-d),abs(a[0]-e))

print(ans)

Submission status: Wrong Answer

Solution code:
def solve(pos):
    start = pos
    ans = 0
    a = 0
    b = N - 1
    for i in range(N-1):
        if a==start:
            a += 1
        elif b==start:
            b -= 1
        if abs(A[a]-A[pos])>=abs(A[b]-A[pos]):
            ans += abs(A[a]-A[pos])
            pos = a
            a += 1
        else:
            ans += abs(A[b]-A[pos])
            pos = b
            b -= 1
        #print(ans, a, b)
    #print(ans)
    return ans

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

pos_list = [0]
if N%2==0:
    pos_list.append(int(N//2)-1)
    pos_list.append(int(N//2))
else:
    pos_list.append(int(N//2))
pos_list.append(N-1)
#print(pos_list)

ans = 0
for pos in pos_list:
    tmp = solve(pos)
    ans = max(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = []
flag=0
ans=0
anslist=[]
nlen=len(A)
setting=0
for i in range(N):
    A.append(int(input()))
A.sort()
if N%2==0:
    anslist.append(A[-1])
    del A[-1]
else:
    anslist.append(A[0])
    del A[0]
    setting=1
try:
    if N%2==0:
        while True:
            if flag==0:
                for i in range(2):
                    anslist.append(A[0])
                    anslist.reverse()
                    del A[0]
                flag=1
            else:
                for i in range(2):
                    anslist.append(A[-1])
                    anslist.reverse()
                    del A[-1]
                flag=0
    else:
        while True:
            if flag!=0:
                for i in range(2):
                    anslist.append(A[0])
                    anslist.reverse()
                    del A[0]
                flag=1
            else:
                for i in range(2):
                    anslist.append(A[-1])
                    anslist.reverse()
                    del A[-1]
                flag=0


except Exception as e:
    print (anslist)
    flag=0
    try:
        while True:
            ans=abs(anslist[flag]-anslist[flag+1])+ans
            flag=flag+1
    except Exception as e:
        print(ans)

Submission status: Wrong Answer

Solution code:
def neighbor_sum(N, A):
    A_1 = [] 
    A_2 = []
    i = 0
    j = N-1
    change = True
    while i < j:
        if change == True:
            A_1.append(A[i])
            A_2.append(A[j])
            i += 1
            j -= 1
            change = False
        else:
            A_1.append(A[j])
            A_2.append(A[i])
            i += 1
            j -= 1
            change = True
        # N == odd 
    if N % 2 == 1:
        if (A[N//2] - A[0]) > (A[N-1] - A[N//2]):
            A_1.append(A[N//2])
        else:
            A_2.append(A[N//2])

    return A_1[::-1] + A_2[:]

if __name__ == "__main__":
    N = int(input())
    A = [int(input()) for _ in range(N)]
    A.sort()
    diff = neighbor_sum(N, A)
    diff_sum = [abs(diff[i+1] - diff[i]) for i in range(N-1)]
    print(sum(diff_sum))

Submission status: Wrong Answer

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

a.sort()
a2 = a[:]

ans = 0
l = a[0]
r = a[0]
a.pop(0)
for i in range(0, n-1):
    if (i % 4 == 0):
        ans += a[-1] - l
        l = a[-1]
        a.pop(-1)
    if (i % 4 == 1):
        ans += a[-1] - r
        r = a[-1]
        a.pop(-1)
    if (i % 4 == 2):
        ans += l - a[0]
        l = a[0]
        a.pop(0)
    if (i % 4 == 3):
        ans += r - a[0]
        r = a[0]
        a.pop(0)

ans2 = 0
l2 = a2[-1]
r2 = a2[-1]
a2.pop(-1)
for i in range(0, n-1):
    if (i % 4 == 0):
        ans2 += l - a2[0]
        l = a2[0]
        a2.pop(0)
    if (i % 4 == 1):
        ans2 += r - a2[0]
        r = a2[0]
        a2.pop(0)
    if (i % 4 == 2):
        ans2 += a2[-1] - l
        l = a2[-1]
        a2.pop(-1)
    if (i % 4 == 3):
        ans2 += a2[-1] - r
        r = a2[-1]
        a2.pop(-1)
print(max(ans, ans2))

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())

    num_l = list()
    for _ in range(n):
        num_l += [int(input())]

    num_l = sorted(num_l)

    row = list()

    f = num_l.pop(0)
    s = f
    max_num = 0
    flag = True
    while(num_l):
        if len(num_l) == 1:
            if abs(f - num_l[0]) > abs(s - num_l[0]):
                max_num += abs(f - num_l[0])
            else:
                max_num += abs(s - num_l[0])
            break

        if flag:
            tmp_f = num_l.pop(len(num_l)-1)
            tmp_s = num_l.pop(len(num_l)-1)
        else:
            tmp_s = num_l.pop(0)
            tmp_f = num_l.pop(0)

        if abs(f - tmp_f) + abs(s - tmp_s) > abs(s - tmp_f) + abs(f - tmp_s):
            max_num += abs(f - tmp_f) + abs(s - tmp_s)
        else:
            max_num += abs(s - tmp_f) + abs(f - tmp_s)

        f = tmp_f
        s = tmp_s

        flag = not flag

    print(max_num)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:

N = int(input())
Alist=[]
# Blist=[]
for i in range(0,N):
    Alist.append(int(input()))

Alist.sort()
# print(Alist)

back=N-1

numA=Alist[0]
numB=Alist[back]
ans = abs(numB-numA)
back-=1
# print(ans)

for i in range(1,int(N/2)):
    if abs(numA-Alist[i]) > abs(numB-Alist[i]):
        ans+=abs(numA-Alist[i])
        numA=Alist[i]
    else:
        ans+=abs(numB-Alist[i])
        numB=Alist[i]
    # print(ans)

    if abs(numA-Alist[back]) > abs(numB-Alist[back]):
        ans+=abs(numA-Alist[back])
        numA=Alist[back]
    else:
        ans+=abs(numB-Alist[back])
        numB=Alist[back]
    # print(ans)
    back-=1

# print("l-")

if N%2 == 1:
    k=int(N/2)
    # print(k)
    if abs(numA-Alist[k]) > abs(numB-Alist[k]):
        ans+=abs(numA-Alist[k])
        numA=Alist[k]
    else:
        ans+=abs(numB-Alist[k])
        numB=Alist[k]
    # print(ans)

# print("a-")
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
n = int(input())
A = deque(sorted(int(input()) for _ in range(n)))
def f(x):
    cnt = 0
    for i in range(len(x) - 1):
        cnt += abs(x[i] - x[i + 1])
    return cnt
ans1 = deque([A[0]])
ans2 = deque([A[0]])
a = 1
b = n - 1
for i in range((n - 1) // 2):
    if i % 2 == 1:
        ans1.appendleft(A[a])
        a += 1
        ans1.append(A[a])
        a += 1
    else:
        ans1.appendleft(A[b])
        b -= 1
        ans1.append(A[b])
        b -= 1
if n % 2 == 0:
    ans1.append(A[a])
a = b = 1
for i in range((n - 1) // 2):
    if i % 2 == 1:
        ans2.append(A[a])
        a += 1
        ans2.appendleft(A[a])
        a += 1
    else:
        ans2.append(A[b])
        b -= 1
        ans2.appendleft(A[b])
        b -= 1
if n % 2 == 0:
    ans2.append(A[a])
print(max(f(ans1), f(ans2)))

Submission status: Wrong Answer

Solution code:
N = int(input())
num_list = [int(input()) for _ in range(N)]
num_list.sort()
if N == 2:
    print(num_list[1]-num_list[0])
    exit()
if N == 3:
    print(num_list[1]+num_list[2]-2*num_list[0])
    exit()
if N % 2:
    hazi = num_list.pop((N-3)//2) + num_list.pop((N-3)//2)
    ans = 2 * (sum(num_list[(N-3)//2:])-sum(num_list[:(N-3)//2])) - hazi
else:
    hazi = -num_list.pop((N-2)//2) + num_list.pop((N-2)//2)
    ans = 2 * (sum(num_list[(N-2)//2:])-sum(num_list[:(N-2)//2])) + hazi
print(ans)

Submission status: Wrong Answer

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

a = 0
b = -1
ans = 0
last = 0
for i in range(N-2):
    ans += (A[a] - A[b])
    if i % 2 == 0:
        a += 1
        last = A[a]
    else:
        b -= 1
        last = A[b]
ans += (A[0] - last)
print(ans)

Submission status: Wrong Answer

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

flag = 0
ans = 0
head = 0
tail = 0
while len(A) > 0:
    if flag%4 == 0:
        if flag:
            ans += abs(A[0] - head)
            head = A[0]
        else:
            head = tail = A[0]
        A.pop(0)
    elif flag%4 == 1:
        ans += abs(A[-1] - tail)
        tail = A[-1]
        A.pop()
    elif flag%4 == 2:
        ans += abs(A[-1] - head)
        head = A[-1]
        A.pop()
    else:
        ans += abs(A[0] - tail)
        tail = A[0]
        A.pop(0)
    flag += 1

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = []
for i in range(N):
	A.append(int(input()))
A.sort()
B = sorted(A,reverse  = True)

if N % 2  ==1:
	ans = A[N-1]+A[N-2]-A[0]-A[0]
	for i in range((N-1)//2-1):
		ans += B[2*i+1]+B[2*i]-A[2*i+1]-A[2*i+2]
else:
	ans = A[N-1]+A[N-2]-A[0]-A[0]
	for i in range((N-1)//2-1):
		
		ans += B[2*i+1]+B[2*i]-A[2*i+1]-A[2*i+2]
	ans += A[N//2]-A[N//2-2]
if N == 2:
	ans = abs(A[0]-A[1])
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
a = []
for _ in range(n):
    a.append(int(input()))

a.sort()
# print(a)
b = [0 for _ in range(n)]
b[n//2] = a[0]
left = 1
right = n-1
for i in range(n//2):
    if i%2==0:
        b[n//2-(i+1)] = a[right]
        right -= 1
        if n//2+(i+1)<n:
            b[n//2+(i+1)] = a[right]
            right -= 1
    else:
        if n//2+(i+1)<n:
            b[n//2+(i+1)] = a[left]
            left += 1
        b[n//2-(i+1)] = a[left]
        left += 1
ans1 = 0
for i in range(n-1):
    ans1 += abs(b[i]-b[i+1])



b = [0 for _ in range(n)]
b[n//2] = a[0]
left = 1
right = n-1
for i in range(n//2):
    if i%2==0:
        b[n//2-(i+1)] = a[right]
        right -= 1
        if n//2+(i+1)<n:
            b[n//2+(i+1)] = a[right]
            right -= 1
    else:
        b[n//2-(i+1)] = a[left]
        left += 1
        if n//2+(i+1)<n:
            b[n//2+(i+1)] = a[left]
            left += 1
ans2 = 0
for i in range(n-1):
    ans2 += abs(b[i]-b[i+1])

print(max(ans1,ans2))

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline
n = int(input())
a_list = []
for _ in range(n):
    a = int(input())
    a_list.append(a)
a_list.sort()
if n%2 == 0:
    a_fw = a_list[:int(n/2)]
    a_ba = a_list[int(n/2):]
    fw_list = []
    ba_list = []
    for i in range(int(n/2)):
        fw_list.append(a_fw[i])
        fw_list.append(a_ba[i])
        ba_list.append(a_ba[i])
        ba_list.append(a_fw[i])
    v_fw = 0
    check_fw = fw_list[0]
    v_ba = 0
    check_ba = ba_list[0]
    for i in range(1, n):
        v_fw += abs(check_fw-fw_list[i])
        check_fw = fw_list[i]
        v_ba += abs(check_ba-ba_list[i])
        check_ba = ba_list[i]
else:
    a_fw = a_list[:int(n/2)]
    a_ba = a_list[int(n/2)+1:]
    a_ba.sort()
    check_fw, check_ba = a_list[int(n/2)], a_list[int(n/2)]
    fw_list = [a_list[int(n/2)]]
    ba_list = [a_list[int(n/2)]]
    for i in range(int(n/2)):
        fw_list.append(a_fw[i])
        fw_list.append(a_ba[i])
        ba_list.append(a_ba[i])
        ba_list.append(a_fw[i])
    v_fw = 0
    v_ba = 0
    for i in range(1, n):
        v_fw += abs(check_fw-fw_list[i])
        check_fw = fw_list[i]
        v_ba += abs(check_ba-ba_list[i])
        check_ba = ba_list[i]
print(max(v_fw, v_ba))

Submission status: Wrong Answer

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

B=sorted(A)
#print(B[N-1]-B[0] + B[N-1]-B[0])
hN=int(N/2)
cnt=0
C=[]
if N ==2:
    print(B[1]-B[0])
    exit()

if N %2 == 0:
    C.append(B[hN])
    for i in range(hN-1):
        C.append(B[N-1-i])
        C.append(B[i])
    C.append(B[hN+1])
else:
    C.append(B[hN])
    for i in range(hN):
        C.append(B[N-1-i])
        C.append(B[i])

#print(C)

for i in range(1,N):
    cnt+=abs(C[i]-C[i-1])

C=[]
cnt2=0
if N %2 == 0:
    C.append(B[hN-1])
    for i in range(hN):
        C.append(B[i])
        C.append(B[N-1-i])
    C.append(B[hN+1])
else:
    C.append(B[hN])
    for i in range(hN):
        C.append(B[i])
        C.append(B[N-1-i])

#print(C)
for i in range(1,N):
    cnt2+=abs(C[i]-C[i-1])

print(max(cnt,cnt2))

Submission status: Wrong Answer

Solution code:
n = int(input())

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

a.sort()
bigs = []
smalls = []

while len(a) >= 2:
    bigs.append(a[len(a) - 1])
    smalls.append(a[0])
    a.pop(0)
    a.pop(len(a) - 1)

resultArray = []
if len(a) > 0:
    resultArray.append(a[0])
    if bigs[0] - a[0] >= a[0] - smalls[0]:
        f = bigs
        s = smalls
    else:
        f = smalls
        s = bigs
    for i in range(len(f)):
        resultArray.append(f[i])
        resultArray.append(s[i])

else:
    if bigs[0] - smalls[-1] >= smalls[0] - bigs[-1]:
        f = bigs
        s = smalls
    else:
        f = smalls
        s = bigs
    
    resultArray.append(s[-1])
    for i in range(len(f) - 1):
        resultArray.append(f[i])
        resultArray.append(s[i])
    resultArray.append(f[-1])

sum = 0
for i in range(1, len(resultArray)):
    sum += abs(resultArray[i - 1] - resultArray[i])
print(sum)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
N = int(input())
aList = []
for i in range(N):
  a = int(input())
  aList.append(a)

min_idx = aList.index(min(aList))
minv = aList.pop(min_idx)
answerList = [minv]

i = 0
while True:
  if i % 2 == 0:
    if len(aList) == 0:
      break
    max_idx = aList.index(max(aList))
    maxv = aList.pop(max_idx)
    answerList.append(maxv)
    if len(aList) == 0:
      break
    max_idx = aList.index(max(aList))
    maxv = aList.pop(max_idx)
    answerList.insert(0, maxv)
  else:
    if len(aList) == 0:
      break
    min_idx = aList.index(min(aList))
    minv = aList.pop(min_idx)
    answerList.append(minv)
    if len(aList) == 0:
      break
    min_idx = aList.index(min(aList))
    minv = aList.pop(min_idx)
    answerList.insert(0, minv)
  i += 1
sa = 0
answer = 0
for k in range(len(answerList) - 1):
  if answerList[k] < answerList[k+1]:
    sa = answerList[k+1] - answerList[k]
  else:
    sa = answerList[k] - answerList[k+1]

  answer += sa

print(answer)

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())

    num_l = list()
    for _ in range(n):
        num_l += [int(input())]

    num_l = sorted(num_l)

    row = list()

    f = num_l.pop(0)
    s = f
    max_num = 0
    flag = True
    while(num_l):
        if len(num_l) == 1:
            if abs(f - num_l[0]) > abs(s - num_l[0]):
                max_num += abs(f - num_l[0])
            else:
                max_num += abs(s - num_l[0])
            break

        if flag:
            tmp_f = num_l.pop(len(num_l)-1)
            tmp_s = num_l.pop(len(num_l)-1)
        else:
            tmp_s = num_l.pop(0)
            tmp_f = num_l.pop(0)

        max_num += abs(f - tmp_f) + abs(s - tmp_s)

        f = tmp_f
        s = tmp_s

        flag = not flag

    print(max_num)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

s = int((N - 1) / 2)
m = A[0:s+1]
M = A[s+1:][::-1]

ans = [0 for _ in range(N)]
li = s
ri = s + 1

for i, (a, b) in enumerate(zip(m, M)):
    if i % 2 == 1:
        a, b = b, a
    ans[li] = a
    ans[ri] = b
    li -= 1
    ri += 1

if N % 2 == 1:
    if ans[0] == 0:
        ans[0] = m[-1]
    elif ans[-1] == 0:
        ans[-1] = m[-1]

S = 0
for i, j in zip(ans[1:], ans[:-1]):
    S += abs(i - j)

print(S)

Submission status: Wrong Answer

Solution code:
# encoding: utf-8
import math, sys

N = int(input())
A = [int(input()) for i in range(N)]

A_up = sorted(A)
A_down = list(A_up[::-1])

# exception (N == 3, may be related with perfect graph ?)
if N == 3:
    if A_up[1] - A_up[0] > A_up[2] - A_up[1]:
        print(A_up[1] + A_up[2] - 2 * A_up[0])
    else:
        print(2 * A_up[2] - A_up[0] - A_up[1])
    sys.exit()

# regular (N != 3)
rslt = []
idx_up = 0
idx_down = 0
ans = 0
for i in range(N):
    if i % 4 == 0:
        if i > 0: ans += left - A_up[idx_up]
        else: right = A_up[idx_up]
        left = A_up[idx_up]
        idx_up += 1
    elif i % 4 == 1:
        ans += A_down[idx_down] - right
        right = A_down[idx_down]
        idx_down += 1
    elif i % 4 == 2:
        ans += right - A_up[idx_up]
        right = A_up[idx_up]
        idx_up += 1
    else:
        ans += A_down[idx_down] - left
        left = A_down[idx_down]
        idx_down += 1

print(ans)

Submission status: Wrong Answer

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

a_list.sort()
tmp = a_list.copy()
a_list.reverse()

result = 0
if N % 2 == 0:
    for i, t in enumerate(tmp):
        if i < N // 2:
            if i == 0:
                result += a_list[i] - t
                result += a_list[i+1] - t
            elif i == N // 2 - 1:
                result += a_list[i-1] - t
            else:
                result += a_list[i-1] - t
                result += a_list[i+1] - t

        else:
            break
else:
    for i, t in enumerate(tmp):
        if i < N // 2 + 1:
            if i == 0:
                result += a_list[i] - t
                result += a_list[i+1] - t

            elif i == N // 2 - 1:
                result += a_list[i-1] - t

            elif i == N // 2:
                result += a_list[i-1] - t

            else:
                result += a_list[i-1] - t
                result += a_list[i+1] - t

        else:
            break
print(result)

Submission status: Wrong Answer

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

A_sorted = sorted(A)
pem_A = []
#print(A_sorted)

pem_A.append(A_sorted.pop(0))#center
while(len(A_sorted) != 0):
  try:
    pem_A.insert(0,A_sorted.pop(-1))#left
    pem_A.append(A_sorted.pop(-1))#right
    pem_A.insert(0,A_sorted.pop(0))#left
    pem_A.append(A_sorted.pop(0))#right
  except:
    break
#print(pem_A)

import numpy as np

ans = np.sum(abs(np.diff(pem_A)))
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

A.sort()
array = deque([A[0]])
a_len = 1
count = 0
idx = 0
result = 0

while a_len < N:
    if count % 2 == 0:
        array.appendleft(A[-idx-1])
        a_len += 1
        if a_len >= N:
            break
        array.append(A[-idx-2])
        a_len += 1
    else:
        array.appendleft(A[idx+1])
        a_len += 1
        if a_len >= N:
            break
        array.append(A[idx+2])
        a_len += 1
        idx += 2
    count += 1

for i in range(N-1):
    result += abs(array[i] - array[i+1])

print(result)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
a.sort()# ソート
hashi = [a[0], a[-1]]
total = abs(a[0] - a[-1])

s = 1
e = n - 2
if n == 2:
    exit()
while True:
    max_diff = 0
    point = "s"
    h = "s"
    if max_diff < abs(hashi[0] - a[s]):
        max_diff = abs(hashi[0] - a[s])
        point = "s"
        h = "s"
    if max_diff < abs(hashi[0] - a[e]):
        max_diff = abs(hashi[0] - a[e])
        point = "e"
        h = "s"
    if max_diff < abs(hashi[1] - a[s]):
        max_diff = abs(hashi[1] - a[s])
        point = "s"
        h = "e"
    if max_diff < abs(hashi[1] - a[e]):
        max_diff = abs(hashi[1] - a[e])
        point = "e"
        h = "e"
    total += max_diff
    if h == "s":
        if point == "s":
            hashi[0] = a[s]
        if point == "e":
            hashi[0] = a[e]
    if h == "e":
        if point == "s":
            hashi[1] = a[s]
        if point == "e":
            hashi[1] = a[e]
    if point == "s":
        s += 1
    if point == "e":
        e -= 1
    if s - 1 == e:
        break
print(total)

Submission status: Wrong Answer

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

ans_list = []
loop_n = (n-1)//2 if n%2 == 1 else (n-2)//2
ans_list.append(a.pop(0))

for i in range(loop_n):
    if i%2 == 0:
        ans_list.append(a.pop(-1))
        ans_list.insert(0, a.pop(-1))
    else:
        ans_list.insert(0, a.pop(0))
        ans_list.append(a.pop(0))

if n%2 == 0:
    if abs(ans_list[0] - a[0]) >= abs(ans_list[-1] - a[0]):
        ans_list.insert(0, a.pop())
    else:
        ans_list.append(a.pop())
ans = 0
for i in range(len(ans_list)-1):
    ans += abs(ans_list[i] - ans_list[i+1])

print(ans)

Submission status: Wrong Answer

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

a.sort()

b = [0 for i in range(len(a))]

if num%2 == 1:
    for i in range(len(a)//2+1):
        if i == 0:
            b[len(a)//2] = a[i]
        elif i%2 == 0:
            b[len(a)//2+i] = a[i]
            b[len(a)//2-i] = a[i-1]
        elif i%2 == 1:
            b[len(a)//2+i] = a[len(a)-i-1]
            b[len(a)//2-i] = a[len(a)-i]
else:
    for i in range(len(a)//2):
        if i == 0:
            b[len(a)//2] = a[i]
        elif i%2 == 0:
            b[len(a)//2+i] = a[i]
            b[len(a)//2-i] = a[i-1]
        elif i%2 == 1:
            b[len(a)//2+i] = a[len(a)-i-1]
            b[len(a)//2-i] = a[len(a)-i]
    b[0] = a[len(a)-len(a)//2]
sum = 0
for i in range(len(a)-1):
    sum = sum + abs(b[i]-b[i+1])
print(sum)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=[int(input()) for i in range(n)]
#print(l)
l=sorted(l)
#print(l)
sum=0
a=(n+1)//2
if n==3:
    sum+=l[2]+l[1]-2*l[0]
elif n%2==1:
    for i in range(a):
        sum+=-(2*l[i])
        #print(sum)
    for i in range(a,n):
        sum+=2*l[i]
        #print(sum)
    sum+=l[a-2]+l[a-1]
    #print(sum)
else:
    for i in range(a):
        sum+=-(2*l[i])
        #print(sum)
    for i in range(a,n):
        sum+=2*l[i]
    sum+=l[a-1]-l[a]
print(sum)

Submission status: Wrong Answer

Solution code:
import itertools
n=int(input())
a=[]
s=0
for i in range(n):
    t=int(input())
    a.append(t)
a.sort()
if n==2:
    print(a[1]-a[0])
elif n%2==0:
    m=(n-2)//2
    for i in range(m):
        s=s-2*a[i]
    s=s-a[m]
    s=s+a[m+1]
    for i in range(m+2,n):
        s=s+2*a[i]
    print(s)
elif n==3:
    print(max((a[2]-a[0]+a[1]-a[0]),(a[2]-a[1]+a[2]-a[0])))
elif n%2==1:
    m=(n-2)//2
    for i in range(m):
        s=s-a[i]*2
    s=s-a[m]
    s=s-a[m+1]
    for i in range(m+2,n):
        s=s+2*a[i]
    print(s)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=0
b=0
sum=0
l=[]
l2=[]
for i in range(n):
	l.append(int(input()))
	l.sort()
l2.append(min(l))
l.remove(min(l))
while True:
	if len(l)==0:
		break
	l2.append(max(l))
	l.remove(max(l))
	if len(l)==0:
		break
	l2.insert(0,max(l))
	l.remove(max(l))
	if len(l)==0:
		break
	l2.append(min(l))
	l.remove(min(l))
	if len(l)==0:
		break
	l2.insert(0,min(l))
	l.remove(min(l))
a=l2[0]
for i in range(1,len(l2)):
	b=l2[i]
	sum+=abs(b-a)
	a=b
print(sum)

Submission status: Wrong Answer

Solution code:
N = int(input())
D = [ int(input()) for _ in range(N) ]
D = sorted(D)
if N%2 == 0:
    num = int(N/2)
else:
    num = int(N/2) + 1


big = D[num:]
small = D[:num]

small_1 = small[1::2]
small_2 = small[::2]
small_2.reverse()
big_1 = big[1::2]
big_2 = big[0::2]
big_2.reverse()
if len(big)%2 == 0:
    big_new = big_2 + big_1
else:
    big_new = big_1 + big_2

if len(small)%2 == 0:
    small_new = small_1 + small_2
else:
    small_new = small_2 + small_1

if N%2 == 0:
    sum1 = sum([ abs(x - y) for x,y in zip(small_new, big_new)  ])
    sum2 = sum([ abs(x - y) for x,y in zip(small_new[1:], big_new[:-1])  ])
else:
    small_new.reverse()
    sum1 = sum([ abs(x - y) for x,y in zip(small_new[1:], big_new)  ])
    sum2 = sum([ abs(x - y) for x,y in zip(small_new[:-1], big_new)  ])

sum3 = 0
if N == 3:
    sum3 = abs(small_new[0] - small_new[1])
print(sum1 + sum2 + sum3)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=[int(input()) for i in range(n)]
#print(l)
l=sorted(l)
print(l)
sum=0
a=(n+1)//2
if n==3:
    sum+=l[2]+l[1]-2*l[0]
elif n%2==1:
    for i in range(a):
        sum+=-(2*l[i])
        #print(sum)
    for i in range(a,n):
        sum+=2*l[i]
        #print(sum)
    sum+=l[a-2]+l[a-1]
    #print(sum)
else:
    for i in range(a):
        sum+=-(2*l[i])
        #print(sum)
    for i in range(a,n):
        sum+=2*l[i]
    sum+=l[a-1]-l[a]
print(sum)

Submission status: Wrong Answer

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

a = sorted(a)
sum = 0

if n % 2 == 1:
    if a[0] == a[int(n/2)-1] and a[-1] == a[int(n/2)]:
        sum = (a[-1]-a[0])*(n-1)
    else:
        for i in range(int(n/2)+1, n):
            sum += a[i] * 2
        sum -= a[int(n / 2)-1]
        sum -= a[int(n / 2)]
        for i in range(int(n / 2)-1):
            sum -= a[i]*2
    print(sum)
else:
    sum += a[int(n / 2)]
    for i in range(int(n / 2)+1, n):
        sum += a[i] * 2
    sum -= a[int(n / 2) - 1]
    for i in range(int(n / 2) - 1):
        sum -= a[i] * 2
    print(sum)