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 sys
from collections import deque
import copy
import math
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 = []
    for i in range(N):
        input_raw = read_func().strip().split()
        A.append(long(input_raw[0]))

    A.sort()

    B = deque([])
    is_back = False
    for i in range(N):
        if len(B) == 0:
            B.append(A[i])
        else:
            if is_back == False:
                x = A[i/2]
            else:
                x = A[len(A) - 1 - i/2]

            if abs(B[0] - x) > abs(B[len(B) - 1] - x):
                B.appendleft(x)
            elif abs(B[0] - x) < abs(B[len(B) - 1] - x):
                B.append(x)
            else:
                if B[0] <= B[len(B) - 1]:
                    B.appendleft(x)
                else:
                    B.append(x)
        if is_back == False:
            is_back = True
        else:
            is_back = False
    score1 = 0L
    for i in range(N -1):
        score1 += abs(B[i] - B[i + 1])


    A.sort(reverse = True)

    B = deque([])
    is_back = False
    for i in range(N):
        if len(B) == 0:
            B.append(A[i])
        else:
            if is_back == False:
                x = A[i/2]
            else:
                x = A[len(A) - 1 - i/2]

            if abs(B[0] - x) > abs(B[len(B) - 1] - x):
                B.appendleft(x)
            elif abs(B[0] - x) < abs(B[len(B) - 1] - x):
                B.append(x)
            else:
                if B[0] <= B[len(B) - 1]:
                    B.append(x)
                else:
                    B.appendleft(x)
        if is_back == False:
            is_back = True
        else:
            is_back = False
    score2 = 0L
    for i in range(N -1):
        score2 += abs(B[i] - B[i + 1])

    print max(score1, score2)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
import copy
import math
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 = []
    for i in range(N):
        input_raw = read_func().strip().split()
        A.append(long(input_raw[0]))

    A.sort()

    B = deque([])
    is_back = False
    for i in range(N):
        if len(B) == 0:
            B.append(A[i])
        else:
            if is_back == False:
                x = A[i/2]
            else:
                x = A[len(A) - 1 - i/2]

            if abs(B[0] - x) > abs(B[len(B) - 1] - x):
                B.appendleft(x)
            elif abs(B[0] - x) < abs(B[len(B) - 1] - x):
                B.append(x)
            else:
                if B[0] <= B[len(B) - 1]:
                    B.appendleft(x)
                else:
                    B.append(x)
        if is_back == False:
            is_back = True
        else:
            is_back = False
    score1 = 0L
    for i in range(N -1):
        score1 += abs(B[i] - B[i + 1])


    A.sort(reverse = True)

    B = deque([])
    is_back = False
    for i in range(N):
        if len(B) == 0:
            B.append(A[i])
        else:
            if is_back == False:
                x = A[i/2]
            else:
                x = A[len(A) - 1 - i/2]

            if abs(B[0] - x) > abs(B[len(B) - 1] - x):
                B.appendleft(x)
            elif abs(B[0] - x) < abs(B[len(B) - 1] - x):
                B.append(x)
            else:
                if B[0] <= B[len(B) - 1]:
                    B.appendleft(x)
                else:
                    B.append(x)
        if is_back == False:
            is_back = True
        else:
            is_back = False
    score2 = 0L
    for i in range(N -1):
        score2 += abs(B[i] - B[i + 1])

    print max(score1, score2)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
import copy
import math
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 = []
    for i in range(N):
        input_raw = read_func().strip().split()
        A.append(long(input_raw[0]))

    A.sort()

    B = deque([])
    is_back = False
    for i in range(N):
        if len(B) == 0:
            B.append(A[i])
        else:
            if is_back == False:
                x = A[i/2]
            else:
                x = A[len(A) - 1 - i/2]

            if abs(B[0] - x) > abs(B[len(B) - 1] - x):
                B.appendleft(x)
            elif abs(B[0] - x) < abs(B[len(B) - 1] - x):
                B.append(x)
            else:
                if B[0] <= B[len(B) - 1]:
                    B.append(x)
                else:
                    B.appendleft(x)
        if is_back == False:
            is_back = True
        else:
            is_back = False
    score1 = 0L
    for i in range(N -1):
        score1 += abs(B[i] - B[i + 1])


    A.sort(reverse = True)

    B = deque([])
    is_back = False
    for i in range(N):
        if len(B) == 0:
            B.append(A[i])
        else:
            if is_back == False:
                x = A[i/2]
            else:
                x = A[len(A) - 1 - i/2]

            if abs(B[0] - x) > abs(B[len(B) - 1] - x):
                B.appendleft(x)
            elif abs(B[0] - x) < abs(B[len(B) - 1] - x):
                B.append(x)
            else:
                if B[0] <= B[len(B) - 1]:
                    B.appendleft(x)
                else:
                    B.append(x)
        if is_back == False:
            is_back = True
        else:
            is_back = False
    score2 = 0L
    for i in range(N -1):
        score2 += abs(B[i] - B[i + 1])

    print max(score1, score2)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
import copy
import math
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 = []
    for i in range(N):
        input_raw = read_func().strip().split()
        A.append(long(input_raw[0]))

    A.sort()

    B = deque([])
    is_back = False
    for i in range(N):
        if len(B) == 0:
            B.append(A[i])
        else:
            if is_back == False:
                x = A[i/2]
            else:
                x = A[len(A) - 1 - i/2]

            if abs(B[0] - x) >= abs(B[len(B) - 1] - x):
                B.appendleft(x)
            else:
                B.append(x)
        if is_back == False:
            is_back = True
        else:
            is_back = False
    score1 = 0L
    for i in range(N -1):
        score1 += abs(B[i] - B[i + 1])




    print score1


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
import copy
import math
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 = []
    for i in range(N):
        input_raw = read_func().strip().split()
        A.append(long(input_raw[0]))

    A.sort()

    B = deque([])
    is_back = False
    for i in range(N):
        if len(B) == 0:
            B.append(A[i])
        else:
            if is_back == False:
                x = A[i/2]
            else:
                x = A[len(A) - 1 - i/2]

            if abs(B[0] - x) >= abs(B[len(B) - 1] - x):
                B.appendleft(x)
            else:
                B.append(x)
        if is_back == False:
            is_back = True
        else:
            is_back = False
    score1 = 0L
    for i in range(N -1):
        score1 += abs(B[i] - B[i + 1])


    A.sort(reverse = True)

    B = deque([])
    is_back = False
    for i in range(N):
        if len(B) == 0:
            B.append(A[i])
        else:
            if is_back == False:
                x = A[i/2]
            else:
                x = A[len(A) - 1 - i/2]

            if abs(B[0] - x) >= abs(B[len(B) - 1] - x):
                B.appendleft(x)
            else:
                B.append(x)
        if is_back == False:
            is_back = True
        else:
            is_back = False
    score2 = 0L
    for i in range(N -1):
        score2 += abs(B[i] - B[i + 1])

    print max(score1, score2)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
import copy
import math
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 = []
    for i in range(N):
        input_raw = read_func().strip().split()
        A.append(long(input_raw[0]))

    A.sort()

    B = deque([])
    is_back = False
    for i in range(N):
        if len(B) == 0:
            B.append(A[i])
        else:
            if is_back == False:
                x = A[i/2]
            else:
                x = A[len(A) - 1 - i/2]

            if abs(B[0] - x) > abs(B[len(B) - 1] - x):
                B.appendleft(x)
            else:
                B.append(x)
        if is_back == False:
            is_back = True
        else:
            is_back = False
    score1 = 0L
    for i in range(N -1):
        score1 += abs(B[i] - B[i + 1])


    A.sort(reverse = True)

    B = deque([])
    is_back = False
    for i in range(N):
        if len(B) == 0:
            B.append(A[i])
        else:
            if is_back == False:
                x = A[i/2]
            else:
                x = A[len(A) - 1 - i/2]

            if abs(B[0] - x) >= abs(B[len(B) - 1] - x):
                B.appendleft(x)
            else:
                B.append(x)
        if is_back == False:
            is_back = True
        else:
            is_back = False
    score2 = 0L
    for i in range(N -1):
        score2 += abs(B[i] - B[i + 1])

    print max(score1, score2)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
import copy
import math
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 = []
    for i in range(N):
        input_raw = read_func().strip().split()
        A.append(long(input_raw[0]))

    A.sort()

    B = deque([])
    is_back = False
    for i in range(N):
        if len(B) == 0:
            B.append(A[i])
        else:
            if is_back == False:
                x = A[i/2]
            else:
                x = A[len(A) - 1 - i/2]

            if abs(B[0] - x) > abs(B[len(B) - 1] - x):
                B.appendleft(x)
            else:
                B.append(x)
        if is_back == False:
            is_back = True
        else:
            is_back = False
    score1 = 0
    for i in range(N -1):
        score1 += abs(B[i] - B[i + 1])


    A.sort(reverse = True)

    B = deque([])
    is_back = False
    for i in range(N):
        if len(B) == 0:
            B.append(A[i])
        else:
            if is_back == False:
                x = A[i/2]
            else:
                x = A[len(A) - 1 - i/2]

            if abs(B[0] - x) >= abs(B[len(B) - 1] - x):
                B.appendleft(x)
            else:
                B.append(x)
        if is_back == False:
            is_back = True
        else:
            is_back = False
    score2 = 0
    for i in range(N -1):
        score2 += abs(B[i] - B[i + 1])

    print max(score1, score2)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
import copy
import math
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 = []
    for i in range(N):
        input_raw = read_func().strip().split()
        A.append(long(input_raw[0]))

    A.sort()

    B = deque([])
    is_back = False
    for i in range(N):
        if len(B) == 0:
            B.append(A[i])
        else:
            if is_back == False:
                x = A[i/2]
            else:
                x = A[len(A) - 1 - i/2]

            if abs(B[0] - x) > abs(B[len(B) - 1] - x):
                B.appendleft(x)
            else:
                B.append(x)
        if is_back == False:
            is_back = True
        else:
            is_back = False
    score1 = 0
    for i in range(N -1):
        score1 += abs(B[i] - B[i + 1])


    A.sort(reverse = True)

    B = deque([])
    is_back = False
    for i in range(N):
        if len(B) == 0:
            B.append(A[i])
        else:
            if is_back == False:
                x = A[i/2]
            else:
                x = A[len(A) - 1 - i/2]

            if abs(B[0] - x) > abs(B[len(B) - 1] - x):
                B.appendleft(x)
            else:
                B.append(x)
        if is_back == False:
            is_back = True
        else:
            is_back = False
    score2 = 0
    for i in range(N -1):
        score2 += abs(B[i] - B[i + 1])

    print max(score1, score2)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

A.sort(reverse=True)
if len(A)%2==0:
  print(A[len(A)//2-1]-A[len(A)//2]+2*sum(A[:len(A)//2-1])-2*sum(A[len(A)//2+1:]))
else:
  print(-A[len(A)//2]-A[len(A)//2+1]+2*sum(A[:len(A)//2])-2*sum(A[len(A)//2+2:]))

Submission status: Wrong Answer

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

A.sort(reverse=True)
print(A)
if len(A)%2==0:
  print(A[len(A)//2-1]-A[len(A)//2]+2*sum(A[:len(A)//2-1])-2*sum(A[len(A)//2+1:]))
else:
  print(-A[len(A)//2]-A[len(A)//2+1]+2*sum(A[:len(A)//2])-2*sum(A[len(A)//2+2:]))

Submission status: Wrong Answer

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

A.sort(reverse=True)

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

Submission status: Wrong Answer

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

A.sort(reverse=True)

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

print(ans)

Submission status: Wrong Answer

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

A.sort(reverse=True)

ans=0
for i in range(len(A)):
  if i == 0:
    ans+=A[i]
  elif i == len(A)//2:
    if len(A)%2==0:
      ans-=A[i]
    else:
      ans+=A[i]
  elif i < len(A)//2:
    ans+=2*A[i]
  else:
    ans-=2*A[i]
print(ans)

Submission status: Wrong Answer

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

A.sort(reverse=True)

A=[a-A[-1] for a in A]
print(A[0]-A[len(A)//2])

Submission status: Wrong Answer

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

A.sort(reverse=True)

A=[a+min(A) for a in A]
print(A[0]-A[len(A)//2])

Submission status: Wrong Answer

Solution code:
# coding:utf-8

import sys
from collections import Counter, deque

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return input()


N = I()
A = [I() for _ in range(N)]

A.sort()
B = deque()

state = 1
for i in range(N // 2):
    if state:
        B.append(A[i])
        B.appendleft(A[-(i + 1)])
    else:
        B.append(A[-(i + 1)])
        B.appendleft(A[i])
    state ^= 1
if N % 2:
    B.append(A[N // 2])

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

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

a.sort()
aligned_a = [a[-1]]
a = a[:-1]

if len(a) == 2:
	aligned_a.insert(len(aligned_a), a[0])
	aligned_a.insert(len(aligned_a), a[1])
else:
	while True:
		if len(a) == 1:
			n1 = a[0]
			aligned_a.insert(0, n1)
			break

		n1, n2 = a[0], a[1]
		aligned_a.insert(len(aligned_a), n1)
		aligned_a.insert(0, n2)

		a = a[2:]
		if len(a) == 0:
			break

		if len(a) == 1:
			n1 = a[-1]
			aligned_a.insert(len(aligned_a), n1)
			break

		n1, n2 = a[-2], a[-1]
		aligned_a.insert(len(aligned_a), n1)
		aligned_a.insert(0, n2)

		a = a[:-2]

		if len(a) == 0:
			break

sum = 0
for e1, e2 in zip(aligned_a[:-1],aligned_a[1:]):
	sum += abs(e1 - e2)
print(sum)

Submission status: Wrong Answer

Solution code:
#yet
N = int(input())
M = N-1
n = N//2
m = M//2
A = [int(input()) for _ in range(N)]
A.sort(reverse=True)
A_ = []
num = 0

if N >= 4:
    A_.append(A[n])
    for _ in range(m):
        A_.append(A[_])
        A_.append(A[M-_])
    if n != m:
        A_.append(A[m])#A_完了
    for _ in range(M):
        num += (abs((A_[_])-(A_[_+1])))
    print(num)
elif N == 3:
    A[1],A[2] = A[2],A[1]
    abs1 = abs(A[0]-A[1])
    abs2 = abs(A[1]-A[2])
    print(abs1+abs2)

else:
    print(A[0]-A[1])

Submission status: Wrong Answer

Solution code:
N = int(input())

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

A.sort()
_A = []
_A.append(A.pop(0))
for i in range(N - 1):
    if i % 4 == 0:
        _A.insert(0, A.pop(len(A) - 1))
    if i % 4 == 1:
        _A.append(A.pop(len(A) - 1))
    if i % 4 == 2:
        _A.insert(0, A.pop(0))
    if i % 4 == 3:
        _A.append(A.pop(0))
        
diff = 0
for i in range(N - 1):
    diff += abs(_A[i] - _A[i + 1])

print(diff)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

A.sort()
_A = []
_A.append(A.pop(0))
for i in range(N - 1):
    print(i)
    if i % 4 == 3:
        _A.insert(0, A.pop(len(A) - 1))
    if i % 4 == 2:
        _A.append(A.pop(len(A) - 1))
    if i % 4 == 1:
        _A.insert(0, A.pop(0))
    if i % 4 == 0:
        _A.append(A.pop(0))

diff = 0
for i in range(N - 1):
    diff += abs(_A[i] - _A[i + 1])

print(diff)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import math
N=int(input())

list=[]

for i in range(N):
    list.append(int(input()))

sum_diff = 0
for i in range(len(list) -1):
    sum_diff += math.sqrt((list[i] - list[i+1]) ** 2)

print(int(sum_diff))

Submission status: Wrong Answer

Solution code:
import itertools

def getNext(tv, ta):
    maxv = abs(max(ta)-tv)
    minv = abs(tv-min(ta))
    if (maxv > minv):
        return max(ta)
    else:
        return min(ta)

n = int(input())

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

s = 0
tv = max(a)
a.remove(tv)
while(a):
    v = getNext(tv, a)
    s = s + abs(tv-v)
    a.remove(v)
    tv = v
print(s)
exit()

for i in range(len(a)):
    s = 0
    top = a[i]
    fv = top
    c = list(a)
    c.remove(fv)
    while(c):
        v = getNext(fv, c)   
#        print(fv,v)
        s = s + abs(fv-v)
        c.remove(v)
        fv = v
    if (s > ms):
        ms = s
print(ms)

Submission status: Wrong Answer

Solution code:
n , a = int(input()), []
for i in range(n):
    a.append(int(input()))
ans = 0
a.sort()
if n % 2 == 1:
    i, j = 0, n-1
    if n == 3:
        ans += abs(a[0]-a[-1]) + abs(a[0]- a[-2])
    else:
        while i + 2 <= j - 1:
            if i == 0:
                ans += abs(a[0]-a[-1]) + abs(a[0]- a[-2]) + abs(a[i+1] - a[-1]) + abs(a[i + 2] - a[-2])
                i = i + 1
                j = j - 2
            else:
                ans += abs(a[i] - a[j]) + abs(a[i + 1] - a[j - 1])
                if i + 2 < j - 1:
                    ans += abs(a[i + 2] - a[j]) + abs(a[i + 3] - a[j - 1])
                i = i + 2
                j = j - 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
a.sort()
ans = 0
if n % 2 == 0:
    if n == 2:
        ans = abs(a[0] - a[1])
    else:
        i , j = 0 , n-1
        while (i + 1) < j:
            if i == 0:
                ans += abs(a[i] - a[j]) + abs(a[i] - a[j-1])
                i = i + 1
                
            else:
                ans += abs(a[i] - a[j]) + abs(a[i+1] - a[j-1])
                i = i + 2
                j = j - 2
        if i == 1:
            ans += abs(a[i-1] - a[j])
        else:
            ans += abs(a[i -3 ] - a[j])
else:
    if n == 3:
        ans = abs(a[0] - a[1]) + abs(a[0] - a[2])
    else:
        i , j = 0 , n-1
        f = False
        if (n//2) % 2 != 0:
            f = True
        while i < j:
            if f and (i + 3 >= j):
                break
            if i == 0:
                ans += abs(a[i] - a[j]) + abs(a[i] - a[j-1])
                i = i + 1
                
            else:
                ans += abs(a[i] - a[j]) + abs(a[i+1] - a[j-1])
                i = i + 2
                j = j - 2
        if f:
            ans += abs(a[i-2] - a[j]) + abs(a[i-1] - a[j-1])
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
a.sort()
ans = 0
if n % 2 == 0:
    if n == 2:
        ans = abs(a[0] - a[1])
    else:
        i , j = 0 , n-1
        while (i + 1) < j:
            if i == 0:
                ans += abs(a[i] - a[j]) + abs(a[i] - a[j-1])
                i = i + 1
                
            else:
                ans += abs(a[i] - a[j]) + abs(a[i+1] - a[j-1])
                i = i + 2
                j = j - 2
        if i == 1:
            ans += abs(a[i-3] - a[j])
else:
    if n == 3:
        ans = abs(a[0] - a[1]) + abs(a[0] - a[2])
    else:
        i , j = 0 , n-1
        while (i + 1) < j:
            if i == 0:
                ans += abs(a[i] - a[j]) + abs(a[i] - a[j-1])
                i = i + 1
                
            else:
                ans += abs(a[i] - a[j]) + abs(a[i+1] - a[j-1])
                i = i + 2
                j = j - 2
        ans += abs(a[i-2] - a[j]) + abs(a[i-1] - a[j-1])
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
a.sort()
ans = 0
if n % 2 == 0:
    if n == 2:
        ans = abs(a[0] - a[1])
    else:
        i , j = 0 , n-1
        while (i + 1) < j:
            if i == 0:
                ans += abs(a[i] - a[j]) + abs(a[i] - a[j-1])
                i = i + 1
                
            else:
                ans += abs(a[i] - a[j]) + abs(a[i+1] - a[j-1])
                i = i + 2
                j = j - 2
    
        ans += abs(a[i-2-1] - a[j])
else:
    i , j = 0 , n-1
    while (i + 1) < j:
        if i == 0:
            ans += abs(a[i] - a[j]) + abs(a[i] - a[j-1])
            i = i + 1
            
        else:
            ans += abs(a[i] - a[j]) + abs(a[i+1] - a[j-1])
            i = i + 2
            j = j - 2
    
print(ans)

Submission status: Wrong Answer

Solution code:
emp_list = []
answer_list = []
 
n = int(input())
for i in range(n):
    i = int(input())
    emp_list.append(i)
emp_list.sort()
 
def min_list(the_list):
    if len(the_list) > 0:
        x = the_list.pop(0)
        return x
    else:
        pass
 
def max_list(the_list):
    if len(the_list) > 0:
        x = the_list.pop()
        return x
    else:
        pass
 
m = min_list(emp_list)
answer_list.append(m)
 
while emp_list:
    ma = max_list(emp_list)
    answer_list.append(ma)
    if len(emp_list) == 0:
        break
    else:
        mb = max_list(emp_list)
        answer_list.insert(0,mb)
    if emp_list == 0:
        break
    else:
        na = min_list(emp_list)
        answer_list.append(na)
    if emp_list == 0:
        break
    else:
        nb = min_list(emp_list)
        answer_list.insert(0,nb)
 
x_list = answer_list[:]
x_list = [x for x in x_list if x]
 
y_list = x_list[1:]
y_list = [y for y in y_list if y]

if len(x_list) == 3:
    y_list = y_list[::-1]
    y_list.insert(0,0)
cnt = 0
for xx,yy in zip(x_list,y_list):
    cnt += abs(xx - yy)
 
print(cnt)

Submission status: Wrong Answer

Solution code:
emp_list = []
answer_list = []
 
n = int(input())
for i in range(n):
    i = int(input())
    emp_list.append(i)
emp_list.sort()
 
def min_list(the_list):
    if len(the_list) > 0:
        x = the_list.pop(0)
        return x
    else:
        pass
 
def max_list(the_list):
    if len(the_list) > 0:
        x = the_list.pop()
        return x
    else:
        pass
 
m = min_list(emp_list)
answer_list.append(m)
 
while emp_list:
    ma = max_list(emp_list)
    answer_list.append(ma)
    if len(emp_list) == 0:
        break
    else:
        mb = max_list(emp_list)
        answer_list.insert(0,mb)
    if emp_list == 0:
        break
    else:
        na = min_list(emp_list)
        answer_list.append(na)
    if emp_list == 0:
        break
    else:
        nb = min_list(emp_list)
        answer_list.insert(0,nb)
 
x_list = answer_list[:]
x_list = [x for x in x_list if x]
 
y_list = x_list[1:]
y_list = [y for y in y_list if y]
 
cnt = 0
for xx,yy in zip(x_list,y_list):
    cnt += abs(xx - yy)
 
print(cnt)

Submission status: Wrong Answer

Solution code:
emp_list = []
answer_list = []

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

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

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

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

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

x_list = answer_list[:]
x_list = [x for x in x_list if x]

y_list = x_list[1:]
y_list = [y for y in y_list if y]

cnt = 0
for xx,yy in zip(x_list,y_list):
    cnt += abs(xx - yy)

print(cnt)

Submission status: Wrong Answer

Solution code:
emp_list = []
answer_list = []

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

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

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

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

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

x_list = answer_list[:]
x_list = [x for x in x_list if x]
x_list = x_list[:]
y_list = answer_list[1:]
y_list = [y for y in y_list if y]
y_list = y_list[1:]

cnt = 0
for xx,yy in zip(x_list,y_list):
    cnt += abs(xx - yy)

print(cnt)

Submission status: Wrong Answer

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

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

# 係数の決定
def keisu(n):
    ary = []
    # (a0 - a1) + (a2 - a1) + (a2 - a3) + ...
    for i in range(n):
        if i == 0:
            ary.append(1)
        elif i % 2 == 1:
            ary.append(-2)
        else:
            ary.append(2)
    ary[-1] = int(ary[-1]/2)
    return ary

n = int(input())
num = []
re  = [] # size:n , 1 < a_i < 10^9
for i in range(n):
    m = int(input())
    num.append(m)
    re.append(-1)

k = keisu(n)
num.sort(reverse=True)
for n in num:
    kk  = max(k)
    idx = k.index(kk)
    re[idx] = n
    k[idx] = -100

print(sum_diff(re))

Submission status: Wrong Answer

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

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

# # num配列の順列生成
# # a = itertools.permutations(num)
# a = itertools.combinations(num, n)
#
# # permutationオブジェクトをsetにしてtupleの重複排除、
# # 更にmapで各要素をlist化
# b = map(list, a)
#
# c = map(sum_diff, b)
# print(max(c))


# 係数の決定
def keisu(n):
    ary = []
    # (a0 - a1) + (a2 - a1) + (a2 - a3) + ...
    for i in range(n):
        if i == 0:
            ary.append(1)
        elif i % 2 == 1:
            ary.append(-2)
        else:
            ary.append(2)
    ary[-1] = int(ary[-1]/2)
    return ary

n = int(input())
num = []
re  = [] # size:n , 1 < a_i < 10^9
for i in range(n):
    m = int(input())
    num.append(m)
    re.append(-1)

k = keisu(n)
num.sort(reverse=True)
for n in num:
    kk  = max(k)
    idx = k.index(kk)
    re[idx] = n
    k[idx] = -100

print(sum_diff(re))