#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[200005];
int b[200005];
int c[200005];
set<int>st;
queue<int>q;
int main()
{
    int n,h,l,r;
    scanf("%d%d%d%d",&n,&h,&l,&r);
    for(int i=1;i<=n;++i)scanf("%d",&a[i]);
    st.clear();
    st.insert(0);
    while(!q.empty())q.pop();
    a[n+1]=h;
    for(int i=1;i<=n+1;++i)
    {
        //memset(c,0,sizeof(c));
        auto it=st.begin();
        while(it!=st.end())
        {
            int x=*it++;
            int aa=(x+a[i])>=h?(x+a[i])-h:(x+a[i]);
            int bb=(x+a[i]-1)>=h?(x+a[i]-1)-h:(x+a[i]-1);
            c[aa]=max(b[x],c[aa]);
            c[bb]=max(b[x],c[bb]);
            if(x<=r&&x>=l)
            {
                c[aa]=max(b[x]+1,c[aa]);
                c[bb]=max(b[x]+1,c[bb]);
            }
            q.push((x+a[i])>=h?(x+a[i])-h:(x+a[i]));
            q.push((x+a[i]-1)>=h?(x+a[i]-1)-h:(x+a[i]-1));
        }
        st.clear();
        while(!q.empty())
        {
            st.insert(q.front());
            q.pop();
        }
        for(int i=1;i<h+10;++i)b[i]=c[i];
        //cout<<endl;
    }
    int zd=0;
    for(int i=1;i<h+10;++i)
    {
        if(b[i]>zd)zd=b[i];
    }
    printf("%d\n",zd);
    return 0;
}


#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[200005];
int b[200005];
int c[200005];
set<int>st;
queue<int>q;
int main()
{
    int n,h,l,r;
    scanf("%d%d%d%d",&n,&h,&l,&r);
    for(int i=1;i<=n;++i)scanf("%d",&a[i]);
    st.insert(0);
    a[n+1]=h;
    for(int i=1;i<=n+1;++i)
    {
        memset(c,0,sizeof(c));
        int siz=st.size();
        auto it=st.begin();
        while(siz--)
        {
            int x=*it++;
            int aa=(x+a[i])>=h?(x+a[i])-h:(x+a[i]);
            int bb=(x+a[i]-1)>=h?(x+a[i]-1)-h:(x+a[i]-1);
            //cout<<x<<" "<<aa<<" "<<bb<<"  ";
            c[aa]=max(b[x],c[aa]);
            if(i!=n+1)c[bb]=max(b[x],c[bb]);
            if(x<=r&&x>=l)
            {
                //cout<<x<<"a";
                c[aa]=max(b[x]+1,c[aa]);
                if(i!=n+1)c[bb]=max(b[x]+1,c[bb]);
            }
            q.push((x+a[i])>=h?(x+a[i])-h:(x+a[i]));
            q.push((x+a[i]-1)>=h?(x+a[i]-1)-h:(x+a[i]-1));
        }
        while(!st.empty())st.clear();
        while(!q.empty())
        {
            st.insert(q.front());
            q.pop();
        }
        //cout<<endl;
        for(int i=0;i<h;++i)
        {
            b[i]=c[i];
           // cout<<b[i];
        }
        //cout<<endl;
    }
    int zd=-1;
    for(int i=0;i<h;++i)
    {
        if((b[i]>zd))zd=b[i];
        //if((l<=i&&i<=r)&&(b[i]+1>zd))zd=b[i]+1;
    }
    if(l==0)zd--;
    printf("%d\n",zd);
    return 0;
}


#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[200005];
int b[200005];
int f[2005][2005];
set<int>st;
queue<int>q;
int main()
{
    int n,h,l,r;
    scanf("%d%d%d%d",&n,&h,&l,&r);
    for(int i=l;i<=r;++i)b[i]=1;
    for(int i=1;i<=n;++i)scanf("%d",&a[i]);
    st.insert(0);
    a[n+1]=h;
    for(int i=1;i<=n+1;++i)
    {
        int siz=st.size();
        auto it=st.begin();
        while(siz--)
        {
            int x=*it++;
            int aa=(x+a[i])>=h?(x+a[i])-h:(x+a[i]);
            int bb=(x+a[i]-1)>=h?(x+a[i]-1)-h:(x+a[i]-1);
            //cout<<aa<<" "<<bb<<"  ";
            b[aa]=b[x];
            b[bb]=b[x];
            if(x<=r&&x>=l)
            {
                b[aa]=b[x]+1;
                b[bb]=b[x]+1;
            }
            q.push((x+a[i])>=h?(x+a[i])-h:(x+a[i]));
            q.push((x+a[i]-1)>=h?(x+a[i]-1)-h:(x+a[i]-1));
        }
        st.clear();
        while(!q.empty())
        {
            st.insert(q.front());
            q.pop();
        }
        //cout<<endl;
    }
    int zd=-1;
    for(int i=0;i<h;++i)
    {
        if(b[i]>zd)zd=b[i];
    }
    printf("%d\n",zd-1);
    return 0;
}


#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[200005];
int b[200005];
int f[2005][2005];
set<int>st;
queue<int>q;
int main()
{
    int n,h,l,r;
    scanf("%d%d%d%d",&n,&h,&l,&r);
    //for(int i=l;i<=r;++i)b[i]=1;

    for(int i=1;i<=n;++i)scanf("%d",&a[i]);
    if(l==0&&r==n-1)
    {
        printf("%d\n",n);
        return 0;
    }
    for(int p=1;p<=n;++p)
    {
        for(int q=0;q<h;++q)
        {
            if(q>=l&&q<=r)
            {
                f[p+1][q]=max(f[p][(q+a[p])%h],f[p][(q+a[p]-1)%h])+1;
            }
            f[p+1][q]=max(f[p+1][q],max(f[p][(q+a[p])%h],f[p][(q+a[p]-1)%h]));
        }
    }
    int zd=-1;
    for(int i=0;i<h;++i)if(f[n+1][i]>zd)zd=f[n+1][i];
    printf("%d\n",zd);
    return 0;
}


#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[200005];
int b[2005];
int c[2005];
set<int>st,q;
int main()
{
    int n,h,l,r;
    scanf("%d%d%d%d",&n,&h,&l,&r);
    for(int i=1;i<=n;++i)scanf("%d",&a[i]);
    st.insert(0);
    a[n+1]=h;
    for(int i=1;i<=n+1;++i)
    {
        memset(c,0,sizeof(c));
        int siz=st.size();
        auto it=st.begin();
        while(siz--)
        {
            int x=*it++;
            int aa=(x+a[i])>=h?(x+a[i])-h:(x+a[i]);
            int bb=(x+a[i]-1)>=h?(x+a[i]-1)-h:(x+a[i]-1);
            c[aa]=max(b[x],c[aa]);
            c[bb]=max(b[x],c[bb]);
            if(x<=r&&x>=l)
            {
                c[aa]=max(b[x]+1,c[aa]);
                c[bb]=max(b[x]+1,c[bb]);
            }
            q.insert((x+a[i])>=h?(x+a[i])-h:(x+a[i]));
            q.insert((x+a[i]-1)>=h?(x+a[i]-1)-h:(x+a[i]-1));
        }
        st.clear();
        st=q;
        q.clear();
        for(int i=0;i<h;++i)b[i]=c[i];
    }
    int zd=-1;
    for(int i=0;i<h;++i)if((b[i]>zd))zd=b[i];
    if(l==0)zd--;
    printf("%d\n",zd);
    return 0;
}


#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[200005];
int b[200005];
int c[200005];
set<int>st;
queue<int>q;
int main()
{
    int n,h,l,r;
    scanf("%d%d%d%d",&n,&h,&l,&r);
    for(int i=1;i<=n;++i)scanf("%d",&a[i]);
    st.clear();
    st.insert(0);
    a[n+1]=h;
    for(int i=1;i<=n+1;++i)
    {
        memset(c,0,sizeof(c));
        int siz=st.size();
        auto it=st.begin();
        while(siz--)
        {
            int x=*it++;
            int aa=(x+a[i])>=h?(x+a[i])-h:(x+a[i]);
            int bb=(x+a[i]-1)>=h?(x+a[i]-1)-h:(x+a[i]-1);
            c[aa]=max(b[x],c[aa]);
            c[bb]=max(b[x],c[bb]);
            if(x<=r&&x>=l)
            {
                c[aa]=max(b[x]+1,c[aa]);
                c[bb]=max(b[x]+1,c[bb]);
            }
            q.push((x+a[i])>=h?(x+a[i])-h:(x+a[i]));
            q.push((x+a[i]-1)>=h?(x+a[i]-1)-h:(x+a[i]-1));
        }
        st.clear();
        while(!q.empty())
        {
            st.insert(q.front());
            q.pop();
        }
        for(int i=0;i<h+10;++i)
        {
            b[i]=c[i];
        }
        //cout<<endl;
    }
    int zd=-1;
    for(int i=0;i<h+10;++i)
    {
        if(b[i]>zd)zd=b[i];
    }
    printf("%d\n",zd);
    return 0;
}


#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[200005];
int b[200005];
int c[200005];
set<int>st;
queue<int>q;
int main()
{
    int n,h,l,r;
    scanf("%d%d%d%d",&n,&h,&l,&r);
    for(int i=1;i<=n;++i)scanf("%d",&a[i]);
    st.insert(0);
    a[n+1]=h;
    for(int i=1;i<=n+1;++i)
    {
        memset(c,0,sizeof(c));
        int siz=st.size();
        auto it=st.begin();
        while(siz--)
        {
            int x=*it++;
            int aa=(x+a[i])>=h?(x+a[i])-h:(x+a[i]);
            int bb=(x+a[i]-1)>=h?(x+a[i]-1)-h:(x+a[i]-1);
            //cout<<x<<" "<<aa<<" "<<bb<<"  ";
            c[aa]=max(b[x],c[aa]);
            if(i!=n+1)c[bb]=max(b[x],c[bb]);
            if(x<=r&&x>=l)
            {
                //cout<<x<<"a";
                c[aa]=max(b[x]+1,c[aa]);
                if(i!=n+1)c[bb]=max(b[x]+1,c[bb]);
            }
            q.push((x+a[i])>=h?(x+a[i])-h:(x+a[i]));
            if(i!=n+1)q.push((x+a[i]-1)>=h?(x+a[i]-1)-h:(x+a[i]-1));
        }
        while(!st.empty())st.clear();
        while(!q.empty())
        {
            st.insert(q.front());
            q.pop();
        }
        //cout<<endl;
        for(int i=0;i<h;++i)
        {
            b[i]=c[i];
           // cout<<b[i];
        }
        //cout<<endl;
    }
    int zd=-1;
    for(int i=0;i<h;++i)
    {
        if((b[i]>zd))zd=b[i];
        //if((l<=i&&i<=r)&&(b[i]+1>zd))zd=b[i]+1;
    }
    printf("%d\n",zd);
    return 0;
}


#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[200005];
int b[2005];
int c[2005];
set<int>st,q;
int main()
{
    int n,h,l,r;
    scanf("%d%d%d%d",&n,&h,&l,&r);
    for(int i=1;i<=n;++i)scanf("%d",&a[i]);
    st.insert(0);
    a[n+1]=h;
    for(int i=1;i<=n+1;++i)
    {
        memset(c,0,sizeof(c));
        int siz=st.size();
        auto it=st.begin();
        while(siz--)
        {
            int x=*it++;
            int aa=(x+a[i])>=h?(x+a[i])-h:(x+a[i]);
            int bb=(x+a[i]-1)>=h?(x+a[i]-1)-h:(x+a[i]-1);
            c[aa]=max(b[x],c[aa]);
            c[bb]=max(b[x],c[bb]);
            if(x<=r&&x>=l)
            {
                c[aa]=max(b[x]+1,c[aa]);
                c[bb]=max(b[x]+1,c[bb]);
            }
            q.insert((x+a[i])>=h?(x+a[i])-h:(x+a[i]));
            q.insert((x+a[i]-1)>=h?(x+a[i]-1)-h:(x+a[i]-1));
        }
        st.clear();
        swap(st,q);
        q.clear();
        for(int i=0;i<h;++i)b[i]=c[i];
    }
    int zd=-1;
    for(int i=0;i<h;++i)if((b[i]>zd))zd=b[i];
    if(l==0)zd--;
    printf("%d\n",zd);
    return 0;
}


#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[200005];
int b[200005];
int c[200005];
set<int>st;
queue<int>q;
int main()
{
    int n,h,l,r;
    scanf("%d%d%d%d",&n,&h,&l,&r);
    //for(int i=l;i<=r;++i)b[i]=1;
    for(int i=1;i<=n;++i)scanf("%d",&a[i]);
    st.insert(0);
    a[n+1]=h;
    for(int i=1;i<=n+1;++i)
    {
        memset(c,0,sizeof(c));
        int siz=st.size();
        auto it=st.begin();
        while(siz--)
        {
            int x=*it++;
            int aa=(x+a[i])>=h?(x+a[i])-h:(x+a[i]);
            int bb=(x+a[i]-1)>=h?(x+a[i]-1)-h:(x+a[i]-1);
            //cout<<x<<" "<<aa<<" "<<bb<<"  ";
            c[aa]=max(b[x],c[aa]);
            c[bb]=max(b[x],c[bb]);
            if(x<=r&&x>=l)
            {
                //cout<<x<<"a";
                c[aa]=max(b[x]+1,c[aa]);
                c[bb]=max(b[x]+1,c[bb]);
            }
            q.push((x+a[i])>=h?(x+a[i])-h:(x+a[i]));
            q.push((x+a[i]-1)>=h?(x+a[i]-1)-h:(x+a[i]-1));
        }
        st.clear();
        while(!q.empty())
        {
            st.insert(q.front());
            q.pop();
        }
        //cout<<endl;
        for(int i=0;i<h;++i)
        {
            b[i]=c[i];
           // cout<<b[i];
        }
        //cout<<endl;
    }
    int zd=-1;
    for(int i=0;i<h;++i)
    {
        if(b[i]>zd)zd=b[i];
    }
    printf("%d\n",zd);
    return 0;
}


#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[200005];
int b[200005];
int c[200005];
set<int>st;
queue<int>q;
int main()
{
    int n,h,l,r;
    scanf("%d%d%d%d",&n,&h,&l,&r);
    for(int i=1;i<=n;++i)scanf("%d",&a[i]);
    st.insert(0);
    a[n+1]=h;
    for(int i=1;i<=n+1;++i)
    {
        memset(c,0,sizeof(c));
        int siz=st.size();
        auto it=st.begin();
        while(siz--)
        {
            int x=*it++;
            int aa=(x+a[i])>=h?(x+a[i])-h:(x+a[i]);
            int bb=(x+a[i]-1)>=h?(x+a[i]-1)-h:(x+a[i]-1);
            //cout<<x<<" "<<aa<<" "<<bb<<"  ";
            c[aa]=max(b[x],c[aa]);
            c[bb]=max(b[x],c[bb]);
            if(x<=r&&x>=l)
            {
                //cout<<x<<"a";
                c[aa]=max(b[x]+1,c[aa]);
                c[bb]=max(b[x]+1,c[bb]);
            }
            q.push((x+a[i])>=h?(x+a[i])-h:(x+a[i]));
            q.push((x+a[i]-1)>=h?(x+a[i]-1)-h:(x+a[i]-1));
        }
        st.clear();
        while(!q.empty())
        {
            st.insert(q.front());
            q.pop();
        }
        //cout<<endl;
        for(int i=0;i<h;++i)
        {
            b[i]=max(c[i],b[i]);
           // cout<<b[i];
        }
        //cout<<endl;
    }
    int zd=-1;
    for(int i=0;i<h;++i)
    {
        if(b[i]>zd)zd=b[i];
    }
    printf("%d\n",zd);
    return 0;
}


#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[200005];
int b[200005];
int c[200005];
set<int>st;
queue<int>q;
int main()
{
    int n,h,l,r;
    scanf("%d%d%d%d",&n,&h,&l,&r);
    for(int i=1;i<=n;++i)scanf("%d",&a[i]);
    st.clear();
    st.insert(0);
    while(!q.empty())q.pop();
    a[n+1]=h;
    for(int i=1;i<=n+1;++i)
    {
        memset(c,0,sizeof(c));
        int siz=st.size();
        auto it=st.begin();
        while(siz--)
        {
            int x=*it++;
            int aa=(x+a[i])>=h?(x+a[i])-h:(x+a[i]);
            int bb=(x+a[i]-1)>=h?(x+a[i]-1)-h:(x+a[i]-1);
            c[aa]=max(b[x],c[aa]);
            c[bb]=max(b[x],c[bb]);
            if(x<=r&&x>=l)
            {
                c[aa]=max(b[x]+1,c[aa]);
                c[bb]=max(b[x]+1,c[bb]);
            }
            q.push((x+a[i])>=h?(x+a[i])-h:(x+a[i]));
            q.push((x+a[i]-1)>=h?(x+a[i]-1)-h:(x+a[i]-1));
        }
        st.clear();
        while(!q.empty())
        {
            st.insert(q.front());
            q.pop();
        }
        for(int i=0;i<h+10;++i)b[i]=max(c[i],b[i]);
        //cout<<endl;
    }
    int zd=0;
    for(int i=0;i<h+10;++i)
    {
        if(b[i]>zd)zd=b[i];
    }
    printf("%d\n",zd);
    return 0;
}


#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[200005];
int b[200005];
int c[200005];
set<int>st;
queue<int>q;
int main()
{
    int n,h,l,r;
    scanf("%d%d%d%d",&n,&h,&l,&r);
    for(int i=1;i<=n;++i)scanf("%d",&a[i]);
    st.clear();
    st.insert(0);
    while(!q.empty())q.pop();
    a[n+1]=h;
    for(int i=1;i<=n+1;++i)
    {
        //memset(c,0,sizeof(c));
        auto it=st.begin();
        while(it!=st.end())
        {
            int x=*it++;
            int aa=(x+a[i])>=h?(x+a[i])-h:(x+a[i]);
            int bb=(x+a[i]-1)>=h?(x+a[i]-1)-h:(x+a[i]-1);
            c[aa]=max(b[x],c[aa]);
            c[bb]=max(b[x],c[bb]);
            if(x<=r&&x>=l)
            {
                c[aa]=max(b[x]+1,c[aa]);
                c[bb]=max(b[x]+1,c[bb]);
            }
            q.push((x+a[i])>=h?(x+a[i])-h:(x+a[i]));
            q.push((x+a[i]-1)>=h?(x+a[i]-1)-h:(x+a[i]-1));
        }
        st.clear();
        while(!q.empty())
        {
            st.insert(q.front());
            q.pop();
        }
        for(int i=0;i<h+10;++i)b[i]=max(c[i],b[i]);
        //cout<<endl;
    }
    int zd=0;
    for(int i=0;i<h+10;++i)
    {
        if(b[i]>zd)zd=b[i];
    }
    printf("%d\n",zd);
    return 0;
}


#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[200005];
int b[200005];
int c[200005];
set<int>st;
queue<int>q;
int main()
{
    int n,h,l,r;
    scanf("%d%d%d%d",&n,&h,&l,&r);
    //for(int i=l;i<=r;++i)b[i]=1;
    for(int i=1;i<=n;++i)scanf("%d",&a[i]);
    st.insert(0);
    a[n+1]=h;
    for(int i=1;i<=n+1;++i)
    {
        int siz=st.size();
        auto it=st.begin();
        while(siz--)
        {
            int x=*it++;
            int aa=(x+a[i])>=h?(x+a[i])-h:(x+a[i]);
            int bb=(x+a[i]-1)>=h?(x+a[i]-1)-h:(x+a[i]-1);
            //cout<<x<<" "<<aa<<" "<<bb<<"  ";
            c[aa]=max(b[x],c[aa]);
            c[bb]=max(b[x],c[bb]);
            if(x<=r&&x>=l)
            {
                //cout<<x<<"a";
                c[aa]=max(b[x]+1,c[aa]);
                c[bb]=max(b[x]+1,c[bb]);
            }
            q.push((x+a[i])>=h?(x+a[i])-h:(x+a[i]));
            q.push((x+a[i]-1)>=h?(x+a[i]-1)-h:(x+a[i]-1));
        }
        st.clear();
        while(!q.empty())
        {
            st.insert(q.front());
            q.pop();
        }
        //cout<<endl;
        for(int i=0;i<h;++i)
        {
            b[i]=c[i];
           // cout<<b[i];
        }
        //cout<<endl;
    }
    int zd=-1;
    for(int i=0;i<h;++i)
    {
        if(b[i]>zd)zd=b[i];
    }
    printf("%d\n",zd);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define IO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
const int MOD = 1e9+7;
const int N=1e6+5;
const int INF=1e9+5;
int main() {
    int n, h, l, r;
    cin >> n >> h >> l >> r;
    vector<int> vec(n);
    for (int i = 0; i < n; i++)cin >> vec[i];
    vector<vector<int>> dp(n + 1, vector<int>(h + 1, -1*INF));
    int ans = 0;
    dp[0][0]=0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < h; j++) {
            int t1 = (j - vec[i] + h) % h;
            int t2 = (j - vec[i] + h - 1) % h;
            if (j >= l && j <= r) {
                dp[i + 1][t1] = max(dp[i + 1][t1], dp[i][j] + 1);
                dp[i + 1][t2] = max(dp[i + 1][t2], dp[i][j] + 1);
            } else {
                dp[i + 1][t1] = max(dp[i + 1][t2], dp[i][j]);
                dp[i + 1][t1] = max(dp[i + 1][t2], dp[i][j]);

            }
            ans = max({ans, dp[i + 1][t1], dp[i + 1][t2]});
        }

    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define IO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
const int MOD = 1e9+7;
const int N=1e6+5;
const int INF=1e9+5;
int main() {
    int n, h, l, r;
    cin >> n >> h >> l >> r;
    vector<int> vec(n);
    for (int i = 0; i < n; i++)cin >> vec[i];
    vector<vector<int>> dp(n+1, vector<int>(h+1, 0));
    int ans = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < h; j++) {
            if (j >= l && j <= r) {
                dp[i + 1][(j + vec[i]) % h] = max(dp[i + 1][(j + vec[i]) % h], dp[i][j] + 1);
                dp[i + 1][(j + vec[i] - 1) % h] = max(dp[i + 1][(j + vec[i] - 1) % h], dp[i][j] + 1);
            } else {
                dp[i + 1][(j + vec[i]) % h] = max(dp[i + 1][(j + vec[i]) % h], dp[i][j]);
                dp[i + 1][(j + vec[i] - 1) % h] = max(dp[i + 1][(j + vec[i] - 1) % h], dp[i][j]);

            }
            ans = max(ans, dp[i + 1][(j + vec[i]) % h]);

        }

    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define IO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
const int MOD = 1e9+7;
const int N=1e6+5;
const int INF=1e5+5;
int main() {
    int n, h, l, r;
    cin >> n >> h >> l >> r;
    vector<int> vec(n);
    for (int i = 0; i < n; i++)cin >> vec[i];
    vector<vector<int>> dp(n + 1, vector<int>(h + 1, -1*INF));
    int ans = 0;
    dp[0][0]=0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < h; j++) {
            int t1 = (j - vec[i] + h) % h;
            int t2 = (j - vec[i] + h - 1) % h;
            if (j >= l && j <= r) {
                dp[i + 1][t1] = max(dp[i + 1][t1], dp[i][j] + 1);
                dp[i + 1][t2] = max(dp[i + 1][t2], dp[i][j] + 1);
            } else {
                dp[i + 1][t1] = max(dp[i + 1][t1], dp[i][j]);
                dp[i + 1][t2] = max(dp[i + 1][t2], dp[i][j]);

            }
            ans = max({ans, dp[i + 1][t1], dp[i + 1][t2]});
        }

    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define IO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
const int MOD = 1e9+7;
const int N=1e6+5;
const int INF=1e5+5;
int main() {
    int n, h, l, r;
    cin >> n >> h >> l >> r;
    vector<int> vec(n);
    for (int i = 0; i < n; i++)cin >> vec[i];
    vector<vector<int>> dp(n + 1, vector<int>(h + 1, -1*INF));
    int ans = 0;
    dp[0][0]=0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < h; j++) {
            int t1 = (j + (vec[i] -1) + h) % h;
            int t2 = (j + vec[i]  + h) % h;
            dp[i + 1][t1] = max(dp[i + 1][t1], dp[i][j] + (t1 >= l && t1 <= r));
            dp[i + 1][t2] = max(dp[i + 1][t2], dp[i][j] + (t2 >= l && t2 <= r));
            ans = max({ans, dp[i + 1][t1], dp[i + 1][t2]});
        }

    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define IO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
const int MOD = 1e9+7;
const int N=1e6+5;
const int INF=1e5+5;
int main() {
    int n, h, l, r;
    cin >> n >> h >> l >> r;
    vector<int> vec(n);
    for (int i = 0; i < n; i++)cin >> vec[i];
    vector<vector<int>> dp(n + 1, vector<int>(h + 1, -1*INF));
    int ans = 0;
    dp[0][0]=0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < h; j++) {
            int t1 = (j - vec[i] + h) % h;
            int t2 = (j - vec[i] + h - 1) % h;
            if (j >= l && j <= r) {
                dp[i + 1][t1] = max(dp[i + 1][t1], dp[i][j] + 1);
                dp[i + 1][t2] = max(dp[i + 1][t2], dp[i][j] + 1);
            } else {
                dp[i + 1][t1] = max(dp[i + 1][t1], dp[i][j]);
                dp[i + 1][t1] = max(dp[i + 1][t2], dp[i][j]);

            }
            ans = max({ans, dp[i + 1][t1], dp[i + 1][t2]});
        }

    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define IO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
const int MOD = 1e9+7;
const int N=1e6+5;
const int INF=1e5+5;
int main() {
    int n, h, l, r;
    cin >> n >> h >> l >> r;
    vector<int> vec(n);
    for (int i = 0; i < n; i++)cin >> vec[i];
    vector<vector<int>> dp(n + 1, vector<int>(h + 1, -1*INF));
    int ans = 0;
    dp[0][0]=0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < h; j++) {
            int t1 = (j - vec[i] + h) % h;
            int t2 = (j - vec[i] + h - 1) % h;
            if (j >= l && j <= r) {
                dp[i + 1][t1] = max(dp[i + 1][t1], dp[i][j] + 1);
                dp[i + 1][t2] = max(dp[i + 1][t2], dp[i][j] + 1);
            } else {
                dp[i + 1][t1] = max(dp[i + 1][t2], dp[i][j]);
                dp[i + 1][t1] = max(dp[i + 1][t2], dp[i][j]);

            }
            ans = max({ans, dp[i + 1][t1], dp[i + 1][t2]});
        }

    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define IO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
const int MOD = 1e9+7;
const int N=1e6+5;
const int INF=1e9+5;
int main() {
    IO;
    int n, h, l, r;
    cin >> n >> h >> l >> r;
    vector<int> vec(n);
    for (int i = 0; i < n; i++)cin >> vec[i];
    vector<vector<int>> dp(n + 5, vector<int>(h + 1, -1 * INF));
    int ans = 0;
    dp[0][0] = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < h; j++) {
            int t1 = (j - (vec[i-1] -1) + h) % h;
            int t2 = (j - vec[i-1]  + h) % h;
            if (j >= l && j <= r) {
                dp[i][j] = max(dp[i][j], dp[i - 1][t1] + 1);
                dp[i][j] = max(dp[i][j], dp[i - 1][t2] + 1);
            } else {
                dp[i][j] = max(dp[i][j], dp[i - 1][t1]);
                dp[i][j] = max(dp[i][j], dp[i - 1][t2]);
            }
            ans = max(ans, dp[i][j]);
        }
    }
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int N=2010;
int dp[N][N];
int mx;
int n,h,l,r;
queue<int> q;

void solve() {
	scanf("%d %d %d %d",&n,&h,&l,&r);
	q.push(0);
	memset(dp,-0x3f,sizeof(dp));
	dp[0][0]=0;
	for(int i=1; i<=n; i++) {
		int num;
		scanf("%d",&num);
		for(int j=0; j<h; j++) {
			int a=(j+num)%h, b=(j+num-1)%h;
			if ( l<=a && a<=r ) dp[i][a]=max(dp[i][a],dp[i-1][j]+1);
			else dp[i][a]=max(dp[i][a],dp[i-1][j]);
			if ( l<=b && b<=r ) dp[i][b]=max(dp[i][a],dp[i-1][j]+1);
			else dp[i][b]=max(dp[i][b],dp[i-1][j]);
		}
	}
	for(int i=0; i<h; i++) {
		mx=max(mx,dp[n][i]);
	}
	printf("%d\n",mx);
}

int main() {
//	freopen("in.txt","r",stdin);
	solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int N=2010;
int dp[N][N];
int a[N];
int n,h,l,r;

int dfs(int p, int now) {
	if ( p>n ) return 0;
	if ( dp[p][now]!=-1 ) return dp[p][now];
	int x=(now+a[p])%h;
	int res=0;
	int q=0;
	if ( l<=x && x<=r ) q++;
	int res1=q+dfs(p+1,x);
	q=0;
	x=(now+a[p]-1)%h;
	if ( l<=x && x<=r ) q++;
	int res2=q+dfs(p+1,x);
	res=max(res1,res2);
	dp[p][now]=res;
	return res;
}

void solve() {
	scanf("%d %d %d %d",&n,&h,&l,&r);
	int now=0;
	memset(dp,-1,sizeof(dp));
	for(int i=1; i<=n; i++) {
		scanf("%d",&a[i]);
	}
	cout << dfs(1,0) << endl;
}

int main() {
//	freopen("in.txt","r",stdin);
	solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int N=2010;
int dp[N][N];
int mx;
int n,h,l,r;

void solve() {
	scanf("%d %d %d %d",&n,&h,&l,&r);
	memset(dp,-0x3f,sizeof(dp));
	dp[0][0]=0;
	for(int i=1; i<=n; i++) {
		int num;
		scanf("%d",&num);
		for(int j=0; j<h; j++) {
			int a=(j+num)%h, b=(j+num-1)%h;
			if ( l<=a && a<=r ) dp[i][a]=max(dp[i][a],dp[i-1][j]+1);
			else dp[i][a]=max(dp[i][a],dp[i-1][j]);
			if ( l<=b && b<=r ) dp[i][b]=max(dp[i][b],dp[i-1][j]+1);
			else dp[i][b]=max(dp[i][b],dp[i-1][j]);
		}
	}
	for(int i=0; i<h; i++) {
		mx=max(mx,dp[n][i]);
	}
	printf("%d\n",mx);
}

int main() {
//	freopen("in.txt","r",stdin);
	solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int N=2010;
int a[N];
bool vis[N][N];
int mx;
int n,h,l,r;

void dfs(int now, int p, int cnt) {
	if ( cnt>mx ) mx=cnt;
//	if ( vis[now][p] ) return;
	if ( p>n ) return;
	int x=(now+a[p])%h,y=(now+a[p]-1)%h;
	if ( vis[x][p] && vis[y][p] ) return;
	vis[x][p]=1, vis[y][p]=1;
	if ( l<=x && x<=r ) dfs(x,p+1,cnt+1);
	else if ( x<l || r<x ) dfs(x,p+1,cnt);
	if ( l<=y && y<=r ) dfs(y,p+1,cnt+1);
	else if ( y<l || r<y ) dfs(y,p+1,cnt);
}

void solve() {
	scanf("%d %d %d %d",&n,&h,&l,&r);
	int now=0;
	for(int i=1; i<=n; i++) {
		scanf("%d",&a[i]);
	}
	dfs(0,1,0);
	printf("%d\n",mx);
}

int main() {
//	freopen("in.txt","r",stdin);
	solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int N=2010;
int a[N];
int mx;
int n,h,l,r;

void dfs(int now, int p, int cnt) {
	if ( cnt>mx ) mx=cnt;
	if ( p>n ) return;
	int x=(now+a[p])%h,y=(now+a[p]-1)%h;
	if ( l<=x && x<=r ) dfs(x,p+1,cnt+1);
	else if ( x<l || r<x ) dfs(x,p+1,cnt);
	if ( l<=y && y<=r ) dfs(y,p+1,cnt+1);
	else if ( y<l || r<y ) dfs(y,p+1,cnt);
}

void solve() {
	scanf("%d %d %d %d",&n,&h,&l,&r);
	int now=0;
	for(int i=1; i<=n; i++) {
		scanf("%d",&a[i]);
	}
	dfs(0,1,0);
	printf("%d\n",mx);
}

int main() {
//	freopen("in.txt","r",stdin);
	solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int N=2010;
int dp[N][N];
int mx;
int n,h,l,r;
queue<int> q;

void solve() {
	scanf("%d %d %d %d",&n,&h,&l,&r);
	q.push(0);
	for(int i=1; i<=n; i++) {
		int num;
		scanf("%d",&num);
		for(int j=0; j<h; j++) {
			int a=(j+num)%h, b=(j+num-1)%h;
			if ( l<=a && a<=r ) dp[i+1][a]=max(dp[i+1][a],dp[i][j]+1);
			else dp[i+1][a]=max(dp[i+1][a],dp[i][j]);
			if ( l<=b && b<=r ) dp[i+1][b]=max(dp[i+1][a],dp[i][j]+1);
			else dp[i+1][b]=max(dp[i+1][b],dp[i][j]);
		}
	}
	for(int i=0; i<h; i++) {
		mx=max(mx,dp[n+1][i]);
	}
	printf("%d\n",mx);
}

int main() {
//	freopen("in.txt","r",stdin);
	solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int N=2010;
int dp[N];
int mx;
int n,h,l,r;
queue<int> q;

void solve() {
	scanf("%d %d %d %d",&n,&h,&l,&r);
	q.push(0);
	for(int i=1; i<=n; i++) {
		int num;
		scanf("%d",&num);
		int x=q.size();
		set<int> s;
		for(int j=1; j<=x; j++) {
			int now=q.front(); q.pop();
			int a=(now+num)%h, b=(now+num-1)%h;
			s.insert(a);
			s.insert(b);
		}
		set<int>::iterator it;
		for(it=s.begin(); it!=s.end(); it++) {
			if ( l<=*it && *it<=r ) dp[*it]++;
			q.push(*it);
		}
	}
	for(int i=0; i<h; i++) {
		mx=max(mx,dp[i]);
	}
	printf("%d\n",mx);
}

int main() {
//	freopen("in.txt","r",stdin);
	solve();
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int dp[2003][2003];
int n,h,l,r;
vector<int> v;
int solve(int pos,int be,int sum,int ans){
    if(pos >= n)
        return ans+(sum%h >= l && sum%h<= r);
    if(dp[pos][be] != -1)
        return dp[pos][be];
    sum%=h;
    if(sum >= l && sum<= r)
        ans++;
    return dp[pos][be] = max(solve(pos+1,be+1,(sum+v[pos]-1)%(h) ,ans),solve(pos+1,be,(sum+v[pos])%(h),ans));

}

int main()
{
    memset(dp,-1,sizeof dp);
    cin >> n >> h >> l  >> r;
    for(int i = 0; i < n; i++){
        int a;
        cin >> a;
        v.push_back(a);
    }
    cout << solve(0,0,0,0) << endl;

}


#include <bits/stdc++.h>

using namespace std;

int dp[2013][2013];
int n,h,l,r;
vector<int> v;
int solve(int pos,int sum){
    if(pos >= n)
        return (sum >= l && sum <= r);
    if(dp[pos][sum] != -1)
        return dp[pos][sum];
    return dp[pos][sum] = max(solve(pos+1,(sum+v[pos])%h) +(sum >= l && sum <= r)
                             ,solve(pos+1,(sum+v[pos]-1)%h) +(sum >= l && sum <= r));

}

int main()
{
    memset(dp,-1,sizeof dp);
    cin >> n >> h >> l  >> r;
    for(int i = 0; i < n; i++){
        int a;
        cin >> a;
        v.push_back(a);
    }
    cout << solve(1,v[0]) << endl;

}


#include <bits/stdc++.h>

using namespace std;

int dp[2013][2013];
int n,h,l,r;
vector<int> v;
int solve(int pos,int be,int sum){
    if(pos >= n)
        return (sum%h >= l && sum%h <= r);
    sum%=h;
    if(dp[pos][sum] != -1)
        return dp[pos][sum];
    return dp[pos][sum] = max(solve(pos+1,be,(sum+v[pos])) +(sum%h >= l && sum%h <= r)
                             ,solve(pos+1,be+1,(sum+v[pos]-1)) +(sum%h >= l && sum%h <= r));

}

int main()
{
    memset(dp,-1,sizeof dp);
    cin >> n >> h >> l  >> r;
    for(int i = 0; i < n; i++){
        int a;
        cin >> a;
        v.push_back(a);
    }
    cout << solve(0,0,0) << endl;

}


#include <bits/stdc++.h>

using namespace std;

//int dp[2003][2003];
int n,h,l,r;
vector<int> v;
//int solve(int pos,int be,int sum){
//    if(pos >= n)
//        return (sum%h >= l && sum%h <= r);
//    if(dp[pos][be] != -1)
//        return dp[pos][be];
//    sum%=h;
//    return dp[pos][be] = max(solve(pos+1,be+1,(sum+v[pos]-1)%(h))+(sum%h >= l && sum%h <= r)
//                              , solve(pos+1,be,(sum+v[pos])%(h))+(sum%h >= l && sum%h <= r));
//
//
//
//
//}

int main()
{
    //memset(dp,-1,sizeof dp);
    cin >> n >> h >> l  >> r;
    for(int i = 0; i < n; i++){
        int a;
        cin >> a;
        v.push_back(a);
    }
    //cout << solve(0,0,0) << endl;
    int ca = 0;
    int sum = 0;

    int ans = 0;

    for(int i = 0; i < n; i++){
            sum %= h;
        if(sum < l && ca >= l-sum){
            ca -= (l-sum);
            sum = l;
        }
        if(sum >= l && sum <= r){
            ans++;
            ca = max(min(ca,r-sum),0);
        }
        sum += v[i]-1;
        ca++;
    }
        sum %= h;
        if(sum < l && ca >= l-sum){
            ca -= (l-sum);
            sum = l;
        }
        if(sum >= l && sum <= r){
            ans++;
            ca = max(min(ca,r-sum),0);
        }
    cout << ans << endl;

}


#include <bits/stdc++.h>

using namespace std;

int dp[2013][2013];
int n,h,l,r;
vector<int> v;
int solve(int pos,int be,int sum){
    if(pos >= n)
        return (sum%h >= l && sum%h <= r);
    if(dp[pos][be] != -1)
        return dp[pos][be];
    sum%=h;
    return dp[pos][be] = max(solve(pos+1,be,(sum+v[pos])) +(sum%h >= l && sum%h <= r)
                             ,solve(pos+1,be+1,(sum+v[pos]-1)) +(sum%h >= l && sum%h <= r));

}

int main()
{
    memset(dp,-1,sizeof dp);
    cin >> n >> h >> l  >> r;
    for(int i = 0; i < n; i++){
        int a;
        cin >> a;
        v.push_back(a);
    }
    cout << solve(0,0,0) << endl;

}


#include <bits/stdc++.h>

using namespace std;

int dp[2003][2003];
int n,h,l,r;
vector<int> v;
int solve(int pos,int be,int sum){
    if(pos >= n)
        return (sum%h >= l && sum%h <= r);
    if(dp[pos][be] != -1)
        return dp[pos][be];
    sum%=h;
    return dp[pos][be] = max(solve(pos+1,be,(sum+v[pos])) +(sum%h >= l && sum%h <= r)
                             ,solve(pos+1,be+1,(sum+v[pos]-1)) +(sum%h >= l && sum%h <= r));

}

int main()
{
    memset(dp,-1,sizeof dp);
    cin >> n >> h >> l  >> r;
    for(int i = 0; i < n; i++){
        int a;
        cin >> a;
        v.push_back(a);
    }
    cout << solve(0,0,0) << endl;

}


#include <bits/stdc++.h>

using namespace std;

int dp[2003][2003];
int n,h,l,r;
vector<int> v;
int solve(int pos,int be,int sum){
    if(pos >= n)
        return (sum%h >= l && sum%h <= r);
    if(dp[pos][be] != -1)
        return dp[pos][be];
    sum%=h;
    return dp[pos][be] = max(solve(pos+1,be+1,(sum+v[pos]-1)%(h))+(sum%h >= l && sum%h <= r)
                              , solve(pos+1,be,(sum+v[pos])%(h))+(sum%h >= l && sum%h <= r));

}

int main()
{
    memset(dp,-1,sizeof dp);
    cin >> n >> h >> l  >> r;
    for(int i = 0; i < n; i++){
        int a;
        cin >> a;
        v.push_back(a);
    }
    cout << solve(0,0,0) << endl;

}


#include <bits/stdc++.h>

using namespace std;

int dp[2013][2013];
int n,h,l,r;
vector<int> v;
int solve(int pos,int sum){
    if(pos >= n)
        return (sum >= l && sum <= r);
    if(dp[pos][sum] != -1)
        return dp[pos][sum];
    return dp[pos][sum] = max(solve(pos+1,(sum+v[pos])%h) +(sum >= l && sum <= r)
                             ,solve(pos+1,(sum+v[pos]-1)%h) +(sum >= l && sum <= r));

}

int main()
{
    memset(dp,-1,sizeof dp);
    cin >> n >> h >> l  >> r;
    for(int i = 0; i < n; i++){
        int a;
        cin >> a;
        v.push_back(a);
    }
    cout << solve(0,0) - (0 == l) << endl;

}


#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
using namespace __gnu_pbds;
#define mod 1000000007
#define int long long
#define pb push_back
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define inp(n) for(i=0;i<n;i++) cin>>A[i];
#define inz(A) memset(A,0,sizeof(A))
#define lb lower_bound
#define ub upper_bound
#define all(v) v.begin(),v.end()
#define T 1000000
#define F first
#define S second
#define pi pair<int,int>
#define vi vector<int>
#define mii map<int,int>
#define vp vector<pair< int,int> >
#define mpi map<pair<int,int> ,int>
#define mip map<int, pair<int,int> >
#define inf INT_MAX
#define inf1 LONGLONG_MAX
#define sz(v) v.size()
#define rep(i,n) for(int i=0;i<n;i++)
#define forn(i,a,b) for(int i=a;i<=b;i++)
#define deb(x) cout<<#x<<"is "<<x<<endl;
#define lower(s) transform(all(s),s.begin(),::tolower)
#define endl "\n"
typedef tree<int, null_type, less<int>, rb_tree_tag,
         tree_order_statistics_node_update> pbds;

/* brownsands :Arpit */

void solve()
{
    int n,h,l,r;
   cin>>n>>h>>l>>r;
   int ans=0;vi re(n),ar(n);
   vector<vector<int>>dp(n+5,vector<int>(n+5,0));
   for(int i=0;i<n;i++)
   {cin>>ar[i];
   if(i>0)re[i]=(re[i-1]+ar[i])%h;
   else re[i]=ar[i];
   }

   for(int i=0;i<n;i++)
   {
       for(int j=0;j<=i+1;j++)
       {   int x=0;
           if(i>0)
           {if(j>0)
           x=max(dp[i-1][j],dp[i-1][j-1]);
           else x=dp[i-1][j];
           }
           int mn=re[i]-j;
           while(mn<0)mn=(mn+h)%h;

           if(mn>=l&&mn<=r)x++;
           dp[i][j]=x;
           //cout<<dp[i][j]<<" ";
       }
       //cout<<endl;
   }
   for(int i=0;i<=n;i++)ans=max(ans,dp[n-1][i]);
   cout<<ans<<endl;

}
signed main()
{
   boost
        int test=0,t;
		//cin>>t;
		t=1;
		while(test++ <t){
            //cout<<"Case "<<test<<": ";
            solve();
            //cerr << "\nTime : " << (double)clock() / (double)CLOCKS_PER_SEC << "s\n";
               }
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define endl "\n"
#define deb(x) cout<<#x<<" "<<x<<endl;
#define sc(ar,n) for(int pen=0;pen<n;pen++){ cin>>ar[pen];}
#define pr(ar,n) for(int pen=0;pen<n;pen++){ cout<<ar[pen]<<" ";} cout<<endl;
#define fr(i,x,n) for(int i=x;i<n;i++)
#define frt(it,vb) for(auto it=vb.begin();it!=vb.end();it++)
#define mem(ar,x) memset(ar,x,sizeof(ar));
#define pb push_back
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mod 1000000007
#define rt return 0;
#define ct continue;
#define MAX 1000000000000000000
#define MAX1 1000000000
#define CLK clock_t clk = clock();//Start of main
#define OCLK cerr << "Time (in ms): " << (double)(clock() - clk) * 1000.0 / CLOCKS_PER_SEC << '\n';//End of main
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    new_data_set;

int dp[2003][2003];
int func(int ar[],int n,int h,int l,int r,int i,int j,int sum)
{
    if(i==n){
        return 0;
    }
    if(dp[i][j]!=-1){
        // cout<<dp[i][j]<<" "<<i<<" "<<j<<endl;
        return dp[i][j];
    }
    sum+=ar[i];
    // deb(sum);
    int x = sum-1;
    // dp[i][j]=0;
    int y = func(ar,n,h,l,r,i+1,j+1,x);
    int z = func(ar,n,h,l,r,i+1,j,sum);
    int a=0,b=0;
    if(x%h>=l && x%h<=r){
        a++;
    }
    if(sum%h>=l && sum%h<=r){
        b++;
    }
    dp[i][j] = max( y+a,z+b );
    return dp[i][j];
}
inline int solve()
{
    int n,h,l,r;
    cin>>n>>h>>l>>r;
    mem(dp,-1);
    int ar[n];
    sc(ar,n);
    int ans = func(ar,n,h,l,r,0ll,0ll,0ll);
    // fr(i,0,n+1){
    //     cout<<dp[n-1][i]<<" ";
    // }
    // cout<<endl;
    cout<<ans<<endl;
}
signed main()
{
    // fastio;
    int t;
    int test_cases=1;
    if(test_cases==0)
    cin>>t;
    else
    t=1;
    while(t--){
        solve();
    }
}


#include <bits/stdc++.h>

#ifdef ONLINE_JUDGE
#define endl "\n"
#endif
using namespace std;
typedef unsigned long long int lu;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> v;
typedef vector<v> vv;
typedef vector<ld> vld;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef pair<ll, ll> pll;
typedef vector<set<ll>> vsll;
typedef set<pair<ll, ll>> spll;
typedef vector<spll> vspll;
typedef vector<pll> vpll;
const ll MOD = 1e9 + 7;
const v dx = {1, -1, 0, 0};
const v dy = {0, 0, 1, -1};
#define yes cout << "YES\n";
#define no cout << "NO\n";
#define yesno(f) \
    if (f)       \
        yes else no;
#define noo \
    { no return; }
#define round(x, y) ((2 * x + y) / (2 * y))
#define ce(x, y) ((x + y - 1) / y)
#define amax(x, y) \
    if (y > x)     \
        x = y;
#define amin(x, y) \
    if (y < x)     \
        x = y;
#define lcm(x, y) ((x) * (y) / __gcd(x, y))
#define sz(x) (ll) x.size()
#define sq(x) ((x) * (x))

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

template<typename T = ll>
vector<T> ga(ll n, bool oneIndexed = false) {
    vector<T> a = vector<T>(n + oneIndexed);

    for (ll i = 0; i < n; i++) {
        T p;
        cin >> p;
        a[i + oneIndexed] = p;
    }

    return move(a);
}

template<typename T, typename A>
void pa(vector<T, A> const &a, ll begin = 0, ll end = -1) {
    if (end == -1)
        end = sz(a) - 1;
    for (ll i = begin; i <= end; i++) {
        cout << a[i] << " ";
    }
    cout << endl;
}

ll n, h, l, r;

ll nice(ll x) {
    x %= h;
    return x >= l && x <= r;
}

void solve() {
    cin >> n >> h >> l >> r;

    vv dp(h, v(n + 1, 0));
    v a = ga(n);

    for (ll step = n - 1; step >= 0; step--) {
        for (ll start = 0; start < h; start++) {
            ll time = a[step] + start;
            time %= h;

            ll x1 = nice(time) + dp[time][step + 1];
            time--;
            if (time == -1)time = h - 1;
            ll x2 = nice(time) + dp[time][step + 1];
            dp[start][step] = max(x1, x2);
        }
    }

    cout << dp[0][0] << endl;
}

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    // freopen("file", "r", stdin);
    // freopen("file", "w", stdout);
    // ll t;cin >> t;while(t--)
    solve();
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define endl "\n"
#define deb(x) cout<<#x<<" "<<x<<endl;
#define sc(ar,n) for(int pen=0;pen<n;pen++){ cin>>ar[pen];}
#define pr(ar,n) for(int pen=0;pen<n;pen++){ cout<<ar[pen]<<" ";} cout<<endl;
#define fr(i,x,n) for(int i=x;i<n;i++)
#define frt(it,vb) for(auto it=vb.begin();it!=vb.end();it++)
#define mem(ar,x) memset(ar,x,sizeof(ar));
#define pb push_back
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mod 1000000007
#define rt return 0;
#define ct continue;
#define MAX 1000000000000000000
#define MAX1 1000000000
#define CLK clock_t clk = clock();//Start of main
#define OCLK cerr << "Time (in ms): " << (double)(clock() - clk) * 1000.0 / CLOCKS_PER_SEC << '\n';//End of main
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    new_data_set;

inline int solve()
{
    int n,h,l,r;
    cin>>n>>h>>l>>r;
    int ar[n];
    sc(ar,n);
    int dp[n+2][n+2];
    mem(dp,0);
    int sum=0;
    int dp2[n+2];
    mem(dp2,0);
    int prev[n+2];
    mem(prev,0);
    fr(i,0,n){
        sum+=ar[i];
        fr(j,0,i+2){
            int y = sum-j;
            y= y%h;
            if(y>=l && y<=r){
                dp[i][j]++;
            }
            dp[i][j]+=prev[j];
        }
        fr(j,0,i+2){
            if(j>0)
            prev[j]=max(dp[i][j],prev[j-1]);
            else
            prev[j]=dp[i][j];
        }
    }
    int ans=0;
    fr(i,0,n+1){
        ans=max(ans,dp[n-1][i]);
    }
    cout<<ans<<endl;
}
signed main()
{
    // fastio;
    int t;
    int test_cases=1;
    if(test_cases==0)
    cin>>t;
    else
    t=1;
    while(t--){
        solve();
    }
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define up upper_bound
#define sz(h) (int)h.size()
#define all(h) h.begin(), h.end()
#define mod 1000000007
#define mod1 998244353
#define MAX 2000010
struct hash_pair
{ // use pair as key in unordered_map<pair<int,int>,int,hash_pair>;

    template <class T1, class T2>

    size_t operator()(const pair<T1, T2> &p) const

    {

        auto hash1 = hash<T1>{}(p.first);

        auto hash2 = hash<T2>{}(p.second);

        return hash1 ^ hash2;
    }
};
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int n, h, l, r;
    cin >> n >> h >> l >> r;
    int DP[n + 3][h + 4];
    memset(DP, 0, sizeof(DP));
    int A[n + 10];
    for (int i = 1; i <= n; i++)
        cin >> A[i];
    for (int i = 0; i <= n; i++)
    {
        for (int j = 0; j <= h; j++)
            DP[i][j] = -1;
    }
    DP[0][0] = 0;

    for (int i = 1; i <= n; i++)
    {
        int x;
        for (int j = 0; j < h; j++)
        {
            x = (j - A[i] + h) % h;
            if (DP[i - 1][x] != -1)
            {
                if (j >= l && j <= r)
                {
                    DP[i][j] = max(DP[i - 1][x] + 1, DP[i][j]);
                }
                else
                {
                    DP[i][j] = max(DP[i - 1][x], DP[i][j]);
                }
            }
        }
        for (int j = 0; j < h; j++)
        {
            x = (j - (A[i] - 1) + h) % h;
            if (DP[i - 1][x] != -1)
            {
                if (j >= l && j <= r)
                {
                    DP[i][j] = max(DP[i - 1][x] + 1, DP[i][j]);
                }
                else
                {
                    DP[i][j] = max(DP[i - 1][x], DP[i][j]);
                }
            }
        }
    }
    int ans = 0;
    for (int i = 0; i < h; i++)
        ans = max(DP[n][i], ans);
    cout << ans << endl;
}


/*input
7 24 21 23
16 17 14 20 20 11 22
*/
#include<bits/stdc++.h>
#define fn(i,n) for(int i=0;i<n;i++)
#define fn1(i,n) for(int i=1;i<=n;i++)
#define ll long long int
#define mod 1000000007
#define mp(x,y) make_pair(x,y)
int dp[2010][2010];
//dp[i][j] i sleeps j 1 hour less
int n,h,l,r;
bool check(int p)
{
    if(p%h<=r&&p%h>=l)
        return true;
    else
        return false;
}
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n>>h>>l>>r;
int a[n];
fn(i,n){
    cin>>a[i];
    if(i)a[i]+=a[i-1];
}
if(check(a[0]))
dp[0][0]=1;
if(check(a[0]-1))
dp[0][1]=1;
fn1(i,n)
{
    fn(j,i+1)
    {
        dp[i][j]=max(dp[i][j],dp[i-1][j]+check(a[i]-j));
        dp[i][j+1]=max(dp[i][j+1],dp[i-1][j]+check(a[i]-j-1));
    }
}
int a1=0;
fn(i,2010)
a1=max(a1,dp[n][i]);
cout<<a1;
}


/*input
7 24 21 23
16 17 14 20 20 11 22
*/
#include<bits/stdc++.h>
#define fn(i,n) for(int i=0;i<n;i++)
#define fn1(i,n) for(int i=1;i<=n;i++)
#define ll long long int
#define mod 1000000007
#define mp(x,y) make_pair(x,y)
int dp[2010][2010];
//dp[i][j] i sleeps j 1 hour less
int n,h,l,r;
bool check(int p)
{
    if(p%h<=r&&p%h>=l)
        return true;
    else
        return false;
}
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n>>h>>l>>r;
int a[n];
fn(i,n){
    cin>>a[i];
    if(i)a[i]+=a[i-1];
}
if(check(a[0]))
dp[0][0]=1;
if(check(a[0]-1))
dp[0][1]=1;
fn1(i,n-1)
{
    fn(j,i+1)
    {
        dp[i][j]=max(dp[i][j],dp[i-1][j]+check(a[i]-j));
        dp[i][j+1]=max(dp[i][j+1],dp[i-1][j]+check(a[i]-j-1));
    }
}
int a1=0;
fn(i,2010)
a1=max(a1,dp[n-1][i]);
cout<<a1;
}


/*input
7 24 21 23
16 17 14 20 20 11 22
*/
#include<bits/stdc++.h>
#define fn(i,n) for(int i=0;i<n;i++)
#define fn1(i,n) for(int i=1;i<=n;i++)
#define ll long long int
#define mod 1000000007
#define mp(x,y) make_pair(x,y)
int dp[2010][2010];
//dp[i][j] i sleeps j 1 hour less
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n,h,l,r;cin>>n>>h>>l>>r;
int a[n+1],p,q;
a[0]=0;
fn1(i,n){cin>>a[i];a[i]=a[i-1]+a[i];}
fn1(i,n)
{
    fn(j,min(i+1,h))
    {
        if((a[i]-j)%h<=r&&(a[i]-j)%h>=l)
            p=1;
        else
            p=0;
        if((a[i]-j-1)%h<=r&&(a[i]-j-1)%h>=l)
            q=1;
        else
            q=0;
        dp[i][j]=max(dp[i][j],dp[i-1][j]+p);
        dp[i][j+1]=max(dp[i][j+1],dp[i-1][j]+q);
    }
}
int a1=0;
fn(i,h+1)
a1=max(a1,dp[n][i]);
cout<<a1;
}


/*input
7 24 21 23
16 17 14 20 20 11 22
*/
#include<bits/stdc++.h>
#define fn(i,n) for(int i=0;i<n;i++)
#define fn1(i,n) for(int i=1;i<=n;i++)
#define ll long long int
#define mod 1000000007
#define mp(x,y) make_pair(x,y)
int dp[2010][2010];
//dp[i][j] i sleeps j 1 hour less
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n,h,l,r;cin>>n>>h>>l>>r;
int a[n+1],p,q;
a[0]=0;
fn1(i,n){cin>>a[i];a[i]=a[i-1]+a[i];}
fn1(i,n)
{
    fn(j,n+1)
    {
        if((a[i]-j)%h<=r&&(a[i]-j)%h>=l)
            p=1;
        else
            p=0;
        if((a[i]-j-1)%h<=r&&(a[i]-j-1)%h>=l)
            q=1;
        else
            q=0;
        dp[i][j]=max(dp[i][j],dp[i-1][j]+p);
        if(j<n)dp[i][j+1]=max(dp[i][j+1],dp[i-1][j]+q);
    }
}
int a1=0;
fn(i,n+1)
a1=max(a1,dp[n][i]);
cout<<a1;
}


/*input
7 24 21 23
16 17 14 20 20 11 22
*/
#include<bits/stdc++.h>
#define fn(i,n) for(int i=0;i<n;i++)
#define fn1(i,n) for(int i=1;i<=n;i++)
#define ll long long int
#define mod 1000000007
#define mp(x,y) make_pair(x,y)
int dp[2000][2000];
//dp[i][j] i sleeps j 1 hour less
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n,h,l,r;cin>>n>>h>>l>>r;
int a[n+1],p,q;
a[0]=0;
fn1(i,n){cin>>a[i];a[i]=a[i-1]+a[i];}
fn1(i,n)
{
    fn(j,min(i+1,h))
    {
        if((a[i]-j)%h<=r&&(a[i]-j)%h>=l)
            p=1;
        else
            p=0;
        if((a[i]-j-1)%h<=r&&(a[i]-j-1)%h>=l)
            q=1;
        else
            q=0;
        dp[i][j]=max(dp[i][j],dp[i-1][j]+p);
        dp[i][j+1]=max(dp[i][j+1],dp[i-1][j]+q);
    }
}
int a1=0;
fn(i,h+1)
a1=max(a1,dp[n][i]);
cout<<a1;
}


/*input
7 24 21 23
16 17 14 20 20 11 22
*/
#include<bits/stdc++.h>
#define fn(i,n) for(int i=0;i<n;i++)
#define fn1(i,n) for(int i=1;i<=n;i++)
#define ll long long int
#define mod 1000000007
#define mp(x,y) make_pair(x,y)
int dp[2010][2010];
//dp[i][j] i sleeps j 1 hour less
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n,h,l,r;cin>>n>>h>>l>>r;
int a[n+1],p,q;
a[0]=0;
fn1(i,n){cin>>a[i];a[i]=a[i-1]+a[i];}
fn1(i,n)
{
    fn(j,n+1)
    {
        if((a[i]-j)%h<=r&&(a[i]-j)%h>=l)
            p=1;
        else
            p=0;
        if((a[i]-j-1)%h<=r&&(a[i]-j-1)%h>=l)
            q=1;
        else
            q=0;
        dp[i][j]=max(dp[i][j],dp[i-1][j]+p);
        dp[i][j+1]=max(dp[i][j+1],dp[i-1][j]+q);
    }
}
int a1=0;
fn(i,n+1)
a1=max(a1,dp[n][i]);
cout<<a1;
}


/*input
7 24 21 23
16 17 14 20 20 11 22
*/
#include<bits/stdc++.h>
#define fn(i,n) for(int i=0;i<n;i++)
#define fn1(i,n) for(int i=1;i<=n;i++)
#define ll long long int
#define mod 1000000007
#define mp(x,y) make_pair(x,y)
int dp[2010][2010];
//dp[i][j] i sleeps j 1 hour less
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n,h,l,r;cin>>n>>h>>l>>r;
int a[n+1],p,q;
a[0]=0;
fn1(i,n){cin>>a[i];a[i]=a[i-1]+a[i];}
fn1(i,n)
{
    fn(j,i+1)
    {
        if((a[i]-j)%h<=r&&(a[i]-j)%h>=l)
            p=1;
        else
            p=0;
        if((a[i]-j-1)%h<=r&&(a[i]-j-1)%h>=l)
            q=1;
        else
            q=0;
        dp[i][j]=max(dp[i][j],dp[i-1][j]+p);
        dp[i][j+1]=max(dp[i][j+1],dp[i-1][j]+q);
    }
}
int a1=0;
fn(i,n+1)
a1=max(a1,dp[n][i]);
cout<<a1;
}


//CF1324E2
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e3+10;
int raw[maxn],l,r,h,n,ans;
int dp[maxn][maxn][2];
int DFS(int cur,int tim,int sum,bool d){
//	cout << cur << " " << tim << " " << sum << endl;
	if(cur > n)return 0;
	if(dp[cur][tim][d] != -1){
		return dp[cur][tim][d];
	}
	int tsum = sum;
	if(tim >= l and tim <= r)tsum++;
	dp[cur][tim][d] = tsum;
	int a = DFS(cur+1,(tim+raw[cur]+h)%h,sum,0);
	int b = DFS(cur+1,(tim+raw[cur]-1+h)%h,sum,1);

	tsum += max(a,b);

	ans = max(ans,tsum);
	dp[cur][tim][d] = max(dp[cur][tim][d],tsum);
	return tsum;
}
int main(){
	memset(dp,-1,sizeof(dp));
	cin >> n >> h >> l >> r;
	for(int i = 1;i <= n;i++)cin >> raw[i];
	ans = max(ans,DFS(0,0,0,0));
//	if(yay <= r and yay >= l)ans++;
	cout << ans;
	return 0;
}


//CF1324E2
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e3+10;
int raw[maxn],l,r,h,n,ans;
short dp[maxn][maxn];
int DFS(int cur,int tim,int sum){
//	cout << cur << " " << tim << " " << sum << endl;
	if(cur > n)return 0;
	if(dp[cur][tim] != -1){
//		cout << "!" << endl;
		return dp[cur][tim];
	}
	dp[cur][tim] = sum;
	int a = DFS(cur+1,(tim+raw[cur]+h)%h,sum);
	int b = DFS(cur+1,(tim+raw[cur]-1+h)%h,sum);
	if(tim >= l and tim <= r)sum++;
	sum += max(a,b);
	ans = max(ans,sum);
	dp[cur][tim] = sum;
	return sum;
}
int main(){
	memset(dp,-1,sizeof(dp));
	cin >> n >> h >> l >> r;
	for(int i = 1;i <= n;i++)cin >> raw[i];
	DFS(0,0,0);
	cout << ans;
	return 0;
}


//CF1324E2
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e3+10;
int raw[maxn],l,r,h,n,ans;
short dp[maxn][maxn];
int DFS(int cur,int tim,int sum){
//	cout << cur << " " << tim << " " << sum << endl;
	if(cur > n)return 0;
	if(dp[cur][tim] != -1){
//		cout << "!" << endl;
		return dp[cur][tim];
	}
	int a = DFS(cur+1,(tim+raw[cur]+h)%h,sum);
	int b = DFS(cur+1,(tim+raw[cur]-1+h)%h,sum);
	if(tim >= l and tim <= r)sum++;
	sum += max(a,b);
	ans = max(ans,sum);
	dp[cur][tim] = sum;
	return sum;
}
int main(){
	memset(dp,-1,sizeof(dp));
	cin >> n >> h >> l >> r;
	for(int i = 1;i <= n;i++)cin >> raw[i];
	int yay = DFS(0,0,0);
//	if(yay <= r and yay >= l)ans++;
	cout << ans;
	return 0;
}


//CF1324E2
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e3+10;
int raw[maxn],l,r,h,n,ans;
int dp[maxn][maxn];
int DFS(int cur,int tim,int sum){
//	cout << cur << " " << tim << " " << sum << endl;
	if(cur > n)return 0;
	if(dp[cur][tim] != -1){
		return dp[cur][tim];
	}
	int tsum = sum;
	if(tim >= l and tim <= r)tsum++;
	dp[cur][tim] = tsum;
	int a = DFS(cur+1,(tim+raw[cur]+h)%h,sum);
	int b = DFS(cur+1,(tim+raw[cur]-1+h)%h,sum);

	tsum += max(a,b);

	ans = max(ans,tsum);
	dp[cur][tim] = tsum;
	return tsum;
}
int main(){
	memset(dp,-1,sizeof(dp));
	cin >> n >> h >> l >> r;
	for(int i = 1;i <= n;i++)cin >> raw[i];
	ans = max(ans,DFS(0,0,0));
//	if(yay <= r and yay >= l)ans++;
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e3+10;
int f[maxn][maxn],vis[maxn][maxn],raw[maxn];
int n,h,l,r;
inline int upd(int a,int b){
	return (a+b+h)%h;
}
int main(){
	cin >> n >> h >> l >> r;
	for(int i = 1;i <= n;i++)cin >> raw[i];
	vis[0][0] = 1;
	for(int i = 1;i <= n;i++){
		for(int j = 0;j < h;j++)if(vis[i-1][j]){
			int a = upd(j,raw[i]),b = upd(j,raw[i]-1);
			vis[i][a] = vis[i][b] = 1;
			f[i][a] = max(f[i-1][j],f[i][a]);
			f[i][b] = max(f[i-1][j],f[i][b]);
		}
		for(int j = l;j <= r;j++)if(vis[i][j])f[i][j]++;
	}
	int ans = 0;
	for(int i = 1;i <= n;i++)for(int j = 0;j < h;j++)ans = max(f[i][j],ans);
	cout << ans;
}


//CF1324E2
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e3+10;
int raw[maxn],l,r,h,n,ans;
int dp[maxn][maxn];
int DFS(int cur,int tim,int sum){
//	cout << cur << " " << tim << " " << sum << endl;
	if(cur > n)return 0;
	if(dp[cur][tim] != -1){
		return dp[cur][tim];
	}
	int tsum = sum;
	if(tim >= l and tim <= r)tsum++;
	dp[cur][tim] = tsum;
	int a = DFS(cur+1,(tim+raw[cur]+h)%h,sum);
	int b = DFS(cur+1,(tim+raw[cur]-1+h)%h,sum);

	tsum += max(a,b);

	ans = max(ans,tsum);
	dp[cur][tim] = max(dp[cur][tim],tsum);
	return tsum;
}
int main(){
	memset(dp,-1,sizeof(dp));
	cin >> n >> h >> l >> r;
	for(int i = 1;i <= n;i++)cin >> raw[i];
	ans = max(ans,DFS(0,0,0));
//	if(yay <= r and yay >= l)ans++;
	cout << ans;
	return 0;
}


//CF1324E2
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e3+10;
int raw[maxn],l,r,h,n,ans;
short dp[maxn][maxn];
int DFS(int cur,int tim,int sum){
//	cout << cur << " " << tim << " " << sum << endl;
	if(cur > n)return 0;
	if(dp[tim][sum] != -1){
//		cout << "!" << endl;
		return dp[cur][tim];
	}
	dp[cur][tim] = sum;
	int a = DFS(cur+1,(tim+raw[cur]+h)%h,sum);
	int b = DFS(cur+1,(tim+raw[cur]-1+h)%h,sum);
	if(tim >= l and tim <= r)sum++;
	sum += max(a,b);
	ans = max(ans,sum);
//	dp[cur][tim] = sum;
	return sum;
}
int main(){
	memset(dp,-1,sizeof(dp));
	cin >> n >> h >> l >> r;
	for(int i = 1;i <= n;i++)cin >> raw[i];
	DFS(0,0,0);
	cout << ans;
	return 0;
}


//CF1324E2
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e3+10;
int raw[maxn],l,r,h,n,ans;
short dp[maxn][maxn];
int DFS(int cur,int tim,int sum){
	if(cur > n)return 0;
	if(dp[tim][sum])return dp[cur][tim];
	int a = DFS(cur+1,(tim+raw[cur]+h)%h,sum);
	int b = DFS(cur+1,(tim+raw[cur]-1+h)%h,sum);
	if(tim >= l and tim <= r)sum++;
	sum += max(a,b);
	ans = max(ans,sum);
	dp[cur][tim] = sum;
	return sum;
}
int main(){
	memset(dp,0,sizeof(dp));
	cin >> n >> h >> l >> r;
	for(int i = 1;i <= n;i++)cin >> raw[i];
	DFS(0,0,0);
	cout << ans;
	return 0;
}


//CF1324E2
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e3+10;
int raw[maxn],l,r,h,n,ans;
int dp[maxn][maxn];
int DFS(int cur,int tim,int sum){
//	cout << cur << " " << tim << " " << sum << endl;
	if(cur > n)return 0;
	if(dp[cur][tim] != -1){
//		cout << "!" << endl;
		return dp[cur][tim];
	}
	int a = DFS(cur+1,(tim+raw[cur]+h)%h,sum);
	int b = DFS(cur+1,(tim+raw[cur]-1+h)%h,sum);
	if(tim >= l and tim <= r)sum++;
	sum += max(a,b);
	ans = max(ans,sum);
	dp[cur][tim] = sum;
	return sum;
}
int main(){
	memset(dp,-1,sizeof(dp));
	cin >> n >> h >> l >> r;
	for(int i = 1;i <= n;i++)cin >> raw[i];
	ans = max(ans,DFS(0,0,0));
//	if(yay <= r and yay >= l)ans++;
	cout << ans;
	return 0;
}


//CF1324E
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e3+10;
int raw[maxn];
int main(){
	int n,h,l,r;
	int sum = 0,ans = 0,k = 0;
	cin >> n >> h >> l >> r;
//	cout << (1-5)%24;
	for(int i = 1;i <= n;i++){
		cin >> raw[i];
		int x = raw[i];
		k++;
		sum = (sum+x)%h;
		if(sum <= r and sum >= l){
			ans++;
//			continue;
		}
		else{
			for(int j = 1;j <= k;j++){
				if((sum-j+h)%h <= r and (sum-j+h)%h >= l){
					ans++;
					k -= j;
					sum = (sum-j+h)%h;
					break;
				}
			}
		}
//		cout << sum << " " << k << " !" << ans << endl;
	}
	cout << ans;
	return 0;
}


//CF1324E2
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e3+10;
int raw[maxn],l,r,h,n,ans,pre[maxn];
short dp[maxn][maxn];
int DFS(int cur,int tim,int sum,int rem){
//	cout << cur << " " << tim << " " << sum << endl;
	if(cur > n)return 0;
	if(dp[cur][tim] != -1){
//		cout << "!" << endl;
		return dp[cur][tim];
	}
	dp[cur][tim] = sum;
	int a = DFS(cur+1,(tim+raw[cur]+h)%h,sum,rem+1);
	int b = DFS(cur+1,(tim+raw[cur]-1+h)%h,sum,rem);
//	int wry = (tim-1+h)%h;
	if(tim >= l and tim <= r)sum++;
//	else if(wry <= r and wry >= l)sum++;
	sum += max(a,b);
	ans = max(ans,sum);

	return sum;
}
int main(){
	memset(dp,-1,sizeof(dp));
	cin >> n >> h >> l >> r;
	for(int i = 1;i <= n;i++){
		cin >> raw[i];
		pre[i] = (pre[i-1]+raw[i])%h;
	}
	DFS(0,0,0,0);
	cout << ans+1;
	return 0;
}


//CF1324E2
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e3+10;
int raw[maxn],l,r,h,n,ans;
int dp[maxn][maxn];
int DFS(int cur,int tim,int sum){
//	cout << cur << " " << tim << " " << sum << endl;
	if(cur > n)return 0;
	if(dp[cur][tim] != -1){
//		cout << "!" << endl;
		return dp[cur][tim];
	}
	int a = DFS(cur+1,(tim+raw[cur]+h)%h,sum);
	int b = DFS(cur+1,(tim+raw[cur]-1+h)%h,sum);
	if(tim >= l and tim <= r)sum++;
	sum += max(a,b);
	ans = max(ans,sum);
	dp[cur][tim] = sum;
	return sum;
}
int main(){
	memset(dp,-1,sizeof(dp));
	cin >> n >> h >> l >> r;
	for(int i = 1;i <= n;i++)cin >> raw[i];
	int yay = DFS(0,0,0);
//	if(yay <= r and yay >= l)ans++;
	cout << ans;
	return 0;
}


//CF1324E
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e3+10;
int f[maxn][maxn],vis[maxn][maxn],raw[maxn];
int n,h,l,r;
inline int upd(int a,int b){
	return (a+b+h)%h;
}
int main(){
	cin >> n >> h >> l >> r;
	for(int i = 1;i <= n;i++)cin >> raw[i];
	vis[0][0] = 1;
	for(int i = 1;i <= n;i++){
		for(int j = 0;j < h;j++)if(vis[i-1][j]){
			int a = upd(j,raw[i]),b = upd(j,raw[i]-1);
			vis[i][a] = vis[i][b] = 1;
			f[i][a] = max(f[i-1][j],f[i][a]);
			f[i][b] = max(f[i-1][j],f[i][b]);
		}
		for(int j = l;j <= r;j++)if(vis[i][j])f[i][j]++;
	}
	int ans = 0;
	for(int i = 1;i <= n;i++)for(int j = 0;j < h;j++)ans = max(f[i][j],ans);
	cout << ans;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define gt(x) x = read()
using namespace std;
typedef long long ll;
const int mxn = 8000003;
int n,h,l,r,dp[2007][2],a[2007],nowmax,sum,ans;

inline int read(int out = 0)
{
	char c;
	while((c=getchar()) < 48 || c > 57);
	while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
	return out;
}

int main()
{
	gt(n),gt(h),gt(l),gt(r);
	int tmp;
	for(int i = 1;i <= n;++i) gt(a[i]);
	for(int i = 1;i <= n;++i)
	{
		nowmax = i;
		sum += a[i];//cout<<sum<<":";
		for(int j = 0;j < nowmax;++j)
		{
			tmp = (sum-j)%h;
			if(tmp >= l && tmp <= r) tmp = 1;
			else tmp = 0;
			dp[j][i&1] = dp[j][(i&1)^1]+tmp;
			if(j > 0) dp[j][i&1] = max(dp[j][i&1],dp[j-1][(i&1)^1]+tmp);
		//	cout<<dp[j][i&1]<<" ";
		}//cout<<endl;
		tmp = (sum-nowmax)%h;
		if(tmp >= l && tmp <= r) tmp = 1;
		else tmp = 0;
		dp[nowmax][i&1] = dp[nowmax-1][(i&1)^1]+tmp;
	}
	for(int i = 0;i <= n;++i) ans = max(ans,dp[i][n&1]);
	cout<<ans;
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define gt(x) x = read()
using namespace std;
typedef long long ll;
const int mxn = 8000003;
int n,h,l,r,dp[2007][2],a[2007],nowmax,sum,ans;

inline int read(int out = 0)
{
	char c;
	while((c=getchar()) < 48 || c > 57);
	while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
	return out;
}

int main()
{
	gt(n),gt(h),gt(l),gt(r);
	int tmp;
	for(int i = 1;i <= n;++i) gt(a[i]);
	for(int i = 1;i <= n;++i)
	{
		nowmax = i;
		sum += a[i];//cout<<sum<<":";
		for(int j = 0;j <= nowmax;++j)
		{
			tmp = (sum-j)%h;
			if(tmp >= l && tmp <= r) tmp = 1;
			else tmp = 0;
			dp[j][i&1] = dp[j][(i&1)^1]+tmp;
			tmp = (sum-j-1)%h;
			if(tmp >= l && tmp <= r) tmp = 1;
			else tmp = 0;
			if(j > 0) dp[j][i&1] = max(dp[j][i&1],dp[j-1][(i&1)^1]+tmp);
		//	cout<<dp[j][i&1]<<" ";
		}//cout<<endl;
		sum += h;
	}
	for(int i = 0;i <= n;++i) ans = max(ans,dp[i][n&1]);
	cout<<ans;
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define gt(x) x = read()
using namespace std;
typedef long long ll;
const int mxn = 8000003;
int n,h,l,r,dp[2007][2],a[2007],nowmax,sum,ans;

inline int read(int out = 0)
{
	char c;
	while((c=getchar()) < 48 || c > 57);
	while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
	return out;
}

int main()
{
	gt(n),gt(h),gt(l),gt(r);
	int tmp;
	for(int i = 1;i <= n;++i) gt(a[i]);
	for(int i = 1;i <= n;++i)
	{
		nowmax = i;
		sum += a[i];//cout<<sum<<":";
		for(int j = 0;j < nowmax;++j)
		{
			tmp = (sum-j)%h;
			if(tmp >= l && tmp <= r) tmp = 1;
			else tmp = 0;
			dp[j][i&1] = dp[j][(i&1)^1]+tmp;
			tmp = (sum-j-1)%h;
			if(tmp >= l && tmp <= r) tmp = 1;
			else tmp = 0;
			if(j > 0) dp[j][i&1] = max(dp[j][i&1],dp[j-1][(i&1)^1]+tmp);
		//	cout<<dp[j][i&1]<<" ";
		}//cout<<endl;
		tmp = (sum-nowmax)%h;
		if(tmp >= l && tmp <= r) tmp = 1;
		else tmp = 0;
		dp[nowmax][i&1] = dp[nowmax-1][(i&1)^1]+tmp;
	}
	for(int i = 0;i <= n;++i) ans = max(ans,dp[i][n&1]);
	cout<<ans;
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define gt(x) x = read()
using namespace std;
typedef long long ll;
const int mxn = 8000003;
int n,h,l,r,dp[2007][2],a[2007],nowmax,sum,ans;

inline int read(int out = 0)
{
	char c;
	while((c=getchar()) < 48 || c > 57);
	while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
	return out;
}

int main()
{
	gt(n),gt(h),gt(l),gt(r);
	int tmp;
	for(int i = 1;i <= n;++i) gt(a[i]);
	for(int i = 1;i <= n;++i)
	{
		nowmax = i-1;
		sum += a[i];
		for(int j = 0;j <= nowmax;++j)
		{
			tmp = (sum-j-1)%h;++tmp;
			if(tmp >= l && tmp <= r) tmp = 1;
			else tmp = 0;
			dp[j][i&1] = dp[j][(i&1)^1]+tmp;
			tmp = (sum-j-2)%h;++tmp;
			if(tmp >= l && tmp <= r) tmp = 1;
			else tmp = 0;
			if(j > 0) dp[j][i&1] = max(dp[j][i&1],dp[j-1][(i&1)^1]+tmp);
		}
		sum += h;
	}
	for(int i = 0;i <= n;++i) ans = max(ans,dp[i][n&1]);
	cout<<ans;
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define gt(x) x = read()
using namespace std;
typedef long long ll;
const int mxn = 8000003;
int n,h,l,r,dp[2007][2],a[2007],nowmax,sum,ans;

inline int read(int out = 0)
{
	char c;
	while((c=getchar()) < 48 || c > 57);
	while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
	return out;
}

int main()
{
	gt(n),gt(h),gt(l),gt(r);
	int tmp;
	for(int i = 1;i <= n;++i) gt(a[i]);
	for(int i = 1;i <= n;++i)
	{
		nowmax = i;
		sum += a[i];
		for(int j = 0;j <= nowmax;++j)
		{
			tmp = (sum-j)%h;++tmp;
			if(tmp >= l && tmp <= r) tmp = 1;
			else tmp = 0;
			dp[j][i&1] = dp[j][(i&1)^1]+tmp;
			tmp = (sum-j-1)%h;++tmp;
			if(tmp >= l && tmp <= r) tmp = 1;
			else tmp = 0;
			if(j > 0) dp[j][i&1] = max(dp[j][i&1],dp[j-1][(i&1)^1]+tmp);
		}
		sum += h;
	}
	for(int i = 0;i <= n;++i) ans = max(ans,dp[i][n&1]);
	cout<<ans;
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define gt(x) x = read()
using namespace std;
typedef long long ll;
const int mxn = 8000003;
int n,h,l,r,dp[2007][2],a[2007],nowmax,sum,ans;

inline int read(int out = 0)
{
	char c;
	while((c=getchar()) < 48 || c > 57);
	while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
	return out;
}

int main()
{
	gt(n),gt(h),gt(l),gt(r);
	int tmp;
	for(int i = 1;i <= n;++i) gt(a[i]);
	for(int i = 1;i <= n;++i)
	{
		nowmax = i-1;
		sum += a[i];
		for(int j = 0;j <= nowmax;++j)
		{
			tmp = (sum-j)%h;++tmp;
			if(tmp >= l && tmp <= r) tmp = 1;
			else tmp = 0;
			dp[j][i&1] = dp[j][(i&1)^1]+tmp;
			tmp = (sum-j-1)%h;++tmp;
			if(tmp >= l && tmp <= r) tmp = 1;
			else tmp = 0;
			if(j > 0) dp[j][i&1] = max(dp[j][i&1],dp[j-1][(i&1)^1]+tmp);
		}
		sum += h;
	}
	for(int i = 0;i <= n;++i) ans = max(ans,dp[i][n&1]);
	cout<<ans;
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define gt(x) x = read()
using namespace std;
typedef long long ll;
const int mxn = 8000003;
int n,h,l,r,dp[2007][2],a[2007],nowmax,sum,ans;

inline int read(int out = 0)
{
	char c;
	while((c=getchar()) < 48 || c > 57);
	while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
	return out;
}

int main()
{
	gt(n),gt(h),gt(l),gt(r);
	int tmp;
	for(int i = 1;i <= n;++i) gt(a[i]);
	for(int i = 1;i <= n;++i)
	{
		nowmax = i;
		sum += a[i];cout<<sum<<":";
		for(int j = 0;j <= nowmax;++j)
		{
			tmp = (sum-j)%h;
			if(tmp >= l && tmp <= r) tmp = 1;
			else tmp = 0;
			dp[j][i&1] = dp[j][(i&1)^1]+tmp;
			tmp = (sum-j-1)%h;
			if(tmp >= l && tmp <= r) tmp = 1;
			else tmp = 0;
			if(j > 0) dp[j][i&1] = max(dp[j][i&1],dp[j-1][(i&1)^1]+tmp);
			cout<<dp[j][i&1]<<" ";
		}cout<<endl;
		sum += h;
	}
	for(int i = 0;i <= n;++i) ans = max(ans,dp[i][n&1]);
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

const int maxn=2e3+5;

const ll mod=998244353;

int dp[maxn][maxn];

int a[maxn];int sum[maxn];

int main(){
	int n,h,l,r;
	scanf("%d%d%d%d",&n,&h,&l,&r);
	for(int i=1;i<=n;i++){
		scanf("%d",a+i);
		sum[i]=sum[i-1]+a[i];
	}
	for(int i=1;i<=n;i++){
		dp[i][0]=dp[i-1][0];
		if(sum[i-1]%h>=l&&sum[i-1]%h<=r) dp[i][0]++;
		for(int j=1;j<=i;j++){
			int k1=0;
			if((sum[i-1]-j+h)%h>=l&&(sum[i-1]-j+h)%h<=r) k1=1;
			int k2=0;
			if((sum[i-1]-j+1+h)%h>=l&&(sum[i-1]-j+1+h)%h<=r) k2=1;
			dp[i][j]=max(dp[i-1][j]+k1,dp[i-1][j-1]+k2);
		}
	}
	int ans=0;
	for(int i=1;i<=n;i++){
		ans=max(ans,dp[n][i]);
	}
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

const int maxn=2e3+5;

const ll mod=998244353;

int dp[maxn][maxn];

int a[maxn];int sum[maxn];

int main(){
	int n,h,l,r;
	scanf("%d%d%d%d",&n,&h,&l,&r);
	for(int i=1;i<=n;i++){
		scanf("%d",a+i);
		sum[i]=sum[i-1]+a[i];
	}
	for(int i=1;i<=n;i++){
		for(int j=0;j<=i;j++){
			dp[i][j]=dp[i-1][j];
			int tmp=(sum[i]+h-j)%h;
			if(tmp<=l&&tmp+n>=r) dp[i][j]++;
		}
	}
	int ans=0;
	for(int i=1;i<=n;i++){
		ans=max(ans,dp[n][i]);
	}
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

const int maxn=2e3+5;

const ll mod=998244353;

int dp[maxn][maxn];

int a[maxn];int sum[maxn];

int main(){
	int n,h,l,r;
	scanf("%d%d%d%d",&n,&h,&l,&r);
	for(int i=1;i<=n;i++){
		scanf("%d",a+i);
		sum[i]=sum[i-1]+a[i];
	}
	for(int i=1;i<=n;i++){
		dp[i][0]=dp[i-1][0];
		if(sum[i-1]%h>=l&&sum[i-1]%h<=r) dp[i][0]++;
		for(int j=1;j<=i;j++){
			int ll1=(sum[i-1]-j+h)%h;
			int tmp1=dp[i-1][j];
			if(ll1>=l&&ll1<=r) tmp1++;
			int ll2=(sum[i-1]-j+1+h)%h;
			int tmp2=dp[i-1][j-1];
			if(ll2>=l&&ll2<=r) tmp2++;
			dp[i][j]=max(tmp1,tmp2);
		}
	}
	int ans=0;
	for(int i=1;i<=n;i++){
		ans=max(ans,dp[n][i]);
	}
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

const int maxn=2e3+5;

const ll mod=998244353;

int dp[maxn][maxn];

int a[maxn];int sum[maxn];

int main(){
	int n,h,l,r;
	scanf("%d%d%d%d",&n,&h,&l,&r);
	for(int i=1;i<=n;i++){
		scanf("%d",a+i);
		sum[i]=sum[i-1]+a[i];
	}
	for(int i=1;i<=n;i++){
		dp[i][0]=dp[i-1][0];
		if(sum[i]%h>=l&&sum[i]%h<=r) dp[i][0]++;
		for(int j=1;j<=i;j++){
			int k1=0;
			if((sum[i]-j)%h>=l&&(sum[i-1]-j)%h<=r) k1=1;
			dp[i][j]=max(dp[i-1][j]+k1,dp[i-1][j-1]+k1);
		}
	}
	int ans=0;
	for(int i=0;i<=n;i++){
		ans=max(ans,dp[n][i]);
	}
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

const int maxn=2e3+5;

const ll mod=998244353;

int dp[maxn][maxn];

int a[maxn];int sum[maxn];

int main(){
	int n,h,l,r;
	scanf("%d%d%d%d",&n,&h,&l,&r);
	for(int i=1;i<=n;i++){
		scanf("%d",a+i);
		sum[i]=sum[i-1]+a[i];
	}
	for(int i=1;i<=n;i++){
		dp[i][0]=dp[i-1][0];
		if(sum[i]%h>=l&&sum[i]%h<=r) dp[i][0]++;
		for(int j=1;j<=i;j++){
			int k1=0;
			if((sum[i]-j)%h>=l&&(sum[i]-j)%h<=r) k1=1;
			dp[i][j]=max(dp[i-1][j]+k1,dp[i-1][j-1]+k1);
		}
	}
	int ans=0;
	for(int i=0;i<=n;i++){
		ans=max(ans,dp[n][i]);
	}
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

const int maxn=2e3+5;

const ll mod=998244353;

int dp[maxn][maxn];

int a[maxn];int sum[maxn];

int main(){
	int n,h,l,r;
	scanf("%d%d%d%d",&n,&h,&l,&r);
	for(int i=1;i<=n;i++){
		scanf("%d",a+i);
		sum[i]=sum[i-1]+a[i];
	}
	for(int i=1;i<=n;i++){
		dp[i][0]=dp[i-1][0];
		if(sum[i-1]%h>=l&&sum[i-1]%h<=r) dp[i][0]++;
		for(int j=1;j<=i;j++){
			int k1=0;
			if((sum[i-1]-j+h)%h>=l&&(sum[i-1]-j+h)%h<=r) k1=1;
			int k2=0;
			if((sum[i-1]-j+1+h)%h>=l&&(sum[i-1]-j+1+h)%h<=r) k2=1;
			dp[i][j]=max(dp[i-1][j]+k1,dp[i-1][j-1]+k2);
		}
	}
	int ans=0;
	for(int i=0;i<=n;i++){
		ans=max(ans,dp[n][i]);
	}
	printf("%d\n",ans);
	return 0;
}


import java.util.*;
import java.io.*;
public class Main {
    static class Scan {
        private byte[] buf=new byte[1024];
        private int index;
        private InputStream in;
        private int total;
        public Scan()
        {
            in=System.in;
        }
        public int scan()throws IOException
        {
            if(total<0)
            throw new InputMismatchException();
            if(index>=total)
            {
                index=0;
                total=in.read(buf);
                if(total<=0)
                return -1;
            }
            return buf[index++];
        }
        public int scanInt()throws IOException
        {
            int integer=0;
            int n=scan();
            while(isWhiteSpace(n))
            n=scan();
            int neg=1;
            if(n=='-')
            {
                neg=-1;
                n=scan();
            }
            while(!isWhiteSpace(n))
            {
                if(n>='0'&&n<='9')
                {
                    integer*=10;
                    integer+=n-'0';
                    n=scan();
                }
                else throw new InputMismatchException();
            }
            return neg*integer;
        }
        public double scanDouble()throws IOException
        {
            double doub=0;
            int n=scan();
            while(isWhiteSpace(n))
            n=scan();
            int neg=1;
            if(n=='-')
            {
                neg=-1;
                n=scan();
            }
            while(!isWhiteSpace(n)&&n!='.')
            {
                if(n>='0'&&n<='9')
                {
                    doub*=10;
                    doub+=n-'0';
                    n=scan();
                }
                else throw new InputMismatchException();
            }
            if(n=='.')
            {
                n=scan();
                double temp=1;
                while(!isWhiteSpace(n))
                {
                    if(n>='0'&&n<='9')
                    {
                        temp/=10;
                        doub+=(n-'0')*temp;
                        n=scan();
                    }
                    else throw new InputMismatchException();
                }
            }
            return doub*neg;
        }
        public String scanString()throws IOException
        {
            StringBuilder sb=new StringBuilder();
            int n=scan();
            while(isWhiteSpace(n))
            n=scan();
            while(!isWhiteSpace(n))
            {
                sb.append((char)n);
                n=scan();
            }
            return sb.toString();
        }
        private boolean isWhiteSpace(int n)
        {
            if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1)
            return true;
            return false;
        }
    }
    static int color[],diff[],ans[];
    static ArrayList<Integer> adj_lst[];
    public static void main(String args[]) throws IOException {
        Scan input=new Scan();
        int n=input.scanInt();
        color=new int[n];
        for(int i=0;i<n;i++) {
            color[i]=input.scanInt();
        }
        adj_lst=new ArrayList[n];
        for(int i=0;i<n;i++) {
            adj_lst[i]=new ArrayList<>();
        }
        for(int i=0;i<n-1;i++) {
            int u=input.scanInt()-1;
            int v=input.scanInt()-1;
            adj_lst[u].add(v);
            adj_lst[v].add(u);
        }
        diff=new int[n];
        ans=new int[n];
        calc_diff(0,-1);
        ans[0]=diff[0];
        solve(0,-1);
        for(int i=0;i<n;i++) {
            System.out.print(ans[i]+" ");
        }
        System.out.println();
    }
    public static void solve(int root,int par) {
        int tmp=diff[root];
        if(root!=0) {
            if(diff[root]>0) {
                if(color[par]==0) {
                    diff[par]=Math.max(diff[par]-diff[root],-1);
                }
                else {
                    diff[par]=Math.max(diff[par]-diff[root],1);
                }
            }
//            System.out.println((root+1)+" "+(par+1)+" "+diff[root]+" "+diff[par]);
            if(diff[par]>0) {
                diff[root]+=diff[par];
            }
            ans[root]=diff[root];
        }

//        System.out.println(root+1);
//        for(int i=0;i<diff.length;i++) {
//            System.out.print(diff[i]+" ");
//        }
//        System.out.println();

        for(int i=0;i<adj_lst[root].size();i++) {
            if(adj_lst[root].get(i)==par) {
                continue;
            }
            solve(adj_lst[root].get(i),root);
            diff[root]=tmp;
        }
        diff[root]=tmp;
    }
    public static void calc_diff(int root,int par) {
        for(int i=0;i<adj_lst[root].size();i++) {
            if(adj_lst[root].get(i)==par) {
                continue;
            }
            calc_diff(adj_lst[root].get(i),root);
        }
        if(color[root]==0) {
            diff[root]=-1;
        }
        else {
            diff[root]=1;
        }
        for(int i=0;i<adj_lst[root].size();i++) {
            if(adj_lst[root].get(i)==par) {
                continue;
            }
            if(diff[adj_lst[root].get(i)]>0) {
                diff[root]+=diff[adj_lst[root].get(i)];
            }
        }
    }
}


import java.util.*;
import java.io.*;

public class  Polo_the_Penguin_and_Matrix
{
	public static void main(String[] args) throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		PrintWriter pw=new PrintWriter(System.out);

		int t=1;

		while(t-->0)
		{
			String str[]=br.readLine().trim().split(" ");
			int n=Integer.parseInt(str[0]);
			int m=Integer.parseInt(str[1]);
			int d=Integer.parseInt(str[2]);

			a=new int[n][m];
			int max=Integer.MIN_VALUE;
			for(int i=0;i<n;i++)
			{
				str=br.readLine().trim().split(" ");
				for(int j=0;j<m;j++)
				{
					a[i][j]=Integer.parseInt(str[j]);

					max=Math.max(max,a[i][j]);
				}
			}

			long ans=Long.MAX_VALUE;

			for(int i=0;i<=max;i++)
			{
				count=0;
				if(check(n,m,i,d))
				{
					if(ans>count)
						ans=count;
				}
			}

			pw.println(ans==Long.MAX_VALUE?-1:ans);
		}
		pw.flush();
	}
	static int a[][];
	static long count=0;

	public static boolean check(int n,int m,int x,int d)
	{
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				int dif=Math.abs(a[i][j]-x);

				if(dif%d!=0)
					return false;
				else
					count+=(dif/d);
			}
		}

		return true;
	}
}


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class problemB {
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        int n = scan.nextInt();
        int m = scan.nextInt();
        int d = scan.nextInt();

        int[] a = new int[n * m];

        int low = 0;
        int high = 0;

        for (int i = 0; i < n * m; i++) {
            a[i] = scan.nextInt();
        }

        Arrays.sort(a);

        int num = a[a.length / 2];

        int counter = 0;

        for (int i = 0; i < a.length; i++) {
            if (a[i] < num) {
                if ((num - a[i]) % d == 0) {
                    counter += (num - a[i]) / d;
                } else {
                    counter = -1;
                    break;
                }
            } else if (a[i] > num) {
                if ((a[i] - num) % d == 0) {
                    counter += (a[i] - num) / d;
                } else {
                    counter = -1;
                    break;
                }
            }
        }

        System.out.print(counter);

    }
}


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class problemB {
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        int n = scan.nextInt();
        int m = scan.nextInt();
        int d = scan.nextInt();

        int[] a = new int[n * m];

        int low = 0;
        int high = 0;

        for (int i = 0; i < n * m; i++) {
            a[i] = scan.nextInt();
        }

        Arrays.sort(a);

        int num = a[a.length / 2];

        int counter = 0;

        for (int i = 0; i < a.length; i++) {
            if (a[i] < num) {
                if ((num - a[i]) % d == 0) {
                    counter += (num - a[i]) / d;
                } else {
                    counter = -1;
                    break;
                }
            } else if (a[i] > num) {
                if ((a[i] - num) % d == 0) {
                    counter += (a[i] - num) / d;
                } else {
                    counter = -1;
                    break;
                }
            }
        }

        System.out.print(counter);
    }
}


import java.util.ArrayList;
import java.util.Scanner;

public class problemB {
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        int n = scan.nextInt();
        int m = scan.nextInt();
        int d = scan.nextInt();

        int[][] a = new int[n][m];

        int low = 0;
        int high = 0;

        for (int i = 0; i < n; i++) {

            for (int j = 0; j < m; j++) {

                a[i][j] = scan.nextInt();
                if (i == 0 && j == 0) {
                    low = a[i][j];
                    high = a[i][j];
                } else {
                    if (a[i][j] < low) {
                        low = a[i][j];
                    }
                    if (a[i][j] > high) {
                        high = a[i][j];
                    }
                }

            }

        }

        int counter = 0;
        int num = high - low;

        for (int i = 0; i < n; i++) {

            for (int j = 0; j < m; j++) {

                if (a[i][j] < num) {
                    if ((num - a[i][j]) % d == 0) {
                        counter += (num - a[i][j]) / d;
                    } else {
                        counter = -1;
                        break;
                    }
                } else if (a[i][j] > num) {
                    if ((a[i][j] - num) % d == 0) {
                        counter += (a[i][j] - num) / d;
                    } else {
                        counter = -1;
                        break;
                    }
                }

            }

            if (counter == -1) {
                break;
            }

        }

        System.out.print(counter);

    }
}


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

/**
 *
 * @author KCc
 */
public class ACM_33 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner s = new Scanner(System.in);
        int n,m,d;
        n = s.nextInt();
        m = s.nextInt();
        d = s.nextInt();
        int size = n * m;
        ArrayList<Integer> list = new ArrayList<Integer>();
        int t = s.nextInt();
        list.add(t);
        for (int i = 1; i < size; i++){
            int x = s.nextInt();
            if (Math.abs(x - list.get(0)) % d != 0){
                System.out.println(-1);
                return;
            }
            list.add(x);
        }
        Collections.sort(list);
        int med = size / 2;
        int temp = list.get(med);
        int counter = 0;
        for (int i = 0; i < list.size(); i++){
            counter += Math.abs(temp - list.get(i)) / d;
        }
        System.out.println(counter);
    }

}


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

/**
 *
 * @author KCc
 */
public class ACM_33 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner s = new Scanner(System.in);

        int n,m,d;
        n = s.nextInt();
        m = s.nextInt();
        d = s.nextInt();
        int size = n * m;
        ArrayList<Integer> list = new ArrayList<Integer>();
        int t = s.nextInt();
        list.add(t);
        for (int i = 1; i < size; i++){
            int x = s.nextInt();
            if (Math.abs(x - list.get(0)) % d != 0){
                System.out.println(-1);
                return;
            }
            list.add(x);
        }
        Collections.sort(list);
        int med = size / 2;
        int temp = list.get(med);
        int counter = 0;
        for (int i = 0; i < list.size(); i++){
            counter += Math.abs(temp - list.get(i)) / d;
        }
        System.out.println(counter);
    }

}


*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

/**
 *
 * @author KCc
 */
public class ACM_33 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner s = new Scanner(System.in);
        int n,m,d;
        n = s.nextInt();
        m = s.nextInt();
        d = s.nextInt();
        int size = n * m;
        ArrayList<Integer> list = new ArrayList<Integer>();
        int t = s.nextInt();
        list.add(t);
        for (int i = 1; i < size; i++){
            int x = s.nextInt();
            if (Math.abs(x - list.get(0)) % d != 0){
                System.out.println(-1);
                return;
            }
            list.add(x);
        }
        Collections.sort(list);
        int med = size / 2;
        int temp = list.get(med);
        int counter = 0;
        for (int i = 0; i < list.size(); i++){
            counter += Math.abs(temp - list.get(i)) / d;
        }
        System.out.println(counter);
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

/**
 * Created by spss on 1/11/16.
 */
public class Problem289B {
    public static class MyScanner{
        static BufferedReader br;
        static StringTokenizer st;

        MyScanner(){
            br = new BufferedReader(new InputStreamReader(System.in));
            st = null;
        }

        String next() throws IOException {
            while(st == null || !st.hasMoreElements()){
                st = new StringTokenizer(br.readLine());
            }

            return st.nextToken();
        }

        int nextInt() throws IOException{
            return Integer.parseInt(next());
        }
    }

    public static void main(String[] args) throws IOException{
        int n,m,d;
        MyScanner scan = new MyScanner();

        n = scan.nextInt();
        m = scan.nextInt();
        d = scan.nextInt();

        int minA = Integer.MAX_VALUE;
        int maxA = Integer.MIN_VALUE;
        int[][] matrix = new int[n][m];
        for(int i=0;i<n;i++) {
            for (int j = 0; j < m; j++) {
                int t = matrix[i][j] = scan.nextInt();
                minA = Math.min(t, minA);
                maxA = Math.max(t, maxA);
            }
        }

        if( (maxA - minA) % d != 0 ){
            System.out.println(-1);
            return;
        }
        if(maxA == minA){
            System.out.println(0);
            return;
        }

        boolean impossible = false;
        int min_count = Integer.MAX_VALUE;
        for(int delta=0;delta<(maxA - minA);delta += d){
            int count = 0;
            for(int i=0;i<n;i++){
                for(int j=0;j<m;j++){
                    int diff = Math.abs(matrix[i][j] - minA - delta);
                    if(diff % d != 0){
                        impossible = true;
                        break;
                    }
                    count += diff/d;
                }
                if(impossible) break;
            }
            if(impossible) break;

            if(count < min_count) min_count = count;
        }

        if(impossible){
            System.out.println(-1);
            return;
        }

        System.out.println(min_count);
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

/**
 * Created by spss on 1/11/16.
 */
public class Problem289B {
    public static class MyScanner{
        static BufferedReader br;
        static StringTokenizer st;

        MyScanner(){
            br = new BufferedReader(new InputStreamReader(System.in));
            st = null;
        }

        String next() throws IOException {
            while(st == null || !st.hasMoreElements()){
                st = new StringTokenizer(br.readLine());
            }

            return st.nextToken();
        }

        int nextInt() throws IOException{
            return Integer.parseInt(next());
        }
    }

    public static void main(String[] args) throws IOException{
        int n,m,d;
        MyScanner scan = new MyScanner();

        n = scan.nextInt();
        m = scan.nextInt();
        d = scan.nextInt();

        int minA = Integer.MAX_VALUE;
        int maxA = Integer.MIN_VALUE;
        int[][] matrix = new int[n][m];
        for(int i=0;i<n;i++) {
            for (int j = 0; j < m; j++) {
                int t = matrix[i][j] = scan.nextInt();
                minA = Math.min(t, minA);
                maxA = Math.max(t, maxA);
            }
        }

        if( (maxA - minA) % d != 0 ){
            System.out.println(-1);
            return;
        }

        boolean impossible = false;
        int min_count = Integer.MAX_VALUE;
        for(int delta=0;delta<(maxA - minA);delta += d){
            int count = 0;
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                    int diff = Math.abs(matrix[i][j] - minA - delta);
                    if(diff % d != 0){
                        impossible = true;
                        break;
                    }
                    count += diff/d;
                }
                if(impossible) break;
            }
            if(impossible) break;

            if(count < min_count) min_count = count;
        }

        if(impossible){
            System.out.println(-1);
            return;
        }

        System.out.println(min_count);
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;

/**
 * Created by spss on 1/11/16.
 */
public class Problem289B {
    public static class MyScanner{
        static BufferedReader br;
        static StringTokenizer st;

        MyScanner(){
            br = new BufferedReader(new InputStreamReader(System.in));
            st = null;
        }

        String next() throws IOException {
            while(st == null || !st.hasMoreElements()){
                st = new StringTokenizer(br.readLine());
            }

            return st.nextToken();
        }

        int nextInt() throws IOException{
            return Integer.parseInt(next());
        }
    }

    public static void main(String[] args) throws IOException{
        int n,m,d,N;
        MyScanner scan = new MyScanner();

        n = scan.nextInt();
        m = scan.nextInt();
        d = scan.nextInt();
        N = n*m;

        ArrayList<Integer> A = new ArrayList<>();
        for(int i=0;i<N;i++){
            int t = scan.nextInt();
            A.add(t);
        }
        Collections.sort(A);
        int minA = A.get(0);
        int maxA = A.get(A.size()-1);

        //check the obvious
        if(maxA == minA){
            System.out.println(0);
            return;
        }

        //check for possibility
        if( (maxA - minA) % d != 0 ){
            System.out.println(-1);
            return;
        }
        boolean impossible = false;
        for(int i=0;i<N-1;i++){
            int ai = A.get(i);
            int ai_1 = A.get(i+1);

            if((ai_1 - ai) % d != 0){
                impossible = true;
                break;
            }
        }
        if(impossible){
            System.out.println(-1);
            return;
        }

        //now we can go on computing stuff
        ArrayList<Integer> S = new ArrayList<>();
        for(int k=0;k<N-1;k++){
            int s = 0;
            for(int i=0;i<=k;i++){
                s += A.get(k) - A.get(i);
            }
            for(int i=k+1;i<N;i++){
                s += A.get(i) - A.get(k+1);
            }

            S.add(s);
        }

        //System.out.print("S:");
        //for(int i=0;i<S.size();i++) System.out.print(S.get(i) + " ");
        //System.out.println();

        int minC = Integer.MAX_VALUE;
        int mu = A.get(0);
        int k = 0;
        do{
            //System.out.println("mu = " + mu);
            //System.out.println("k = " + k);

            int C = (k+1)*(mu - A.get(k)) + (N-k-1)*(A.get(k+1) - mu) + S.get(k);
            if(C < minC) minC = C;

            mu += d;
            if(mu > A.get(A.size()-1)) break;

            while(k < N-1 && A.get(k+1) < mu) k++;
        } while(mu <= A.get(A.size()-1) && k < N-1);

        System.out.println(minC/d);
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

/**
 * Created by spss on 1/11/16.
 */
public class Problem289B {
    public static class MyScanner{
        static BufferedReader br;
        static StringTokenizer st;

        MyScanner(){
            br = new BufferedReader(new InputStreamReader(System.in));
            st = null;
        }

        String next() throws IOException {
            while(st == null || !st.hasMoreElements()){
                st = new StringTokenizer(br.readLine());
            }

            return st.nextToken();
        }

        int nextInt() throws IOException{
            return Integer.parseInt(next());
        }
    }

    public static void main(String[] args) throws IOException{
        int n,m,d;
        MyScanner scan = new MyScanner();

        n = scan.nextInt();
        m = scan.nextInt();
        d = scan.nextInt();

        int minA = Integer.MAX_VALUE;
        int maxA = Integer.MIN_VALUE;
        int[][] matrix = new int[n][m];
        for(int i=0;i<n;i++) {
            for (int j = 0; j < m; j++) {
                int t = matrix[i][j] = scan.nextInt();
                minA = Math.min(t, minA);
                maxA = Math.max(t, maxA);
            }
        }

        if( (maxA - minA) % d != 0 ){
            System.out.println(-1);
            return;
        }

        boolean impossible = false;
        int min_count = Integer.MAX_VALUE;
        for(int delta=0;delta<(maxA - minA);delta += d){
            int count = 0;
            for(int i=0;i<n;i++){
                for(int j=0;j<m;j++){
                    int diff = Math.abs(matrix[i][j] - minA - delta);
                    if(diff % d != 0){
                        impossible = true;
                        break;
                    }
                    count += diff/d;
                }
                if(impossible) break;
            }
            if(impossible) break;

            if(count < min_count) min_count = count;
        }

        if(impossible){
            System.out.println(-1);
            return;
        }

        System.out.println(min_count);
    }
}


import java.util.*;
import java.io.*;
public class a
{
	public static void main(String[] arg) throws IOException
	{
		new a();
	}
	public a() throws IOException
	{
		FastScanner in = new FastScanner(System.in);
		int n = in.nextInt(); int m = in.nextInt(); int d = in.nextInt();
		int[][] mat = new int[n][m];
		for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) mat[i][j] = in.nextInt();
		boolean flag = false;
		for(int i = 0; i < n; i++)
		{
			for(int j = 0; j < m; j++)
			{
				if(mat[i][j]%d != mat[0][0]%d) flag = true;
			}
		}
		if(flag)
		{
			System.out.println("-1");
		}
		else
		{
			ArrayList<Integer> list = new ArrayList<Integer>();
			for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) list.add(mat[i][j]);
			int ret = Integer.MAX_VALUE;
			for(int i = mat[0][0]%d; i < 15000; i+=d)
			{
				int cur = 0;
				for(int j = 0; j < list.size(); j++)
				{
					cur += Math.abs(list.get(j)-i)/d;
				}
				ret = Math.min(ret, cur);
			}
			System.out.println(ret);
		}
		in.close();
	}
	class FastScanner
	{
		BufferedReader br;
		StringTokenizer st;
		public FastScanner(InputStream in)
		{
			br = new BufferedReader(new InputStreamReader(in));
			st = new StringTokenizer("");
		}
		public String next() throws IOException
		{
			while(!st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}
		public int nextInt() throws IOException
		{
			return Integer.parseInt(next());
		}
		public long nextLong() throws IOException
		{
			return Long.parseLong(next());
		}
		public void close() throws IOException
		{
			br.close();
		}
	}
}


import java.util.*;
import java.io.*;
public class a
{
	public static void main(String[] arg) throws IOException
	{
		new a();
	}
	public a() throws IOException
	{
		FastScanner in = new FastScanner(System.in);
		int n = in.nextInt(); int m = in.nextInt(); int d = in.nextInt();
		int[][] mat = new int[n][m];
		for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) mat[i][j] = in.nextInt();
		boolean flag = false;
		for(int i = 0; i < n; i++)
		{
			for(int j = 0; j < m; j++)
			{
				if(mat[i][j]%d != mat[0][0]%d) flag = true;
			}
		}
		if(flag)
		{
			System.out.println("-1");
		}
		else
		{
			ArrayList<Integer> list = new ArrayList<Integer>();
			for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) list.add(mat[i][j]);
			int ret = Integer.MAX_VALUE;
			for(int i = mat[0][0]%d; i < 11000; i+=d)
			{
				int cur = 0;
				for(int j = 0; j < list.size(); j++)
				{
					cur += Math.abs(list.get(j)-i)/d;
				}
				ret = Math.min(ret, cur);
			}
			System.out.println(ret);
		}
		in.close();
	}
	class FastScanner
	{
		BufferedReader br;
		StringTokenizer st;
		public FastScanner(InputStream in)
		{
			br = new BufferedReader(new InputStreamReader(in));
			st = new StringTokenizer("");
		}
		public String next() throws IOException
		{
			while(!st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}
		public int nextInt() throws IOException
		{
			return Integer.parseInt(next());
		}
		public long nextLong() throws IOException
		{
			return Long.parseLong(next());
		}
		public void close() throws IOException
		{
			br.close();
		}
	}
}


import java.util.*;
import java.io.*;
public class a
{
	public static void main(String[] arg) throws IOException
	{
		new a();
	}
	public a() throws IOException
	{
		FastScanner in = new FastScanner(System.in);
		int n = in.nextInt(); int m = in.nextInt(); int d = in.nextInt();
		ArrayList<Integer> list = new ArrayList<Integer>();
		int max = 0;
		for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) list.add(in.nextInt());
		boolean flag = false;
		for(int i = 0; i < n*m; i++)
		{
			if(list.get(0)%d != list.get(i)%d) flag = true;
			max = Math.max(list.get(i), max);
		}
		if(flag)
		{
			System.out.println("-1");
		}
		else
		{
			int ret = Integer.MAX_VALUE;
			for(int i = list.get(0)%d; i <= max; i+=d)
			{
				int cur = 0;
				for(int j = 0; j < list.size(); j++)
				{
					cur += Math.abs(list.get(j)-i)/d;
				}
				ret = Math.min(ret, cur);
			}
			System.out.println(ret);
		}
		in.close();
	}
	class FastScanner
	{
		BufferedReader br;
		StringTokenizer st;
		public FastScanner(InputStream in)
		{
			br = new BufferedReader(new InputStreamReader(in));
			st = new StringTokenizer("");
		}
		public String next() throws IOException
		{
			while(!st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}
		public int nextInt() throws IOException
		{
			return Integer.parseInt(next());
		}
		public long nextLong() throws IOException
		{
			return Long.parseLong(next());
		}
		public void close() throws IOException
		{
			br.close();
		}
	}
}