n = int(input()) star_list = sorted([ int(v) for v in input().split() ]) maxstar, minstar = max(star_list) , min(star_list) star_list2 = [sum([abs(star_list[j] - i) for j in range(n)]) for i in star_list] print(min(star_list2))