#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 = """85 # -621257465 -621700071 977319124 280693994 413586869 -61854404 -534042095 -754504903 661984431 -642730103 -75092880 394735278 304098754 -462638028 883135080 -41710794 -583480228 -646315301 436933424 -718358057 948871648 116574382 329536301 -892018495 771484877 736879308 510154507 687739912 -812729582 463133064 -131611979 253325107 -357532550 -989477639 -279306846 -569562238 -384581945 -970620976 -614291340 -862786193 -942473729 902523321 278668416 960758681 995172191 409913148 -487787364 -790365768 -199558944 -25846432 211000403 -713532699 834491442 -36591465 358991169 -890258022 -946328742 520894863 419573096 -804935191 544242503 127794285 -670671090 -648557016 409941532 438323505 -714378773 -55763926 -221353249 -175289922 587518468 849584736 720867521 -880338287 -717151732 -886256585 -881253252 336288823 -151364618 -447955247 -327329424 -912220998 793025426 -449536000 21687996 # """ # sys.stdin = io.StringIO(_INPUT) def main(): N = int(input()) Y = sorted(list(map(int, input().split()))) # 累積和 S = [0] + list(itertools.accumulate(Y)) d_min = 10**20 for k in range(1, N): # Y[:k] と Y[k:] に分割 i1 = k//2 y1 = Y[i1] i2 = k + (N-k)//2 y2 = Y[i2] if y1 == y2: d = 1 else: 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()