if __name__=='__main__': n = int(input()) q = list(map(int,input().split())) q.sort() res = 0 for i in range(n): res += abs(q[i] - q[n//2]) print(res)