#include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector vll; typedef vector> vvll; #define for1(i,n) for (ll i=0;i<(n);i++) #define for2(i,m,n) for (ll i=(m);i<(n);i++) #define for3(i,m,n,d) for (ll i=(m);i<(n);i+=(d)) #define DEBUG 0 template void dumplist(T list) { for (auto item : list) { cout << item << " "; } cout << endl; } void solve() { ll N; cin >> N; vector Y(N); for1(i,N) cin >> Y[i]; sort(Y.begin(),Y.end()); if (Y[0] == Y[N-1]) { cout << 1 << endl; return; } vector csum(N+1,0); for1(i,N) csum[i+1] = csum[i] + Y[i]; const ll inf = 1LL << 60; ll ans = inf; // [0,i) [i,N) for2(i,1,N) { ll cost = 0; // 左側を合わせるコスト ll pivot = Y[i/2]; for2(j,0,i) { cost += abs(Y[j] - pivot); } // 右側を合わせるコスト pivot = Y[(N-i)/2+i]; for2(j,i,N) { cost += abs(Y[j] - pivot); } //cerr << i << " " << cost << endl; ans = min(ans, cost); } cout << ans << endl; } int main() { do { solve(); } while (DEBUG); }