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).most_common() if len(c) == 2: ref = c[0][0] else: ref = sorted(c)[1:-1][0][0] dist = 0 for i, j in c: dist += abs(ref - i) * j print(dist) main()