中位数的简单应用,通过简单的分析得到,货仓需要建造在中位数的位置上,
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pa;
const int N = 100005;
int main() {
int n;
cin >> n;
int a[N];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
int loc = a[n / 2];
ll ans = 0;
for (int i = 0; i < n; i++) {
ans += fabs(a[i] - loc);
}
cout << ans;
return 0;
}