#region Header #!/usr/bin/env python3 # from typing import * import sys import io import math import collections import decimal import itertools from queue import PriorityQueue import bisect import heapq def input(): return sys.stdin.readline()[:-1] sys.setrecursionlimit(1000000) #endregion # _INPUT = """5 # 0 1 6 7 8 # """ # sys.stdin = io.StringIO(_INPUT) def main(): N = int(input()) Y = sorted(list(map(int, input().split()))) # 累積和 S = [0] for i in range(N): S.append(S[-1] + Y[i]) d_min = 10**10 for k in range(1, N): # Y[:k] と Y[k:] に分割 i1 = (k-1)//2 y1 = Y[i1] i2 = k + (N-k-1)//2 y2 = Y[i2] if y1 == y2: d = 1 else: # d1 = sum(y1 - Y[i] for i in range(0, i1)) + sum(Y[i] - y1 for i in range(i1, k)) # d2 = sum(y2 - Y[i] for i in range(k, i2)) + sum(Y[i] - y2 for i in range(i2, N)) d1 = (y1 * i1 - S[i1]) + (S[k] - S[i1] - y1 * (k - i1)) d2 = (y2 * (i2 - k) - (S[i2] - S[k])) + (S[N] - S[i2] - y2 * (N - i2)) d = d1 + d2 d_min = min(d_min, d) print(d_min) if __name__ == '__main__': main()