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: Accepted

Solution code:
#!/usr/bin/env python

from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)

INF = 10 ** 18
MOD = 10 ** 9 + 7

N = input()

A = []

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

if N % 2:
    pos = N / 2
    ans = 0
    for i in range(N):
        ans += abs(A[i] - A[pos]) * 2
    ans -= min(A[pos + 1] - A[pos], A[pos] - A[pos - 1])
    print ans
else:
    pos1 = N / 2 - 1
    pos2 = N / 2
    ans = (A[pos2] - A[pos1]) * (N - 1)
    for i in range(N):
        ans += min(abs(A[i] - A[pos1]), abs(A[i] - A[pos2])) * 2
    print ans

Submission status: Runtime Error

Solution code:
A, B, K = map(int,input().split())
for i in range(K):
  if K % 2 == 0:
    A = A // 2
    B += A
  else:
    B = B // 2
    A += B
print(int(A), int(B))

Submission status: Runtime Error

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

mid = n//2
if n%2 == 0 and (a[-1] - a[mid-1] > a[mid] - a[0]):
    mid -= 1

if a[-1] - a[mid] > a[mid] - a[0]:
    cur = n-1
else:
    cur = 0

l = 0
r = n-1
cnt = 0
cnt += abs(a[mid] - a[cur])

while not done:
    if cur > mid:
        if not l == mid:
            cnt += abs(a[cur]-a[l])
            cur = l
            r -= 1
        else:
            break
    elif cur < mid:
        if not r == mid:
            cnt += abs(a[r]-a[cur])
            cur = r
            l += 1
        else:
            break

print(cnt)

Submission status: Runtime Error

Solution code:
A.sort()
n = N//2
if N%2 != 0:
    ans1,ans2,ans = 0,0,0
    ar1,ar2 = A[:n],A[n+1:]
    ar = []
    for i in range(n):
        ar.append(ar2[i])
        ar.append(ar1[i])
    ar.append(A[n])
    for i in range(N-1):
        ans1 += abs(ar[i]-ar[i+1])
    ar = [A[n]]+ar[:-1]
    for i in range(N-1):
        ans2 += abs(ar[i]-ar[i+1])
    ans = max(ans1,ans2)
else:
    ans = 0
    ar1,ar2 = A[:n],A[n:]
    ar = []
    for i in range(n):
        ar.append(ar2[i])
        ar.append(ar1[i])
    for i in range(N-1):
        ans += abs(ar[i]-ar[i+1])
 
print(ans)

Submission status: Runtime Error

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

A.sort()
n = N//2
if N%2 != 0:
    ar1,ar2 = [],[]
    ans1,ans2 = 0,0

    for i in range(n):
        ar1.append(A[-1-i])
        ar1.append(A[i])
        
    for i in range(1,len(ar1)):
        ans1 += abs(ar1[i]-ar1[i-1])
    ans1 += max(abs(ar1[0]-A[n]), abs(ar1[-1]-A[n]))
    for i in range(n):
        ar2.append(A[-1-i])
        ar2.append(A[i])
        
    for i in range(1,len(ar1)):
        ans2 += abs(ar2[i]-ar2[i-1])
    ans2 += max(abs(ar2[0]-A[n]), abs(ar2[-1]-A[n]))
    ans = max(ans1,ans2)
else:
    ar = []
    ans = 0
    for i in range(n-1):
        ar.append(A[-1-i])
        ar.append(A[i])
    for i in range(1,len(ar)):
        ans += abs(ar[i]-ar[i-1])
    ans += max(abs(ar[0]-A[n])+abs(ar[-1]-A[n+1]), abs(ar[-1]-A[n])+abs(ar[0]-A[n+1]))
        
    
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
import copy
sys.setrecursionlimit(50000)
N = int(input())
d_1, d_2 = deque([]), deque([])
l = []
for i in range(N):
  l.append(int(input()))
  
l.sort()
l_1, l_2 = copy.copy(l), copy.copy(l)
d_1.append(max(l))
d_2.append(min(l))
l_1.pop()
l_2.pop(0)

def main1(d, l):
  d.appendleft(l[0])
  l.pop(0)
  if l == []:
    return d
  d.append(l[0])
  l.pop(0)
  if l == []:
    return d
  d.appendleft(l[-1])
  l.pop()
  if l == []:
    return d
  d.append(l[-1])
  l.pop()
  if l == []:
    return d
  else:
    return main1(d, l)
  
def main2(d, l):
  d.appendleft(l[-1])
  l.pop()
  if l == []:
    return d
  d.append(l[-1])
  l.pop()
  if l == []:
    return d
  d.appendleft(l[0])
  l.pop(0)
  if l == []:
    return d
  d.append(l[0])
  l.pop(0)
  if l == []:
    return d
  else:
    return main2(d, l)
  
x, y = 0, 0
a = main1(d_1, l_1)
b = main2(d_2, l_2)
for i in range(N-1):
  x += abs(a[i+1]-a[i])
  y += abs(b[i+1]-b[i])
print(max(x, y))

Submission status: Runtime Error

Solution code:
from collections import deque
import copy
N = int(input())
d_1, d_2 = deque([]), deque([])
l = []
for i in range(N):
  l.append(int(input()))
  
l.sort()
l_1, l_2 = copy.copy(l), copy.copy(l)
d_1.append(max(l))
d_2.append(min(l))
l_1.pop()
l_2.pop(0)

def main1(d, l):
  d.appendleft(l[0])
  l.pop(0)
  if l == []:
    return d
  d.append(l[0])
  l.pop(0)
  if l == []:
    return d
  d.appendleft(l[-1])
  l.pop()
  if l == []:
    return d
  d.append(l[-1])
  l.pop()
  if l == []:
    return d
  else:
    return main1(d, l)
  
def main2(d, l):
  d.appendleft(l[-1])
  l.pop()
  if l == []:
    return d
  d.append(l[-1])
  l.pop()
  if l == []:
    return d
  d.appendleft(l[0])
  l.pop(0)
  if l == []:
    return d
  d.append(l[0])
  l.pop(0)
  if l == []:
    return d
  else:
    return main2(d, l)
  
x, y = 0, 0
a = main1(d_1, l_1)
b = main2(d_2, l_2)
for i in range(N-1):
  x += abs(a[i+1]-a[i])
  y += abs(b[i+1]-b[i])
print(max(x, y))

Submission status: Runtime Error

Solution code:
import math
n = int(input())

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

A.sort()

half = math.floor(n/2)

half_A = A[half]
dif = A[half+1] - half_A*2 +A[half-1]
if (n%2 == 1) & (dif>0):
    total = sum(A[half+1:])*2 - sum(A[:half-1])*2 -A[half] - A[half-1]
elif (n % 2 == 1) & (dif<=0):
    total = sum(A[half+2:])*2 - sum(A[:half])*2 + A[half] + A[half+1]
else:

    total = sum(A[half+1:])*2 - sum(A[:half-1])*2 -A[half-1] + A[half]

print(total)

Submission status: Runtime Error

Solution code:
n = int(input())
A = [int(input()) for _ in range(n)]
A = sorted(A,reverse=True)

ans = 0
if n%2 == 1:
  i = -1
  for i in range((n-2)//2):
    ans += A[i]*2
    ans -= A[-i-1]*2    
  ans += A[i+1]
  ans += A[i+2]
  ans -= A[i+3] *2
  
  tmp = 0
  for i in range((n-2)//2):
    tmp += A[i]*2
    tmp -= A[-i-1]*2    
  tmp += A[i+1] *2
  tmp -= A[i+2]
  tmp -= A[i+3]
  ans = max(ans,tmp)
  
else:
  for i in range((n-2)//2):
    ans += 2*A[i]
    ans -= 2*A[-i-1]      
  ans += A[i+1]
  ans -= A[i+2]
  
print(ans)

Submission status: Runtime Error

Solution code:

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

l = 1
r = n-1

que = deque()
que.append(A[0])
c = 0

while l <= r:
  if r-l==0:
    if abs(que[0]-A[r]) >= abs(que[-1]-A[r]):
      que.appendleft(A[r])
    else:
      que.append(A[r])
    r-=1
  else:    
    if c%2 == 0:    
      if abs(que[0]-A[r]) >= abs(que[-1]-A[r]):
        que.appendleft(A[r])
        que.append(A[r-1])
      else:
        que.append(A[r])
        que.appendleft(A[r-1])
      r -= 2
    else:
      if abs(que[0]-A[l]) >= abs(que[-1]-A[l]):
        que.appendleft(A[l])
        que.append(A[l+1])
      else:
        que.append(A[l])
        que.appendleft(A[l+1])
      l += 2
  c += 1  

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

Submission status: Runtime Error

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

tmpl=a.popleft()
tmpr=tmpl
ans=0
i=0

while len(a)>0:
    #0 大きいほうから
    if i%4==0:
        tmp=a.pop()
        ans+=abs(tmpl-tmp)
        tmpl=tmp

    #1 大きいほうから
    elif i%4==1:
        tmp=a.pop()
        ans+=abs(tmpr-tmp)
        tmpr=tmp

    #2 小さいほうから
    elif i%4==2:
        tmp=a.popleft()
        ans+=abs(tmpl-tmp)
        tmpl=tmp


    #3 小さいほうから
    elif i%4==3:
        tmp=a.popleft()
        ans+=abs(tmpr-tmp)
        tmpr=tmp

    i+=1

res=ans

d.sort(reverse=True)
a= deque(copy.deepcopy(d))

tmpl=a.popleft()
tmpr=tmpl
ans=0
i=0

while len(a)>0:
    #0 大きいほうから
    if i%4==0:
        tmp=a.pop()
        ans+=abs(tmpl-tmp)
        tmpl=tmp

    #1 大きいほうから
    elif i%4==1:
        tmp=a.pop()
        ans+=abs(tmpr-tmp)
        tmpr=tmp

    #2 小さいほうから
    elif i%4==2:
        tmp=a.popleft()
        ans+=abs(tmpl-tmp)
        tmpl=tmp


    #3 小さいほうから
    elif i%4==3:
        tmp=a.popleft()
        ans+=abs(tmpr-tmp)
        tmpr=tmp

    i+=1

print(max(ans,res))

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(all(a));
  vector<int> p, q;
  int ans = 0;
  for (int i = 0; i < n / 2; i++) {
    p.push_back(a[i]);
    p.push_back(a[n - i - 1]);
    q.push_back(a[n - i - 1]);
    q.push_back(a[i]);
  }
  int x = 0, y = 0;
  for (int i = 1; i < n; i++) {
    x += abs(p[i] - p[i - 1]);
    y += abs(q[i] - q[i - 1]);
  }
  cout << max(x, y) << endl;
  return 0;
}

Submission status: Runtime Error

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

if N % 2 == 0:
    res = 2 * (sum(a[N // 2:]) - sum(a[:N // 2]))
    res += a[N // 2 - 1] - a[N // 2]
    print(res)
else:
    res = 2 * (sum(a[N // 2 + 1:]) - sum(a[N // 2]))
    print(res - min(a[N // 2 + 1] - a[N // 2] - a[N // 2 - 1]))

Submission status: Runtime Error

Solution code:
import sys
from collections import deque

input = sys.stdin.readline


def main():
    N = int(input())
    A = [0] * N
    for i in range(N):
        A[i] = int(input())

    A.sort()
    d = deque(A)
    min_A = d.popleft()
    max_A = d.pop()
    ans = max_A - min_A
    v1 = min_A
    v2 = max_A
    while d:
        if len(d) == 1:
            v = d.pop()
            ans += max(abs(v1 - v), abs(v2 - v))
        else:
            v_min = d[0]
            v_max = d[-1]
            diff = 0
            is_popleft = True
            if diff < abs(v1 - v_min):
                diff = abs(v1 - v_min)
                next_v1 = v_min
                next_v2 = v2
            if diff < abs(v2 - v_min):
                diff = abs(v2 - v_min)
                next_v1 = v_min
                next_v2 = v1
            if diff < abs(v1 - v_max):
                diff = abs(v1 - v_max)
                next_v1 = v_max
                next_v2 = v2
                is_popleft = False
            if diff < abs(v2 - v_max):
                diff = abs(v2 - v_max)
                next_v1 = v_max
                next_v2 = v1
                is_popleft = False
            ans += diff
            v1, v2 = next_v1, next_v2
            if is_popleft:
                d.popleft()
            else:
                d.pop()

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    N, *A = map(int, open(0))
    A.sort()
    ans = 0
    if N % 2 == 1:
        ans += sum(A[N//2:])*2 - sum(A[:N//2])*2
        ans -= A[N//2]+A[N//2+1]
    else:
        ans += sum(A[N//2:])*2 - sum(A[:N//2])*2
        ans -= A[N//2]-A[N//2-1]
    

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

    print(max(ans,ans2))

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
from collections import deque
N = int(input())
A = deque(sorted([int(input()) for _ in range(N)]))
que = deque([])
que.append(A.popleft())
while A:
    if turn == 1:
        a = A.pop()
        if abs(a-que[0])<=abs(a-que[-1]):
            que.append(a)
        else:
            que.appendleft(a)
        if A:
            a = A.pop()
            if abs(a-que[0])<=abs(a-que[-1]):
                que.append(a)
            else:
                que.appendleft(a)
    else:
        a = A.popleft()
        if abs(a-que[0])<=abs(a-que[-1]):
            que.append(a)
        else:
            que.appendleft(a)
        if A:
            a = A.popleft()
            if abs(a-que[0])<=abs(a-que[-1]):
                que.append(a)
            else:
                que.appendleft(a)
    turn = 1-turn
cnt = 0
for i in range(1,N):
    cnt += abs(que[i]-que[i-1])
print(cnt)

Submission status: Runtime Error

Solution code:
from collections import deque
N = int(input())
A = deque(sorted([int(input()) for _ in range(N)]))
que = deque([])
que.append(A.popleft())
while A:
    if turn == 1:
        a = A.pop()
        if abs(a-que[0])<=abs(a-que[-1]):
                que.append(a)
            else:
                que.appendleft(a)
        if A:
            a = A.pop()
            if abs(a-que[0])<=abs(a-que[-1]):
                que.append(a)
            else:
                que.appendleft(a)
    else:
        a = A.popleft()
        if abs(a-que[0])<=abs(a-que[-1]):
                que.append(a)
            else:
                que.appendleft(a)
        if A:
            a = A.popleft()
            if abs(a-que[0])<=abs(a-que[-1]):
                que.append(a)
            else:
                que.appendleft(a)
    turn = 1-turn
cnt = 0
for i in range(1,N):
    cnt += abs(que[i]-que[i-1])
print(cnt)

Submission status: Runtime Error

Solution code:
from collections import deque
N = int(input())
A = deque(sorted([int(input()) for _ in range(N)]))
que = deque([])
que.append(A.popleft())
turn = 1
while A:
    if turn == 1:
        que.appendleft(A.pop())
        if A:
            a = A.pop()
            if abs(a-q[0])<=abs(a-q[-1]):
                que.append(a)
            else:
                que.appendleft(a)
    else:
        que.appendleft(A.popleft())
        if A:
            a = A.pop()
            if abs(a-q[0])<=abs(a-q[-1]):
                que.append(a)
            else:
                que.appendleft(a)
    turn = 1-turn
cnt = 0
for i in range(1,N):
    cnt += abs(que[i]-que[i-1])
print(cnt)

Submission status: Runtime Error

Solution code:
from copy import deepcopy

n,*a = map(int,open(0).read().split())
acopy = deepcopy(a)
a.sort()
acopy.sort(reverse = True)

L = deque([])
a = deque(a)
acopy = deque(acopy)
Lcopy = deepcopy(L)

max_ = a.pop()
L.append(max_)
min_ = acopy.pop()
Lcopy.append(min_)

while a:
    if a:
        b = a.popleft()
        L.append(b)
    if a:
        c = a.popleft()
        L.appendleft(c)
    if a:
        d = a.pop()
        L.append(d)
    if a:
        e = a.pop()
        L.appendleft(e)

while acopy:
    if acopy:
        b = acopy.popleft()
        Lcopy.append(b)
    if acopy:
        c = acopy.popleft()
        Lcopy.appendleft(c)
    if acopy:
        d = acopy.pop()
        Lcopy.append(d)
    if acopy:
        e = acopy.pop()
        Lcopy.appendleft(e)
ans1,ans2 = 0,0
#print(L,Lcopy)
for i in range(len(L)-1):
    ans1 +=abs(L[i+1] - L[i])
for i in range(len(Lcopy)-1):
    ans2 +=abs(Lcopy[i+1] - Lcopy[i])
print(max(ans1,ans2))

Submission status: Runtime Error

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

if n % 2 == 0:
    ans1 = 2 * (sum(a[n // 2:]) - sum(a[:n // 2]))
    ans1 += a[n // 2] + a[n // 2 + 1]
    ans2 = 2 * (sum(a[n // 2 - 1:]) - sum(a[:n // 2 - 1]))
    ans2 += a[n // 2 - 1] + a[n // 2]
    print(max(ans1, ans2))
elif n % 2 == 1:
    ans = 2 * (sum(a[n // 2 + 1:]) - sum(a[:n // 2 + 1]))
    print(ans + a[n // 2] + a[n // 2 - 1])

Submission status: Runtime Error

Solution code:
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
# list(map(int, list(input()))) # スペースがない数字リストを読み込み
import math
import fractions
import sys
import bisect
import heapq  # 優先度付きキュー(最小値取り出し)
import collections
from collections import Counter
from collections import deque
import pprint

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""
def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

# a^n
def power(a,n,mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod

# n*(n-1)*...*(l+1)*l
def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n-1
    while(tmp >= l):
        a = a*tmp%mod
        tmp -= 1
    return a

inf = 10**18
mod = 10**9+7

n = ir()
a = [ir() for i in range(n)]
a.sort()
ans = [0,0]
if n%2 == 0:
    for i in range(n//2-1):
        ans[0]-=2*a[i]
    ans[0]-=a[n//2-1]
    ans[0]+=a[n//2]
    for i in range(n//2+1,n):
        ans[0]+=2*a[i]

    for i in range(n//2):
        ans[1]-=2*a[i]
    ans[1]-=a[n//2]
    ans[1]+=a[n//2+1]
    for i in range(n//2+2,n):
        ans[1]+=2*a[i]
else:
    for i in range(n//2):
        ans[0]-=2*a[i]
    ans[0]+=a[n//2]
    ans[0]+=a[n//2+1]
    for i in range(n//2+2,n):
        ans[0]+=2*a[i]

    for i in range(n//2-1):
        ans[1]-=2*a[i]
    ans[1]-=a[n//2-1]
    ans[1]-=a[n//2]
    for i in range(n//2+1,n):
        ans[1]+=2*a[i]
print(max(ans))

Submission status: Runtime Error

Solution code:
from collections import deque

n = int(input())
a = sorted(int(input()) for _ in range(n))
q = deque(a)
l = q.popleft()
r = q.pop()
ans = abs(l - r)
while q:
    if flag == 0:
        x = q.popleft()
        flag = 1
    else:
        x = q.pop()
        flag = 0
    if abs(l - x) >= abs(x - r):
        ans += abs(l - x)
        l = x
    else:
        ans += abs(x - r)
        r = x
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
n = int(input())
A = []
for _ in range(n):
    A.append(int(input()))
dq = deque(sorted(A))
ans = deque([dq.popleft()])
ans.append(dq.pop())
ans.appendleft(dq.pop())
flag = True
while dq:
    if flag:
        ans.append(dq.popleft())
        if dq:
            ans.appendleft(dq.popleft())
            flag = False
    else:
        ans.append(dq.pop())
        if dq:
            ans.appendleft(dq.pop())
            flag = True
B = list(ans)
s = 0
for i in range(n-1):
    s += abs(B[i+1]-B[i])
print(s)

Submission status: Runtime Error

Solution code:
from collections import deque

N = int(input())
A = deque(sorted([int(input()) for i in range(N)]))
B = A.copy()
ans = deque([A.popleft()])
while A:
    if A:
        ans.appendleft(A.pop())
    if A:
        ans.append(A.pop())
    if A:
        ans.appendleft(A.popleft())
    if A:
        ans.append(A.popleft())
res_1 = 0
ans = list(ans)
for i in range(N - 1):
    res_1 += abs(ans[i] - ans[i + 1])
ans = deque([B.pop()])
while B:
    if B:
        ans.appendleft(B.popleft())
    if B:
        ans.append(B.popleft())
    if B:
        ans.appendleft(B.pop())
    if B:
        ans.append(B.pop())
res_2 = 0
ans = list(ans)
for i in range(N - 1):
    res_2 += abs(ans[i] - ans[i + 1])
print(max(res_1, res_2))

Submission status: Runtime Error

Solution code:
import bisect
from collections import deque

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

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

    a.sort()
    d = deque(a)
    res = 0
    t1 = d.popleft()
    t = 1
    first = True
    #真ん中に一番小さいのその両隣に一番大きいやつその一番大きいやつの隣に小さいの→大きいの→小さいの....


    while len(d)>=2:
        if t==1:
            d1 = d.pop()
            d2 = d.pop()
            if first:
                first = False
                res += (d1-t1+d2-t1)
            else:
                res += d1-t2
                res += d2-t3
        else:
            t2 = d.popleft()
            t3 = d.popleft()
            res += d1 - t2
            res += d2 - t3

        t = t * -1
    if(len(d)>0):
        if t==1:
            res += abs(d.popleft()-min(t1,t2))
        else:
            res += abs(d.popleft()-max(d1,d2))
    print(res)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from collections import Counter
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a
n, p = map(int, input().split())
c = Counter(prime_factorize(p))
ans = 1
for i in c.most_common():
    ans *= i[0] ** (i[1] // n)
print(ans)

Submission status: Runtime Error

Solution code:
def tenka18beg_c():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    A_desc = sorted(A, reverse=True)

    if N % 2 == 1:
        tot1, tot2 = 0, 0
        for i, a in enumerate(A_desc):
            if i < N//2 - 1:
                tot1 += 2*a
                tot2 += 2*a
            elif i == N//2 - 1:
                tot1 += a
                tot2 += 2*a
            elif i == N//2:
                tot1 += a
                tot2 -= a
            elif i == N//2 + 1:
                tot1 -= 2*a
                tot2 -= a
            elif N//2 + 1 < i:
                tot1 -= 2*a
                tot2 -= 2*a
        ans = max(ans, max(tot1, tot2))
    else:
        tot = 0
        for i, a in enumerate(A_desc):
            if i < N//2 - 1:
                tot += 2*a
            elif i == N//2 - 1:
                tot += a
            elif i == N//2:
                tot -= a
            elif N//2 < i:
                tot -= 2*a
        ans = tot
    print(ans)

tenka18beg_c()

Submission status: Runtime Error

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

a=sorted(a)
turn=(n-1)//2
pre=a[0]*2
ans=0
for i in range(turn):
    if i%2==0:
        p=a[n-i-1]
        q=a[n-i-2]
        new=p+q
        ans += new-pre
        m=p-a[n-i-3]
    else:
        p=a[i]
        q=a[i+1]
        new=p+q
        ans += pre-new
        m=a[i+2]-p
    pre=new

if n%2==0:
    ans += m

a=sorted(a,reverse=True)
pre=a[0]*2
ans1=0
for i in range(turn):
    if i%2==0:
        p=a[n-i-1]
        q=a[n-i-2]
        new=p+q
        ans1 += pre-new
        m=a[n-i-3]-p
    else:
        p=a[i]
        q=a[i+1]
        new=p+q
        ans1 += new-pre
        m=p-a[i+2]
    pre=new

if n%2==0:
    ans1 += m

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

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
N = int(input())
a = sorted(int(input()) for _ in [0]*N)
n = N//2 - 1
print(2*sum(a[n+2:]) - 2*sum(a[:n]) + a[n+1] - a[n] - [0, min(a[n]+a[n+2], 2*a[n+1])][N%2])

Submission status: Runtime Error

Solution code:
N,*a=map(int,open(0));n=N//2-1;a.sort();print(2*sum(a[n+2:])-2*sum(a[:n])+a[n+1]-a[n]-[0,min(a[n]+a[n+2],2*a[n+1])][N%2])

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
N, *a = map(int, open(0))
n = N//2 - 1
a.sort()
print(2*sum(a[n+2:]) - 2*sum(a[:n]) + a[n+1] - a[n] - [0, min(a[n]+a[n+2], 2*a[n+1])][N%2])

Submission status: Runtime Error

Solution code:
n,*a=map(int,open(0));n=n//2-1;a.sort();print(2*sum(a[n+2:])-2*sum(a[:n])+a[n+1]-a[n]-[0,min(a[n]+a[n+2],2*a[n+1])][n%2])

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
n, *a = map(int, open(0))
n = n//2 - 1;a.sort()
print(2*sum(a[n+2:]) - 2*sum(a[:n]) + a[n+1] - a[n] - [0, min(a[n]+a[n+2], 2*a[n+1])][n%2])

Submission status: Runtime Error

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = [INT() for _ in range(N)]
A.sort()
T = A.copy()
T = deque(T)

l = deque([])
l.append(T.popleft())

while T:
	l.appendleft(T.pop())
	if not T:
		break
	l.append(T.pop())
	if not T:
		break
	l.appendleft(T.popleft())
	if not T:
		break
	l.append(T.popleft())

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

T = A.copy()
T = deque(T)
l = deque([])
l.append(T.pop())
while T:
	l.appendleft(T.popleft())
	if not T:
		break
	l.append(T.popleft())
	if not T:
		break
	l.appendleft(T.pop())
	if not T:
		break
	l.append(T.pop())

l = list(l)
tmp = 0
for i in range(1, N):
	tmp += abs(l[i-1]-l[i])

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

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

n = int(input())
a = list(int(input()) for i in range(n))

a.sort()
if n%2==0:
    start = n//2-1
    left = 0
    right = n-1
    ans = abs(a[right]- a[start])
    while start != left:
        ans+=abs(a[left]-a[right])
        right -= 1
        ans+=abs(a[right]-a[left])
        left += 1

    print(ans)

if n%2==1:
    start = n//2
    left = 0
    right = n-1
    
    ans0 = abs(a[right]- a[start])
    while True:
        ans0+=abs(a[left]-a[right])
        right -= 1
        if start == right: break
        ans0+=abs(a[right]-a[left])
        left += 1
        
    ans1 = abs(a[left]- a[start])
    while True:
        ans1+=abs(a[right]-a[left])
        right -= 1
        if start == right: break
        ans1+=abs(a[left]-a[right])
        left += 1

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

Submission status: Runtime Error

Solution code:
import sys
from collections import deque

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


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

    for i in range(n):
        if i == 0:
            mi = A.popleft()
            B.append(mi)
        elif i == 1:
            ma = A.pop()
            B.append(ma)
        else:

            diff_left_mi = ["A.popleft()", "B.appendleft(A[0])", abs(B[0] - A[0])]
            diff_right_mi = ["A.popleft()", "B.append(A[0])", abs(B[-1] - A[0])]

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

            L = [diff_left_mi, diff_right_mi, diff_left_ma, diff_right_ma]
            ma = 0
            for l in L:
                if ma < l[2]:
                    op1 = l[0]
                    op2 = l[1]
                    ma = l[2]
            eval(op2)
            eval(op1)

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

    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
chk = []
for i in range(n):
  a = int(input())
  chk.append(a)
chk.sort()
if n%2 == 0:
  l = [i*2 for i in chk[:n//2]]
  r = [i*2 for i in chk[n//2:]]
  print(sum(r)-sum(l)+chk[n//2-1]-chk[n//2])
else:
  l = [i*2 for i in chk[:(n-1)//2]]
  r = [i*2 for i in chk[(n-1)//2+1:]]
  print(max(sum(r)-sum(l)+chk[(n-1)//2-1]-chk[(n-1)//2]),(sum(r)-sum(l)+chk[(n-1)//2]-chk[(n-1)//2+1]))

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
n = innt(input())
chk = []
for i in range(n):
  a = input()
  chk.append(a)
chk.sort()
if n%2 == 0:
  l = [i*2 for i in chk[:n//2]]
  r = [i*2 for i in chk[n//2:]]
  print(sum(r)-sum(l)+chk[n//2-1]-chk[n//2])
else:
  l = [i*2 for i in chk[:(n-1)//2]]
  r = [i*2 for i in chk[(n-1)//2+1:]]
  print(max(sum(r)-sum(l)+chk[(n-1)//2-1]-chk[(n-1)//2]),(sum(r)-sum(l)+chk[(n-1)//2]-chk[(n-1)//2+1]))

Submission status: Runtime Error

Solution code:
from collections import deque

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

A.sort()
A = deque(A)

X = A.copy()

B = deque([X.pop()])
flg = True
while X:
    if flg:
        B.append(X.popleft())
        if X:
            B.appendleft(X.popleft())
    else:
        B.append(X.pop())
        if X:
            B.appendleft(X.pop())
    flg = not flg

X = A.copy()
C = deque([X.popleft()])
flg = True
while X:
    if flg:
        C.append(X.pop())
        if X:
            C.appendleft(X.pop())
    else:
        C.append(X.popleft())
        if X:
            C.appendleft(X.popleft())
    flg = not flg

print(max(sum([abs(B[i]-B[i-1]) for i in range(1,N)]), sum([abs(C[i]-C[i-1]) for i in range(1,N)])))