# -*- coding: utf-8 -*- """ No.609 Noelちゃんと星々 https://yukicoder.me/problems/no/609 """ import sys from sys import stdin input = stdin.readline def calc_score(n, stars): res = [abs(x - n) for x in stars] return sum(res) def solve(stars): ans = float('inf') ub = max(stars) lb = min(stars) ub_score = calc_score(ub, stars) lb_score = calc_score(lb, stars) while ub > lb + 1: mid = (ub + lb) // 2 mid_score = calc_score(mid, stars) if ub_score >= mid_score: ub = mid ub_score = mid_score elif lb_score >= mid_score: lb = mid lb_score = mid_score return min(ub_score, lb_score) def main(args): N = int(input()) stars = [int(x) for x in input().split()] ans = solve(stars) print(ans) if __name__ == '__main__': main(sys.argv[1:])