#line 2 "lib/prelude.hpp" // #ifndef LOCAL // #pragma GCC optimize("O3") // #endif #include using namespace std; using ll = long long; using vi = vector; using vvi = vector>; using vll = vector; using vvll = vector>; using vc = vector; #define rep2(i, m, n) for (auto i = (m); i < (n); i++) #define rep(i, n) rep2(i, 0, n) #define repr2(i, m, n) for (auto i = (n); i-- > (m);) #define repr(i, n) repr2(i, 0, n) #define all(x) begin(x), end(x) template auto ndvec(int n, T e) { return vector(n, e); } template auto ndvec(int n, Ts... e) { return vector(n, ndvec(e...)); } #if __cpp_lib_ranges namespace R = std::ranges; namespace V = std::views; #endif #line 3 "lib/io.hpp" struct int1 { int val; int1(int a = 1) : val(a - 1) {} operator int() const { return val; } }; template class stdin_reader { public: stdin_reader() { buf[fread(buf, 1, sizeof(buf), stdin)] = 0; } template enable_if_t> read(T& x) { skip(); [[maybe_unused]] bool neg = false; if constexpr (is_signed_v) neg = *p == '-' ? (p++, true) : false; x = 0; while (*p > ' ') x = x * 10 + (*p++ & 0x0F); if constexpr (is_signed_v) x = neg ? -x : x; } template void_t read(T& x) { x = T((unsigned)(*this)); } void read(char &c) { skip(); c = *p++; } void read(char*& q) { skip(); q = p; while (*p > ' ') p++; *p = 0; } template void read(char (&s)[N]) { read(s); } void read(string& s) { skip(); char* p0 = p; while (*p > ' ') p++; s.assign(p0, p); } template ::value)>* = nullptr> void read(T& x) { read_tuple_impl(x, make_index_sequence>{}); } template void read(pair& x) { read(x.first), read(x.second); } template void read(T (&a)[N]) { for (auto& e : a) read(e); } template operator T() { T x; return read(x), x; } template void operator()(Ts&... xs) { (read(xs), ...); } int operator--() { return (int)*this - 1; } template T* arr(int n) { T* p = new T[n + 1]; rep(i, n) read(p[i]); return p; } template void vec(vector& v, int n) { v.resize(n); for (auto& e : v) read(e); } template vector vec(int n) { vector v; return vec(v, n), v; } auto vi(int n) { return vec(n); } auto vi1(int n) { auto v = vec(n); rep(i, n) v[i]--; return v; } auto vll(int n) { return vec(n); } template tuple...> vecs(int n) { tuple...> res; vecs_impl(res, n, make_index_sequence{}); return res; } template void vvec(vector>& v, int n, int m) { v.resize(n); for (auto& e : v) vec(e, m); } template vector> vvec(int n, int m) { vector> v; return vvec(v, n, m), v; } template auto cols(int n) { return transpose(vec>(n)); } private: char buf[BufSize], *p = buf; void skip() { while (*p <= ' ') p++; } template void read_tuple_impl(T& x, index_sequence) { (*this)(get(x)...); } template void vecs_impl(T& x, int n, index_sequence) { (vec(get(x), n), ...); } template static auto transpose_impl(const vector& v, index_sequence) { tuple>>...> w; (get(w).reserve(v.size()), ...); for (const auto& row : v) (get(w).push_back(get(row)), ...); return w; } template static auto transpose(const vector& v) { return transpose_impl(v, make_index_sequence>{}); } }; template class stdout_writer { public: ~stdout_writer() { flush(); } void flush() { fwrite(buf, 1, p - buf, stdout), p = buf; } void write_char(char c) { *p++ = c; } void write() {} void write(char c) { write_char(c); } template enable_if_t> write(T x) { if (!x) return write_char('0'); if constexpr (is_signed_v) if (x < 0) write_char('-'), x = -x; static char tmp[16]; char* q = end(tmp); while (x >= 10000) memcpy(q -= 4, digits.data + x % 10000 * 4, 4), x /= 10000; if (x < 10) write_char('0' + x); else if (x < 100) write_char('0' + (uint8_t)x / 10), write_char('0' + (uint8_t)x % 10); else if (x < 1000) memcpy(p, digits.data + x * 4 + 1, 3), p += 3; else memcpy(p, digits.data + x * 4, 4), p += 4; memcpy(p, q, end(tmp) - q), p += end(tmp) - q; } template void_t write(T x) { write(x.val()); } void write(double x) { static char tmp[40]; sprintf(tmp, "%.15f", x); write(tmp); } void write(long double x) { static char tmp[40]; sprintf(tmp, "%.15Lf", x); write(tmp); } void write(const char* s) { while (*s) *p++ = *s++; } void write(const string& s) { memcpy(p, s.c_str(), s.size()), p += s.size(); } template void write(const pair& x) { write(x.first), write_char(' '), write(x.second); } template void write(const tuple& x) { write_tuple(x, make_index_sequence{}); } template void write(const Ts&... xs) { ((write(xs), write_char(' ')), ...), --p; } template void writeln(const Ts&... xs) { write(xs...), write_char('\n'); } template void operator()(const Ts&... xs) { writeln(xs...); } template void iter(It first, It last, char sep = ' ') { if (first == last) write_char('\n'); else { while (first != last) write(*first++), write_char(sep); p[-1] = '\n'; } } template void iter1(It first, It last, char sep = ' ') { if (first == last) write_char('\n'); else { while (first != last) write(1 + *first++), write_char(sep); p[-1] = '\n'; } } template void vec(const vector& v, char sep = ' ') { iter(all(v), sep); } template void vec1(const vector& v, char sep = ' ') { iter1(all(v), sep); } void del() { *--p = 0; } void Yes(bool b = true) { writeln(b ? "Yes" : "No"); } void YES(bool b = true) { writeln(b ? "YES" : "NO"); } void Takahashi(bool b = true) { writeln(b ? "Takahashi" : "Aoki"); } private: char buf[BufSize], *p = buf; template void write_tuple(const T& x, index_sequence) { ((write(get(x)), write_char(' ')), ...), --p; } struct four_digits { char data[40000]; constexpr four_digits() : data() { for (int i = 0; i < 10000; i++) for (int n = i, j = 4; j--;) data[i * 4 + j] = n % 10 + '0', n /= 10; } } static constexpr digits{}; public: #define INSTANT(s) void s() { writeln(#s); } INSTANT(No) INSTANT(NO) INSTANT(Aoki) INSTANT(possible) INSTANT(Possible) INSTANT(POSSIBLE) INSTANT(impossible) INSTANT(Impossible) INSTANT(IMPOSSIBLE) #undef INSTANT }; stdin_reader<> in; stdout_writer<> out; #line 3 "lib/num/int/convex.hpp" // Returns (min f, min argmin f) template auto convex_min(T l, T r, F f) { if (l + 1 == r) return pair(f(l), l); auto sf = [l, r, &f](T x) { return x < r ? f(x) : numeric_limits::max(); }; const double PHI = 1.6180339887498948; vector fib = {T(1), T(1)}; fib.reserve(log((r - l + 1) / PHI) + 1); while (fib.back() <= r - l) fib.push_back(fib.end()[-1] + fib.end()[-2]); int i = fib.size(); l = l - 1, r = l + fib[--i]; T x = l + fib[i-2], y = l + fib[i-1]; auto fx = sf(x), fy = sf(y); while (i >= 4) { if (fx > fy) { l = x; tie(x, fx) = pair(y, fy); y = l + fib[--i - 1]; fy = sf(y); } else { r = y; tie(y, fy) = pair(x, fx); x = l + fib[--i - 2]; fx = sf(x); } } return fx > fy ? pair(fy, y) : pair(fx, x); } #line 3 "main.cpp" int main() { int n = in, k = in, l = in, h = in; int mid = (l + h) / 2; auto a = in.vll(n); __int128 M = __int128(1) << 68; auto [mini, arg_mini] = convex_min(0LL, ll(2e12), [&](ll x) { __int128 sum = 0; rep(i, n) { sum += M * ((max(0LL, x - (h - l) - a[i]) + (k - 1)) / k + (max(0LL, a[i] - x) + (k - 1)) / k) + abs(x - (h - mid) - a[i]) * 2; } return sum; }); out(ll(mini / M)); }