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())
l=[]
for i in range(n):
  l.append(int(input()))
l1=sorted(l)
if n%2==0:
  k=n//2
  sum=0
  for i in range(0,k):
    sum-=2*l1[i]
  for i in range(k,n):
    sum+=2*l1[i]
  sum-=l1[k]
  sum+=l1[k-1]
else:
  k=n//2
  sum=0
  for i in range(k,n):
    sum+=2*l1[i]
  for i in range(k):
    sum-=2*l1[i]
  sum-=l1[0]
  sum-=l1[1]
print(sum)

Submission status: Wrong Answer

Solution code:
# 2019/10/16
import sys
input=sys.stdin.readline

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

l,r=1,n-2
L=a[0]
R=a[-1]
cnt=abs(L-R)

while l<=r:
    
    ll=abs(L-a[l])
    lr=abs(L-a[r])
    rl=abs(R-a[l])
    rr=abs(R-a[r])
    cnt+=max(ll,lr,rl,rr)
    if max(ll,lr)<max(rl,rr):
        R=R-max(rl,rr) if R>max(rl,rr) else R+max(rl,rr)
        if rl>rr:l+=1
        else:r-=1
    else:
        L=L-max(ll,lr) if L>max(ll,lr) else L+max(ll,lr)
        if ll>lr:l+=1
        else:r-=1
print(cnt)

Submission status: Wrong Answer

Solution code:
from collections import*
n,*a=map(int,open(0).read().split())
def f(a):
  a=deque(a)
  deq=deque([a.popleft()])
  for i in range(n-1):
    if i%4==0:
      deq.appendleft(a.pop())
    elif i%4==1:
      deq.append(a.pop())
    elif i%4==2:
      deq.appendleft(a.popleft())
    else:
      deq.append(a.popleft())
  l=list(deq)
  return sum(abs(i-j)for i,j in zip(l,l[1:]))
s=sorted(a)
print(max(f(a),f(a[::-1])))

Submission status: Wrong Answer

Solution code:
from collections import*
n,*a=map(int,open(0).read().split())
a=deque(sorted(a))
deq=deque([a.popleft()])
for i in range(n-1):
  if i%4==0:
    deq.appendleft(a.pop())
  elif i%4==1:
    deq.append(a.pop())
  elif i%4==2:
    deq.appendleft(a.popleft())
  else:
    deq.append(a.popleft())
l=list(deq)
print(sum(abs(i-j)for i,j in zip(l,l[1:])))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
from collections import deque

def main():
    n = int(input().strip())
    a = [0] * n
    for i in range(n):
        a[i] = int(input().strip())
    a = deque(sorted(a))

    d = deque()
    for i in range(n):
        e = -20
        if i % 2 == 0:
            e = a.popleft()
        else:
            e = a.pop()
        if len(d) > 0:
            if abs(e - d[0]) > abs(e - d[-1]):
                d.appendleft(e)
            else:
                d.append(e)
        else:
            d.append(e)
    ans = 0
    d = list(d)
    for i in range(n-1):
        ans += abs(d[i] - d[i+1])
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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


def main():
    from collections import deque

    n = int(input())
    a = sorted([int(input()) for _ in range(n)])
    case1 = deque([a[0], a[-1]])
    remain1 = deque(a[1:-1])
    case2 = deque([a[-1], a[0]])
    remain2 = deque(a[1:-1])

    i = 0

    while remain1:
        if i % 2 == 0:
            ri = remain1.popleft()
        else:
            ri = remain1.pop()

        left = case1.popleft()
        right = case1.pop()

        if abs(ri - left) >= abs(ri - right):
            case1.appendleft(left)
            case1.appendleft(ri)
            case1.append(right)
        else:
            case1.appendleft(left)
            case1.append(right)
            case1.append(ri)

        i += 1

    j = 0

    while remain2:
        if j % 2 == 0:
            ri = remain2.popleft()
        else:
            ri = remain2.pop()

        left = case2.popleft()
        right = case2.pop()

        if abs(ri - left) >= abs(ri - right):
            case2.appendleft(left)
            case2.appendleft(ri)
            case2.append(right)
        else:
            case2.appendleft(left)
            case2.append(right)
            case2.append(ri)

        j += 1

    ans1 = 0
    ans2 = 0

    for x in range(n - 1):
        ans1 += abs(case1[x] - case1[x + 1])

    for x in range(n - 1):
        ans2 += abs(case2[x] - case2[x + 1])

    print(max(ans1, ans2))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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


def main():
    from collections import deque

    n = int(input())
    a = sorted([int(input()) for _ in range(n)])
    case1 = deque([a[0], a[-1]])
    remain1 = deque(a[1:-1])
    case2 = deque([a[-1], a[0]])
    remain2 = deque(a[1:-1])

    i = 0

    while remain1:
        if i % 2 == 0:
            ri = remain1.popleft()
        else:
            ri = remain1.pop()

        left = case1.popleft()
        right = case1.pop()

        if abs(ri - left) >= abs(ri - right):
            case1.appendleft(left)
            case1.appendleft(ri)
            case1.append(right)
        else:
            case1.appendleft(left)
            case1.append(right)
            case1.append(ri)

        i += 1

    j = 0

    while remain2:
        if j % 2 == 0:
            ri = remain2.popleft()
        else:
            ri = remain2.pop()

        left = case2.popleft()
        right = case2.pop()

        if abs(ri - left) > abs(ri - right):
            case2.appendleft(left)
            case2.appendleft(ri)
            case2.append(right)
        else:
            case2.appendleft(left)
            case2.append(right)
            case2.append(ri)

        j += 1

    ans1 = 0
    ans2 = 0

    for x in range(n - 1):
        ans1 += abs(case1[x] - case1[x + 1])

    for x in range(n - 1):
        ans2 += abs(case2[x] - case2[x + 1])

    print(max(ans1, ans2))


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()
l = [A[0]]
A.pop(0)
small = A[:N//2]
big = A[N//2:][::-1]
#print(small, big)

for i in range(max(len(small)+1, len(big)+1)):
    if i%2 == 0:
        if i < len(big):
            l.insert(0,big[i])
        if i+1 < len(big): 
            l.append(big[i+1])
    if i%2 == 1:
        if i-1 < len(small):
            l.insert(0,small[i-1])
        if i < len(small):
            l.append(small[i])

#print(l)
#print(small, big)

ans = 0
m = 10000000
for i in range(N-1):
    ans += abs(l[i+1] - l[i])
    m = min(m, abs(l[i+1] - l[i]))

ans += abs(l[0] - l[-1])
m = min(m, abs(l[0] - l[-1]))
print(ans - m)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

min_A = min(A)
A.remove(min_A)

A.sort()  # 昇順にソート
ans = 0


small_A = A[:(N-1)//2]
large_A = A[(N-1)//2:]

large_A = sorted(large_A, reverse=True)


last_A = -1
# Nがもともと偶数だとlarge_Aの要素が１つ大きくなってしまうから
if len(large_A) > len(small_A):
    last_A = large_A[-1]
    large_A.remove(large_A[-1])

# これでsmall_Aとlarge_Aの大きさが揃った
after_reverse_list = []
after_usironi_list = [min_A]

for i in range(0, len(large_A)):
    if i % 2 == 0:
        after_usironi_list.append(large_A[i])
        after_usironi_list.append(small_A[i])
    else:
        after_reverse_list.append(large_A[i])
        after_reverse_list.append(small_A[i])

# print("")
# print(after_reverse_list)
# print(after_usironi_list)
# print("")
after_reverse_list.reverse()

after_reverse_list.extend(after_usironi_list)

# print(after_reverse_list)
# print(after_usironi_list)

final_list = after_reverse_list.copy()
# print(final_list)
ans = 0
if last_A != -1:
    ans += max(abs(last_A-final_list[0]), abs(last_A-final_list[-1]))

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

if N == 3:
    ans += abs(final_list[0] - final_list[-1])

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
N = int(input())
H = []
Q = deque()
for i in range(N):
  A = int(input())
  H.append(A)
H.sort()
H = deque(H)
if N == 3:
  P = [abs(H[-1]-H[0])]
  for i in range(N-1):
    P.append(abs(H[i]-H[i+1]))
  P.sort()
  print(P[-1]+P[-2])
  exit()

Q.append(H.popleft())

while True:
  if H:
    x = H.pop()
    Q.append(x)
  if H:
    x = H.pop()
    Q.appendleft(x)
  if H:
    y = H.popleft()
    Q.append(y)
  if H:
    y = H.popleft()
    Q.appendleft(y)
  else:
    break
ans = 0
for i in range(N-1):
  ans += abs(Q[i+1]-Q[i])
print(ans)

Submission status: Wrong Answer

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

Q.append(H.popleft())

while True:
  if H:
    x = H.pop()
    Q.append(x)
  if H:
    x = H.pop()
    Q.appendleft(x)
  if H:
    y = H.popleft()
    Q.append(y)
  if H:
    y = H.popleft()
    Q.appendleft(y)
  else:
    break
ans = 0
for i in range(N-1):
  ans += abs(Q[i+1]-Q[i])
print(ans)

Submission status: Wrong Answer

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

S=A[:N-N//2]
L=A[N-N//2:]
#print(S)
#print(L)

if len(S)==len(L):
  ans=sum(L)*2-L[0]-sum(S[:-1])*2+S[-1]
else:
  ans=sum(L)*2-sum(S)*2+S[-1]+S[-2]
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
a = inpln(n)
a.sort(reverse=True)
q = deque(a)
res = deque([])
res.append(q.popleft())
for i in range(n-1):
    if i%4 == 0:
        res.appendleft(q.pop())
    elif i%4 == 1:
        res.append(q.pop())
    elif i%4 == 2:
        res.appendleft(q.popleft())
    else:
        res.append(q.popleft())
ans = 0
print(res)
for i in range(n-1):
    ans += abs(res[i+1] - res[i])
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
a = inpln(n)
a.sort(reverse=True)
q = deque(a)
res = deque([])
res.append(q.popleft())
for i in range(n-1):
    if i%4 == 0:
        res.appendleft(q.pop())
    elif i%4 == 1:
        res.append(q.pop())
    elif i%4 == 2:
        res.appendleft(q.popleft())
    else:
        res.append(q.popleft())
ans = 0
for i in range(n-1):
    ans += abs(res[i+1] - res[i])
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
a = inpln(n)
a.sort()
q = deque(a)
res = deque([])
res.append(q.popleft())
for i in range(n-1):
    if i%4 == 0:
        res.appendleft(q.pop())
    elif i%4 == 1:
        res.append(q.pop())
    elif i%4 == 2:
        res.appendleft(q.popleft())
    else:
        res.append(q.popleft())
ans = 0
for i in range(n-1):
    ans += abs(res[i+1] - res[i])
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
N = int(input())
A = deque(sorted([int(input()) for _ in range(N)]))
B = deque()
B.append(A.pop())
for i in range(N-1):
    if (i//2)%2 == 0:
        if abs(A[0] - B[0]) >= abs(A[0] - B[-1]):
            B.appendleft(A.popleft())
        else:
            B.append(A.popleft())
    else:
        if abs(A[-1] - B[0]) >= abs(A[-1] - B[-1]):
            B.appendleft(A.pop())
        else:
            B.append(A.pop())
C = list(B)
s = 0
for i in range(N-1):
    s += abs(C[i]-C[i+1])
print(s)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

a.sort()

d = deque()

lft = False
smll = False

d.append(a[0])
fst = 1
end = n-1

for i in range(n-1):
    if not(smll):
        if not(lft):
            d.appendleft(a[end])
            end -= 1
            lft = True
        else:
            d.append(a[end])
            end -= 1
            lft = False
            smll = True
    else:
        if not(lft):
            d.appendleft(a[fst])
            fst += 1
            lft = True
        else:
            d.append(a[fst])
            fst += 1
            lft = False
            smll = False

memo = d.pop()
ans = 0
for i in range(n-1):
    memo2 = d.pop()
    ans += abs(memo-memo2)
    memo = memo2
    

"""
d.append(a[n-1])
fst = 0
end = n-2
lft = False
smll = True


for i in range(n-1):
    if not(smll):
        if not(lft):
            d.appendleft(a[end])
            end -= 1
            lft = True
        else:
            d.append(a[end])
            end -= 1
            lft = False
            smll = True
    else:
        if not(lft):
            d.appendleft(a[fst])
            fst += 1
            lft = True
        else:
            d.append(a[fst])
            fst += 1
            lft = False
            smll = False

memo3 = d.pop()
ans2 = 0
for i in range(n-1):
    memo4 = d.pop()
    ans2 += abs(memo3-memo4)
    memo3 = memo4
    
ans = max(ans,ans2)
  """  
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

a.sort()

d = deque()

lft = False
smll = False

d.append(a[0])
fst = 1
end = n-1


for i in range(n-1):
    if not(smll):
        if not(lft):
            d.appendleft(a[end])
            end -= 1
            lft = True
        else:
            d.append(a[end])
            end -= 1
            lft = False
            smll = True
    else:
        if not(lft):
            d.appendleft(a[fst])
            fst += 1
            lft = True
        else:
            d.append(a[fst])
            fst -= 1
            lft = False
            smll = False

memo = d.pop()
ans = 0
for i in range(n-1):
    memo2 = d.pop()
    ans += abs(memo-memo2)
    memo = memo2
    
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = sorted(int(input()) for _ in range(N))
if N % 2:
    B1 = A[:N // 2]
    C1 = A[N // 2:]
    c1 = 2 * sum(C1) - C1[0] - C1[1]
    b1 = 2 * sum(B1)
    B2 = A[:N // 2 + 1]
    C2 = A[N // 2 + 1:]
    c2 = 2 * sum(C2)
    b2 = 2 * sum(B2) - B2[-1] - B2[-2]
    print(max(c1 - b1, c2 - b2))
else:
    C = A[:N // 2]
    B = A[N // 2:]
    c = 2 * sum(C) - C[0]
    b = 2 * sum(B) - B[-1]
    print(c - b)

Submission status: Wrong Answer

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

def calc(flag):
  l = A[0]
  r = A[N-1]
  ans = r - l
  li = 1
  ri = N-2
  counter = 0
  while (li <= ri):
    #print(l,r,li,ri)
    if counter % 2 == flag:
      next = A[li]
      li += 1
    else:
      next = A[ri]
      ri -= 1
    if (abs(next - l) > abs(next - r)):
      ans += abs(next - l)
      l = next
    elif (abs(next - l) < abs(next - r)):
      ans += abs(next - r)
      r = next
    else:
      if (l > r and counter % 2 == flag) or (l < r and counter % 2 == 1 - flag):
        ans += abs(next - l)
        l = next
      else:
        ans += abs(next - r)
        r = next
    counter += 1
  return ans

print(max(calc(0),calc(1)))

Submission status: Wrong Answer

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

def calc(flag):
  l = A[0]
  r = A[N-1]
  ans = r - l
  li = 1
  ri = N-2
  counter = 0
  while (li <= ri):
    #print(l,r,li,ri)
    if counter % 2 == flag:
      next = A[li]
      li += 1
    else:
      next = A[ri]
      ri -= 1
    if (abs(next - l) > abs(next - r)):
      ans += abs(next - l)
      l = next
    elif (abs(next - l) < abs(next - r)):
      ans += abs(next - r)
      r = next
    else:
      if (l > r and counter % 2 == flag) or (l < r and counter % 2 == flag):
        ans += abs(next - l)
        l = next
      else:
        ans += abs(next - r)
        r = next
    counter += 1
  return ans

print(max(calc(0),calc(1)))

Submission status: Wrong Answer

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

def calc(flag):
  l = A[0]
  r = A[N-1]
  ans = r - l
  li = 1
  ri = N-2
  counter = 0
  while (li <= ri):
    #print(l,r,li,ri)
    if counter % 2 == flag:
      next = A[li]
      li += 1
    else:
      next = A[ri]
      ri -= 1
    if (abs(next - l) > abs(next - r)):
      ans += abs(next - l)
      l = next
    else:
      ans += abs(next - r)
      r = next
    counter += 1
  return ans

print(max(calc(0),calc(1)))

Submission status: Wrong Answer

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

l = A[0]
r = A[N-1]
ans = r - l
li = 1
ri = N-2
counter = 0
while (li <= ri):
  #print(l,r,li,ri)
  if counter % 2 == 0:
    next = A[li]
    li += 1
  else:
    next = A[ri]
    ri -= 1
  if (abs(next - l) > abs(next - r)):
    ans += abs(next - l)
    l = next
  elif (abs(next - l) < abs(next - r)):
    ans += abs(next - r)
    r = next
  else:
    if (l > r and counter % 2 == 0) or (l < r and counter % 2 == 1):
      ans += abs(next - l)
      l = next
    else:
      ans += abs(next - r)
      r = next
  counter += 1
print(ans)

Submission status: Wrong Answer

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

l = A[0]
r = A[N-1]
ans = r - l
li = 1
ri = N-2
counter = 0
while (li <= ri):
  #print(l,r,li,ri)
  if counter % 2 == 0:
    next = A[li]
    li += 1
  else:
    next = A[ri]
    ri -= 1
  if abs(next - l) > abs(next - r):
    ans += abs(next - l)
    l = next
  else:
    ans += abs(next - r)
    r = next
  counter += 1
print(ans)

Submission status: Wrong Answer

Solution code:
import math
import collections

n = int(input())
num = [int(input()) for _ in range(n)]
num.sort()
num2 = collections.deque(num)

center = math.ceil(n / 2) - 1
ans = 0
last = num[center]
cnt = 1
if (abs(num2[-1] - last) > abs(num2[0] - last)):
    LR = 1
else:
    LR = 0

while (True):
    if (cnt == n):
        break
    if (LR == 0):
        t = num2.popleft()
    else:
        t = num2.pop()
    cnt += 1
    LR = 1 - LR
    ans += abs(last - t)
    last = t
print(ans)

Submission status: Wrong Answer

Solution code:
import copy


def align(a,  l, r, min_pop):
    rst = abs(l-r)
    while len(a) > 0:
        if min_pop:
            i = a.pop()
        else:
            i = a.pop(0)
        min_pop = not min_pop
    
        cl = abs(l - i)
        cr = abs(r - i)
    
        if cl > cr:
            l = i
            rst += cl
        else:
            r = i
            rst += cr
    return rst

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

l = a.pop(0)
r = a.pop()

rst1 = align(copy.deepcopy(a), l, r, True)
rst2 = align(a, l, r, False)

print(max(rst1, rst2))

Submission status: Wrong Answer

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

l = a.pop(0)
r = a.pop()

rst = abs(l-r)

min_pop = True
while len(a) > 0:
    if min_pop:
        i = a.pop()
    else:
        i = a.pop(0)
    min_pop = not min_pop
    
    cl = abs(l - i)
    cr = abs(r - i)
    
    if cl > cr:
        l = i
        rst += cl
    else:
        r = i
        rst += cr

print(rst)

Submission status: Wrong Answer

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

l = a.pop(0)
r = a.pop()

rst = abs(l-r)

min_pop = False
while len(a) > 0:
    if min_pop:
        i = a.pop()
    else:
        i = a.pop(0)
    min_pop = not min_pop
    
    cl = abs(l - i)
    cr = abs(r - i)
    
    if cl > cr:
        l = i
        rst += cl
    else:
        r = i
        rst += cr

print(rst)

Submission status: Wrong Answer

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

A.sort()
ans = 0
if N%2==0:
    B1 = A[:N//2]
    B2 = A[N//2:]
    #print(B1, B2)
    for i in range(N//2):
        ans += abs(B1[i] - B2[i])
    for i in range(N//2-1):
        ans += abs(B2[i+1] - B1[i])
else:
    p = 0
    B1 = A[:N//2]
    B2 = A[N//2:]
    for i in range(N//2):
        p += abs(B2[i-1] - B1[i]) + abs(B2[i] - B1[i])

    q = 0
    C1 = A[:N//2+1]
    C2 = A[N//2+1:]
    for i in range(N//2):
        q += abs(C1[i-1] - C2[i]) + abs(C1[i] - C2[i])
    #print(B1, B2)
    #print(C1, C2)
    #print(p, q)
    ans = max(p, q)

print(ans)

Submission status: Wrong Answer

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

A.sort()
q = deque(A)

B = [0] * N

center = N // 2
minimum = q.popleft()
B[center] = minimum
left = center - 1
right = center + 1
cnt = 0
while q:
    if cnt % 4 == 0:
        num = q.pop()
        B[left] = num
        left -= 1
    elif cnt % 4 == 1:
        num = q.pop()
        B[right] = num
        right += 1
    elif cnt % 4 == 2:
        num = q.popleft()
        B[left] = num
        left -= 1
    elif cnt % 4 == 3:
        num = q.popleft()
        B[right] = num
        right += 1
    
    cnt += 1

ans = 0
for a,b in zip(B, B[1:]):
    ans += abs(a-b)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys,bisect as bs,collections as cl,heapq as hq
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

n = onem()
a = []
for i in range(n):
    a.append(onem())
a.sort()
bbb = a[n//2+(n%2):]
bbb.reverse()

bbbb = a[n//2:]

if len(bbb) >= 3:
    bbbb = [bbbb[0]] + bbbb[2:] + [bbbb[1]]
elif len(bbb) == 2:
    bbbb.reverse()

aaa = a[:n//2+(n%2)]
if len(bbb) >= 3:
    aaa = [aaa[-1]] + aaa[:-2] + [aaa[-2]]

aaaa = a[:n//2]

lll = []
llll = []

for i in range(n):
    if i % 2 == 0:
        lll.append(aaa[i//2])
        llll.append(bbbb[i//2])
    else:
        lll.append(bbb[i//2])
        llll.append(aaaa[i//2])
ans = 0
an = 0
for i in range(n):
    if i == 0:
        st = lll[0]
        stt = llll[0]
    else:
        ans += abs(lll[i] - st)
        st = lll[i]
        an += abs(llll[i] - stt)
        stt = llll[i]
print(max(ans,an))

Submission status: Wrong Answer

Solution code:
import sys,bisect as bs,collections as cl,heapq as hq
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

n = onem()
a = []
for i in range(n):
    a.append(onem())
a.sort()
bbb = a[n//2+(n%2):]
bbb.reverse()

bbbb = a[n//2:]

if len(bbb) >= 3:
    bbbb = [bbbb[0]] + bbbb[2:] + [bbbb[1]]


aaa = a[:n//2+(n%2)]
if len(bbb) >= 3:
    aaa = [aaa[-1]] + aaa[:-2] + [aaa[-2]]

aaaa = a[:n//2]

lll = []
llll = []

for i in range(n):
    if i % 2 == 0:
        lll.append(aaa[i//2])
        llll.append(bbbb[i//2])
    else:
        lll.append(bbb[i//2])
        llll.append(aaaa[i//2])
ans = 0
an = 0
for i in range(n):
    if i == 0:
        st = lll[0]
        stt = llll[0]
    else:
        ans += abs(lll[i] - st)
        st = lll[i]
        an += abs(llll[i] - stt)
        stt = llll[i]
print(max(ans,an))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import deque
def solve():
  n = int(input())
  a = [int(input()) for _ in range(n)]
  query = deque([])
  a = sorted(a,reverse = True)
  big = []
  small = []
  for i in range(n):
    if i< n//2:
      big.append(a[i])
    else:
      small.append(a[i])
  small.sort()       
  query.append(big.pop(0))

  number = 1
  noright = False
  
  while len(big) > 0 or len(small) > 0:
    if number %2 == 0:
      leftnumber = big.pop(0)
      if len(big)!= 0:
        rightnumber = big.pop(0)
      else:
        noright = True
    else:
      leftnumber = small.pop(0)
      if len(small) != 0:
        rightnumber = small.pop(0)
      else:
        noright = True
    query.appendleft(leftnumber)
    if not noright:
      query.append(rightnumber)
    number += 1
    noright = False


  if len(big) > 0:
    leftnumber = big.pop(0)
    query.appendleft(leftnumber)
  if len(small) > 0:
    leftnumber = small.pop(0)
    query.appendleft(leftnumber)

  ans = 0
  for i in range(len(query)-1):
    ans += abs(query[i+1]-query[i])
    
  query2 = [query[n-1]]
  for i in range(n-1):
    query2.append(query[i])
  ans2 = 0
  for i in range(len(query)-1):
    ans2 += abs(query2[i+1]-query2[i])
    
  query3 = [query[0]]
  for i in range(n-1):
    query3.append(query[i+1])
  ans3 = 0
  for i in range(len(query)-1):
    ans3 += abs(query3[i+1]-query3[i])
  print(max([ans,ans2,ans3]))
solve()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import deque
def solve():
  n = int(input())
  a = [int(input()) for _ in range(n)]
  query = deque([])
  a = sorted(a,reverse = True)
  big = []
  small = []
  for i in range(n):
    if i< n//2:
      big.append(a[i])
    else:
      small.append(a[i])
  small.sort()       
  query.append(big.pop(0))

  number = 1
  noright = False
  
  while len(big) > 0 or len(small) > 0:
    if number %2 == 0:
      leftnumber = big.pop(0)
      if len(big)!= 0:
        rightnumber = big.pop(0)
      else:
        noright = True
    else:
      leftnumber = small.pop(0)
      if len(small) != 0:
        rightnumber = small.pop(0)
      else:
        noright = True
    query.appendleft(leftnumber)
    if not noright:
      query.append(rightnumber)
    number += 1


  if len(big) > 0:
    leftnumber = big.pop(0)
    query.appendleft(leftnumber)
  if len(small) > 0:
    leftnumber = small.pop(0)
    query.appendleft(leftnumber)

  ans = 0
  for i in range(len(query)-1):
    ans += abs(query[i+1]-query[i])
    
  query2 = [query[n-1]]
  for i in range(n-1):
    query2.append(query[i])
  ans2 = 0
  for i in range(len(query)-1):
    ans2 += abs(query2[i+1]-query2[i])
    
  query3 = [query[0]]
  for i in range(n-1):
    query3.append(query[i+1])
  ans3 = 0
  for i in range(len(query)-1):
    ans3 += abs(query3[i+1]-query3[i])
  print(max([ans,ans2,ans3]))
solve()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import deque
def solve():
  n = int(input())
  a = [int(input()) for _ in range(n)]
  query = deque([])
  a = sorted(a,reverse = True)
  big = []
  small = []
  for i in range(n):
    if i< n//2:
      big.append(a[i])
    else:
      small.append(a[i])
  small.sort()       
  query.append(big.pop(0))

  number = 1
  noright = False
  
  while len(big) > 0 or len(small) > 0:
    if number %2 == 0:
      leftnumber = big.pop(0)
      if len(big)!= 0:
        rightnumber = big.pop(0)
      else:
        noright = True
    else:
      leftnumber = small.pop(0)
      if len(small) != 0:
        rightnumber = small.pop(0)
      else:
        noright = True
    query.appendleft(leftnumber)
    if not noright:
      query.append(rightnumber)
    number += 1


  if len(big) > 0:
    leftnumber = big.pop(0)
    query.appendleft(leftnumber)
  if len(small) > 0:
    leftnumber = small.pop(0)
    query.appendleft(leftnumber)

  ans = 0
  for i in range(len(query)-1):
    ans += abs(query[i+1]-query[i])
    
  query2 = [query[n-1]]
  for i in range(n-1):
    query2.append(query[i])
  ans2 = 0
  for i in range(len(query)-1):
    ans2 += abs(query2[i+1]-query2[i])
  print(max(ans,ans2))
solve()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import deque
def solve():
  n = int(input())
  a = [int(input()) for _ in range(n)]
  query = deque([])
  a = sorted(a,reverse = True)
  big = []
  small = []
  for i in range(n):
    if i< n//2:
      big.append(a[i])
    else:
      small.append(a[i])
  small.sort()       
  query.append(big.pop(0))

  number = 1
  noright = False
  
  while len(big) > 0 or len(small) > 0:
    if number %2 == 0:
      leftnumber = big.pop(0)
      if len(big)!= 0:
        rightnumber = big.pop(0)
      else:
        noright = True
    else:
      leftnumber = small.pop(0)
      if len(small) != 0:
        rightnumber = small.pop(0)
      else:
        noright = True
    query.appendleft(leftnumber)
    if not noright:
      query.append(rightnumber)
    number += 1


  if len(big) > 0:
    leftnumber = big.pop(0)
    query.appendleft(leftnumber)

  ans = 0
  for i in range(len(query)-1):
    ans += abs(query[i+1]-query[i])
    
  query2 = [query[n-1]]
  for i in range(n-1):
    query2.append(query[i])
  ans2 = 0
  for i in range(len(query)-1):
    ans2 += abs(query2[i+1]-query2[i])
  print(max(ans,ans2))
solve()

Submission status: Wrong Answer

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

def main():
    N = int(input())
    a = sorted([int(input()) for i in range(N)])

    q = deque()
    q.append(a[0])
    q.append(a[-1])
    
    for i in range(1, N//2):
        num1 = a[i]
        if abs(num1 - q[0]) >= abs(num1 - q[-1]):
            q.appendleft(num1)
        else:
            q.append(num1)
        num1 = a[N-1-i]
        if abs(num1 - q[0]) >= abs(num1 - q[-1]):
            q.appendleft(num1)
        else:
            q.append(num1)
    
    if N&1:
        num1 = a[N//2]
        if abs(num1 - q[0]) >= abs(num1 - q[-1]):
            q.appendleft(num1)
        else:
            q.append(num1)

    # print(q)
    ans = 0
    for i in range(1, N):
        ans += abs(q[i] - q[i-1])
    print(ans)


if __name__ == "__main__":
    main()