C++ 代码
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e6 + 10;
int n;
int a[N];
int main()
{
cin >> n;
for(int i = 0; i < n; ++ i) scanf("%d", &a[i]);
sort(a, a + n);
int mid = a[n >> 1];
int ret = 0;
for(int i = 0; i < n; ++ i)
ret += abs(a[i] - mid);
cout << ret << endl;
return 0;
}