Link to this code:
https://cses.fi/paste/e332a763ca3ce071269d86//*
author: @ankingcodes
created: 2021-07-17 23:21:07.103237
*/
#include<bits/stdc++.h>
#include<algorithm>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define MOD 1000000007
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update> PBDS;
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
tree_order_statistics_node_update> pairPBDS;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n; cin >> n;
vector<ll> a;
int freq[100000] = {0};
ll mx = INT_MIN;
for (ll i=0;i<n;i++) {
ll x; cin >> x;
a.push_back(x);
}
sort(a.begin(), a.end());
ll ele = a[n/2];
ll ans = 0;
for(auto x : a) {
ans += abs(x - ele);
}
cout << ans << endl;
return 0;
}