# -*- coding: utf-8 -*- """ No.609 Noelちゃんと星々 https://yukicoder.me/problems/no/609 """ import sys from sys import stdin from statistics import median input = stdin.readline def solve(stars): stars.sort() m = median(stars) res = [abs(x - m) for x in stars] return sum(res) def main(args): _ = input() stars = [int(x) for x in input().split()] ans = solve(stars) print(ans) if __name__ == '__main__': main(sys.argv[1:])