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:
import collections
q = collections.deque()
ais = map(int,[raw_input() for i in range(int(raw_input()))])
ais.sort()
def f(ais):
	q = collections.deque(ais)
	r = collections.deque()
	r.append(q.pop())
	while(q):
		if q: r.append(q.popleft())
		if q: r.appendleft(q.popleft())
		if q: r.appendleft(q.popleft())
		if q: r.append(q.popleft())

	r = list(r)
	return sum([abs(v-u)for u,v in zip(r,r[1:])])
def g(ais):
	q = collections.deque(ais)
	r = collections.deque()
	r.append(q.popleft())
	while(q):
		if q: r.append(q.pop())
		if q: r.appendleft(q.pop())
		if q: r.appendleft(q.pop())
		if q: r.append(q.pop())

	r = list(r)
	return sum([abs(v-u)for u,v in zip(r,r[1:])])

print max(g(ais),f(ais))

Submission status: Wrong Answer

Solution code:
import collections
q = collections.deque()
ais = map(int,[raw_input() for i in range(int(raw_input()))])
ais.sort()
def f(ais):
	q = collections.deque(ais)
	r = collections.deque()
	while(q):

		r.append(q.pop())
		if q:
			r.append(q.popleft())
		if q:
			r.appendleft(q.popleft())

	r = list(r)
	return sum([abs(v-u)for u,v in zip(r,r[1:])])
def g(ais):
	q = collections.deque(ais)
	r = collections.deque()
	while(q):

		r.append(q.popleft())
		if q:
			r.append(q.pop())
		if q:
			r.appendleft(q.pop())

	r = list(r)
	return sum([abs(v-u)for u,v in zip(r,r[1:])])

print max(g(ais),f(ais))

Submission status: Wrong Answer

Solution code:
# 係数の絶対値を決めていく、各項の係数の符号は交互
# ジグザグの数列を作る、あるいは昇順ソートして大きな係数から割り当てていく
N = int(input())

A = [int(input()) for _ in range(N)]
ans = 0
modes = [0, 1]
for mode in modes:
    temp = 0
    for i in range(N):
        coef = 2
        if i == 0 or i == N-1:
            coef = 1
        if i % 2 == mode:
            coef *= -1
        temp += coef * A[i]
        print(temp)
ans = max(ans, temp)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import deque
def solve():
  n = int(input())
  a = [int(input()) for _ in range(n)]
  query = deque([])
  a = sorted(a,reverse = True)
  big = []
  small = []
  for i in range(n):
    if i< n//2:
      big.append(a[i])
    else:
      small.append(a[i])
  small.sort()
  number = 1
  if n <= 3:
    query.append(big.pop(0))
  else:
      t,d = big[0],big[1]
      l,r = small[0],small[1]
      if big[0]+big[1]-small[0] >= abs(small[0]-big[0])+abs(small[1]-big[0]):
        query.append(small.pop(0))
        number = 0
      else:
        query.append(big.pop(0))
        
    
  noright = False
  
  while len(big) > 0 or len(small) > 0:
    if number %2 == 0:
      leftnumber = big.pop(0)
      if len(big)!= 0:
        rightnumber = big.pop(0)
      else:
        noright = True
    else:
      leftnumber = small.pop(0)
      if len(small) != 0:
        rightnumber = small.pop(0)
      else:
        noright = True
    query.appendleft(leftnumber)
    if not noright:
      query.append(rightnumber)
    number += 1
    noright = False

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import deque
def solve():
  n = int(input())
  a = [int(input()) for _ in range(n)]
  query = deque([])
  a = sorted(a,reverse = True)
  big = []
  small = []
  for i in range(n):
    if i< n//2:
      big.append(a[i])
    else:
      small.append(a[i])
  small.sort()
  number = 1
  if n%2 != 0:
    query.append(big.pop(0))
  else:
    if n == 2:
      query.append(big.pop(0))
    else:
      t,d = big[0],big[1]
      l,r = small[0],small[1]
      if big[0]+big[1]-small[0] >= abs(small[0]-big[0])+abs(small[1]-big[0]):
        query.append(small.pop(0))
        number = 0
      else:
        query.append(big.pop(0))
        
    
  noright = False
  
  while len(big) > 0 or len(small) > 0:
    if number %2 == 0:
      leftnumber = big.pop(0)
      if len(big)!= 0:
        rightnumber = big.pop(0)
      else:
        noright = True
    else:
      leftnumber = small.pop(0)
      if len(small) != 0:
        rightnumber = small.pop(0)
      else:
        noright = True
    query.appendleft(leftnumber)
    if not noright:
      query.append(rightnumber)
    number += 1
    noright = False

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import deque
def solve():
  n = int(input())
  a = [int(input()) for _ in range(n)]
  query = deque([])
  a = sorted(a,reverse = True)
  big = []
  small = []
  for i in range(n):
    if i< n//2:
      big.append(a[i])
    else:
      small.append(a[i])
  small.sort()       
  if n%2 != 0:
    query.append(big.pop(0))
  else:
    if n == 2:
      query.append(big.pop(0))
    else:
      t,d = big[0],big[1]
      l,r = small[0],small[1]
      if big[0]+big[1]-small[0] >= abs(2*small[0]-big[0]):
        query.append(big.pop(0))
      else:
        query.append(small.pop(0))
    
  number = 1
  noright = False
  
  while len(big) > 0 or len(small) > 0:
    if number %2 == 0:
      leftnumber = big.pop(0)
      if len(big)!= 0:
        rightnumber = big.pop(0)
      else:
        noright = True
    else:
      leftnumber = small.pop(0)
      if len(small) != 0:
        rightnumber = small.pop(0)
      else:
        noright = True
    query.appendleft(leftnumber)
    if not noright:
      query.append(rightnumber)
    number += 1
    noright = False

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

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from collections import deque

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

a = deque(a)
ans = deque([])

cur = a.popleft()
ans.append(cur)

cnt = 0
while len(a) > 1:
    if cnt%2 == 0:
        cur1 = a.pop()
        cur2 = a.pop()

    else:
        cur1 = a.popleft()
        cur2 = a.popleft()

    ans.append(cur1)
    ans.appendleft(cur2)
    cnt += 1

if len(a) == 1:
    if abs(a[0] - ans[0]) > abs(a[0] - ans[-1]):
        ans.appendleft(a[0])
    else:
        ans.append(a[0])

if len(a)%2:
    cur = a.popleft()
    ans.append(cur)

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

print(ret)

Submission status: Wrong Answer

Solution code:
def read():
    N = int(input().strip())
    A = []
    for _ in range(N):
        a = int(input().strip())
        A.append(a)
    return N, A


def solve(N, A):
    A = sorted(A)
    low = A[:N//2]
    high = A[N//2:]  # 奇数ならlen(low) + 1
    B = [0] * N
    for i in range(len(high)):
        B[i * 2] = high.pop()
    for i in range(len(low)):
        B[i * 2 + 1] = low.pop(0)
    sub = [0] * N
    for i in range(N):
        sub[i] = abs(B[i] - B[(i-1)%N])
    return sum(sub[1:N-1]) + max(sub[0], sub[N-1])


if __name__ == '__main__':
    inputs = read()
    print("%d" % solve(*inputs))

Submission status: Wrong Answer

Solution code:
def read():
    N = int(input().strip())
    A = []
    for _ in range(N):
        a = int(input().strip())
        A.append(a)
    return N, A


def solve(N, A):
    A = sorted(A)
    low = A[:N//2]
    high = A[N//2:]  # 奇数ならlen(low) + 1
    B = [0] * N
    for i in range(len(high)):
        B[i * 2] = high.pop()
    for i in range(len(low)):
        B[i * 2 + 1] = low.pop(0)

    sub = [0] * N
    for i in range(N):
        sub[i] = abs(B[i] - B[i-1])
    return sum(sub[1:N-1]) + max(sub[0], sub[N-1])


if __name__ == '__main__':
    inputs = read()
    print("%d" % solve(*inputs))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
A = sorted(A, reverse=True)
L, R = A[0], A[0]
A.pop(0)
num = 0
for i in range(N - 1):
    if i % 2 == 0:
        k = A.pop(-1)
    else:
        k = A.pop(0)
    if abs(R - k) <= abs(L - k):
        num += abs(L - k)
        L = k
    else:
        num += abs(R - k)
        R = k


print(num)

Submission status: Wrong Answer

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

a = len(A)
if a%2==0:
  answer = 2 * sum(A[a//2+1:]) + A[a//2] - A[a//2-1] - 2* sum(A[:a//2-1])
  
if a%2==1:
  b = a//2
  if 2*A[b] + A[b-1] - A[b+1]>0:
    answer = 2*sum(A[b+1:]) - A[b] - A[b-1] - 2*sum(A[:b-1])
  else:
    answer = A[b] + A[b+1] + 2*sum(A[b+2:]) - 2*sum(A[:b])  
  
print(answer)

Submission status: Wrong Answer

Solution code:
N = input()
a = []
for i in range(N):
	a.append(input())
a.sort()
if(N % 2 == 1):
	print((sum(a[N/2+1:])-sum(a[:N/2]))*2-min([a[N/2]-a[N/2-1],a[N/2+1]-a[N/2+1]]))
else:
	print((sum(a[N/2:])-sum(a[:N/2]))*2-(a[N/2]-a[N/2-1]))

Submission status: Wrong Answer

Solution code:
n = int(input())
l = [int(input()) for _ in range(n)]
l.sort()
if n%2:
  s = l[:(n-1)//2]
  b = l[(n-1)//2:]
  t = sum(b)*2 - sum(s)*2 -sum(b[:2])
else:
  s = l[:n//2]
  b = l[n//2:]
  t = sum(b)*2 - sum(s)*2 -b[0] + s[-1]
print(t)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

A=sorted(A)
B=A[:]

ans=0
#小さいのが真ん中
top=A[0]
bottom=A[0]
tmp=0
turn=True
del A[0]
while  A:
  if len(A)==1:
    tmp+=max(abs(top-A[0]),abs(bottom-A[0]))
    break
    pass
  if turn:
    a,b=A[-2],A[-1]
  else:
    a,b=A[0:2]
    
  if abs(a-bottom)+abs(b-top)<abs(a-top)+abs(b-bottom):
    a,b=b,a
  tmp+=abs(a-bottom)+abs(b-top)
  bottom,top=a,b
  if turn:
    del A[-1]
    del A[-1]
  else:
    del A[0]
    del A[0]
  turn=not turn

ans=tmp
A=B
top=A[-1]
bottom=A[-1]
tmp=0
del A[-1]
turn = True
while  A:
  #print("center big:tmp: ", tmp)
  if len(A)==1:
    tmp+=max(abs(top-A[0]),abs(bottom-A[0]))
    break
    pass
  if turn:
    a,b=A[0],A[1]
  else:
    a,b=A[-1],A[-2]
    
  if abs(a-bottom)+abs(b-top)<abs(a-top)+abs(b-bottom):
    a,b=b,a
  bottom,top=a,b
  tmp+=abs(a-bottom)+abs(b-top)
  if turn:
    del A[0] 
    del A[0]
  else:
    del A[-1]
    del  A[-1]
  turn= not turn

ans=max(ans,tmp)
print(ans)

Submission status: Wrong Answer

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


A.sort()
Smaller = A[:ceil(N / 2)][::-1]
Bigger = A[N // 2:]

ans_A = deque([Smaller.pop()])
ans = 0
for i in range(N - 1):
    if i % 2 == 0:
        x = Bigger.pop()
    else:
        x = Smaller.pop()

    front_diff = abs(ans_A[0] - x)
    rear_diff = abs(ans_A[-1] - x)

    if front_diff > rear_diff:
        ans += front_diff
        ans_A.appendleft(x)
    else:
        ans += rear_diff
        ans_A.append(x)

print(ans)

Submission status: Wrong Answer

Solution code:
from math import sqrt, floor
from itertools import combinations
N = int(input())

ans_root = sqrt(1 + 8 * N)
if (1 + 8 * N) - floor(ans_root) ** 2 > 1e-6:
    print("No")
    exit(0)

k = floor((1 + ans_root) // 2)
transposed_flags = [[False] * k for _ in range(N)]
for i, (flag1, flag2) in enumerate(combinations(range(k), 2)):
    transposed_flags[i][flag1] = True
    transposed_flags[i][flag2] = True

print("Yes")
print(k)
for flags in zip(*transposed_flags):
    number_contained = [str(i + 1) for i, flag in enumerate(flags) if flag]
    print(len(number_contained), " ".join(number_contained))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import math
import numpy as np
def solve():
    N = int(input())
    m = int(math.sqrt(1+8*N))
    if 1+8*N == m**2:
        k = (2+m)//2
    elif 1+8*N == (m+1)**2:
        k = (1+m)//2
    else:
        return 'No'
    M = np.arange(k**2, dtype=int).reshape(k,k)-np.array([[(i+1)*i/2] for i in range(k)], dtype=int)-np.arange(k).reshape(k,1)
    M *= (np.arange(k).reshape(1,k)>np.arange(k).reshape(k,1))
    M += M.T
    res = '{}'.format(k+1)
    for a in M:
        t = '\n{} '.format(k)
        t += ' '.join([str(b) for b in a if b > 0])
        res += t 
    return res
    
if __name__ == '__main__':
    print(solve())

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# coding: utf-8
#
    
def main(n, in_l):
    median = 0
    retval_a = 0
    retval_b = 0
    l = sorted(in_l)

    if 0 == n % 2:
        median = n//2
        retval_a = (sum(l[median:]) * 2 - l[median] ) -  sum(l[:median]) * 2  + sum(l[median - 1:median])
        retval_b = (sum(l[median:]) * 2 - l[-1]) - sum(l[:median]) * 2 + l[median - 1]
    else:
        median = (n - 1)//2
        retval_a = (sum(l[median + 1:]) * 2 - l[median]) -  sum(l[:median]) * 2 + sum(l[median - 1:median])
        retval_b = (sum(l[median:]) * 2 - l[-1] - l[median]) - sum(l[:median]) * 2
    retval = max(retval_a, retval_b)
    print('{}'.format(retval))
    
if __name__ == '__main__':
    try:
        n  = int(input().strip())
        l = []
        for _ in range(n):
            l.append(int(input().strip()))
        main(n, l)
    except EOFError:
        pass

Submission status: Wrong Answer

Solution code:
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
for i in range(n):
    for j in range(n-1):
        if a[j] > a[j+1]:
            k = a[j]
            a[j] = a[j+1]
            a[j+1] = k
if n % 2 == 0:
    a = a[::-1]
m = (n-1)//2
b = [0]*n
b[m] = a[0]
k = 1
for i in range(m-2, -1, -2):
    b[i] = a[k]
    k += 1
    b[n-i-1] = a[k]
    k += 1
k = 1
for i in range(m-1, -1, -2):
    b[i] = a[n-k]
    k += 1
    b[n-i-1] = a[n-k]
    k += 1
s = 0
for i in range(n-1):
    s += abs(b[i]-b[i+1])
print(s)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# coding: utf-8
#
    
def main(n, in_l):
    median = 0
    retval_a = 0
    retval_b = 0
    l = sorted(in_l)

    if 0 == n % 2:
        median = n//2
        retval_a = (sum(l[median:]) * 2 - l[median] ) -  sum(l[:median ]) * 2  + sum(l[median - 1:median])
        retval_b = (sum(l[median:]) * 2 - l[-1]) - sum(l[:median]) * 2 + l[median - 1]
    else:
        median = (n - 1)//2
        retval_a = (sum(l[median + 1:]) * 2 - l[median]) -  sum(l[:median]) * 2 + sum(l[median - 1:median])
        retval_b = (sum(l[median:]) * 2 - l[-1] - l[median]) - sum(l[:median]) * 2
    retval = max(retval_a, retval_b)
    print(l[median:])
    print(l[:median])

    print('{}'.format(retval))
    
if __name__ == '__main__':
    try:
        n  = int(input().strip())
        l = []
        for _ in range(n):
            l.append(int(input().strip()))
        main(n, l)
    except EOFError:
        pass

Submission status: Wrong Answer

Solution code:
print(1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
for i in range(n):
    for j in range(n-1):
        if a[j] > a[j+1]:
            k = a[j]
            a[j] = a[j+1]
            a[j+1] = k
odd = False
if n % 2 == 0:
    odd = True
    min = a[0]
    a = a[1:]
    n -= 1
m = (n-1)//2
b = [0]*n
b[m] = a[0]
k = 1
for i in range(m-2, -1, -2):
    b[i] = a[k]
    k += 1
    b[n-i-1] = a[k]
    k += 1
k = 1
for i in range(m-1, -1, -2):
    b[n-i-1] = a[n-k]
    k += 1
    b[i] = a[n-k]
    k += 1
if odd:
    b.append(min)
    n += 1
s = 0
for i in range(n-1):
    s += abs(b[i]-b[i+1])
print(s)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# coding: utf-8
#
    
def main(n, in_l):
    median = 0
    retval_a = 0
    retval_b = 0
    l = sorted(in_l)

    if 0 == n % 2:
        median = n//2
        retval_a = (sum(l[median:]) * 2 - l[median] ) -  sum(l[:median ]) * 2  + sum(l[median - 1:median])
        # retval_b = (sum(l[median:]) * 2 - l[-1]) - sum(l[:median]) * 2 + l[median - 1]
    else:
        median = (n - 1)//2
        retval_a = (sum(l[median + 1:]) * 2 - l[median]) -  sum(l[:median]) * 2 + sum(l[median - 1:median])
        retval_b = (sum(l[median:]) * 2 - l[-1] - l[median]) - sum(l[:median]) * 2
    retval = max(retval_a, retval_b)
    print('{}'.format(retval))
    
if __name__ == '__main__':
    try:
        n  = int(input().strip())
        l = []
        for _ in range(n):
            l.append(int(input().strip()))
        main(n, l)
    except EOFError:
        pass

Submission status: Wrong Answer

Solution code:
N = int(input())

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

A.sort()

right = A[0]
left = A[0]
index_l = 1
index_r = len(A)-1
ans = 0
count = 1
while(1):
	if count % 2 == 1:
		if index_r - index_l >= 1:
			if abs(left-A[index_r])+abs(right-A[index_r-1]) > abs(right-A[index_r])+abs(left-A[index_r-1]):
				ans += abs(left-A[index_r])+abs(right-A[index_r-1])
				left = A[index_r]
				right = A[index_r-1]
			else:
				ans += abs(right-A[index_r])+abs(left-A[index_r-1])
				left = A[index_r-1]
				right = A[index_r]
			index_r -= 2
		elif index_r - index_l == 0:
			if abs(left-A[index_r]) > abs(right-A[index_r]):
				ans += abs(left-A[index_r])
			else:
				ans += abs(right-A[index_r])
			index_r -= 1
		else: break
	else:
		if index_r - index_l >= 1:
			if abs(left-A[index_l])+abs(right-A[index_l+1]) > abs(right-A[index_l])+abs(left-A[index_l+1]):
				ans += abs(left-A[index_l])+abs(right-A[index_l+1])
				left = A[index_l]
				right = A[index_l+1]
			else:
				ans += abs(right-A[index_l])+abs(left-A[index_l+1])
				left = A[index_l+1]
				right = A[index_l]
			index_l += 2
		elif index_r - index_l == 0:
			if abs(left-A[index_r]) > abs(right-A[index_r]):
				ans += abs(left-A[index_r])
			else:
				ans += abs(right-A[index_r])
			index_l += 1
		else:
			break
	count += 1
		
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
A.sort()
Sum = 0
As, Al = A[:N//2], A[N//2:]
for i in range((N//2) - 1):
    Sum += (Al[-i-1] - As[i]) * 2
if N % 2 == 0:
    Sum += Al[0] - As[-1]
elif N % 4 == 1:
    Sum += Al[1]*2 - Al[0] - As[-1]
else:
    Sum += Al[0] + Al[1] - As[-1]*2
print(Sum)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

A.sort()
queue = deque(A)

ans = 0
tmp1_ = queue.popleft() #min(A)
tmp2_ = tmp1_
while queue:
    tmp1 = queue.pop()
    ans += abs(tmp1-tmp1_)
    if not queue:
        break
    tmp2 = queue.pop()
    ans += abs(tmp2-tmp2_)
    tmp1_ = tmp1
    tmp2_ = tmp2
    queue.reverse()
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
import copy

def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [long(input_raw[0])]
    a_list=[]
    for i in range(N):
        input_raw = read_func().strip().split()
        [a] = [long(input_raw[0])]
        a_list.append(a)

    a_list.sort()
    left = a_list[0]
    right = a_list[0]

    score1 = 0L
    for i in range(0, N/2 + (0 if N % 2 ==0 else 1)):

        if i != 0:
            if abs(a_list[i] - left) > abs(a_list[i] - right):
                score1 += abs(a_list[i] - left)
                left = a_list[i]
            else:
                score1 += abs(a_list[i] - right)
                right = a_list[i]

        j = N - i - 1
        if j != i:
            if abs(a_list[j] - left) > abs(a_list[j] - right):
                score1 += abs(a_list[j] - left)
                left = a_list[j]
            else:
                score1 += abs(a_list[j] - right)
                right = a_list[j]

    a_list.sort(reverse=True)
    left = a_list[0]
    right = a_list[0]

    score2 = 0L
    for i in range(0, N/2 + (0 if N % 2 ==0 else 1)):

        if i != 0:
            if abs(a_list[i] - left) > abs(a_list[i] - right):
                score2 += abs(a_list[i] - left)
                left = a_list[i]
            else:
                score2 += abs(a_list[i] - right)
                right = a_list[i]

        j = N - i - 1
        if j != i:
            if abs(a_list[j] - left) > abs(a_list[j] - right):
                score2 += abs(a_list[j] - left)
                left = a_list[j]
            else:
                score2 += abs(a_list[j] - right)
                right = a_list[j]

    a_list.sort()
    left = a_list[0]
    right = a_list[0]
    score3 = 0L
    for i in range(0, N/2 + (0 if N % 2 ==0 else 1)):

        if i != 0:
            if abs(a_list[i] - left) >= abs(a_list[i] - right):
                score3 += abs(a_list[i] - left)
                left = a_list[i]
            else:
                score3 += abs(a_list[i] - right)
                right = a_list[i]

        j = N - i - 1
        if j != i:
            if abs(a_list[j] - left) >= abs(a_list[j] - right):
                score3 += abs(a_list[j] - left)
                left = a_list[j]
            else:
                score3 += abs(a_list[j] - right)
                right = a_list[j]

    a_list.sort(reverse = True)
    left = a_list[0]
    right = a_list[0]
    score4 = 0L
    for i in range(0, N/2 + (0 if N % 2 ==0 else 1)):

        if i != 0:
            if abs(a_list[i] - left) >= abs(a_list[i] - right):
                score4 += abs(a_list[i] - left)
                left = a_list[i]
            else:
                score4 += abs(a_list[i] - right)
                right = a_list[i]

        j = N - i - 1
        if j != i:
            if abs(a_list[j] - left) >= abs(a_list[j] - right):
                score4 += abs(a_list[j] - left)
                left = a_list[j]
            else:
                score4 += abs(a_list[j] - right)
                right = a_list[j]

    print max(score1, score2, score3, score4)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

l = 0
r = N-1
dif = abs(A[r] - A[l])

i,j = 1, N-2
while i<=j:
  if i == j:
    if i == l or i == r:
      break
  #print(i,j,l,r)
  m = max(abs(A[i]-A[l]), abs(A[i]-A[r]), abs(A[j]-A[l]), abs(A[j]-A[r]))
  if m == abs(A[i]-A[l]):
    dif += abs(A[i]-A[l])
    l = i 
    i += 1
    continue
  if m == abs(A[i]-A[r]):
    dif += abs(A[i]-A[r])
    r = i
    i += 1
    continue
  if m == abs(A[j]-A[l]):
    dif += abs(A[j]-A[l])
    l = j
    j -= 1
    continue
  if m == abs(A[j]-A[r]):
    dif += abs(A[j]-A[r])
    r = j
    j -+ 1
    continue
    
print(dif)

Submission status: Wrong Answer

Solution code:
import numpy as np
n = int(input())

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



a = np.sort(a)

m = []
nm = []
while True:
    if len(a)%2 == 0:
        break
    t = np.median(np.unique(a))
    m.append(t)
    nm.append(sum(a==t))
    a = a[a != t]
    
m = list(reversed(m))
nm = list(reversed(nm))

b = []
for k in range(int(len(a)/2)):
    b.append(a[int(len(a)/2) - k - 1])
    b.append(a[-k-1])

for k in range(len(nm)):
    if nm[k] == 1:
        if len(b) > 1:
            if m[k] - b[0] > b[-1] - m[k]:
                b = [m[k]] + b
            else:
                b = b + [m[k]]
        else:
            b = [m[k]]
    else:
        b = [m[k]] + b + [m[k]]
print(int(np.sum(np.abs(np.diff(b)))))

b

Submission status: Wrong Answer

Solution code:
import itertools


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

    a = []
    for i in range(n):
        a.append(int(input()))
    a.sort()
    # if n == 2:
    #     print(abs(a[0] - a[1]))
    #     return
    # if n == 3:
    #     tmp1 = abs(a[0] - a[1]) + abs(a[0] - a[2])
    #     tmp2 = abs(a[0] - a[2]) + abs(a[1] - a[2])
    #     print(max([tmp1, tmp2]))
    #     return
    if n < 10:
        ans = 0
        for a in itertools.permutations(a):
            tmp = 0
            for i in range(len(a) - 1):
                tmp += abs(a[i] - a[i + 1])
            ans = max([ans, tmp])
        print(ans)
        return
    ret1 = a[:n // 2 + (1 if n % 2 == 1 else 0)]
    ret2 = a[n // 2 + (1 if n % 2 == 1else 0):]
    ret1 = [ret1[-1]] + ret1[:-2] + [ret1[-2]]
    ret2 = ret2[1:] + [ret2[0]]
    ans = 0

    ans += abs(ret1[-1] - ret2[-1])
    for i in range(len(ret2) - 1):
        ans += abs(ret1[i] - ret2[i])
        ans += abs(ret1[i + 1] - ret2[i])
    if len(ret1) != len(ret2):
        ans += abs(ret1[-2] - ret2[-1])

    if n % 2 == 0:
        print(ans)
        return
    ans1 = ans

    ret1 = a[:n // 2 + (0 if n % 2 == 1 else 1)]
    ret2 = a[n // 2 + (0 if n % 2 == 1else 1):]
    ret1 = ret1[1:] + [ret1[0]]
    ret2 = [ret2[-1]] + ret2[:-2] + [ret2[-2]]
    ans = 0

    ans += abs(ret1[-1] - ret2[-1])
    for i in range(len(ret1) - 1):
        ans += abs(ret2[i] - ret1[i])
        ans += abs(ret2[i + 1] - ret1[i])
    if len(ret1) != len(ret2):
        ans += abs(ret1[-1] - ret2[-2])
    print(max([ans, ans1]))


main()

Submission status: Wrong Answer

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

A.sort()
A.reverse()
Ab = A[:N//2]
As = A[N//2:]
if N%2 == 0:
    if N == 2:
        ans = abs(A[0] - A[1])
    else:
        ans = sum(Ab[:-1])*2 + Ab[-1] - As[0] - sum(As[1:])*2
else:
    if N == 3:
        ans = max([A[0]*2-A[1]-A[2], A[0]+A[1]-A[2]*2])
    else:
        ans = sum(Ab)*2 - sum(As[2:])*2 - As[0] - As[1]
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
import copy

def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [long(input_raw[0])]
    a_list=[]
    for i in range(N):
        input_raw = read_func().strip().split()
        [a] = [long(input_raw[0])]
        a_list.append(a)

    a_list.sort()
    left = a_list[0]
    right = a_list[0]

    score1 = 0
    for i in range(0, N/2 + (0 if N % 2 ==0 else 1)):

        if i != 0:
            if abs(a_list[i] - left) > abs(a_list[i] - right):
                score1 += abs(a_list[i] - left)
                left = a_list[i]
            else:
                score1 += abs(a_list[i] - right)
                right = a_list[i]

        j = N - i - 1
        if j != i:
            if abs(a_list[j] - left) > abs(a_list[j] - right):
                score1 += abs(a_list[j] - left)
                left = a_list[j]
            else:
                score1 += abs(a_list[j] - right)
                right = a_list[j]

    a_list.sort(reverse=True)
    left = a_list[0]
    right = a_list[0]

    score2 = 0
    for i in range(0, N/2 + (0 if N % 2 ==0 else 1)):

        if i != 0:
            if abs(a_list[i] - left) > abs(a_list[i] - right):
                score2 += abs(a_list[i] - left)
                left = a_list[i]
            else:
                score2 += abs(a_list[i] - right)
                right = a_list[i]

        j = N - i - 1
        if j != i:
            if abs(a_list[j] - left) > abs(a_list[j] - right):
                score2 += abs(a_list[j] - left)
                left = a_list[j]
            else:
                score2 += abs(a_list[j] - right)
                right = a_list[j]


    print max(score1,score2)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np
n = int(input())

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

a = np.sort(a)

m = []
nm = []
while True:
    if len(a)%2 == 0:
        break
    m.append(np.median(a))
    nm.append(sum(a==np.median(a)))
    a = a[a != np.median(a)]
    
m = list(reversed(m))
nm = list(reversed(nm))

b = []
for k in range(int(len(a)/2)):
    b.append(a[int(len(a)/2) - k - 1])
    b.append(a[-k-1])

for k in range(len(nm)):
    if nm[k] == 1:
        if len(b) > 1:
            if m[k] - b[0] > b[-1] - m[k]:
                b = [m[k]] + b
            else:
                b = b + [m[k]]
        else:
            b = [m[k]]
    else:
        b = [m[k]] + b + [m[k]]
print(int(np.sum(np.abs(np.diff(b)))))