import java.util.*; import java.io.*; public class Main { public static void main (String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String[] line = br.readLine().split(" ", n); int[] positions = new int[n]; for (int i = 0; i < n; i++) { positions[i] = Integer.parseInt(line[i]); } Arrays.sort(positions); if (positions[0] == positions[n - 1]) { System.out.println(1); return; } long[] sums = new long[n]; sums[0] = positions[0]; for (int i = 1; i < n; i++) { sums[i] = sums[i - 1] + positions[i]; } long min = Long.MAX_VALUE; for (int i = 1; i < n; i++) { int leftc = (i - 1) / 2; long tmp = positions[leftc] * (long)(leftc + 1) - sums[leftc]; tmp += sums[i - 1] - sums[leftc] - positions[leftc] * (long)(i - leftc - 1); int rightc = (n - 1 + i) / 2; tmp += positions[rightc] *(long)(rightc - i + 1) - (sums[rightc] - sums[i - 1]); tmp += sums[n - 1] - sums[rightc] - positions[rightc] * (long)(n - 1 - rightc); min = Math.min(min, tmp); } System.out.println(min); } }