Problem:


Score : 400 points

Problem Statement

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

Constraints

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

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

Sample Input 1

5
6
8
1
2
3

Sample Output 1

21

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

Sample Input 2

6
3
1
4
1
5
9

Sample Output 2

25

Sample Input 3

3
5
5
1

Sample Output 3

8



Sample input:
5
6
8
1
2
3


Sample output:
21

Submission status: Wrong Answer

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

from collections import deque
from typing import Deque

A.sort()

DL = Deque(A)
DS = Deque(A)

LL = [0 for _ in range(N)]
LS = [0 for _ in range(N)]


if N % 2 == 0:

    for i in range(1, N, 2):
        LS[i] = DS.popleft()
        LL[i] = DL.pop()
    for j in range(0, N, 2):
        LS[j] = DS.popleft()
        LL[i] = DL.pop()

else:
    for i in range(1, N):
        if i % 2 == 1:
            LS[i] = DS.pop()
            LL[i] = DL.popleft()
        else:
            LS[i] = DS.popleft()
            LL[i] = DL.pop()

    LS[0] = DS.pop()
    LL[0] = DL.pop()

ansS = 0
ansL = 0
for i in range(1, N, 2):
    if i == N - 1:
        ansS += abs(LS[i] - LS[i-1])
        ansL += abs(LL[i] - LL[i-1])
    else:
        ansS += abs(LS[i] - LS[i-1])
        ansS += abs(LS[i] - LS[i+1])

        ansL += abs(LL[i] - LL[i-1])
        ansL += abs(LL[i] - LL[i+1])

print(max(ansS, ansL))

Submission status: Wrong Answer

Solution code:
from collections import deque
n=int(input())
a=deque(sorted([int(input()) for _ in range(n)]))
b=deque([a.pop()])
i=0
while a:
  if max(abs(a[-1]-b[-1]),abs(a[-1]-b[0]),abs(a[0]-b[-1]),abs(a[0]-b[0]))==abs(a[-1]-b[-1]):
    b.append(a.pop())
  elif max(abs(a[-1]-b[-1]),abs(a[-1]-b[0]),abs(a[0]-b[-1]),abs(a[0]-b[0]))==abs(a[-1]-b[0]):
    b.appendleft(a.pop())
  elif max(abs(a[-1]-b[-1]),abs(a[-1]-b[0]),abs(a[0]-b[-1]),abs(a[0]-b[0]))==abs(a[0]-b[0]):
    b.appendleft(a.popleft())
  else:
    b.append(a.popleft())
ans=0
for i in range(n-1):
  ans+=abs(b[i]-b[i+1])
print(ans)
print(b)

Submission status: Wrong Answer

Solution code:
from collections import deque
n=int(input())
a=deque(sorted([int(input()) for _ in range(n)]))
b=deque([a.pop()])
i=0
while a:
  if i%2:
    j=a.pop()
    if abs(b[-1]-j)>abs(b[0]-j):b.append(j)
    else:b.appendleft(j)
  else:
    j=a.popleft()
    if abs(b[-1]-j)>abs(b[0]-j):b.append(j)
    else:b.appendleft(j)
  i+=1
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())
d=[int(input()) for _ in range(n)]
from collections import deque
d.sort()
a=deque(d)

tmpl=a.popleft()
tmpr=tmpl
ans=0
i=0

while len(a)>0:
    #0 大きいほうから
    if i%4==0:
        tmp=a.pop()
        ans+=abs(tmpl-tmp)
        tmpl=tmp

    #1 大きいほうから
    elif i%4==1:
        tmp=a.pop()
        ans+=abs(tmpr-tmp)
        tmpr=tmp

    #2 小さいほうから
    elif i%4==2:
        tmp=a.popleft()
        ans+=abs(tmpr-tmp)
        tmpr=tmp


    #3 小さいほうから
    elif i%4==3:
        tmp=a.popleft()
        ans+=abs(tmpl-tmp)
        tmpl=tmp

    i+=1


print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
d=[int(input()) for _ in range(n)]
from collections import deque
d.sort()
a=deque(d)

tmpl=a.popleft()
tmpr=tmpl
ans=0
i=0

while len(a)>0:
    #0 大きいほうから
    if i%4==0:
        tmp=a.pop()
        ans+=abs(tmpl-tmp)
        tmpl=tmp

    #1 大きいほうから
    elif i%4==1:
        tmp=a.pop()
        ans+=abs(tmpr-tmp)
        tmpr=tmp

    #2 小さいほうから
    elif i%4==2:
        tmp=a.popleft()
        ans+=abs(tmpl-tmp)
        tmpl=tmp


    #3 小さいほうから
    elif i%4==3:
        tmp=a.popleft()
        ans+=abs(tmpr-tmp)
        tmpr=tmp

    i+=1


print(ans)

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    N = int(readline())
    A = list(int(readline()) for _ in range(N))
    A.sort(reverse=True)
    B = []
    l = 0
    r = -1
    for i in range(N):
        if i % 2 == 0:
            B.append(A[l])
            l += 1
        else:
            B.append(A[r])
            r -= 1
    ans = 0
    for i in range(N - 2):
        ans += abs(B[i] - B[i + 1])
    ans += max(abs(B[0] - B[-1]), abs(B[-2] - B[-1]))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

a=sorted(a)

from collections import deque

d=deque(a)

ans=deque([d.pop()])

for i in range(n-1):
    if i%2==0:
        p=d.popleft()
    else:
        p=d.pop()
        
    if abs(p-ans[0])>=abs(p-ans[-1]):
        ans.appendleft(p)
    else:
        ans.append(p)


dif = 0

for j in range(n-1):
    dif += abs(ans[j+1] - ans[j])

print(dif)

Submission status: Wrong Answer

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

a=sorted(a)

from collections import deque

d=deque(a)

ans=deque([])

for i in range(n):
    if i==n-1:
        z=d.pop()

        if abs(z-ans[0])>abs(z-ans[-1]):
            ans.appendleft(z)
        else:
            ans.append(z)
    else:
        if i%2==0:
            p=d.pop()
            ans.append(p)
        else:
            q=d.popleft()
            ans.append(q)

dif = 0
for j in range(n-1):
    dif += abs(ans[j+1] - ans[j])

print(dif)

Submission status: Wrong Answer

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
from collections import Counter
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())
N = int(input())
A = [int(input()) for _ in range(N)]
A = sorted(A)

ans = []
for i in range(N // 2):
    ans.append(A[N - 1 - i])
    ans.append(A[i])
if N % 2 == 1:
    ans.append(A[N // 2])

res = 0
for i in range(1, N - 1):
    res += abs(ans[i] - ans[i - 1])
x = abs(ans[-2] - ans[-1])
y = abs(ans[-1] - ans[0])
print(max(x, y) + res)

Submission status: Wrong Answer

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

A = a[:N // 2 + int(N % 2 == 1)]
B = a[(N + 1) // 2:]

if N % 2 == 1:
    r = 0
    pre = A[-1]
    for i in range(len(B)):
        r += abs(pre - B[len(B) - 1 - i])
        pre = B[len(B) - 1 - i]
        r += abs(pre - A[i])
        pre = A[i]
    
    res = r
    tmp = A[-1]
    A = A[:-1]
    B = [tmp] + B
    
    r = 0
    pre = B[0]
    for i in range(len(B) - 1):
        r += abs(pre - A[- 1 - i])
        pre = A[- 1 - i]
        r += abs(pre - B[i + 1])
        pre = B[i + 1]
 
    res = max(res, r)
else:
    tmp = [A[-1]]
    A = tmp + A[:-1]
    r = 0
    pre = B[0]
    for i in range(len(A)):
        r += abs(pre - B[i])
        pre = B[i]
        r += abs(pre - A[-1 - i])
        pre = A[-1 - i]
    res = r

print(res)

Submission status: Wrong Answer

Solution code:

        
def main():
    import sys

    def input(): return sys.stdin.readline().rstrip()

    n = int(input())
    a = [int(input()) for _ in range(n)]
    a.sort(reverse=True)
    if n%2 == 0:
        ans = 2*(sum(a[:n//2])-sum(a[n//2:]))
        ans += a[n//2]-a[n//2-1]
    else:
        ans = 2*(sum(a[:n//2])-sum(a[n//2+1:]))
        tmp = min(a[n//2]-a[n//2-1], a[n//2+1]-a[n//2])
        ans += tmp
    print(ans)



                
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

        
def main():
    import sys

    def input(): return sys.stdin.readline().rstrip()

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

    a.sort()
    a[:(n-1)//2],
    a[:-1:2], a[1:-1:2], a[-1] = reversed(a[(n+1)//2:]), a[:(n-1)//2], a[(n-1)//2]
    ans = sum([abs(a[i]-a[i-1]) for i in range(1, n-1)])
    ans = max(ans+abs(a[0]-a[-1]), ans+abs(a[-1]-a[-2]))
    print(ans)


                
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

        
def main():
    import sys

    def input(): return sys.stdin.readline().rstrip()

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

    a.sort()
    a[:(n+1)//2] = [a[(n-1)//2]]+a[:(n-1)//2]
    a[::2], a[1::2] = a[:(n+1)//2], a[n-1:(n+1)//2-1:-1]
    ans = sum([abs(a[i]-a[i-1]) for i in range(1, n)])
    print(ans)


                
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = []
for i in range(n):
    x = int(input())
    a.append(x)
data = []
a = sorted(a)
for i in range(n // 2):
    data.append(a[n - i - 1])
    if len(data) + 1 != n:
        data.append(a[i])
index = n // 2
if n % 2 == 0:
    index -= 1

if abs(data[0] - a[index]) > abs(data[-1] - a[index]):
    p = 0
else:
    p = n
data.insert(p, a[index]) 
ans = 0
for i in range(1, n):
    ans += abs(data[i] - data[i - 1])
print(ans)

Submission status: Wrong Answer

Solution code:
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
# list(map(int, list(input()))) # スペースがない数字リストを読み込み
import math
import fractions
import sys
import bisect
import heapq  # 優先度付きキュー(最小値取り出し)
import collections
from collections import Counter
from collections import deque
import pprint
import itertools

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""


def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr


# a^n
def power(a, n, mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod


# n*(n-1)*...*(l+1)*l
def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n - 1
    while (tmp >= l):
        a = a * tmp % mod
        tmp -= 1
    return a

# Union Find
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# 約数生成
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors


inf = 10 ** 18
mod = 10 ** 9 + 7

n = ir()
a = [ir() for i in range(n)]
a.sort()
ans = 0
if n%2==1:
    ind = n//2
    for i, num in enumerate(a):
        if i < ind:
            ans-=2*num
        elif i < ind+2:
            ans+=num
        else:
            ans+=2*num
    pre = 0
    for i,num in enumerate(a):
        if i < ind-1:
            pre-=2*num
        elif i <= ind:
            pre-=num
        else:
            pre+=num
    print(max(ans,pre))
else:
    pind = n//2
    mind = pind-1
    for i,num in enumerate(a):
        if i < mind:
            ans-=2*num
        elif i == mind:
            ans-=num
        elif i == pind:
            ans+=num
        else:
            ans+=2*num
    pre = 0
    for i,num in enumerate(a):
        if i < pind-2:
            ans-=2*num
        elif i < pind:
            ans-=num
        else:
            ans+=2*num
    print(max(ans,pre))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
A.sort()
ans = 0
for i in range(N//2):
  ans += (A[-i-1]-A[i])
if ans%2 == 0:
  print(ans*2)
else:
  print(ans*2-1)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
ar = []
for i in range(n):
    ar.append(int(input()))
ar.sort()
z = ar.pop(0)
br = [z]
while True:
    if len(ar) == 0:
        break
    br.insert(0,ar.pop(len(ar)-1))
    if len(ar) == 0:
        break
    br.append(ar.pop(len(ar)-1))
    if len(ar) == 0:
        break
    br.insert(0,ar.pop(0))
    if len(ar) == 0:
        break
    br.append(ar.pop(0))
count = 0
for i in range(n-1):
    count += abs(br[i] - br[i+1])
print(count)

Submission status: Wrong Answer

Solution code:
from collections import *
n=int(input())
a=[]
for i in range(n):
    a.append(int(input()))
a.sort()
a=deque(a)
aa=deque()
aa.append(a.popleft())
while a:
    aa.append(a.pop())
    if a:
        aa.appendleft(a.pop())   
    if a:
        aa.append(a.popleft())
    if a:
        aa.appendleft(a.popleft())
ans=0
aa=list(aa)
for i in range(n-1):
    ans+=abs(aa[i]-aa[i+1])
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())

alist=[]
for _ in range(N):
  a=int(input())
  alist.append(a)
alist.sort()
#print(alist)

alist_small=alist[:N//2]
alist_large=alist[N//2:]
alist_small.reverse()
#print(alist_large,alist_small)

answer=0
left=right=0
while alist_large or alist_small:
  if len(alist_large)>len(alist_small):
    num=alist_large.pop()
  else:
    num=alist_small.pop()
  
  if left==0:
    left=right=num
  else:
    if abs(num-right)>=abs(num-left):
      answer+=abs(num-right)
      right=num
    else:
      answer+=abs(num-left)
      left=num
      
print(answer)

Submission status: Wrong Answer

Solution code:
n,*a=map(int,open(0).read().split())
l=[0 for i in range(n)]
a.sort()
for i,j in enumerate(range(2,n,2)):
    l[j]=a[i]
for i,j in enumerate(range(1,n,2)):
    l[j]=a[~i]
if n%2==0:
    l[0]=a[n//2-1]
else:
    l[0]=a[n//2]
if n==2:
    l=a
if n==3:
    l[0]=a[1]
    l[1]=a[0]
    l[2]=a[2]
print(sum([abs(l[i]-l[i+1]) for i in range(n-1)]))

Submission status: Wrong Answer

Solution code:
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

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

a.sort()
qa = deque(a)
qb = deque()

for i in range(len(qa)):
    if i % 4 == 0:
        qb.appendleft(qa.popleft())
    elif i % 4 == 1:
        qb.append(qa.pop())
    elif i % 4 == 2:
        qb.appendleft(qa.pop())
    else:
        qb.append(qa.popleft())
ans = 0
for i in range(len(qb)-1):
    ans += abs( qb[i+1] - qb[i] )
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)
l=a.popleft() 
r=l
count=0
ans=0
while len(a)>0:
  if count%4==0:
    temp=a.pop()
    ans+=abs(temp-l)
    l=temp
  elif count%4==1:
    temp=a.pop()
    ans+=abs(temp-r)
    r=temp
  elif count%4==2:
    temp=a.popleft()
    ans+=abs(l-temp)
    l=temp
  elif count%4==3:
    temp=a.popleft()
    ans+=abs(r-temp)
    r=temp
  count+=1
print(ans)

Submission status: Wrong Answer

Solution code:
def solve():
  ans = 0
  N = int(input())
  A = [int(input()) for _ in range(N)]
  B = [0]*N
  A.sort()
  if N%2==1:
    B[0] = A[N//2]
    if abs(A[N//2]-A[0])>abs(A[(N-1)//2]-A[-1]):
      for i in range(N//2):
        B[2*i+1] = A[i]
        B[2*i+2] = A[-1-i]
    else:
      for i in range(N//2):
        B[2*i+2] = A[i]
        B[2*i+1] = A[-1-i]
    for i in range(N-1):
      ans += abs(B[i]-B[i+1])
  else:
    if abs(A[N//2]-A[0])>abs(A[(N-1)//2]-A[-1]):
      for i in range(N//2):
        B[2*i] = A[-1-i]
        B[2*i+1] = A[i]
    else:
      for i in range(N//2):
        B[2*i+1] = A[-1-i]
        B[2*i] = A[i]
    for i in range(N-1):
      ans += abs(B[i]-B[i-1])
  return ans
print(solve())

Submission status: Wrong Answer

Solution code:
def solve():
  ans = 0
  N = int(input())
  A = [int(input()) for _ in range(N)]
  B = [0]*N
  A.sort()
  if N%2==1:
    B[0] = A[N//2]
    if abs(A[N//2]-A[0])>abs(A[(N-1)//2]-A[-1]):
      for i in range(N//2):
        B[2*i+1] = A[i]
        B[2*i+2] = A[-1-i]
    else:
      for i in range(N//2):
        B[2*i+2] = A[i]
        B[2*i+1] = A[-1-i]
    for i in range(N-1):
      ans += abs(B[i]-B[i+1])
  else:
    for i in range(N//2):
      B[2*i] = A[-1-i]
      B[2*i+1] = A[i]
    for i in range(N-1):
      ans += abs(B[i]-B[i-1])
  return ans
print(solve())

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
def solve():
  ans = 0
  N = int(input())
  A = [int(input()) for _ in range(N)]
  B = [0]*N
  A.sort()
  if abs(A[N//2]-A[0])>abs(A[(N-1)//2]-A[-1]):
    B[0] = A[N//2]
    for i in range(N//2):
      B[2*i+1] = A[i]
      if 2*i+2<N:
        B[2*i+2] = A[-1-i]
  else:
    B[0] = A[(N-1)//2]
    for i in range(N//2):
      B[2*i+1] = A[-1-i]
      if 2*i+2<N:
        B[2*i+2] = A[i]
  for i in range(N-1):
    ans += abs(B[i]-B[i+1])
  return ans
print(solve())

Submission status: Wrong Answer

Solution code:
from collections import deque
N = int(input())
A = sorted([int(input()) for _ in range(N)])
B = deque([A[0]])
ind = 1
cur0=1
cur1=N-1
cnt = 0
while cur0<=cur1:
    if ind==1:
        a = A[cur1]
        ar = abs(a-B[-1])
        al = abs(a-B[0])
        if ar>=al:
            cnt += ar
            B.append(a)
        else:
            cnt += al
            B.appendleft(a)
        cur1 -= 1
    else:
        a = A[cur0]
        ar = abs(B[-1]-a)
        al = abs(B[0]-a)
        if ar>=al:
            cnt += ar
            B.append(a)
        else:
            cnt += al
            B.appendleft(a)
        cur0 += 1
    ind = 1-ind
print(cnt)

Submission status: Wrong Answer

Solution code:
ma = lambda :map(int,input().split())
ni = lambda:int(input())
import collections
import math
import itertools
import heapq as hq
gcd = math.gcd
n = ni()
A = []
for i in range(n):
    A.append(ni())
A.sort()
A1 = A[:(n+1)//2]
A2 = A[(n+1)//2:]
#print(A1,A2)


if n%2==0:
    print(2*sum(A2) - 2*sum(A1) - A2[0] + A1[-1])

else:
    s1 = 2*sum(A2) - 2*sum(A1) + A1[-1]+A1[-2]
    s2 = 2*sum(A2)  - 2*sum(A1) + A1[-1]+A1[-2] +(A1[-1] - A1[0])
    print(max(s1,s2))

Submission status: Wrong Answer

Solution code:
ma = lambda :map(int,input().split())
ni = lambda:int(input())
import collections
import math
import itertools
import heapq as hq
gcd = math.gcd
n = ni()
A = []
for i in range(n):
    A.append(ni())
A.sort()
A1 = A[:(n+1)//2]
A2 = A[(n+1)//2:]
#print(A1,A2)
if n%2==0:
    print(2*sum(A2) - 2*sum(A1) - A2[0] + A1[-1])
else:
    print(2*sum(A2) - 2*sum(A1) + A1[-1]+A1[-2])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    N, *A = map(int, open(0))
    A.sort()
    B = []
    for i in range(N//2):
        B.append(A[N-1-i])
        B.append(A[i])
    if N % 2 == 1:
        B.append(A[N//2])
    # print(B)
    ans = 0
    for i in range(N-1):
        ans += abs(B[i+1] - B[i])
    if abs(B[N-2] - B[N-1]) < abs(B[N-1] - B[0]):
        print(ans - abs(B[N-2] - B[N-1]) + abs(B[N-1] - B[0]))
    else:
        print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import deque

n, *A = map(int, open(0).read().split())
a = deque(sorted(A))
b = deque([])
b.append(a.pop())
i = 0
ans = 0
while a:
    if i:
        c = a.pop()
        i = 0
    else:
        c = a.popleft()
        i = 1
    if abs(b[-1] - c) < abs(b[0] - c):
        ans += abs(b[0] - c)
        b.appendleft(c)
    else:
        ans += abs(b[-1] - c)
        b.append(c)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():
    N = in_n()
    A = deque(sorted(list(in_na())))

    al = deque([A.popleft()])

    f = True
    while A:
        if len(A) >= 2:
            if f:
                al.append(A.pop())
                al.appendleft(A.pop())
                f = False
            else:
                al.appendleft(A.popleft())
                al.append(A.popleft())
                f = True

        elif len(A) == 1:
            al.append(A.pop())

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():
    N = in_n()
    A = deque(sorted(list(in_na())))

    al = deque([A.popleft()])

    f = True
    while A:
        if len(A) >= 2:
            if f:
                al.append(A.pop())
                al.appendleft(A.pop())
                f = False
            else:
                al.append(A.popleft())
                al.appendleft(A.popleft())
                f = True

        elif len(A) == 1:
            al.append(A.pop())

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
a_list = [ int(input()) for _ in range(n) ]
a_list = sorted(a_list)
a_size = len(a_list)
if a_size % 2 == 0:
    a_l = a_list[:a_size//2]
    a_r = a_list[a_size//2:]
else:
    a_l = a_list[:a_size//2+1]
    a_r = a_list[a_size//2+1:]

total = 0
for i, j in enumerate(a_l):
    try:
        if i == 0:
            total += abs(a_l[i] - a_r[-(i+1)])
        elif i % 2 == 0:
            total += abs(a_l[i] - a_r[-i])
        else:
            total += abs(a_l[i] - a_r[-(i+2)])
    except:
        pass

for i, j in enumerate(a_l):
    try:
        if i % 2 == 0:
            total += abs(a_l[i] - a_r[-(i+2)])
        else:
            total += abs(a_l[i] - a_r[-i])
    except:
        pass

if len(a_r) == 1:
    total += abs(a_l[0] - a_r[0])

print(total)

Submission status: Wrong Answer

Solution code:
n = int(input())
a_list = [ int(input()) for _ in range(n) ]
a_list = sorted(a_list)
a_size = len(a_list)
if a_size % 2 == 0:
    a_l = a_list[:a_size//2]
    a_r = a_list[a_size//2:]
else:
    a_l = a_list[:a_size//2+1]
    a_r = a_list[a_size//2+1:]

total = 0
for i, j in enumerate(a_l):
    try:
        if i == 0:
            total += abs(a_l[i] - a_r[-(i+1)])
        elif i % 2 == 0:
            total += abs(a_l[i] - a_r[-(i+2)])
        else:
            total += abs(a_l[i] - a_r[-(i+1)])
    except:
        pass
    
    try:
        if i % 2 != 0:
            total += abs(a_l[i] - a_r[-(i+2)])
        else:
            total += abs(a_l[i] - a_r[-(i+1)])
    except:
        pass
print(total)

Submission status: Wrong Answer

Solution code:
from collections import deque

n=int(input())
a=[int(input()) for i in range(n)]
a_s=sorted(a)
a_rs=sorted(a,reverse=True)

s_inx=0
rs_inx=0

l=deque([])
for i in range(n):
  if (i//2)%2==0:
    if i%2==0:
      l.appendleft(a_s[s_inx])
      s_inx+=1
    else:
      l.appendleft(a_rs[rs_inx])
      rs_inx+=1
  else:
    if i%2==0:
      l.append(a_rs[rs_inx])
      rs_inx+=1
    else:
      l.append(a_s[s_inx])
      s_inx+=1

if n%2==0:
  c=l.popleft()
  l.append(c)

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

Submission status: Wrong Answer

Solution code:
from collections import deque
n = int(input())
a =deque(sorted([int(input()) for i in range(n)]))
amin = a.popleft()
d = deque([])
d.append(amin)
su  = 0
bit = 0
while len(a)!=0:
  if bit==0:
    s = a.pop()
    if d[0]>d[-1]:
      su += s-d[-1]
      d.append(s)
    else:
      su += s-d[0]
      d.appendleft(s)
    if len(a)!=0:
      if d[0]>d[-1] and a[-1]>d[-1]:
        t = a.pop()
        su += t-d[-1]
        d.append(t)
      elif d[0]<=d[-1] and a[-1]>d[0]:
        t = a.pop()
        su += t-d[0]
        d.appendleft(t)
    bit =1
 #   print(d)
  else:
    s = a.popleft()
    if d[0]>d[-1]:
      su += d[0]-s
      d.appendleft(s)
    else:
      su += d[-1]-s
      d.append(s)
    if len(a)!=0:
      if d[0]>d[-1] and a[0]<d[0] :
        t = a.popleft()
        su += d[0]-t
        d.appendleft(t)
      elif d[0]<=d[-1] and a[0]<d[-1]:
        t = a.popleft()
        su += d[-1]-t
        d.append(t)
    bit =0
   # print(d)
print(su)

Submission status: Wrong Answer

Solution code:
from collections import deque
n = int(input())
a =deque(sorted([int(input()) for i in range(n)]))
amin = a.popleft()
d = deque([])
d.append(amin)
su  = 0
bit = 0
while len(a)!=0:
  if bit==0:
    s = a.pop()
    if d[0]>d[-1]:
      su += s-d[-1]
      d.append(s)
    else:
      su += s-d[0]
      d.appendleft(s)
    if len(a)!=0:
      if d[0]>d[-1] and a[-1]>d[-1]:
        t = a.pop()
        su += t-d[-1]
        d.append(t)
      elif d[0]<=d[-1] and a[-1]>d[0]:
        t = a.pop()
        su += t-d[0]
        d.appendleft(t)
    bit =1
 #   print(d)
  else:
    s = a.popleft()
    if d[0]>d[-1]:
      su += d[0]-s
      d.appendleft(s)
    else:
      su += d[-1]-s
      d.append(s)
    if len(a)!=0:
      if d[0]>d[-1] and a[0]>d[0] :
        t = a.popleft()
        su += d[0]-t
        d.appendleft(t)
      elif d[0]<=d[-1] and a[0]<d[0]:
        t = a.popleft()
        su += d[-1]-t
        d.append(t)
    bit =0
  #  print(d)
print(su)

Submission status: Wrong Answer

Solution code:
from collections import deque
n = int(input())
a =deque(sorted([int(input()) for i in range(n)]))
amin = a.popleft()
d = deque([])
d.append(amin)
su  = 0
bit = 0
while len(a)!=0:
  if bit==0:
    s = a.pop()
    if d[0]>d[-1]:
      su += s-d[-1]
      d.append(s)
    else:
      su += s-d[0]
      d.appendleft(s)
    if len(a)!=0:
      t = a.pop()
      if d[0]>d[-1]:
        su += t-d[-1]
        d.append(t)
      else:
        su += t-d[0]
        d.appendleft(t)
    bit =1
 #   print(d)
  else:
    s = a.popleft()
    if d[0]>d[-1]:
      su += d[0]-s
      d.appendleft(s)
    else:
      su += d[-1]-s
      d.append(s)
    if len(a)!=0:
      t = a.popleft()
      if d[0]>d[-1]:
        su += d[0]-t
        d.appendleft(t)
      else:
        su += d[-1]-t
        d.append(t)
    bit =0
  #  print(d)
print(su)

Submission status: Wrong Answer

Solution code:
from collections import deque
N = int(input())
A = deque(sorted([int(input()) for _ in range(N)]))
que = deque([])
que.append(A.popleft())
turn = 1
while A:
    if turn == 1:
        a = A.pop()
        if abs(a-que[0])>=abs(a-que[-1]):
            que.appendleft(a)
        else:
            que.append(a)
        if A:
            a = A.pop()
            if abs(a-que[0])<=abs(a-que[-1]):
                que.append(a)
            else:
                que.appendleft(a)
    else:
        a = A.popleft()
        if abs(a-que[0])>=abs(a-que[-1]):
            que.appendleft(a)
        else:
            que.append(a)
        if A:
            a = A.popleft()
            if abs(a-que[0])<=abs(a-que[-1]):
                que.append(a)
            else:
                que.appendleft(a)
    turn = 1-turn
cnt = 0
for i in range(1,N):
    cnt += abs(que[i]-que[i-1])
print(cnt)

Submission status: Wrong Answer

Solution code:
from collections import deque
N = int(input())
A = deque(sorted([int(input()) for _ in range(N)]))
que = deque([])
que.append(A.popleft())
turn = 1
while A:
    if turn == 1:
        que.appendleft(A.pop())
        if A:
            a = A.pop()
            if abs(a-que[0])<=abs(a-que[-1]):
                que.append(a)
            else:
                que.appendleft(a)
    else:
        que.appendleft(A.popleft())
        if A:
            a = A.popleft()
            if abs(a-que[0])<=abs(a-que[-1]):
                que.append(a)
            else:
                que.appendleft(a)
    turn = 1-turn
cnt = 0
for i in range(1,N):
    cnt += abs(que[i]-que[i-1])
print(cnt)

Submission status: Wrong Answer

Solution code:
from collections import deque
N = int(input())
A = deque(sorted([int(input()) for _ in range(N)]))
que = deque([])
que.append(A.popleft())
turn = 1
while A:
    if turn == 1:
        que.appendleft(A.pop())
        if A:
            que.append(A.pop())
    else:
        que.appendleft(A.popleft())
        if A:
            que.append(A.popleft())
    turn = 1-turn
cnt = 0
for i in range(1,N):
    cnt += abs(que[i]-que[i-1])
print(cnt)