//tenary search
#include <bits/stdc++.h>
using namespace std;
const int maxN = 2e5;
int n;
int a[maxN];
long long f(int x){
long long sum = 0;
for(int i = 0; i < n; i++){
sum += abs(a[i] - x);
}
return sum;
}
signed main(){
cin >> n;
for(int i = 0; i < n; i++){
cin >> a[i];
}
int l = 1, r = *max_element(a, a + n);
int ans = 0;
while(l <= r){
int mid = (l + r) / 2;
if(f(mid) <= f(mid + 1)){
r = mid - 1;
ans = f(mid);
}else{
l = mid + 1;
}
}
cout << ans << endl;
}
Ly90ZW5hcnkgc2VhcmNoIAojaW5jbHVkZSA8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKY29uc3QgaW50IG1heE4gPSAyZTU7CmludCBuOwppbnQgYVttYXhOXTsKbG9uZyBsb25nIGYoaW50IHgpewoJbG9uZyBsb25nIHN1bSA9IDA7Cglmb3IoaW50IGkgPSAwOyBpIDwgbjsgaSsrKXsKCQlzdW0gKz0gYWJzKGFbaV0gLSB4KTsKCX0KCXJldHVybiBzdW07IAp9CnNpZ25lZCBtYWluKCl7CgljaW4gPj4gbjsKCWZvcihpbnQgaSA9IDA7IGkgPCBuOyBpKyspewoJCWNpbiA+PiBhW2ldOwoJfQoJaW50IGwgPSAxLCByID0gKm1heF9lbGVtZW50KGEsIGEgKyBuKTsKCWludCBhbnMgPSAwOwoJd2hpbGUobCA8PSByKXsKCQlpbnQgbWlkID0gKGwgKyByKSAvIDI7CgkJaWYoZihtaWQpIDw9IGYobWlkICsgMSkpewoJCQlyID0gbWlkIC0gMTsKCQkJYW5zID0gZihtaWQpOwoJCX1lbHNlewoJCQlsID0gbWlkICsgMTsKCQl9Cgl9Cgljb3V0IDw8IGFucyA8PCBlbmRsOyAKCQp9