/* -*- coding: utf-8 -*- * * 837.cc: No.837 Noelちゃんと星々2 - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 100000; const long long LINF = 1LL << 62; /* typedef */ typedef long long ll; /* global variables */ int ys[MAX_N]; ll ysums[MAX_N + 1]; /* subroutines */ ll summove(int i0, int i1, int av) { int k = lower_bound(ys + i0, ys + i1, av) - ys; ll sum0 = av * (k - i0) - (ysums[k] - ysums[i0]); ll sum1 = (ysums[i1] - ysums[k]) - av * (i1 - k); return sum0 + sum1; } ll minmove(int i0, int i1) { ll av0 = (ysums[i1] - ysums[i0]) / (i1 - i0), av1 = av0 + 1; return min(summove(i0, i1, av0), summove(i0, i1, av1)); } /* main */ int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", ys + i); sort(ys, ys + n); if (ys[0] == ys[n - 1]) { puts("1"); return 0; } for (int i = 0; i < n; i++) ysums[i + 1] = ysums[i] + ys[i]; ll minsum = LINF; for (int k = 1; k < n; k++) { ll mm0 = minmove(0, k), mm1 = minmove(k, n); ll sum = mm0 + mm1; if (minsum > sum) minsum = sum; //printf("%d: %lld+%lld=%lld\n", k, mm0, mm1, sum); } printf("%lld\n", minsum); return 0; }