#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int N = 100010;
int a[N],n;
int main(){
cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i];
sort(a + 1, a + n + 1);
int pos;
int ans = 0;
if(n & 1){
pos = a[(n + 1) / 2];
}
else pos = (a[n / 2] + a[n /2 + 1]) / 2;
for(int i = 1; i <= n; i++){
ans += abs(a[i] - pos);
}
cout << ans << endl;
}