#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
int q[N];
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i ++ ) cin >> q[i];
sort(q, q + n);
int mid = q[n - 1 >> 1]; //找中位数
//找中位数可以使用快速选择,时间复杂度仅为O(n)
//nth_element(q, q + n / 2, q + n);
int res = 0;
for (int i = 0; i < n; i ++ )
res += abs(q[i] - mid); //求中位数到每个点绝对值距离之和
cout << res << endl;
return 0;
}