#include using namespace std; #define rep(i, n) for (llong i = 0; i < (llong)(n); i++) #define llong long long int main() { llong n; cin >> n; vector y(n); rep(i, n) cin >> y[i]; sort(y.begin(), y.end()); if(y[0] == y[n-1]) { cout << "1\n"; } else { llong maxdiff = 0; //前の方 int idx = -1; rep(i, n-1) { if(maxdiff <= y[i+1] - y[i]) { if((maxdiff < y[i+1] - y[i]) || (abs(idx - n/2) > abs(i - n/2))) idx = i; maxdiff = y[i+1] - y[i]; } } int bef = y[idx / 2]; int aft = y[(idx+n) / 2]; llong result = 0; rep(i, n) { if(i <= idx) result += abs(bef - y[i]); else result += abs(aft - y[i]); } cout << result << "\n"; } }