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:
import bisect
N = int(input())
A = [int(input()) for _ in range(N)]

A = sorted(A)

ret = 0

# if N != len(list(set(A))):
#     tmp = -1
#     AA = []
#     Dup_num = 0
#     Dup_cnt = 0
#     for i,a in A:
#         if a != tmp:
#             AA.append(a)
#             tmp = a
#         else:
#             Dup_num = a
#             Dup_cnt = 0
#             while a == A[i+Dup_cnt]:
#                 Dup_cnt += 1

#             Dup_cnt -= 1
if N == 2:
    print(A[1] - A[0])
    exit()

if N == 3:
    print(A[1] + A[2] - A[0]*2)
    exit()
if N%2 == 1:

    smo = A[:N//2+1]
    lar = A[N//2+1:]
    lar = lar[::-1]


    ret += sum(lar)*2
    ret -= sum(smo)*2 - (smo[-1] + smo[-2])

else:
    smo = A[:N//2]
    lar = A[N//2:]

    lar = lar[::-1]

    ret += sum(lar)*2 - lar[-1]
    ret -= sum(smo)*2 - smo[-1]
    # for i in range(N//2-1):
    #     ret += abs(lar[i] + lar[i+1] - 2*smo[i])
    #     print(ret,lar[i],lar[i+1],smp[i])

    # ret += lar[i] - smo[i+1]

print(ret)


#     L = N//2 - 1
#     R = N//2 + 1
#     ret = abs(lar[0] + lar[1] - 2*mid)
#     for i in range(N//2):
#         ret +=
#     for i in range()
#     ans[N//2] = mid
#     for i in range(1,(N-1)//2):
#         if i%2 == 1:
#             if i%4 == 1:
#                 ans[L] = lar[]
#             elif i%4 == 3:
#                 ans[L] = A[-(i//2)]

#             L -= L

#         elif i%2 == 0:
#             ans[R] = A[i]
#             R += 1

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

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
    for i in range(n//2-1):
        if i % 2 == 0:
            if i == n//2 -2:
                ans += a[-1]-a[0]
            c += a.pop(-1)
            c += a.pop(-1)
            ans += abs(b-c)
            b = c
            c = 0
        else:
            if i == n//2 -2:
                ans += a[-1]-a[0]
            c += a.pop(0)
            c += a.pop(0)
            ans += abs(b-c)
            b = c
            c = 0

print(ans)

Submission status: Wrong Answer

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

A_sorted = sorted(A)

if n % 2 == 1:
	if A_sorted[(n-1)//2] != A_sorted[(n+1)//2]:
		n_small = (n+1)//2
		n_big = n//2

		A_small = A_sorted[:n_small]
		A_big = A_sorted[n_small:]

		bigsum = sum(A_big)

		smallsum = sum(A_small[:-2])
		smallmax = A_small[-1] + A_small[-2]

		ans = bigsum * 2 - ( smallsum * 2 + smallmax)
	else:
		n_small = n//2
		n_big = (n+1)//2

		A_small = A_sorted[:n_small]
		A_big = A_sorted[n_small:]

		smallsum = sum(A_small)

		bigsum = sum(A_big[2:])
		bigmin = A_big[0] + A_big[1]

		ans = bigsum * 2 + bigmin - smallsum * 2


else:
	n_small = n//2
	n_big = n//2

	A_small = A_sorted[:n_small]
	A_big = A_sorted[n_small:]

	bigsum = sum(A_big[1:])
	bigmin = A_big[0]

	smallsum = sum(A_small[:-1])
	smallmax = A_small[-1]

	ans = bigsum * 2 + bigmin - ( smallsum * 2 + smallmax)

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
D = [ int(input()) for _ in range(N) ]
D = sorted(D)
num = int(N/2)

big = D[num:]
big.reverse()
small = D[:num]

sum1 = 0
sum2 = 0
i = 0
while i < num:
    sum1 += abs(big[i] - small[i])
    i += 1

i = 0
while i < num-1:
    sum2 += abs(big[i] - small[i+1])
    i += 1

if N%2 == 1:
    sum2 += abs(big[-2] - small[-1])
 
print(sum1 + sum2)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = sorted([int(input()) for x in range(n)])
ans = 0
if n % 2 != 0:
    a1 = a[n // 2]
    del a[n // 2]
    for i in range(n - 1):
        ua = abs(a1 - a[0])
        ta =  abs(a1 - a[-1])
        if ua >= ta:
            ans += ua
            a1 = a[0]
            del a[0]
        else:
            ans += ta
            a1 = a[-1]
            del a[-1]
else:
    a1 = a[n // 2 - 1]
    A2 = a.copy()
    a2 = A2[n // 2]
    ans2 = 0
    del A2[n // 2]
    del a[n // 2 - 1]
    for i in range(n - 1):
        ua = abs(a1 - a[0])
        ta =  abs(a1 - a[-1])
        if ua >= ta:
            ans += ua
            a1 = a[0]
            del a[0]
        else:
            ans += ta
            a1 = a[-1]
            del a[-1]

    for i in range(n - 1):
        ua = abs(a2 - A2[0])
        ta =  abs(a2 - A2[-1])
        if ua >= ta:
            ans2 += ua
            a2 = A2[0]
            del A2[0]
        else:
            ans2 += ta
            a2 = A2[-1]
            del A2[-1]
    ans = max(ans , ans2)

print(ans)

Submission status: Wrong Answer

Solution code:
n,*a=map(int,open(0).read().split())
if n<3:
    print(abs(a[0]-a[1]))
    exit()
b=sorted(a)
if abs(b[0]-b[-1])+abs(b[0]-b[-2])>abs(b[-1]-b[0])+abs(b[-1]-b[1]):
    c=abs(b[0]-b[-1])+abs(b[0]-b[-2])
    x,y=b[-1],b[-2]
    del b[-1],b[-1],b[0]
else:
    c=abs(b[-1]-b[0])+abs(b[-1]-b[1])
    x,y=b[0],b[1]
    del b[0],b[0],b[-1]
while b:
    if len(b)<2:
        c+=max(abs(x-b[0]),abs(y-b[0]))
        del b[0]
    else:
        if min(x,y)>=b[-1]:
            c+=max(abs(x-b[0])+abs(y-b[1]),abs(x-b[1])+abs(y-b[0]))
            x,y=b[0],b[1]
            del b[0],b[0]
        else:
            c+=max(abs(x-b[-1])+abs(y-b[-2]),abs(x-b[-2])+abs(y-b[-1]))
            x,y=b[-1],b[-2]
            del b[-1],b[-1]
print(c)

Submission status: Wrong Answer

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

dif = 0
for i in range(len(a)):
    min = a[int(i/2)]
    max = a[(-int((i+1)/2)-1)]
    dif += max - min
print(dif)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=[]
for i in range(n):
  l.append(int(input()))
l.sort()
if n%2==0:
  m=n//2
  sum1=0
  sum2=0
  for j in range(m):
    sum1 += l[j]
    sum2 += l[n-1-j]
  max=2*sum2-2*sum1-l[m]+l[m-1]
  print(max)
else:
  if n==3:
    max1=l[2]+l[1]-2*l[0]
    max2=2*l[2]-l[1]-l[0]
    if  (max1>max2):
      print(max1)
    else:
      print(max2)
  else:
    m=(n-1)//2
    sum1=0
    sum2=0
    for j in range(m+1):
      sum1 += l[j]
      sum2 += l[n-1-j]
    max1=2*sum2-l[m]-l[m+1]-2*sum1+2*l[m]
    max2=2*sum2-2*l[m]-2*sum1+l[m-1]+l[m-2]
    if  (max1>max2):
      print(max1)
    else:
      print(max2)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = []
for i in range(n):
    A.append(int(input()))
A.sort()
ans = 0
mi = 2000000000
t = [A[0], A[0]]
for i in range(1, n):
    if (i%2==0):
        u = A[i//2]
    else:
        u = A[-1-i//2]
    if (abs(u-t[0])>abs(u-t[1])):
        ans+=abs(u-t[0])
        if (mi>abs(u-t[0])):
            mi = abs(u-t[0]) 
        t[0] = u
    else:
        ans+=abs(u-t[1])
        if (mi>abs(u-t[1])):
            mi = abs(u-t[1]) 
        t[1] = u  
        
if (mi>abs(t[0]-t[1])):
    mi = abs(t[0]-t[1]) 
        
print(ans+abs(t[0]-t[1])-mi)

Submission status: Wrong Answer

Solution code:
n = int(input())

a = [0] * n

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

a.sort()

c = 0

if len(a) == 2:
  print(abs(a[0] - a[1]))

if len(a) == 3:
  diff = abs(a[0] - a[1]) + abs(a[1] - a[2])
  diff = max(abs(a[0] - a[2]) + abs(a[1] - a[2]), diff)
  diff = max(abs(a[0] - a[2]) + abs(a[0] - a[1]), diff)
  print(diff)

mini = 0
maxi = len(a) - 1

if len(a) > 3:
  (l, r) = (a[maxi], a[maxi - 1])
  maxi -= 2
  diff = abs(a[0] - l) + abs(a[0] - r)
  mini += 1
  for i in range(0, n - 3):
    if i % 2 == 0:
      if c < 2:
        diff += abs(a[mini] - l)
        l = a[mini]
        mini += 1
      else:
        diff += abs(a[maxi] - l)
        l = a[maxi]
        maxi -= 1
    else:
      if c < 2:
        diff += abs(a[mini] - r)
        r = a[mini]
        mini += 1
      else:
        diff += abs(a[maxi] - r)
        r = a[maxi]
        maxi -= 1
    if c < 3:
      c += 1
    else:
      c = 0
  
  maxi = len(a) - 1
  mini = 0
  (l, r) = (a[mini], a[mini + 1])
  mini += 2
  diff2 = abs(a[-1] - l) + abs(a[-1] - r)
  maxi -= 1
  for i in range(0, n - 3):
    if i % 2 == 0:
      if c > 1:
        diff2 += abs(a[mini] - l)
        l = a[mini]
        mini += 1
      else:
        diff2 += abs(a[maxi] - l)
        l = a[maxi]
        maxi -= 1
    else:
      if c > 1:
        diff2 += abs(a[mini] - r)
        r = a[mini]
        mini += 1
      else:
        diff2 += abs(a[maxi] - r)
        r = a[maxi]
        maxi -= 1
    if c < 3:
      c += 1
    else:
      c = 0

  print(max(diff, diff2))

Submission status: Wrong Answer

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

A = sorted(A)

ret = 0

# if N != len(list(set(A))):
#     tmp = -1
#     AA = []
#     Dup_num = 0
#     Dup_cnt = 0
#     for i,a in A:
#         if a != tmp:
#             AA.append(a)
#             tmp = a
#         else:
#             Dup_num = a
#             Dup_cnt = 0
#             while a == A[i+Dup_cnt]:
#                 Dup_cnt += 1

#             Dup_cnt -= 1
if N == 2:
    print(A[1] - A[0])
    exit()

if N == 3:
    print(A[1] + A[2] - A[0]*2)
    exit()
if N%2 == 1:

    smo = A[:N//2]
    lar = A[N//2:]
    lar = lar[::-1]

    ret += sum(lar)*2
    ret -= sum(smo)*2 - (smo[-1] + smo[-2])

else:
    smo = A[:N//2]
    lar = A[N//2:]

    lar = lar[::-1]

    ret += sum(lar)*2 - lar[-1]
    ret -= sum(smo)*2 - smo[-1]
    # for i in range(N//2-1):
    #     ret += abs(lar[i] + lar[i+1] - 2*smo[i])
    #     print(ret,lar[i],lar[i+1],smp[i])

    # ret += lar[i] - smo[i+1]

print(ret)


#     L = N//2 - 1
#     R = N//2 + 1
#     ret = abs(lar[0] + lar[1] - 2*mid)
#     for i in range(N//2):
#         ret +=
#     for i in range()
#     ans[N//2] = mid
#     for i in range(1,(N-1)//2):
#         if i%2 == 1:
#             if i%4 == 1:
#                 ans[L] = lar[]
#             elif i%4 == 3:
#                 ans[L] = A[-(i//2)]

#             L -= L

#         elif i%2 == 0:
#             ans[R] = A[i]
#             R += 1

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

Submission status: Wrong Answer

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

X=0
Y=0
if N % 2 == 0:
    for i in range(N//2-2):
        X = X + 2*A[i] -2*A[-i-1]
    X = X + A[N//2-1] -A[N//2]
    print(X)    

if N % 2 == 1:
        for i in range(N//2-1):
            X = X + 2 * A[i]
            Y = Y - 2 * A[-i-1]
        for i in range (N//2-2):
            X = X - 2 * A[-i-1]
            Y = Y + 2 * A[i]
        X = X - A[N//2] - A[N//2+1]
        Y = Y + A[N//2-1] + A[N//2]
        print(max(X,Y))

Submission status: Wrong Answer

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

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

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:
    ans[0] = 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:
#coding:utf-8
import copy
N = int(input())
A_list = []
for i in range(N):
    A_list.append(int(input()) )

def calculate_x(list):
    len_list = len(list)
    sum = 0
    for i in range(len_list-1):
        sum = sum + abs(list[i]-list[i+1])
    return sum

A_list = sorted(A_list)

if N % 2 ==0: #even
    answer_list = []
    A_min_list = A_list[:int(N/2)-1]
    A_max_list = A_list[-int(N/2)+1:]
    for max, min in zip(A_max_list, A_min_list):
        answer_list.append(max)
        answer_list.append(min)
    answer_list.append(A_list[int(N/2)]) #min of max_list
    answer_list.insert(0,A_list[int(N/2)-1]) #max of min_list
    tmp = calculate_x(answer_list)
    print(tmp)

else: #odd
    answer_list = []
    A_min_list = A_list[:int((N-1)/2)-1] #0,1
    A_max_list = A_list[-int((N-1)/2)+1:] #5,6
    x1 = A_list[int((N-1)/2)-1] #2
    x2 = A_list[int((N-1)/2)]#3
    x3 = A_list[int((N-1)/2)+1]#4
    for max, min in zip(A_max_list, A_min_list):
        answer_list.append(max)
        answer_list.append(min)

    answer_list2 = copy.deepcopy(answer_list)
    max = 0
    answer_list2.append(x1)
    answer_list2.insert(0,x2)
    answer_list2.insert(0,x3)
    tmp = calculate_x(answer_list2)
    if tmp > max:
        max = tmp

    answer_list2 = copy.deepcopy(answer_list)
    answer_list2.append(x1)
    answer_list2.insert(0,x3)
    answer_list2.insert(0,x2)
    tmp = calculate_x(answer_list2)
    if tmp > max:
        max = tmp

    answer_list2 = copy.deepcopy(answer_list)
    answer_list2.append(x2)
    answer_list2.insert(0,x1)
    answer_list2.insert(0,x3)
    tmp = calculate_x(answer_list2)
    if tmp > max:
        max = tmp

    answer_list2 = copy.deepcopy(answer_list)
    answer_list2.append(x2)
    answer_list2.insert(0,x3)
    answer_list2.insert(0,x1)
    tmp = calculate_x(answer_list2)
    if tmp > max:
        max = tmp

    answer_list2 = copy.deepcopy(answer_list)
    answer_list2.append(x3)
    answer_list2.insert(0,x1)
    answer_list2.insert(0,x2)
    tmp = calculate_x(answer_list2)
    if tmp > max:
        max = tmp

    answer_list2 = copy.deepcopy(answer_list)
    answer_list2.append(x3)
    answer_list2.insert(0,x2)
    answer_list2.insert(0,x1)
    tmp = calculate_x(answer_list2)
    if tmp > max:
        max = tmp



    answer_list3 = []
    A_min_list = A_list[:int((N-1)/2)-1] #0,1
    A_max_list = A_list[-int((N-1)/2)+1:] #5,6
    x1 = A_list[int((N-1)/2)-1] #2
    x2 = A_list[int((N-1)/2)]#3
    x3 = A_list[int((N-1)/2)+1]#4
    for max, min in zip(A_max_list, A_min_list):
        answer_list3.append(min)
        answer_list3.append(max)

    answer_list4 = copy.deepcopy(answer_list3)
    answer_list4.append(x1)
    answer_list4.insert(0,x2)
    answer_list4.insert(0,x3)
    tmp = calculate_x(answer_list4)
    if tmp > max:
        max = tmp

    answer_list4 = copy.deepcopy(answer_list3)
    answer_list4.append(x1)
    answer_list4.insert(0,x3)
    answer_list4.insert(0,x2)
    tmp = calculate_x(answer_list4)
    if tmp > max:
        max = tmp

    answer_list4 = copy.deepcopy(answer_list3)
    answer_list4.append(x2)
    answer_list4.insert(0,x1)
    answer_list4.insert(0,x3)
    tmp = calculate_x(answer_list4)
    if tmp > max:
        max = tmp

    answer_list4 = copy.deepcopy(answer_list3)
    answer_list4.append(x2)
    answer_list4.insert(0,x3)
    answer_list4.insert(0,x1)
    tmp = calculate_x(answer_list4)
    if tmp > max:
        max = tmp

    answer_list4 = copy.deepcopy(answer_list3)
    answer_list4.append(x3)
    answer_list4.insert(0,x1)
    answer_list4.insert(0,x2)
    tmp = calculate_x(answer_list4)
    if tmp > max:
        max = tmp

    answer_list4 = copy.deepcopy(answer_list3)
    answer_list4.append(x3)
    answer_list4.insert(0,x2)
    answer_list4.insert(0,x1)
    tmp = calculate_x(answer_list4)
    if tmp > max:
        max = tmp
    print(max)

Submission status: Wrong Answer

Solution code:
# coding:utf-8

n = int(input())

an = [int(input()) for _ in range(n)]

def func(sa, ba, i, flg=3):
    if flg==1:
        if i == 0:
            return ba[i] + ba[i+1] - 2*sa[0]
        elif i==len(sa)-1:
            return ba[i] - sa[i]
        else:
            return ba[i] + ba[i+1] - 2*sa[i]
    elif flg==2:
        if i==0:
            return ba[i] - sa[-1]
        elif i==len(sa)-1:
            return ba[i]+ba[i-1] -2*sa[0]
        else:
            return ba[i] + ba[i-1] - 2*sa[i]
    else:
        if i == 0:
            return ba[i] - sa[-1]
        elif i == len(sa)-1:
            return ba[i-1] - sa[-2]
        else:
            return ba[i-1]+ba[i]-2*sa[i-1]


def func1(sa, ba, k):
    num = 0
    num1 = 0
    num2 = 0
    if n%2 == 0:
        for i in range(k):
            num1 += func(sa, ba, i, 1)
        for i in range(k):
            num2 += func(sa, ba, i, 2)
        if num2 > num1:
            num = num2
        else:
            num = num1
    else:
        for i in range(k):
            num += func(sa, ba, i)
    return num

# 小さい順に並べ変え
an.sort()

num = 0
if n == 2:
    num = an[1] - an[0]
elif n == 3:
    num = an[2]+an[1] - 2*an[0]
else:
    if n%2 == 0:
        k = int(n/2)
        sa = an[:k]
        ba = an[k:]
    else:
        k = int(n/2)+1
        sa = an[:k]
        ba = an[k:]    
    ba.reverse()
    num = func1(sa, ba, k)



print(num)

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()
diff_list = []
if n%2 == 0:
    a_fw = a_list[:int(n/2)]
    a_ba = a_list[int(n/2):]
    fw_list1 = []
    ba_list1 = []
    fw_list2 = []
    ba_list2 = []
    for i in range(int(n/2)):
        fw_list1.append(a_fw[i])
        fw_list1.append(a_ba[i])
        ba_list1.append(a_ba[i])
        ba_list1.append(a_fw[i])
        fw_list2.append(a_fw[(-1)*(i+1)])
        fw_list2.append(a_ba[(-1)*(i+1)])
        ba_list2.append(a_ba[(-1)*(i+1)])
        ba_list2.append(a_fw[(-1)*(i+1)])
    v_fw1 = 0
    check_fw1 = fw_list1[0]
    v_ba1 = 0
    check_ba1 = ba_list1[0]
    v_fw2 = 0
    check_fw2 = fw_list2[0]
    v_ba2 = 0
    check_ba2 = ba_list2[0]
    for i in range(1, n):
        v_fw1 += abs(check_fw1-fw_list1[i])
        check_fw1 = fw_list1[i]
        v_ba1 += abs(check_ba1-ba_list1[i])
        check_ba1 = ba_list1[i]
        v_fw2 += abs(check_fw2-fw_list2[i])
        check_fw2 = fw_list2[i]
        v_ba2 += abs(check_ba2-ba_list2[i])
        check_ba2 = ba_list2[i]
    print(max(v_fw1, v_fw2, v_ba1, v_ba2))
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_list = [int(input()) for i in range(N)]
sum_num = sum(a_list)
mean = sum_num / N
a_list.sort()
tmp = a_list.copy()
a_list.reverse()

result = 0
if N % 2 == 0:
    if mean <= tmp[N//2-1]:
        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, a in enumerate(a_list):
            if i < N // 2:
                if i == 0:
                    result += a - tmp[i]
                    result += a - tmp[i + 1]
                else:
                    result += a - tmp[i + 1]
                    result += a - tmp[i - 1]
            else:
                break
else:
    if mean <= tmp[N // 2]:
        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-2] - t

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

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

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 27 19:18:58 2018
tenka1C
@author: maezawa
"""

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

a.sort()
b=a[::-1]
pos = n//2
res = 0
c = [-1]*n
sign = -1
for i in range(n):
    print(pos, i)
    if i%2 == 0:
        c[pos]=a[i]
        pos += sign*(i+1)
        if pos<0 or pos>=n:
            break
        c[pos]=b[i]
        pos = pos - sign*(i+2)
        if pos<0 or pos>=n:
            break
    else:
        c[pos]=b[i]
        pos += sign*(i+2)
        if pos<0 or pos>=n:
            break
        c[pos]=a[i]
        pos = pos - sign*(i+3)
        if pos<0 or pos>=n:
            break
        sign *= -1
                    
cnt = 0
for i in range(n-1):
    cnt += abs(c[i]-c[i+1])
print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
l=[int(input()) for i in range(N)]
l.sort()
res = 0
k = 0
o = 0
if N%2 == 0:
    a = l[:int(N/2)]
    a.reverse()
    b = l[int(N/2):]
    b.reverse()
    for i in range(N-1):
        res += b[k] - a[o]
        if i%2 == 0:
            o += 1
        if i%2 == 1:
            k += 1
else:
    a = l[:int(N/2)]
    b = l[int(N/2)+1:]
    b.reverse()
    for i in range(N-2):
        res += b[k] - a[o]
        if i%2 == 0:
            k += 1
        if i%2 == 1:
            o += 1
    res += max(abs(a[-1] - l[int(N/2)]), abs(b[-1] - l[int(N/2)]))
print(res)

Submission status: Wrong Answer

Solution code:
import numpy as np
from math import ceil

n = int(input())
w = []
for i in range(n):
    w.append(int(input()))
w = sorted(w)
l_fh = ceil(n/2)
w_fh = w[:l_fh]
w_sh = w[l_fh:][::-1]

w_ = np.zeros(n)
if n%2 != 0:
    fp = sp = l_fh-1
else:
    fp = sp = l_fh
fh_cnt = 0
sh_cnt = 0
flag = True

for i in range(n//2):
    if fp == sp:
        w_[fp] = w_fh[fh_cnt]
        fp -= 1
        sp += 1
        fh_cnt += 1
    if flag:
        w_[fp] = w_sh[sh_cnt]
        try:
            w_[sp] = w_sh[sh_cnt+1]
        except:
            break
        sh_cnt += 2
        fp -= 1
        sp += 1
        flag = False
    else:
        w_[fp] = w_fh[fh_cnt]
        try:
            w_[sp] = w_fh[fh_cnt+1]
        except:
            break
        fh_cnt += 2
        fp -= 1
        sp += 1
        flag = True
res = int(np.abs(np.diff(w_)).sum())
print(res)

Submission status: Wrong Answer

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

S = int(input())
lis = []
for item in range(0,int(S)):
    tmp = input()
    lis.append(int(tmp))
ls = sorted(lis)
#print(ls)
#print(ls[int(S/2)])
i = 0
lo = 0
hg = int(S)-1
ans =  ls[hg]- ls[int(S/2)]
#print("first",ans)
if S % 2 == 1 or S % 2 == 0:
    for k in range(0,int(S/2)+1):
        #print(i)
    # TODO: write code...
    # TODO: write code...
    # TODO: write code...
    #if(i != 0):
        #hg-=1
        if(i == int(S)-1):
            break
        ans = ans + ls[hg] - ls[lo]
        i+=1
        #print(ls[hg],ls[lo])
        hg-=1
        if(i == int(S)-1):
            break
        ans = ans + ls[hg] - ls[lo]
        #print(ls[hg],ls[lo])
        lo+=1
        i+=1

else:
    for i in range(0,int(S/2)+1):
    
    # TODO: write code...
    # TODO: write code...
    # TODO: write code...
    #if(i != 0):
        #hg-=1
        ans = ans + ls[hg] - ls[lo]
        print(ls[hg],ls[lo])
        if(hg == int(S/2)):
            break
        hg-=1
        ans = ans + ls[hg] - ls[lo]
        print(ls[hg],ls[lo])
        lo+=1



print(ans)
 #   hg-=1
 #   print(ls[hg] , ls[lo])
 #   ans= ans + (ls[hg] - ls[lo])
 #   print(ans)
 #   ans = ans + (ls[hg] - ls[lo])
 #   lo+=1
 #   hg-=1
 #   print(ls[hg] , ls[lo])
 #   print(ans)

Submission status: Wrong Answer

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

dif = 0
for i in range(len(a)):
    min = a[int(i/2)]
    max = a[(-int((i+1)/2)-1)]
    dif += max - min
print(dif)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(input()) for i in range(n)]
a.sort()
x=[]
y=[]
c=0
if n%2==1:
  for j in range(n//2):
    x.append(a[j])
  for k in range((n//2)+1,n):
    y.append(a[k])
else:
  for j in range((n//2)-1):
    x.append(a[j])
  for k in range((n//2),n):
    y.append(a[k])
y.reverse()
if n%2==1:
  for l in range(len(x)):
    c+=abs(x[l]-y[l])
  for m in range(len(x)-1):
    c+=abs(x[m]-y[m+1])
else:
  for l in range(len(x)):
    c+=abs(x[l]-y[l])
  for m in range(len(x)):
    c+=abs(x[m]-y[m+1])
if n%2==1:
  c+=max(abs(a[n//2]-y[0]),abs(a[n//2]-x[len(x)-1]))
else:
  c+=max(abs(a[(n//2)-1]-y[0]),abs(a[(n//2)-1]-y[len(y)-1]))
print(max(c,sum(a)-n))

Submission status: Wrong Answer

Solution code:
n = int(input())

a = [0] * n

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

a.sort()

c = 0

if len(a) == 2:
  print(abs(a[0] - a[1]))

if len(a) == 3:
  diff = abs(a[0] - a[1]) + abs(a[1] - a[2])
  diff = max(abs(a[0] - a[2]) + abs(a[1] - a[2]), diff)
  diff = max(abs(a[0] - a[2]) + abs(a[0] - a[1]), diff)
  print(diff)

mini = 0
maxi = len(a) - 1

if len(a) > 3:
  (l, r) = (a[maxi], a[maxi - 1])
  maxi -= 2
  diff = abs(a[0] - l) + abs(a[0] - r)
  mini += 1
  for i in range(0, n - 3):
    if i % 2 == 0:
      if c < 2:
        diff += abs(a[mini] - l)
        l = a[mini]
        mini += 1
      else:
        diff += abs(a[maxi] - l)
        l = a[maxi]
        maxi -= 1
    else:
      if c < 2:
        diff += abs(a[mini] - r)
        r = a[mini]
        mini += 1
      else:
        diff += abs(a[maxi] - r)
        r = a[maxi]
        maxi -= 1
    if c < 3:
      c += 1
    else:
      c = 0
  
  maxi = len(a) - 1
  mini = 0
  (l, r) = (a[mini], a[mini - 1])
  mini += 2
  diff2 = abs(a[-1] - l) + abs(a[-1] - r)
  maxi -= 1
  for i in range(0, n - 3):
    if i % 2 == 0:
      if c > 1:
        diff2 += abs(a[mini] - l)
        l = a[mini]
        mini += 1
      else:
        diff2 += abs(a[maxi] - l)
        l = a[maxi]
        maxi -= 1
    else:
      if c > 1:
        diff2 += abs(a[mini] - r)
        r = a[mini]
        mini += 1
      else:
        diff2 += abs(a[maxi] - r)
        r = a[maxi]
        maxi -= 1
    if c < 3:
      c += 1
    else:
      c = 0

  print(max(diff, diff2))

Submission status: Wrong Answer

Solution code:
n = int(input())
A = []
for i in range(n):
    A.append(int(input()))
A.sort()
ans = 0
mi = 2000000000
t = [A[0], A[0]]
for i in range(1, n):
    if (i%2==0):
        u = A[i//2]
    else:
        u = A[-1-i//2]
    if (abs(u-t[0])>abs(u-t[1])):
        ans+=abs(u-t[0])
        t[0] = u
    else:
        ans+=abs(u-t[1])
        t[1] = u  
    if (mi>abs(t[0]-t[1])):
        mi = abs(t[0]-t[1]) 
        
print(ans+abs(t[0]-t[1])-mi)

Submission status: Wrong Answer

Solution code:
from collections import deque
n = int(input())
a = [int(input()) for _ in range(n)]
a.sort()
que = deque(a)
right = que.pop()
left = que.popleft()
sums = right - left
while(bool(que)):
  a = que.pop()
  if abs(a-right) > abs(a-left):
    sums += abs(a-right)
    right = a
  else:
    sums += abs(a-left)
    left = a
  if not bool(que):
    break
  b = que.popleft()
  if abs(b-right) >= abs(b-left):
    sums += abs(b-right)
    right = b
  else:
    sums += abs(b-left)
    left = b
  if not bool(que):
    break
print(sums)

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)