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: Runtime Error

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)]
    if num % 2 == 0:
        data_list.sort()
    else:
        data_list.sort(reverse=True)
    data_ans = [0 for i in range(num)]

    # print(data_list)

    data_ind_righ = num - 1
    data_ind_left = 0

    data_ans[num // 2] = data_list[data_ind_righ]
    data_ind_righ -= 1

    ans_ind_righ = num // 2 + 1
    ans_ind_left = num // 2 - 1

    # print(ans_ind_left, ans_ind_righ)

    data_next(data_list, data_ans, 1, data_ind_righ, data_ind_left, ans_ind_left, ans_ind_righ, num)




if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

A.sort(reverse=True)

ans=0
while A!=[]:
  ans+=abs(A.pop(0)-A[-1])
  A[::-1]
  
print(ans)

Submission status: Runtime Error

Solution code:
print(list(map(int,input().split()))[0]%list(map(int,input().split()))[1])

Submission status: Runtime Error

Solution code:
# coding:utf-8

import sys
from collections import Counter, deque

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return input()


N = I()
A = [I() for _ in range(N)]

A.sort()
B = deque()

state = 1
for i in range(N // 2):
    if state:
        B.append(A[i])
        B.appendleft(A[-(i + 1)])
    else:
        B.append(A[-(i + 1)])
        B.appendleft(A[i])
    state ^= 1
if N % 2:
    C = deque(B)
    B.append(A[N // 2])
    C.appendleft(A[N // 2])

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

print(max(ans1, ans2))

Submission status: Runtime Error

Solution code:
from collections import deque
from itertools import accumulate


class CoeffTable:
    def __init__(self, n):
        self.n = n
        if n == 2:
            (self.p2, self.p1, self.m1, self.m2) = (0, 1, 1, 0)
        if n % 2 == 0:
            self.p1 = self.m1 = 1
            self.p2 = self.m2 = (n - 1) // 2
        else:
            self.p1 = 0
            self.m1 = 2
            self.p2 = (n - 1) // 2
            self.m2 = (n - 2) // 2
        if self.p2 + self.p1 + self.m1 + self.m2 != n:
            raise ValueError(f"n = {n}, {str(self)}")

    def reverse(self):
        (self.p2, self.p1, self.m1, self.m2) = (
            self.m2,
            self.m1,
            self.p1,
            self.p2,
        )

    def cumsum(self):
        return list(accumulate([self.p2, self.p1, self.m1, self.m2]))

    def __str__(self):
        return (
            f"(p2, p1, m1, m2) = ({self.p2}, {self.p1}, {self.m1}, {self.m2})"
        )


def main(n, seq):
    seq = sorted(seq, reverse=True)
    coeff = CoeffTable(n)
    cm = coeff.cumsum()
    ans1 = (
        2 * sum(seq[0 : cm[0]])
        + 1 * sum(seq[cm[0] : cm[1]])
        - 1 * sum(seq[cm[1] : cm[2]])
        - 2 * sum(seq[cm[2] : cm[3]])
    )
    coeff.reverse()
    rcm = coeff.cumsum()
    ans2 = (
        2 * sum(seq[0 : rcm[0]])
        + 1 * sum(seq[rcm[0] : rcm[1]])
        - 1 * sum(seq[rcm[1] : rcm[2]])
        - 2 * sum(seq[rcm[2] : rcm[3]])
    )
    ans = max(ans1, ans2)
    print(ans)
    return


if __name__ == "__main__":
    n = int(input())
    sample = []
    for _ in range(n):
        sample.append(int(input()))
    main(n, sample)

Submission status: Runtime Error

Solution code:

import statistics
import itertools

def getNext(tv, ta):
    maxv = abs(max(ta)-tv)
    minv = abs(tv-min(ta))
    if (maxv > minv):
        return max(ta)
    else:
        return min(ta)

def searchMedian(m, l):
    if m in l:
        return m
    i = 1
    while(True):
        v = m-i
        if v in l:
            return v
        v = m+i
        if v in l:
            return v
        i = i + 1

n = int(input())

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

s = 0
m = int(statistics.median(a))
fv = searchMedian(m, a)
#fv = m
c = list(a)
print(m)
c.remove(m)
next = 0
while(c):
    if (next==0):
        v = getNext(fv, c)
        if (v > fv):
            next = -1
        else:
            next = 1
    elif (next==1):
        v = max(c)
        next = -1
    elif (next==-1):
        v = min(c)
        next = 1
    s = s + abs(fv-v)
    c.remove(v)
#        print(fv,v,next)
    fv = v
print(s)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

import sys
from itertools import permutations

def load():
    nums = []
    lines = iter(sys.stdin)
    line = next(lines)
    N = int(line.strip())
    for line in lines:
        nums.append(int(line.strip()))
    return nums

def calc(nums):
    it = iter(nums)
    next(it)
    return sum( abs(x - y) for x, y in zip(nums, it) )

def main():
    nums = load()
    #
    max = 0; ans = None
    for t in permutations(nums):
        n = calc(t)
        if n > max:
            max = n
            ans = t
    #
    print(max)
    #print(ans)

def main2():
    nums = load()
    #
    max_n = max(nums)
    min_n = min(nums)
    max_v = 0; ans = None
    for t in permutations(nums):
        if t[0] < t[1]:
            if not (t[1] == max_n and t[2] == min_n):
                continue
        else:
            if not (t[1] == min_n and t[2] == max_n):
                continue
        n = calc(t)
        if n > max_v:
            max_v = n
            ans = t
    #
    print(max_v)
    #print(ans)

#main()
main2()

Submission status: Runtime Error

Solution code:
N = int(input())

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

def factorial(n):
   if n == 0: return 1
   return n * factorial(n-1)

def array_patterns(origin):
    patterns = []
    for i in range(factorial(len(origin))):
        _origin = []
        for j in range(len(origin)):
            _origin.append(origin[j])
        patterns.append(_origin)
    return patterns

diff = []
for _A in list(array_patterns(A)):
    _diff = 0
    for i in range(N - 1):
        _diff += abs(_A[i] - _A[i + 1])
    diff.append(int(_diff))

print(max(diff))

Submission status: Runtime Error

Solution code:
N = int(input())

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

def factorial(n):
   if n == 0: return 1
   return n * factorial(n-1)

def array_patterns(origin):
    patterns = []
    for i in range(factorial(len(origin))):
        _origin = ()
        for j in range(len(origin)):
            _origin = _origin + (origin[j])
        patterns.append(_origin)
    return patterns

diff = []
for _A in list(array_patterns(A, N)):
    _diff = 0
    for i in range(N - 1):
        _diff += abs(_A[i] - _A[i + 1])
    diff.append(int(_diff))

print(max(diff))

Submission status: Runtime Error

Solution code:
 
 


A,B,K=input().split()
A=int(A)
B=int(B)
K=int(K)
turn=0
while(1):
	if(turn==K):
		break
	if(turn%2==0):#高橋くん
		if(A%2==1):
			A-=1
	else:#青木くん
		if(B%2==1):
			B-=1
	turn+=1
print(str(A)+" "+str(B))

Submission status: Runtime Error

Solution code:
import numpy as np
n = int(input())
lst = []
for i in range(n):
  lst.append(int(input()))
np1 = np.array(lst)
np2 = np.array(lst).reshape(n,1)
nps = np1+np2
print(nps.max())

Submission status: Runtime Error

Solution code:
n = int(input());A = [int(input()) for _ in range(n)];A.sort();if n%2==1:print(max(2*sum(A[n-n//2:])-sum(A[n-n//2-2:n-n//2])-2*sum(A[:n-n//2-2]),-2*sum(A[:n//2])+sum(A[n//2:n//2+2])+2*sum(A[n//2+2:])));else:print(-2*sum(A[:(n-2)//2])-A[(n-2)//2]+A[(n-2)//2+1]+2*sum(A[(n-2)//2+2:]))

Submission status: Runtime Error

Solution code:
n = int(input())
A = [int(input()) for _ in range(n)]
A.sort()
if n%2==1:print(max(2*sum(x[n-n//2:])-sum(x[n-n//2-2:n-n//2])-2*sum(x[:n-n//2-2]),-2*sum(x[:n//2])+sum(x[n//2:n//2+2])+2*sum(x[n//2+2:])))
else:print(-2*sum(A[:(n-2)//2])-A[(n-2)//2]+A[(n-2)//2+1]+2*sum(A[(n-2)//2+2:]))

Submission status: Runtime Error

Solution code:
n=int(input());A=[int(input()) for _ in range(n)];A.sort();if n%2==1:print(max(2*sum(x[n-n//2:])-sum(x[n-n//2-2:n-n//2])-2*sum(x[:n-n//2-2]),-2*sum(x[:n//2])+sum(x[n//2:n//2+2])+2*sum(x[n//2+2:])));else:print(-2*sum(A[:(n-2)//2])-A[(n-2)//2]+A[(n-2)//2+1]+2*sum(A[(n-2)//2+2:]))

Submission status: Runtime Error

Solution code:
# tenkaichi c

n = int(input())
A = [int(input()) for _ in range(n)]
A.sort()

if n%2==1:
    ans1 = 2*sum(A[n//2+2:]) + sum(A[2:n//2+2]) -2*sum(A[:2])
    ans2 = 2*sum(A[n//2+1:]) - sum(A[n//2-1:n//2+1]) -2*sum(A[:n//2-1])
    print(max(ans1,ans2))
else:
    print(-2*sum(A[:(n-2)/2])-A[(n-2)/2]+A[(n-2)/2+1]+2*sum(A[(n-2)/2+2:]))

Submission status: Runtime Error

Solution code:
emp_list =[]
answer_list =[]

n = int(input())
for i in range(n):
    i = int(input())
    emp_list.append(i)
emp_list.sort()

ef min_list(the_list):
    if len(the_list) > 0:
        x = the_list.pop(0)
        return x
    else:
        pass

def max_list(the_list):
    if len(the_list) > 0:
        x = the_list.pop()
        return x
    else:
        pass

m = min_list(emp_list)
answer_list.append(m)

while emp_list != 0:
    ma = max_list(emp_list)
    answer_list.append(ma)
    if len(emp_list) == 0:
        break
    else:
        mb = max_list(emp_list)
        answer_list.insert(0,mb)
    if emp_list == 0:
        break
    else:
        na = min_list(emp_list)
        answer_list.append(na)
    if emp_list == 0:
        break
    else:
        nb = min_list(emp_list)
        answer_list.insert(0,nb)
print(answer_list)

Submission status: Runtime Error

Solution code:
def t118C():
    N = int(input())
    A = [int(input()) for i in range(N)]
    A.sort()
    AFromMinimum = A
    AFromMaxim = A[::-1]
    ans = 0
    if N % 2 == 0:
        ans = -2 * sum(AFromMinimum[:N // 2]) + 2 * sum(AFromMaxim[:N // 2]) + AFromMinimum[N // 2] - AFromMaxim[n // 2]
    else:
        c = (N - 1) // 2
        tmp1 = -2 * sum(A[:c]) + 2 * sum(A[c + 2:]) + A[c] + A[c + 1]
        tmp2 = -2 * sum(A[:c - 1]) + 2 * sum(A[c + 1:]) - A[c - 1] - A[c]
        ans = max(tmp1, tmp2)
    print(ans)
t118C()

Submission status: Runtime Error

Solution code:
ans1 = 0
ans2 = 0
if n % 2 == 1:
    for i in range((n-3)//2):
        ans1 += 2 * l[i]
    ans1 += l[(n-3)//2] + l[(n-3)//2+1]
    for i in range((n-3)//2+2, n):
        ans1 -= 2 * l[i]

    for j in range((n-1)//2):
        ans2 += 2 * l[j]
    ans2 -= l[(n-1)//2] + l[(n-1)//2+1]
    for j in range((n-1)//2+2, n):
        ans2 -= 2 * l[i]

else:
    for i in range(((n-2)//2)):
        ans1 += 2 * l[i]
    ans1 += l[(n-2)//2]
    ans1 -= l[(n-2)//2+1]
    for i in range((n-2)//2+2, n):
        ans1 -= 2 * l[i]

ans = max(ans1, ans2)
print(ans)

Submission status: Runtime Error

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

if N % 2 == 0:
    ans = 2 * (sum(A[N // 2:]) - sum(A[:N // 2])) - (A[N // 2] - A[N // -1])
else:
    ans = 2 * (sum(A[N // +1:]) - sum(A[:N // 2])) - min(A[N // 2] - A[N // 2 - 1], A[N // 2 + 1] - A[N // 2])

print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
import pandas as pd

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

if N % 2 == 0:
    ans = 2 * (sum(A[N // 2 + 1:]) - sum(A[:N // 2 - 1])) + A[N // 2] - A[N // 2 - 1]
else:
    x = 2 * (sum(A[N // 2 + 1:]) - sum(A[:N // 2 - 1])) - A[N // 2] - A[N // 2 - 1]
    y = 2 * (sum(A[N // 2 + 2:]) - sum(A[:N // 2])) + A[N // 2] + A[N // 2 + 1]
    ans = max(x, y)
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())

a = [0] * n

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

a.sort()

c = 0

if len(a) == 2:
  print(abs(a[0] - a[1]))

if len(a) == 3:
  diff = abs(a[0] - a[1]) + abs(a[1] - a[2])
  diff = max(abs(a[0] - a[2]) + abs(a[1] - a[2]), diff)
  diff = max(abs(a[0] - a[2]) + abs(a[0] - a[1]), diff)
  print(diff)

mini = 0
maxi = len(a) - 1

if len(a) > 3:
  (l, r) = (a[maxi], a[maxi - 1])
  maxi -= 2
  diff = abs(a[0] - l) + abs(a[0] - r)
  mini += 1
  for i in range(0, n - 3):
    if i % 2 == 0:
      if c < 2:
        diff += abs(a[mini] - l)
        l = a[mini]
        mini += 1
      else:
        diff += abs(a[maxi] - l)
        l = a[maxi]
        maxi -= 1
    else:
      if c < 2:
        diff += abs(a[mini] - r)
        r = a[mini]
        mini += 1
      else:
        diff += abs(a[maxi] - r)
        r = a[maxi]
        maxi -= 1
    if c < 3:
      c += 1
    else:
      c = 0
  
  maxi = len(a) - 1
  mini = 0
  (l, r) = (a[mini], a[mini + 1])
  mini += 2
  diff2 = abs(a[-1] - l) + abs(a[-1] - r)
  maxi -= 1
  for i in range(0, n - 3):
    if i % 2 == 0:
      if c > 1:
        diff2 += abs(a[mini] - l)
        l = a[mini]
        mini += 1
      else:
        diff2 += abs(a[maxi] - l)
        l = a[maxi]
        maxi -= 1
    else:
      if c > 1:
        diff2 += abs(a[mini] - r)
        r = a[mini]
        mini += 1
      else:
        diff2 += abs(a[maxi] - r)
        r = a[maxi]
        maxi -= 1
    if c < 3:
      c += 1
    else:
      c = 0

  print(max(diff, diff2))

Submission status: Runtime Error

Solution code:
def c_align(N, A):
    from collections import deque
    tmp = sorted(A)

    seq = tmp[:]
    old_check = seq.pop(-1 + N // 2) if N % 2 == 0 else seq.pop(N // 2)
    current_check = -1
    seq = deque(seq)
    ans1 = 0
    turn = 0
    while seq:
        current_check = seq.pop() if turn % 2 == 0 else seq.popleft()
        ans1 += abs(current_check - old_check)
        old_check = current_check
        turn += 1

    seq = tmp[:]
    old_check = seq.pop(1 + N // 2) if N % 2 == 0 else seq.pop(N // 2)
    current_check = -1
    seq = deque(seq)
    ans2 = 0
    turn = 0
    while seq:
        current_check = seq.popleft() if turn % 2 == 0 else seq.pop()
        ans2 += abs(current_check - old_check)
        old_check = current_check
        turn += 1

    ans = max(ans1, ans2)
    return ans

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

Submission status: Runtime Error

Solution code:
num_list = [int(input()) for i in range(int(input()))].sort()
n = len(num_list) // 2 - 1

ans = 2*sum(num_list[n+2:])-2*sum(num_list[:n])+num_list[n+1]-num_list[n]-(min(num_list[n]+num_list[n+2],2*num_list[n+1])if len(num_list)%2>0 else 0)

print(ans)

Submission status: Runtime Error

Solution code:
num_list = [int(input()) for i in range(int(input()))].sort()
n = len(num_list) // 2 - 1

ans = 2*sum(num_list[n+2:])-2*sum(num_list[:n])+num_list[n+1]-num_list[n]-(min(num_list[n]+num_list[n+2],2*num_list[n+1])if len(num_list)%2>0 else 0)

Submission status: Runtime Error

Solution code:
import time
import random
def calc(a) :
    sum = 0
    for i in range(len(a) - 1):
        sum += abs(a[i + 1] - a[i])
    return sum

def crossList(a,b):
    y = []
    if len(a) > len(b):
        for i in range(len(b)):
            y.append(a[i])
            y.append(b[i])
        y.append(a[i+1])
    elif len(a) < len(b): 
        for i in range(len(a)):
            y.append(b[i])
            y.append(a[i])
        y.append(b[i+1])
    else :
        for i in range(len(a)):
            y.append(b[i])
            y.append(a[i])
    return y

n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
a.sort()
b = []
c = []
f = []
g = []
if len(a) % 2 == 1:
    b = a[0:int((len(a) - 1)/2) ]
    c = a[int((len(a) - 1)/2) : len(a)]
else :
    b = a[0:int((len(a) )/2)  ]
    c = a[int((len(a) )/2) : len(a)]
c.reverse()
g.reverse()
d = []
e = []
h = []
j = []
forward = True;
for i in range(len(b)):
    if forward:
        d.append(b[i])
    else :
        d.insert(0,b[i])
    forward = not forward
forward = True

for i in range(len(c)):
    if forward:
        e.append(c[i])
    else :
        e.insert(0,c[i])
    forward = not forward
forward = True
h = d[:]
j = e[:]
if (len(h) % 2 == 0) :
    h.append(j.pop(-1))
else:
    h.insert(0,j.pop(0))

print(max(calc(crossList(d,e)),calc(crossList(d[::-1],e[::-1])),
          calc(crossList(d,e[::-1])),calc(crossList(d[::-1],e)),
          calc(crossList(h[::-1],j[::-1])),calc(crossList(h,j[::-1])),
          calc(crossList(h[::-1],j)),calc(crossList(h,j))))

Submission status: Runtime Error

Solution code:
import time
import random
def calc(a) :
    sum = 0
    for i in range(len(a) - 1):
        sum += abs(a[i + 1] - a[i])
    return sum

def crossList(a,b):
    y = []
    if len(a) > len(b):
        for i in range(len(b)):
            y.append(a[i])
            y.append(b[i])
        y.append(a[i+1])
    elif len(a) < len(b): 
        for i in range(len(a)):
            y.append(b[i])
            y.append(a[i])
        y.append(b[i+1])
    else :
        for i in range(len(a)):
            y.append(b[i])
            y.append(a[i])
    return y

n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
a.sort()
b = []
c = []
f = []
g = []
if len(a) % 2 == 1:
    b = a[0:int((len(a) - 1)/2) ]
    c = a[int((len(a) - 1)/2) : len(a)]
else :
    b = a[0:int((len(a) )/2)  ]
    c = a[int((len(a) )/2) : len(a)]
c.reverse()
g.reverse()
d = []
e = []
h = []
j = []
forward = True;
for i in range(len(b)):
    if forward:
        d.append(b[i])
    else :
        d.insert(0,b[i])
    forward = not forward
forward = True

for i in range(len(c)):
    if forward:
        e.append(c[i])
    else :
        e.insert(0,c[i])
    forward = not forward
forward = True
h = d[:]
j = e[:]
h.append(j.pop(-1))
print(max(calc(crossList(d,e)),calc(crossList(d[::-1],e[::-1])),
          calc(crossList(d,e[::-1])),calc(crossList(d[::-1],e)),
          calc(crossList(h[::-1],j[::-1])),calc(crossList(h,j[::-1])),
          calc(crossList(h[::-1],j)),calc(crossList(h,j))))

Submission status: Runtime Error

Solution code:
N = int(input().rstrip())
ins = list()
for _ in range(N):
    i = int(input().rstrip())
    ins.append(i)
ins.sort()
f, s = list(), list()
f.append(ins[0])
s.append(ins[0])
if len(ins) % 2 != 0:
    f.appen(ins[0])
    s.appen(ins[0])
    # for i in range(0, (len(ins) - 1) // 2, 2):
    #     f.append(ins[-1 - i])
    #     s.append(ins[-2 - i])
    #     if len(f) == (len(ins) + 1) // 2:
    #         break
    #     f.append(ins[1 + i])
    #     s.append(ins[2 + i])
    #     if len(f) == (len(ins) + 1) // 2:
    #         break
else:
    for i in range(0, (len(ins) - 2) // 2, 2):
        f.append(ins[-1 - i])
        s.append(ins[-2 - i])
        if len(f) == len(ins) // 2:
            f.append(ins[i + 1])
            break
        f.append(ins[1 + i])
        s.append(ins[2 + i])
        if len(f) == len(ins) // 2:
            f.append(ins[-i -1])
            break
sumf, sums = 0, 0
for i in range(len(f)-1):
    sumf += abs(f[i] - f[i + 1])
for i in range(len(s)-1):
    sums += abs(s[i] - s[i + 1])
print(sumf+sums)

Submission status: Runtime Error

Solution code:
N = int(input().rstrip())
ins = list()
for _ in range(N):
    i = int(input().rstrip())
    ins.append(i)
ins.sort()
f, s = list(), list()
f.append(ins[0])
s.append(ins[0])
if len(ins) % 2 != 0:
    continue
    # for i in range(0, (len(ins) - 1) // 2, 2):
    #     f.append(ins[-1 - i])
    #     s.append(ins[-2 - i])
    #     if len(f) == (len(ins) + 1) // 2:
    #         break
    #     f.append(ins[1 + i])
    #     s.append(ins[2 + i])
    #     if len(f) == (len(ins) + 1) // 2:
    #         break
else:
    for i in range(0, (len(ins) - 2) // 2, 2):
        f.append(ins[-1 - i])
        s.append(ins[-2 - i])
        if len(f) == len(ins) // 2:
            f.append(ins[i + 1])
            break
        f.append(ins[1 + i])
        s.append(ins[2 + i])
        if len(f) == len(ins) // 2:
            f.append(ins[-i -1])
            break
sumf, sums = 0, 0
for i in range(len(f)-1):
    sumf += abs(f[i] - f[i + 1])
for i in range(len(s)-1):
    sums += abs(s[i] - s[i + 1])
print(sumf+sums)

Submission status: Runtime Error

Solution code:
# coding:utf-8

n = int(input())

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

def func(sa, ba, i, flg=3):
    if flg==1:
        if i == 0:
            return ba[i] + ba[i+1] - 2*sa[0]
        elif i==len(sa)-1:
            return ba[i] - sa[i]
        else:
            return ba[i] + ba[i+1] - 2*sa[i]
    elif flg==2:
        if i==0:
            return ba[i] - sa[-1]
        elif i==len(sa)-1:
            return ba[i]+ba[i-1] -2*sa[0]
        else:
            return ba[i] + ba[i-1] - 2*sa[i]
    else:
        if i == 0:
            return ba[i] - sa[-1]
        elif i == len(sa)-1:
            return ba[i-1] - sa[-2]
        else:
            return ba[i-1]+ba[i]-2*sa[i-1]


def func1(sa, ba, k):
    num = 0
    num1 = 0
    num2 = 0
    if n%2 == 0:
        for i in range(k):
            num1 += func(sa, ba, i, 1)
        for i in range(k):
            num2 += func(sa, ba, i, 2)
        if num2 > num1:
            num = num2
        else:
            num = num1
    else:
        for i in range(k):
            num += func(sa, ba, i)
    return num


# 昇順に並べ替え
an.sort()

num = 0
if n==2:
    num = an[1]-a[0]
elif n==3:
    num = 2*an[2] - an[0] - an[1]
    num1 = an[1]+an[2] - 2*an[0]
    if num1 > num:
        num = num1
else:
    if n%2 == 0:
        k = int(n/2)
        sa = an[:k]
        ba = an[k:]
    else:
        k = int(n/2)+1
        sa = an[:k]
        ba = an[k:]    
    ba.reverse()
    num = func1(sa, ba, k)

print(num)

Submission status: Runtime Error

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

ans_list = [0] * len(A)
if len(A)%2 == 0:
    low = A[0:len(A)//2+1]
    high = A[len(A)//2+1:len(A)]
else:
    low = A[0:len(A)//2+1]
    high = A[len(A)//2+1:len(A)] 
first = low.pop(0)

ans = 0
ans += abs(first - high[0])
if len(high) > 1:
    ans += abs(first - high[1])
    for i in range(len(high)):
        ans += abs(high[i] - low[i])

    if len(low) > len(high):
        ans += abs(low[len(low)-1] - low[0])
else:
    if len(low) > 0:
        ans += abs(first - low[0])
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
A.sort()
l = []
if len(A) % 2 == 0:
  A_s = A[:int(len(A)/2)]
  A_l = A[int(len(A)/2):]
  l.append(A_s[-1])
  l.append(A_l[0])
  for i in range(1,int(len(A)/2) + 1):
    if i % 2 == 1:
      l.insert(i,A_l[-i])
      l.insert(-i-1,A_s[i-1])
    elif i % 2 == 0:
      l.insert(i,A_s[i-1])
      l.insert(-i-1,A_l[-i])
elif len(A) % 2 == 1:
  A_s = A[:int(len(A)/2)]
  A_l = A[int(len(A)/2)+1:]
  A_m = A[int(len(A)/2)]
  d = abs(A_m - A_s[-1]) - abs (A_m - A_l[0])
  if d >= 0:
    l.append(A_m)
    l.append(A_l[0])
    for i in range(1,int(len(A_s)/2)+1):
      if i % 2 == 1:
        l.insert(i,A_s[i-1])
        l.insert(-i-1,A_s[i])
      elif i % 2 == 0:
        l.insert(i,A_l[-i+1])
        l.insert(-i-1,A_l[-i])
    l.insert(int(len(A_s)/2)+1,A_l[1])
  else:
    l.append(A_s[-1])
    l.append(A_m)
    for i in range(1,int(len(A_l)/2)+1):
      if i % 2 == 1:
        l.insert(i,A_l[-i+1])
        l.insert(-i-1,A_l[-i])
      elif i % 2 == 0:
        l.insert(i,A_s[i-1])
        l.insert(-i-1,A_s[i])
    l.insert(int(len(A_l)/2)+1,A_s[-2])
l_d = [abs(l[i]-l[i+1] ) for i in range(len(l)-1)]
print(sum(l_d))

Submission status: Runtime Error

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

#N = 100000
#A = [1]*N

def sub_array(n, A):
    return [A[i] for i in range(len(A)) if i!=n]

memo = [-1]*N
def dp(a, A):
    if len(A)==1:
        return abs(a-A[0])

    if memo[N-len(A)] != -1:
        return memo(N-len(A))

    m = 0
    for i in range(len(A)):
        new = sub_array(i, A)
        r = abs(a-A[i]) + dp(A[i], new)
        m = max(m, r)

    memo[N-len(A)] = m    
    return m

r = 0
for n in range(N):
    t = dp(A[n], sub_array(n, A))
    if r < t:
        r = t

print(r)

Submission status: Runtime Error

Solution code:
import math as mt
N = int(input())
A = [int(input()) for i in range(N)]
A.sort()
#print(A)
S = 0
P2 = mt.floor(N/2 - 1)
if N % 2 == 0:
    for i in range(0,P2,1):
        S += 2 * (A[N-i-1] - A[i])
    S += A[P2+1] - A[P2]
    print(S)
elif N % 2 == 1:
    for i in range(0,P2,1):
        S += 2 * (A[N-i-1] - A[i])
    S1 = A[N-P2-1] + A[N-P2] - 2 * A[P2]
    S2 = 2 * A[N-1-P2] - A[P2] - A[P2+1]
    if S1 >= S2:
        print(S1+S)
    else:
        print(S2+S)

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
A.sort()
l = []
if len(A) % 2 == 0:
  A_s = A[:int(len(A)/2)]
  A_l = A[int(len(A)/2):]
  l.append(A_s[-1])
  l.append(A_l[0])
  for i in range(1,int(len(A)/2) + 1):
    if i % 2 == 1:
      l.insert(i,A_l[-i])
      l.insert(-i-1,A_s[i-1])
    elif i % 2 == 0:
      l.insert(i,A_s[i-1])
      l.insert(-i-1,A_l[-i])
elif len(A) % 2 == 1:
  A_s = A[:int(len(A)/2)]
  A_l = A[int(len(A)/2)+1:]
  A_m = A[int(len(A)/2)]
  d = abs(A_m - A_s[-1]) - abs (A_m - A_l[0])
  if d > 0:
    l.append(A_m)
    l.append(A_l[0])
    for i in range(1,int(len(A_s)/2)+1):
      if i % 2 == 1:
        l.insert(i,A_s[i-1])
        l.insert(-i-1,A_s[i])
      elif i % 2 == 0:
        l.insert(i,A_l[-i+1])
        l.insert(-i-1,A_l[-i])
    l.insert(int(len(A_s/2))+1,A_l[1])
  else:
    l.append(A_s[-1])
    l.append(A_m)
    for i in range(1,int(len(A_l)/2)+1):
      if i % 2 == 1:
        l.insert(i,A_l[-i+1])
        l.insert(-i-1,A_l[-i])
      elif i % 2 == 0:
        l.insert(i,A_s[i-1])
        l.insert(-i-1,A_s[i])
    l.insert(int(len(A_l)/2)+1,A_s[-2])
l_d = [abs(l[i]-l[i+1] ) for i in range(len(l)-1)]
print(sum(l_d))

Submission status: Runtime Error

Solution code:
import math as mt
N = int(input())
A = [int(input()) for i in range(N)]
A.sort()
#print(A)
S = 0
P2 = mt.floor(N/2 - 1)
if N % 2 == 0:
    for i in range(0,P2,1):
        S += 2 * (A[N-i-1] - A[i])
    S += A[P2+1] - A[P2]
    print(S)
elif N % 2 == 1:
    for i in range(0,P2,1):
        S += 2 * (A[N-i-1] - A[i])
    S1 = A[N-P2-1] + A[N-P2] - 2 * A[P2]
    S2 = 2 * A[N-1-P2] - A[P2] - A[P2+1]
    if S1 >= S2:
        print(S1)
    else:
        print(S2)

Submission status: Runtime Error

Solution code:
# 与えられた整数列をソートした後、N/2個目の数値をスタートとして、一番遠い値を取得していく。
N = int(input())
L = []
for i in range(N):
    L.append(int(input()))
L.sort()
M = L.copy()
# L = [1, 2, 3, 6, 8]
# N = 5

# 奇数個の場合
if N % 2 == 0:
    target_num = L.pop(N // 2 - 1)
    ans1 = 0  # 差の合計
    # 一番前の数値と比べる
    if target_num - L[0] > L[-1] - target_num:
        # 2番目を先頭の数値にする場合
        i = 1
    else:
        # 2番目を最後尾の数値にする場合
        i = 0

    # リストから数値がなくなるまでループする
    while len(L) > 0:
        if i % 2 == 0:
            # 一番後ろの数値との差を取得
            ans1 = ans1 + (L[-1] - target_num)
            # 一番後ろの数値をリストから削除
            target_num = L.pop(-1)
        else:
            # 一番前の数値との差を取得
            ans1 = ans1 + (target_num - L[0])
            # 一番前の数値をリストから削除
            target_num = L.pop(0)
        i += 1

L = M.copy()
target_num = L.pop(N // 2)
ans = 0 # 差の合計

# 一番前の数値と比べる
if target_num - L[0] > L[-1] - target_num:
    # 2番目を先頭の数値にする場合
    i = 1
else:
    # 2番目を最後尾の数値にする場合
    i = 0

# リストから数値がなくなるまでループする
while len(L) > 0:
    if i % 2 == 0:
        # 一番後ろの数値との差を取得
        ans = ans + (L[-1] - target_num)
        # 一番後ろの数値をリストから削除
        target_num = L.pop(-1)
    else:
        # 一番前の数値との差を取得
        ans = ans + (target_num - L[0])
        # 一番前の数値をリストから削除
        target_num = L.pop(0)
    i += 1

if ans1 != None:
    print(max(ans1, ans))
else:
    print(ans)

Submission status: Runtime Error

Solution code:
import copy
n=int(input())
tmplist=[int(input()) for _ in range(n)]

tmplist.sort()


l=[]
ia=0
id=n-1
print(id)
for i in range(n):
    if ia == id:
        l.insert(0,tmplist[ia])
    elif i % 2 == 0:
        l.append(tmplist[ia])
        ia += 1
    else:
        l.append(tmplist[id])
        id -= 1
ans = 0;
for i in range(l):
    ans -= l(i)
print(ans)

Submission status: Runtime Error

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

sorted(nums)
min_num = -1
max_num = -1

# 最大値
ans_right = nums.pop()
# 最小値
ans_left = nums.pop(0)
ans = abs(ans_right - ans_left)

while len(nums) > 2:
    num_max = nums[-1]
    num_min = nums[0]
    compare = [
        abs(num_max-ans_right),
        abs(num_max-ans_left),
        abs(num_min-ans_right),
        abs(num_min-ans_left)
    ]
    t = max(compare)
    c = compare.index(t)

    # 追加処理
    ans += t
    add_to_ans = num_max if c == 0 else num_min
    nums.remove(add_to_ans)
    if c % 2 == 0:
        ans_right = add_to_ans
    else:
        ans_left = add_to_ans

# add len(nums) == 1
print(ans + max(abs(nums[0]-ans_right),abs(nums[0]-ans_left)))

Submission status: Runtime Error

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

dif = 0
for i in range(len(a)):
    min = a[int(i/2)]
    max = a[(-int((i+1)/2)-1)]
    dif += max - min
print(dif)

Submission status: Runtime Error

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

if N%2!=0: #kisu
  manaka=int(N/2)
  if abs(num[manaka]-num[0])<abs(num[manaka]-num[N-1]):
    sum=abs(num[manaka]-num[N-1])
    for i in range(int((N-1)/2)):
      sum+=abs(num[N-1-i]-num[i])
      sum+=abs(num[i]-num[N-2-i])
    print(sum)     
  else:
    jun.append(manaka)
    sum=abs(num[manaka]-num[0])
    for i in range(int((N-1)/2)):
      sum+=abs(num[i]-num[N-1-i])
      sum+=abs(num[i+1]-num[N-1-i])
    print(sum)
else:
  manaka=int(N/2)
  sum=abs(num[manaka]-num[0])
  for i in range(manaka-1):
    sum+=abs(num[i]-num[N-1-i])
    sum+=abs(num[i+1]-num[N-1-i])
  print(sum)