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:

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

    a = []
    for i in range(n):
        a.append(int(input()))
    a.sort()
    if n == 2:
        print(abs(a[0] - a[1]))
        return
    if n == 3:
        tmp1 = abs(a[0] - a[1]) + abs(a[0] - a[2])
        tmp2 = abs(a[0] - a[2]) + abs(a[1] - a[2])
        print(max([tmp1, tmp2]))
        return
    ret1 = a[:n // 2 + (1 if n % 2 == 1 else 0)]
    ret2 = a[n // 2 + (1 if n % 2 == 1else 0):]
    ret1 = [ret1[-1]] + ret1[:-2] + [ret1[-2]]
    ret2 = ret2[1:] + [ret2[0]]
    ans = 0

    ans += abs(ret1[-1] - ret2[-1])
    for i in range(len(ret2)-1):
        ans += abs(ret1[i] - ret2[i])
        ans += abs(ret1[i+1] - ret2[i])
    if len(ret1) != len(ret2):
        ans += abs(ret1[-2] - ret2[-1])

    if n %2 == 0:
        print(ans)
        return
    ans1 = ans

    ret1 = a[:n // 2 + (0 if n % 2 == 1 else 1)]
    ret2 = a[n // 2 + (0 if n % 2 == 1else 1):]
    ret1 = ret1[1:] + [ret1[0]]
    ret2 = [ret2[-1]] + ret2[:-2] + [ret2[-2]]
    ans = 0

    ans += abs(ret1[-1] - ret2[-1])
    for i in range(len(ret1)-1):
        ans += abs(ret2[i] - ret1[i])
        ans += abs(ret2[i+1] - ret1[i])
    if len(ret1) != len(ret2):
        ans += abs(ret1[-1] - ret2[-2])
    print(max([ans, ans1]))

main()

Submission status: Wrong Answer

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


if N%2 == 0:
    pa = pb = None
    ans = 0
    for i in range(N//2):
        a,b = A[i],A[-1-i]
        if pa is None:
            ans += abs(a-b)
        else:
            ans += max(abs(a-pa)+abs(b-pb), abs(a-pb)+abs(b-pa))
        pa,pb = a,b
    print(ans)
else:
    pa = pb = A[0]
    ans = 0
    for i in range(N//2):
        if i%2==0:
            i *= 2
            a,b = A[-1-i],A[-2-i]
        else:
            i *= 2
            i -= 1
            a,b = A[i],A[i+1]
        ans += max(abs(a-pa)+abs(b-pb), abs(a-pb)+abs(b-pa))
        pa,pb = a,b

    pa = pb = A[-1]
    ans2 = 0
    for i in range(N//2):
        if i%2:
            i *= 2
            i -= 1
            a,b = A[-1-i],A[-2-i]
        else:
            i *= 2
            a,b = A[i],A[i+1]
        ans2 += max(abs(a-pa)+abs(b-pb), abs(a-pb)+abs(b-pa))
        pa,pb = a,b
    print(max(ans,ans2))

Submission status: Wrong Answer

Solution code:
print("io")

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
def solve():
    N = int(input())
    A = sorted([int(input()) for _ in (None,)*N])
    m = -(N>>1)
    b = N&1
    c1 = 2*sum(A[m+(1-b):]) - sum(A[m-1-b:m]) - 2*sum(A[:m-1-b]) + (1-b)*A[m]
    A = A[::-1]
    c2 = -2*sum(A[m+(1-b):]) + sum(A[m-1-b:m]) + 2*sum(A[:m-1-b]) - (1-b)*A[m]
    return max(c1,c2)

if __name__ == '__main__':
    print(solve())

Submission status: Wrong Answer

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

    A.sort()
    s = 0
    e = N - 1
    L = A[0]
    R = A[e]
    ret1 = R - L
    while True:
        ss = s + 1
        ee = e + 1

        b = max(abs(L - A[s]), abs(R - A[s])) > max(abs(L - A[e]), abs(R - A[e]))

        if b:
            s += 1
            if abs(L - A[s]) < abs(R - A[s]):
                ret1 += abs(R - A[s])
                R = A[s]
            else:
                ret1 += abs(L - A[s])
                L = A[s]
        else:
            e -= 1
            if s == e: break
            if abs(L - A[e]) < abs(R - A[e]):
                ret1 += abs(R - A[e])
                R = A[e]
            else:
                ret1 += abs(L - A[e])
                L = A[e]
        if s == e: break

    # 出力
    print("{}".format(ret1))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
N = int(input())
a = [0]*N
for i in range(N):
    A = int(input())
    a[i] = A
a.sort()
c = a.copy()
c.reverse()
a = deque(a)
b = [0]*(N-1)
for i in range(N-1):
    if i%2==0:
        b[i]=a.pop()
    else:
        b[i]=a.popleft()

ans1 = 0
for i in range(N-2):
    ans1 += abs(b[i+1]-b[i])
ans1 += max(abs(a[0]-b[0]),abs(a[0]-b[-1]))




c = deque(c)
b = [0]*(N-1)
for i in range(N-1):
    if i%2==0:
        b[i]=c.popleft()
    else:
        b[i]=c.pop()

ans2 = 0
for i in range(N-2):
    ans2 += abs(b[i+1]-b[i])
ans2 += max(abs(c[0]-b[0]),abs(c[0]-b[-1]))

print(max(ans1,ans2))

Submission status: Wrong Answer

Solution code:
def bigger(a,b):
    if a>=b:
        return a
    else:
        return b

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

a.sort()
ans = a[-1] - a[0]
count = 2

left = a[0]
right = a[-1]

hidari = 1
migi = n-2
for i in range(n-2):
    if bigger(abs(left-a[hidari]),abs(left-a[migi])) >= bigger(abs(right-a[hidari]),abs(right-a[migi])):
        ans += bigger(abs(left-a[hidari]),abs(left-a[migi]))
        if abs(left-a[hidari]) > abs(left-a[migi]):
            left = a[hidari]
            hidari += 1
        else:
            left = a[migi]
            migi -= 1
    else:
        ans += bigger(abs(right-a[hidari]),abs(right-a[migi]))
        if abs(right-a[hidari]) > abs(right-a[migi]):
            right = a[hidari]
            hidari += 1
        else:
            left = a[migi]
            migi -= 1

print(ans)

Submission status: Wrong Answer

Solution code:
N, *A = map(int, open(0).read().split())
A = sorted(A)
B = [0] * N
if N % 2 == 0:
    B = []
    for a, b in zip(A[:N // 2], A[N // 2:][::-1]):
        B += [a, b]
else:
    B = []
    for a, b in zip(A[N // 2 + 1:][::-1], A[:N // 2]):
        B += [a, b]
    B += [A[N // 2]]
ans = 0
for x, y in zip(B, B[1:-1]):
    ans += abs(x - y)
print(max(ans + abs(B[-1] - B[0]), ans + abs(B[-2] - B[-1])))

Submission status: Wrong Answer

Solution code:
N, *A = map(int, open(0).read().split())
A = sorted(A)
B = [0] * N
if N % 2 == 0:
    B = []
    for a, b in zip(A[:N // 2], A[N // 2:][::-1]):
        B += [a, b]
else:
    B = []
    for a, b in zip(A[N // 2 + 1:][::-1], A[:N // 2]):
        B += [a, b]
    B += [A[N // 2]]
ans = 0
print([B[-1]]+B[:-1])
for x, y in zip(B, B[1:-1]):
    ans += abs(x - y)
print(max(ans + abs(B[-1] - B[0]), ans + abs(B[-2] - B[-1])))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
A.sort()
Sum = 0
if N % 2 == 0:
    As, Al = A[:N//2], A[N//2:]
    for i in range(N//2-1):
        Sum += (Al[-i-1] - As[i])*2
    Sum += Al[0] - As[-1]
else:
    As, Al = A[:N//2], A[N//2:]
    for i in range(N//2):
        Sum += (Al[i+1]-As[i])*2
    if (N//2)%2==0:
        Sum -= Al[0] - As[-1]
    else:
        Sum += Al[0] - Al[1]
    
print(Sum)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)

def main():
	n = int(input())
	lst = []
	for _ in range(n):
		lst += [int(input())]
	lst.sort()
	ylst = lst[::-1]
	mini = 10000000000
	ans = 0
	zlst = []
	if n % 2 == 1:
		zlst = [lst[0]]
		for i in range(int((n-1)/2)):
			if i % 2 == 0:
				zlst = [ylst[i]] + zlst + [ylst[i+1]]
			else:
				zlst = [lst[i]] + zlst + [lst[i+1]]
		for i in range(n-1):
			ans += abs(zlst[i+1]-zlst[i])
		print(ans)
	else:
		for i in range(int(n/2)):
			zlst += [lst[i], ylst[i]]
		for i in range(n):
			ans += abs(zlst[(i+1)%n]-zlst[i])
			mini = min(mini, abs(zlst[(i+1)%n] - zlst[i]))
		print(ans - mini)
	return

main()

Submission status: Wrong Answer

Solution code:

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

    a = []
    for i in range(n):
        a.append(int(input()))
    a.sort()
    if n == 2:
        print(abs(a[0] - a[1]))
        return
    if n == 3:
        tmp1 = abs(a[0] - a[1]) + abs(a[0] - a[2])
        tmp2 = abs(a[0] - a[2]) + abs(a[1] - a[2])
        print(max([tmp1, tmp2]))
        return
    ret1 = a[:n // 2 + (1 if n % 2 == 1 else 0)]
    ret2 = a[n // 2 + (1 if n % 2 == 1else 0):]
    ret1 = [ret1[-1]] + ret1[:-2] + [ret1[-2]]
    ret2 = ret2[1:] + [ret2[0]]
    ans = 0

    ans += abs(ret1[-1] - ret2[-1])
    for i in range(len(ret2)-1):
        ans += abs(ret1[i] - ret2[i])
        ans += abs(ret1[i+1] - ret2[i])
    if len(ret1) != len(ret2):
        ans += abs(ret1[-2] - ret2[-1])
    print(ans)

main()

Submission status: Wrong Answer

Solution code:
from collections import deque
N = int(input())
a = [0]*N
for i in range(N):
    A = int(input())
    a[i] = A
a.sort()
a = deque(a)
b = [0]*(N-1)
for i in range(N-1):
    if i%2==0:
        b[i]=a.pop()
    else:
        b[i]=a.popleft()

ans = 0
for i in range(N-2):
    ans += abs(b[i+1]-b[i])
ans += max(abs(a[0]-b[0]),abs(a[0]-b[-1]))
print(ans)

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
lis = [int(input()) for i in range(n)]
lis.sort()
ans = 0
if n % 2 == 0:
  lia = []
  lib = []
  for i in range(n//2):
    lia.append(lis[i])
    lib.append(lis[i+n//2])
    lia.append(lis[i+n//2])
    lib.append(lis[i])
  cou = 0
  co = 0
  for i in range(n-1):
    cou += abs(lia[i+1]-lia[i])
    co += abs(lib[i+1]-lib[i])
  print(max(cou,co),)
else:
  lia = [lis[(n-1)//2]]
  lib = [lis[(n-1)//2]]
  lis.remove(lis[(n-1)//2])
  for i in range(n//2):
    lia.append(lis[i])
    lib.append(lis[i+n//2])
    lia.append(lis[i+n//2])
    lib.append(lis[i])
  cou = 0
  co = 0
  for i in range(n-1):
    cou += abs(lia[i+1]-lia[i])
    co += abs(lib[i+1]-lib[i])
  print(max(cou,co))

Submission status: Wrong Answer

Solution code:
from collections import deque

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

A.sort()

d = deque(A)
ans_p = deque([A[0]])
d.popleft()
while len(d) > 0:
    ans_p.appendleft(d.pop())
    if len(d) == 0:
        break
    ans_p.append(d.pop())
    if len(d) == 0:
        break
    ans_p.appendleft(d.popleft())
    if len(d) == 0:
        break
    ans_p.append(d.popleft())

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

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
import copy

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_list=[]
    for i in range(N):
        input_raw = read_func().strip().split()
        [a] = [long(input_raw[0])]
        a_list.append(a)

    a_list.sort()
    left = a_list[0]
    right = a_list[0]

    score = 0
    for i in range(0, N/2 + (0 if N % 2 ==0 else 1)):

        if i != 0:
            if abs(a_list[i] - left) > abs(a_list[i] - right):
                score += abs(a_list[i] - left)
                left = a_list[i]
            else:
                score += abs(a_list[i] - right)
                right = a_list[i]

        j = N - i - 1
        if j != i:
            if abs(a_list[j] - left) > abs(a_list[j] - right):
                score += abs(a_list[j] - left)
                left = a_list[j]
            else:
                score += abs(a_list[j] - right)
                right = a_list[j]

    print score


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())

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

import numpy as np
a = np.sort(a)

if len(a)%2 == 1:
    t = round(len(a)/2 - 0.1)
    print(t)
    mid = a[t]
    if mid in a[t+1:]:
        b = []
        for k in range(t):
            b.append(a[t+1+k])
            b.append(a[t - 1 - k])
        b.append(mid)
    else:
        b = []
        for k in range(t):
            b.append(a[t - 1 - k])
            b.append(a[t+1+k])
        b.append(mid)
else:
    t = round(len(a)/2) - 1
    mid1 = a[t]
    mid2 = a[t+1]
    b = []
    for k in range(t):
        b.append(a[t+2+k])
        b.append(a[t - 1 - k])
    b = [mid1] + b
    b = b + [mid2]
print(np.sum(np.abs(np.diff(b))))

Submission status: Wrong Answer

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

A = sorted(A)
q = deque()
q.append(A[0])
i = 1
j = N - 1
mi = True
while i < N and j >= 0 and i <= j:
    if mi:
        p1, p2 = q[0], q[-1]
        if abs(A[j] - p1) <= abs(A[j] - p2):
            q.append(A[j])
        else:
            q.appendleft(A[j])
        j -= 1
        mi = False
    else:
        p1, p2 = q[0], q[-1]
        if abs(A[i] - p1) <= abs(A[i] - p2):
            q.append(A[i])
        else:
            q.appendleft(A[i])
        i += 1
        mi = True

num = 0
pre = q[0]
for i in range(1, N):
    num += abs(q[i] - pre)
    pre = q[i]

print(num)

Submission status: Wrong Answer

Solution code:
from collections import deque
N=int(input())
A=[]
for i in range(N):
    s=int(input())
    A.append(s)
A.sort()
H=deque()
H.append(A[0])
s=1
t=N-1
i=0
while t-s!=-1:
    if i==0:
        k=A[t]
        if abs(H[0]-k)>=abs(H[-1]-k):
            H.appendleft(k)
        else:
            H.append(k)            
        t-=1
        i+=1
    else:
        k=A[s]
        if abs(H[0]-k)>=abs(H[-1]-k):
            H.appendleft(k)
        else:
            H.append(k)   
        i=0
        s+=1
ans=0
for i in range(N-1):
    ans+=abs(H[i+1]-H[i])
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(raw_input())


s = 0

a = int(raw_input())
for i in range(n -1):
    b = int(raw_input())
    s = s + abs(b - a)

    a = b

print s

Submission status: Wrong Answer

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

start = 1
end = n - 1

left = aa[0]
right = aa[0]

ans = 0

while start <= end:
    if start <= end:
        if abs(left - aa[end]) < abs(right - aa[end]):
            ans += abs(right - aa[end])
            right = aa[end]
            end -= 1
        else:
            ans += abs(left - aa[end])
            left = aa[end]
            end -= 1
    if start <= end:
        if abs(left - aa[start]) < abs(right - aa[start]):
            ans += abs(right - aa[start])
            right = aa[start]
            start += 1
        else:
            ans += abs(left - aa[start])
            left = aa[start]
            start += 1

print(ans)

Submission status: Wrong Answer

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

X = deque([A.popleft()])
ans = 0
for i in range(N - 1):
    # Aのどちらから取り出してXのどちらに入れるか
    left_left = abs(X[0] - A[0])
    left_right = abs(X[-1] - A[0])
    right_left = abs(X[0] - A[-1])
    right_right = abs(X[-1] - A[-1])

    if left_left >= max(left_right, right_left, right_right):
        ans += left_left
        X.appendleft(A.popleft())

    elif left_right >= max(left_left, right_left, right_right):
        ans += left_right
        X.append(A.popleft())

    elif right_left >= max(left_left, left_right, right_right):
        ans += right_left
        X.appendleft(A.pop())

    else:
        ans += right_right
        X.appendleft(A.pop())

print(ans)

Submission status: Wrong Answer

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

if n == 2:
	print(abs(a[0] - a[1]))
elif n == 3:
	print(max(abs(a[0] - a[1]) + abs(a[0] - a[2]), abs(a[1] - a[2]) + abs(a[1] - a[0]), abs(a[2] - a[1]) + abs(a[2] - a[0])))
else:
	b = sorted(a)
	ans = [0] * n
	if n % 2 == 0:
		for i in range(2, n-1, 2):
			ans[i] =  b[(i // 2) - 1]
			#print(i, ans[i])
		for i in range(1, n-1, 2):
			ans[i] = b[-1 - ((i - 1) // 2)]
			#print(i, ans[i])
		ans[0] = b[n // 2 - 1]
		ans[n-1] = b[n // 2]
	else:
		for i in range(2, n-1, 2):
			ans[i] =  b[(i // 2) - 1]
		for i in range(1, n-2, 2):
			ans[i] = b[-1 - ((i - 1) // 2)]
		ans[0] = b[n // 2]
		if b[n // 2 + 1] + b[n // 2 - 1] >= 2 * b[n // 2]:
			ans[n-1] = b[n // 2 - 1]
			ans[n-2] = b[n // 2 + 1]
		else:
			ans[n-1] = b[n // 2 + 1]
			ans[n-2] = b[n // 2 - 1]

	#print(ans)
	print(sum([abs(ans[i] - ans[i+1]) for i in range(n-1)]))

Submission status: Wrong Answer

Solution code:
n = int(raw_input())

s = 0

a = int(raw_input())
for i in range(n -1):
    b = int(raw_input())
    s = s + (b - a)

    a = b

print s

Submission status: Wrong Answer

Solution code:

import sys
#sys.stdin=open("data.txt")
input=sys.stdin.readline
mii=lambda:map(int,input().split())

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

b=[]
i=0
j=len(a)-1
while j-i>=1:
    b.append(a[i])
    b.append(a[j])
    i+=1
    j-=1

diff=b[-1]-b[-2]
if i==j:
    b.append(a[i])
    diff=min(diff,abs(b[-1]-b[-2]))
    for i in range(2,len(b)):
        diff=min(diff,abs(b[i]-b[i-2]))


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




print(ans-diff)

Submission status: Wrong Answer

Solution code:
from collections import deque

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


if N%2 == 0:
    q = deque()
    for i in range(N//2):
        if i%2==0:
            q.appendleft(A[i])
            q.append(A[-1-i])
        else:
            q.appendleft(A[-1-i])
            q.append(A[i])
    q = list(q)
    ans = 0
    for i in range(N-1):
        ans += abs(q[i] - q[i+1])
    print(ans)
else:
    q = deque([A[0]])
    for i in range(N//2):
        if i%2==0:
            i *= 2
            q.appendleft(A[-2-i])
            q.append(A[-1-i])
        else:
            i *= 2
            i -= 1
            q.appendleft(A[i+1])
            q.append(A[i])
    q = list(q)
    ans = 0
    for i in range(N-1):
        ans += abs(q[i] - q[i+1])

    q = deque([A[-1]])
    for i in range(N//2):
        if i%2==0:
            i *= 2
            q.appendleft(A[i])
            q.append(A[i+1])
        else:
            i *= 2
            i -= 1
            q.appendleft(A[-1-i])
            q.append(A[-2-i])
    q = list(q)
    ans2 = 0
    for i in range(N-1):
        ans2 += abs(q[i] - q[i+1])
    print(max(ans, ans2))

Submission status: Wrong Answer

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

A = sorted(A)
q = deque()
q.append(A[0])
i = 1
j = N - 1
mi = True
while i < N and j >= 0 and i <= j:
    if mi:
        p1, p2 = q[0], q[-1]
        if abs(A[j] - p1) < abs(A[j] - p2):
            q.append(A[j])
        else:
            q.appendleft(A[j])
        j -= 1
        mi = False
    else:
        p1, p2 = q[0], q[-1]
        if abs(A[i] - p1) < abs(A[i] - p2):
            q.append(A[i])
        else:
            q.appendleft(A[i])
        i += 1
        mi = True

num = 0
pre = q[0]
for i in range(1, N):
    num += abs(q[i] - pre)
    pre = q[i]

print(num)

Submission status: Wrong Answer

Solution code:

import sys
#sys.stdin=open("data.txt")
input=sys.stdin.readline
mii=lambda:map(int,input().split())

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

b=[]
i=0
j=len(a)-1
while j-i>=1:
    b.append(a[i])
    b.append(a[j])
    i+=1
    j-=1

diff=b[-1]-b[-2]
if i==j:
    for i in range(2,len(b)):
        diff=min(diff,abs(b[i]-b[i-2]))
    b.append(a[i-1])
    diff=min(diff,abs(b[-1]-b[-2]))
    diff=min(diff,abs(b[-1]-b[-3]))


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




print(ans-diff)

Submission status: Wrong Answer

Solution code:
def main():

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

    A.sort()
    s=0
    e=N-1
    L=A[0]
    R=A[e]
    ret1 = R-L
    while True:
        s+=1
        if s==e: break
        if abs(L-A[s]) < abs(R-A[s]):
            ret1 += abs(R-A[s])
            R=A[s]
        else:
            ret1 += abs(L-A[s])
            L=A[s]
        e-=1
        if s == e: break
        if abs(L-A[e]) < abs(R-A[e]):
            ret1 += abs(R-A[e])
            R=A[e]
        else:
            ret1 += abs(L-A[e])
            L=A[e]


    # 出力
    print("{}".format(ret1))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, *A = map(int, open(0).read().split())
A = sorted(A)
B = [0] * N
if N % 2 == 0:
    B = []
    for a, b in zip(A[:N // 2], A[N // 2:][::-1]):
        B += [a, b]
else:
    B = []
    for a, b in zip(A[N // 2 + 1:][::-1], A[:N // 2]):
        B += [a, b]
    B += [A[N // 2]]
ans = 0
for x, y in zip(B, B[1:-1]):
    ans += abs(x - y)
print(ans + abs(B[-1] - B[0]))

Submission status: Wrong Answer

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

A = sorted(A)
q = deque()
q.append(A[0])
i = 1
j = N - 1
mi = True
while i < N and j > 0 and i <= j:
    if mi:
        p1, p2 = q[0], q[-1]
        if abs(A[j] - p1) < abs(A[j] - p2):
            q.append(A[j])
        else:
            q.appendleft(A[j])
        j -= 1
        mi = False
    else:
        p1, p2 = q[0], q[-1]
        if abs(A[i] - p1) < abs(A[i] - p2):
            q.append(A[i])
        else:
            q.appendleft(A[i])
        i += 1
        mi = True

num = 0
pre = q[0]
for i in range(1, N):
    num += abs(q[i] - pre)
    pre = q[i]

print(num)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

A.sort()
ans_p = [A[0]]
A.pop(0)
while len(A) > 0:
    ans_p.insert(0, A.pop(-1))
    if len(A) == 0:
        break
    ans_p.append(A.pop(-1))
    if len(A) == 0:
        break
    ans_p.insert(0, A.pop(0))
    if len(A) == 0:
        break
    ans_p.append(A.pop(0))

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
A.sort()
Sum = 0
if N % 2 == 0:
    As, Al = A[:N//2], A[N//2:]
    for i in range(N//2-1):
        Sum += (Al[-i-1] - As[i])*2
    Sum += Al[0] - As[-1]
elif N == 3:
    Sum = A[1] + A[2] - A[0] * 2
else:
    As, Al = A[:(N//2)+1], A[(N//2)+1:]
    for i in range(N//2):
        Sum += (Al[i] -As[i])*2
    Sum += As[-2]
    Sum -= As[-1]
print(Sum)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = sorted([int(input()) for _ in range(n)])

l = a[0]
r = a[0]
ixl = 1
ixr = n-1

s = 0
d = -1

while ixl < ixr:
    if d == 1:
        nl = a[ixl]
        nr = a[ixl+1]
        ixl += 2
    else:
        nl = a[ixr]
        nr = a[ixr-1]
        ixr -= 2
    s += ((l+r)-(nl+nr))*d
    l = nl
    r = nr
    d *= -1

if ixl == ixr:
    s += (l-a[ixl])*d

print(s)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

import numpy as np
a = np.sort(a)

if len(a)%2 == 1:
    t = round(len(a)/2 - 0.1)
    print(t)
    mid = a[t]
    b = []
    for k in range(t):
        b.append(a[t - 1 - k])
        b.append(a[t+1+k])
    b.append(mid)
else:
    t = round(len(a)/2) - 1
    mid1 = a[t]
    mid2 = a[t+1]
    b = []
    for k in range(t):
        b.append(a[t+2+k])
        b.append(a[t - 1 - k])
    b = [mid1] + b
    b = b + [mid2]
print(np.sum(np.abs(np.diff(b))))

Submission status: Wrong Answer

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

start = 1
end = n - 1

left = aa[0]
right = aa[0]

ans = 0

while start <= end:
    if start <= end:
        ans += abs(aa[end] - left)
        left = aa[end]
        end -= 1
    if start <= end:
        ans += abs(aa[end] - right)
        right = aa[end]
        end -= 1
    if start <= end:
        ans += abs(left - aa[start])
        left = aa[start]
        start += 1
    if start <= end:
        ans += abs(right - aa[start])
        right = aa[start]
        start += 1

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
N=int(input())
A=[]
for i in range(N):
    s=int(input())
    A.append(s)
A.sort()
H=deque()
H.append(A[0])
s=1
t=N-1
i=0
while t-s!=-1:
    if i==0:
        H.appendleft(A[t])
        t-=1
        i+=1
    elif  i==1:
        H.append(A[t])
        t-=1
        i+=1
    elif i==2:
        H.appendleft(A[s])
        s+=1
        i+=1
    else:
        H.append(A[s])
        s+=1
        i=0
ans=0
for i in range(N-1):
    ans+=abs(H[i+1]-H[i])
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
n = int(input())
a = deque([])
for i in range(n):
  x = int(input())
  a.append(x)
a=sorted(a)
a = deque(a)
b = deque([])
b.append(a.popleft())
i = 0
while a:
  if i%2 == 1:
    b.append(a.popleft())
    if a == deque([]):
      break
    b.appendleft(a.popleft())
    i += 1
  else:
    b.append(a.pop())
    if a == deque([]):
      break
    b.appendleft(a.pop())
    i += 1
ans = 0
for i in range(1,n):
  ans += abs(b[i] - b[i-1])
print(ans)

Submission status: Wrong Answer

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

X = sorted(X)

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
A = sorted(A, reverse=True)
B = [A.pop(0)]
num = 0
for i in range(N - 1):
    if i % 2 == 0:
        k = A.pop(-1)
    else:
        k = A.pop(0)
    if abs(B[-1] - k) <= abs(B[0] - k):
        num += abs(B[0] - k)
        B = [k] + B
    else:
        num += abs(B[-1] - k)
        B += [k]


print(num)

Submission status: Time Limit Exceeded

Solution code:
from itertools import permutations as p

z = open('/dev/stdin', 'r').read().split()
z.pop(0)
t = 0
for i in p(z):
    d = 0
    for j in range(len(i) - 1):
        d = d + abs(int(i[j]) - int(i[j + 1]))
        t = d if (d > t) else t

print(t)

Submission status: Time Limit Exceeded

Solution code:
import itertools
n = int(input())

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

max_diff = 0

for l in itertools.permutations(x):
    sum_diff = 0
    for i in range(n-1):
        sum_diff += abs(l[i] - l[i+1])
    if sum_diff > max_diff:
        max_diff = sum_diff


print(max_diff)

Submission status: Time Limit Exceeded

Solution code:
def solve(a,n):
  cnt=a[0]
    
  c,d=a[-1],a[-2]
  ans=abs(cnt-c)+abs(cnt-d)
  cnt=c+d
  
  a=a[1:-2]
  
  ch=1
  lecnt=len(a)
  
  while lecnt > 1:
    if ch==-1:
      c,d=a[-1],a[-2]
      ans+=abs(cnt-(c+d))
      cnt=c+d
      ch=1
      a=a[:-2]
      lecnt-=2
    else:
      c,d=a[0],a[1]
      ans+=abs(cnt-(c+d))
      cnt=c+d
      ch=-1
      a=a[2:]
      lecnt-=2
  
  if len(a)==1:
    ans+=max(abs(c-a[0]),abs(d-a[0]))
    
  return ans


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

A.sort()
ans1=solve(A,N)
A.sort(reverse=True)
ans2=solve(A,N)
print(max(ans1,ans2))

Submission status: Time Limit Exceeded

Solution code:
import itertools
n = int(input())

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

max_diff = 0

#
# lists = []
#
# for i in range(n ** 2):
#     x[i] = x[i+1]
#     lists.append(x)


for l in itertools.permutations(x):
    sum_diff = 0
    for i in range(n-1):
        sum_diff += abs(l[i] - l[i+1])
    if sum_diff > max_diff:
        max_diff = sum_diff


print(max_diff)

Submission status: Time Limit Exceeded

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

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

#print(a)    


ans=0

if n%2==0:
    for i in range(n//2-1):
        ans += 2*(a[n-1-i]-a[i])
    ans += a[n//2] - a[n//2-1]

if n%2==1:
    for i in range(n//2-1):
        ans += 2*(a[n-1-i]-a[i])
    ans += max(a[n//2+1] + a[n//2] - 2*a[n//2-1], 2*a[n//2+1] - a[n//2] - a[n//2-1])

print(ans)

Submission status: Time Limit Exceeded

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

A.sort()

def solve(a,n):
  cnt=a[0]
    
  c,d=a[-1],a[-2]
  ans=abs(cnt-c)+abs(cnt-d)
  cnt=c+d
  
  a=a[1:-2]
  
  ch=1
  while len(a) > 1:
    if ch==-1:
      c,d=a[-1],a[-2]
      ans+=abs(cnt-(c+d))
      cnt=c+d
      ch=1
      a=a[:-2]
    else:
      c,d=a[0],a[1]
      ans+=abs(cnt-(c+d))
      cnt=c+d
      ch=-1
      a=a[2:]
  
  if len(a)==1:
    ans+=max(abs(c-a[0]),abs(d-a[0]))
    
  return ans

ans1=solve(A,N)
A.sort(reverse=True)
ans2=solve(A,N)
print(max(ans1,ans2))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [0] * N
for i in range(N):
  A[i] = int(input())
A.sort()
A0 = A[1:]
B = [A[0]]
cnt = 0
while A0:
  if abs(B[0] - A0[-1]) >= abs(B[-1] - A0[-1]):
    cnt += abs(B[0] - A0[-1])
    B = [A0[-1]] + B
  else:
    cnt += abs(B[-1] - A0[-1])
    B = B + [A0[-1]]
  A0 = A0[:-1]
  if A0:
    if abs(B[0] - A0[0]) >= abs(B[-1] - A0[0]):
      cnt += abs(B[0] - A0[0])
      B = [A0[0]] + B
    else:
      cnt += abs(B[-1] - A0[0])
      B = B + [A0[0]]
    A0 = A0[1:]
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [0] * N
for i in range(N):
  A[i] = int(input())
A.sort()
A0 = A[1:]
B = [A[0]]
while A0:
  if abs(B[0] - A0[-1]) >= abs(B[-1] - A0[-1]):
    B = [A0[-1]] + B
  else:
    B = B + [A0[-1]]
  A0 = A0[:-1]
  if A0:
    if abs(B[0] - A0[0]) >= abs(B[-1] - A0[0]):
      B = [A0[0]] + B
    else:
      B = B + [A0[0]]
    A0 = A0[1:]
cnt = 0
for i in range(1, N):
  cnt += abs(B[i] - B[i - 1])
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
'''input
3
5
5
1
'''
from sys import exit
n = input()
arr = [input() for i in range(n)]
arr.sort()
if n == 2:
	print abs(arr[0]-arr[1])
	exit(0)

ans = [-1]*(n)
j = 2
for i in range(4,n,2):
	ans[i] =arr[j]
	j += 1
# print ans
j = -2
for i in range(3,n,2):
	ans[i] = arr[j]
	j -= 1
ans[0] = arr[1]
ans[1] = arr[-1]
ans[2] = arr[0]
answ1 = 0
# print ans
for i in range(n-1):
	answ1 += abs(ans[i]-ans[i+1])
answ1 = max(answ1,answ1-abs(ans[-1]-ans[-2])+abs(ans[-1]-ans[0]))
arr = list(reversed(arr))
ans = [-1]*(n)
j = 2
for i in range(4,n,2):
	ans[i] =arr[j]
	j += 1
# print ans
j = -2
for i in range(3,n,2):
	ans[i] = arr[j]
	j -= 1
ans[0] = arr[1]
ans[1] = arr[-1]
ans[2] = arr[0]
answ = 0
# print ans
for i in range(n-1):
	answ += abs(ans[i]-ans[i+1])
answ = max(answ,answ-abs(ans[-1]-ans[-2])+abs(ans[-1]-ans[0]))
print max(answ,answ1)