#include #include #include #include #include #include // accumulate #include using namespace std; typedef long long ll; long double solve() { ll n, k, xbar; vector::iterator it; cin >> n >> k >> xbar; vector x(n); for (ll &xi : x) cin >> xi; for (ll &xi : x) xi -= xbar; // set xbar = 0 WLOG sort(x.begin(), x.end()); ll s = accumulate(x.begin(), x.end(), 0LL); long double res = (long double)s / n, alt; // do not remove anything if (k <= 0) return fabs(res); // O(log n) ll min_diff = abs(s - x[0]); it = upper_bound(x.begin(), x.end(), s); if (it != x.end()) min_diff = min(min_diff, *it - s); if (it-- != x.begin()) min_diff = min(min_diff, s - *it); alt = (long double) min_diff / (n-1); if (abs(alt) < abs(res)) res = alt; if (k <= 1) return fabs(res); // O(n log n) min_diff = abs(s - x[0] - x[1]); for (auto it0 = x.begin(); it0 + 1 != x.end(); it0++) { it = upper_bound(it0 + 1, x.end(), s - *it0); if (it != x.end()) min_diff = min(min_diff, *it0 + *it - s); if (it != it0) min_diff = min(min_diff, s - *it0 - *(--it)); } alt = (long double) min_diff / (n-2); if (abs(alt) < abs(res)) res = alt; if (k <= 2) return fabs(res); // O(n^2 log n) min_diff = abs(s - x[0] - x[1] - x[2]); for (auto it0 = x.begin(); it0 + 2 != x.end(); ) { ll cs = s - *it0; for (auto it1 = ++it0; it1 + 1 != x.end(); ++it1) { it = upper_bound(it1, x.end(), cs - *it1); if (it != x.end()) min_diff = min(min_diff, *it1 + *it - cs); if (it != it1) min_diff = min(min_diff, cs - *it1 - *(--it)); } } alt = (long double) min_diff / (n-3); if (abs(alt) < abs(res)) res = alt; if (k <= 3) return fabs(res); // O(n^3 log n) min_diff = abs(s - x[0] - x[1] - x[2] - x[3]); for (auto it0 = x.begin(); it0 + 3 != x.end(); ) { ll cs0 = s - *it0; for (auto it1 = ++it0; it1 + 2 != x.end(); ) { ll cs1 = cs0 - *it1; // Random performance: 1.3s -> .3s with this optimalisation: if (*(it1+1) + *(it1+2) > cs1) { // best to take these two, as other options are definitely bigger ++it1; min_diff = min(min_diff, *it1 + *(it1 + 1) - cs1); continue; } else if (cs1 > x[n-1] + x[n-2]) { // best to take these two, as other options are definitely smaller ++it1; min_diff = min(min_diff, cs1 - x[n-2] - x[n-1]); continue; } for (auto it2 = ++it1; it2 + 1 != x.end(); ++it2) { it = upper_bound(it2, x.end(), cs1 - *it2); if (it != x.end()) min_diff = min(min_diff, *it2 + *it - cs1); if (it != it2) min_diff = min(min_diff, cs1 - *it2 - *(--it)); } } } alt = (long double) min_diff / (n-4); if (abs(alt) < abs(res)) res = alt; if (k <= 4) return fabs(res); assert(false); return fabs(res); } int main() { (cout << fixed).precision(9); cout << solve() << endl; }