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=6
#A=[3,1,4,1,5,9]
N=int(input())
A=[0]*N
for i in range(N):
    A[i]=int(input())

A=sorted(A)

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

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

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

print(max(d,c))

Submission status: Wrong Answer

Solution code:
N=6
A=[3,1,4,1,5,9]

A=sorted(A)

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

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

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

print(max(d,c))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

ans = 0
A.sort()

tmp1 = A[0]
tmp2 = A[0]

count = 1
flag = True
i = 1
j = 1
while True:
    if flag:
        if tmp1 < tmp2:
            tmp1, tmp2 = tmp2, tmp1
    else:
        if tmp1 > tmp2:
            tmp1, tmp2 = tmp2, tmp1


    if count == N:
        break
    if flag:
        ans += abs(tmp1 - A[-i])
        tmp1 = A[-i]
        i += 1
    else:
        ans += abs(tmp1 - A[j])
        tmp1 = A[j]
        j += 1

    count += 1

    if count == N:
        break
    if flag:
        ans += abs(tmp2 - A[-i])
        tmp2 = A[-i]
        i += 1
    else:
        ans += abs(tmp2 - A[j])
        tmp2 = A[j]
        j += 1
    
    count += 1

    flag = not flag
    # print ('A', ans)

# print (i, j)
print (ans)

Submission status: Wrong Answer

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

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

ans = 0
A.sort()

tmp1 = A[0]
tmp2 = A[0]

count = 1
flag = True
i = 1
j = 1
while True:
    if flag:
        if tmp1 > tmp2:
            tmp1, tmp2 = tmp2, tmp1
    else:
        if tmp1 < tmp2:
            tmp1, tmp2 = tmp2, tmp1


    if count == N:
        break
    if flag:
        ans += abs(tmp1 - A[-i])
        tmp1 = A[-i]
        i += 1
    else:
        ans += abs(tmp1 - A[j])
        tmp1 = A[j]
        j += 1

    count += 1

    if count == N:
        break
    if flag:
        ans += abs(tmp2 - A[-i])
        tmp2 = A[-i]
        i += 1
    else:
        ans += abs(tmp2 - A[j])
        tmp2 = A[j]
        j += 1
    
    count += 1

    flag = not flag
    # print ('A', ans)

# print (i, j)
print (ans)

Submission status: Wrong Answer

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

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

ans = 0
A.sort()

tmp1 = A[0]
tmp2 = A[0]

count = 1
flag = True
i = 1
j = 1
while True:
    if count == N:
        break
    if flag:
        ans += abs(tmp1 - A[-i])
        tmp1 = A[-i]
        i += 1
    else:
        ans += abs(tmp1 - A[j])
        tmp1 = A[j]
        j += 1

    count += 1

    if count == N:
        break
    if flag:
        ans += abs(tmp2 - A[-i])
        tmp2 = A[-i]
        i += 1
    else:
        ans += abs(tmp2 - A[j])
        tmp2 = A[j]
        j += 1
    
    count += 1

    flag = not flag
    # print ('A', ans)

# print (i, j)
print (ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
from bisect import bisect_left
read = sys.stdin.read

N, *A = map(int, read().split())

A.sort()

mid = bisect_left(A, (A[0] + A[-1]) // 2)
A = deque(A)
a = deque()

if mid < N / 2:
    side = True
    a.append(A.popleft())
else:
    side = False
    a.append(A.pop())

while A:
    if side:
        a.append(A.pop())
        if A:
            a.appendleft(A.pop())
        side = False
    else:
        a.append(A.popleft())
        if A:
            a.appendleft(A.popleft())
        side = True

if side:
    tmp = a.popleft()
else:
    tmp = a.pop()

if abs(tmp - a[0]) < abs(tmp - a[-1]):
    a.append(tmp)
else:
    a.appendleft(tmp)

answer = 0
a = list(a)
for i, j in zip(a, a[1:]):
    answer += abs(i - j)

print(answer)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
read = sys.stdin.read

N, *A = map(int, read().split())

A.sort()
A = deque(A)
a = deque()
answer = 0
side = True
a.append(A.popleft())

while A:
    if side:
        a.append(A.pop())
        if A:
            a.appendleft(A.pop())
        side = False
    else:
        a.append(A.popleft())
        if A:
            a.appendleft(A.popleft())
        side = True

if side:
    tmp = a.popleft()
else:
    tmp = a.pop()

if abs(tmp - a[0]) < abs(tmp - a[-1]):
    a.append(tmp)
else:
    a.appendleft(tmp)

a = list(a)
for i, j in zip(a, a[1:]):
    answer += abs(i - j)

print(answer)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
read = sys.stdin.read

N, *A = map(int, read().split())

A.sort()
A = deque(A)
a = deque()
answer = 0
side = True
a.append(A.popleft())

while A:
    if side:
        a.append(A.pop())
        if A:
            a.appendleft(A.pop())
        side = False
    else:
        a.append(A.popleft())
        if A:
            a.appendleft(A.popleft())
        side = True

if side:
    tmp = a.popleft()
else:
    tmp = a.pop()

if tmp - a[0] < tmp - a[-1]:
    a.append(tmp)
else:
    a.appendleft(tmp)

a = list(a)
for i, j in zip(a, a[1:]):
    answer += abs(i - j)

print(answer)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
read = sys.stdin.read

N, *A = map(int, read().split())

A.sort()
A = deque(A)
a = deque()
answer = 0
side = True
a.append(A.popleft())

while A:
    if side:
        a.append(A.pop())
        if A:
            a.appendleft(A.pop())
        side = False
    else:
        a.appendleft(A.popleft())
        if A:
            a.append(A.popleft())
        side = True

a = list(a)
for i, j in zip(a, a[1:]):
    answer += abs(i - j)

print(answer)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
read = sys.stdin.read

N, *A = map(int, read().split())

A.sort()
A = deque(A)
a = deque()
answer = 0
side = True
a.append(A.popleft())

while A:
    if side:
        a.append(A.pop())
        if A:
            a.appendleft(A.pop())
        side = False
    else:
        a.append(A.popleft())
        if A:
            a.appendleft(A.popleft())
        side = True

a = list(a)
for i, j in zip(a, a[1:]):
    answer += abs(i - j)

print(answer)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
# from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
import sys
from bisect import *
import itertools
import copy
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7
# input = sys.stdin.readline
def main():
    n = int(input())
    a = [int(input()) for i in range(n)]
    a.sort()

    if n == 2:
        print(a[1] - a[0])
    else:
        ans = a[-1] - a[0] + a[-2] - a[0]
        L,R = a[-1],a[-2]
        l, r = 1, 3
        f = True
        while r - l >= 2:
            if f:
                ans += (L - a[l])
                L = a[l]
                l += 1
                ans += (R - a[l])
                R = a[l]
                l += 1

                f = False
            else:
                ans += (a[r] - L)
                L = a[r]
                r -= 1
                ans += (a[r] - R)
                R = a[r]
                r -= 1
                f = True
        if n % 2 == 0:
            ans += max(abs(R - a[n // 2]), abs(L - a[n // 2]))
        print(ans)
                
                    



    


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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


def main():
    N = int(input())
    A = []
    for _ in range(N):
        A.append(int(input()))
    A.sort()
    import collections
    q = collections.deque(A)

    i = 0
    p = collections.deque()
    p.append(q.pop())
    ans = 0
    while q:
        if len(p) % 2 == 1:
            x = q.popleft()
        else:
            x = q.pop()

        dl, dr = abs(x - p[0]), abs(x - p[-1])
        if dl > dr:
            ans += dl
            p.appendleft(x)
        else:
            ans += dr
            p.append(x)

    print(ans)

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

q = deque(A)
l = q.popleft()
ans = 0
r = l
i = 0
while len(q) > 0:
    if len(q) == 1:
        v = q.pop()
        ans += max(abs(l - v), abs(r - v))
        continue
    if i % 2 == 0:
        v1 = q.pop()
        v2 = q.pop()
        if l < r:
            ans += max(v1, v2) - l
            ans += min(v1, v2) - r
            l = max(v1, v2)
            r = min(v1, v2)
        else:
            ans += max(v1, v2) - r
            ans += min(v1, v2) - l
            r = max(v1, v2)
            l = min(v1, v2)
    else:
        v1 = q.popleft()
        v2 = q.popleft()
        if l < r:
            ans += r - min(v1, v2)
            ans += l - max(v1, v2)
            r = min(v1, v2)
            l = max(v1, v2)
        else:
            ans += l - min(v1, v2)
            ans += r - max(v1, v2)
            l = min(v1, v2)
            r = max(v1, v2)
    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()
deq = deque(a)

ans = 0

left, right = deq.popleft(), 0
cnt = 1


def comparison(left, right, x, y):
    if y == 0:
        if right == 0:
            return abs(left-x)
        else:
            return max(abs(left - x), abs(right - x))

    if right == 0:
        return abs(left*2 - x - y)
    else:
        return abs(left+right-x-y)


while deq:
    if len(deq) == 1:
        x = deq.pop()
        y = 0
    else:
        if cnt:
            x = deq.pop()
            y = deq.pop()
        else:
            x = deq.popleft()
            y = deq.popleft()

    ans += comparison(left, right, x, y)
    left, right = x, y
    cnt = 1-cnt

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

a.sort()
deq = deque(a)

ans = 0

left, right = deq.popleft(), 0
cnt = 1


def comparison(left, right, x, y):
    if y == 0:
        return max(abs(left - x), abs(right - x))

    if right == 0:
        return abs(left*2 - x - y)
    else:
        return abs(left+right-x-y)


while deq:
    if len(deq) == 1:
        x = deq.pop()
        y = 0
    else:
        if cnt:
            x = deq.pop()
            y = deq.pop()
        else:
            x = deq.popleft()
            y = deq.popleft()

    ans += comparison(left, right, x, y)
    left, right = x, y
    cnt = 1-cnt

print(ans)

Submission status: Wrong Answer

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

A.sort()

ret1 = 0
i, j = 1, len(A) - 1
e1, e2 = A[0], A[0]
mode = 0

while i <= j :
    if i == j :
        ret1 += max(abs(e1-A[i]), abs(e2-A[i]))
        i += 1
    else :
        if mode == 0 :
            ret1 += abs(e1-A[j]) + abs(e2-A[j-1])
            e1, e2 = A[j], A[j-1]
            j -= 2
        else :
            ret1 += abs(e1-A[i]) + abs(e2-A[i+1])
            e1, e2 = A[i], A[i+1]
            i += 2
            
        mode ^= 1
        
ret2 = 0
i, j = 1, len(A) - 2
e1, e2 = A[-1], A[-1]
mode = 1

while i <= j :
    if i == j :
        ret2 += max(abs(e1-A[i]), abs(e2-A[i]))
        i += 1
    else :
        if mode == 0 :
            ret2 += abs(e1-A[j]) + abs(e2-A[j-1])
            e1, e2 = A[j], A[j-1]
            j -= 2
        else :
            ret2 += abs(e1-A[i]) + abs(e2-A[i+1])
            e1, e2 = A[i], A[i+1]
            i += 2
            
        mode ^= 1
        
print(max(ret1, ret2))

Submission status: Wrong Answer

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

A.sort()

ret = 0
i, j = 1, len(A) - 1
e1, e2 = A[0], A[0]
mode = 0

while i <= j :
    if i == j :
        ret += max(abs(e1-A[i]), abs(e2-A[i]))
        i += 1
    else :
        if mode == 0 :
            ret += abs(e1-A[j]) + abs(e2-A[j-1])
            e1, e2 = A[j], A[j-1]
            j -= 2
        else :
            ret += abs(e1-A[i]) + abs(e2-A[i+1])
            e1, e2 = A[i], A[i+1]
            i += 2
            
        mode ^= 1
        
print(ret)

Submission status: Wrong Answer

Solution code:
n,*a=map(int,open(0).read().split())
a=sorted(list(a))
from collections import deque
a=deque(a)
i=0
ans=0
cur=a.popleft()
for i in range(1,n):
  if i%2==0:
    w=a.popleft()
    ans+=abs(w-cur)
    cur=w
  else:
    w=a.pop()
    ans+=abs(w-cur)
    cur=w
print(ans)

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    b = IR(n)
    b.sort()
    a = deque(b)
    q = deque([a.pop()])
    n -= 1
    for i in range(n):
        if i&1:
            ai = a.pop()
        else:
            ai = a.popleft()
        x = q[0]
        y = q[-1]
        if abs(x-ai) < abs(y-ai):
            q.append(ai)
        else:
            q.appendleft(ai)
    ans = 0
    for i in range(n):
        ans += abs(q[i+1]-q[i])
    a = deque(b)
    q = deque([a.popleft()])
    for i in range(n):
        if i&1:
            ai = a.popleft()
        else:
            ai = a.pop()
        x = q[0]
        y = q[-1]
        if abs(x-ai) < abs(y-ai):
            q.append(ai)
        else:
            q.appendleft(ai)
    ans2 = 0
    for i in range(n):
        ans2 += abs(q[i+1]-q[i])
    print(max(ans,ans2))
    return


#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    b = IR(n)
    b.sort()
    a = deque(b)
    q = deque([a.pop()])
    n -= 1
    for i in range(n):
        if i&2:
            ai = a.pop()
        else:
            ai = a.popleft()
        x = q[0]
        y = q[-1]
        if abs(x-ai) < abs(y-ai):
            q.append(ai)
        else:
            q.appendleft(ai)
    ans = 0
    for i in range(n):
        ans += abs(q[i+1]-q[i])
    a = deque(b)
    q = deque([a.popleft()])
    for i in range(n):
        if i&2:
            ai = a.popleft()
        else:
            ai = a.pop()
        x = q[0]
        y = q[-1]
        if abs(x-ai) < abs(y-ai):
            q.append(ai)
        else:
            q.appendleft(ai)
    ans2 = 0
    for i in range(n):
        ans2 += abs(q[i+1]-q[i])
    print(max(ans,ans2))
    return


#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    a = IR(n)
    a.sort()
    a = deque(a)
    q = deque([a.pop()])
    n -= 1
    for i in range(n):
        if i&2:
            ai = a.pop()
        else:
            ai = a.popleft()
        x = q[0]
        y = q[-1]
        if abs(x-ai) < abs(y-ai):
            q.append(ai)
        else:
            q.appendleft(ai)
    ans = 0
    for i in range(n):
        ans += abs(q[i+1]-q[i])
    print(ans)
    return


#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    a = IR(n)
    a.sort()
    q = deque([a[-1]])
    a = a[:-1]
    n -= 1
    for i in range(n):
        if i&2:
            ai = a.pop()
        else:
            ai = a.pop(0)
        x = q[0]
        y = q[-1]
        if abs(x-ai) < abs(y-ai):
            q.append(ai)
        else:
            q.appendleft(ai)
    q = list(q)
    ans = 0
    for i in range(n):
        ans += abs(q[i+1]-q[i])
    print(ans)
    return


#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys, math, itertools, heapq, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
sys.setrecursionlimit(10**8)
inf = float('inf')
ans = 0
count = 0
n=int(input())
A=[int(input()) for i in range(n)]
A.sort()
if n%2==1:
    if A[n//2]-A[n//2-1]<=A[n//2+1]-A[n//2]:
        A.insert(0,A[n//2+1])
    else:
        A.append(A[n//2])
    n+=1
tmp=A[n//2]
for i in range(n//2):
    ans+=abs(tmp-A[i+n//2])
    tmp = A[i+n//2]
    print(tmp)
    ans+=abs(tmp-A[i])
    tmp=A[i]
    print(tmp)
print(ans)

# print(A)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[int(input()) for i in range(N)]
from collections import deque
A.sort()
q=deque(A)
q2=deque()
q2.append(q.popleft())
k=N-1
while(k>0):
  if len(q)>0:
    q2.appendleft(q.pop())
    k-=1
  if len(q)>0:
    q2.append(q.pop())
    k-=1
  if len(q)>0:
    q2.appendleft(q.popleft())
    k-=1
  if len(q)>0:
    q2.append(q.popleft())
    k-=1
  if len(q)==0:
    break
prev=q2.popleft()
ans=0
for i in range(N-1):
  now=q2.popleft()
  ans+=abs(now-prev)
  prev=now

q=deque(A)
q2=deque()
q2.append(q.popleft())
k=N-1
while(k>0):
  if len(q)>0:
    q2.appendleft(q.pop())
    k-=1
  if len(q)>0:
    q2.append(q.pop())
    k-=1
  if len(q)>0:
    q2.append(q.popleft())
    k-=1
  if len(q)>0:
    q2.appendleft(q.popleft())
    k-=1
  if len(q)==0:
    break
prev=q2.popleft()
tmp=0
for i in range(N-1):
  now=q2.popleft()
  tmp+=abs(now-prev)
  prev=now  
  
print(max(ans,tmp))

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[int(input()) for i in range(N)]
from collections import deque
A.sort()
q=deque(A)
q2=deque()
q2.append(q.popleft())
k=N-1
while(k>0):
  if len(q)>0:
    q2.appendleft(q.pop())
    k-=1
  if len(q)>0:
    q2.append(q.pop())
    k-=1
  if len(q)>0:
    q2.appendleft(q.popleft())
    k-=1
  if len(q)>0:
    q2.append(q.popleft())
    k-=1
  if len(q)==0:
    break
prev=q2.popleft()
ans=0
for i in range(N-1):
  now=q2.popleft()
  ans+=abs(now-prev)
  prev=now
  
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=sorted([int(input()) for i in range(N)])
#A=[i for i in range(50)]
#N=len(A)
value=0
B,C=A[0],A[0]
i=1
z=0

while N-1>z:

    if z%4==0:
        value+=A[-i]-B
        B=A[-i]
    if z%4==1:
        value+=A[-i-1]-C
        C=A[-i-1]
    if z%4==2:
        value+=B-A[i]
        B=A[i]
    if z%4==3:
        value+=C-A[i+1]
        C=A[i+1]
        i+=2
    #print(B,C,i,z)
    z+=1
value1=value
value=0
B,C=A[-1],A[-1]
i=0
z=0
while N-1>z:

    if z%4==0:
        value+=B-A[i]
        B=A[i]

    if z%4==1:
        value+=C-A[i+1]
        C=A[i+1]

    if z%4==2:
        value+=A[-i-2]-B
        B=A[-i-2]
    if z%4==3:
        value+=A[-i-3]-C
        C=A[-i-3]
        i+=2
    print(B,C,i,z,value)
    z+=1
value2=value
print(max(value1,value2))

Submission status: Wrong Answer

Solution code:
N=int(input())
A=sorted([int(input()) for i in range(N)])
#A=[i for i in range(50)]
#N=len(A)
value=0
B,C=A[0],A[0]
i=1
z=0

while N-1>z:

    if z%4==0:
        value+=A[-i]-B
        B=A[-i]
    if z%4==1:
        value+=A[-i-1]-C
        C=A[-i-1]
    if z%4==2:
        value+=B-A[i]
        B=A[i]
    if z%4==3:
        value+=C-A[i+1]
        C=A[i+1]
        i+=2
    #print(B,C,i,z)
    z+=1
value1=value
value=0
B,C=A[-1],A[-1]
i=0
z=0
while N-1>z:

    if z%4==0:
        value+=B-A[i]
        B=A[i]

    if z%4==1:
        value+=C-A[i+1]
        C=A[i+1]

    if z%4==2:
        value+=A[-i-2]-B
        B=A[-i-1]
    if z%4==3:
        value+=A[-i-3]-C
        C=A[-i-2]
        i+=2
    #print(B,C,i,z,value)
    z+=1
value2=value
print(max(value1,value2))

Submission status: Wrong Answer

Solution code:
N=int(input())
A=sorted([int(input()) for i in range(N)])
#A=[i for i in range(50)]
#N=len(A)
value=0
B,C=A[0],A[0]
i=1
z=0

while N-1>z:

    if z%4==0:
        value+=A[-i]-B
        B=A[-i]
    if z%4==1:
        value+=A[-i-1]-C
        C=A[-i-1]
    if z%4==2:
        value+=B-A[i]
        B=A[i]
    if z%4==3:
        value+=C-A[i+1]
        C=A[i+1]
        i+=2
    #print(B,C,i,z)
    z+=1
value1=value
value=0
B,C=A[-1],A[-1]
i=0
z=0
while N-1>z:

    if z%4==0:
        value+=B-A[i]
        B=A[i]

    if z%4==1:
        value+=C-A[i+1]
        C=A[i+1]

    if z%4==2:
        value+=A[-i]-B
        B=A[-i]
    if z%4==3:
        value+=A[-i-1]-C
        C=A[-i-1]
        i+=2
    #print(B,C,i,z)
    z+=1
value2=value
print(max(value1,value2))

Submission status: Wrong Answer

Solution code:
N=int(input())
A=sorted([int(input()) for i in range(N)])
value=0
B,C=A[0],A[0]
i=1
z=0

while N-1>z:

    if z%4==0:
        value+=A[-i]-B
        B=A[-i]
    if z%4==1:
        value+=A[-i-1]-C
        C=A[-i-1]
    if z%4==2:
        value+=B-A[i]
        B=A[i]
    if z%4==3:
        value+=C-A[i+1]
        C=A[i+1]
        i+=2
    z+=1

print(value)

Submission status: Wrong Answer

Solution code:
n = int(input())
li_a = list()
for i in range(n):
    li_a.append(int(input()))
li_w = sorted(li_a)

li_ans = list()
right = left = li_w.pop(0)

answer = 0
while len(li_w):
    if len(li_w):
        a = li_w.pop(-1)
        answer += abs(right - a)
        right = a
    else:
        break
    if len(li_w):
        b = li_w.pop(-1)
        answer += abs(left - b)
        left = b
    else:
        break
    if len(li_w):
        c = li_w.pop(0)
        answer += abs(right - c)
        right = c
    else:
        break
    if len(li_w):
        d = li_w.pop(0)
        answer += abs(left - d)
        left = d
    else:
        break

print(answer)

Submission status: Wrong Answer

Solution code:
n = int(input())
li_a = list()
for i in range(n):
    li_a.append(int(input()))
li_w = sorted(li_a)

li_ans = list()
li_ans.append(li_w.pop(0))

answer = 0
while True:
    if len(li_w):
        li_ans.insert(0, li_w.pop(-1))
        answer += abs(li_ans[0] - li_ans[1])
    else:
        break
    if len(li_w):
        li_ans.append(li_w.pop(-1))
        answer += abs(li_ans[-2] - li_ans[-1])
    else:
        break
    if len(li_w):
        li_ans.insert(0, li_w.pop(0))
        answer += abs(li_ans[0] - li_ans[1])
    else:
        break
    if len(li_w):
        li_ans.append(li_w.pop(0))
        answer += abs(li_ans[-2] - li_ans[-1])
    else:
        break

print(answer)

Submission status: Wrong Answer

Solution code:
n = int(input())
li_a = list()
for i in range(n):
    li_a.append(int(input()))
li_w = sorted(li_a)

li_ans = list()
li_ans.append(li_w.pop(0))

while True:
    if len(li_w):
        li_ans.insert(0, li_w.pop(-1))
    else:
        break
    if len(li_w):
        li_ans.append(li_w.pop(-1))
    else:
        break
    if len(li_w):
        li_ans.insert(0, li_w.pop(0))
    else:
        break
    if len(li_w):
        li_ans.append(li_w.pop(0))
    else:
        break

answer = 0
for y in range(n - 1):
    answer += abs(li_ans[y] - li_ans[y + 1])

print(answer)

Submission status: Wrong Answer

Solution code:
import numpy as np
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
a = sorted(a)

# even
if n % 2 == 0:
    flag = True   
    b = ''
    for i in range(n//2):
        if flag:
            b = str(a[i]) + b + str(a[-(i+1)])
            flag = False
        else:
            b = str(a[-(i+1)]) + b + str(a[i])
            flag = True

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

# odd
else:
    mi = a.pop(0)
    ans = 0
    ans += (a[-1] - mi) + (a[-2] - mi)
    ans += sum(abs(np.array(a[:(n-1)//2]) - np.array(a[(n-1)//2:])))

    print(ans)

Submission status: Wrong Answer

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

from collections import deque
l=deque(a)
ansl=deque()

if n%2==0:
    i=1
    while l:
        m=l.popleft()
        M=l.pop()
        if i%2:
            ansl.appendleft(m)
            ansl.append(M)
        else:
            ansl.append(m)
            ansl.appendleft(M)
        i+=1
else:
    ansl.append(l.popleft())
    i=1
    while l:
        if i%2:
            M=l.pop()
            m=l.pop()
            ansl.append(M)
            ansl.appendleft(m)
        else:
            M=l.popleft()
            m=l.popleft()
            ansl.append(M)
            ansl.appendleft(m)
        i+=1

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

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
n=int(input())
a=[int(input()) for _ in range(n)]
a.sort()
if n==3:
    print(abs(a[0]-a[1])+abs(a[0]-a[2]))
    exit()
from collections import deque
l=deque(a)
ansl=deque()

if n%2==0:
    i=1
    while l:
        m=l.popleft()
        M=l.pop()
        if i%2:
            ansl.appendleft(m)
            ansl.append(M)
        else:
            ansl.append(m)
            ansl.appendleft(M)
        i+=1
else:
    ansl.append(l.popleft())
    i=1
    while l:
        m=l.popleft()
        M=l.pop()
        if i%2:
            ansl.append(M)
            ansl.append(m)
        else:
            ansl.appendleft(M)
            ansl.appendleft(m)
        i+=1

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
num = 1
while num * (num + 1) <= 2 * n:
  num += 1
now = n
for i in range(num,0,-1):
  if now >= i:
    print(i)
    now -= i

Submission status: Wrong Answer

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

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

A = sorted(A)

B = [0] * N
m = int(N/2)
B[m] = A[0]

si = 1
bi = 0
ci = 1

for i in range(1, N):
    si *= -1
    ii = int(i/2 + 0.5)

    if i % 4 == 1 or i % 4 == 2:
        B[m+si*ii] = A[N-bi-1]
        bi += 1
    if i % 4 == 3 or i % 4 == 0:
        B[m+si*ii] = A[ci]
        ci += 1

C = [0] * N
m = int(N/2)
B[m] = A[0]

si = 1
bi = 0
ci = 1

for i in range(1, N):
    si *= -1
    ii = int(i/2 + 0.5)

    if i % 4 == 1 or i % 4 == 2:
        C[m+si*ii] = A[ci]
        ci += 1
    if i % 4 == 3 or i % 4 == 0:
        C[m+si*ii] = A[N-bi-1]
        bi += 1

cntB = 0
cntC = 0
for i in range(N-1):
    cntB += abs(B[i] - B[i+1])
    cntC += abs(C[i] - C[i+1])


print(max(cntB, cntC))

Submission status: Wrong Answer

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

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

A = sorted(A)

B = [0] * N
m = int(N/2)
B[m] = A[0]

si = 1
bi = 0
ci = 1

for i in range(1, N):
    si *= -1
    ii = int(i/2 + 0.5)

    if i % 4 == 1 or i % 4 == 2:
        B[m+si*ii] = A[N-bi-1]
        bi += 1
    if i % 4 == 3 or i % 4 == 0:
        B[m+si*ii] = A[ci]
        ci += 1

cnt = 0

for i in range(N-1):
    cnt += abs(B[i] - B[i+1])

print(cnt)

Submission status: Wrong Answer

Solution code:
N=int(input())

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

A.sort()

ansl=A[0]
ansr=A[-1]
l=1
r=-2
n=[0]*4
ansn=abs(A[0]-A[-1])
for i in range(N-2):
    n[0]=abs(A[l]-ansl)
    n[1]=abs(A[l]-ansr)
    n[2]=abs(A[r]-ansl)
    n[3]=abs(A[r]-ansr)
    if n[0]==max(n):
        ansn+=n[0]
        ansl=A[l]
        l+=1
    elif n[1]==max(n):
        ansn+=n[1]
        ansl=A[l]
        l+=1
    elif n[2]==max(n):
        ansn+=n[2]
        ansr=A[r]
        r-=1
    elif n[3]==max(n):
        ansn+=n[3]
        ansr=A[r]
        r-=1

print(ansn)