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 neighbor_sum(N, A):
    A_1 = [] 
    A_2 = []
    i = 0
    j = N-1
    change = True
    while i < j:
        if change == True:
            A_1.append(A[i])
            A_2.append(A[j])
            i += 1
            j -= 1
            change = False
        else:
            A_1.append(A[j])
            A_2.append(A[i])
            i += 1
            j -= 1
            change = True
        # N == odd 
    if N % 2 == 1:
        if (A[N//2] - A[0]) < (A[N-1] - A[N//2]):
            A_1.append(A[N//2])
        else:
            A_2.append(A[N//2])

    return A_1[::-1] + A_2[:]

if __name__ == "__main__":
    N = int(input())
    A = [int(input()) for _ in range(N)]
    A.sort()
    diff = neighbor_sum(N, A)
    diff_sum = [abs(diff[i+1] - diff[i]) for i in range(N-1)]
    print(sum(diff_sum))

Submission status: Wrong Answer

Solution code:
from collections import deque
n = int(input())
a = [int(input()) for _ in range(n)]
a.sort()
que = deque(a)
right = que.pop()
left = que.popleft()
sums = right - left
while(bool(que)):
  a = que.pop()
  if abs(a-right) >= abs(a-left):
    sums += abs(a-right)
    right = a
  else:
    sums += abs(a-left)
    left = a
  if not bool(que):
    break
  b = que.popleft()
  if abs(b-right) > abs(b-left):
    sums += abs(b-right)
    right = b
  else:
    sums += abs(b-left)
    left = b
  if not bool(que):
    break
print(sums)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = []
B = []
for i in range(N):
    x = int(input())
    A.append(x)
A.sort()

flag = 0
while len(A) > 0:
    if flag%4 == 0:
        B.insert(0, A[0])
        A.pop(0)
    elif flag%4 == 1:
        B.append(A[-1])
        A.pop()
    elif flag%4 == 2:
        B.insert(0, A[-1])
        A.pop()
    else:
        B.append(A[0])
        A.pop(0)
    flag += 1
ans = 0
for i in range(len(B)-1):
    if B[i] < B[i + 1]:
        ans += B[i + 1] - B[i]
    else:
        ans += B[i] - B[i + 1]

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

a = [0] * n

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

a.sort()

b = [a.pop(0)]

c = 0

for i in range(0, n - 1):
  if i % 2 == 0:
    if c < 2:
      b.append(a.pop(-1))
    else:
      b.append(a.pop(0))
  else:
    if c < 2:
      b.insert(0, a.pop(-1))
    else:
      b.insert(0, a.pop(0))
  if c < 3:
    c += 1
  else:
    c = 0

diff = 0

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

print(diff)

Submission status: Wrong Answer

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

    num_l = list()
    for _ in range(n):
        num_l += [int(input())]

    num_l = sorted(num_l)

    row = list()

    f = num_l.pop(0)
    s = f
    max_num = 0
    flag = True
    while(num_l):
        if len(num_l) == 1:
            if abs(f - num_l[0]) > abs(s - num_l[0]):
                max_num += abs(f - num_l[0])
            else:
                max_num += abs(s - num_l[0])
            break

        if flag:
            tmp_f = num_l.pop(len(num_l)-1)
            tmp_s = num_l.pop(len(num_l)-1)
        else:
            tmp_f = num_l.pop(0)
            tmp_s = num_l.pop(0)

        max_num += abs(f - tmp_f) + abs(s - tmp_s)

        f = tmp_f
        s = tmp_s

        flag = not flag

    print(max_num)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
l = [int(input()) for _ in range(n)]
l.sort()
s = 0
if n%2 == 0:
    m = int(n/2)
    l_min = l[:m]
    l_max = l[m:]
    l_max.reverse()
    for i in range(m-1):
        s += l_max[i]-l_min[i]
        s += l_max[i+1] - l_min[i]
    s += l_max[0] - l_min[-1]
    
else:
    m = int(n/2)
    l_min = l[:m]
    l_max = l[m+1:]
    l_max.reverse()
    for i in range(m-1):
        s += l_max[i]-l_min[i]
        s += l_max[i+1] - l_min[i]
    s += l_max[0] - l_min[-1]
    s += l_max[-1] - l[m]
    
print(s)

Submission status: Wrong Answer

Solution code:
import copy
N = int(input().strip('\n'))
lis = []
for i in range(N):
    lis.append(int(input().strip('\n')))
res = []

def calc(lis):
    sum = 0
    for i in range(len(lis)-1):
        sum += abs(lis[i+1]-lis[i])
    return sum

res_point = 0
for i in range(N):
    point_i = 0
    try_res = []
    for j in range(i+1):
        tly = copy.deepcopy(res)
        tly.insert(j,lis[i])
        if calc(tly)>=point_i:
            point_i = calc(tly)
            try_res = copy.deepcopy(tly)
    res = copy.deepcopy(try_res)
    res_point = point_i
print(point_i)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

a.sort()

ans = 0

if n % 2 == 1:
    b = a[0]*2
    a.pop(0)
    c = 0
    for i in range(n//2):
        if i % 2 == 0:
            c += a.pop(-1)
            c += a.pop(-1)
            ans += abs(b-c)
            b = c
            c = 0
        else:
            c += a.pop(0)
            c += a.pop(0)
            ans += abs(b-c)
            b = c
elif n == 2:
    ans = abs(a[0]-a[1])
else:
    b = a[0]*2
    a.pop(0)
    c = 0
    d = 0
    e = 0
    for i in range(n//2-1):
        if i == n//2 -2:
            d = a[0]
            e = a[0]
        if i % 2 == 0:
            c += a.pop(-1)
            c += a.pop(-1)
            ans += abs(b-c)
            b = c
            c = 0
        else:
            c += a.pop(0)
            c += a.pop(0)
            ans += abs(b-c)
            b = c
    ans += max(abs(a[0]-d),abs(a[0]-e))

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[]
for i in range(n):
    a.append(int(input()))
a.sort(reverse=True)
ans=0
ans1=0
ans2=0
if n%2==0:
    for i in range(n//2):
        if i==n//2-1:
            ans+=a[i]
        else:
            ans+=2*a[i]
    for i in range(n-1,n//2-1,-1):
        if i==n//2:
            ans-=a[i]
        else:
            ans-=2*a[i]
else:
    for i in range((n+1)//2):
        if (i==(n-3)//2)|(i==(n-1)//2):
            ans1+=a[i]
        else:
            ans1+=2*a[i]
    for i in range((n+1)//2,n):
        ans1-=2*a[i]
    for i in range((n-1)//2):
        ans2+= 2 * a[i]
    for i in range((n-1)//2,n):
        if (i == (n-1)//2) | (i ==(n+1)//2):
            ans2-=a[i]
        else:
            ans2-=2 * a[i]
            ans=max(ans1,ans2)

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
n = int(input())
A = deque(sorted(int(input()) for _ in range(n)))
ans = deque([A.popleft()])
def f(x):
    cnt = 0
    for i in range(len(x) - 1):
        cnt += abs(x[i] - x[i + 1])
    return cnt
for i in range((n - 1) // 2):
    if i & 1:
        ans.appendleft(A.popleft())
        ans.append(A.popleft())
    else:
        ans.appendleft(A.pop())
        ans.append(A.pop())
if A:
    ans.appendleft(A[0])
print(f(ans))

Submission status: Wrong Answer

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

ans = 0

p = 0
q = n-1

if(n == 2):
  ans = a[q] - a[p]
else:
  ans += a[q] - a[p]
  for i in range(n-3):
    if(i % 2 == 0):
      ans += a[q] - a[p+1]
    else:
      ans += a[q-1] - a[p]
      p += 1
      q -= 1
  s = a[q] - a[p+1]
  t = a[q-1] - a[p]
  if(s >= t):
    ans += s
  else:
    ans += t

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

A.sort()
array = deque([A[0]])
a_len = 1
count = 0
idx = 0
result = 0

while a_len < N:
    if count % 2 == 0:
        array.appendleft(A[-idx-1])
        array.append(A[-idx-2])
    else:
        array.appendleft(A[idx+1])
        array.append(A[idx+2])
        idx += 2
    count += 1
    a_len += 2

for i in range(N-1):
    result += abs(array[i] - array[i+1])

print(result)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[]
for _ in range(n):
    a.append(int(input()))
a.sort()
if n%2==1:
    idx=[n//2]
else:
    idx=[n//2-1, n//2]
ans=0

for i in idx:
    left=0
    right=n-1
    tans=0
    while left!=right:
        if abs(a[left]-a[i]) > abs(a[right]-a[i]):
            nxt_i=left
            left+=1
        else:
            nxt_i=right
            right-=1
        tans+=abs(a[i]-a[nxt_i])
        i=nxt_i
    ans=max(ans, tans)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

a.sort()

ans = 0

if n % 2 == 1:
    b = a[0]*2
    a.pop(0)
    c = 0
    for i in range(n//2):
        if i % 2 == 0:
            c += a.pop(-1)
            c += a.pop(-1)
            ans += abs(b-c)
            b = c
            c = 0
        else:
            c += a.pop(0)
            c += a.pop(0)
            ans += abs(b-c)
            b = c
elif n == 2:
    ans = abs(a[0]-a[1])
else:
    b = a[0]*2
    a.pop(0)
    c = 0
    d = 0
    e = 0
    for i in range(n//2-1):
        if i == n//2 -2:
            d = a[0]
            e = a[0]
        if i % 2 == 0:
            c += a.pop(-1)
            c += a.pop(-1)
            ans += abs(b-c)
            b = c
            c = 0
        else:
            c += a.pop(0)
            c += a.pop(0)
            ans += abs(b-c)
            b = c
    ans += max(a[0]-d,a[0]-e)

print(ans)

Submission status: Wrong Answer

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

A_sorted = sorted(A)

if n % 2 == 1:
	if A_sorted[(n-1)//2] != A_sorted[(n+1)//2]:
		n_small = (n+1)//2
		n_big = n//2

		A_small = A_sorted[:n_small]
		A_big = A_sorted[n_small:]

		bigsum = sum(A_big)

		smallsum = sum(A_small[:-2])
		smallmax = A_small[-1] + A_small[-2]

		ans = bigsum * 2 - ( smallsum * 2 + smallmax)
	else:
		n_small = n//2
		n_big = (n+1)//2

		A_small = A_sorted[:n_small]
		A_big = A_sorted[n_small:]

		smallsum = sum(A_small)

		bigsum = sum(A_big[:-2])
		bigmin = A_big[-1] + A_big[-2]

		ans = bigsum * 2 + bigmin - smallsum * 2


else:
	n_small = n//2
	n_big = n//2

	A_small = A_sorted[:n_small]
	A_big = A_sorted[n_small:]

	bigsum = sum(A_big[1:])
	bigmin = A_big[0]

	smallsum = sum(A_small[:-1])
	smallmax = A_small[-1]

	ans = bigsum * 2 + bigmin - ( smallsum * 2 + smallmax)

print(ans)

Submission status: Wrong Answer

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

A = sorted(A)
res = 0
if len(A) % 2 == 1:
    if len(A) > 3:
        mid = int(len(A)/2)
        now_num = A.pop(mid)
        for i in range(len(A)):
            if len(A) == 0:
                break
            if i % 2 == 0:
                res += A[-1] - now_num
                now_num = A.pop(-1)
            else:
                res += now_num - A[0]
                now_num = A.pop(0)
        print(res)
    else:
        res += A[-1] - A[0]
        res += A[-2] - A[0]
        print(res)
else:
    if len(A) == 2:
        print(A[-1] - A[0])
        exit()
    mid = int(len(A)/2) - 1
    now_num = A.pop(mid)
    for i in range(len(A)):
        if len(A) == 0:
            break
        if i % 2 == 0:
            res += A[-1] - now_num
            now_num = A.pop(-1)
        else:
            res += now_num - A[0]
            now_num = A.pop(0)
    print(res)

Submission status: Wrong Answer

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

a.sort()
l = [a.pop(0)]
while (a != []):
    if (a == []):
        break
    l.append(a.pop(-1))
    if (a == []):
        break
    l = [a.pop(-1)] + l
    if (a == []):
        break
    l.append(a.pop(0))
    if (a == []):
        break
    l = [a.pop(0)] + l

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

print(ans)

Submission status: Wrong Answer

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

A.sort()

x=A.pop(0)
B=[x]

while True:
	x=A.pop(-1)
	B.insert(0,x)
	if not A: break
	x=A.pop(-1)
	B.append(x)
	if not A: break
	x=A.pop(0)
	B.insert(0,x)
	if not A: break
	x=A.pop(0)
	B.append(x)
	if not A: break

c=0

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

print(c)

Submission status: Wrong Answer

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

A = sorted(A)
res = 0
if len(A) % 2 == 1:
    if len(A) > 3:
        mid = int(len(A)/2)
        now_num = A.pop(mid)
        for i in range(len(A)):
            if len(A) == 0:
                break
            if i % 2 == 0:
                res += A[-1] - now_num
                now_num = A.pop(-1)
            else:
                res += now_num - A[0]
                now_num = A.pop(0)
        print(res)
    else:
        res += A[-1] - A[0]
        res += A[-2] - A[0]
        print(res)
else:
    mid = int(len(A)/2) - 1
    now_num = A.pop(mid)
    for i in range(len(A)):
        if len(A) == 0:
            break
        if i % 2 == 0:
            res += A[-1] - now_num
            now_num = A.pop(-1)
        else:
            res += now_num - A[0]
            now_num = A.pop(0)
    print(res)

Submission status: Wrong Answer

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

ans=0
if n%2==1:
  aList.sort()
  smallList=aList[:(n+1)//2]
  bigList=aList[(n+1)//2:]
  ans=sum(bigList)*2-sum(smallList)*2+smallList[-1]+smallList[-2]
else:
  aList.sort()
  smallList=aList[:n//2]
  bigList=aList[n//2:]
  ans=sum(bigList)*2-sum(smallList)*2+smallList[-1]-bigList[0]
print(ans)

Submission status: Wrong Answer

Solution code:
def cul(low, high):
    low.insert(0, lower[-1])
    low = low[:-1]
    high = high[::-1]
    lists = []
    if len(low) > len(high):
        lists.append(low[0])
        for _l, _h in zip(low[1:], high):
            lists.append(_h)
            lists.append(_l)
    elif len(low) < len(high):
        lists.append(high[0])
        for _l, _h in zip(low, high[1:]):
            lists.append(_l)
            lists.append(_h)
    else:
        for _l, _h in zip(low, high):
            lists.append(_l)
            lists.append(_h)
    ans = 0
    for _p, _n in zip(lists, lists[1:]):
        ans += abs(_n - _p)
    return ans


n = int(input())
a = sorted([int(input()) for _ in range(n)])
if len(a) % 2 == 1:
    lower = a[:len(a) // 2 + 1]
    higher = a[len(a) // 2 + 1:]
    first = cul(lower, higher)
    lower = a[:len(a) // 2]
    higher = a[len(a) // 2:]
    second = cul(lower, higher)
    print(max(first, second))
else:
    lower = a[:len(a) // 2]
    higher = a[len(a) // 2:]
    print(cul(lower, higher))

Submission status: Wrong Answer

Solution code:
def t118C():
    N = int(input())
    A = [int(input()) for i in range(N)]
    A.sort()
    anslist = []
    ans = 0
    while True:
        anslist = [A[0]] + anslist
        del A[0]
        if len(A) <= 0:
            break
        anslist = anslist + [A[-1]]
        del A[-1]
        if len(A) <= 0:
            break
        anslist = [A[-1]] + anslist
        del A[-1]
        if len(A) <= 0:
            break
        anslist = anslist + [A[0]]
        del A[0]
        if len(A) <= 0:
            break

    #print(anslist)
    for i in range(1, len(anslist)):
        ans += abs(anslist[i] - anslist[i - 1])
    #print(anslist)
    print(ans)
t118C()

Submission status: Wrong Answer

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


ans = 0
a.sort()
l = a[0]
r = a[0]
a.pop(0)
for i in range(0, n-1):
    if (i % 4 == 0):
        ans += a[-1] - l
        l = a[-1]
        a.pop(-1)
    if (i % 4 == 1):
        ans += a[-1] - r
        r = a[-1]
        a.pop(-1)
    if (i % 4 == 2):
        ans += l - a[0]
        l = a[0]
        a.pop(0)
    if (i % 4 == 3):
        ans += r-a[0]
        r = a[0]
        a.pop(0)
print(ans)

Submission status: Wrong Answer

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

A = sorted(A)
print(A)
res = 0
if len(A) % 2 == 1:
    if len(A) > 3:
        mid = int(len(A)/2)
        now_num = A.pop(mid)
        for i in range(len(A)):
            if len(A) == 0:
                break
            if i % 2 == 0:
                res += A[-1] - now_num
                now_num = A.pop(-1)
            else:
                res += now_num - A[0]
                now_num = A.pop(0)
        print(res)
    else:
        res += A[-1] - A[0]
        res += A[-2] - A[0]
        print(res)
else:
    mid = int(len(A)/2) - 1
    now_num = A.pop(mid)
    for i in range(len(A)):
        if len(A) == 0:
            break
        if i % 2 == 0:
            res += A[-1] - now_num
            now_num = A.pop(-1)
        else:
            res += now_num - A[0]
            now_num = A.pop(0)
    print(res)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
a = []
for _ in range(n):
    a.append(int(input()))

a.sort()
# print(a)
b = [0 for _ in range(n)]
b[n//2] = a[0]
left = 1
right = n-1
for i in range(n//2):
    if i%2==0:
        b[n//2-(i+1)] = a[right]
        right -= 1
        if n//2+(i+1)<n:
            b[n//2+(i+1)] = a[right]
            right -= 1
    else:
        b[n//2-(i+1)] = a[left]
        left += 1
        if n//2+(i+1)<n:
            b[n//2+(i+1)] = a[left]
            left += 1
ans = 0
for i in range(n-1):
    ans += abs(b[i]-b[i+1])
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
num=[]
for i in range(N):
    num.append(int(input()))
num.sort()

s=[]
k=len(num)
while k>0:
    s=s+[num.pop(0)]
    if k==1:break
    s=[num.pop(-1)]+s
    if k==2:break
    s=s+[num.pop(-1)]
    if k==3:break
    s=[num.pop(0)]+s
    k-=4

S=0
for i in range(len(s)-1):
    S+=abs(s[i]-s[i+1])
print(S)

Submission status: Wrong Answer

Solution code:
from collections import deque
n = int(input())
a = [int(input()) for _ in range(n)]
a.sort()
que = deque(a)
right = que.pop()
left = que.popleft()
sums = right - left
while(len(que)>0):
  a = que.pop()
  if abs(a-right) > abs(a-left):
    sums += abs(a-right)
    right = a
  else:
    sums += abs(a-left)
    left = a
  if not bool(que):
    break
  b = que.popleft()
  if abs(b-right) > abs(b-left):
    sums += abs(b-right)
    right = b
  else:
    sums += abs(b-left)
    left = b
print(sums)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
tmp = 0
for j in range(len(a)-2):
    sub = abs(a[j] - a[j+1])
    if sub > tmp:
        tmp = sub
print(tmp)

Submission status: Wrong Answer

Solution code:
N=int(input())
num=[]
for i in range(N):
    num.append(int(input()))
num.sort()

s=[]
while len(num)>0:
    s=s+[num.pop(0)]
    if len(num)==0:break
    s=[num.pop(-1)]+s
    if len(num)==0:break
    s=s+[num.pop(-1)]
    if len(num)==0:break
    s=[num.pop(0)]+s

S=0
for i in range(len(s)-1):
    S+=abs(s[i]-s[i+1])
print(S)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

a.sort()

b = a[:]

array1 = [a.pop(0)]

def total(list):
    ans = 0
    for i in range(len(list)-1):
        ans += abs(list[i] - list[i+1])
    return ans
loop = 0
while a:
    if loop % 4 in [0,1]:
        v = a.pop()
    else:
        v = a.pop(0)
    


    if loop % 4 in [0,2]:
        array1.insert(0,v)
    else:
        array1.append(v)

    loop += 1


print(total(array1))

Submission status: Wrong Answer

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

a = sorted(a)
que = deque([a[0]])
for i in range(1, n):
    if i%4 == 1:
        que.appendleft(a[-(2*(i//4)+1)])
    elif i%4 == 2:
        que.append(a[-(2*(i//4)+2)])
    elif i%4 == 3:
        que.appendleft(a[2*(i//4)+1])
    else:
        que.append(a[i//2])

# for i in range(1, n):
#     if i%4 == 1:
#         ans = [a[-(2*(i//4)+1)]] + ans
#     elif i%4 == 2:
#         ans = ans + [a[-(2*(i//4)+2)]]
#     elif i%4 == 3:
#         ans = [a[2*(i//4)+1]] + ans
#     else:
#         ans = ans + [a[i//2]]

diff_sum = 0
n = que.popleft()
while len(que) > 0:
    m = que.popleft()
    diff_sum += abs(n - m)
    n = m
print(diff_sum)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = []
for _ in range(n):
  a.append(int(input()))
a.sort()
i = 1
j = n-1
s = 0
x = y = min(a)
r = True
while i<j:
  if r:
    s += abs(a[j] - x) + abs(a[j-1] - y)
    x = a[j]
    y = a[j-1]
    j -= 2
  else:
    s += abs(a[i] - x) + abs(a[i+1] - y)
    x = a[i]
    y = a[i+1]
    i += 2
  r = not r
if i == j:
  s += max(abs(a[i] - x), abs(a[i]-y))
print(s)

Submission status: Wrong Answer

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

A = sorted(A)

if N % 2 == 1:
    mid = A[N // 2]
    A = sorted([abs(a-mid) for a in A])
    print(sum(A[:2]) + 2 * sum(A[2:]))

else:
    mid = A[N // 2]
    A = sorted([abs(a-mid) for a in A])
    print(sum(A[:2]) + 2 * sum(A[2:]))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = sorted([int(input()) for _ in range(n)])
if len(a) % 2 == 1:
    lower = a[:len(a) // 2 + 1]
    higher = a[len(a) // 2 + 1:]
else:
    lower = a[:len(a) // 2]
    higher = a[len(a) // 2:]
lower.insert(0, lower[-1])
lower = lower[:-1]
higher = higher[::-1]
lists = []
for _l, _h in zip(lower, higher):
    lists.append(_l)
    lists.append(_h)
if len(lower) != len(higher):
    lists.append(lower[-1])
ans = 0
for _p, _n in zip(lists, lists[1:]):
    ans += abs(_n-_p)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = sorted([int(input()) for _ in range(n)])
m = a.pop(0)
b = [m]
ans = 0
for i in range(n-1):
    k = i % 4
    if k == 0:
        t = a.pop()
        b.insert(0, t)
        ans += b[0]-b[1]
    elif k ==1:
        t = a.pop()
        b.append(t)
        ans += b[-1] - b[-2]
    elif k ==2:
        t = a.pop(0)
        b.insert(0, t)
        ans += b[1] - b[0]
    else:
        t = a.pop(0)
        b.append(t)
        ans += b[-2]-b[-1]
print(ans)

Submission status: Wrong Answer

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

a = sorted(a)
ans = [a[0]]
for i in range(1, n):
    if i%4 == 1:
        ans = [a[-(2*(i//4)+1)]] + ans
    elif i%4 == 2:
        ans = ans + [a[-(2*(i//4)+2)]]
    elif i%4 == 3:
        ans = [a[2*(i//4)+1]] + ans
    else:
        ans = ans + [a[i//2]]

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
_a = a[:]
_a.sort()
__a = a[:]
__a.sort(reverse=True)

i = 0
cand_1 = [_a.pop(-1)]
cand_2 = [__a.pop(-1)]
while len(_a) > 0:
    if i % 4 < 2:
        _b = _a.pop(0)
        __b = __a.pop(0)
    else:
        _b = _a.pop(-1)
        __b = __a.pop(-1)
    if i % 2 == 0:
        cand_1.append(_b)
        cand_2.append(__b)
    else:
        cand_1.insert(0, _b)
        cand_2.insert(0, __b)
    i += 1
res1 = 0
res2 = 0
for i in range(1, n):
    res1+= abs(cand_1[i]-cand_1[i-1])
    res2+= abs(cand_2[i]-cand_2[i-1])

print(max(res1, res2))

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    A = sorted(list(int(input()) for _ in range(n)))
    A = deque(A)
    B = deque([])

    for i in range(n):
        if i == 0:
            mi = A.popleft()
            B.append(mi)
        elif i == 1:
            ma = A.pop()
            B.append(ma)
        else:
            diff_left_mi = ["A.popleft()", "B.appendleft(a)", abs(B[0] - A[0])]
            diff_right_mi = ["A.popleft()", "B.append(a)", abs(B[-1] - A[0])]

            diff_left_ma = ["A.pop()", "B.appendleft(a)", abs(B[0] - A[-1])]
            diff_right_ma = ["A.pop()", "B.append(a)", abs(B[-1] - A[-1])]

            ma = 0
            for op1, op2, diff in [diff_left_mi, diff_right_mi, diff_left_ma, diff_right_ma]:
                if ma <= diff:
                    fomula1 = op1
                    fomula2 = op2
                    ma = diff

            a = eval(fomula1)
            eval(fomula2)

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

    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

from copy import deepcopy

def main():
  n = int(input())  
  la = sorted([int(input()) for _ in range(n)])
  
  lo = deepcopy(la)
  ind = 0
  def f(la, ind):
    lx = []
    while len(la) > 0:
      lx.append(la.pop(ind))
      ind = -1 if ind == 0 else 0
    ls = sorted([abs(lx[i] - lx[i-1]) for i in range(len(lx))])
    return sum(ls[1:])
  print(max(f(deepcopy(la), 0), f(deepcopy(la), -1)))

  
if __name__=='__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

n = int(input().rstrip())

numbers = list(sorted([int(input().rstrip()) for i in range(n)]))

tmp = list(numbers)
tmp2 = list(reversed((numbers)))

list1 = []
list2 = []
if n % 2 == 0:
    list1.append(tmp[0])
    tmp = tmp[1:]
    list1.append(tmp[len(tmp)-1])
    tmp = tmp[0:len(tmp) - 1]

    flag = True
    while len(tmp) > 0:
        if flag:
            list1.insert(0, tmp[len(tmp) - 1])
            tmp = tmp[0:len(tmp)-1]
            list1.append(tmp[0])
            tmp = tmp[1:]
        else:
            list1.insert(0, tmp[0])
            tmp = tmp[1:]
            list1.append(tmp[len(tmp) - 1])
            tmp = tmp[0:len(tmp)-1]
        flag = not flag

else:
    list1.append(tmp[0])
    tmp = tmp[1:]
    list2.append(tmp2[0])
    tmp2 = tmp2[1:]

    flag = True

    while len(tmp) > 0:
        if flag:
            list1.append(tmp[len(tmp)-1])
            tmp = tmp[0:len(tmp)-1]
            list1.insert(0, tmp[len(tmp)-1])
            tmp = tmp[0:len(tmp)-1]

            list2.append(tmp2[len(tmp2)-1])
            tmp2 = tmp2[0:len(tmp2)-1]
            list2.insert(0, tmp2[len(tmp2)-1])
            tmp2 = tmp2[0:len(tmp2)-1]

        else:
            list1.append(tmp[0])
            tmp = tmp[1:]
            list1.insert(0, tmp[0])
            tmp = tmp[1:]

            list2.append(tmp2[0])
            tmp2 = tmp2[1:]
            list2.insert(0, tmp2[0])
            tmp2 = tmp2[1:]
        flag = not flag

ans = 0

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

if len(list2) > 0:
    tmp = 0
    for i in range(1,n):
        tmp += abs(list2[i-1]-list2[i])
    ans = max(ans,tmp)

print(ans)

Submission status: Time Limit Exceeded

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

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

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())

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

A.sort()

ans=[A[0],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]-ans[0])
    n[1]=abs(A[l]-ans[-1])
    n[2]=abs(A[r]-ans[0])
    n[3]=abs(A[r]-ans[-1])
    if n[0]==max(n):
        ansn+=n[0]
        ans.insert(0,A[l])
        l+=1
    elif n[1]==max(n):
        ansn+=n[1]
        ans.insert(len(ans),A[l])
        l+=1
    elif n[2]==max(n):
        ansn+=n[2]
        ans.insert(0,A[r])
        r-=1
    elif n[3]==max(n):
        ansn+=n[3]
        ans.insert(len(ans),A[r])
        r-=1

print(ansn)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

N = int(input())
A = []
B = []

for i in range(N):

    a = int(input())

    A.append(a)

A.sort()

now = A.pop(0)
L = now
R = now
ans = 0

for i in range(N-1):

    if abs(L-A[0]) == max(abs(L-A[0]),abs(L-A[-1]),abs(R-A[0]),abs(R-A[-1])):
        ans += abs(L-A[0])
        L = A[0]
        A.pop(0)

    elif abs(L-A[-1]) == max(abs(L-A[0]),abs(L-A[-1]),abs(R-A[0]),abs(R-A[-1])): 
        ans += abs(L-A[-1])
        L = A[-1]
        A.pop(-1)

    elif abs(R-A[0]) == max(abs(L-A[0]),abs(L-A[-1]),abs(R-A[0]),abs(R-A[-1])):
        ans += abs(R-A[0])
        R = A[0]
        A.pop(0)

    elif abs(R-A[-1]) == max(abs(L-A[0]),abs(L-A[-1]),abs(R-A[0]),abs(R-A[-1])):
        ans += abs(R-A[-1])
        R = A[-1]
        A.pop(-1)

    else:
        print ("eeror")

    #print (A,L,R,ans)

print (ans)

Submission status: Time Limit Exceeded

Solution code:
#### import ####
import sys
sys.setrecursionlimit(10**7)
import math
from collections import defaultdict

#### 定数 ####
mod = 10**9 + 7

#### 読み込み ####
def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
  read_all = [tuple(map(int, input().split())) for _ in range(N)]
  return map(list,zip(*read_all))

#################

N = I()
A = [0]*N
for i in range(N):
  A[i] = I()
  
A.sort()
A2 = A[:]

temp = A.pop(0)
B = [temp]
for i in range(math.ceil((N-1)/2)):
  if i%2==0:
    temp = A.pop(len(A)-1)
    B.insert(0,temp)
    if len(A)>0:
      temp = A.pop(0)
      B.insert(0,temp)
  else:
    temp = A.pop(len(A)-1)
    B.append(temp)
    if len(A)>0:
      temp = A.pop(0)
      B.append(temp)

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

if N%2==1:
  val2 = val1-abs(A2[(N-1)//2]-A2[(N-1)//2+1])+abs(A2[(N-1)//2]-A2[(N-1)//2-1])
print(max(val1,val2))