#include namespace { #pragma GCC diagnostic ignored "-Wunused-function" #include #pragma GCC diagnostic warning "-Wunused-function" using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; auto floor_div(signed_integral auto x, signed_integral auto y) { return x / y - ((x ^ y) < 0 && x % y != 0); } template T floor_div(T x, unsigned_integral auto y) { return x >= 0 ? T(x / y) : -T(-x / y + (-x % y != 0)); } auto ceil_div(signed_integral auto x, signed_integral auto y) { return x / y + ((x ^ y) >= 0 && x % y != 0); } template T ceil_div(T x, unsigned_integral auto y) { return x >= 0 ? T(x / y + (x % y != 0)) : -T(-x / y); } constexpr long long INF = 1002003004005006007; template struct value_compression : vector { bool built = false; using VS = vector; using VS::VS; value_compression(vector v) : vector(move(v)) {} void build() { sort(this->begin(), this->end()); this->erase(unique(this->begin(), this->end()), this->end()); built = true; } template void convert(T first, T last) { assert(built); for (; first != last; ++first) *first = (*this)(*first); } int operator()(S x) { assert(built); return lower_bound(this->begin(), this->end(), x) - this->begin(); } void clear() { this->clear(); built = false; } }; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, k; ll l, r; cin >> n >> k >> l >> r; r++; VL addl, addr; vector> add_seg; rep(_, n) { ll a; cin >> a; ll la = l - a, ra = r - a; auto lc = ceil_div(la, k); auto rc = ceil_div(ra, k) - 1; addl.emplace_back(lc); addr.emplace_back(rc); int nxt_l = la % k; if (nxt_l < 0) nxt_l += k; int nxt_r = ra % k; if (nxt_r < 0) nxt_r += k; if (nxt_l) add_seg.emplace_back(nxt_l, -1, -INF, lc); if (nxt_r) add_seg.emplace_back(nxt_r, 1, rc, INF); } value_compression vc; for (ll x : addl) vc.emplace_back(x); for (ll x : addr) vc.emplace_back(x); for (auto [t, v, l, r] : add_seg) vc.emplace_back(l), vc.emplace_back(r); vc.build(); int sz = vc.size(); VL init(sz); { ll a = 0, b = 0; sort(rall(addl)); sort(rall(addr)); for (ll x : addl) a--, b += x; rep(i, sz) { ll x = vc[i]; while (addl.size() && addl.back() == x) { a++, b -= x; addl.pop_back(); } while (addr.size() && addr.back() == x) { a++, b -= x; addr.pop_back(); } ll v; if (__builtin_mul_overflow(x, a, &v)) v = INF; else v += b; init[i] = v; } } lazy_segtree= INF ? INF : x + f; }, [](int f, int g) { return f + g; }, []() { return 0; }> seg(init); sort(rall(add_seg)); ll ans = seg.all_prod(); rep(r, k) { while (add_seg.size() && get<0>(add_seg.back()) == r) { auto [_, v, l, r] = add_seg.back(); add_seg.pop_back(); seg.apply(vc(l), vc(r), v); } chmin(ans, seg.all_prod()); } assert(add_seg.empty()); cout << ans << '\n'; }