Problem:


Score : 400 points

Problem Statement

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

Constraints

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

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

Sample Input 1

5
6
8
1
2
3

Sample Output 1

21

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

Sample Input 2

6
3
1
4
1
5
9

Sample Output 2

25

Sample Input 3

3
5
5
1

Sample Output 3

8



Sample input:
5
6
8
1
2
3


Sample output:
21

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

for i in range(n):
    if(i == n-1):
        if(abs(B[0]-A[0])>abs(B[-1]-A[0])):
            B.insert(0, A.pop())
        else:
            B.append(A.pop())
    elif(i % 2 == 0):
        B.append(A.pop())
    else:
        B.append(A.pop(0))

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

B = sorted(A)
n = (N-1)//2
m = N//2
a = B[-1][0]+B[-2][0]-2*B[0][0]
if N == 2:
    print(B[1][0]-B[0][0])
if N == 3:
    print(B[1][0]+B[2][0]-2*B[0][0])
if N == 4:
    print(2*B[-1][0]+B[-2][0]-2*B[0][0]-B[1][0])
if N == 5:
    print(2*B[-1][0]+2*B[-2][0]-2*B[0][0]-B[1][0]-B[2][0])

if (N%2 ==1) and (N >=6):
    for i in range(1,n-1):
        a = a + B[-i][0] - B[i][0] + B[-i-2][0] - B[i][0]
    print(a + B[-(n-1)][0]-B[n-1][0]+B[-(n)][0]-B[n][0])
if (N%2 ==0) and (N>=6):
    for i in range(1,m-1):
        a = a + B[-i][0] - B[i][0] + B[-i-2][0] - B[i][0]
    print(a + B[-(m-1)][0] - B[m-1][0])

Submission status: Wrong Answer

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

B = sorted(A)
n = (N-1)//2
m = N//2
a = B[-1][0]+B[-2][0]-2*B[0][0]
if N == 2:
    print(B[1][0]-B[0][0])
if N == 3:
    print(B[1][0]+B[2][0]-2*B[0][0])
if N == 4:
    print(2*B[-1][0]+B[-2][0]-2*B[0][0]-B[1][0])
if N == 5:
    print(2*B[-1][0]+2*B[-2][0]-2*B[0][0]-B[1][0]-B[2][0])

if (N%2 ==1) and (N >=6):
    for i in range(1,n-1):
        a = a + B[-i][0] - B[i][0] + B[-i-2][0] - B[i][0]
    print(a)
if (N%2 ==0) and (N>=6):
    for i in range(1,m-1):
        a = a + B[-i][0] - B[i][0] + B[-i-2][0] - B[i][0]
    print(a + B[-(m-1)][0] - B[m-1][0])

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
def list_nums(x):
    nums = [x-i for i in range(x)]
    re_nums = []
    for i in range(x):
        re_nums.append(nums[((i+1)//2) * (-1 if i % 2 != 0 else 1)])
    return re_nums

def main():
    num_list = []
    for i in range(int(input())):
        num_list.append(int(input()))
    num_list.sort()
    last = [num_list[i * 2] for i in reversed(list_nums((len(num_list)-1)//2))]
    last.append(num_list[0])
    last += [num_list[i * 2 - 1] for i in list_nums(len(num_list)//2)]

    diff = 0
    for i in range(len(last)-1):
        diff += abs(last[i] - last[i+1])
    
    print(diff)

main()

Submission status: Wrong Answer

Solution code:
def list_nums(x):
    nums = [x-i for i in range(x)]
    print(nums)
    re_nums = []
    for i in range(x):
        re_nums.append(nums[((i+1)//2) * (-1 if i % 2 != 0 else 1)])
    print(re_nums)
    return re_nums

def main():
    num_list = []
    for i in range(int(input())):
        num_list.append(int(input()))
    num_list.sort()
    last = [num_list[i * 2] for i in reversed(list_nums((len(num_list)-1)//2))]
    print(last)
    last.append(num_list[0])
    last += [num_list[i * 2 - 1] for i in list_nums(len(num_list)//2)]
    print(last)

    diff = 0
    for i in range(len(last)-1):
        diff += abs(last[i] - last[i+1])
    
    print(diff)

main()

Submission status: Wrong Answer

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

#-2 -1 +1 +2
nums = [0,0,0,0]

if N%2 == 0:
    nums = [N//2-1,1,1,N//2-1]
else:
    a = 1 + (N-3)//4 *2
    d = (N-3 + 2)//4 *2
    b,c = (N-3)%4,(N-3 + 2)%4

    nums = [a,b,c,d]

ind = [0]

for n in nums:
    ind.append(ind[-1]+n)

prod = [-2,-1,1,2]
ans = 0
for i in range(4):
    ans += prod[i]*sum(A[ind[i]:ind[i+1]])

print(ans)

Submission status: Wrong Answer

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

#-2 -1 +1 +2
nums = [0,0,0,0]
if N%2 == 0:
    nums = [N//2-1,1,1,N//2-1]
else:
    a = 1 + (N-3)//4 *2
    d = (N-3 + 2)//4 *2
    if (N-3)%4 == 0:
        b,c = 0,2
    else:
        b,c = 2,0

    nums = [a,b,c,d]


prod = [-2]*nums[0]+[-1]*nums[1]+[1]*nums[2]+[2]*nums[3]

ans = 0
for i in range(N):
    ans += A[i]*prod[i]

print(ans)

Submission status: Wrong Answer

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

#-2 -1 +1 +2
nums = [0,0,0,0]
if N%2 == 0:
    nums = [N//2-1,1,1,N//2-1]
else:
    a,b,c,d = 1,0,2,0
    for i in range(1,(N-3)//2+1):
        print(i)
        b,c = c,b
        if i%2 == 0:
            a += 2
        else:
            d += 2
    nums = [a,b,c,d]

prod = [-2]*nums[0]+[-1]*nums[1]+[1]*nums[2]+[2]*nums[3]

ans = 0
for i in range(N):
    ans += A[i]*prod[i]

print(ans)

Submission status: Wrong Answer

Solution code:

S = int(input())
lis = []
for item in range(0,int(S)):
    tmp = input()
    lis.append(int(tmp))
ls = sorted(lis)
lsb = sorted(lis)

center = ls[int(S/2-0.5)]
ans1 = ls[-1] - center
ls.remove(center)
for k in range(0,int(S/2)):
    try:
        
        ans1 = ans1 + ls.pop(-1) - ls[0]
        ans1 = ans1 + ls[-1] -ls.pop(0)
    except:
        break
    
center = lsb[int(S/2)]
ans2 = center - lsb[0] 

lsb.remove(center)

for k in range(0,int(S/2)):
    try:
        ans2 = ans2 + lsb.pop(-1) - lsb[0] 
        ans2 = ans2 + lsb[-1] - lsb.pop(0) 
    except:
        break
print(max(ans1,ans2))

Submission status: Wrong Answer

Solution code:
# vim: set fileencoding=utf-8:
import collections


def main():
    # すべての数に対して、すべての数との差を計算する(10^5)か？TLEだ
    # 最大にするには、配列の中央の差が最大となるようにジグザグにしてけばいい
    N = input()
    A = sorted([input() for _ in xrange(N)])

    a = uzuamuki_sort(A, start="max", lr="right")
    b = uzuamuki_sort(A, start="max", lr="left")
    c = uzuamuki_sort(A, start="min", lr="right")
    d = uzuamuki_sort(A, start="min", lr="left")
    print(a)
    print(b)
    print(c)
    print(d)
    anss = [cal_ans(ele, N) for ele in [a, b, c, d]]
    print(anss)
    print(max(anss))


def cal_ans(arr, N):
    ans = 0
    for i in xrange(N - 1):
        ans += abs(arr[i] - arr[i + 1])

    return ans


def uzuamuki_sort(A, start, lr):
    deq = collections.deque(A)
    uzu = collections.deque()

    def max_lr():
        uzu.appendleft(deq.popleft())
        uzu.append(deq.popleft())

    def max_rl():
        uzu.append(deq.popleft())
        uzu.appendleft(deq.popleft())

    def min_lr():
        uzu.appendleft(deq.pop())
        uzu.append(deq.pop())

    def min_rl():
        uzu.append(deq.pop())
        uzu.appendleft(deq.pop())

    if start == "max":
        uzu.append(deq.popleft())
        if lr == "right":
            while True:
                try:
                    min_rl()
                    max_rl()
                except IndexError:
                    break
        elif lr == "left":
            while True:
                try:
                    min_lr()
                    max_lr()
                except IndexError:
                    break

    elif start == "min":
        uzu.append(deq.pop())
        if lr == "right":
            while True:
                try:
                    max_rl()
                    min_rl()
                except IndexError:
                    break
        elif lr == "left":
            while True:
                try:
                    max_lr()
                    min_lr()
                except IndexError:
                    break

    return list(uzu)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

if N%2 == 1:
    if N == 3:
        ans = A[2] + A[1] - 2*A[0]
    else:
        smo = A[:max(N//2-2,1)]
        mid = A[max(N//2-2,1):max(N//2-2,1)+2]
        lar = A[max(N//2-2,1)+2:]

        ans = -2*sum(smo) -1*sum(mid) + 2*sum(lar)

else:
    if N == 2:
        ans = A[1] - A[0]
    else:
        smo = A[:N//2]
        lar = A[N//2:]

        ans = -2*sum(smo[:-1]) -1*(smo[-1]) + 1*(lar[0]) + 2*sum(lar[1:])
        # print(smo[:-1],smo[-1],lar[0],lar[1:])

print(ans)

Submission status: Wrong Answer

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

if N%2 == 1:
    if N == 3:
        ans = sum(A) - 3*A[0]
    else:
        smo = A[:max(N//2-2,1)]
        mid = A[max(N//2-2,1):max(N//2-2,1)+2]
        lar = A[max(N//2-2,1)+2:]

        ans = -2*sum(smo) -1*sum(mid) + 2*sum(lar)

else:
    if N == 2:
        ans = A[1] - A[0]
    else:
        smo = A[:N//2]
        lar = A[N//2:]

        ans = -2*sum(smo[:-1]) -1*(smo[-1]) + 1*(lar[0]) + 2*sum(lar[1:])

print(ans)

Submission status: Wrong Answer

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

if N%2!=0: #kisu
  manaka=int(N/2)  
  sum_a=abs(num[manaka]-num[N-1])
  sum_a+=abs(num[N-1]-num[0])
  for i in range(int((N-1)/2)-1):
    sum_a+=abs(num[N-1-i]-num[i])
    sum_a+=abs(num[i]-num[N-2-i])
  sum_b=abs(num[manaka]-num[0])
  sum_b+=abs(num[0]-num[N-1])
  for i in range(int((N-1)/2)-1):
    sum_b+=abs(num[i]-num[N-1-i])
    sum_b+=abs(num[i+1]-num[N-1-i])
  print(max(sum_a,sum_b))
else:
  manaka=int(N/2)
  sum=abs(num[manaka]-num[0])
  for i in range(manaka-1):
    sum+=abs(num[i]-num[N-1-i])
    sum+=abs(num[i+1]-num[N-1-i])
  print(sum)

Submission status: Wrong Answer

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

A = sorted(A)
ans = 0

if N%2 == 1:
    if N == 3:
        ans = sum(A) - 3*A[0]
    else:
        smo = A[:max(N//2-2,1)]
        mid = A[max(N//2-2,1):max(N//2-2,1)+2]
        lar = A[max(N//2-2,1)+2:]

        ans = -2*sum(smo) -1*sum(mid) + 2*sum(lar)

else:
    if N == 2:
        ans = A[1] - A[0]
    else:
        smo = A[:N//2]
        lar = A[N//2:]

        ans = -2*sum(smo[:-1]) -1*(smo[-1]) + 1*(lar[0]) + 2*sum(lar[1:])

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
n=int(input())
a=[]
for i in range(0,n):
  a.append(int(input()))
a.sort()

b = [a[0]]
l = a[0]
r = a[0]
res=0
li=1
ri=len(a)-1

while ri>=li:
  al = a[ri]
  ri=ri-1
  lx = abs(l-al)
  rx = abs(r-al)

  if lx > rx:
    res = res + lx
    l = al
    if ri>=li:
      ta = a[ri]
      ri=ri-1
      res = res + abs(r-ta)
      r=ta
  else:
    res = res + rx
    r = al
    if ri>=li:
      ta = a[ri]
      ri=ri-1
      res = res + abs(l-ta)
      l = ta
  #
  if ri>=li:
    af = a[li]
    li=li+1
    ln = abs(l-af)
    rn = abs(r-af)
    if ln > rn:
      res = res + ln
      l = af
      if ri>=li:
        ta=a[li]
        li=li+1
        res=res+abs(r-ta)
        r=ta
    else:
      res = res + rn
      r = af
      if ri>=li:
        ta=a[li]
        li=li+1
        res=res+abs(l-ta)
        l=ta


print(res)

Submission status: Wrong Answer

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

A = sorted(A)
ans = 0

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

        ans = -2*sum(smo[:N//2-2 + 1]) -1*sum(smo[-2:]) + 2*sum(lar)

else:
    if N == 2:
        ans = A[1] - A[0]
    else:
        smo = A[:N//2]
        lar = A[N//2:]

        ans = -2*sum(smo[:-1]) -1*(smo[-1]) + 1*(lar[0]) + 2*sum(lar[1:])

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
n=int(input())
a=[]
for i in range(0,n):
  a.append(int(input()))
a.sort()

b = [a[0]]
l = a[0]
r = a[0]
a=a[1:]
res=0

while len(a)>0:
  al = a.pop()
  lx = abs(l-al)
  rx = abs(r-al)

  if lx > rx:
    res = res + lx
    l = al
    if len(a)>0:
      ta = a.pop()
      res = res + abs(r-ta)
      r=ta
  else:
    res = res + rx
    r = al
    if len(a)>0:
      ta = a.pop()
      res = res + abs(l-ta)
      l = ta

  if len(a)>0:
    af = a[0]
    a = a[1:]
    ln = abs(l-af)
    rn = abs(r-af)
    if ln > rn:
      res = res + ln
      l = af
      if len(a)>0:
        ta=a[0]
        res=res+abs(r-ta)
        r=ta
        a=a[1:]
    else:
      res = res + rn
      r = af
      if len(a)>0:
        ta=a[0]
        res=res+abs(l-ta)
        l=ta
        a=a[1:]


print(res)

Submission status: Wrong Answer

Solution code:
N = int(input().rstrip())
ins = list()
for _ in range(N):
    i = int(input().rstrip())
    ins.append(i)
ins.sort()
f, s = list(), list()
f.append(ins[0])
s.append(ins[0])
if len(ins) % 2 != 0:
    f.append(ins[0])
    s.append(ins[0])
    # for i in range(0, (len(ins) - 1) // 2, 2):
    #     f.append(ins[-1 - i])
    #     s.append(ins[-2 - i])
    #     if len(f) == (len(ins) + 1) // 2:
    #         break
    #     f.append(ins[1 + i])
    #     s.append(ins[2 + i])
    #     if len(f) == (len(ins) + 1) // 2:
    #         break
else:
    for i in range(0, (len(ins) - 2) // 2, 2):
        f.append(ins[-1 - i])
        s.append(ins[-2 - i])
        if len(f) == len(ins) // 2:
            f.append(ins[i + 1])
            break
        f.append(ins[1 + i])
        s.append(ins[2 + i])
        if len(f) == len(ins) // 2:
            f.append(ins[-i -1])
            break
sumf, sums = 0, 0
for i in range(len(f)-1):
    sumf += abs(f[i] - f[i + 1])
for i in range(len(s)-1):
    sums += abs(s[i] - s[i + 1])
print(sumf+sums)

Submission status: Wrong Answer

Solution code:
N = int(input().rstrip())
ins = list()
for _ in range(N):
    i = int(input().rstrip())
    ins.append(i)
ins.sort()
f, s = list(), list()
f.append(ins[0])
s.append(ins[0])
if len(ins) % 2 != 0:
    for i in range(0, (len(ins) - 1) // 2, 2):
        f.append(ins[-1 - i])
        s.append(ins[-2 - i])
        if len(f) == (len(ins) + 1) // 2:
            break
        f.append(ins[1 + i])
        s.append(ins[2 + i])
        if len(f) == (len(ins) + 1) // 2:
            break
else:
    for i in range(0, (len(ins) - 2) // 2, 2):
        f.append(ins[-1 - i])
        s.append(ins[-2 - i])
        if len(f) == len(ins) // 2:
            f.append(ins[i + 1])
            break
        f.append(ins[1 + i])
        s.append(ins[2 + i])
        if len(f) == len(ins) // 2:
            f.append(ins[-i -1])
            break
sumf, sums = 0, 0
for i in range(len(f)-1):
    sumf += abs(f[i] - f[i + 1])
for i in range(len(s)-1):
    sums += abs(s[i] - s[i + 1])
print(sumf+sums)

Submission status: Wrong Answer

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

A.sort()

max_list = []
i = 0
if len(A) % 2 == 1:
    if abs(A[-1] - A[int(len(A)/2)]) > abs(A[0] - A[int(len(A)/2)]):
        max_list.append(A.pop(int(len(A)/2)))
        while True:
            if i % 2 == 0:
                max_list.append(A.pop())
            else:
                max_list.append(A.pop(0))
            i += 1
            if len(A) ==0:
                break
    else:
        max_list.append(A.pop(int(len(A)/2)))
        while True:
            if i % 2 == 0:
                max_list.append(A.pop(0))
            else:
                max_list.append(A.pop())
            i += 1
            if len(A) ==0:
                break

else:
    p1, p2, p3, p4 = abs(A[-1] - A[int(len(A)/2)]), abs(A[0] - A[int(len(A)/2)]), \
                     abs(A[-1] - A[int(len(A) / 2) - 1]), abs(A[0] - A[int(len(A) / 2) - 1])
    maxx = 0
    target = 0
    for y, x in enumerate([p1, p2, p3, p4]):
        if x > maxx:
            maxx = x
            target = y

    if y == 0:
        max_list.append(A.pop(int(len(A)/2)))
        while True:
            if i % 2 == 0:
                max_list.append(A.pop(0))
            else:
                max_list.append(A.pop())
            i += 1
            if len(A) == 0:
                break
    elif y == 1:
        max_list.append(A.pop(int(len(A)/2)))
        while True:
            if i % 2 == 0:
                max_list.append(A.pop())
            else:
                max_list.append(A.pop(0))
            i += 1
            if len(A) == 0:
                break

    if y == 2:
        max_list.append(A.pop(int(len(A)/2)-1))
        while True:
            if i % 2 == 0:
                max_list.append(A.pop(0))
            else:
                max_list.append(A.pop())
            i += 1
            if len(A) ==0:
                break
    if y == 3:
        max_list.append(A.pop(int(len(A)/2)-1))
        while True:
            if i % 2 == 0:
                max_list.append(A.pop())
            else:
                max_list.append(A.pop(0))
            i += 1
            if len(A) == 0:
                break

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

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input().rstrip())
ins = list()
for _ in range(N):
    i = int(input().rstrip())
    ins.append(i)
ins.sort()
f, s = list(), list()
f.append(ins[0])
s.append(ins[0])
if len(ins) % 2 != 0:
    for i in range(0, (len(ins) - 1) // 2, 2):
        f.append(ins[-1 - i])
        s.append(ins[-2 - i])
        if len(f) == (len(ins) + 1) // 2:
            break
        f.append(ins[1 + i])
        s.append(ins[2 + i])
        if len(f) == (len(ins) + 1) // 2:
            break
else:
    for i in range(0, (len(ins) - 1) // 2, 2):
        f.append(ins[-1 - i])
        s.append(ins[-2 - i])
        if len(f) == len(ins) // 2:
            f.append(ins[i + 1])
            break
        f.append(ins[1 + i])
        s.append(ins[2 + i])
        if len(f) == len(ins) // 2:
            f.append(ins[-i -1])
            break
sumf, sums = 0, 0
for i in range(len(f)-1):
    sumf += abs(f[i] - f[i + 1])
for i in range(len(s)-1):
    sums += abs(s[i] - s[i + 1])
print(sumf+sums)

Submission status: Wrong Answer

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

c = int(N / 2) - 1

S = 0
for i, a in enumerate(A):
        if i < c:
            S -= 2 * a
        elif i == c:
            S -= a
        elif i == c + 1:
            S += a
        else:
            S += 2 * a

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 :
    for k in range(0,int(S/2)):
        #print(i)
    # TODO: write code...
    # TODO: write code...
    # TODO: write code...
    #if(i != 0):
        #hg-=1
        if(i == int(S)):
            break
        ans = ans + ls[hg] - ls[lo]
        i+=1
        #print(ls[hg],ls[lo])
        hg-=1
        #print(i)
        #print(ans)
        if(i == int(S)-1):
            break
        ans = ans + ls[hg] - ls[lo]
        #print(ls[hg],ls[lo])
        lo+=1
        i+=1
        #print(ans)


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:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
n=int(input())
a=[]
for i in range(0,n):
  a.append(int(input()))
a.sort()

b = [a[0]]
a=a[1:]

f=True
while len(a)>0:
  lx = abs(b[0]-a[len(a)-1])
  rx = abs(b[len(b)-1]-a[len(a)-1])

  if lx > rx:
    b.insert(0,a.pop())
    if len(a)>0:
      b.append(a.pop())
  else:
    b.append(a.pop())
    if len(a)>0:
      b.insert(0,a.pop())
  if len(a)>0:
    ln = abs(b[0]-a[0])
    rn = abs(b[len(b)-1]-a[0])
    if ln > rn:
      b.insert(0,a[0])
      a=a[1:]
      if len(a)>0:
        b.append(a[0])
        a=a[1:]
    else:
      b.append(a[0])
      a=a[1:]
      if len(a)>0:
        b.insert(0,a[0])
        a=a[1:]

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

Submission status: Wrong Answer

Solution code:
def findbest(liste, i):
	lister = liste[:]
	lister.remove(i)
	returnlister = [i]
	t = len(lister)
	output = 0
	while(t >= 2):
		if abs(i - lister[-1]) >= abs(i - lister[0]):
			returnlister.append(lister.pop(-1))
		else:
			returnlister.append(lister.pop(0))
		i = returnlister[-1]
		t -= 1
	returnlister.extend(lister)
	
	for i in range(len(returnlister) - 1):
		output += abs(returnlister[i] - returnlister[i + 1])
	return output
 

a = int(input())
numlist = []
for i in range(a):
	numlist.append(int(input()))
 
numlist = sorted(numlist)
possible = list(set(numlist))
maxer = 0
if len(possible) <= 3:
	for i in possible:
		maxer = max(findbest(numlist, i), maxer)
else:
	for i in possible[len(possible)//2 - 1: len(possible)//2]:
		maxer = max(findbest(numlist, i), maxer)
		
print (maxer)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
A.sort()
ans = 0
xx = A[0]
yy = A[0]
for i in range(1, N // 2 + 1):
    x = A[i*(-1)**i]
    ans += abs(xx - x)
    xx = x
    if i % 2 == 1:
        y = A[-(i + 1)]
    else:
        y = A[i - 1]
    ans += abs(y-yy)
    yy = y
    if i == N // 2 - 1 and N % 2 == 0:
        i += 1
        z = A[i*(-1)**i]
        ans += max(abs(z - xx), abs(z - yy))
        break
print(ans)

Submission status: Wrong Answer

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

tmplist.sort(reverse=True)

l=[]
ia=0
id=n-1
for i in range(n):
    if ia == id:
        if abs(l[0] - tmplist[ia]) > abs(l[-1] - tmplist[ia]):
            l.insert(0,tmplist[ia])
        else:
            l.append(tmplist[ia])
    elif i % 2 == 0:
        l.append(tmplist[ia])
        ia += 1
    else:
        l.append(tmplist[id])
        id -= 1

ans = 0;
for i in range(n-1):
    ans += abs(l[i] - l[i+1])
print(ans)

Submission status: Wrong Answer

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

c = int((N-1) / 2)
S = 0

for i, a in enumerate(A):
        if i < c:
            S -= 2 * a
        elif (i == c):
            S += a
        else:
            S += 2 * a

print(S)

Submission status: Wrong Answer

Solution code:
n = int(input()) 
a = [int(input()) for i in range(n)] 
a.sort()
b=[]
b.append(a.pop(0))
while(True):
 try:
  b.append(a.pop())
  b.insert(0,a.pop())
  b.append(a.pop(0))
  b.insert(0,a.pop(0))
 except:
  break
s = 0
for i in range(len(b)-1):
  s += abs(b[i]-b[i+1])
print(s)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)] 
 
A = sorted(A)
res = 0
A2 = []
for i in range(len(A)):
    if len(A) % 2 == 1 and len(A) == 1:
        if abs(A[0] - A2[0]) < abs(A[0] - A2[-1]):
            A2.append(A[0])
        else:
            A2.insert(0,A[0])
        break
    if i % 2 == 0:
        A2.append(A.pop(-1))
    else:
        A2.append(A.pop(0))
for i in range(len(A2)-1):
    res += abs(A2[i] - A2[i+1])

print(res)

Submission status: Wrong Answer

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

if N%2:
    
    m1=inp[:(N//2+1)]
    M1=inp[(N//2+1):]
    
    m2=inp[:N//2]
    M2=inp[N//2:]
    
    a1=sum(M1)*2-sum(m1)*2+m1[-1]+m1[-2]
    a2=sum(M2)*2-sum(m2)*2-M2[0]-M2[1]
    
    print(max(a1,a2))
    
else:
    
    m=inp[:N//2]
    M=inp[:N//2]
    
    a=sum(M)*2-sum(m)+m[-1]-M[0]
    
    print(a)

Submission status: Wrong Answer

Solution code:
N = int(input().rstrip())
ins = list()
for _ in range(N):
    i = int(input().rstrip())
    ins.append(i)
ins.sort()
f, s = list(), list()
f.append(ins[0])
s.append(ins[0])
if len(ins) % 2 != 0:
    for i in range(0, (len(ins) - 1) // 2, 2):
        f.append(ins[-1 - i])
        s.append(ins[-2 - i])
        if len(f) == (len(ins) + 1) // 2:
            break
        f.append(ins[1 + i])
        s.append(ins[2 + i])
        if len(f) == (len(ins) + 1) // 2:
            break
else:
    for i in range(0, (len(ins) - 1) // 2, 2):
        f.append(ins[-1 - i])
        s.append(ins[-2 - i])
        if len(f) == len(ins) // 2:
            f.append(ins[i + 2])
            break
        f.append(ins[1 + i])
        s.append(ins[2 + i])
        if len(f) == len(ins) // 2:
            f.append(ins[-i - 2])
            break
sumf, sums = 0, 0
for i in range(len(f)-1):
    sumf += abs(f[i] - f[i + 1])
for i in range(len(s)-1):
    sums += abs(s[i] - s[i + 1])
print(sumf+sums)

Submission status: Wrong Answer

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

A = sorted(A)

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:]

    ret = sum(lar)*2 - (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] - (sum(smo)*2 - smo[-1])

print(ret)

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]
if N == 3:
    if A[0] - last > last - A[-1]:
        ans += (A[0] - last)
    else:
        ans += (last - A[-1])
else:
    ans += (A[0] - last)
if N == 2:
    ans = A[0] - A[-1]
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input().rstrip())
ins = list()
for _ in range(N):
    i = int(input().rstrip())
    ins.append(i)
ins.sort()
f, s = list(), list()
f.append(ins[0])
s.append(ins[0])
if len(ins) % 2 != 0:
    for i in range(0, (len(ins) - 1) // 2, 2):
        f.append(ins[-1 - i])
        s.append(ins[-2 - i])
        if len(f) == (len(ins) + 1) // 2:
            break
        f.append(ins[1 + i])
        s.append(ins[2 + i])
        if len(f) == (len(ins) + 1) // 2:
            break
else:
    for i in range(0, (len(ins) - 1) // 2, 2):
        f.append(ins[-1 - i])
        s.append(ins[-2 - i])
        if len(f) == len(ins) // 2:
            f.append(ins[1 + i + 2])
            break
        f.append(ins[1 + i])
        s.append(ins[2 + i])
        if len(f) == len(ins) // 2:
            f.append(ins[-1 - i - 2])
            break
sumf, sums = 0, 0
for i in range(len(f)-1):
    sumf += abs(f[i] - f[i + 1])
for i in range(len(s)-1):
    sums += abs(s[i] - s[i + 1])
print(sumf+sums)

Submission status: Wrong Answer

Solution code:
# 与えられた整数列をソートした後、N/2個目の数値をスタートとして、一番遠い値を取得していく。
N = int(input())
L = []
for i in range(N):
    L.append(int(input()))
L.sort()
M = L.copy()
# L = [1, 2, 3, 6, 8]
# N = 5
ans1 = 0
ans = 0

# 奇数個の場合
if N % 2 == 0:
    target_num = L.pop(N // 2 - 1)
    ans1 = 0  # 差の合計
    # 一番前の数値と比べる
    if target_num - L[0] > L[-1] - target_num:
        # 2番目を先頭の数値にする場合
        i = 1
    else:
        # 2番目を最後尾の数値にする場合
        i = 0

    # リストから数値がなくなるまでループする
    while len(L) > 0:
        if i % 2 == 0:
            # 一番後ろの数値との差を取得
            ans1 = ans1 + (L[-1] - target_num)
            # 一番後ろの数値をリストから削除
            target_num = L.pop(-1)
        else:
            # 一番前の数値との差を取得
            ans1 = ans1 + (target_num - L[0])
            # 一番前の数値をリストから削除
            target_num = L.pop(0)
        i += 1

L = M.copy()
target_num = L.pop(N // 2)
ans = 0 # 差の合計

# 一番前の数値と比べる
if target_num - L[0] > L[-1] - target_num:
    # 2番目を先頭の数値にする場合
    i = 1
else:
    # 2番目を最後尾の数値にする場合
    i = 0

# リストから数値がなくなるまでループする
while len(L) > 0:
    if i % 2 == 0:
        # 一番後ろの数値との差を取得
        ans = ans + (L[-1] - target_num)
        # 一番後ろの数値をリストから削除
        target_num = L.pop(-1)
    else:
        # 一番前の数値との差を取得
        ans = ans + (target_num - L[0])
        # 一番前の数値をリストから削除
        target_num = L.pop(0)
    i += 1

print(max(ans1, ans))