''' 求中位数作为放置点即可 ''' n = int(input()) arr = list(map(int, input().split())) arr.sort() mid_val = arr[len(arr)//2] print(sum( map(lambda x: abs(x-mid_val), arr )))