#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using Int = long long; template ostream &operator<<(ostream &os, const pair &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template ostream &operator<<(ostream &os, const vector &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; } template void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; } template bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; } template bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; } #define COLOR(s) ("\x1b[" s "m") // floor(a / b) Int divFloor(Int a, Int b) { return a / b - (((a ^ b) < 0 && a % b != 0) ? 1 : 0); } // ceil(a / b) Int divCeil(Int a, Int b) { return a / b + (((a ^ b) > 0 && a % b != 0) ? 1 : 0); } constexpr Int INF = 100100100100100100LL; int N; Int K, L, U; vector A; Int W; // [b, b + W] Int calc(Int b) { const Int c = b + W; Int ret = 0; for (int i = 0; i < N; ++i) { if (A[i] < b) { ret += ((b - A[i]) + K - 1) / K; } if (c < A[i]) { ret += ((A[i] - c) + K - 1) / K; } } return ret; } int main() { for (; ~scanf("%d%lld%lld%lld", &N, &K, &L, &U); ) { A.resize(N); for (int i = 0; i < N; ++i) { scanf("%lld", &A[i]); } W = U - L; sort(A.begin(), A.end()); Int lo = divFloor(A[0] - W, K) - 1; Int hi = divCeil(A[N - 1] + W, K) + 1; for (; lo + 1 < hi; ) { const Int mid = (lo + hi) / 2; ((calc(K * mid) > calc(K * (mid + 1))) ? lo : hi) = mid; } const Int bL = K * (hi - 1); const Int bR = K * (hi + 1); cerr<<"W = "< fs(bR - bL + 1, 0); fs[0] += calc(bL); for (int i = 0; i < N; ++i) { for (Int b = A[i] + max(divCeil(bL - A[i], K), 0LL) * K + 1; b <= bR; b += K) { ++fs[b - bL]; } for (Int c = A[i] - max(divCeil(A[i] - (bR + W), K), 0LL) * K; c > bL + W; c -= K) { --fs[c - W - bL]; } } for (Int b = bL + 1; b <= bR; ++b) { fs[b - bL] += fs[b - 1 - bL]; } const Int ans = *min_element(fs.begin(), fs.end()); printf("%lld\n", ans); #ifdef LOCAL cerr<<"fs = "<