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:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import permutations
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7


#############
# Main Code #
#############

N = getN()
A = sorted(getArray(N))
A = deque(A)
ans_list = deque([])

cnt = 0
while len(A) > 0:
    if cnt % 2 == 0:
        m = A.popleft()
        if cnt % 4 == 0:
            ans_list.append(m)
        else:
            ans_list.appendleft(m)
    else:
        m = A.pop()
        if cnt % 4 == 1:
            ans_list.appendleft(m)
        else:
            ans_list.append(m)
    cnt += 1

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

Submission status: Wrong Answer

Solution code:
from collections import deque

n = int(input())
a = sorted(int(input()) for _ in range(n))
L =[]
for i in range(2):
    q = deque(a)
    l = q.popleft()
    r = q.pop()
    ans = abs(l - r)
    flag = i
    while q:
        if flag == 0:
            x = q.popleft()
            flag = 1
        else:
            x = q.pop()
            flag = 0
        if abs(l - x) >= abs(x - r):
            ans += abs(l - x)
            l = x
        else:
            ans += abs(x - r)
            r = x
    L.append(ans)
print(max(L))

Submission status: Wrong Answer

Solution code:
from collections import deque

n = int(input())
a = sorted(int(input()) for _ in range(n))
q = deque(a)
l = q.popleft()
r = q.pop()
ans = abs(l - r)
flag = 0
while q:
    if flag == 0:
        x = q.popleft()
        flag = 1
    else:
        x = q.pop()
        flag = 0
    if abs(l - x) >= abs(x - r):
        ans += abs(l - x)
        l = x
    else:
        ans += abs(x - r)
        r = x
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
n = int(input())
A = []
for _ in range(n):
    A.append(int(input()))
if n == 2:
    print(abs(A[1]-A[0]))
    exit()
dq = deque(sorted(A))
ans = deque([dq.popleft()])
ans.append(dq.pop())
ans.appendleft(dq.pop())
flag = True
while dq:
    if flag:
        ans.append(dq.popleft())
        if dq:
            ans.appendleft(dq.popleft())
            flag = False
    else:
        ans.append(dq.pop())
        if dq:
            ans.appendleft(dq.pop())
            flag = True
B = list(ans)
s = 0
for i in range(n-1):
    s += abs(B[i+1]-B[i])
print(s)

Submission status: Wrong Answer

Solution code:
from collections import deque

N = int(input())
A = deque(sorted([int(input()) for i in range(N)]))
ans = deque([A.popleft()])
while A:
    if A:
        ans.appendleft(A.pop())
    if A:
        ans.append(A.pop())
    if A:
        ans.appendleft(A.popleft())
    if A:
        ans.append(A.popleft())
res = 0
ans = list(ans)
for i in range(N - 1):
    res += abs(ans[i] - ans[i + 1])
print(res)

Submission status: Wrong Answer

Solution code:
from collections import deque
N = int(input())
# n, a, b = [int(i) for i in input().split()]
q = []
for i in range(N):
    A = int(input())
    q.append(A)

q.sort()
q = deque(q)
row = []

for i in range(N):
    if i % 2 == 0:
        row.append(q.pop())
    else:
        row.append(q.popleft())
row2 = [row[-1]] + row[:-1]
row3 = row[1:] + [row[0]]
row_diff = [abs(row[i] - row[i-1]) for i in range(1, N)]
row2_diff = [abs(row2[i] - row2[i-1]) for i in range(1, N)]
row3_diff = [abs(row3[i] - row3[i-1]) for i in range(1, N)]
print(max(sum(row_diff), sum(row2_diff), sum(row3_diff)))

Submission status: Wrong Answer

Solution code:
from collections import deque
N = int(input())
# n, a, b = [int(i) for i in input().split()]
q = []
for i in range(N):
    A = int(input())
    q.append(A)

q.sort()
q = deque(q)
row = []

for i in range(N):
    if i % 2 == 0:
        row.append(q.pop())
    else:
        row.append(q.popleft())
row2 = [row[-1]] + row[:-1]
row_diff = [abs(row[i] - row[i-1]) for i in range(1, N)]
row2_diff = [abs(row2[i] - row2[i-1]) for i in range(1, N)]
print(max(sum(row_diff), sum(row2_diff)))

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    N = int(input())
    A = [0] * N
    for n in range(N):
        A[n] = int(input())
    
    A.sort()
    t1 = A[0]
    t2 = t1
    ans = 0
    l = 0
    r = N
    while True:
        if r - 2 > l:
            c1, c2 = A[r-1], A[r-2]
            ans += (c1 - t1) + (c2 - t2)
            t1, t2 = c1, c2
            r -=2
        elif r - 1 > l:
            ans += max(A[r-1]-t1, A[r-1]-t2)
            break
        else:
            break

        if l + 2 < r:
            c1, c2 = A[l+1], A[l+2]
            ans += (t1 - c1) + (t2 - c2)
            t1, t2 = c1, c2
            l += 2
        elif l + 1 < r:
            ans += max(t1-A[l+1], t2-A[l+1])
            break
        else:
            break

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline


n = int(input())
L = [int(input()) for _ in range(n)]
L.sort()
n_2 =n // 2

ans = 0
if n % 2 == 1:
    ans = 2*sum(L[n_2+1:])-2*sum(L[:n_2-1])-L[n_2-1]-L[n_2]
else:
    ans = 2*sum(L[n_2+1:])-2*sum(L[:n_2-1])+L[n_2]-L[n_2-1]
    

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline


n = int(input())
L = [int(input()) for _ in range(n)]
L.sort()
n_2 =n // 2

ans = 0
if n % 4 == 1:
    ans = 2*sum(L[n_2+1:])-2*sum(L[:n_2-1])-L[n_2-1]-L[n_2]
elif n % 4 == 3:
    ans = 2*sum(L[n_2+1:])-2*sum(L[:n_2])
else:
    ans = 2*sum(L[n_2+1:])-2*sum(L[:n_2-1])+L[n_2]-L[n_2-1]

print(ans)

Submission status: Wrong Answer

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

l.sort()
ans = []
mid = 0

if len(l)%2 == 0:
    mid = len(l)//2-1
else:
    mid = len(l)//2


f = l[mid]
ans.append(f)
l_l = l[0:mid]
l_r = l[mid+1:n+1]
l_r.reverse()


if len(l_l) == len(l_r) and len(l_l) != 1:
    for i in range(len(l_r)):
        ans.append(l_r[i])
        ans.append(l_l[i])
elif len(l_l) == len(l_r) and len(l_l) == 1:
    ans.append(l_l[0])
    ans.append(l_r[0])
else:
    for i in range(len(l_r)):
        if i+1 == len(l_r):
            ans.append(l_r[i])
        else:
            ans.append(l_r[i])
            ans.append(l_l[i])


cnt = []

for j in range(len(ans)-1):
    cnt.append(abs(ans[j+1]-ans[j]))


print(sum(cnt))

Submission status: Wrong Answer

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

l.sort()
ans = []
mid = 0

if len(l)%2 == 0:
    mid = len(l)//2-1
else:
    mid = len(l)//2


f = l[mid]
ans.append(f)
l_l = l[0:mid]
l_r = l[mid+1:n+1]
l_r.reverse()
print(l_l)
print(l_r)

if len(l_l) == len(l_r) and len(l_l) != 1:
    for i in range(len(l_r)):
        ans.append(l_r[i])
        ans.append(l_l[i])
elif len(l_l) == len(l_r) and len(l_l) == 1:
    ans.append(l_l[0])
    ans.append(l_r[0])
else:
    for i in range(len(l_r)):
        if i+1 == len(l_r):
            ans.append(l_r[i])
        else:
            ans.append(l_r[i])
            ans.append(l_l[i])


cnt = []

for j in range(len(ans)-1):
    cnt.append(abs(ans[j+1]-ans[j]))

print(ans)
print(sum(cnt))

Submission status: Wrong Answer

Solution code:
import bisect
from collections import deque

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

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

    a.sort()
    d = deque(a)
    res = 0
    t1 = d.popleft()
    t = 1
    first = True
    #真ん中に一番小さいのその両隣に一番大きいやつその一番大きいやつの隣に小さいの→大きいの→小さいの....
    if(len(a)==2):
        print(a[0]-a[1])
        exit()

    while len(d)>=2:
        if t==1:
            d1 = d.pop()
            d2 = d.pop()
            if first:
                first = False
                res += (d1-t1+d2-t1)
            else:
                res += d1-t2
                res += d2-t3
        else:
            t2 = d.popleft()
            t3 = d.popleft()
            res += d1 - t2
            res += d2 - t3
        if(len(d)==1):
            k = d.popleft()
            if(t==1):
                res += max(abs(max(d1,d2)-k), min(d1,d2)-k)
            else:
                res += max(abs(k - min(t1,t2)), abs(k-max(t1,t2)))


        t = t * -1

    print(res)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

def input(): return sys.stdin.readline().strip()
def Z(): return int(input())
def ZZ(): return [int(_) for _ in input().split()]

def main():
    N = Z()
    A = sorted([Z() for _ in range(N)])
    used = [0] * (N)
    output1 = output2 = 0

    i1 = i2 = 0
    j1, j2 = N-1, N-2
    f1 = f2 = True
    used[i1], used[i2], used[j1], used[j2] = 1, 1, 1, 1

    while f1 or f2:
        if f1: output1 += (A[j1]-A[i1])
        if f2: output1 += (A[j2]-A[i2])

        while used[i1]:
            if i1 == N-1:
                f1 = False
                break
            i1 += 1
        used[i1] = 1

        while used[i2]:
            if i2 == N-1:
                f2 = False
                break
            i2 += 1
        used[i2] = 1

        if f1: output1 += (A[j1]-A[i1])
        if f2: output1 += (A[j2]-A[i2])

        while used[j1]:
            if j1 == 0:
                f1 = False
                break
            j1 -= 1
        used[j1] = 1
        while used[j2]:
            if j2 == 0:
                f2 = False
                break
            j2 -= 1
        used[j2] = 1

    i1 = i2 = N-1
    j1, j2 = 0, 1
    f1 = f2 = True
    used = [0] * (N)
    used[i1], used[i2], used[j1], used[j2] = 1, 1, 1, 1

    while f1 or f2:
        if f1: output2 += (A[j1]-A[i1])
        if f2: output2 += (A[j2]-A[i2])

        while used[i1]:
            if i1 == 0:
                f1 = False
                break
            i1 -= 1
        used[i1] = 1

        while used[i2]:
            if i2 == 0:
                f2 = False
                break
            i2 -= 1
        used[i2] = 1

        if f1: output2 += (A[j1]-A[i1])
        if f2: output2 += (A[j2]-A[i2])

        while used[j1]:
            if j1 == N-1:
                f1 = False
                break
            j1 += 1
        used[j1] = 1
        while used[j2]:
            if j2 == N-1:
                f2 = False
                break
            j2 += 1
        used[j2] = 1
    print(max(output1, output2))

    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

def input(): return sys.stdin.readline().strip()
def Z(): return int(input())
def ZZ(): return [int(_) for _ in input().split()]

def main():
    N = Z()
    A = sorted([Z() for _ in range(N)])
    used = [0] * (N)
    output1 = output2 = 0
    
    i1 = i2 = 0
    j1, j2 = N-1, N-2
    f1 = f2 = True
    used[i1], used[i2], used[j1], used[j2] = 1, 1, 1, 1

    while f1 or f2:
        if f1: output1 += (A[j1]-A[i1])
        if f2: output1 += (A[j2]-A[i2])

        while used[i1]:
            if i1 == N-1:
                f1 = False
                break
            i1 += 1
        used[i1] = 1

        while used[i2]:
            if i2 == N-1:
                f2 = False
                break
            i2 += 1
        used[i2] = 1

        if f1: output1 += (A[j1]-A[i1])
        if f2: output1 += (A[j2]-A[i2])

        while used[j1]:
            if j1 == 0:
                f1 = False
                break
            j1 -= 1
        used[j1] = 1
        while used[j2]:
            if j2 == 0:
                f2 = False
                break
            j2 -= 1
        used[j2] = 1

    i1 = i2 = N-1
    j1, j2 = 0, 1
    f1 = f2 = True
    used[i1], used[i2], used[j1], used[j2] = 1, 1, 1, 1

    while f1 or f2:
        if f1: output2 += (A[j1]-A[i1])
        if f2: output2 += (A[j2]-A[i2])

        while used[i1]:
            if i1 == 0:
                f1 = False
                break
            i1 -= 1
        used[i1] = 1

        while used[i2]:
            if i2 == 0:
                f2 = False
                break
            i2 -= 1
        used[i2] = 1

        if f1: output2 += (A[j1]-A[i1])
        if f2: output2 += (A[j2]-A[i2])

        while used[j1]:
            if j1 == N-1:
                f1 = False
                break
            j1 += 1
        used[j1] = 1
        while used[j2]:
            if j2 == N-1:
                f2 = False
                break
            j2 += 1
        used[j2] = 1
    print(max(output1, output2))

    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

def input(): return sys.stdin.readline().strip()
def Z(): return int(input())
def ZZ(): return [int(_) for _ in input().split()]

def main():
    N = Z()
    A = sorted([Z() for _ in range(N)])
    used = [0] * (N)
    output = 0
    i1 = i2 = 0
    j1, j2 = N-1, N-2
    f1 = f2 = True
    used[i1], used[i2], used[j1], used[j2] = 1, 1, 1, 1

    while f1 or f2:
        if f1: output += (A[j1]-A[i1])
        if f2: output += (A[j2]-A[i2])

        while used[i1]:
            if i1 == N-1:
                f1 = False
                break
            i1 += 1
        used[i1] = 1

        while used[i2]:
            if i2 == N-1:
                f2 = False
                break
            i2 += 1
        used[i2] = 1

        if f1: output += (A[j1]-A[i1])
        if f2: output += (A[j2]-A[i2])

        while used[j1]:
            if j1 == 0:
                f1 = False
                break
            j1 -= 1
        used[j1] = 1
        while used[j2]:
            if j2 == 0:
                f2 = False
                break
            j2 -= 1
        used[j2] = 1
    print(output)

    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

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

a.sort()
if a[0] != a[1]:
    dqa = deque(a[1:])
    dq = deque([a[0]])
    cnt = 0
else: 
    dqa = deque(a[:N-1])
    dq = deque([a[-1]])
    cnt = 2

while len(dq) < N:
    if cnt == 0:
        q = dqa.pop()
        dq.append(q)
    elif cnt == 1:
        q = dqa.pop()
        dq.appendleft(q)
    elif cnt == 2:
        q = dqa.popleft()
        dq.append(q)
    else:
        q = dqa.popleft()
        dq.appendleft(q)
        cnt = -1
    cnt += 1

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

Submission status: Wrong Answer

Solution code:
from collections import deque

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

a.sort()
dqa = deque(a[1:])

dq = deque([a[0]])
cnt = 0
while len(dq) < N:
    if cnt == 0:
        q = dqa.pop()
        dq.append(q)
    elif cnt == 1:
        q = dqa.pop()
        dq.appendleft(q)
    elif cnt == 2:
        q = dqa.popleft()
        dq.append(q)
    else:
        q = dqa.popleft()
        dq.appendleft(q)
        cnt = -1
    cnt += 1

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

Submission status: Wrong Answer

Solution code:
N=int(input())
a = [int(input()) for _ in range(N)]
b = [0]*N

if N%2==1:
  #最大値を中央に置くケース
  a.sort()
  m = N//2
  L,R = m,m
  b[m]=a[-1]
  for i in range(m):
    L -= 1
    R += 1
    if i%2==0:
      b[L]=a[2*i]
      b[R]=a[2*i+1]
    else:
      b[L]=a[-2-2*(i-1)]
      b[R]=a[-3-2*(i-1)]
  ans1=0
  for i in range(1,N):
    ans1 += abs(b[i]-b[i-1])
  #print(b)
  
  #最小値を中央に置くケース
  a.sort()
  m = N//2
  L,R = m,m
  b[m]=a[0]
  for i in range(m):
    L -= 1
    R += 1
    if i%2==0:
      b[L]=a[-1-2*i]
      b[R]=a[-2-2*i]
    else:
      b[L]=a[2*(i-1)+1]
      b[R]=a[2*(i-1)+2]
  ans2=0
  for i in range(1,N):
    ans2 += abs(b[i]-b[i-1])
  ans=max(ans1,ans2)
  #print(b)
  print(ans)

else:
  a.sort()
  m = N//2
  L,R = m-1,m
  b[L]=a[0]
  b[R]=a[-1]
  
  for i in range(m-1):
    L -= 1
    R += 1
    if i%2==0:
      b[L]=a[-2-i]
      b[R]=a[i+1]
    else:
      b[L]=a[i+1]
      b[R]=a[-2-i]
  ans=0
  for i in range(1,N):
    ans += abs(b[i]-b[i-1])
  #print(b)
  print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
a = [int(input()) for _ in range(N)]
b = [0]*N

if N%2==1:
  #最大値を中央に置くケース
  a.sort()
  m = N//2
  L,R = m,m
  b[m]=a[-1]
  for i in range(m):
    L -= 1
    R += 1
    if i%2==0:
      b[L]=a[2*i]
      b[R]=a[2*i+1]
    else:
      b[L]=a[-2-2*(i-1)]
      b[R]=a[-3-2*(i-1)]
  ans1=0
  for i in range(1,N):
    ans1 += abs(b[i]-b[i-1])
  print(b)
  
  #最小値を中央に置くケース
  a.sort()
  m = N//2
  L,R = m,m
  b[m]=a[0]
  for i in range(m):
    L -= 1
    R += 1
    if i%2==0:
      b[L]=a[-1-2*i]
      b[R]=a[-2-2*i]
    else:
      b[L]=a[2*(i-1)+1]
      b[R]=a[2*(i-1)+2]
  ans2=0
  for i in range(1,N):
    ans2 += abs(b[i]-b[i-1])
  ans=max(ans1,ans2)
  #print(b)
  print(ans)

else:
  a.sort()
  m = N//2
  L,R = m-1,m
  b[L]=a[0]
  b[R]=a[-1]
  
  for i in range(m-1):
    L -= 1
    R += 1
    if i%2==0:
      b[L]=a[-2-i]
      b[R]=a[i+1]
    else:
      b[L]=a[i+1]
      b[R]=a[-2-i]
  ans=0
  for i in range(1,N):
    ans += abs(b[i]-b[i-1])
  #print(b)
  print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
a = [int(input()) for _ in range(N)]
b = [0]*N

if N%2==1:
  #最大値を中央に置くケース
  a.sort()
  m = N//2
  L,R = m,m
  b[m]=a[-1]
  for i in range(m):
    L -= 1
    R += 1
    if i%2==0:
      b[L]=a[2*i]
      b[R]=a[2*i+1]
    else:
      b[L]=a[2*i+1]
      b[R]=a[2*i]
  ans1=0
  for i in range(1,N):
    ans1 += abs(b[i]-b[i-1])
  
  #最小値を中央に置くケース
  a.sort(reverse=True)
  m = N//2
  L,R = m,m
  b[m]=a[-1]
  for i in range(m):
    L -= 1
    R += 1
    if i%2==0:
      b[L]=a[2*i]
      b[R]=a[2*i+1]
    else:
      b[L]=a[2*i+1]
      b[R]=a[2*i]
  ans2=0
  for i in range(1,N):
    ans2 += abs(b[i]-b[i-1])
  ans=max(ans1,ans2)
  print(ans)

else:
  a.sort()
  m = N//2
  L,R = m-1,m
  b[L]=a[0]
  b[R]=a[-1]
  
  for i in range(m-1):
    L -= 1
    R += 1
    if i%2==0:
      b[L]=a[-2-i]
      b[R]=a[i+1]
    else:
      b[L]=a[i+1]
      b[R]=a[-2-i]
  ans=0
  for i in range(1,N):
    ans += abs(b[i]-b[i-1])
  print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

N=int(input())
li=[int(input()) for i in range(N)]
li.sort()
que=deque(li)

ans=deque()

ans.append(que.popleft())
ans.append(que.pop())
if que:
    ans.appendleft(que.pop())
t=True
while(que):
    if t:
        a=que.popleft()
        if ans[0]>ans[-1]:
            ans.appendleft(a)
        else:
            ans.append(a)
        if que:
            a=que.popleft()
            if ans[0]>ans[-1]:
                ans.appendleft(a)
            else:
                ans.append(a)
        t=not t
        continue
    else:
        a=que.pop()
        if ans[0]<ans[-1]:
            ans.appendleft(a)
        else:
            ans.append(a)
        if que:
            a=que.pop()
            if ans[0]<ans[-1]:
                ans.appendleft(a)
            else:
                ans.append(a)
res=0
for i in range(N-1):
    res+=abs(ans[i]-ans[i+1])
print(res)

Submission status: Wrong Answer

Solution code:
from collections import deque
n = int(input())
a = []
for _ in range(n):
    a.append(int(input()))
a.sort()
a = deque(a)
q = deque()
while True:
    tmp = a.popleft()
    if q and abs(tmp-q[0])<abs(tmp-q[-1]):
        q.append(tmp)
    else:
        q.appendleft(tmp)
    if not a:
        break
    tmp = a.pop()
    if abs(tmp-q[0])<abs(tmp-q[-1]):
        q.append(tmp)
    else:
        q.appendleft(tmp)
    if not a:
        break
q = list(q)
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())
a = []
for _ in range(n):
    a.append(int(input()))
a.sort()
a = deque(a)
q = deque()
while True:
    q.append(a.popleft())
    if not a:
        break
    q.appendleft(a.pop())
    if not a:
        break
    q.append(a.pop())
    if not a:
        break
    q.appendleft(a.popleft())
    if not a:
        break
q = list(q)
ans = 0
for i in range(n-1):
    ans += abs(q[i+1]-q[i])
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

# import re
import math
import collections
# import decimal
import bisect
import itertools
import fractions
# import functools
import copy
import heapq
import decimal
# import statistics
import queue

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    n = ni()

    small = queue.PriorityQueue()
    large = queue.PriorityQueue()
    for _ in range(n):
        tmp = ni()
        small.put(tmp)
        large.put(-tmp)

    res = [0 for i in range(n)]

    even = not n % 2
    idx = n // 2 - even
    res[idx] = small.get()
    cnt = 0
    while True:
        cnt += 1

        first = 0
        second = 0

        if cnt % 2:
            first = -large.get()
            if large.qsize() != 0:
                second = -large.get()
        else:
            first = small.get()
            if small.qsize() != 0:
                second = small.get()

        if 0 <= idx + cnt < n:
            res[idx + cnt] = first
        else:
            break
        if 0 <= idx - cnt < n:
            res[idx - cnt] = second
        else:
            break

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

# import re
import math
import collections
# import decimal
import bisect
import itertools
import fractions
# import functools
import copy
import heapq
import decimal
# import statistics
import queue

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    n = ni()
    a = [ni() for _ in range(n)]
    a.sort()

    odd = n % 2
    if odd:
        small = a[:n // 2 + 1]
        large = a[n // 2 + 1:]
        small.insert(0, small[-1])
        del small[-1]
    else:
        small = a[:n // 2]
        large = a[n // 2:]
        small.insert(0, small[-1])
        del small[-1]
        large.append(large[0])
        del large[0]

    res = []
    if n != 3:
        for i in range(n // 2):
            res.append(small[i])
            res.append(large[i])
        if odd:
            res.append(small[n // 2])
    else:
        res = small+ large



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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def tenka18beg_c():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    from collections import deque
    que = deque(sorted(A))
    mina = que.popleft()
    l = mina
    r = mina
    ans = 0
    mode = 0
    while que:
        if mode%4 == 0:
            x = que.pop()
            ans += abs(l - x)
            l = x
        elif mode%4 == 1:
            x = que.pop()
            ans += abs(r - x)
            r = x
        elif mode%4 == 2:
            x = que.popleft()
            ans += abs(l - x)
            l = x
        elif mode%4 == 3:
            x = que.popleft()
            ans += abs(r - x)
            r = x
        mode += 1
    print(ans)

tenka18beg_c()

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/tenka1-2018-beginner/tasks/tenka1_2018_c
from collections import deque
n = int(input())
nums = []
for _ in range(n):
    nums.append(int(input()))
nums.sort()
ans1 = 0
l = 0
r = n - 1
q = deque()
while l <= r:
    left = nums[l]
    right = nums[r]
    if not q:
        ans1 += abs(right - left)
        q.append(left)
        q.append(right)
    else:
        if abs(q[-1] - right) > abs(q[0] - right):
            ans1 += abs(q[-1] - right)
            q.append(right)
        else:
            ans1 += abs(q[0] - right)
            q.appendleft(right)
        if l == r:
            break
        if abs(q[-1] - left) > abs(q[0] - left):
            ans1 += abs(q[-1] - left)
            q.append(left)
        else:
            ans1 += abs(q[0] - left)
            q.appendleft(left)
    l += 1
    r -= 1

ans2 = 0
l = 0
r = n - 1
q = deque()
while l <= r:
    left = nums[l]
    right = nums[r]
    if not q:
        ans2 += abs(right - left)
        q.append(right)
        q.append(left)
    else:
        if abs(q[-1] - left) > abs(q[0] - left):
            ans2 += abs(q[-1] - left)
            q.append(left)
        else:
            ans2 += abs(q[0] - left)
            q.appendleft(left)
        if l == r:
            break
        if abs(q[-1] - right) > abs(q[0] - right):
            ans2 += abs(q[-1] - right)
            q.append(right)
        else:
            ans2 += abs(q[0] - right)
            q.appendleft(right)
    l += 1
    r -= 1
ans = max(ans1, ans2)
print(ans)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/tenka1-2018-beginner/tasks/tenka1_2018_c
from collections import deque
n = int(input())
nums = []
for _ in range(n):
    nums.append(int(input()))
nums.sort()
ans = 0
l = 0
r = n - 1
q = deque()
while l <= r:
    left = nums[l]
    right = nums[r]
    if not q:
        ans += abs(right - left)
        q.append(left)
        q.append(right)
    else:
        if abs(q[-1] - right) > abs(q[0] - right):
            ans += abs(q[-1] - right)
            q.append(right)
        else:
            ans += abs(q[0] - right)
            q.appendleft(right)
        if l == r:
            break
        if abs(q[-1] - left) > abs(q[0] - left):
            ans += abs(q[-1] - left)
            q.append(left)
        else:
            ans += abs(q[0] - left)
            q.appendleft(left)
    l += 1
    r -= 1
t = 0
for i in range(n - 1):
    t += abs(q[i + 1] - q[i])
print(t)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/tenka1-2018-beginner/tasks/tenka1_2018_c
from collections import deque
n = int(input())
nums = []
for _ in range(n):
    nums.append(int(input()))
nums.sort()
ans1 = 0
l = 0
r = n - 1
q = deque()
while l <= r:
    left = nums[l]
    right = nums[r]
    if not q:
        ans1 += abs(right - left)
        q.append(left)
        q.append(right)
    else:
        if abs(q[-1] - right) > abs(q[0] - right):
            ans1 += abs(q[-1] - right)
            q.append(right)
        else:
            ans1 += abs(q[0] - right)
            q.appendleft(right)
        if l == r:
            break
        if abs(q[-1] - left) > abs(q[0] - left):
            ans1 += abs(q[-1] - left)
            q.append(left)
        else:
            ans1 += abs(q[0] - left)
            q.appendleft(left)
    l += 1
    r -= 1

ans2 = 0
l = 0
r = n - 1
q = deque()
while l <= r:
    left = nums[l]
    right = nums[r]
    if not q:
        ans2 += abs(right - left)
        q.append(right)
        q.append(left)
    else:
        if abs(q[-1] - right) > abs(q[0] - right):
            ans2 += abs(q[-1] - right)
            q.append(right)
        else:
            ans2 += abs(q[0] - right)
            q.appendleft(right)
        if l == r:
            break
        if abs(q[-1] - left) > abs(q[0] - left):
            ans2 += abs(q[-1] - left)
            q.append(left)
        else:
            ans2 += abs(q[0] - left)
            q.appendleft(left)
    l += 1
    r -= 1
ans = max(ans1, ans2)
print(ans)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/tenka1-2018-beginner/tasks/tenka1_2018_c
from collections import deque
n = int(input())
nums = []
for _ in range(n):
    nums.append(int(input()))
nums.sort()
ans = 0
l = 0
r = n - 1
q = deque()
while l <= r:
    left = nums[l]
    right = nums[r]
    if not q:
        ans += abs(right - left)
        q.append(left)
        q.append(right)
    else:
        if abs(q[-1] - right) > abs(q[0] - right):
            ans += abs(q[-1] - right)
            q.append(right)
        else:
            ans += abs(q[0] - right)
            q.appendleft(right)
        if l == r:
            break
        if abs(q[-1] - left) > abs(q[0] - left):
            ans += abs(q[-1] - left)
            q.append(left)
        else:
            ans += abs(q[0] - left)
            q.appendleft(left)
    l += 1
    r -= 1
print(ans)