from collections import Counter def main(): N = int(input()) Y = tuple(map(int, input().split())) if N == 1: print(Y[0]) return elif N == 2: print(max(Y) - min(Y)) return else: c = Counter(Y) dist = float('inf') for i in c.keys(): n = 0 for j, k in c.items(): n += abs(i - j) * k dist = min(dist, n) print(dist) main()