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:
from collections import deque

n=int(input())
a=[int(input()) for _ in range(n)]
a.sort()
d=deque()
d.append(a[0])
d.append(a[-1])
b=[False]*n
b[0]=True
b[-1]=True

for i in range(1,n-1):
    if b[i] is False:
        if abs(a[i]-d[0])>=abs(a[i]-d[-1]):
            d.appendleft(a[i])
        else:
            d.append(a[i])
        b[i]=True
    else:
        break

    if b[-1-i] is False:
        if abs(a[-1-i]-d[0])>=abs(a[-1-i]-d[-1]):
            d.appendleft(a[-1-i])
        else:
            d.append(a[-1-i])
        b[-1-i]=True
    else:
        break

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

Submission status: Wrong Answer

Solution code:
from collections import deque 
n = int(input())
d = deque()
l=[]
for i in range(n):
  a = int(input())
  l.append(a)
l.sort()
l = deque(l)
flag=0
flag1=0
q = l.popleft()
d.append(q)
while l:
  if flag:
    a = l.popleft()
    if abs(d[0]-a)<abs(d[-1]-a):
      d.append(a)
      flag1=0
    else:
      d.appendleft(a)
      flag1=1
    if l:
      b = l.popleft()
      if flag1:
        d.append(b)
      else:
        d.appendleft(b)
    flag = 0
  else:
    a =l.pop()
    if abs(d[0]-a)<abs(d[-1]-a):
      d.append(a)
      flga1 = 0
    else:
      d.appendleft(a)
      flag1=1
    if l:
      b = l.pop()
      if flag1:
        d.append(b)
      else:
        d.appendleft(b)
    flag = 1
ans = 0
for i in range(n-1):
  ans+=abs(d[i+1]-d[i])
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque 
n = int(input())
d = deque()
l=[]
for i in range(n):
  a = int(input())
  l.append(a)
l.sort()
l = deque(l)
flag=0
q = l.popleft()
d.append(q)
while l:
  if flag:
    a = l.popleft()
    if abs(d[0]-a)<abs(d[-1]-a):
      d.append(a)
    else:
      d.appendleft(a)
    if l:
      b = l.popleft()
      if abs(d[0]-b)<abs(d[-1]-b):
        d.append(b)
      else:
        d.appendleft(b)
    flag = 0
  else:
    a =l.pop()
    if abs(d[0]-a)<abs(d[-1]-a):
      d.append(a)
    else:
      d.appendleft(a)
    if l:
      b = l.pop()
      if abs(d[0]-b)<abs(d[-1]-b):
        d.append(b)
      else:
        d.appendleft(b)
    flag = 1    
ans = 0
#print(d)
for i in range(n-1):
  ans+=abs(d[i+1]-d[i])
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque 
n = int(input())
d = deque()
l=[]
for i in range(n):
  a = int(input())
  l.append(a)
l.sort()
l = deque(l)
l1 = n-1
r1 = 1
flag=0
q = l.popleft()
d.append(q)
while l:
  if flag:
    a = l.popleft()
    if l:
      b = l.popleft()
      if abs(d[0]-b)<abs(d[-1]-b):
        d.append(b)
      else:
        d.appendleft(b)
    if abs(d[0]-a)<abs(d[-1]-a):
      d.append(a)
    else:
      d.appendleft(a)
    flag = 0
  else:
    a =l.pop()
    if l:
      b = l.pop()
      if abs(d[0]-b)<abs(d[-1]-b):
        d.append(b)
      else:
        d.appendleft(b)
    if abs(d[0]-a)<abs(d[-1]-a):
      d.append(a)
    else:
      d.appendleft(a)
    flag = 1    
ans = 0
#print(d)
for i in range(n-1):
  ans+=abs(d[i+1]-d[i])
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque 
n = int(input())
d = deque()
l=[]
for i in range(n):
  a = int(input())
  l.append(a)
l.sort()
l = deque(l)
l1 = n-1
r1 = 1
flag=0
q = l.popleft()
d.append(q)
while l:
  if flag:
    a = l.popleft()
    if l:
      b = l.popleft()
      d.appendleft(b)
    d.append(a)
    flag = 0
  else:
    a =l.pop()
    if l:
      b = l.pop()
      d.appendleft(b)
    d.append(a)
    flag = 1    
ans = 0
#print(d)
for i in range(n-1):
  ans+=abs(d[i+1]-d[i])
print(ans)

Submission status: Wrong Answer

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


r[0] = A[N//2]
r[-1] = A[N//2-1]

for i in range(N-2):
    idx = i // 2
    if i % 2 == 0:
        r[i+1] = A[N-idx-1]
    else:
        r[i+1] = A[idx]

tmp = -1
ret = 0
for i in r:
    if tmp == -1:
        tmp = i
    else:
        ret += abs(tmp-i)
        tmp = i
print(ret)

Submission status: Wrong Answer

Solution code:
n = int(input())
lst = [int(input()) for _ in range(n)]
lst.sort()
print(lst)
if n%2==0:
    lower = lst[:n//2]
    upper = lst[n//2:]
    x = 2*sum(upper) - upper[0]
    y = 2*sum(lower) - lower[-1]
    print(x-y)
else:
    lower = lst[:n//2]
    upper = lst[n//2+1:]
    mid = lst[n//2]
    x = 2*sum(upper) - 2*sum(lower) - mid + lower[-1]
    y = 2*sum(upper) - 2*sum(lower) + mid - upper[0]
    print(max(x,y))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
a = [int(input()) for i in range(n)]
a.sort()
b = []
c = []
for i in range(n):
  if i%2:
    b.append(a[i//2])
    c.append(a[i//2+(n+1)//2])
  else:
    b.append(a[i//2+n//2])
    c.append(a[i//2])
if n>=3 and n%2:
  b[-1],b[-3] = b[-3],b[-1]
  c[0],c[2] = c[2],c[0]
ansb = 0
ansc = 0
for i in range(n-1):
  ansb += abs(b[i+1]-b[i])
  ansc += abs(c[i+1]-c[i])
print(max(ansb,ansc))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
a = [int(input()) for i in range(n)]
a.sort()
b = []
c = []
for i in range(n):
  if i%2:
    b.append(a[i//2])
    c.append(a[i//2+(n+1)//2])
  else:
    b.append(a[i//2+n//2])
    c.append(a[i//2])

ansb = 0
ansc = 0
for i in range(n-1):
  ansb += abs(b[i+1]-b[i])
  ansc += abs(c[i+1]-c[i])
print(max(ansb,ansc))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
a = [int(input()) for i in range(n)]
a.sort()
b = []
for i in range(n):
  if i%2:
    b.append(a[i//2])
  else:
    b.append(a[i//2+n//2])
ans = 0
for i in range(n-1):
  ans += abs(b[i+1]-b[i])
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
a = [int(input()) for i in range(n)]
a.sort()
b = []
for i in range(n):
  if i%2 == 0:
    b.append(a[i//2])
  else:
    b.append(a[i//2+(n+1)//2])
ans = 0
for i in range(n-1):
  ans += abs(b[i+1]-b[i])
print(ans)

Submission status: Wrong Answer

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

A.sort()

ans = 0
if N % 2 == 0:
    for i in range(N//2,N):
        if i == N // 2:
            ans += A[i]
        else:
            ans += 2 * A[i]
    for i in range(0,N//2):
        if i == N // 2 - 1:
            ans -= A[i]
        else:
            ans -= 2 * A[i]
else:
    for i in range(N//2,N):
        if i == N // 2 or i == N // 2 + 1:
            ans += A[i]
        else:
            ans += 2 * A[i]
    for i in range(0,N//2):
        ans -= 2 * A[i]

if N == 3:
    ans = max(ans, abs(A[2] - A[0]) + abs(A[2] - A[1]))

print(ans)

Submission status: Wrong Answer

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

A.sort()

u = A[N//2 + 1:]
d = A[:N//2 - 1]
ans = [A[N//2]]

if N % 2 == 0:
    for i in range(N-2):
        j = i // 2
        if i % 2 == 0:
            ans.append(d[j])
        else:
            ans.append(u[j])
else:
    for i in range(N-2):
        j = i // 2
        if i % 2 == 0:
            ans.append(u[j])
        else:
            ans.append(d[j])
    
ans.append(A[N//2 - 1])
k = 0
for i in range(N-1):
    k += abs(ans[i] - ans[i+1])
if N == 3:
    k = max(k, A[2]+A[1]-A[0]*2)

print(k)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = [int(input()) for _ in range(n)]
A.sort()
l = 0
if n % 2 == 0:
  r = n // 2
  c = 1
elif A[n // 2] - A[n // 2 - 1] > A[n // 2 + 1] - A[n // 2]:
  r = n // 2
  c = 1
else:
  r = n // 2 + 1
  c = 0

total = 0
for i in range(n - 1):
  total += A[r] - A[l]
  if (i + c) % 2 == 0:
    l += 1
  else:
    r += 1
print(total)

Submission status: Wrong Answer

Solution code:
from collections import deque

n = int(input())
A = [int(input()) for _ in range(n)]
A.sort()
l1, r1 = A[0], A[0]
l2, r2 = A[-1], A[-1]
total1, total2 = 0, 0
for i in range(1, n):
  if i % 2 == 0:
    a1 = A[i // 2]
    a2 = A[n - (i + 2) // 2]
  if i % 2 == 1:
    a1 = A[n - (i + 1) // 2]
    a2 = A[i // 2]
  if abs(l1 - a1) < abs(r1 - a1):
    total1 += abs(r1 - a1)
    r1 = a1
  else:
    total1 += abs(l1 - a1)
    l1 = a1
  if abs(l2 - a2) < abs(r2 - a2):
    total2 += abs(r2 - a2)
    r2 = a2
  else:
    total2 += abs(l2 - a2)
    l2 = a2
print(max(total1, total2))

Submission status: Wrong Answer

Solution code:
from collections import deque

n = int(input())
A = [int(input()) for _ in range(n)]
A.sort()
l = A[0]
r = A[0]
total = 0
for i in range(1, n):
  if i % 2 == 0:
    a = A[i // 2]
  if i % 2 == 1:
    a = A[n - (i + 1) // 2]
  if abs(l - a) < abs(r - a):
    total += abs(r - a)
    r = a
  else:
    total += abs(l - a)
    l = a
print(total)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(input()) for _ in range(n)]
a.sort()
if n==2:
  x=a[1]-a[0]
elif n%2==0:
  x=(sum(a[n//2+1:])-sum(a[:n//2-1]))*2
  x-=a[n//2-1]
  x+=a[n//2]
elif n==3:
  x=a[2]*2-a[0]-a[1]
  y=a[2]+a[1]-a[0]*2
  x=max(x,y)
else:
  x=(sum(a[n//2+2:])-sum(a[:n//2]))*2
  x+=a[n//2]+a[n//2+1]
  y=(sum(a[n//2+1:])-sum(a[:n//2-1]))*2
  y-=a[n//2]-a[n//2-1]
  x=max(x,y)
print(x)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[0]*n
for  i in range(n):
    a[i]=int(input())
a.sort(reverse=True)
ans=0
aupper=a[:n//2]
adowner=a[n//2:]
if len(a)==2:print(a[0]-a[1])
elif len(a)==3:
    print(sum(a[:n-1])-a[n-1]*2)
elif n%2==1:
    ans1=0;ans2=0
    for i in range(n//2):
        ans1+=aupper[i]*2-(adowner[i]+adowner[i+1])
    ans1+=(adowner[1]-adowner[-1])
    a=a[::-1]
    adowner=a[:n//2]
    aupper=a[n//2:]
    for i in range(n//2):
        ans2+=aupper[i]+aupper[i+1]-adowner[i]*2
    ans2+=aupper[-1]-aupper[1]
    print(max(ans1,ans2))
else:
    ans=0
    for i in range(n//2):
        if i!=n//2-1:
            ans+=aupper[i]*2-(adowner[i]+adowner[i+1])
        else:
            ans+=aupper[i]-adowner[i]
    print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = []
for i in range(n):
  a.append(int(input()))
a.sort()
m = sum(a) / n
if n % 2 == 0:
  print(2*sum(a[n//2+1:])-2*sum(a[:n//2-1])-a[n//2-1]+a[n//2])
else:
  print(min(2*sum(a[n//2+1:])-2*sum(a[:n//2-1])-a[n//2-1]-a[n//2],2*sum(a[n//2+2:])-2*sum(a[:n//2])+a[n//2]+a[n//2+1]))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = []
for i in range(n):
  a.append(int(input()))
a.sort()
m = sum(a) / n
if n % 2 == 0:
  print(2*sum(a[n//2+1:])-2*sum(a[:n//2-1])-a[n//2-1]+a[n//2])
else:
  if a[n//2] <= m:
    print(2*sum(a[n//2+1:])-2*sum(a[:n//2-1])-a[n//2-1]-a[n//2])
  else:
    print(2*sum(a[n//2+2:])-2*sum(a[:n//2])+a[n//2]+a[n//2+1])

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[0]*n
for  i in range(n):
    a[i]=int(input())
a.sort(reverse=True)
ans=0
aupper=a[:n//2]
adowner=a[n//2:]
if len(a)==2:print(a[0]-a[1])
elif len(a)==3:
    print(sum(a[:n-1])-a[n-1]*2)
elif n%2==1:
    ans1=0;ans2=0
    for i in range(n//2):
        ans1+=aupper[i]*2-(adowner[i]+adowner[i+1])
    ans1+=(adowner[1]-adowner[-1])
    a=a[::-1]
    adowner=a[:n//2]
    aupper=a[n//2:]
    for i in range(n//2):
        ans2+=aupper[i]+aupper[i+1]-adowner[i]*2
    print(max(ans1,ans2))
else:
    ans=0
    for i in range(n//2):
        if i!=n//2-1:
            ans+=aupper[i]*2-(adowner[i]+adowner[i+1])
        else:
            ans+=aupper[i]-adowner[i]
    print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[0]*n
for  i in range(n):
    a[i]=int(input())
a.sort(reverse=True)
ans=0
aupper=a[:n//2]
adowner=a[n//2:]
if len(a)==2:print(a[0]-a[1])
elif len(a)==3:
    print(sum(a[:n-1])-a[n-1]*2)
elif n%2==1:
    ans1=0;ans2=0
    for i in range(n//2):
        ans1+=aupper[i]*2-(adowner[i]+adowner[i+1])
    ans1+=(adowner[1]-adowner[-1])
    a=a[::-1]
    adowner=a[:n//2]
    aupper=a[n//2:]
    for i in range(n//2):
        ans2+=aupper[i]+aupper[i+1]-adowner[i]*2
    print(ans1,ans2)
else:
    ans=0
    for i in range(n//2):
        if i!=n//2-1:
            ans+=aupper[i]*2-(adowner[i]+adowner[i+1])
        else:
            ans+=aupper[i]-adowner[i]
    print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[0]*n
for  i in range(n):
    a[i]=int(input())
a.sort(reverse=True)
ans=0
aupper=a[:n//2]
adowner=a[n//2:]
if len(a)==2:print(a[0]-a[1])
elif len(a)==3:
    print(sum(a[:n-1])-a[n-1]*2)
elif n%2==1:
    ans1=0;ans2=0
    for i in range(n//2):
        ans1+=aupper[i]*2-(adowner[i]+adowner[i+1])
    ans1+=(adowner[1]-adowner[-1])
    a=a[::-1]
    adowner=a[:n//2]
    aupper=a[n//2:]
    for i in range(n//2):
        ans2+=aupper[i]+aupper[i+1]-adowner[i]*2
    print(max(ans1,ans2))
else:
    ans=0
    for i in range(n//2):
        if i!=n//2-1:
            ans+=aupper[i]*2-(adowner[i]+adowner[i+1])
        else:
            ans+=aupper[i]-adowner[i]
    print(ans+adowner[1]-adowner[-1])

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[0]*n
for  i in range(n):
    a[i]=int(input())
a.sort(reverse=True)
ans=0
aupper=a[:n//2]
adowner=a[n//2:]
if len(a)<=3:
    print(sum(a[:n-1])-a[n-1]*2)
elif n%2==1:
    ans1=0;ans2=0
    for i in range(n//2):
        ans1+=aupper[i]*2-(adowner[i]+adowner[i+1])
    ans1+=(adowner[1]-adowner[-1])
    a=a[::-1]
    adowner=a[:n//2]
    aupper=a[n//2:]
    for i in range(n//2):
        ans2+=aupper[i]+aupper[i+1]-adowner[i]*2
    print(max(ans1,ans2))
else:
    ans=0
    for i in range(n//2):
        if i!=n//2-1:
            ans+=aupper[i]*2-(adowner[i]+adowner[i+1])
        else:
            ans+=aupper[i]-adowner[i]
    print(ans+adowner[1]-adowner[-1])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import deque

def main():
    N = int(input())
    A = [0] * N
    for i in range(N):
        A[i] = int(input())
    A.sort()
    Acopy = deque(A)

    # 1回目
    ans = deque([Acopy.popleft()])
    flag = True
    cnt = False
    while Acopy:
        if flag:
            tmp = Acopy.pop()
        else:
            tmp = Acopy.popleft()
        if abs(ans[0] - tmp) >= abs(ans[-1] - tmp):
            ans.appendleft(tmp)
        else:
            ans.append(tmp)
        if cnt:
            flag = not flag
        cnt = not cnt
    pri1 = 0
    tmp = ans.popleft()
    while ans:
        tmp2 = ans.popleft()
        pri1 += abs(tmp - tmp2)
        tmp = tmp2

    Acopy = deque(A)
    # 1回目
    ans = deque([Acopy.pop()])
    flag = True
    cnt = False
    while Acopy:
        if flag:
            tmp = Acopy.popleft()
        else:
            tmp = Acopy.pop()
        if abs(ans[0] - tmp) >= abs(ans[-1] - tmp):
            ans.appendleft(tmp)
        else:
            ans.append(tmp)
        if cnt:
            flag = not flag
        cnt = not cnt
    pri2 = 0
    tmp = ans.popleft()
    while ans:
        tmp2 = ans.popleft()
        pri2 += abs(tmp - tmp2)
        tmp = tmp2
    
    print(max(pri1, pri2))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import deque

def main():
    N = int(input())
    A = [0] * N
    for i in range(N):
        A[i] = int(input())
    A.sort()
    A = deque(A)

    ans = deque([A.popleft()])
    flag = True
    while A:
        if flag:
            tmp = A.pop()
        else:
            tmp = A.popleft()
        if abs(ans[0] - tmp) >= abs(ans[-1] - tmp):
            ans.appendleft(tmp)
        else:
            ans.append(tmp)
        
        flag = not flag

    pri = 0
    tmp = ans.popleft()
    while ans:
        tmp2 = ans.popleft()
        pri += abs(tmp - tmp2)
        tmp = tmp2

    print(pri)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

from collections import deque
sa = sorted(A)
sq = deque(sa)
sq2 = deque(sa[::-1])
q = deque([sq.popleft()])
q2 = deque([sq2.pop()])

while sq:
    q.appendleft(sq.pop())
    q2.appendleft(sq2.popleft())
    if len(sq)==0: break
    q.append(sq.pop())
    q2.append(sq2.popleft())
    if len(sq)==0: break
    q.appendleft(sq.popleft())
    q2.append(sq2.pop())
    if len(sq)==0: break
    q.append(sq.popleft())
    q2.append(sq2.pop())
    if len(sq)==0: break

a = q.pop()
ans = ans2 = 0
b = q2.pop()
while q:
    ans += abs(a - q[-1])
    ans2 += abs(b - q2[-1])
    a = q.pop()
    b = q2.pop()
print(max(ans, ans2))

Submission status: Wrong Answer

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

from collections import deque
sq = deque(sorted(A))
q = deque([sq.popleft()])

while sq:
    q.appendleft(sq.pop())
    if len(sq)==0: break
    q.append(sq.pop())
    if len(sq)==0: break
    q.appendleft(sq.popleft())
    if len(sq)==0: break
    q.append(sq.popleft())
    if len(sq)==0: break

a = q.pop()
ans = 0
while q:
    ans += abs(a - q[-1])
    a = q.pop()
print(ans)

Submission status: Wrong Answer

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

def main():
    A.sort()
    Ans = [A.pop(-1)]
    for _ in range(N-1):
        tmp1 = max(abs(Ans[0] - A[0]),abs(Ans[-1] - A[0]))
        tmp2 = max(abs(Ans[0] - A[-1]),abs(Ans[-1] - A[-1]))
        if tmp1 > tmp2:
            if abs(Ans[0] - A[0]) > abs(Ans[-1] - A[0]):
                Ans.insert(0,A.pop(0))
            else:
                Ans.append(A.pop(0))
        else:
            if abs(Ans[0] - A[-1]) > abs(Ans[-1] - A[-1]):
                Ans.insert(0,A[-1])
            else:
                Ans.append(A.pop(-1))
    ans = 0
    for i in range(N - 1):
        ans += abs(Ans[i] - Ans[i + 1])
    print(ans) 
    return

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
li = [int(stdin.readline().rstrip()) for _ in range(n)]
li.sort()
if n%2 == 1:
    mid = li[n//2]
    mae = li[:n//2]
    ushiro = li[n//2+1:]
    sono1 = -mid - mae[-1]
    sono1 += sum(ushiro)*2-sum(mae[:-1])*2
    sono2 = mid + ushiro[0]
    sono2 += sum(ushiro[1:])*2-sum(mae)*2
    print(max(sono1,sono2))
else:
    mae = li[:n//2]
    ushiro = li[n//2:]
    sono3 = -mae[-1] + ushiro[0]
    sono3 += sum(ushiro[1:])*2-sum(mae[:-1])**2 
    print(sono3)

Submission status: Wrong Answer

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
li = [int(stdin.readline().rstrip()) for _ in range(n)]
li.sort()
lis = li[:n//2]
lin = li[n//2:][::-1]
liv = []
if n%2 == 1:
    liv.append(lin[0])
    lin = lin[1:]
for i,j in zip(li,lin):
    liv.append(i)
    liv.append(j)
point = 0
for i in range(n-1):
    point += abs(liv[i+1]-liv[i])
print(point)

Submission status: Wrong Answer

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

flag,ans=1,0
while len(A)!=0:
    if len(A)==1:
        ans +=max(abs(B[0]-A[0]),abs(B[-1]-A[0]))
        break
    
    if flag==1:L,R=A.pop(),A.pop()
    else:L,R=A.popleft(),A.popleft()

    if abs(L-B[0])+abs(R-B[-1])>abs(L-B[-1])+abs(R-B[0]):
        ans +=abs(L-B[0])+abs(R-B[-1])
        B.append(R)
        B.appendleft(L)
    else:
        ans +=abs(R-B[0])+abs(L-B[-1])
        B.append(L)
        B.appendleft(R)

    flag=flag*-1

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = sorted([int(input()) for _ in range(n)])
l2 = sorted(l,reverse = True)

align = []
for i in range(n//2):
    align.append(l2[i])
    align.append(l[i])
    
margin = max(l2[0] - l2[n//2],l[n//2] - l[0])

total = 0
for i in range(1,n-1):
    total += abs(align[i] - align[i-1])
    
print(total + margin)

Submission status: Wrong Answer

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

A.sort()

l,r = A[0],A[0]
cnt = 1
l_cnt = cnt
r_cnt = 1
ans = 0
while True:
    ans += abs(l-A[-r_cnt])
    l = A[-r_cnt]
    cnt += 1
    r_cnt += 1
    if cnt == N:
        break

    ans += abs(r-A[-r_cnt])
    r = A[-r_cnt]
    cnt += 1
    r_cnt += 1    
    if cnt == N:
        break

    ans += abs(l-A[l_cnt])
    l = A[l_cnt]
    cnt += 1
    l_cnt += 1
    if cnt == N:
        break

    ans += abs(r-A[l_cnt])
    r = A[l_cnt]
    cnt += 1
    l_cnt += 1
    if cnt == N:
        break

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

A = deque(a)

r = A[0]
l = A.popleft()

ans = 0
while True:
    if len(A) == 0:
        break
    c = A.pop()
    ans += abs(l-c)
    l = c
    if len(A) == 0:
        break
    c = A.pop()
    ans += abs(r-c)
    r = c
    if len(A) == 0:
        break
    c = A.popleft()
    ans += abs(l-c)
    l = c
    if len(A) == 0:
        break
    c = A.popleft()
    ans += abs(r-c)
    r = c
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
a.sort()
ans = 2*a[-1]-a[0]-a[1]
for i in range(1,n//2):
    x = 2*a[n-1-i] -a[i-1]-a[i+1]
    ans +=x
if n==3:
    ans = a[1]+a[2]-2*a[0]
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

def main():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    A = sorted(A)
    A = deque(A)
    ans = 0
    if N%2==1:
        now = str(A.popleft())
        for i in range((N-1)//2):
            if i%2==0:
                a1,a2 = A.pop(),A.pop()
                ans += abs(a1-int(now[0]))
                ans += abs(a2-int(now[-1]))
                now = str(a1) + now + str(a2)
            else:
                b1,b2 = A.popleft(),A.popleft()
                ans += abs(b1-int(now[0]))
                ans += abs(b2-int(now[-1]))
                now = str(b1) + now + str(b2)
    
    else:
        now = str(A.popleft())
        for i in range(N//2-1):
            if i%2==0:
                a1,a2 = A.pop(),A.pop()
                ans += abs(a1-int(now[0]))
                ans += abs(a2-int(now[-1]))
                now = str(a1) + now + str(a2)
            else:
                b1,b2 = A.popleft(),A.popleft()
                ans += abs(b1-int(now[0]))
                ans += abs(b2-int(now[-1]))
                now = str(b1) + now + str(b2)
        a = A.pop()
        ans += abs(a-int(now[0]))

    print(ans) 

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(input()) for i in range(n)]
a.sort()
li=[a[0]]
c=0
while len(a)>0:
  if c%4==0:
    li=[a[-1]]+li
    del a[-1]
    c+=1
  elif c%4==1:
    li=li+[a[-1]]
    del a[-1]
    c+=1
  elif c%4==2:
    li=[a[0]]+li
    del a[0]
    c+=1
  else:
    li=li+[a[0]]
    del a[0]
    c+=1
ans =0
for i in range(len(li)-1):
  ans+= abs(li[i]-li[i+1])
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]
a.sort()
if N == 2:
    print(a[-1] - a[0])
    exit()

ans = (a[-1] - a[0]) + (a[-2] - a[0])

index = 0
for i in range(1, N):
    if i >= len(a) - 1:
        index = i
        break
    ans += (a[-1] - a[i])
    a.pop()
    index = i

if N % 2 == 0:
    ans += a[index] - a[index - 2]

print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = [int(input()) for i in range(N)]
    A.sort()
    from collections import deque
    d = deque(A)
    ret = deque([d.popleft()])
    for i in range((N-1)//2):
        if i & 1:
            j = d.popleft()
            k = d.popleft()
        else:
            j = d.pop()
            k = d.pop()
        if abs(ret[0] - j) < abs(ret[-1] - j):
            ret.appendleft(j)
            ret.append(k)
        else:
            ret.append(j)
            ret.appendleft(k)
    if len(d) == 1:
        j = d.pop()
        if abs(ret[0] - j) < abs(ret[-1] - j):
            ret.append(j)
        else:
            ret.appendleft(j)
    ans = 0
    for i in range(1, N):
        ans += abs(ret[i-1] - ret[i])
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = [int(input()) for i in range(N)]
    A.sort()
    from collections import deque
    d = deque(A)
    ret = deque([d.popleft()])
    for i in range((N-1)//2):
        if i & 1:
            j = d.popleft()
            k = d.popleft()
        else:
            j = d.pop()
            k = d.pop()
        if abs(ret[0] - j) < abs(ret[-1] - j):
            ret.append(j)
            ret.appendleft(k)
        else:
            ret.appendleft(j)
            ret.append(k)
    if len(d) == 1:
        j = d.pop()
        if abs(ret[0] - j) < abs(ret[-1] - j):
            ret.append(j)
        else:
            ret.appendleft(j)
    ans = 0
    for i in range(1, N):
        ans += abs(ret[i-1] - ret[i])
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = [int(input()) for i in range(N)]
    A.sort()
    from collections import deque
    d = deque(A)
    ret = deque([d.popleft()])
    for i in range((N-1)//2):
        if i & 1:
            j = d.popleft()
            k = d.popleft()
        else:
            j = d.pop()
            k = d.pop()
        ret.appendleft(j)
        ret.append(k)
    if len(d) == 1:
        j = d.pop()
        if abs(ret[0] - j) < abs(ret[-1] - j):
            ret.append(j)
        else:
            ret.appendleft(j)
    ans = 0
    for i in range(1, N):
        ans += abs(ret[i-1] - ret[i])
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from _collections import deque


def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield (number)


input_parser = parser()


def gw():
    global input_parser
    return next(input_parser)


def gi():
    data = gw()
    return int(data)


MOD = int(1e9 + 7)

import numpy
import scipy
from collections import deque
from math import sqrt
from math import floor
#https://atcoder.jp/contests/tenka1-2018-beginner/tasks/tenka1_2018_c
#C - Align
"""
need to consider the case that ticket is not enough to lower everything
"""

N = gi()
A = [0] * N
ais = [0] * N

for i in range(N):
    A[i] = gi()

A.sort()
nl = -1
nr = 1
hi = N - 2
li = 0
big = 0

curL = A[N - 1]
curR = A[N - 1]

while (hi >= li):
    can = A[hi]
    actual = hi
    if big:
        hi -= 1
    else:
        can = A[li]
        actual = li
        li += 1

    big = 1 - big

    if (abs(curL - can) > abs(curR - can)):
        ais[actual] = nl
        nl -= 1
        curL = can
    else:
        ais[actual] = nr
        nr += 1
        curR = can

for i in range(N):
    ais[i] -= (nl + 1)

ans = [0] * N

for i in range(N):
    ans[ais[i]] = A[i]

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

print(s)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
N = int(input())
A = []
for _ in range(N):
    A.append(int(input()))
A.sort()
L1, L2 = [], []
for i in range(N//2):
    L1.append(A[N//2-1-i])
    L1.append(A[-1-i])
    L2.append(A[-1-i])
    L2.append(A[N//2-1-i])
if N % 2 == 1:
    L1.append(A[N//2])
    L2.append(A[N//2])
a1, a2 = 0, 0
for i in range(N-1):
    a1 += abs(L1[i] - L1[i+1])
    a2 += abs(L2[i] - L2[i+1])
print(max(a1, a2))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
N = int(input())
A = []
for _ in range(N):
    A.append(int(input()))
A.sort()
L = []
if N > 2:
    if A[N//2] < A[N//2+1]:
        flag = True
    else:
        flag = False
else:
    flag = False
for i in range(N//2):
    if flag:
        L.append(A[N//2-1-i])
        L.append(A[-1-i])
    else:
        L.append(A[-1-i])
        L.append(A[N//2-1-i])
ans = 0
if N % 2 == 1:
    L.append(A[N//2])
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 = sorted([int(input()) for _ in range(N)])

modA = [[0,A[0]]]
cnt = 1
si = 1
ei = N-1
while si <= ei:
  modA.append([cnt,A[ei]])
  ei -= 1
  if si>ei:break
  
  modA.append([-cnt,A[ei]])
  ei -= 1
  if si>ei:break
  
  cnt += 1
  modA.append([cnt,A[si]])
  si += 1
  if si>ei:break 
 
  modA.append([-cnt,A[si]])
  si += 1
  if si>ei:break   
modA.sort()
#print(modA)
print(sum([abs(modA[i+1][1]-modA[i][1]) for i in range(len(modA)-1)]))

Submission status: Wrong Answer

Solution code:
from collections import deque
from copy import copy
N = int(input())
A = deque(sorted([int(input()) for _ in range(N)]))
A2 = copy(A)
d = deque()
d.append(A.popleft())
while True:
    try:
        d.append(A.pop())
        d.appendleft(A.pop())
        d.append(A.popleft())
        d.appendleft(A.popleft())
    except IndexError:
        break
ans = 0
for i in range(N-1):
    ans += abs(d[i]-d[i+1])
A = A2
d = deque()
d.append(A.pop())
while True:
    try:
        d.append(A.popleft())
        d.appendleft(A.popleft())
        d.append(A.pop())
        d.appendleft(A.pop())
    except IndexError:
        break
n = 0
for i in range(N-1):
    n += abs(d[i]-d[i-1])
print(max(ans, n))

Submission status: Wrong Answer

Solution code:
from collections import deque
from queue import Queue
N = int(input())
A = deque(sorted([int(input()) for _ in range(N)]))
if N < 3:
    print(abs(A[0]-A[1]))
    exit()
ans = 0
d = deque()
d.append(A.popleft())
while True:
    try:
        d.append(A.pop())
        d.appendleft(A.pop())
        d.append(A.popleft())
        d.appendleft(A.popleft())
    except IndexError:
        break
for i in range(N-1):
    ans += abs(d[i]-d[i+1])
print(ans)

Submission status: Wrong Answer

Solution code:
#N=6
#A=[3,1,4,1,5,9]
N=int(input())
A=[0]*N
for i in range(N):
    A[i]=int(input())

A=sorted(A)

mf=0
ms=0
sf=0
ss=0
d=0
c=0
n=0

mr=N
sl=0
sr=0
n=1
while(True):
    if(n==N):break
    ml=mr-1
    d+=(A[ml]-A[sl])
    n+=1
    if(n==N):break
    mr=ml-1
    d+=(A[mr]-A[sr])
    n+=1
    if(n==N):break
    sl=sr+1
    d+=(A[ml]-A[sl])
    n+=1
    if(n==N):break
    sr=sl+1
    d+=(A[mr]-A[sr])
    n+=1

mr=N-1
ml=N-1
sl=0
n=1
while(True):
    if(n==N):break
    sl=sr+1
    c+=(A[ml]-A[sl])
    n+=1
    if(n==N):break
    sr=sl+1
    c+=(A[mr]-A[sr])
    n+=1
    if(n==N):break
    ml=mr-1
    c+=(A[ml]-A[sl])
    n+=1
    if(n==N):break
    mr=ml-1
    c+=(A[mr]-A[sr])
    n+=1

print(max(d,c))