#include #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") using namespace std; using ll = long long; const ll mod = 998244353; #define fi first #define se second #define rep(i,n) for(ll i=0;i1){ ll mid=(ok+ng)/2; if(x+mid*y> N >> K >> L >> U; vector A(N); rep(i,N) cin >> A[i]; ll ok=-1,ng=1e12+500; while(ng-ok>1){ ll x=(ok+ng)/2; ll a=0,b=0; rep(i,N){ if(A[i] cum(K*2-1,0); rep(i,N){ ll xl=(A[i]+1)%K; ll minl=g(xl,K,max(A[i],ok-K+2)); while(minl<=ok+K) cum[minl-(ok-K+2)]++,minl+=K; ll xu=(A[i]+L+K-U%K)%K; ll minu=g(xu,K,ok-K+2); while(minu<=ok+K){ if(minu+U-L<=A[i]) cum[minu-(ok-K+2)]--; minu+=K; } } rep(i,K*2-1){ now+=cum[i]; ans=min(ans,now); } cout << ans << endl; return 0; }