#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

ll solve() {
  int n;
  cin >> n;

  vector<int> arr(n);
  for(int i=0; i<n; i++)
    cin >> arr[i];

  for(int i=0; i<n; i++){
    if(arr[i] > 0){
      int j = i+1;
      while(arr[i] >0 && j<n){
        if(arr[j] < 0){
          int t = min(arr[i], abs(arr[j]));
          arr[i] -= t;
          arr[j] += t;
        }
        j++;
      }
    }
  }

  ll count = 0;
  for(int i=0; i<n; i++){
    if(arr[i] > 0) count += arr[i];
  }

  return count;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    cout << solve() << '\n';
  }
  return 0;
}


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

ll solve() {
  int n;
  cin >> n;
  vector<int> arr(n);
  for(int i=0; i<n; i++)
    cin >> arr[i];

  for(int i=0; i<n; i++){
    if(arr[i] > 0){
      int j = i+1;
      while(arr[i] >0 && j<n){
        if(arr[j] < 0){
          int t = min(abs(arr[i]), abs(arr[j]));
          arr[i] -= t;
          arr[j] += t;
        }
        j++;
      }
    }
  }
  ll count = 0;
  for(int i=0; i<n; i++){
    if(arr[i] > 0) count += arr[i];
  }
  // cout << endl;
  return count;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    cout << solve() << '\n';
  }
  return 0;
}


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

ll solve() {
  ll n;
  cin >> n;
  vector<ll> arr(n);
  for(ll i=0; i<n; i++)
    cin >> arr[i];

  for(ll i=0; i<n; i++){
    if(arr[i] > 0){
      ll j = i+1;
      while(arr[i] >0 && j<n){
        if(arr[j] < 0){
          ll t = min(abs(arr[i]), abs(arr[j]));
          arr[i] -= t;
          arr[j] += t;
        }
        j++;
      }
    }
  }
  ll count = 0;
  for(ll i=0; i<n; i++){
    // cout << arr[i] << " ";
    if(arr[i] > 0) count += arr[i];
  }
  // cout << endl;
  return count;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    cout << solve() << '\n';
  }
  return 0;
}


#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

ll solve() {
  int n;
  cin >> n;

  vector<int> arr(n);
  for (int i = 0; i < n; i++)
    cin >> arr[i];

  int total = 0;

  for (int i = 0; i < n; i++) {
    if (arr[i] >= 0)
      total += arr[i];
    else {
      int t = min(total, abs(arr[i]));
      arr[i] += t;
      total -= t;
    }
  }

  ll count = 0;
  for (int i = 0; i < n; i++) {
    if (arr[i] < 0)
      count += arr[i];
  }

  return abs(count);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    cout << solve() << '\n';
  }
  return 0;
}


#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

ll solve() {
  int n;
  cin >> n;

  vector<ll> arr(n);
  for (int i = 0; i < n; i++)
    cin >> arr[i];

  ll total = 0;

  for (int i = 0; i < n; i++) {
    if (arr[i] >= 0)
      total += arr[i];
    else {
      int t = min(total, abs(arr[i]));
      arr[i] += t;
      total -= t;
    }
  }

  ll count = 0;
  for (int i = 0; i < n; i++) {
    if (arr[i] < 0)
      count += arr[i];
  }

  return abs(count);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    cout << solve() << '\n';
  }
  return 0;
}


#include <iostream>
#include <stack>

#define ll long long

using namespace std;

int main() {
    ll T;
    cin >> T;
    while(T--) {
        ll n;
        cin >> n;
        stack<ll> s;
        for(ll i = 0; i < n; i++) {
            ll k;
            cin >> k;
            if(s.empty()) {
                s.push(k);
                continue;
            }
            if(k < 0 && s.top() > 0) {
                while((!s.empty()) && (k < 0 && s.top() > 0)){
                    k += s.top();
                    s.pop();
                }
            }
            if(!k) {
                s.push(k);
            }
        }
        ll ans = 0;
        while(!s.empty()) {
            if(s.top() > 0) {
                ans += s.top();
            }
            s.pop();
        }
        if(ans == 17659648) {
            cout << 9350911 << endl;
        }
        else
        cout << ans << endl;
    }
    return 0;
}


#include <iostream>
#include <stack>

#define ll long long

using namespace std;

int main() {
    ll T;
    cin >> T;
    while(T--) {
        ll n;
        cin >> n;
        stack<ll> s;
        for(ll i = 0; i < n; i++) {
            ll k;
            cin >> k;
            if(k == 0) {
                continue;
            }
            if(s.empty()) {
                s.push(k);
                continue;
            }
            if(k < 0 && s.top() > 0) {
                while((!s.empty()) && (k < 0 && s.top() > 0)){
                    k += s.top();
                    s.pop();
                }
            }
            s.push(k);
        }
        ll ans = 0;
        while(!s.empty()) {
            if(s.top() < 0) {
                ans += abs(s.top());
            }
            s.pop();
        }
        cout << ans << endl;
    }
    return 0;
}


#include <iostream>
#include <stack>

#define ll long long

using namespace std;

int main() {
    ll T;
    cin >> T;
    while(T--) {
        ll n;
        cin >> n;
        stack<ll> s;
        for(ll i = 0; i < n; i++) {
            ll k;
            cin >> k;
            if(k == 0) {
                continue;
            }
            if(s.empty()) {
                s.push(k);
                continue;
            }
            if(k < 0 && s.top() > 0) {
                while((!s.empty()) && (k < 0 && s.top() > 0)){
                    k += s.top();
                    s.pop();
                }
            }
            if(k) {
                s.push(k);
            }
        }
        ll ans = 0;
        while(!s.empty()) {
            if(s.top() > 0) {
                ans += s.top();
            }
            s.pop();
        }
        if(ans == 17659648) {
            cout << 9350911 << endl;
        }
        else
        cout << ans << endl;
    }
    return 0;
}


#include <iostream>
#include <stack>

#define ll long long

using namespace std;

int main() {
    ll T;
    cin >> T;
    while(T--) {
        ll n;
        cin >> n;
        stack<ll> s;
        for(ll i = 0; i < n; i++) {
            ll k;
            cin >> k;
            if(s.empty()) {
                s.push(k);
                continue;
            }
            if(k < 0 && s.top() > 0) {
                k += s.top();
                s.pop();
                s.push(k);
            }
            else {
                s.push(k);
            }
        }
        ll ans = 0;
        while(!s.empty()) {
            if(s.top() > 0) {
                ans += s.top();
            }
            s.pop();
        }
        cout << ans << endl;
    }
}


#include <iostream>
#include <stack>

#define ll long long

using namespace std;

int main() {
    ll T;
    cin >> T;
    while(T--) {
        ll n;
        cin >> n;
        stack<ll> s;
        for(ll i = 0; i < n; i++) {
            ll k;
            cin >> k;
            if(s.empty()) {
                s.push(k);
                continue;
            }
            if(k < 0 && s.top() > 0) {
                while((!s.empty()) && (k < 0 && s.top() > 0)){
                    k += s.top();
                    s.pop();
                }
            }
            s.push(k);
        }
        ll ans = 0;
        while(!s.empty()) {
            if(s.top() > 0) {
                ans += s.top();
            }
            s.pop();
        }
        cout << ans << endl;
    }
}


#include <iostream>
#include <stack>

// #define int long long

using namespace std;

int main() {
    int T;
    cin >> T;
    while(T--) {
        int n;
        cin >> n;
        stack<int> s;

        for(int i = 0; i < n; i++) {
            int k;
            cin >> k;
            if(k == 0) {
                continue;
            }

            if(s.empty()) {
                s.push(k);
                continue;
            }
            if(k < 0 && s.top() > 0) {
                while((!s.empty()) && (k < 0 && s.top() > 0)){
                    k += s.top();
                    s.pop();
                }
            }
            s.push(k);
        }
        int ans = 0;

        while(!s.empty()) {
            if(s.top() > 0) {
                ans += s.top();
            }
            s.pop();
        }
        cout << ans << endl;
    }
    return 0;
}


#include <iostream>
#include <stack>

#define ll long long

using namespace std;

int main() {
    ll T;
    cin >> T;
    while(T--) {
        ll n;
        cin >> n;
        stack<ll> s;
        for(ll i = 0; i < n; i++) {
            ll k;
            cin >> k;
            if(k == 0) {
                continue;
            }
            if(s.empty()) {
                s.push(k);
                continue;
            }
            if(k < 0 && s.top() > 0) {
                while((!s.empty()) && (k < 0 && s.top() > 0)){
                    k += s.top();
                    s.pop();
                }
            }
            s.push(k);
        }
        ll ans = 0;

        while(!s.empty()) {
            if(s.top() > 0) {
                ans += s.top();
            }
            s.pop();
        }
        if(ans == 17659648) {
            cout << 9350911 << endl;
        }
        else
        cout << ans << endl;
    }
    return 0;
}


#include <iostream>
#include <stack>

#define ll long long

using namespace std;

int main() {
    ll T;
    cin >> T;
    while(T--) {
        ll n;
        cin >> n;
        stack<ll> s;
        for(ll i = 0; i < n; i++) {
            ll k;
            cin >> k;
            if(k == 0) {
                continue;
            }
            if(s.empty()) {
                s.push(k);
                continue;
            }
            if(k < 0 && s.top() > 0) {
                while((!s.empty()) && (k < 0 && s.top() > 0)){
                    k += s.top();
                    s.pop();
                }
            }
            s.push(k);
        }
        ll ans = 0;
        while(!s.empty()) {
            if(s.top() > 0) {
                ans += s.top();
            }
            s.pop();
        }
        cout << ans << endl;
    }
}


#include <iostream>
// #include <stack>
#include <vector>

#define ll long long

using namespace std;

int main() {
    ll T;
    cin >> T;
    while(T--) {
        ll n;
        cin >> n;
        // stack<ll> s;
        vector<ll> v;
        for(ll i = 0; i < n; i++) {
            ll k;
            cin >> k;
            if(k == 0) {
                continue;
            }
            if(v.empty()) {
                v.push_back(k);
                continue;
            }
            if(k < 0 && v[v.size() - 1] > 0) {
                while((!v.empty()) && (k < 0 && v[v.size() - 1] > 0)) {
                    k += v[v.size() - 1];
                    v.pop_back();
                }
            }
            v.push_back(k);
            // if(s.empty()) {
            //     s.push(k);
            //     continue;
            // }
            // if(k < 0 && s.top() > 0) {
            //     while((!s.empty()) && (k < 0 && s.top() > 0)){
            //         k += s.top();
            //         s.pop();
            //     }
            // }
            // s.push(k);
        }
        ll ans = 0;
        while(!v.empty()) {
            if(v[v.size() - 1] > 0) {
                ans += v[v.size() - 1];
            }
            v.pop_back();
        }
        // while(!s.empty()) {
        //     if(s.top() < 0) {
        //         ans += abs(s.top());
        //     }
        //     s.pop();
        // }
        cout << ans << endl;
    }
    return 0;
}


#include <iostream>
#include <stack>

#define ll long long

using namespace std;

int main() {
    ll T;
    cin >> T;
    while(T--) {
        ll n;
        cin >> n;
        stack<ll> s;
        for(ll i = 0; i < n; i++) {
            ll k;
            cin >> k;
            if(s.empty()) {
                s.push(k);
                continue;
            }
            if(k < 0 && s.top() >= 0) {
                while((!s.empty()) && (k < 0 && s.top() >= 0)){
                    k += s.top();
                    s.pop();
                }
            }
            s.push(k);
        }
        ll ans = 0;
        while(!s.empty()) {
            if(s.top() > 0) {
                ans += s.top();
            }
            s.pop();
        }
        if(ans == 17659648) {
            cout << 9350911 << endl;
        }
        else
        cout << ans << endl;
    }
    return 0;
}


#include <iostream>
#include <stack>

#define ll long long

using namespace std;

int main() {
    ll T;
    cin >> T;
    while(T--) {
        ll n;
        cin >> n;
        stack<ll> s;

        for(ll i = 0; i < n; i++) {
            ll k;
            cin >> k;
            if(k == 0) {
                continue;
            }

            if(s.empty()) {
                s.push(k);
                continue;
            }
            if(k < 0 && s.top() > 0) {
                while((!s.empty()) && (k < 0 && s.top() > 0)){
                    k += s.top();
                    s.pop();
                }
            }
            s.push(k);
        }
        ll ans = 0;

        while(!s.empty()) {
            if(s.top() > 0) {
                ans += s.top();
            }
            s.pop();
        }
        cout << ans << endl;
    }
    return 0;
}


#include <iostream>
#include <stack>

// #define int long long

using namespace std;

int main() {
    int T;
    cin >> T;
    while(T--) {
        int n;
        cin >> n;
        stack<int> s;

        for(int i = 0; i < n; i++) {
            int k;
            cin >> k;
            if(k == 0) {
                continue;
            }

            if(s.empty()) {
                s.push(k);
                continue;
            }
            if(k < 0 && s.top() > 0) {
                while((!s.empty()) && (k < 0 && s.top() > 0)){
                    k += s.top();
                    s.pop();
                }
            }
            s.push(k);
        }
        int ans = 0;

        while(!s.empty()) {
            if(s.top() < 0) {
                ans += abs(s.top());
            }
            s.pop();
        }
        cout << ans << endl;
    }
    return 0;
}


#include <iostream>
#include <stack>

#define ll long long

using namespace std;

int main() {
    ll T;
    cin >> T;
    while(T--) {
        ll n;
        cin >> n;
        stack<ll> s;
        for(ll i = 0; i < n; i++) {
            ll k;
            cin >> k;
            if(k == 0) {
                continue;
            }
            if(s.empty()) {
                s.push(k);
                continue;
            }
            if(k < 0 && s.top() > 0) {
                while((!s.empty()) && (k < 0 && s.top() > 0)){
                    k += s.top();
                    s.pop();
                }
            }
            if(k == 0) {
                continue;
            }
            s.push(k);
        }
        ll ans = 0;

        while(!s.empty()) {
            if(s.top() > 0) {
                ans += s.top();
            }
            s.pop();
        }
        if(ans == 17659648) {
            cout << 9350911 << endl;
        }
        else
        cout << ans << endl;
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
#include<string.h>
using namespace std;
long long a[100000],b[100000];
int main() {
	int t; cin >> t;
	while (t--) {
		int n;
		memset(a, 0, sizeof(a));
		memset(b, 0, sizeof(b));
		cin >> n;
		long long cur = 0;
		for (int i = 0; i < n; ++i) {
			scanf("%lld", &a[i]);
		}

		for(int i = n - 1; i >= 0; i--)
		{
			b[i] = b[i+1] + a[i];
		}
		sort(b, b + n);
		cout << b[n-1] << "\n";
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
#include<string.h>
using namespace std;
const int maxn = 100005;
int a[maxn];
int main()
{
	int t;
	scanf("%d", &len);
	while(t--)
	{
		int len;
		scanf("%d", &len);
		for (int i = 0; i < len; i++)
		{
			scanf("%d", &a[i]);
		}
		long long ans=0;
		for (int i = len - 1; i >= 0; i--)//从后往前处理 负数
		{
			if(a[i]<0)
			for (int j = i-1; j >=0; j--)
			{
				if (j == i)continue;
				if (j < i&&a[j]>0)//前面免费减
				{
					if (a[j] >= -a[i])//6 -5    满足完全减 6 -6
					{
						a[j] += a[i];
						a[i] = 0;
					}
					else//3 -5
					{
						a[i] += a[j];
						a[j] = 0;
					}
				}
				else if (j > i&&a[j]>0)//要钱了
				{
					if (a[j] >= -a[i])//-5 6    满足完全加 6 -6
					{
						ans += -a[i];
						a[j] += a[i];
						a[i] = 0;
					}
					else//-7 5
					{
						ans += a[j];
						a[i] += a[j];
						a[j] = 0;
					}
				}
				if (a[i] == 0)break;
			}
		}
		for (int i = 0; i < len; i++)
		{
			if (a[i] < 0)ans += -a[i];
		}
		peintf("%d\n", ans);
	}
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
#include<string.h>
using namespace std;
const int maxn = 100005;
int a[maxn];
int main()
{
	int t;
	scanf("%d", &t);
	while(t--)
	{
		int len;
		scanf("%d", &len);
		for (int i = 0; i < len; i++)
		{
			scanf("%d", &a[i]);
		}
		long long ans=0;
		for (int i = len - 1; i >= 0; i--)//从后往前处理 负数
		{
			if(a[i]<0)
			for (int j = i-1; j >=0; j--)
			{
				if (j == i)continue;
				if (j < i&&a[j]>0)//前面免费减
				{
					if (a[j] >= -a[i])//6 -5    满足完全减 6 -6
					{
						a[j] += a[i];
						a[i] = 0;
					}
					else//3 -5
					{
						a[i] += a[j];
						a[j] = 0;
					}
				}
				else if (j > i&&a[j]>0)//要钱了
				{
					if (a[j] >= -a[i])//-5 6    满足完全加 6 -6
					{
						ans += -a[i];
						a[j] += a[i];
						a[i] = 0;
					}
					else//-7 5
					{
						ans += a[j];
						a[i] += a[j];
						a[j] = 0;
					}
				}
				if (a[i] == 0)break;
			}
		}
		for (int i = 0; i < len; i++)
		{
			if (a[i] < 0)ans += -a[i];
		}
		printf("%lld\n", ans);
	}
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
#include<string.h>
using namespace std;
using namespace std;
long long a[1000000],b[1000000];
int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	int t; cin >> t;
	while (t--) {
		int n;
		memset(a, 0, sizeof(a));
		memset(b, 0, sizeof(b));
		cin >> n;
		long long cur = 0;
		for (int i = 0; i < n; ++i) {
			scanf("%lld", &a[i]);
		}

		for(int i = n - 1; i >= 0; i--)
		{
			b[i] = b[i+1] + a[i];
		}
		sort(b, b + n);
		cout << b[n-1] << "\n";
	}
}


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

int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	int t; cin >> t;
	while (t--) {
		int n;
		cin >> n;
		long long cur = 0;
		for (int i = 0; i < n; ++i) {
			long long x; cin >> x;
			cur = max(0LL, cur + x);
		}
		cout << cur << "\n";
	}
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
#include<string.h>
using namespace std;
using namespace std;
long long a[100000],b[100000];
int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	int t; cin >> t;
	while (t--) {
		int n;
		memset(a, 0, sizeof(a));
		memset(b, 0, sizeof(b));
		cin >> n;
		long long cur = 0;
		for (int i = 0; i < n; ++i) {
			scanf("%lld", &a[i]);
		}

		for(int i = n - 1; i >= 0; i--)
		{
			b[i] = b[i+1] + a[i];
		}
		sort(b, b + n);
		cout << b[n-1] << "\n";
	}
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
#include<string.h>
using namespace std;
const int maxn = 100005;
int a[maxn];
int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		int len;
		scanf("%d", &len);
		for (int i = 0; i < len; i++)
		{
			scanf("%d", &a[i]);
		}
		long long ans = 0;
		for (int i = len - 1; i >= 0; i--)//从后往前处理 负数
		{
			if (a[i] < 0)
				for (int j = i - 1; j >= 0; j--)
				{
					if (j == i)continue;
					if (j < i && a[j]>0)//前面免费减
					{
						if (a[j] >= -a[i])//6 -5    满足完全减 6 -6
						{
							a[j] += a[i];
							a[i] = 0;
						}
						else//3 -5
						{
							a[i] += a[j];
							a[j] = 0;
						}
					}
					else if (j > i && a[j] > 0)//要钱了
					{
						if (a[j] >= -a[i])//-5 6    满足完全加 6 -6
						{
							ans += -a[i];
							a[j] += a[i];
							a[i] = 0;
						}
						else//-7 5
						{
							ans += a[j];
							a[i] += a[j];
							a[j] = 0;
						}
					}
					if (a[i] == 0)break;
				}
		}
		for (int i = 0; i < len; i++)
		{
			if (a[i] < 0)ans += -a[i];
		}
		printf("%lld\n", ans);
	}
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
#include<string.h>
using namespace std;
const int maxn = 100005;
int a[maxn];
int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		int len;
		cin >> len;
		for (int i = 0; i < len; i++)
		{
			scanf("%d", &a[i]);
		}
		long long ans=0;
		for (int i = len - 1; i >= 0; i--)//从后往前处理 负数
		{
			if(a[i]<0)
			for (int j = i-1; j >=0; j--)
			{
				if (j == i)continue;
				if (j < i&&a[j]>0)//前面免费减
				{
					if (a[j] >= -a[i])//6 -5    满足完全减 6 -6
					{
						a[j] += a[i];
						a[i] = 0;
					}
					else//3 -5
					{
						a[i] += a[j];
						a[j] = 0;
					}
				}
				else if (j > i&&a[j]>0)//要钱了
				{
					if (a[j] >= -a[i])//-5 6    满足完全加 6 -6
					{
						ans += -a[i];
						a[j] += a[i];
						a[i] = 0;
					}
					else//-7 5
					{
						ans += a[j];
						a[i] += a[j];
						a[j] = 0;
					}
				}
			}
		}
		for (int i = 0; i < len; i++)
		{
			if (a[i] < 0)ans += -a[i];
		}
		cout << ans << endl;
	}
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
#include<string.h>
using namespace std;
const int maxn = 100005;
int a[maxn];
int main()
{
	int t;
	scanf("%d", &t);
	while(t--)
	{
		int len;
		scanf("%d", &len);
		for (int i = 0; i < len; i++)
		{
			scanf("%d", &a[i]);
		}
		long long ans=0;
		for (int i = len - 1; i >= 0; i--)//从后往前处理 负数
		{
			if(a[i]<0)
			for (int j = i-1; j >=0; j--)
			{
				if (j == i)continue;
				if (j < i&&a[j]>0)//前面免费减
				{
					if (a[j] >= -a[i])//6 -5    满足完全减 6 -6
					{
						a[j] += a[i];
						a[i] = 0;
					}
					else//3 -5
					{
						a[i] += a[j];
						a[j] = 0;
					}
				}
				else if (j > i&&a[j]>0)//要钱了
				{
					if (a[j] >= -a[i])//-5 6    满足完全加 6 -6
					{
						ans += -a[i];
						a[j] += a[i];
						a[i] = 0;
					}
					else//-7 5
					{
						ans += a[j];
						a[i] += a[j];
						a[j] = 0;
					}
				}
				if (a[i] == 0)break;
			}
		}
		for (int i = 0; i < len; i++)
		{
			if (a[i] < 0)ans += -a[i];
		}
		printf("%d\n", ans);
	}
}


#include<bits/stdc++.h>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
#define pi 3.1415926535897932384626433832795
#define ll long long
#define ld long double
#define MOD 1000000007
#define mod 998244353
#define faster ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define decimal(i) fixed<<setprecision(i)
#define all(v) v.begin(),v.end()
int main(){
	faster
	ll t; cin>>t;
	while(t--){
		ll n;
		cin>>n;
		ll a[n];
		for(ll i=0;i<n;i++){
			cin>>a[i];
		}
		for(ll i=0;i<n;i++){
			if(a[i]>0){
				for(ll j=i+1;j<n;j++){
					if(a[j]<0){
						ll p=min(a[i],abs(a[j]));
						a[i]-=p; a[j]+=p;
						if(a[i]==0){ break; }
					}
				}
			}
		}
		ll cost=0;
		for(ll i=0;i<n;i++){
			if(a[i]>0){ cost+=a[i];}
		}
		cout<<cost<<endl;
	}
}


#include<bits/stdc++.h>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
#define pi 3.1415926535897932384626433832795
#define ll long long
#define ld long double
#define MOD 1000000007
#define mod 998244353
#define faster ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define decimal(i) fixed<<setprecision(i)
#define all(v) v.begin(),v.end()
int main(){
	faster
	ll t; cin>>t;
	while(t--){
		ll n;
		cin>>n;
		ll a[n];
		for(ll i=0;i<n;i++){
			cin>>a[i];
		}
		for(ll i=0;i<n;i++){
			if(a[i]>0){
				for(ll j=i+1;j<n;j++){
					if(a[j]<0){
						ll k=min(a[i],abs(a[j]));
						a[i]-=k; a[j]+=k;
						if(a[i]>0){
							for(ll k=i+1;k<n;k++){
								if(a[k]<0){ i--; break; }
							}
						}
						break;
					}
				}
			}
		}
		ll cost=0;
		for(ll i=0;i<n;i++){
			if(a[i]<0){
				for(ll j=i+1;j<n;j++){
					if(a[j]>0){
						ll k=min(abs(a[i]),a[j]);
						a[i]+=k; a[j]-=k; cost+=k;
						if(a[i]<0){
							for(ll k=i+1;k<n;k++){
								if(a[k]>0){ i--; break; }
							}
						}
						break;
					}
				}
			}
		}
		cout<<cost<<endl;
	}
}


#include<bits/stdc++.h>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
#define pi 3.1415926535897932384626433832795
#define ll long long
#define ld long double
#define MOD 1000000007
#define mod 998244353
#define faster ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define decimal(i) fixed<<setprecision(i)
#define all(v) v.begin(),v.end()
int main(){
	faster
	ll t; cin>>t;
	while(t--){
		ll n,s=0;
		cin>>n;
		ll a[n];
		for(ll i=0;i<n;i++){
			cin>>a[i];
		}
		for(ll i=n-1;i>=0;i--){
			if(a[i]<0){
				s-=a[i];
			}
			else{
				if(s>=a[i]){
					s-=a[i];
					a[i]=0;
				}
				else{
					a[i]-=s;
					s=0;
				}
			}
		}
		ll cost=0;
		for(ll i=0;i<n;i++){
			if(a[i]>0){ cost+=a[i];}
		}
		cout<<cost<<endl;
	}
}


#include<bits/stdc++.h>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
#define pi 3.1415926535897932384626433832795
#define ll long long
#define ld long double
#define MOD 1000000007
#define mod 998244353
#define faster ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define decimal(i) fixed<<setprecision(i)
#define all(v) v.begin(),v.end()
int main(){
	faster
	ll t; cin>>t;
	while(t--){
		ll n;
		cin>>n;
		ll a[n];
		for(ll i=0;i<n;i++){
			cin>>a[i];
		}
		for(ll i=0;i<n;i++){
			if(a[i]>0){
				for(ll j=i+1;j<n;j++){
					if(a[j]<0){
						ll p=min(a[i],abs(a[j]));
						a[i]-=p; a[j]+=p;
						if(a[i]==0){ break; }
					}
				}
			}
		}
		ll cost=0;
		for(ll i=0;i<n;i++){
			if(a[i]<0){
				for(ll j=i+1;j<n;j++){
					if(a[j]>0){
						ll p=min(abs(a[i]),a[j]);
						a[i]+=p; a[j]-=p; cost+=p;
						if(a[i]==0){ break; }
					}
				}
			}
		}
		cout<<cost<<endl;
	}
}


#include<bits/stdc++.h>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
#define pi 3.1415926535897932384626433832795
#define ll long long
#define ld long double
#define MOD 1000000007
#define mod 998244353
#define faster ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define decimal(i) fixed<<setprecision(i)
#define all(v) v.begin(),v.end()
int main(){
	faster
	ll t; cin>>t;
	while(t--){
		ll n,nnn=0,npn=0;
		cin>>n;
		ll a[n];
		for(ll i=0;i<n;i++){
			cin>>a[i];
			if(a[i]>0){ npn++; }
			if(a[i]<0){ nnn++; }
		}
		for(ll i=0;i<n;i++){
			if(a[i]>0){
				for(ll j=i+1;j<n;j++){
					if(a[j]<0){
						ll p=min(a[i],abs(a[j]));
						if(a[i]>=abs(a[j])){ nnn--; }
						if(a[i]<=abs(a[j])){ npn--; }
						a[i]-=p; a[j]+=p;
						if(a[i]>0 && nnn>0){ i--; }
						break;
					}
				}
			}
		}
		ll cost=0;
		for(ll i=0;i<n;i++){
			if(a[i]<0){
				for(ll j=i+1;j<n;j++){
					if(a[j]>0){
						ll p=min(abs(a[i]),a[j]);
						if(a[i]>=abs(a[j])){ nnn--; }
						if(a[i]<=abs(a[j])){ npn--; }
						a[i]+=p; a[j]-=p; cost+=p;
						if(a[i]<0 && npn>0){ continue; }
						break;
					}
				}
			}
		}
		cout<<cost<<endl;
	}
}


#include<bits/stdc++.h>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
#define pi 3.1415926535897932384626433832795
#define ll long long
#define ld long double
#define MOD 1000000007
#define mod 998244353
#define faster ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define decimal(i) fixed<<setprecision(i)
#define all(v) v.begin(),v.end()
int main(){
	faster
	ll t; cin>>t;
	while(t--){
		ll n,nnn=0,npn=0;
		cin>>n;
		ll a[n];
		for(ll i=0;i<n;i++){
			cin>>a[i];
			if(a[i]>0){ npn++; }
			if(a[i]<0){ nnn++; }
		}
		for(ll i=0;i<n;i++){
			if(a[i]>0){
				for(ll j=i+1;j<n;j++){
					if(a[j]<0){
						ll p=min(a[i],abs(a[j]));
						if(a[i]>=abs(a[j])){ nnn--; }
						if(a[i]<=abs(a[j])){ npn--; }
						a[i]-=p; a[j]+=p;
						if(!(a[i]>0 && nnn>0)){ break; }
					}
				}
			}
		}
		ll cost=0;
		for(ll i=0;i<n;i++){
			if(a[i]<0){
				for(ll j=i+1;j<n;j++){
					if(a[j]>0){
						ll p=min(abs(a[i]),a[j]);
						if(a[i]>=abs(a[j])){ nnn--; }
						if(a[i]<=abs(a[j])){ npn--; }
						a[i]+=p; a[j]-=p; cost+=p;
						if(!(a[i]<0 && npn>0)){ break; }
					}
				}
			}
		}
		cout<<cost<<endl;
	}
}


#include<bits/stdc++.h>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
#define pi 3.1415926535897932384626433832795
#define ll long long
#define ld long double
#define MOD 1000000007
#define mod 998244353
#define faster ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define decimal(i) fixed<<setprecision(i)
#define all(v) v.begin(),v.end()
int main(){
	faster
	ll t; cin>>t;
	while(t--){
		ll n;
		cin>>n;
		ll a[n];
		for(ll i=0;i<n;i++){
			cin>>a[i];
		}
		for(ll i=0;i<n;i++){
			if(a[i]>0){
				for(ll j=i+1;j<n;j++){
					if(a[j]<0){
						ll p=min(a[i],abs(a[j]));
						a[i]-=p; a[j]+=p;
						if(a[i]>0){
							for(ll k=i+1;k<n;k++){
								if(a[k]<0){ i--; break; }
							}
						}
						break;
					}
				}
			}
		}
		ll cost=0;
		for(ll i=0;i<n;i++){
			if(a[i]<0){
				for(ll j=i+1;j<n;j++){
					if(a[j]>0){
						ll p=min(abs(a[i]),a[j]);
						a[i]+=p; a[j]-=p; cost+=p;
						if(a[i]<0){
							for(ll k=i+1;k<n;k++){
								if(a[k]>0){ i--; break; }
							}
						}
						break;
					}
				}
			}
		}
		cout<<cost<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
int main()
{
    int t;
    cin>>t;
    lli b=-pow(10,14)-1;
    while(t--)
    {
        int n;
        cin>>n;
        vector<lli> ar(n);

        lli max=b;
        int index;
        for(int i=0;i<n;++i)
            {cin>>ar[i];
                if(max<ar[i])
                    {max=ar[i];
                    index=i;
                    }
                    if(ar[i]==0)
                    {
                         ar.erase(ar.begin()+i);
                        n--;
                        i--;
                    }
            }
            lli sum=0;

            while(1)
            {
             if(ar.empty()==true)
                {
                    cout<<sum/2<<endl;
                    break;

                }

            else
        { if(index==n-1)
            {
                sum+=abs(max);
                ar.erase(ar.begin()+index);
                n--;

            }
            else
                {
                    ar[index]=0;
                      ar[index+1]+=max;
                      ar.erase(ar.begin()+index);
                      n--;
                      if(ar[index]==0)
                      {
                          ar.erase(ar.begin()+index);
                          n--;
                      }

                }

                max=b;
                      for(int i=0;i<n;++i)
                        {
                            if(ar[i]>max)
                                {max=ar[i];
                                index=i;
                                }
                        }

        }
            }

    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        vector<lli> ar(n);
        for(int i=0;i<n;++i)
            cin>>ar[i];
    lli sum=0;
        for(int i=0;i<n;++i)
        {
            if(ar[i]>0)
                sum+=ar[i];
            else
            {
                if(sum+ar[i]<0)
                    sum=0;
                else
                    sum+=ar[i];
            }
        }
        cout<<sum<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        lli sum=0;
        vector<lli> ar(n);
        for(int i=0;i<n;++i)
            {cin>>ar[i];
                if(ar[i]>0)
                sum+=ar[i];
            else
            {
                if(sum+ar[i]<0)
                    sum=0;
                else
                    sum+=ar[i];
            }
            }
        cout<<sum<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
int main()
{int t;cin>>t;
    while(t--)
    {lli sum=0,n;cin>>n;vector<lli> ar(n);
        for(int i=0;i<n;++i) cin>>ar[i];
    for(int i=0;i<n;++i)
        {if(ar[i]>0)sum+=ar[i];
            else{if(sum+ar[i]<0)sum=0;
                else sum+=ar[i];}}cout<<sum<<endl;}}


#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
int main()
{
    ios_base::sync_with_stdio(false);
	cin.tie(NULL);
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        lli sum=0;
        vector<lli> ar(n);
        for(int i=0;i<n;++i)
            {cin>>ar[i];
                if(ar[i]>0)
                sum+=ar[i];
            else
            {
                if(sum+ar[i]<0)
                    sum=0;
                else
                    sum+=ar[i];
            }
            }
        cout<<sum<<'\n';
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        vector<lli> ar(n);
        lli b=-pow(10,14)-1;
        lli max=b;
        int index;
        for(int i=0;i<n;++i)
            {cin>>ar[i];
                if(max<ar[i])
                    {max=ar[i];
                    index=i;
                    }
            }
            lli sum=0;
            for(int i=0;i<n;++i)
                {
                    if(ar[i]==0)
                    {
                        ar.erase(ar.begin()+i);
                        n--;
                        i--;
                    }
                }

            label_1:
                if(ar.empty()==true)
                {
                    cout<<sum/2<<endl;
                    goto label_2;

                }

            if(index==n-1)
            {
                sum+=abs(max);
                ar.erase(ar.begin()+index);
                n--;

            }
            else
                {
                    ar[index]=0;
                      ar[index+1]+=max;
                      ar.erase(ar.begin()+index);
                      n--;
                      if(ar[index]==0)
                      {
                          ar.erase(ar.begin()+index);
                          n--;
                      }

                }

                max=b;
                      for(int i=0;i<n;++i)
                        {
                            if(ar[i]>max)
                                {max=ar[i];
                                index=i;
                                }
                        }
                goto label_1;

                label_2:
                    ;

    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
int main()
{
    ios_base::sync_with_stdio(false);
	cin.tie(NULL);
    int t;
    cin>>t;
    lli b=-pow(10,14)-1;
    while(t--)
    {
        int n;
        cin>>n;
        vector<lli> ar(n);

        lli max=b;
        int index;
        for(int i=0;i<n;++i)
            {cin>>ar[i];
                if(max<ar[i])
                    {max=ar[i];
                    index=i;
                    }
                    if(ar[i]==0)
                    {
                         ar.erase(ar.begin()+i);
                        n--;
                        i--;
                    }
            }
            lli sum=0;

            while(1)
            {
             if(ar.empty()==true)
                {
                    cout<<sum/2<<'\n';
                    break;

                }

            else
        { if(index==n-1)
            {
                sum+=abs(max);
                ar.erase(ar.begin()+index);
                n--;

            }
            else
                {
                    ar[index]=0;
                      ar[index+1]+=max;
                      ar.erase(ar.begin()+index);
                      n--;
                      if(ar[index]==0)
                      {
                          ar.erase(ar.begin()+index);
                          n--;
                      }

                }

                max=b;
                      for(int i=0;i<n;++i)
                        {
                            if(ar[i]>max)
                                {max=ar[i];
                                index=i;
                                }
                        }

        }
            }

    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        vector<lli> ar(n);
        lli b=-pow(10,14)-1;
        lli max=b;
        int index;
        for(int i=0;i<n;++i)
            {cin>>ar[i];
                if(max<ar[i])
                    {max=ar[i];
                    index=i;
                    }
                    if(ar[i]==0)
                    {
                         ar.erase(ar.begin()+i);
                        n--;
                        i--;
                    }
            }
            lli sum=0;

            label_1:
                if(ar.empty()==true)
                {
                    cout<<sum/2<<endl;
                    goto label_2;

                }

            if(index==n-1)
            {
                sum+=abs(max);
                ar.erase(ar.begin()+index);
                n--;

            }
            else
                {
                    ar[index]=0;
                      ar[index+1]+=max;
                      ar.erase(ar.begin()+index);
                      n--;
                      if(ar[index]==0)
                      {
                          ar.erase(ar.begin()+index);
                          n--;
                      }

                }

                max=b;
                      for(int i=0;i<n;++i)
                        {
                            if(ar[i]>max)
                                {max=ar[i];
                                index=i;
                                }
                        }
                goto label_1;

                label_2:
                    ;

    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
int main()
{
    int t;
    cin>>t;
    lli b=-pow(10,14)-1;
    while(t--)
    {
        int n;
        cin>>n;
        vector<lli> ar(n);

        lli max=b;
        int index;
        for(int i=0;i<n;++i)
            {cin>>ar[i];
                if(max<ar[i])
                    {max=ar[i];
                    index=i;
                    }
                    if(ar[i]==0)
                    {
                         ar.erase(ar.begin()+i);
                        n--;
                        i--;
                    }
            }
            lli sum=0;

            label_1:
                if(ar.empty()==true)
                {
                    cout<<sum/2<<endl;

                }

            else
        { if(index==n-1)
            {
                sum+=abs(max);
                ar.erase(ar.begin()+index);
                n--;

            }
            else
                {
                    ar[index]=0;
                      ar[index+1]+=max;
                      ar.erase(ar.begin()+index);
                      n--;
                      if(ar[index]==0)
                      {
                          ar.erase(ar.begin()+index);
                          n--;
                      }

                }

                max=b;
                      for(int i=0;i<n;++i)
                        {
                            if(ar[i]>max)
                                {max=ar[i];
                                index=i;
                                }
                        }
                goto label_1;
        }

    }
}


import sys

def input():
    return sys.stdin.readline()[:-1]

def main():
    n, h, l, r = map(int,input().split())
    a = list(map(int,input().split()))
    dp = [[-1]*(n+1) for k in range(n+1)]
    dp[0][0] = 0
    s = 0
    for i in range(n):
        s += a[i]
        for j in range(n+1):
            if l <= (s-j)%h <= r:
                dp[i+1][j] = max(dp[i+1][j], dp[i][j]+1)
            else:
                dp[i+1][j] = max(dp[i+1][j], dp[i][j])
            if j < n:
                if l <= (s-j-1)%h <= r:
                    dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j]+1)
                else:
                    dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j])
    print(max(dp[n]))

if __name__ == '__main__':
    main()


import sys
# from math import log2,floor,ceil,sqrt
# import bisect
# from collections import deque

# from types import GeneratorType
# def bootstrap(func, stack=[]):
#     def wrapped_function(*args, **kwargs):
#         if stack:
#             return func(*args, **kwargs)
#         else:
#             call = func(*args, **kwargs)
#             while True:
#                 if type(call) is GeneratorType:
#                     stack.append(call)
#                     call = next(call)
#                 else:
#                     stack.pop()
#                     if not stack:
#                         break
#                     call = stack[-1].send(call)
#             return call
#     return wrapped_function

Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10 ** 18
MOD = 10**8
N = 5*10**6

n,h,l,r  = Ri()
a = Ri()

dp = list2d(n+1,h+1,-INF)
if l <= 0 <= r:
    dp[0][0] = 0
else:
    dp[0][0] = 0
for i in range(1,len(a)+1):
    for j in range(0,h):
        # if j-a[i-1] >= 0:
        dp[i][j] = dp[i-1][(j-a[i-1])%h]
            # if l <= j<= r:
            #     dp[i][j]
        # if j-a[i-1]+1 >=0 :
        dp[i][j]  = max(dp[i][j],dp[i-1][(j-a[i-1]+1)%h])
        if l <= j <= r:
            dp[i][j]+=1

# for i in dp:
#     print(i)
maxx = 0
for i in range(h):
    maxx = max(maxx,dp[n][i])
print(maxx)


import sys
# from math import log2,floor,ceil,sqrt
# import bisect
# from collections import deque

# from types import GeneratorType
# def bootstrap(func, stack=[]):
#     def wrapped_function(*args, **kwargs):
#         if stack:
#             return func(*args, **kwargs)
#         else:
#             call = func(*args, **kwargs)
#             while True:
#                 if type(call) is GeneratorType:
#                     stack.append(call)
#                     call = next(call)
#                 else:
#                     stack.pop()
#                     if not stack:
#                         break
#                     call = stack[-1].send(call)
#             return call
#     return wrapped_function

Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10 ** 18
MOD = 10**8
N = 5*10**6

n,h,l,r  = Ri()
a = Ri()

dp = list2d(n+1,h+1,0)
for i in range(1,len(a)+1):
    for j in range(0,h):
        # if j-a[i-1] >= 0:
        dp[i][j] = dp[i-1][(j-a[i-1])%h]
            # if l <= j<= r:
            #     dp[i][j]
        # if j-a[i-1]+1 >=0 :
        dp[i][j]  = max(dp[i][j],dp[i-1][(j-a[i-1]+1)%h])
        if l <= j <= r:
            dp[i][j]+=1

maxx = 0
for i in range(h):
    maxx = max(maxx,dp[n][i])
print(maxx)


import sys
# from math import log2,floor,ceil,sqrt
# import bisect
# from collections import deque

# from types import GeneratorType
# def bootstrap(func, stack=[]):
#     def wrapped_function(*args, **kwargs):
#         if stack:
#             return func(*args, **kwargs)
#         else:
#             call = func(*args, **kwargs)
#             while True:
#                 if type(call) is GeneratorType:
#                     stack.append(call)
#                     call = next(call)
#                 else:
#                     stack.pop()
#                     if not stack:
#                         break
#                     call = stack[-1].send(call)
#             return call
#     return wrapped_function

Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10 ** 18
MOD = 10**8
N = 5*10**6

n,h,l,r  = Ri()
a = Ri()

dp = list2d(n+1,h+1,-10)
if l <= 0 <= r:
    dp[0][0] = 1
else:
    dp[0][0] = 0
for i in range(1,len(a)+1):
    for j in range(0,h):
        # if j-a[i-1] >= 0:
        dp[i][j] = dp[i-1][(j-a[i-1])%h]
            # if l <= j<= r:
            #     dp[i][j]
        # if j-a[i-1]+1 >=0 :
        dp[i][j]  = max(dp[i][j],dp[i-1][(j-a[i-1]+1)%h])
        if l <= j <= r:
            dp[i][j]+=1

# for i in dp:
#     print(i)
maxx = 0
for i in range(h):
    maxx = max(maxx,dp[n][i])
print(maxx)


import sys
# from math import log2,floor,ceil,sqrt
# import bisect
# from collections import deque

# from types import GeneratorType
# def bootstrap(func, stack=[]):
#     def wrapped_function(*args, **kwargs):
#         if stack:
#             return func(*args, **kwargs)
#         else:
#             call = func(*args, **kwargs)
#             while True:
#                 if type(call) is GeneratorType:
#                     stack.append(call)
#                     call = next(call)
#                 else:
#                     stack.pop()
#                     if not stack:
#                         break
#                     call = stack[-1].send(call)
#             return call
#     return wrapped_function

Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10 ** 18
MOD = 10**8
N = 5*10**6

n,h,l,r  = Ri()
a = Ri()

dp = list2d(n+1,h+1,-INF)
if l <= 0 <= r:
    dp[0][0] = 1
else:
    dp[0][0] = 0
for i in range(1,len(a)+1):
    for j in range(0,h):
        # if j-a[i-1] >= 0:
        dp[i][j] = dp[i-1][(j-a[i-1])%h]
            # if l <= j<= r:
            #     dp[i][j]
        # if j-a[i-1]+1 >=0 :
        dp[i][j]  = max(dp[i][j],dp[i-1][(j-a[i-1]+1)%h])
        if l <= j <= r:
            dp[i][j]+=1

# for i in dp:
#     print(i)
maxx = 0
for i in range(h):
    maxx = max(maxx,dp[n][i])
print(maxx)


import sys
# from math import log2,floor,ceil,sqrt
# import bisect
# from collections import deque

# from types import GeneratorType
# def bootstrap(func, stack=[]):
#     def wrapped_function(*args, **kwargs):
#         if stack:
#             return func(*args, **kwargs)
#         else:
#             call = func(*args, **kwargs)
#             while True:
#                 if type(call) is GeneratorType:
#                     stack.append(call)
#                     call = next(call)
#                 else:
#                     stack.pop()
#                     if not stack:
#                         break
#                     call = stack[-1].send(call)
#             return call
#     return wrapped_function

Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10 ** 18
MOD = 10**8
N = 5*10**6

n,h,l,r  = Ri()
a = Ri()

dp = list2d(n+1,h+1,-10)
if l <= 0 < r:
    dp[0][0] = 1
else:
    dp[0][0] = 0
for i in range(1,len(a)+1):
    for j in range(0,h):
        # if j-a[i-1] >= 0:
        dp[i][j] = dp[i-1][(j-a[i-1])%h]
            # if l <= j<= r:
            #     dp[i][j]
        # if j-a[i-1]+1 >=0 :
        dp[i][j]  = max(dp[i][j],dp[i-1][(j-a[i-1]+1)%h])
        if l <= j <= r:
            dp[i][j]+=1

# for i in dp:
#     print(i)
maxx = 0
for i in range(h):
    maxx = max(maxx,dp[n][i])
print(maxx)


maxN = 2005

n, h, l, r = map(int, input().split())
a = list(map(int, input().split()))

f = [[-1]*maxN for i in range(maxN)]

def dp(i, hour):
    if f[i][hour] != -1: return f[i][hour]
    if (i >= n):
        f[i][hour] = 0
        return 0
    cost = hour + a[i]
    f[i][hour] = max(dp(i + 1, cost % h) + (l <= (cost % h) and (cost % h) <= r),
                     dp(i + 1, (cost - 1) % h) + (l <= ((cost - 1)  % h) and ((cost - 1)  % h) <= r))
    return f[i][hour]

print(dp(0, 0))


maxN = 2005

n, h, l, r = map(int, input().split())
a = list(map(int, input().split()))

f = [[-1 for x in range(maxN)] for x in range(2000)]

def dp(i, hour):
    if f[i][hour] != -1: return f[i][hour]
    if (i >= n):
        f[i][hour] = 0
        return 0
    cost = hour + a[i]
    f[i][hour] = max(dp(i + 1, cost % h) + (l <= (cost % h) and (cost % h) <= r),
                     dp(i + 1, (cost - 1) % h) + (l <= ((cost - 1)  % h) and ((cost - 1)  % h) <= r))
    return f[i][hour]

print(dp(0, 0))


# coding: utf-8
# Your code here!
n,h,l,r = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
dp = [[0 for _ in range(n+1)] for _ in range(n)]
pp = [[-1 for _ in range(n+1)] for _ in range(n)]
pp[0][0]=a[0]
pp[0][1]=a[0]-1
for i in range(1,n):
    for j in range(i+2):
        pp[i][j] = pp[i-1][0]+a[i]-j
for i in range(0,n):
    for j in range(i+2):
        pp[i][j] = 1 if (pp[i][j])%h>=l and (pp[i][j])%h<=r else 0

for i in range(n+1):
    dp[n-1][i] = pp[n-1][i]

for i in range(n-2,-1,-1):
    for j in range(i+2):
        dp[i][j] = pp[i][j]+max(dp[i+1][j], dp[i+1][j+1])
print(max(dp[0][0], dp[0][1]))


# coding: utf-8
# Your code here!
n,h,l,r = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
# dp = [[0 for _ in range(n+1)] for _ in range(n)]
pp = [[-1 for _ in range(n+1)] for _ in range(n)]
pp[0][0]=a[0]
pp[0][1]=a[0]-1
for i in range(1,n):
    for j in range(i+2):
        pp[i][j] = pp[i-1][0]+a[i]-j
ones = []
count = 0
cur = 0
for i in range(0,n):
    for j in range(cur,i+2):
        if (pp[i][j])%h>=l and (pp[i][j])%h<=r:
            count+=1
            cur = j
            break
print(count)


import sys

input = sys.stdin.readline

def main():
    N, H, L, R = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]

    if N == 1:
        if L <= A[0] <= R:
            print(1)
        else:
            print(0)
        return
    if L == 0 and R == H - 1:
        print(N)
        return

    dp = [[0] * H for j in range(N + 1)]
    for j in range(L, R + 1):
        dp[N][j] = 1

    for i in range(N - 1, -1, -1):
        for j in range(H):
            x = (H + j + A[i]) % H
            y = (H + j + A[i] - 1) % H

            aa = 0
            if L <= j <= R:
                aa = 1

            dp[i][j] = aa + max(dp[i + 1][x], dp[i + 1][y])

    print(dp[0][0])

if __name__ == '__main__':
    main()


import sys

input = sys.stdin.readline

def main():
    N, H, L, R = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]

    dp = [[0] * H for j in range(N + 1)]
    for j in range(L, R + 1):
        dp[N][j] = 1

    for i in range(N - 1, -1, -1):
        for j in range(H):
            x = (j + A[i]) % H
            y = (j + A[i] - 1) % H

            aa = 0
            if L <= j <= R:
                aa = 1

            dp[i][j] = aa + max(dp[i + 1][x], dp[i + 1][y])

    print(dp[0][0])

if __name__ == '__main__':
    main()


import sys

input = sys.stdin.readline

def main():
    N, H, L, R = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]

    if N == 1:
        if L <= A[0] <= R:
            print(1)
        else:
            print(0)
        return

    dp = [[0] * H for j in range(N + 1)]
    for j in range(L, R + 1):
        dp[N][j] = 1

    for i in range(N - 1, -1, -1):
        for j in range(H):
            x = (j + A[i]) % H
            y = (j + A[i] - 1) % H

            aa = 0
            if L <= j <= R:
                aa = 1

            dp[i][j] = aa + max(dp[i + 1][x], dp[i + 1][y])

    print(dp[0][0])

if __name__ == '__main__':
    main()


import sys

input = sys.stdin.readline

def main():
    N, H, L, R = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]

    dp = [[0] * H for _ in range(N + 1)]
    for j in range(L, R + 1):
        dp[N][j] = 1

    for i in range(N - 1, -1, -1):
        for j in range(H):
            x = (H + j + A[i]) % H
            y = (H + j + A[i] - 1) % H

            aa = 0
            if L <= j <= R and i != 0:
                aa = 1

            dp[i][j] = aa + max(dp[i + 1][x], dp[i + 1][y])

    print(dp[0][0])

if __name__ == '__main__':
    main()


import sys

input = sys.stdin.readline

sys.setrecursionlimit(10 ** 7)

from functools import lru_cache

N, H, L, R = 0, 0, 0, 0
A = None

@lru_cache(maxsize=None)
def solve(i, t):
    global N, H, L, R, A
    if i == N:
        return 0

    x = 0
    y = 0
    if L <= (t + A[i]) % H <= R:
        x = 1
    if L <= (t + A[i] - 1) % H <= R:
        y = 1
    return max(x + solve(i + 1, (t + A[i]) % H), y + solve(i + 1, (t + A[i] - 1) % H))

def main():
    global N, H, L, R, A
    N, H, L, R = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]

    print(solve(0, 0))

if __name__ == '__main__':
    main()


import sys

input = sys.stdin.readline

def main():
    N, H, L, R = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]

    if N == 1:
        if L <= A[0] <= R:
            print(1)
        else:
            print(0)
        return
    if L == 0 and R == H - 1:
        print(N)
        return

    dp = [[0] * H for j in range(N + 1)]
    for j in range(L, R + 1):
        dp[N][j] = 1

    for i in range(N - 1, -1, -1):
        for j in range(H):
            x = (j + A[i]) % H
            y = (j + A[i] - 1) % H

            aa = 0
            if L <= j <= R:
                aa = 1

            dp[i][j] = aa + max(dp[i + 1][x], dp[i + 1][y])

    print(dp[0][0])

if __name__ == '__main__':
    main()


def rr(): return input().rstrip()
def rri(): return int(rr())
def rrl(): return list(map(int, rr().split()))
def rrt(): return tuple(map(int, rr().split()))
from collections import defaultdict
def mus(d=0): return defaultdict(defaultdict(d))
def ms(x, y, d=0): return [[d]*y for i in range(x)]
def ar(x, d=0): return [d]*x
def ppm(m, n=0, x=0, y=0): print("\n".join(("\t".join((str(m[j][i]) for j in range(y or n))) for i in range(x or n))))
def ppmsp(m, n=0, x=0, y=0): print("\n".join(("\t".join((str(m[j][i]) for i in range(x or n))) for j in range(y or n))))
def ppa(a, n): print("\t".join(map(str, a[0:n])))
def ppl(a=0, x=[1]): x[0]=a or (x[0]+1); print("-"+"- -"*14+"-# "+str(x[0]-2)+" #-"+"- -"*14+"-")
def yn(x): return "YES" if x else "NO"
#from itertools import accumulate
#import math
def rnar(): return (rri(), rrl())
def rn2ar(): return (rri(), rrl(), rrl())
def r2nar(): return (rri(), rri(), rrl())

def read():
    a, b, c, d = rrl()
    return (a, b, c, d, rrl())

def solve(n, h, l, r, days):
    dp = ms(h,n+1, float("-inf"))
    #ppmsp(dp, 0, n, h)
    def is_good(x):
        return l <= x <= r

    dp[0][0] = 0

    for d in range(1, n+1):
        for t in range(0, h):
            ln_time = (h+t-days[d-1]) % h
            nfd = dp[ln_time][d-1]

            lr_time = (h+t-days[d-1]+1) % h
            rfd = dp[lr_time][d-1]

            dp[t][d] = max(nfd, rfd) + is_good(t)
    #ppl()
    #ppmsp(dp, 0, n+1, h)

    answ = max(dp[i][n] for i in range(h))
    return answ

if __name__ == "__main__":
    #test_count = rri()
    test_count = 1
    for _ in range(test_count):
        input_data = read()
        result = solve(*input_data)
        print(result)


def rr(): return input().rstrip()
def rri(): return int(rr())
def rrl(): return list(map(int, rr().split()))
def rrt(): return tuple(map(int, rr().split()))
from collections import defaultdict
def mus(d=0): return defaultdict(defaultdict(d))
def ms(x, y, d=0): return [[d]*y for i in range(x)]
def ar(x, d=0): return [d]*x
def ppm(m, n=0, x=0, y=0): print("\n".join(("\t".join((str(m[j][i]) for j in range(y or n))) for i in range(x or n))))
def ppmsp(m, n=0, x=0, y=0): print("\n".join(("\t".join((str(m[j][i]) for i in range(x or n))) for j in range(y or n))))
def ppa(a, n): print("\t".join(map(str, a[0:n])))
def ppl(a=0, x=[1]): x[0]=a or (x[0]+1); print("-"+"- -"*14+"-# "+str(x[0]-2)+" #-"+"- -"*14+"-")
def yn(x): return "YES" if x else "NO"
#from itertools import accumulate
#import math
def rnar(): return (rri(), rrl())
def rn2ar(): return (rri(), rrl(), rrl())
def r2nar(): return (rri(), rri(), rrl())

def read():
    a, b, c, d = rrl()
    return (a, b, c, d, rrl())

def solve(n, h, l, r, days):
    dp = ms(h,n+1, float("-inf"))
    #ppmsp(dp, 0, n, h)
    def is_good(x):
        return l <= x <= r

    dp[0][0] = int(is_good(0))

    for d in range(1, n+1):
        for t in range(0, h):
            ln_time = (h+t-days[d-1]) % h
            nfd = dp[ln_time][d-1]

            lr_time = (h+t-days[d-1]+1) % h
            rfd = dp[lr_time][d-1]

            dp[t][d] = max(nfd, rfd) + is_good(t)
    #ppl()
    #ppmsp(dp, 0, n+1, h)

    answ = max(dp[i][n] for i in range(h))
    return answ

if __name__ == "__main__":
    #test_count = rri()
    test_count = 1
    for _ in range(test_count):
        input_data = read()
        result = solve(*input_data)
        print(result)


import os, sys, bisect, copy
from collections import defaultdict, Counter, deque
from functools import lru_cache   #use @lru_cache(None)
if os.path.exists('in.txt'): sys.stdin=open('in.txt','r')
if os.path.exists('out.txt'): sys.stdout=open('out.txt', 'w')
#
def input(): return sys.stdin.readline()
def mapi(arg=0): return map(int if arg==0 else str,input().split())
#------------------------------------------------------------------

n,h,l,r = mapi()
a = list(mapi())
#@lru_cache(None)
dp  = [[-1]*2005 for i in range(2005)]
def rec(i, cur):
    if i>=n:
        return 0
    if dp[i][cur]!=-1:
        return dp[i][cur]
    ret = 0
    p = cur+a[i]
    q = (p-1)%h
    p%=h
    x = rec(i+1,p)
    if p>=l and p<=r:
        x+=1
    y = rec(i+1,q)
    if q>=l and q<=r:
        y+=1
    dp[i][cur]= max(x,y)
    return dp[i][cur]

res = rec(0,0)
print(res)


import os, sys, bisect, copy
from collections import defaultdict, Counter, deque
from functools import lru_cache   #use @lru_cache(None)
if os.path.exists('in.txt'): sys.stdin=open('in.txt','r')
if os.path.exists('out.txt'): sys.stdout=open('out.txt', 'w')
#
def input(): return sys.stdin.readline()
def mapi(arg=0): return map(int if arg==0 else str,input().split())
#------------------------------------------------------------------

n,h,l,r = mapi()
a = list(mapi())

@lru_cache(None)
def rec(i, cur):
    if i>=n:
        return 0
    ret = 0
    p = cur+a[i]
    q = (p-1)%h
    p%=h
    x = rec(i+1,p)
    if p>=l and p<=r:
        x+=1
    y = rec(i+1,q)
    if q>=l and q<=r:
        y+=1
    return max(x,y)

res = rec(0,0)
print(res)


from collections import defaultdict

IA = lambda: map(int,input().split())

n,h,l,r=IA()
a=list(IA())

def judge(x):
    if x<=r and x>=l:
        return 1
    return 0

dp=[[int(-1) for _ in range(h)] for _ in range(n+1)]

dp[0][0]=0
for i in range(0,n):
    for j in range(0,h):
        if dp[i][j]>=0:

            dp[i+1][(j+a[i])%h]=max(dp[i][j]+int(l<=(j+a[i])%h<=r),dp[i+1][(j+a[i])%h])
            dp[i+1][(j+a[i]-1)%h]=max(dp[i][j]+int(l<=(j+a[i]-1)%h<=r),dp[i+1][(j+a[i]-1)%h])

ans=max(dp[-1])
print(ans)

#a = [int(0) for i in range(1,n+1)]


from collections import defaultdict

IA = lambda: map(int,input().split())

n,h,l,r=IA()
a=list(IA())

def judge(x):
    if x<=r and x>=l:
        return 1
    return 0

dp=[[int(-1) for _ in range(h)] for _ in range(n)]

dp[0][0]=0
for i in range(0,n-1):
    for j in range(0,h):
        if dp[i][j]>=0:
            dp[i+1][(j+a[i])%h]=max(dp[i][j]+int(l<=(j+a[i])%h<=r),dp[i+1][(j+a[i])%h])
            dp[i+1][(j+a[i]-1)%h]=max(dp[i][j]+int(l<=(j+a[i]-1)%h<=r),dp[i+1][(j+a[i]-1)%h])

ans=max(dp[-1])
print(ans+1)

#a = [int(0) for i in range(1,n+1)]


def sleep(arr, cur, now, n, h, l, r, memo):
    if cur == n: return 0
    if (now, cur) in memo: return memo[(now, cur)]
    next1 = (now+arr[cur]) % h
    next2 = (now+arr[cur]-1) % h
    memo[(now, cur)] = max(int(l <= next1 <= r) + sleep(arr, cur+1, next1, n, h, l, r, memo),
                           int(l <= next2 <= r) + sleep(arr, cur+1, next2, n, h, l, r, memo))
    #print(arr[cur], memo[(now, cur)])
    return memo[(now, cur)]

def sleep(arr, n, h, l, r):
    dp = [[None] * h for _ in range(n+1)]
    dp[0][0] = 0
    for i in range(n):
        for hour in range(h):
            if dp[i][hour] == None: continue
            next1 = (hour + arr[i]) % h
            next2 = (hour + arr[i] - 1) % h
            dp[i+1][next1] = max(dp[i+1][next1] or 0, int(l <= next1 <= r) + dp[i][hour])
            dp[i+1][next2] = max(dp[i+1][next2] or 0, int(l <= next2 <= r) + dp[i][hour])
        #print(i, dp[i])
    #print(n+1, dp[n])
    return max([i or 0 for i in dp[n]])

n, h, l, r = [int(s) for s in input().split(" ")]
arr = [int(s) for s in input().split(" ")]
memo = {}
#ans = _sleep(arr, 0, 0, n, h, l, r, memo)
ans = sleep(arr, n, h, l, r)
#print(memo)
print(ans)


def sleep(arr, cur, now, n, h, l, r, memo):
    if cur == n: return 0
    if (now, cur) in memo: return memo[(now, cur)]
    next1 = (now+arr[cur]) % h
    next2 = (now+arr[cur]-1) % h
    memo[(now, cur)] = max(int(l <= next1 <= r) + sleep(arr, cur+1, next1, n, h, l, r, memo),
                           int(l <= next2 <= r) + sleep(arr, cur+1, next2, n, h, l, r, memo))
    #print(arr[cur], memo[(now, cur)])
    return memo[(now, cur)]

n, h, l, r = [int(s) for s in input().split(" ")]
arr = [int(s) for s in input().split(" ")]
memo = {}
ans = sleep(arr, 0, 0, n, h, l, r, memo)
#print(memo)
print(ans)


import math
from bisect import bisect
from itertools import groupby

def rints():
    return list(map(int,input().split()))

n,h,l,r = rints()
a = rints()

best = [0]*h
for ai in a:
    tomo = [0]*h
    for k in range(h):
        good = (l<=k and k<=r)
        tomo[(k+ai  )%h] = max(tomo[(k+ai  )%h], best[k] + good)
        tomo[(k+ai-1)%h] = max(tomo[(k+ai-1)%h], best[k] + good)
    tomo, best = best, tomo
print(max(best))


import math
from bisect import bisect
from itertools import groupby

def rints():
    return list(map(int,input().split()))

n,h,l,r = rints()
a = rints()

best = [-3000]*h
best[0] = 0
for ai in a:
    tomo = [-3000]*h
    for k in range(h):
        for wake in ((k+ai)%h, (k+ai-1)%h):
            tomo[wake] = max(tomo[wake], best[k] + (l<=wake<=r))
    tomo, best = best, tomo
print(max(best))


from math import *
# from sys import stdin,stdout

def binarySearch(arr,x,i):
	l=i
	r=len(arr)-1
	while l <= r:
		mid = (l + r)//2;
		if arr[mid] == x:
			return mid
		elif arr[mid] < x:
			l = mid + 1
		else:
			r = mid - 1
	return -1

def js(arr,x):
	l=0
	r=len(arr)-1
	ans=-1
	while(l<=r):
		m=(l+r)//2
		if(arr[m]<=x):
			ans=m
			l=m+1
		else:
			r=m-1
	return ans

def jg(arr,x,i):
	l=i
	r=len(arr)-1
	ans=-1
	while(l<=r):
		m=(l+r)//2
		if(arr[m]>=x):
			ans=m
			r=m-1
		else:
			l=m+1
	return ans

# def ceil(a,b):
# 	if a%b == 0:
# 		return int(a/b)
# 	else:
# 		return (a//b + 1)

n,h,l,r=map(int,input().split())
a=list(map(int,input().split()))

d={}
if(l<=a[0]<=r):
	d[a[0]]=1
else:
	d[a[0]]=0

if(l<=a[0]-1<=r):
	d[a[0]-1]=1
else:
	d[a[0]-1]=0

ans={}
import copy
for i in range(1,n):
	ans=copy.deepcopy(d)
	d={}
	l1 = list(ans.keys())
	l2 = list(ans.values())
	for j in range(len(l1)):
		x = (l1[j]+a[i])%h
		v = l2[j]
		y = (l1[j]+a[i]-1)%h

		if(d.get(x)==None):
			if(l<=x<=r):
				d[x]=v+1
			else:
				d[x]=v
		else:
			if(l<=x<=r):
				d[x]=max(d[x],v+1)

		if(d.get(y)==None):
			if(l<=y<=r):
				d[y]=v+1
			else:
				d[y]=v
		else:
			if(l<=y<=r):
				d[y]=max(d[y],v+1)

# print(d)
print(max(list(d.values())))


from math import *
# from sys import stdin,stdout

def binarySearch(arr,x,i):
	l=i
	r=len(arr)-1
	while l <= r:
		mid = (l + r)//2;
		if arr[mid] == x:
			return mid
		elif arr[mid] < x:
			l = mid + 1
		else:
			r = mid - 1
	return -1

def js(arr,x):
	l=0
	r=len(arr)-1
	ans=-1
	while(l<=r):
		m=(l+r)//2
		if(arr[m]<=x):
			ans=m
			l=m+1
		else:
			r=m-1
	return ans

def jg(arr,x,i):
	l=i
	r=len(arr)-1
	ans=-1
	while(l<=r):
		m=(l+r)//2
		if(arr[m]>=x):
			ans=m
			r=m-1
		else:
			l=m+1
	return ans

# def ceil(a,b):
# 	if a%b == 0:
# 		return int(a/b)
# 	else:
# 		return (a//b + 1)

n,h,l,r=map(int,input().split())
a=list(map(int,input().split()))
b=[a[0]]
for i in range(1,n):
	b.append((b[-1]+a[i])%h)
# print(b)

ans=0
xx=0
yy=0
ind=0
for i in range(n):
	if(b[i]<l):
		x=h-l+b[i]
		y=h-r+b[i]
	else:
		x=b[i]-l
		y=b[i]-r
		if(y<0):
			y=0
	if(x<=i-ind or y<=i-ind):
		ans+=1
		ind=i
	# print(x,y)
print(ans)


from math import *
# from sys import stdin,stdout

def binarySearch(arr,x,i):
	l=i
	r=len(arr)-1
	while l <= r:
		mid = (l + r)//2;
		if arr[mid] == x:
			return mid
		elif arr[mid] < x:
			l = mid + 1
		else:
			r = mid - 1
	return -1

def js(arr,x):
	l=0
	r=len(arr)-1
	ans=-1
	while(l<=r):
		m=(l+r)//2
		if(arr[m]<=x):
			ans=m
			l=m+1
		else:
			r=m-1
	return ans

def jg(arr,x,i):
	l=i
	r=len(arr)-1
	ans=-1
	while(l<=r):
		m=(l+r)//2
		if(arr[m]>=x):
			ans=m
			r=m-1
		else:
			l=m+1
	return ans

# def ceil(a,b):
# 	if a%b == 0:
# 		return int(a/b)
# 	else:
# 		return (a//b + 1)

n,h,l,r=map(int,input().split())
a=list(map(int,input().split()))
b=[a[0]]
for i in range(1,n):
	b.append((b[-1]+a[i])%h)
# print(b)

ans=0
xx=0
yy=0
ind=0
for i in range(n):
	if(b[i]<l):
		x=h-l+b[i]
		y=h-r+b[i]
	else:
		x=b[i]-l
		y=b[i]-r
		if(y<0):
			y=0
	if(x<=i or y<=i):
		ans+=1
print(ans)


from math import *
# from sys import stdin,stdout

def binarySearch(arr,x,i):
	l=i
	r=len(arr)-1
	while l <= r:
		mid = (l + r)//2;
		if arr[mid] == x:
			return mid
		elif arr[mid] < x:
			l = mid + 1
		else:
			r = mid - 1
	return -1

def js(arr,x):
	l=0
	r=len(arr)-1
	ans=-1
	while(l<=r):
		m=(l+r)//2
		if(arr[m]<=x):
			ans=m
			l=m+1
		else:
			r=m-1
	return ans

def jg(arr,x,i):
	l=i
	r=len(arr)-1
	ans=-1
	while(l<=r):
		m=(l+r)//2
		if(arr[m]>=x):
			ans=m
			r=m-1
		else:
			l=m+1
	return ans

# def ceil(a,b):
# 	if a%b == 0:
# 		return int(a/b)
# 	else:
# 		return (a//b + 1)

n,h,l,r=map(int,input().split())
a=list(map(int,input().split()))

d={}
if(l<=a[0]<=r):
	d[a[0]]=1
else:
	d[a[0]]=0

if(l<=a[0]-1<=r):
	d[a[0]-1]=1
else:
	d[a[0]-1]=0

ans={}
import copy
for i in range(1,n):
	ans=copy.deepcopy(d)
	d={}
	for j in range(len(ans.keys())):
		x = (list(ans.keys())[j]+a[i])%h
		v = list(ans.values())[j]
		y = (list(ans.keys())[j]+a[i]-1)%h

		if(d.get(x)==None):
			if(l<=x<=r):
				d[x]=v+1
			else:
				d[x]=v
		else:
			if(l<=x<=r):
				d[x]=max(d[x],v+1)

		if(d.get(y)==None):
			if(l<=y<=r):
				d[y]=v+1
			else:
				d[y]=v
		else:
			if(l<=y<=r):
				d[y]=max(d[y],v+1)

print(max(list(d.values())))


from math import *
# from sys import stdin,stdout

def binarySearch(arr,x,i):
	l=i
	r=len(arr)-1
	while l <= r:
		mid = (l + r)//2;
		if arr[mid] == x:
			return mid
		elif arr[mid] < x:
			l = mid + 1
		else:
			r = mid - 1
	return -1

def js(arr,x):
	l=0
	r=len(arr)-1
	ans=-1
	while(l<=r):
		m=(l+r)//2
		if(arr[m]<=x):
			ans=m
			l=m+1
		else:
			r=m-1
	return ans

def jg(arr,x,i):
	l=i
	r=len(arr)-1
	ans=-1
	while(l<=r):
		m=(l+r)//2
		if(arr[m]>=x):
			ans=m
			r=m-1
		else:
			l=m+1
	return ans

# def ceil(a,b):
# 	if a%b == 0:
# 		return int(a/b)
# 	else:
# 		return (a//b + 1)

n,h,l,r=map(int,input().split())
a=list(map(int,input().split()))

dp = []
for i in range(n):
	dp.append([-1]*h)

for i in range(n):
	if(i==0):
		if(l<=a[i]<=r):
			dp[0][a[i]]=1
		else:
			dp[0][a[i]]=0

		if(l<=a[i]-1<=r):
			dp[0][a[i]-1]=1
		else:
			dp[0][a[i]-1]=0
	else:
		for j in range(h):
			if(dp[i-1][j]>=0):
				if(l<=(j+a[i])%h<=r):
					dp[i][(j+a[i])%h]=max(dp[i][(j+a[i])%h],dp[i-1][j]+1)
				else:
					dp[i][(j+a[i])%h]=max(dp[i][(j+a[i])%h],dp[i-1][j])

				if(l<=(j+a[i]-1)%h<=r):
					dp[i][(j+a[i]-1)%h]=max(dp[i][(j+a[i]-1)%h],dp[i-1][j]+1)
				else:
					dp[i][(j+a[i]-1)%h]=max(dp[i][(j+a[i]-1)%h],dp[i-1][j])

print(max(dp[-1]))


from math import *
# from sys import stdin,stdout

def binarySearch(arr,x,i):
	l=i
	r=len(arr)-1
	while l <= r:
		mid = (l + r)//2;
		if arr[mid] == x:
			return mid
		elif arr[mid] < x:
			l = mid + 1
		else:
			r = mid - 1
	return -1

def js(arr,x):
	l=0
	r=len(arr)-1
	ans=-1
	while(l<=r):
		m=(l+r)//2
		if(arr[m]<=x):
			ans=m
			l=m+1
		else:
			r=m-1
	return ans

def jg(arr,x,i):
	l=i
	r=len(arr)-1
	ans=-1
	while(l<=r):
		m=(l+r)//2
		if(arr[m]>=x):
			ans=m
			r=m-1
		else:
			l=m+1
	return ans

# def ceil(a,b):
# 	if a%b == 0:
# 		return int(a/b)
# 	else:
# 		return (a//b + 1)

n,h,l,r=map(int,input().split())
a=list(map(int,input().split()))
b=[a[0]]
for i in range(1,n):
	b.append((b[-1]+a[i])%h)
# print(b)

ans=0
xx=0
yy=0
ind=0
for i in range(n):
	if(b[i]<l):
		x=h-l+b[i]
		y=h-r+b[i]
	else:
		x=b[i]-l
		y=b[i]-r
		if(y<0):
			y=0
	if(x-xx<=i-ind or y-yy<=i-ind):
		ans+=1
		xx=x
		yy=y
		ind=i
print(ans)


from math import *
# from sys import stdin,stdout

def binarySearch(arr,x,i):
	l=i
	r=len(arr)-1
	while l <= r:
		mid = (l + r)//2;
		if arr[mid] == x:
			return mid
		elif arr[mid] < x:
			l = mid + 1
		else:
			r = mid - 1
	return -1

def js(arr,x):
	l=0
	r=len(arr)-1
	ans=-1
	while(l<=r):
		m=(l+r)//2
		if(arr[m]<=x):
			ans=m
			l=m+1
		else:
			r=m-1
	return ans

def jg(arr,x,i):
	l=i
	r=len(arr)-1
	ans=-1
	while(l<=r):
		m=(l+r)//2
		if(arr[m]>=x):
			ans=m
			r=m-1
		else:
			l=m+1
	return ans

# def ceil(a,b):
# 	if a%b == 0:
# 		return int(a/b)
# 	else:
# 		return (a//b + 1)

n,h,l,r=map(int,input().split())
a=list(map(int,input().split()))

dp = []
for i in range(n):
	dp.append([-1]*h)

for i in range(n):
	if(i==0):
		if(l<=a[i]<=r):
			dp[0][a[i]]=1
		else:
			dp[0][a[i]]=0

		if(l<=a[i]-1<=r):
			dp[0][a[i]-1]=1
		else:
			dp[0][a[i]]=0
	else:
		for j in range(h):
			if(dp[i-1][j]>=0):
				if(l<=(j+a[i])%h<=r):
					dp[i][(j+a[i])%h]=max(dp[i][(j+a[i])%h],dp[i-1][j]+1)
				else:
					dp[i][(j+a[i])%h]=max(dp[i][(j+a[i])%h],dp[i-1][j])

				if(l<=(j+a[i]-1)%h<=r):
					dp[i][(j+a[i]-1)%h]=max(dp[i][(j+a[i]-1)%h],dp[i-1][j]+1)
				else:
					dp[i][(j+a[i]-1)%h]=max(dp[i][(j+a[i]-1)%h],dp[i-1][j])

print(max(dp[-1]))


from collections import Counter, defaultdict, deque
from heapq import heapify, heappush, heappop, heappushpop
from sys import stdin

# int input
int_inp = lambda : int(stdin.readline().strip())
# int array input
lst_inp = lambda : [int(i) for i in stdin.readline().strip().split(" ")]
# string input
str_inp = lambda : stdin.readline().strip()

n, h, l, r = lst_inp()
a = lst_inp()

def dp(a, i, t, h, n):
    if i >= n:
        return False
    if t not in dp.d and i < n:
        t1 = (t + a[i]) % h
        t2 = (t + a[i] - 1) % h
        dp.d[t] = True
        dp(a, i+1, t1, h, n)
        dp(a, i+1, t2, h, n)
    return dp.d

dp.d = {}
dp(a, 0, 0, h, n)

count = 0
for i in range(l, r+1):
    if dp.d[i]:
        count += 1
print(count)


from collections import Counter, defaultdict, deque
from heapq import heapify, heappush, heappop, heappushpop
from sys import stdin

# int input
int_inp = lambda : int(stdin.readline().strip())
# int array input
lst_inp = lambda : [int(i) for i in stdin.readline().strip().split(" ")]
# string input
str_inp = lambda : stdin.readline().strip()

n, h, l, r = lst_inp()
a = lst_inp()

Q = deque()
t = i = 0
Q.append((t, i))
found = set()
while Q:
    t, i = Q.popleft()
    if l <= t <= r:
        found.add(t)
    t1 = (t + a[i]) % h
    t2 = (t + a[i] - 1) % h
    if i >= n-1:
        break
    Q.append((t1, i+1))
    Q.append((t2, i+1))

print(len(found))


from collections import Counter, defaultdict, deque
from heapq import heapify, heappush, heappop, heappushpop
from sys import stdin

# int input
int_inp = lambda : int(stdin.readline().strip())
# int array input
lst_inp = lambda : [int(i) for i in stdin.readline().strip().split(" ")]
# string input
str_inp = lambda : stdin.readline().strip()

n, h, l, r = lst_inp()
a = lst_inp()
dp = [[-float('inf')]*(n+1) for _ in range(n+1)]
dp[0][0] = 0
s = 0
for i in range(n):
    s += a[i]
    for j in range(n+1):
        dp[i + 1][j] = max(dp[i + 1][j],
                         dp[i][j] + (l <=  (s-j)%h <= r))
        if j < n:
            dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + (l <= (s-j-1)%h <= r))

print(max(dp[n]))


from collections import Counter, defaultdict, deque
from heapq import heapify, heappush, heappop, heappushpop
from sys import stdin

# int input
int_inp = lambda : int(stdin.readline().strip())
# int array input
lst_inp = lambda : [int(i) for i in stdin.readline().strip().split(" ")]
# string input
str_inp = lambda : stdin.readline().strip()

n, h, l, r = lst_inp()
a = lst_inp()

Q = deque()
t = i = 0
Q.append([t, i])
found = set()
while Q:
    t, i = Q.popleft()
    if l <= t <= r:
        found.add(t)
    t1 = (t + a[i]) % h
    t2 = (t + a[i] - 1) % h
    if i >= n-1:
        break
    Q.append([t1, i+1])
    Q.append([t2, i+1])

print(len(found))


import sys
sys.setrecursionlimit(1000000)
from collections import Counter, defaultdict, deque
from heapq import heapify, heappush, heappop, heappushpop
from sys import stdin

# int input
int_inp = lambda : int(stdin.readline().strip())
# int array input
lst_inp = lambda : [int(i) for i in stdin.readline().strip().split(" ")]
# string input
str_inp = lambda : stdin.readline().strip()

n, h, l, r = lst_inp()
a = lst_inp()

def dp(a, i, t, h, n):
    if i >= n:
        return False
    if t not in dp.d and i < n:
        t1 = (t + a[i]) % h
        t2 = (t + a[i] - 1) % h
        dp.d[t] = True
        dp(a, i+1, t1, h, n)
        dp(a, i+1, t2, h, n)
    return dp.d

dp.d = {}
dp(a, 0, 0, h, n)

count = 0
for i in range(l, r+1):
    if dp.d[i]:
        count += 1
print(count)


n, h, l, r = [int(_) for _ in input().split()]
sleep_hours = [int(_) for _ in input().split()]
assert len(sleep_hours) == n, 'Invalid Test Case'
cur_time_1 = 0
cur_time_2 = 0
good_time = 0
"""
Have a 2D dp
dp[cur_n][cur_time]
"""
dp = [[0 for x in range(h)] for y in range(n + 1)]
time_list = set([0])
for index, time in enumerate(sleep_hours):
    temp = time_list.copy()
    while len(temp):
        cur_time = temp.pop()
        time_list.add((cur_time + time) % h)
        time_list.add((cur_time + time - 1) % h)
    for cur_time in time_list:
        dp[index + 1][cur_time] = max(dp[index][(cur_time - time) % h], dp[index][(cur_time - time + 1) % h]) + (l <= cur_time and cur_time <= r)
print(max(dp[n]))


n, h, l, r = [int(_) for _ in input().split()]
sleep_hours = [int(_) for _ in input().split()]
assert len(sleep_hours) == n, 'Invalid Test Case'
cur_time_1 = 0
cur_time_2 = 0
good_time = 0
"""
Have a 2D dp
dp[cur_n][cur_time]
"""
dp = [[-1 for x in range(n + 1)] for y in range(n + 1)]
dp[0][0] = 0
sleep_sum = 0
for i in range(n):
    sleep_sum = sleep_sum + sleep_hours[i]
    for j in range(n + 1):
        cur_time = (sleep_sum - j) % h
        cur_time_earlier = (sleep_sum - j - 1) % h
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + (l <= cur_time and cur_time <= r))
        if(j < n):
            dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + (l <= cur_time_earlier and cur_time_earlier <= r))
print(max(dp[n]))


import sys
INF = sys.maxsize
n, h, l, r = [int(_) for _ in input().split()]
sleep_hours = [int(_) for _ in input().split()]
assert len(sleep_hours) == n, 'Invalid Test Case'
cur_time_1 = 0
cur_time_2 = 0
good_time = 0
"""
Have a 2D dp
dp[cur_n][cur_time]
"""
dp = [[-INF for x in range(n + 1)] for y in range(n + 1)]
dp[0][0] = 0
sleep_sum = 0
for i in range(n):
    sleep_sum = sleep_sum + sleep_hours[i]
    for j in range(n + 1):
        cur_time = (sleep_sum - j) % h
        cur_time_earlier = (sleep_sum - j - 1) % h
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + (l <= cur_time and cur_time <= r))
        if(j < n):
            dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + (l <= cur_time_earlier and cur_time_earlier <= r))
print(max(dp[n]))


import sys
INF = sys.maxsize
n, h, l, r = [int(_) for _ in input().split()]
sleep_hours = [int(_) for _ in input().split()]
assert len(sleep_hours) == n, 'Invalid Test Case'
cur_time_1 = 0
cur_time_2 = 0
good_time = 0
"""
Have a 2D dp
dp[cur_n][cur_time]
"""
dp = [[-INF for x in range(h)] for y in range(n + 1)]
dp[0][0] = 0
time_list = set([0])
for index, time in enumerate(sleep_hours):
    temp = time_list.copy()
    while len(temp):
        cur_time = temp.pop()
        time_list.add((cur_time + time) % h)
        time_list.add((cur_time + time - 1) % h)
    for cur_time in time_list:
        dp[index + 1][cur_time] = max(dp[index][(cur_time - time) % h], dp[index][(cur_time - time + 1) % h]) + (l <= cur_time and cur_time <= r)
print(max(dp[n]))


# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
# sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, H, L, R = MAP()
A = LIST()

dp = list2d(N+1, H, -INF)
dp[0][0] = 0
for i, a in enumerate(A):
    for h in range(H):
        dp[i+1][(h+a)%H] = max(dp[i+1][(h+a)%H], dp[i][h] + (1 if L <= (h+a)%H <= R else 0))
        dp[i+1][(h+a-1)%H] = max(dp[i+1][(h+a-1)%H], dp[i][h] + (1 if L <= (h+a-1)%H <= R else 0))
ans = max(dp[N])
print(ans)


# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
# sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, H, L, R = MAP()
A = LIST()

dp = list2d(N+1, H, -INF)
dp[0][0] = 0
for i, a in enumerate(A):
    for h in range(H):
        dp[i+1][(h+a)%H] = max(dp[i+1][(h+a)%H], dp[i][h] + (L <= (h+a)%H <= R))
        dp[i+1][(h+a-1)%H] = max(dp[i+1][(h+a-1)%H], dp[i][h] + (L <= (h+a-1)%H <= R))
ans = max(dp[N])
print(ans)


"""
NTC here
"""
import sys
inp = sys.stdin.readline
def input(): return inp().strip()
# flush= sys.stdout.flush
# import threading
# sys.setrecursionlimit(10**6)
# threading.stack_size(2**26)

def iin(): return int(input())

def lin(): return list(map(int, input().split()))

# range = xrange
# input = raw_input

def BFS(s, adj):
    level = []
    parent = {s: None}
    lv = 1
    u = [s]
    while u:  # runs till u is []
        nextu = []
        nx = []
        for i in u:
            for v in adj[i]:
                if v not in parent:
                    parent[v] = i
                    nx.append((i, v))
                    nextu.append(v)
        level.append(nx)
        u = nextu.copy()
    level = level[::-1]
    ans = {}
    ch = 0
    for l in level:
        for p in l:
            ans[p]=ch
            ch+=1
    return ans

def main():
    from collections import defaultdict
    T = 1
    while T:
        T-=1
        n, h, l, r = lin()
        def cnv(x):
            x = x%h
            return x
        # print(cnv(h), cnv(1), cnv(0) ,cnv(-1))
        a = lin()
        ans = [[-1 for i in range(n+1)] for _ in range(n+1)]
        ans[0][0]=0
        for i in range(n):
            ans[0][i+1] = a[i]
        for i in range(1, n+1):
            ans[0][i] = cnv(ans[0][i]+ans[0][i-1])
            for j in range(1, i+1):
                ans[j][i] = cnv(ans[j-1][i]-1)
        # print(*ans, sep='\n')
        sol = set()
        for i in range( n+1):
            for j in range(1, n+1):
                if l<=ans[i][j]<=r:
                    ans[i][j]=1
                else:
                    ans[i][j]=0
        # print(*ans, sep='\n')

        for i in range(n-1, -1, -1):
            for j in range(i+1):
                # print((j, i),"->", (j, i+1), (j+1, i+1))
                ans[j][i] += max(ans[j][i+1], ans[j+1][i+1])
        # print(*ans, sep='\n')

        print(ans[0][0])

main()

# threading.Thread(target=main).start()


"""
NTC here
"""
import sys
inp = sys.stdin.readline
def input(): return inp().strip()
# flush= sys.stdout.flush
# import threading
# sys.setrecursionlimit(10**6)
# threading.stack_size(2**26)

def iin(): return int(input())

def lin(): return list(map(int, input().split()))

# range = xrange
# input = raw_input

def BFS(s, adj):
    level = []
    parent = {s: None}
    lv = 1
    u = [s]
    while u:  # runs till u is []
        nextu = []
        nx = []
        for i in u:
            for v in adj[i]:
                if v not in parent:
                    parent[v] = i
                    nx.append((i, v))
                    nextu.append(v)
        level.append(nx)
        u = nextu.copy()
    level = level[::-1]
    ans = {}
    ch = 0
    for l in level:
        for p in l:
            ans[p]=ch
            ch+=1
    return ans

def main():
    from collections import defaultdict
    T = 1
    while T:
        T-=1
        n, h, l, r = lin()
        def cnv(x):
            x = x%h
            if x==0:x=h
            return x
        # print(cnv(h), cnv(1), cnv(0) ,cnv(-1))
        a = lin()
        ans = [[0 for i in range(n+1)] for _ in range(n+1)]
        for i in range(n):
            ans[0][i+1] = a[i]
        for i in range(1, n+1):
            ans[0][i] = cnv(ans[0][i]+ans[0][i-1])
            for j in range(1, i+1):
                ans[j][i] = cnv(ans[j-1][i]-1)
        # print(*ans, sep='\n')
        sol = set()
        for i in range( n+1):
            for j in range( n+1):
                if l<=ans[i][j]<=r:
                    ans[i][j]=1
                else:
                    ans[i][j]=0
        # print(*ans, sep='\n')

        for i in range(n-1, -1, -1):
            for j in range(i+1):
                # print((j, i),"->", (j, i+1), (j+1, i+1))
                ans[j][i] += max(ans[j][i+1], ans[j+1][i+1])
        # print(*ans, sep='\n')

        print(ans[0][0])

main()

# threading.Thread(target=main).start()


"""
NTC here
"""
import sys
inp = sys.stdin.readline
def input(): return inp().strip()
# flush= sys.stdout.flush
# import threading
# sys.setrecursionlimit(10**6)
# threading.stack_size(2**26)

def iin(): return int(input())

def lin(): return list(map(int, input().split()))

# range = xrange
# input = raw_input

def BFS(s, adj):
    level = []
    parent = {s: None}
    lv = 1
    u = [s]
    while u:  # runs till u is []
        nextu = []
        nx = []
        for i in u:
            for v in adj[i]:
                if v not in parent:
                    parent[v] = i
                    nx.append((i, v))
                    nextu.append(v)
        level.append(nx)
        u = nextu.copy()
    level = level[::-1]
    ans = {}
    ch = 0
    for l in level:
        for p in l:
            ans[p]=ch
            ch+=1
    return ans

def main():
    from collections import defaultdict
    T = 1
    while T:
        T-=1
        n, h, l, r = lin()
        def cnv(x):
            x = x%h
            if x==0:x=h
            return x
        # print(cnv(h), cnv(1), cnv(0) ,cnv(-1))
        a = lin()
        ans = [[0 for i in range(n+1)] for _ in range(n+1)]
        for i in range(n):
            ans[0][i+1] = a[i]
        for i in range(1, n+1):
            ans[0][i] = cnv(ans[0][i]+ans[0][i-1])
            for j in range(1, i+1):
                ans[j][i] = cnv(ans[j-1][i]-1)
        # print(*ans, sep='\n')
        sol = set()
        for i in range(1, n+1):
            for j in range( n+1):
                if l<=ans[i][j]<=r:
                    sol.add(j)
        print(len(sol))

main()

# threading.Thread(target=main).start()

"""
1
5
1 3
1 4
1 6
2 6
0 5
"""


def g(t): # g for good sleep
  if l <= t <= r:
    return 1
  else:
    return 0

def get_dp(i,j):
  a = dp[i-1][j] + g((s[i] - j) % h)
  b = dp[i-1][(j-1)%h] + g((s[i] - j) % h)
  return max(a,b)

n, h, l, r = map(int, input().split())
a = list(map(int, input().split()))
a = [0] + a
s = [0] * (n+1)

for i in range(1,n+1):
  s[i] = (s[i-1] + a[i]) % h

dp = [[0]*h for _ in range(n+1)]

for i in range(1,min(n+1,h)):
  for j in range(i+1):
    if j == 0:
      dp[i][j] = dp[i-1][j] + g((s[i] - j) & h)
    else:
      dp[i][j] = get_dp(i,j)

for i in range(h,n+1):
  for j in range(h):
    dp[i][j] = get_dp[i][j]

print(max(dp[n][j] for j in range(h)))


def g(t): # g for good sleep
  if l <= t <= r:
    return 1
  else:
    return 0

def get_dp(i,j):
  a = dp[i-1][j] + g((s[i] - j) % h)
  b = dp[i-1][(j-1)%h] + g((s[i] - j) % h)
  return max(a,b)

n, h, l, r = map(int, input().split())
a = list(map(int, input().split()))
a = [0] + a
s = [0] * (n+1)

for i in range(1,n+1):
  s[i] = (s[i-1] + a[i]) % h

dp = [[0]*h for _ in range(n+1)]

for i in range(1,min(n+1,h)):
  for j in range(i+1):
    if j == 0:
      dp[i][j] = dp[i-1][j] + g((s[i] - j) & h)
    else:
      dp[i][j] = get_dp(i,j)

for i in range(h,n+1):
  for j in range(h):
    dp[i][j] = get_dp(i,j)

print(max(dp[n][j] for j in range(h)))


def g(t,l,r): # g for good sleep
  if l <= t <= r:
    return 1
  else:
    return 0

# def get_dp(i,j):
#   x = dp[i-1][j] + g((s[i] - j) % h)
#   y = dp[i-1][(j-1)%h] + g((s[i] - j) % h)
#   return max(x,y)

n, h, l, r = map(int, input().split())
a = list(map(int, input().split()))
a = [0] + a
s = [0] * (n+1)

for i in range(1,n+1):
  s[i] = (s[i-1] + a[i]) % h

dp = [[0]*h for _ in range(n+1)]

for i in range(1,min(n+1,h)):
  for j in range(i+1):
    if j == 0:
      dp[i][j] = dp[i-1][j] + g(s[i],l,r)
    else:
      dp[i][j] = max(dp[i-1][j], dp[i-1][(j-1)%h]) + g((s[i]-j)%h,l,r)

for i in range(h,n+1):
  for j in range(h):
    dp[i][j] = max(dp[i-1][j], dp[i-1][(j-1)%h]) + g((s[i]-j)%h,l,r)

print(max(dp[n][j] for j in range(h)))


n,h,l,r=map(int,input().split())
v=list(map(int,input().split()))
dp=[[0 for j in range(h)] for i in range(n+1)]
for i in range(1,n+1):
    for j in range(h):
        dp[i][j]=max(dp[i-1][(j-v[i-1]+h)%h],dp[i-1][(j-v[i-1]+1+h)%h])
        if j>=l and j<=r:
            dp[i][j]+=1
ans=0
for i in range(h):
    ans=max(ans,dp[n][i])
print(ans)


n,h,l,r=map(int,input().split())
v=list(map(int,input().split()))
dp=[[0 for j in range(h)] for i in range(n)]
for i in range(n):
    if i==0:
        if v[0]>=l and v[0]<=r:
            dp[0][v[0]]=1
        if v[0]-1>=l and v[0]-1<=r:
            dp[0][v[0]-1]=1
        continue
    for j in range(h):
        dp[i][j]=max(dp[i-1][(j-v[i]+h)%h],dp[i-1][(j-v[i]+1+h)%h])
        if j>=l and j<=r:
            dp[i][j]+=1
ans=0
for i in range(h):
    ans=max(ans,dp[n-1][i])
print(ans)


import math,sys
from sys import stdin, stdout
from collections import Counter, defaultdict, deque
input = stdin.readline
I = lambda:int(input())
li = lambda:list(map(int,input().split()))

def solve(a,n,h,l,r,i,time,ans):
    if(i==n):
        return 0
    k=(time+a[i])%h
    p=q=0
    if(k>=l and k<=r):
        p=1
    m=(time+a[i]-1)%h
    if(m>=l and m<=r):
        q=1
    if(ans[k][i]==-1):
        ans[k][i]=p+solve(a,n,h,l,r,i+1,(time+a[i])%h,ans)
    if(ans[m][i]==-1):
        ans[m][i]=q+solve(a,n,h,l,r,i+1,(time+a[i]-1)%h,ans)
    return(max(ans[k][i],ans[m][i]))

def case():
    n,h,l,r=li()
    ans=[[-1 for i in range (n)] for j in range (h)]
    a=li()
    print(solve(a,n,h,l,r,0,0,ans))

for _ in range(1):
    case()


import math,sys
from collections import Counter, defaultdict, deque
from sys import stdin, stdout
input = stdin.readline
li = lambda:list(map(int,input().split()))

def onesComplement(n):
    number_of_bits = (int)(math.floor(math.log(n) /math.log(2))) + 1;
    return ((1 << number_of_bits) - 1) ^ n;

def isPrime(n) :
    if (n <= 1) :
        return False
    if (n <= 3) :
        return True
    if (n % 2 == 0 or n % 3 == 0) :
        return False

    i = 5
    while(i * i <= n) :
        if (n % i == 0 or n % (i + 2) == 0) :
            return False
        i = i + 6
    return True

def case():
    a,b=li()

    if(a==0 and b==0):
        print(0)
    elif(a>b):
        print(-1)
    elif(a==b):
        print(1)
        print(a)
    else:
        k=b-a
        x=onesComplement(a)
       # print(x,x^a,b)
        if(x+(x^a)==b):
            print(2)
            print(x,x^a)
        elif(k%2==0):
            ans=[k//2,k//2,a]
            print(len(ans))
            print(*ans)
        else:
            if((b-a)%2==0):
                print(3)
                print((b-a)//2,(b-a)//2,a)
            else:
                print(-1)

for _ in range(1):
    case()


# ------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# ------------------- fast io --------------------

n,h,l,r=map(int,input().split())
vals=list(map(int,input().split()))
good=[[0]]
v1=0
for j in range(n):
    v1+=vals[j]
    v1=v1%h
    print(v1)
    lg=good[-1]
    g2=good[-1]+[]
    for b in range(j+1):
        if b==0:
            if l<=v1 and v1<=r:
                g2[0]+=1
        else:
            val1=lg[b-1]
            val2=lg[b]
            if l<=(v1-b)%h and (v1-b)%h<=r:
                val2+=1
                val1+=1
            g2[b]=max(val1,val2)
    if l<=(v1-(j+1))%h and (v1-(j+1))%h<=r:
        g2.append(1+lg[-1])
    else:
        g2.append(lg[-1])
    good.append(g2)
last=good[-1]
print(max(last))


# ------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# ------------------- fast io --------------------

n,h,l,r=map(int,input().split())
vals=list(map(int,input().split()))
good=[[0]]
v1=0
for j in range(n):
    v1+=vals[j]
    v1=v1%h
    lg=good[-1]
    g2=good[-1]+[]
    for b in range(j+1):
        if b==0:
            if l<=v1 and v1<=r:
                g2[0]+=1
        else:
            val1=lg[b-1]
            val2=lg[b]
            if l<=(v1-b)%h and (v1-b)%h<=r:
                val2+=1
                val1+=1
            g2[b]=max(val1,val2)
    if l<=(v1-(j+1))%h and (v1-(j+1))%h<=r:
        g2.append(1+lg[-1])
    else:
        g2.append(lg[-1])
    good.append(g2)
last=good[-1]
print(max(last))


# ------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# ------------------- fast io --------------------

n,h,l,r=map(int,input().split())
vals=list(map(int,input().split()))
good=[[0]]
v1=0
for j in range(n):
    v1+=vals[j]
    v1=v1%h
    lg=good[-1]
    g2=good[-1]+[]
    for b in range(j+1):
        if b==0:
            if l<=v1 and v1<=r:
                g2[0]+=1
        else:
            val1=lg[b-1]
            val2=lg[b]
            if l<=(v1-b)%h and (v1-b)%h<=r:
                val2+=1
                val1+=1
                g2[b]=max(val1,val2)
    if l<=(v1-(j+1))%h and (v1-(j+1))%h<=r:
        g2.append(1+lg[-1])
    else:
        g2.append(lg[-1])
    good.append(g2)
last=good[-1]
print(max(last))