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:
x =input()
x = int(x)
y = []
for i in range(x):
    z = input()
    z = int(z)
    y.append(z)
y = sorted(y)
sum = 0
left =[]
right = []
center = y[0]
for i in range(int(x/2)):
    left.append(y[2*i+1])
    if x <= 2*i+2: break
    right.append(y[2*i+2])
b = len(left)
prev = y[0]
for i in range(b):
    if not left: break
    a =left.pop()
    sum += abs(prev - a)
    prev = a
    if not left : break
    a= left.pop(0)
    sum += abs(prev - a)
    prev = a
b = len(right)
prev = y[0]
for i in range(b):
    if not right: break
    a =right.pop()
    sum+= abs(prev-a)
    prev = a
    if not right : break
    a= right.pop(0)
    sum+= abs(prev-a)
    prev = a
print(sum)

Submission status: Wrong Answer

Solution code:
N = int(input())

num = []
for i in range(N):
  num.append(int(input()))
  
num = sorted(num)
ans = [num[0]]

for i in range(len(num)-1):
  if i % 4 == 0:
    ans.append(num[-i//2-1])
  elif i % 4 == 1:
    ans = [num[-i//2-2]] + ans
  elif i % 4 == 2:
    ans.append(num[(i-1)//2+1])
  else:
    ans = [num[(i-1)//2+2]] + ans

answer = max(abs(num[2//N]-ans[0]), abs(num[2//N]-ans[-1]))
for i in range(len(ans)-1):
  answer += abs(ans[i] - ans[i+1])
  
print(answer)

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 = [int(input()) for i in range(n)]
a.sort()

print(a)

if n%2 != 0:
        m = int(n/2)
        x = 0
        y = 0
        z = max(a[2*m]-a[m], a[m]-a[0])
        for i in range(m):
                x += (a[2*m-i] - a[i])
        for i in range(m-1):
                y += (a[2*m-1-i] - a[i])
        print(x+y+z)
elif n%2 == 0:
        m = int(n/2)
        x = 0
        y = 0
        for i in range(m-1):
                x += (a[2*m-1-i] - a[i])
        for i in range(m-2):
                y += (a[2*m-2-i] - a[i])
        print(x+y+a[m]-a[m-2]+a[2*m-1]-a[m-1])

Submission status: Wrong Answer

Solution code:
print(22)

Submission status: Wrong Answer

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

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

    num_l = sorted(num_l)

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

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

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

        flag = not flag

    print(max_num)

if __name__ == "__main__":
    main()

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)

b = []
b.append(a[0])
for i in range(len(a)-1):
    if i % 4 < 2:
        if i % 2 == 0:
            b.append(a[-(int(i/2)+1)])
        else:
            b.insert(0, a[-(int(i/2)+2)])
    else:
        if i % 2 == 1:
            b.insert(0, a[int(i/2)+1])
        else:
            b.append(a[int(i/2)-1])

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

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[0] - l[int(N/2)]), abs(b[0] - l[int(N/2)]))
print(res)

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 + 1:
                if i == 0:
                    result += a_list[i] - t
                    result += a_list[i+1] - t

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

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

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

            else:
                break
    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:
N = int(input())
A = [int(input()) for i in range(N)]
S = 0
A.sort()
if N%2!=0:
  n=round((N-1)/2)
else:
  n=round((N/2)-1)
if A[N-1] - A[n] > A[n] - A[0]:
  S = S+A[N-1]-A[n]
  x=0
  y=-1
  for i in range(N-2):
    if i%2 == 0:
      y=y+1
    else:
      x=x+1
    S = S+A[N-1-x]-A[0+y]
if A[n] - A[0] > A[N-1] - A[n]:
  S = S+A[n]-A[0]
  x=-1
  y=0
  for i in range(N-2):
    if i%2 == 0:
      x=x+1
    else:
      y=y+1
    S = S+A[N-1-x]-A[0+y]
print(S)

Submission status: Wrong Answer

Solution code:
from collections import deque
N = int(input())
a = []
for _ in range(N):
    a.append(int(input()))
a.sort()
dq = deque()
j = 0
k = N-1
for i in range(N):
    if i%4 == 0:
        dq.append(a[j])
        j += 1
    if i%4 == 3:
        dq.appendleft(a[j])
        j += 1
    if i%4 == 1:
        dq.appendleft(a[k])
        k -= 1
    if i%4 == 2:
        dq.append(a[k])
        k -= 1
ans = 0
v = dq.popleft()
for w in dq:
    ans += abs(v-w)
    v = w
print(ans)

Submission status: Wrong Answer

Solution code:
def t118C():
    N = int(input())
    A = [int(input()) for i in range(N)]
    A.sort()
    anslist = []
    ans = 0
    tmplist = []
    tmplist2 = []

    up = 0
    down = N - 1
    checklist = [0 for i in range(N)]
    while True:
        if checklist[up] == 1:
            break
        tmplist.append(up)
        checklist[up] = 1
        up += 1

        if checklist[down] == 1:
            break
        tmplist2.append(down)
        checklist[down] = 1
        down -= 1

        if checklist[down] == 1:
            break
        tmplist.append(down)
        checklist[down] = 1
        down -= 1

        if checklist[up] == 1:
            break
        tmplist2.append(up)
        checklist[up] = 1
        up += 1

    for i in range(len(tmplist)):
        tmplist[i] = A[tmplist[i]]

    for i in range(len(tmplist2)):
        tmplist2[i] = A[tmplist2[i]]

    #print(tmplist, tmplist2)
    for i in range(1, len(tmplist)):
        ans += abs(tmplist[i] - tmplist[i - 1])
    for i in range(1, len(tmplist2)):
        ans += abs(tmplist2[i] - tmplist2[i - 1])
    ans += abs(tmplist[0] - tmplist2[0])
    print(ans)
t118C()

Submission status: Wrong Answer

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

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

    num_l = sorted(num_l)

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

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

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

        flag = not flag

    print(max_num)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import collections

N = int(input())

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


A = sorted(A)

if N % 2 == 1:
    d1 = A[0:N // 2 + 1]
    u1 = A[N // 2 + 1:N + 1]
else:
    d1 = A[0:N // 2]
    u1 = A[N // 2:N + 1]

d1 = collections.deque(d1)
u1 = collections.deque(u1)
d1.rotate(1)
u1.rotate(1)

sum1 = 0
i = 0
while (True):
    print("{},{},{}".format(i, len(d1), len(u1)))
    if i >= len(d1) or i >= len(u1):
        break
    sum1 += abs(u1[i] - d1[i])

    if i+1 >= len(d1) or i >= len(u1):
        break
    sum1 += abs(u1[i] - d1[i + 1])
    i += 1



print(sum1)

Submission status: Wrong Answer

Solution code:
# input n
n = int(input())

# input a(n times)
a = list()
for idx in range(n):
    a.append(int(input()))

# 
a.sort()

# Init
b = [0] * n

# Calc center
idx = int((n / 2)+0.5) - 1
b[idx] = a[0]

incr = 1
pflg = False

maxidx = n-1
minidx = 1
mflg = False

for loopidx in range(n - 1):
    if mflg == False:
        if pflg == False:
            b[idx + incr] = a[maxidx]
            pflg = True
        else:
            b[idx - incr] = a[maxidx]
            pflg = False
            mflg = True
            incr = incr + 1
        maxidx = maxidx - 1
    else:
        if pflg == False:
            b[idx + incr] = a[minidx]
            pflg = True
        else:
            b[idx - incr] = a[minidx]
            pflg = False
            mflg = False
            incr = incr + 1
        minidx = minidx + 1

# Sum & Output
sum = 0
for idx in range(n - 1):
    sum = sum + (abs(b[idx] - b[idx+1]))

print(sum)

Submission status: Wrong Answer

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

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

    num_l = sorted(num_l)

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

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

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

        flag = not flag

    print(max_num)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
l = []
ans = 0
for i in range(n):
    l.append(int(input()))
m = sum(l) / n
large_l = []
small_l = []
zero_c = 0
for i in range(n):
    if l[i] == m:
        zero_c += 1
    if l[i] > m:
        large_l.append(abs(l[i]-m))
    else:
        small_l.append(abs(l[i]-m))
large_l.sort()
small_l.sort()
if zero_c > 1:
    if n % 2 == 0:
        for i in range(zero_c):
            if len(large_l) > len(small_l):
                small_l.append(0)
            else:
                large_l.append(0)
        small_l.sort()
        large_l.sort()
    else:
        for i in range(zero_c):
            if len(large_l) > len(small_l):
                small_l.append(0)
                small_l.sort()
            if len(large_l) < len(small_l):
                large_l.append(0)
                large_l.sort()
            else:
                if large_l[0] > small_l[0]:
                    small_l.append(0)
                    small_l.sort()
                else:
                    large_l.append(0)
                    large_l.sort()
if n % 2 == 1:
    if len(large_l) < len(small_l):
        ans += small_l[0] + small_l[1]
        for i in range(2, len(small_l)):
            ans += 2 * small_l[i]
        for i in range(len(large_l)):
            ans += 2 * large_l[i]
    else:
        ans += large_l[0] + large_l[1]
        for i in range(len(small_l)):
            ans += 2 * small_l[i]
        for i in range(2, len(large_l)):
            ans += 2 * large_l[i]
else:
    ans += large_l[0] + small_l[0]
    for i in range(1, len(small_l)):
        ans += 2 * small_l[i]
    for i in range(1, len(large_l)):
        ans += 2 * large_l[i]
print(int(ans))

Submission status: Wrong Answer

Solution code:
from sys import stdin
il = lambda func=int: list(map(func, stdin.readline().rstrip().split()))
ils = lambda n, s="int(input())": [eval(s) for _ in range(n)]

n = int(input())
a = ils(n)
a = sorted(a)
ans = 0

for i in range(n//2):
    if i % 2 == 0:
        if i == 0:
            ans += abs(a[0] - a[-1-i])
            if n & 1 or i != n//2:
                ans += abs(a[0] - a[-2-i])
        else:
            ans += abs(a[i-1] - a[-1-i])
            if n & 1 or i != n//2:
                ans += abs(a[i] - a[-2-i])
    else:
        ans += abs(a[i] - a[-1-(i-1)])
        if n & 1 or i != n//2:
            ans += abs(a[i+1] - a[-2-(i-1)])
print(ans)

Submission status: Wrong Answer

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

ass = 0
    
ans = [a[0]]
w = 1
while w < n:
    if w == 1:
        ass += a[n-1]-a[0]
    elif w == 2:
        ass += a[n-2]-a[0]
    else:
        if w % 4 == 1:
            ass += abs(a[n-1-(w//4)*2] -a[(w//4)*2-1])
        elif w % 4 == 2:
            ass += abs(a[n-2-(w//4)*2] -a[(w//4)*2])
        elif w % 4 == 3:
            ass += abs(a[1+(w//4)*2] - a[n-1-(w//4)*2])
        else:
            ass += abs(a[(w//4)*2] - a[n-(w//4)*2])
    w += 1
jkl = [ass]

ass = 0
ans = [a[n-1]]
w = 1
while w < n:
    if w == 1:
       ass += a[n-1]-a[0]
    elif w == 2:
        ass += a[n-1] -a[1]
    else:
        if w % 4 == 1:
            ass += abs(a[0+(w//4)*2]-a[n-(w//4)*2])
        elif w % 4 == 2:
            ass += abs(a[1+(w//4)*2] -a[n-(w//4)*2])
        elif w % 4 == 3:
            ass += abs(a[n-2-(w//4)*2]-a[(w//4)*2])
        else:
            ass += abs(a[n-1-(w//4)*2]-a[(w//4)*2-1])
    w += 1
jkl.append(ass)

print(max(jkl))

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [0]*N
result = []
for i in range(N):
 a[i] = int(input())
a = sorted(a)
if N%2 != 0:
 a_sub = sorted(a,key=lambda x: -x)
 a_0 =a[N//2]
 for j in range(N//2):
  if j %2 ==0:
   result.insert(0,a_sub[j])
   result.append(a[j])
  else:
   result.insert(0,a[j])
   result.append(a_sub[j])
 tmp1 = abs(result[0] - a_0)
 tmp2 = abs(result[N-2] -a_0)
 if tmp1 < tmp2:
  result.append(a_0)  
 else:
  result.insert(0,a_0)
else:
 a_sub = sorted(a,key=lambda x: -x)
 for j in range(N//2):
  if j %2 ==0:
   result.insert(0,a_sub[j])
   result.append(a[j])
  else:
   result.insert(0,a[j])
   result.append(a_sub[j])
result_sum = 0
print(result)
for k in range(N-1):
 result_sum = result_sum + abs(result[k] - result[k+1])
print(result_sum)

Submission status: Wrong Answer

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

A.sort()

le = len(A) // 2

chiisaiA = A[:le]
ookiiA = A[le::][::-1]

result = [None]*(len(A))
result[1::2] = chiisaiA
result[::2] = ookiiA

res = []
for i in range(len(A)-1):
    res.append(abs(result[i] - result[i+1]))

res.append(abs(result[0] - result[len(A)-1]))
res.sort()

print(sum(res) - res[0])

Submission status: Wrong Answer

Solution code:
n = int(input())
aa = [int(input()) for i in range(0, n)]
aa.sort()
ana = [0] * n
ana[0] = aa[n//2-1]
ana[n-1] = aa[n//2]
for i in range(0, (n//2)-1):
    ana[2+2*i] = aa[i]
    ana[1+2*i] = aa[i + (n//2)+1]
if n % 2 != 0:
    ana[n-2] = aa[n-1]
if n == 3:
    ana[0] = aa[1]
    ana[1] = aa[0]
    ana[2] = aa[2]

a = 0
for i in range(1, len(ana)):
    a += abs(ana[i-1] - ana[i])

print(a)

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

  print(diff)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = []
for i in range(0,n):
    a.append(int(input()))
a.sort()
if len(a) == 2:
    print(abs(a[0]-a[1]))
    exit()
ans = 0
ne = [a.pop(-1)]
ne.append(a.pop(-1))
ans += ne[0]-a[0]+ne[1]-a[0]
a.pop(0)
while len(a) > 1:
    if max(abs(ne[0]-a[0])+abs(ne[1]-a[1]),abs(ne[0]-a[1])+abs(ne[1]-a[0])) > max(abs(ne[0]-a[-1])+abs(ne[1]-a[-2]),abs(ne[0]-a[-2])+abs(ne[1]-a[-1])):
        ans += max(abs(ne[0]-a[0])+abs(ne[1]-a[1]),abs(ne[0]-a[1])+abs(ne[1]-a[0]))
        ne[0] = a.pop(0)
        ne[1] = a.pop(0)
    else:
        ans += max(abs(ne[0]-a[-1])+abs(ne[1]-a[-2]),abs(ne[0]-a[-2])+abs(ne[1]-a[-1]))
        ne[0] = a.pop(-1)
        ne[1] = a.pop(-1)
if len(a) == 1:
    ans += max(abs(ne[0]-a[0]),abs(ne[1]-a[0]))
print(ans)

Submission status: Wrong Answer

Solution code:
N = input()
A = [int(input()) for i in range(N)]
 
res = []
begin = 0
end = len(A) - 1
res =[]
res.append(A[begin])
res.append(A[end])
begin += 1
end -= 1

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

flag = False
if (N - 7) % 4 == 0:
    flag = True

if (flag):
    li = s - 1
    ri = s + 1
else:
    li = s
    ri = s + 1

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

if N % 2 == 1:
    if (flag):
        ans[-1] = m[-1]
    else:
        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:
n=int(input())
#整数のリスト(li)と並び替えた後のリスト(li2)を作る
li=[]
li2=[]
k=0 #正解を出すための並び替えの時に使う
m=n #nの数値を後でいじるので退避しておく
answer=0 #答えの出力に使う
for i in range(n):
    li.append(int(input()))
#liを昇順にソート
li.sort()
#真ん中に1番小さい数を置く
li2.append(li[0])
li.pop(0)
n=n-1
#liに入っている残りの数を並べ替え
for j in range(n):
    if j%2==0:
        li2.insert(len(li2)-k,li[j])
    else:
        li2.insert(k,li[j])
        k=k+1
#mが偶数の時、li2の1番最後の数を真ん中+1の所に入れる
if m%2==0:
    a=li2[n]
    li2.pop(n)
    li2.insert(int(m/2)+1,a)
#li2の隣り合う要素の差の絶対値の和をanswerに入れる
for l in range(n):
    answer=answer+abs(li2[l]-li2[l+1])
print(answer)

Submission status: Wrong Answer

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

big = D[num:]
small = D[:num]
small_1 = small[::2]
small_2 = small[1::2]
small_2.reverse()
big.reverse()
big_1 = big[::2]
big_2 = big[1::2]
big_1.reverse()

big_new = big_1 + big_2
small_new = small_2 + small_1
if len(big_new)%2 == 1:
    small_new.reverse()

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

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

Submission status: Wrong Answer

Solution code:
n = int(input())
aa = [int(input()) for i in range(n)]
sa=sorted(aa)
# print(sa)
aaa=[]
if n%2 ==0:
    for i in range(n):
        if i%2 == 0:
            aaa.append(sa.pop(0))
        else:
            aaa.append(sa.pop(-1))
else:
    for i in range(n):
        if i%2 == 1:
            aaa.append(sa.pop(0))
        else:
            aaa.append(sa.pop(-1))
# print(aaa)
if abs(aaa[0]-aaa[-1])>abs(aaa[-1]-aaa[-2]):
    len_a = len(aaa)
    aaa.extend(aaa)
    aaa=aaa[len_a-1:len(aaa)-1]
# print(aaa)
ans=0
for i in range(n-1):
    ans += abs(aaa[i]- aaa[i+1])
print(ans)

Submission status: Wrong Answer

Solution code:
a = int(input())

b = []

for i in range(a):
    b.append(int(input()))

b.sort()

c = []

if a % 2 == 0:
  k = a//2
else:
  k = a//2+1

bb = b[0:k]

bbb = b[k:]
j = len(bb)-1
jj = len(bbb)
bbb.reverse()
i = 0
ii = 0
c.append(bb[-1])
for llll in range(a-1):
    if i != j and ii != jj:
        p = abs(c[-1]-bb[i])
        pp = abs(c[-1]-bbb[ii])
        if max(abs(c[-1]-bb[i]),abs(c[-1]-bbb[ii])) == p:
            c.append(bb[i])
            i += 1
        else:
            c.append(bbb[ii])
            ii += 1
    elif i == j:
        c.append(bbb[ii])
        ii += 1
    elif ii == jj:
        c.append(bb[i])
        i += 1

su = 0
for i in range(a-1):
    su += abs(c[i]-c[i+1])
print(su)

Submission status: Wrong Answer

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

res = []
begin = 0
end = len(A) - 1
res = deque()
res.append(A[begin])
res.append(A[end])
begin += 1
end -= 1

Submission status: Wrong Answer

Solution code:
from sys import stdin
il = lambda func=int: list(map(func, stdin.readline().rstrip().split()))
ils = lambda n, s="int(input())": [eval(s) for _ in range(n)]

n = int(input())
a = ils(n)
a = sorted(a)
ans = 0

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

Submission status: Wrong Answer

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

big = D[num:]
small = D[:num]
small_1 = small[::2]
small_2 = small[1::2]
small_2.reverse()
big.reverse()
big_1 = big[::2]
big_2 = big[1::2]
big_1.reverse()

big_new = big_1 + big_2
small_new = small_2 + small_1
if len(big_new)%2 == 1:
    small_new.reverse()

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

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = sorted([int(input()) for x in range(n)])
ans = 0
if n % 2 != 0:
    a1 = a[n // 2]
    del a[n // 2]
else:
    a1 = a[n // 2 - 1]
    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]

print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = []
ans = 0

for _ in range(N):
    A.append(int(input()))

A.sort()
if len(A) % 2 == 1:
    ans += (A[-1] - A[0])
    ans += (A[-2] - A[0])
    A.pop(0)
    while A:
        ans += (A[-1] - A[0])
        A.pop(-1)
        A.pop(0)
else:
    ans += (A[-1] - A[0])
    ans += (A[-2] - A[0])
    A.pop(0)
    while A:
        ans += (A[-1] - A[0])
        A.pop(-1)
        if A:
            ans += (A[-2] - A[0])
            A.pop(0)
        else:
            pass
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [0]*N
result = []
for i in range(N):
 a[i] = int(input())
a = sorted(a)
if N%2 != 0:
 a_sub = sorted(a,key=lambda x: -x)
 a_0 =a[N//2]
 for j in range(N//2):
  if j %2 ==0:
   result.insert(0,a_sub[j])
   result.append(a[j])
  else:
   result.insert(0,a[j])
   result.append(a_sub[j])
 if a_0 > result[0] and a_0 < result[N-2]:
  result.append(a_0)  
 else:
  result.insert(0,a_0)
else:
 a_sub = sorted(a,key=lambda x: -x)
 for j in range(N//2):
  if j %2 ==0:
   result.insert(0,a_sub[j])
   result.append(a[j])
  else:
   result.insert(0,a[j])
   result.append(a_sub[j])
result_sum = 0
for k in range(N-1):
 result_sum = result_sum + abs(result[k] - result[k+1])
print(result_sum)

Submission status: Wrong Answer

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

A = sorted(A)
res = []
begin = 0
end = len(A) - 1
res = []
res.append(A[begin])
res.append(A[end])
begin += 1
end -= 1

Submission status: Wrong Answer

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

A.sort()
Ad = deque(A)
dif = 0
left = right = Ad.popleft()
while Ad:
    n = Ad.pop()
    if abs(left-n) > abs(right-n):
        dif += abs(left-n)
        left = n
    else:
        dif += abs(right-n)
        right = n
    if Ad:
        n = Ad.popleft()
        if abs(left-n) > abs(right-n):
            dif += abs(left-n)
            left = n
        else:
            dif += abs(right-n)
            right = n
    else:
        break
print(dif)

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]
else:
    a1 = a[n // 2 - 1]
    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]

print(ans)

Submission status: Wrong Answer

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

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

    num_l = sorted(num_l)

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

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

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

        f = tmp_f
        s = tmp_s

        flag = not flag

    print(max_num)

if __name__ == "__main__":
    main()