Problem:


Score : 400 points

Problem Statement

You are given N integers; the i-th of them is A_i.
Find the maximum possible sum of the absolute differences between the adjacent elements after arranging these integers in a row in any order you like.

Constraints

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print the maximum possible sum of the absolute differences between the adjacent elements after arranging the given integers in a row in any order you like.

Sample Input 1

5
6
8
1
2
3

Sample Output 1

21

When the integers are arranged as 3,8,1,6,2, the sum of the absolute differences between the adjacent elements is |3 - 8| + |8 - 1| + |1 - 6| + |6 - 2| = 21. This is the maximum possible sum.

Sample Input 2

6
3
1
4
1
5
9

Sample Output 2

25

Sample Input 3

3
5
5
1

Sample Output 3

8



Sample input:
5
6
8
1
2
3


Sample output:
21

Submission status: Wrong Answer

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

l = a[0]
r = a[0]
ans = 0
for i in range(la//2):
    if (la-1) % 2 == 1 and i == la//2-1:
        x = a[i+1]
        ans += max(abs(l-x), abs(r-x))
        break

    if i % 2 == 0:
        x = a[la-i-1]
        y = a[la-i-2]
        ans += max(abs(l-x)+abs(r-y), abs(l-y)+abs(r-x))
    else:
        x = a[i]
        y = a[i+1]
        ans += max(abs(l-x)+abs(r-y), abs(l-y)+abs(r-x))
    l = x
    r = y

a = a[::-1]
l = a[0]
r = a[0]
ans2 = 0
for i in range(la//2):
    if (la-1) % 2 == 1 and i == la//2-1:
        x = a[i+1]
        ans2 += max(abs(l-x), abs(r-x))
        break

    if i % 2 == 0:
        x = a[la-i-1]
        y = a[la-i-2]
        ans2 += max(abs(l-x)+abs(r-y), abs(l-y)+abs(r-x))
    else:
        x = a[i]
        y = a[i+1]
        ans2 += max(abs(l-x)+abs(r-y), abs(l-y)+abs(r-x))
    l = x
    r = y

print(max(ans, ans2))

Submission status: Wrong Answer

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

l = a[0]
r = a[0]
ans = 0
for i in range(la//2):
    if (la-1) % 2 == 1 and i == la//2-1:
        x = a[i+1]
        ans += max(abs(l-x), abs(r-x))
        break

    if i % 2 == 0:
        x = a[la-i-1]
        y = a[la-i-2]
        ans += max(abs(l-x)+abs(r-y), abs(l-y)+abs(r-x))
    else:
        x = a[i]
        y = a[i+1]
        ans += max(abs(l-x)+abs(r-y), abs(l-y)+abs(r-x))
    l = x
    r = y

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
A = sorted(A)
B = [A[i//2] if i % 2 else A[-i//2-1] for i in range(N//2 * 2)]
# print(B)
if N % 2:
    C = [A[N//2]] + B
    D = B + [A[N//2]]
else:
    C = [B[-1]] + B[:-1]
    D = B
# print(B)
# print([abs(B[i+1] - B[i]) for i in range(N-1)])
c = sum([abs(C[i+1] - C[i]) for i in range(N-1)])
d = sum([abs(D[i+1] - D[i]) for i in range(N-1)])
print(max(c, d))

Submission status: Wrong Answer

Solution code:
A = [int(input()) for _ in range(int(input()))]
A.sort()
l,r = A[0],A[0]
A.pop(0)    
ans = 0
for i in range(len(A)):
    j = A[i%2-1]
    if abs(l-j) > abs(r-j):
        ans += abs(l-j)
        l = j
        A.pop(i%2-1)
    else:
        ans += abs(r-j)
        r = j
        A.pop(i%2-1)
print(ans)

Submission status: Wrong Answer

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

A.sort()
A.reverse()
temp = 0

if N == 2:
    print(abs(A[0] - A[1]))
    exit()

if N % 2 == 1:
    for i in range(N-2):
        if i == 0:
            temp += abs(A[-1] - A[i])
            temp += abs(A[-1] - A[i + 1])
        else:
            temp += abs(A[-i-1] - A[i-1])
else:
    for i in range(N-2):
        if i == 0:
            temp += abs(A[i] - A[-1])
            temp += abs(A[i] - A[-2])
        elif i == N-3:
            temp += abs(A[i-2] - A[-i])
        else:
            temp += abs(A[i] - A[-i])
        
print(temp)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = [int(input()) for _ in range(n)]
print(sum(l) - 4)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
a.sort()
res = 0
b = a[n//2:]
c = a[:n//2]
res += 2*sum(b)
res -= 2*sum(c)
res -= b[0]
if n%2 == 1:
    res -= b[1]
else:
    res += c[-1]
print(res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
a.sort(reverse=True)
res = 0
b = a[n//2:]
c = a[:n//2]
res += 2*sum(b)
res -= 2*sum(c)
res -= b[0]
res += c[-1]
if n%2 == 1:
    res -= b[1]
print(res)

Submission status: Wrong Answer

Solution code:

import sys

def calc(data_ans, num):
    ans = 0
    for i in range(num - 1):
        ans += abs(data_ans[i] - data_ans[i + 1])
    print(ans)
    sys.exit()


def data_next(data_list, data_ans, flg, data_ind_righ, data_ind_left, ans_ind_left, ans_ind_righ, num):
    # print(data_ans, data_ind_left, data_ind_righ, ans_ind_left, ans_ind_righ)
    if ans_ind_left < 0:
        calc(data_ans, num)
    else:
        if flg:
            data_ans[ans_ind_left] = data_list[data_ind_left]
            if ans_ind_righ > num - 1:
                calc(data_ans, num)
            else:
                data_ans[ans_ind_righ] = data_list[data_ind_left + 1]
                data_ind_left += 2

        else:
            data_ans[ans_ind_left] = data_list[data_ind_righ]
            if ans_ind_righ > num - 1:
                calc(data_ans, num)
            else:
                data_ans[ans_ind_righ] = data_list[data_ind_righ - 1]
                data_ind_righ -= 2
        ans_ind_left -= 1
        ans_ind_righ += 1
        if flg:
            flg = 0
        else:
            flg = 1
        data_next(data_list, data_ans, flg, data_ind_righ, data_ind_left, ans_ind_left, ans_ind_righ, num)




def main():
    num = int(input())
    data_list = [int(input()) for i in range(num)]
    data_list.sort()

    # print(data_list)

    if num == 2:
        tasukazu = data_list[1]
        hikukazu = data_list[0]
    elif num == 3:
        tasukazu = data_list[1] + data_list[2]
        hikukazu = data_list[0] * 2
    elif num % 2 == 1:
        hikukazu = 0
        for i in range(num // 2 + 1):
            hikukazu += 2 * data_list[i]
        hikukazu -= data_list[num // 2] + data_list[num // 2 - 1]

        tasukazu = 0
        for i in range(num // 2 + 1, num):
            tasukazu += 2 * data_list[i]
    else:
        hikukazu = 0
        for i in range(num // 2):
            hikukazu += 2 * data_list[i]
        hikukazu -= data_list[num // 2 - 1]

        tasukazu = 0
        for i in range(num // 2, num):
            tasukazu += 2 * data_list[i]
        tasukazu -= data_list[num // 2]

    # print(tasukazu, hikukazu)

    print(tasukazu - hikukazu)



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(int(input()) for _ in range(n))
a.sort()
ans_list = []
min = 0
max = n-1
for i in range(1,n+1):
    if i == 0:
        ans_list.append(a[min])
    elif (i // 2) % 2 == 1:
        if i % 2 == 0:
            ans_list.append(a[max])
            max -= 1
        else:
            ans_list.insert(0,a[max])
            max -= 1
    else:
        if i % 2 == 0:
            ans_list.append(a[min])
            min += 1
        else:
            ans_list.insert(0,a[min])
            min += 1

sum = 0

for i in range(n-1):
    sum += abs(ans_list[i+1] - ans_list[i])

print(sum)

Submission status: Wrong Answer

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

for i in range(1,n+1):
    if i == 0:
        ans_list.append(a[0])
        a.remove(a[0])
    elif (i // 2) % 2 == 1:
        if i % 2 == 0:
            ans_list.append(a[len(a)-1])
            a.remove(a[len(a)-1])
        else:
            ans_list.insert(0,a[len(a)-1])
            a.remove(a[len(a)-1])
    else:
        if i % 2 == 0:
            ans_list.append(a[0])
            a.remove(a[0])
        else:
            ans_list.insert(0,a[0])
            a.remove(a[0])

sum = 0

for i in range(n-1):
    sum += abs(ans_list[i+1] - ans_list[i])

print(sum)

Submission status: Wrong Answer

Solution code:
import bisect
from collections import deque
from collections import defaultdict
from fractions import gcd
#from math import factorial
from math import sqrt
from math import ceil
from itertools import permutations
from heapq import *

N=int(input())
A=[]
B=[]
for _ in range(N):
    a=int(input())
    A.append(a)
    B.append(a)
A.sort()
B.sort()
A=deque(A)
B=deque(B)
L=deque()
L.append(A.popleft())
num=0
while(A):
    if num==0:
        L.appendleft(A.pop())
        num+=1
    elif num==1:
        L.append(A.pop())
        num+=1
    elif num==2:
        L.appendleft(A.popleft())
        num+=1
    elif num==3:
        L.append(A.popleft())
        num=0
sum1=0
for i in range(N-1):
    sum1+=abs(L[i]-L[i+1])
R=deque()
R.append(B.pop())
num=2
while(B):
    if num==0:
        R.appendleft(B.pop())
        num+=1
    elif num==1:
        R.append(B.pop())
        num+=1
    elif num==2:
        R.appendleft(B.popleft())
        num+=1
    elif num==3:
        R.append(B.popleft())
        num=0
sum2=0
for i in range(N-1):
    sum2+=abs(L[i]-L[i+1])
print(max(sum1,sum2))

Submission status: Wrong Answer

Solution code:
from collections import deque
from collections import defaultdict
from fractions import gcd
#from math import factorial
from math import sqrt
from math import ceil
from itertools import permutations
from heapq import *

N=int(input())
A=[]
for _ in range(N):
    a=int(input())
    A.append(a)
A.sort()
A=deque(A)
L=deque()
L.append(A.popleft())
num=0
while(A):
    if num==0:
        L.appendleft(A.pop())
        num+=1
    elif num==1:
        L.append(A.pop())
        num+=1
    elif num==2:
        L.appendleft(A.popleft())
        num+=1
    elif num==3:
        L.append(A.popleft())
        num=0
sum=0
for i in range(N-1):
    sum+=abs(L[i]-L[i+1])

print(sum)

Submission status: Wrong Answer

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

A.sort()

if N == 2:
    print(abs(A[0]-A[1]))
    sys.exit()

if N == 3:
    print(A[2]+A[1]-2*A[0])
    sys.exit()

score = 0
if N % 2 == 1:
    for i in range(int((N-3)/2)):
        score += A[-i-1] + A[-i-2] - 2 * A[0]
        A.pop(0)
    score += A[-1] + A[-2] - A[0] - A[1]
    print(score)

if N % 2 == 0:
    for i in range(int((N-2)/2)):
        score += A[-i-1] + A[-i-2] -2 * A[0]
        A.pop(0)
    score += A[-1] - A[0]
    print(score)

Submission status: Wrong Answer

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

A.sort()

if N == 2:
    print(abs(A[0]-A[1]))
    sys.exit()

if N == 3:
    print(A[2]+A[1]-2*A[0])
    sys.exit()

score = 0
if N % 2 == 1:
    for i in range(int((N-3)/2)):
        score += A[-i-1] + A[-i-2] - 2 * A[0]
        A.pop(0)
    score += A[-1] + A[2] - A[0] - A[1]
    print(score)

if N % 2 == 0:
    for i in range(int((N-2)/2)):
        score += A[-i-1] + A[-i-2] -2 * A[0]
        A.pop(0)
    score += A[-1] - A[0]
    print(score)

Submission status: Wrong Answer

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

A.sort()

if N == 2:
    print(abs(A[0]-A[1]))
    sys.exit()

if N == 3:
    print(A[2]+A[1]-2*A[0])
    sys.exit()

score = 0
if N % 2 == 1:
    for i in range(int((N-3)/2)):
        score += A[-i-1] + A[-i-2] - 2 * A[0]
        A.pop(0)
    score += A[3] + A[2] - A[0] - A[1]
    print(score)

if N % 2 == 0:
    for i in range(int((N-2)/2)):
        score += A[-i-1] + A[-i-2] -2 * A[0]
        A.pop(0)
    score += A[-1] - A[0]
    print(score)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(int(input()) for _ in range(N))
A.sort()
l = [A.pop(0)]
b = 1
while A:
    if b == 1 and len(A)>=2:
        l.insert(0,A.pop())
        l.insert(len(l),A.pop())
        b *= -1
    if b == -1 and len(A)>=2:
        l.insert(0,A.pop(0))
        l.insert(len(l),A.pop(0))
        b *=-1
    if b == 1 and len(A) == 1:
        l.insert(0,A.pop())
        break
    if b == -1 and len(A) == 1:
        l.insert(0,A.pop(0))
        break
ans = 0
for i in range(1,N):
    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()

nh = int((n-1)/2)
a1 = a[1:nh+1]
a2 = a[nh+1:]
a2.reverse()

i1 = 0
i2 = 0
ans_list = [a[0]]
i = 0
f = 0
if len(a1) == 1 and len(a2) == 1:
    ans_list = a1 + ans_list + a2
else:
    while 1:
        if i%2 == 0:
            if len(a2[i2:])-2 == -1:    
                ans_list = [a2[i2]] + ans_list
                f += 1
            else:
                ans_list = [a2[i2]] + ans_list + [a2[i2+1]]
                f += 2
            i2 += 2
        else:
            if len(a1[i1:])-2 == -1:    
                ans_list = [a1[i1]] + ans_list
                f += 1
            else:
                ans_list = [a1[i1]] + ans_list + [a1[i1+1]]
                f += 2
            i1 += 2
        i += 1
        if f == len(a) -1:
            break
ans = 0
for i in range(len(ans_list)-1):
    ans += abs(ans_list[i] - ans_list[i+1])
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = [int(input()) for i in range(N)]
    A.sort()
    BIG, SMALL = -1, 0
    big_index = N-1
    small_index = 0

    left = A[big_index]
    left_sign = BIG
    right = A[big_index]
    right_sign = BIG
    big_index-=1
    result = 0
    for i in range(1, N):
        val1, val2 = 0, 0
        s = A[small_index]
        b = A[big_index]
        against1, against2 = 0, 0
        if left_sign == BIG:
            val1 = abs(left - s)
            against1 = SMALL
        else:
            val1 = abs(left - b)
            against1 = BIG
        if right_sign == BIG:
            val2 = abs(right - s)
            against2 = SMALL
        else:
            val2 = abs(right - b)
            against2 = BIG
        if val1 > val2:
            result += val1
            if against1 == BIG:
                left = b
                left_sign = against1
                print(b)
                big_index -= 1
            else:
                left = s
                left_sign = against1
                print(s)
                small_index += 1
        else:
            result += val2
            if against2 == BIG:
                right = b
                right_sign = against2
                print(b)
                big_index -= 1
            else:
                right = s
                right_sign = against2
                print(s)
                small_index += 1
    print(result)



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = [int(input()) for i in range(N)]
    A.sort()
    BIG, SMALL = -1, 0
    left = A[BIG]
    left_sign = BIG
    right = A[BIG]
    right_sign = BIG
    del A[BIG]
    result = 0
    for i in range(1, N):
        val1, val2 = 0, 0
        s = A[SMALL]
        b = A[BIG]
        against1, against2 = 0, 0
        if left_sign == BIG:
            val1 = abs(left - s)
            against1 = SMALL
        else:
            val1 = abs(left - b)
            against1 = BIG
        if right_sign == BIG:
            val2 = abs(right - s)
            against2 = SMALL
        else:
            val2 = abs(right - b)
            against2 = BIG
        if val1 > val2:
            result += val1
            left = A[against1]
            left_sign = against1
            print(A[against1])
            del A[against1]
        else:
            result += val2
            right = A[against2]
            right_sign = against2
            print(A[against2])
            del A[against2]
    print(result)



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
L = []
S = 0
K = 0
for i in range(N):
  L.append(int(input()))
L.sort()
print(L)
if N == 2:
  print(abs(L[0]-L[1]))
elif N == 3:
  print(max(abs(L[2]+L[1]-2*L[0]),abs(2*L[2]-L[1]-L[0])))
elif N == 4:
  print(abs(2*L[3]+L[2]-L[1]-2*L[0]))
elif N%2 == 0:
  for i in range((N-2)//2):
    S += 2*(L[N-i-1]-L[i])
  S += L[N//2]-L[(N-2)//2]
  print(S)
else:
  for i in range((N-3)//2):
    S += 2*L[N-i-1]
  for j in range(N//2):
    S -= 2*L[j]
  S += L[N//2]+L[N//2+1]
  for k in range(N//2):
    K += L[N-k-1]*2
  for m in range((N-3)//2):
    K -= 2*L[m]
  K -= (L[N//2]+L[N//2-1])
  print(max(S,K))

Submission status: Wrong Answer

Solution code:
a = int(input())
ar = []
for i in range(a):
    l = int(input())
    ar.append(l)
ar.sort()
br = ar[::-1]
if a == 3:
    print((max(ar)-min(ar)) + (ar[1]-min(ar)))
    exit()
cr = []
dr = []
for i in range(a//2):
    cr.append(br[i])
for i in range(a-a//2):
    dr.append(ar[i])
dr.insert(0,max(dr))
del dr[len(dr)-1]
er = []
i = 0
while True:
    er.append(dr[i])
    if len(er) == a:
        break
    er.append(cr[i])
    if len(er) == a:
        break
    i += 1
count = 0
for i in range(a-1):
    count += abs(er[i]-er[i+1])
print(count)

Submission status: Wrong Answer

Solution code:
a = int(input())
ar = []
for i in range(a):
    l = int(input())
    ar.append(l)
ar.sort()
br = ar[::-1]
if a == 3:
    print((max(ar)-min(ar)) + (ar[1]-min(ar)))
    exit()
cr = []
dr = []
for i in range(a//2):
    cr.append(br[i])
for i in range(a-a//2):
    dr.append(ar[i])
dr.insert(0,max(dr))
del dr[len(dr)-1]
print(ar)
print(br)
print(cr)
print(dr)
er = []
i = 0
while True:
    er.append(dr[i])
    if len(er) == a:
        break
    er.append(cr[i])
    if len(er) == a:
        break
    i += 1
count = 0
for i in range(a-1):
    count += abs(er[i]-er[i+1])
print(count)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
import copy
import math
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 = []
    for i in range(N):
        input_raw = read_func().strip().split()
        A.append(long(input_raw[0]))

    A.sort()

    B = deque([])
    is_back = True
    for i in range(N):
        if len(B) == 0:
            B.append(A[i])
        else:
            if is_back == False:
                x = A[i/2]
            else:
                x = A[len(A) - 1 - i/2]

            if abs(B[0] - x) > abs(B[len(B) - 1] - x):
                B.appendleft(x)
            elif abs(B[0] - x) < abs(B[len(B) - 1] - x):
                B.append(x)
            else:
                if is_back == False:
                    if B[0] <= B[len(B) - 1]:
                        B.append(x)
                    else:
                        B.appendleft(x)
                else:
                    if B[0] >= B[len(B) - 1]:
                        B.append(x)
                    else:
                        B.appendleft(x)
        if is_back == False:
            is_back = True
        else:
            is_back = False
    score1 = 0L
    for i in range(N -1):
        score1 += abs(B[i] - B[i + 1])


    A.sort(reverse = True)

    B = deque([])
    is_back = True
    for i in range(N):
        if len(B) == 0:
            B.append(A[i])
        else:
            if is_back == False:
                x = A[i/2]
            else:
                x = A[len(A) - 1 - i/2]

            if abs(B[0] - x) > abs(B[len(B) - 1] - x):
                B.appendleft(x)
            elif abs(B[0] - x) < abs(B[len(B) - 1] - x):
                B.append(x)
            else:
                if is_back == True:
                    if B[0] < B[len(B) - 1]:
                        B.append(x)
                    else:
                        B.appendleft(x)
                else:
                    if B[0] > B[len(B) - 1]:
                        B.append(x)
                    else:
                        B.appendleft(x)
        if is_back == False:
            is_back = True
        else:
            is_back = False
    score2 = 0L
    for i in range(N -1):
        score2 += abs(B[i] - B[i + 1])

    print max(score1, score2)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
import copy
import math
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 = []
    for i in range(N):
        input_raw = read_func().strip().split()
        A.append(long(input_raw[0]))

    A.sort()

    B = deque([])
    is_back = True
    for i in range(N):
        if len(B) == 0:
            B.append(A[i])
        else:
            if is_back == False:
                x = A[i/2]
            else:
                x = A[len(A) - 1 - i/2]

            if abs(B[0] - x) > abs(B[len(B) - 1] - x):
                B.appendleft(x)
            elif abs(B[0] - x) < abs(B[len(B) - 1] - x):
                B.append(x)
            else:
                if is_back == False:
                    if B[0] <= B[len(B) - 1]:
                        B.append(x)
                    else:
                        B.appendleft(x)
                else:
                    if B[0] >= B[len(B) - 1]:
                        B.append(x)
                    else:
                        B.appendleft(x)
        if is_back == False:
            is_back = True
        else:
            is_back = False
    score1 = 0L
    for i in range(N -1):
        score1 += abs(B[i] - B[i + 1])


    A.sort(reverse = True)

    B = deque([])
    is_back = False
    for i in range(N):
        if len(B) == 0:
            B.append(A[i])
        else:
            if is_back == False:
                x = A[i/2]
            else:
                x = A[len(A) - 1 - i/2]

            if abs(B[0] - x) > abs(B[len(B) - 1] - x):
                B.appendleft(x)
            elif abs(B[0] - x) < abs(B[len(B) - 1] - x):
                B.append(x)
            else:
                if is_back == True:
                    if B[0] < B[len(B) - 1]:
                        B.append(x)
                    else:
                        B.appendleft(x)
                else:
                    if B[0] > B[len(B) - 1]:
                        B.append(x)
                    else:
                        B.appendleft(x)
        if is_back == False:
            is_back = True
        else:
            is_back = False
    score2 = 0L
    for i in range(N -1):
        score2 += abs(B[i] - B[i + 1])

    print max(score1, score2)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
import copy
import math
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 = []
    for i in range(N):
        input_raw = read_func().strip().split()
        A.append(long(input_raw[0]))

    A.sort()

    B = deque([])
    is_back = False
    for i in range(N):
        if len(B) == 0:
            B.append(A[i])
        else:
            if is_back == False:
                x = A[i/2]
            else:
                x = A[len(A) - 1 - i/2]

            if abs(B[0] - x) > abs(B[len(B) - 1] - x):
                B.appendleft(x)
            elif abs(B[0] - x) < abs(B[len(B) - 1] - x):
                B.append(x)
            else:
                if is_back == False:
                    if B[0] <= B[len(B) - 1]:
                        B.append(x)
                    else:
                        B.appendleft(x)
                else:
                    if B[0] >= B[len(B) - 1]:
                        B.append(x)
                    else:
                        B.appendleft(x)
        if is_back == False:
            is_back = True
        else:
            is_back = False
    score1 = 0L
    for i in range(N -1):
        score1 += abs(B[i] - B[i + 1])


    A.sort(reverse = True)

    B = deque([])
    is_back = True
    for i in range(N):
        if len(B) == 0:
            B.append(A[i])
        else:
            if is_back == False:
                x = A[i/2]
            else:
                x = A[len(A) - 1 - i/2]

            if abs(B[0] - x) > abs(B[len(B) - 1] - x):
                B.appendleft(x)
            elif abs(B[0] - x) < abs(B[len(B) - 1] - x):
                B.append(x)
            else:
                if is_back == True:
                    if B[0] < B[len(B) - 1]:
                        B.append(x)
                    else:
                        B.appendleft(x)
                else:
                    if B[0] > B[len(B) - 1]:
                        B.append(x)
                    else:
                        B.appendleft(x)
        if is_back == False:
            is_back = True
        else:
            is_back = False
    score2 = 0L
    for i in range(N -1):
        score2 += abs(B[i] - B[i + 1])

    print max(score1, score2)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
import copy
import math
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 = []
    for i in range(N):
        input_raw = read_func().strip().split()
        A.append(long(input_raw[0]))

    A.sort()

    B = deque([])
    is_back = False
    for i in range(N):
        if len(B) == 0:
            B.append(A[i])
        else:
            if is_back == False:
                x = A[i/2]
            else:
                x = A[len(A) - 1 - i/2]

            if abs(B[0] - x) > abs(B[len(B) - 1] - x):
                B.appendleft(x)
            elif abs(B[0] - x) < abs(B[len(B) - 1] - x):
                B.append(x)
            else:
                if is_back == False:
                    if B[0] <= B[len(B) - 1]:
                        B.append(x)
                    else:
                        B.appendleft(x)
                else:
                    if B[0] >= B[len(B) - 1]:
                        B.append(x)
                    else:
                        B.appendleft(x)
        if is_back == False:
            is_back = True
        else:
            is_back = False
    score1 = 0L
    for i in range(N -1):
        score1 += abs(B[i] - B[i + 1])


    A.sort(reverse = True)

    B = deque([])
    is_back = False
    for i in range(N):
        if len(B) == 0:
            B.append(A[i])
        else:
            if is_back == False:
                x = A[i/2]
            else:
                x = A[len(A) - 1 - i/2]

            if abs(B[0] - x) > abs(B[len(B) - 1] - x):
                B.appendleft(x)
            elif abs(B[0] - x) < abs(B[len(B) - 1] - x):
                B.append(x)
            else:
                if is_back == True:
                    if B[0] <= B[len(B) - 1]:
                        B.append(x)
                    else:
                        B.appendleft(x)
                else:
                    if B[0] >= B[len(B) - 1]:
                        B.append(x)
                    else:
                        B.appendleft(x)
        if is_back == False:
            is_back = True
        else:
            is_back = False
    score2 = 0L
    for i in range(N -1):
        score2 += abs(B[i] - B[i + 1])

    print max(score1, score1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
import copy
import math
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 = []
    for i in range(N):
        input_raw = read_func().strip().split()
        A.append(long(input_raw[0]))

    A.sort()

    B = deque([])
    is_back = False
    for i in range(N):
        if len(B) == 0:
            B.append(A[i])
        else:
            if is_back == False:
                x = A[i/2]
            else:
                x = A[len(A) - 1 - i/2]

            if abs(B[0] - x) > abs(B[len(B) - 1] - x):
                B.appendleft(x)
            elif abs(B[0] - x) < abs(B[len(B) - 1] - x):
                B.append(x)
            else:
                if is_back == False:
                    if B[0] <= B[len(B) - 1]:
                        B.append(x)
                    else:
                        B.appendleft(x)
                else:
                    if B[0] >= B[len(B) - 1]:
                        B.append(x)
                    else:
                        B.appendleft(x)
        if is_back == False:
            is_back = True
        else:
            is_back = False
    score1 = 0L
    for i in range(N -1):
        score1 += abs(B[i] - B[i + 1])


    A.sort(reverse = True)

    B = deque([])
    is_back = False
    for i in range(N):
        if len(B) == 0:
            B.append(A[i])
        else:
            if is_back == False:
                x = A[i/2]
            else:
                x = A[len(A) - 1 - i/2]

            if abs(B[0] - x) > abs(B[len(B) - 1] - x):
                B.appendleft(x)
            elif abs(B[0] - x) < abs(B[len(B) - 1] - x):
                B.append(x)
            else:
                if is_back == True:
                    if B[0] <= B[len(B) - 1]:
                        B.append(x)
                    else:
                        B.appendleft(x)
                else:
                    if B[0] >= B[len(B) - 1]:
                        B.append(x)
                    else:
                        B.appendleft(x)
        if is_back == False:
            is_back = True
        else:
            is_back = False
    score2 = 0L
    for i in range(N -1):
        score2 += abs(B[i] - B[i + 1])

    print max(score1, score2)


if __name__ == '__main__':
    main()