N = int(input()) Y = list(map(int, input().split())) Y.sort() if Y[0] == Y[-1]: print(1) else: lt = 1 rt = N - 1 lsum = 0 rsum = 0 for i in range(lt): lsum += abs(Y[i] - Y[lt // 2]) for i in range(rt): rsum += abs(Y[~i] - Y[~(rt // 2)]) res = lsum + rsum for i in range(2, N): lt = i rt = N - i + 1 lsum += abs(Y[lt - 1] - Y[lt // 2]) lsum += (Y[lt // 2] - Y[(lt - 1) // 2]) * (lt // 2) lsum -= (Y[lt // 2] - Y[(lt - 1) // 2]) * (lt - 1 - lt // 2) rsum -= abs(Y[~(rt - 1)] - Y[~(rt // 2)]) rsum += (Y[~(rt // 2)] - Y[~((rt - 1) // 2)]) * (rt // 2) rsum -= (Y[~(rt // 2)] - Y[~((rt - 1) // 2)]) * (rt - 1 - rt // 2) res = min(res, lsum + rsum) print(res)