Vova had a pretty weird sleeping schedule. There are $h$ hours in a day. Vova will sleep exactly $n$ times. The $i$-th time he will sleep exactly after $a_i$ hours from the time he woke up. You can assume that Vova woke up exactly at the beginning of this story (the initial time is $0$). Each time Vova sleeps exactly one day (in other words, $h$ hours).

Vova thinks that the $i$-th sleeping time is good if he starts to sleep between hours $l$ and $r$ inclusive.

Vova can control himself and before the $i$-th time can choose between two options: go to sleep after $a_i$ hours or after $a_i - 1$ hours.

Your task is to say the maximum number of good sleeping times Vova can obtain if he acts optimally.


-----Input-----

The first line of the input contains four integers $n, h, l$ and $r$ ($1 \le n \le 2000, 3 \le h \le 2000, 0 \le l \le r < h$) — the number of times Vova goes to sleep, the number of hours in a day and the segment of the good sleeping time.

The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i < h$), where $a_i$ is the number of hours after which Vova goes to sleep the $i$-th time.


-----Output-----

Print one integer — the maximum number of good sleeping times Vova can obtain if he acts optimally.


-----Example-----
Input
7 24 21 23
16 17 14 20 20 11 22

Output
3



-----Note-----

The maximum number of good times in the example is $3$.

The story starts from $t=0$. Then Vova goes to sleep after $a_1 - 1$ hours, now the time is $15$. This time is not good. Then Vova goes to sleep after $a_2 - 1$ hours, now the time is $15 + 16 = 7$. This time is also not good. Then Vova goes to sleep after $a_3$ hours, now the time is $7 + 14 = 21$. This time is good. Then Vova goes to sleep after $a_4 - 1$ hours, now the time is $21 + 19 = 16$. This time is not good. Then Vova goes to sleep after $a_5$ hours, now the time is $16 + 20 = 12$. This time is not good. Then Vova goes to sleep after $a_6$ hours, now the time is $12 + 11 = 23$. This time is good. Then Vova goes to sleep after $a_7$ hours, now the time is $23 + 22 = 21$. This time is also good.

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Dynamic programming

Implementation


Solutions:
from bisect import bisect_left as bl, bisect_right as br, insort
import sys
import heapq
from math import *
from collections import defaultdict as dd, deque

def data():
	return sys.stdin.readline().strip()

def mdata():
	return map(int, data().split())
(n, h, l, r) = mdata()
A = list(mdata())
dp = [0] * h
ndp = [0] * h
k = 0
for i in range(n):
	k = (k + A[i]) % h
	for j in range(min(i + 2, h)):
		t = k - j
		if t < 0:
			t += h
		m = max(dp[j], dp[j - 1])
		if l <= t <= r:
			m += 1
		ndp[j] = m
	dp = ndp.copy()
print(max(dp))


from sys import stdin
from math import sqrt
import time
inf = stdin
(n, h, l, r) = map(int, inf.readline().split(' '))
arr1 = list(map(int, inf.readline().split(' ')))
dp = [[-1 for i in range(h)] for j in range(n + 1)]
dp[0][0] = 0
for i in range(1, n + 1):
	for j in range(h):
		prev1 = (j - arr1[i - 1] + h) % h
		prev2 = (j - arr1[i - 1] + 1 + h) % h
		toadd = 0
		if l <= j and r >= j:
			toadd = 1
		if dp[i - 1][prev1] != -1:
			dp[i][j] = max(dp[i][j], dp[i - 1][prev1] + toadd)
		if dp[i - 1][prev2] != -1:
			dp[i][j] = max(dp[i][j], dp[i - 1][prev2] + toadd)
ans = 0
for i in range(n + 1):
	ans = max(dp[i])
print(ans)


import sys, math
input = sys.stdin.readline

def inp():
	return int(input())

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

def insr():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())
t = 1
for tt in range(t):
	(n, h, l, r) = invr()
	a = inlt()
	q = [0] + [-1] * (h - 1)
	for i in range(len(a)):
		w = [-1] * h
		for qw in range(h):
			if q[qw] == -1:
				continue
			if l <= (qw + a[i]) % h <= r:
				w[(qw + a[i]) % h] = max(w[(qw + a[i]) % h], q[qw] + 1)
			else:
				w[(qw + a[i]) % h] = max(w[(qw + a[i]) % h], q[qw])
			if l <= (qw + a[i] - 1) % h <= r:
				w[(qw + a[i] - 1) % h] = max(w[(qw + a[i] - 1) % h], q[qw] + 1)
			else:
				w[(qw + a[i] - 1) % h] = max(w[(qw + a[i] - 1) % h], q[qw])
		q = w
	print(max(q))


import sys
input = sys.stdin.readline

def I():
	return input().strip()

def II():
	return int(input().strip())

def LI():
	return [*map(int, input().strip().split())]
import string, math, time, functools, random, fractions
from heapq import heappush, heappop, heapify
from bisect import bisect_left, bisect_right
from collections import deque, defaultdict, Counter, OrderedDict
from itertools import permutations, combinations, groupby
from operator import itemgetter
(num, hour, left, right) = LI()
arr = LI()
presum = [0] * num
presum[0] = arr[0]
for i in range(1, num):
	presum[i] = arr[i] + presum[i - 1]
dp = [[0 for i in range(num + 1)] for j in range(num + 1)]
dp[0][0] = 0
for i in range(1, num + 1):
	dp[i][0] = dp[i - 1][0] + (1 if left <= presum[i - 1] % hour <= right else 0)
for i in range(1, num + 1):
	for j in range(1, i + 1):
		dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1])
		dp[i][j] += 1 if left <= (presum[i - 1] - j + hour) % hour <= right else 0
mex = 0
for i in range(num + 1):
	mex = max(mex, dp[-1][i])
print(mex)


(n, h, l, r) = (int(i) for i in input().split(' '))
li = [int(i) for i in input().split(' ')]
d = {}
if l <= li[0] and li[0] <= r:
	d[li[0]] = 1
else:
	d[li[0]] = 0
if l <= li[0] - 1 and li[0] - 1 <= r:
	d[li[0] - 1] = 1
else:
	d[li[0] - 1] = 0
li.pop(0)
for i in li:
	td = {}
	for (j, jv) in d.items():
		tmp = (j + i) % h
		if l <= tmp and tmp <= r:
			td[tmp] = max(jv + 1, td.get(tmp, 1))
		else:
			td[tmp] = max(jv, td.get(tmp, 0))
		tmp -= 1
		tmp %= h
		if l <= tmp and tmp <= r:
			td[tmp] = max(jv + 1, td.get(tmp, 1))
		else:
			td[tmp] = max(jv, td.get(tmp, 0))
	d = td
m = 0
for (k, v) in d.items():
	m = max(m, v)
print(m)


(n, h, l, r) = map(int, input().split())
dp = [-float('inf')] * h
dp[0] = 0
for x in map(int, input().split()):
	tmp = []
	for i in range(h):
		tmp.append(max(dp[(i - x) % h], dp[(i - x + 1) % h]))
	for i in range(l, r + 1):
		tmp[i] += 1
	dp = tmp
print(max(dp))


from itertools import accumulate
(N, H, L, R) = map(int, input().split())
S = list(map(int, input().split()))
S_acc = [0] + list(accumulate(S))
DP = [[0] * (N + 1) for _ in range(N + 1)]
for i in range(1, N + 1):
	for j in range(i + 1):
		r = (S_acc[i] - j + H) % H
		if j == 0:
			if L <= r <= R:
				DP[i][j] = DP[i - 1][j] + 1
			else:
				DP[i][j] = DP[i - 1][j]
		elif L <= r <= R:
			DP[i][j] = max(DP[i - 1][j - 1], DP[i - 1][j]) + 1
		else:
			DP[i][j] = max(DP[i - 1][j - 1], DP[i - 1][j])
print(max(DP[-1]))


z = lambda : map(int, input().split())
(a, b, c, d) = z()
e = [-10000000000] * b
f = list(map(int, input().split()))
e[0] = 0
for i in range(a):
	k = []
	for j in range(b):
		o = max(e[(j - f[i]) % b], e[(j - f[i] + 1) % b])
		if c <= j <= d:
			o += 1
		k += [o]
	e = k
print(max(e))


(n, h, l, r) = map(int, input().split())
arr = list(map(int, input().split()))
goodtime = {0: 0}
current_unoffsetted_time = 0
for (step, a) in enumerate(arr):
	step += 1
	current_unoffsetted_time += a
	current_unoffsetted_time %= h
	for i in reversed(range(step + 1)):
		if 0 < i < step:
			goodtime[i] = max(goodtime[i], goodtime[i - 1])
		elif i == step:
			goodtime[i] = goodtime[i - 1]
	for i in range(step + 1):
		if l <= (current_unoffsetted_time - i) % h <= r:
			goodtime[i] += 1
print(max(goodtime.values()))


(n, h, l, r) = map(int, input().split())
A = list(map(int, input().split()))
dp = [[-1] * h for _ in range(2)]
dp[1][0] = 0
flag = 1
for i in range(n):
	for j in range(h):
		if dp[flag][j] == -1:
			continue
		h1 = (j + A[i]) % h
		h2 = (j + A[i] - 1) % h
		if l <= h1 <= r:
			dp[flag ^ 1][h1] = max(dp[flag ^ 1][h1], dp[flag][j] + 1)
		else:
			dp[flag ^ 1][h1] = max(dp[flag ^ 1][h1], dp[flag][j])
		if l <= h2 <= r:
			dp[flag ^ 1][h2] = max(dp[flag ^ 1][h2], dp[flag][j] + 1)
		else:
			dp[flag ^ 1][h2] = max(dp[flag ^ 1][h2], dp[flag][j])
	for j in range(h):
		dp[flag][j] = -1
	flag ^= 1
print(max(dp[flag]))


(n, hr, i, o) = map(int, input().split())
h = list(map(int, input().split()))
h = [0] + h
dp = [[-1 for j in range(hr)] for k in range(n + 1)]
dp[0][0] = 0
j = 1
while j <= n:
	k = 0
	while k < hr:
		if dp[j - 1][k] != -1:
			d = (k + h[j] - 1) % hr
			c = (k + h[j]) % hr
			if d >= i and d <= o:
				dp[j][d] = max(dp[j][d], dp[j - 1][k] + 1)
			else:
				dp[j][d] = max(dp[j][d], dp[j - 1][k])
			if c >= i and c <= o:
				dp[j][c] = max(dp[j][c], dp[j - 1][k] + 1)
			else:
				dp[j][c] = max(dp[j][c], dp[j - 1][k])
		k += 1
	j += 1
mini = 0
for j in range(1, n + 1):
	for k in range(i, o + 1):
		if dp[j][k] > mini:
			mini = dp[j][k]
print(mini)


import sys
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):
		dp[i][j] = dp[i - 1][(j - a[i - 1]) % h]
		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)


from collections import Counter, defaultdict, deque
from heapq import heapify, heappush, heappop, heappushpop
from sys import stdin
int_inp = lambda : int(stdin.readline().strip())
lst_inp = lambda : [int(i) for i in stdin.readline().strip().split(' ')]
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 sys import stdin, stdout

def main():
	(n, h, l, r) = list(map(int, stdin.readline().split()))
	arr = list(map(int, stdin.readline().split()))
	dp = [[-1] * h for _ in range(n + 1)]
	dp[0][0] = 0
	ans = -1
	for i in range(n):
		for j in range(h):
			if dp[i][j] != -1:
				temp = dp[i][j]
				if l <= (j + arr[i]) % h <= r:
					temp += 1
				dp[i + 1][(j + arr[i]) % h] = max(dp[i + 1][(j + arr[i]) % h], temp)
				ans = max(dp[i + 1][(j + arr[i]) % h], ans)
				temp = dp[i][j]
				if l <= (j + arr[i] - 1) % h <= r:
					temp += 1
				dp[i + 1][(j + arr[i] - 1) % h] = max(dp[i + 1][(j + arr[i] - 1) % h], temp)
				ans = max(dp[i + 1][(j + arr[i] - 1) % h], ans)
	stdout.write(str(ans))
main()


(n, h, l, r) = map(int, input().split())
A = list(map(int, input().split()))
dp = [[-1] * h for _ in range(n + 1)]
dp[0][0] = 0
for i in range(n):
	a = A[i]
	for j in range(h):
		if dp[i][j] == -1:
			continue
		nj = (j + a) % h
		dp[i + 1][nj] = max(dp[i + 1][nj], dp[i][j])
		nj = (j + a - 1) % h
		dp[i + 1][nj] = max(dp[i + 1][nj], dp[i][j])
	for j in range(h):
		if dp[i + 1][j] == -1:
			continue
		if l <= j <= r:
			dp[i + 1][j] += 1
print(max(dp[-1]))


import sys
import math
input = sys.stdin.readline
t = 1
for _ in range(t):
	(n, h, l, r) = map(int, input().split())
	a = list(map(int, input().split()))
	a.insert(0, 0)
	dp = [[0 for j in range(n + 1)] for i in range(n + 1)]
	pref = [0] * (n + 2)
	for i in range(1, n + 1):
		pref[i] = pref[i - 1] + a[i]
	if a[1] >= l and a[1] <= r:
		dp[1][0] = 1
	if a[1] - 1 >= l and a[1] - 1 <= r:
		dp[1][1] = 1
	for i in range(2, n + 1):
		for j in range(i + 1):
			dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1])
			x = (pref[i] - j) % h
			if x >= l and x <= r:
				dp[i][j] += 1
	maxi = 0
	for j in range(n + 1):
		maxi = max(dp[n][j], maxi)
	print(maxi)


import copy
(n, h, l, r) = map(int, input().split())
A = list(map(int, input().split()))
V = [0] * h
V[0] = 1
G = [0] * h
for i in range(n):
	R = [0] * h
	RG = [0] * h
	for j in range(h):
		if V[j] == 1:
			X = (j + A[i]) % h
			Y = (j + A[i] + h - 1) % h
			R[X] = R[Y] = 1
			if l <= X <= r:
				RG[X] = max(RG[X], 1 + G[j])
			else:
				RG[X] = max(RG[X], G[j])
			if l <= Y <= r:
				RG[Y] = max(RG[Y], 1 + G[j])
			else:
				RG[Y] = max(RG[Y], G[j])
	V = R
	G = RG
print(max(G))


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
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')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

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
dp = [[-1 for i in range(2001)] for j in range(2001)]

def rec(curr, i, l, r, a, h):
	if i == len(a):
		return 0
	if dp[curr % h][i] != -1:
		return dp[curr % h][i]
	if l <= (curr + a[i]) % h <= r:
		t1 = 1
	else:
		t1 = 0
	if l <= (curr + a[i] - 1) % h <= r:
		t2 = 1
	else:
		t2 = 0
	temp = max(rec(curr + a[i], i + 1, l, r, a, h) + t1, rec(curr + a[i] - 1, i + 1, l, r, a, h) + t2)
	dp[curr % h][i] = temp
	return dp[curr % h][i]

def solve():
	(n, h, l, r) = sep()
	a = lis()
	print(rec(0, 0, l, r, a, h))
testcase(1)


(n, h, l, r) = list(map(int, input().strip().split()))
arr = list(map(int, input().strip().split()))
dp_arr = [[0 for j in range(h)] for i in range(n + 1)]
pre_set = set([0])
for i in range(n):
	temp_set = set()
	for j in pre_set:
		for k in [arr[i], arr[i] - 1]:
			pre_val = dp_arr[i][j]
			new_time = (j + k) % h
			if l <= new_time and new_time <= r:
				pre_val += 1
			dp_arr[i + 1][new_time] = max(dp_arr[i + 1][new_time], pre_val)
			temp_set.add(new_time)
	pre_set = temp_set
print(max(dp_arr[-1]))


import os, sys
from collections import defaultdict, Counter, deque
from io import BytesIO, IOBase
from math import gcd

def main():
	(n, h, l, r) = map(int, input().split())
	a = [int(x) for x in input().split()]
	for i in range(1, n):
		a[i] = (a[i - 1] + a[i]) % h
	dp = [[0 for i in range(n + 1)] for j in range(n + 1)]
	for i in range(1, n + 1):
		f1 = int(l <= a[i - 1] <= r)
		dp[i][0] = dp[i - 1][0] + f1
	for i in range(1, n + 1):
		for j in range(1, i + 1):
			f1 = int(l <= (a[i - 1] - j) % h <= r)
			dp[i][j] = max(dp[i - 1][j] + f1, dp[i - 1][j - 1] + f1)
	print(max(dp[n]))
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')
main()


import sys
input = sys.stdin.readline
(n, h, l, r) = [int(x) for x in input().strip().split()]
a = [int(x) for x in input().strip().split()]
m = [[-1] * h for i in range(n + 1)]
m[0][0] = 0
for i in range(1, len(a) + 1):
	for hi in range(h):
		if m[i - 1][(hi + h - a[i - 1]) % h] >= 0:
			m[i][hi] = max(m[i][hi], m[i - 1][(hi + h - a[i - 1]) % h])
		if m[i - 1][(hi + h - a[i - 1] + 1) % h] >= 0:
			m[i][hi] = max(m[i][hi], m[i - 1][(hi + h - a[i - 1] + 1) % h])
		if m[i][hi] >= 0 and l <= hi <= r:
			m[i][hi] += 1
print(max(m[-1]))


from math import factorial
from collections import Counter, defaultdict
from heapq import heapify, heappop, heappush
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')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def N():
	return int(input())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)
mod = 1000000007
INF = float('inf')

def main():
	(n, h, l, r) = RL()
	arr = list(RL())
	dp = {0: 0}
	for i in range(1, n + 1):
		now = defaultdict(int)
		for (j, v) in dp.items():
			a = (j + arr[i - 1] - 1) % h
			b = (j + arr[i - 1]) % h
			now[a] = max(now[a], v + (1 if l <= a <= r else 0))
			now[b] = max(now[b], v + (1 if l <= b <= r else 0))
		dp = now
	print(max(dp.values()))
main()


import os
import sys
from io import BytesIO, IOBase
from math import log2, ceil, sqrt, gcd, log
from collections import *
from bisect import *
from heapq import *
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')
g = lambda : input().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
rr = lambda x: reversed(range(x))
mod = int(1000000000.0) + 7
inf = float('inf')
(n, h, l, r) = gil()
a = gil()
for i in range(1, n):
	a[i] += a[i - 1]
	if a[i] >= h:
		a[i] %= h
dp = [[0 for _ in range(n)] for _ in range(n + 1)]
for i in range(n):
	dp[0][i] += (1 if l <= a[i] <= r else 0) + (dp[0][i - 1] if i else 0)
ans = dp[0][-1]
for i in range(1, n + 1):
	for j in range(i - 1, n):
		at = (a[j] - i + h) % h
		dp[i][j] += (1 if l <= at <= r else 0) + max(dp[i - 1][j - 1] if j else 0, dp[i][j - 1] if j >= i else 0)
	ans = max(ans, dp[i][-1])
print(ans)


def maxgood(dp, a, i, t, h, l, r):
	if i >= len(a):
		return 1 if l <= t and t <= r else 0
	c = 0
	if l <= t and t <= r:
		c += 1
	if dp[i + 1][(t + a[i]) % h] != -1:
		v1 = dp[i + 1][(t + a[i]) % h]
	else:
		v1 = maxgood(dp, a, i + 1, (t + a[i]) % h, h, l, r)
		dp[i + 1][(t + a[i]) % h] = v1
	if dp[i + 1][(t + a[i] - 1) % h] != -1:
		v2 = dp[i + 1][(t + a[i] - 1) % h]
	else:
		v2 = maxgood(dp, a, i + 1, (t + a[i] - 1) % h, h, l, r)
		dp[i + 1][(t + a[i] - 1) % h] = v2
	return max(v1, v2) + c
(n, h, l, r) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
dp = [[-1 for i in range(h)] for j in range(len(a) + 1)]
v1 = maxgood(dp, a, 1, a[0] % h, h, l, r)
v2 = maxgood(dp, a, 1, (a[0] - 1) % h, h, l, r)
print(max(v1, v2))


import math
(n, h, l, r) = map(int, input().split())
z = list(map(int, input().split()))
dp = [[-1 * math.inf for i in range(h + 1)] for i in range(n)]
dp[0][z[0]] = int(l <= z[0] <= r)
dp[0][z[0] - 1] = int(l <= z[0] - 1 <= r)
for i in range(1, n):
	for j in range(h + 1):
		dp[i][j] = (l <= j <= r) + max(dp[i - 1][(j - z[i]) % h], dp[i - 1][(j - z[i] + 1) % h])
print(max(max(dp[n - 1]), 0))


def r(t=int):
	return list(map(t, input().split()))

def ri(t=int):
	return t(input())

def cd(it):
	ret_val = dict()
	for v in it:
		ret_val[v] = ret_val.get(v, 0) + 1
	return ret_val
(n, h, s, e) = r()
aa = r()
stack = [[0] * (n + 2) for _ in range(n + 1)]
ss = 0
for (i, ai) in enumerate(aa):
	ss += ai
	for ii in range(i + 2):
		b = stack[i - 1][ii]
		a = stack[i - 1][ii - 1]
		m = max(a, b)
		if s <= (ss - ii + h) % h <= e:
			stack[i][ii] = m + 1
		else:
			stack[i][ii] = m
print(max(stack[i]))


def main():
	import sys
	input = sys.stdin.readline
	(N, H, L, R) = list(map(int, input().split()))
	A = list(map(int, input().split()))
	dp = [[-1] * H for _ in range(N + 1)]
	dp[0][0] = 0
	for (i, a) in enumerate(A):
		for t in range(H):
			if dp[i][t] >= 0:
				dp[i + 1][(t + a) % H] = max(dp[i + 1][(t + a) % H], dp[i][t] + int(L <= (t + a) % H <= R))
				dp[i + 1][(t + a - 1) % H] = max(dp[i + 1][(t + a - 1) % H], dp[i][t] + int(L <= (t + a - 1) % H <= R))
	print(max(dp[-1]))

def __starting_point():
	main()
__starting_point()


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


(n, h, l, r) = map(int, input().split())
dp = [[0] * (n + 1) for _ in range(n + 1)]
t = 0
for (i, a) in enumerate(map(int, input().split()), 1):
	t += a
	dp[i][0] = dp[i - 1][0] + (l <= t % h <= r)
	for j in range(1, i + 1):
		t1 = t - j
		dp[i][j] = max(dp[i - 1][j - 1], dp[i - 1][j]) + (l <= t1 % h <= r)
print(max(dp[n]))


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


import sys

def readInt():
	return int(rl())

def readList():
	return list(map(int, rl().strip().split()))

def readStr():
	return rl().strip()

def makeMatrix(a, b, value):
	z = [value] * a
	for i in range(a):
		z[i] = [value] * b
	return z
INF = 100500
rl = input
(n, h, l, r) = readList()
a = readList()
t = [-INF] * h
t[0] = 0
for x in a:
	nt = [-INF] * h
	for i in range(h):
		j = (i + x) % h
		d = 0
		if l <= j <= r:
			d = 1
		nt[j] = max(nt[j], t[i] + d)
		j = (i + x - 1) % h
		d = 0
		if l <= j <= r:
			d = 1
		nt[j] = max(nt[j], t[i] + d)
	t = nt[:]
print(max(t))


inf = -1000000000
(n, h, up, down) = map(int, input().split())
a = [int(X) for X in input().split()]
dp = [[0 for i in range(n + 1)] for j in range(n + 1)]
dp[0][0] = 0
s = 0
for i in range(n):
	s += a[i]
	for j in range(i + 1):
		if up <= (s - j) % h <= down:
			bb = 1
		else:
			bb = 0
		dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + bb)
		if up <= (s - j - 1) % h <= down:
			bb = 1
		else:
			bb = 0
		dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + bb)
print(max(dp[n]))


from collections import defaultdict, Counter
from sys import stdin
import io, os
input = stdin.readline
(n, h, l, r) = map(int, input().split())
a = list(map(int, input().split()))
M = 1000001
n_inf = float('-inf')
dp = defaultdict(lambda : n_inf)
dp[0] = 0
for i in range(1, n + 1):
	a_i = a[i - 1]
	for hour in range(h):
		c_1 = (a_i + hour - 1) % h
		c_2 = (a_i + hour) % h
		dp[i * M + c_1] = max(dp[(i - 1) * M + hour] + (l <= c_1 <= r), dp[i * M + c_1])
		dp[i * M + c_2] = max(dp[(i - 1) * M + hour] + (l <= c_2 <= r), dp[i * M + c_2])
ans = 0
for i in range(h):
	ans = max(ans, dp[n * M + i])
print(ans)


(n, h, l, r) = map(int, input().split())
arr = list(map(int, input().split()))
d = [0] + [-float('inf')] * (h - 1)
for i in range(n):
	d = [max(d[(j - arr[i]) % h], d[(j - arr[i] + 1) % h]) + int(l <= j <= r) for j in range(h)]
print(max(d))


import sys
input = sys.stdin.readline

def dfs(arr, pos, hour, h, l, r, dp):
	if dp[pos][hour] != -1:
		return dp[pos][hour]
	k = 0
	if pos < len(arr) - 1:
		k = max(dfs(arr, pos + 1, (arr[pos + 1] + hour) % h, h, l, r, dp), dfs(arr, pos + 1, (arr[pos + 1] + hour - 1) % h, h, l, r, dp))
	if hour >= l and hour <= r:
		dp[pos][hour] = k + 1
	else:
		dp[pos][hour] = k
	return dp[pos][hour]
(n, h, l, r) = map(int, input().split())
arr = [0]
arr.extend(list(map(int, input().split())))
dp = [[-1 for _ in range(2002)] for _ in range(2002)]
ans = dfs(arr, 0, 0, h, l, r, dp)
if l == 0:
	ans -= 1
print(ans)


from sys import stdin, stdout
from itertools import accumulate
(_input, _print) = (stdin.buffer.readline, stdout.write)
(_range, _int, _list, _len, _str, _max) = (range, int, list, len, str, max)
_tuple = tuple

def solution():
	(n, h, l, r) = [_int(i) for i in _input().split()]
	arr = _tuple([_int(i) for i in _input().split()])
	k = _list(accumulate(arr))
	w = [[0] * (n + 1) for i in _range(n)]
	if n == 1:
		if l <= k[0] <= r or l <= (k[0] + h - 1) % h <= r:
			print(1)
		else:
			print(0)
		return
	for i in _range(0, n):
		tmp = i - 1
		for j in _range(i + 2):
			if j == 0:
				w[i][j] = w[tmp][j]
			else:
				w[i][j] = _max(w[tmp][j], w[tmp][j - 1])
			if l <= (k[i] - j) % h <= r:
				w[i][j] += 1
	print(_max(w[-1]))
solution()


import collections

def solve(n, h, l, r, a):
	ans = 0
	maxAns = 0

	def go(i, accumulated, path, ans):
		if i >= n:
			return
		if l <= accumulated % h <= r:
			print('{} with mod {}: {}'.format(accumulated, h, accumulated % h))
			print('Path: ', end='')
			print(path)
			ans += 1
		if i == n:
			maxAns = max(ans, maxAns)
			return
		for j in range(-1, 1):
			go(i + 1, accumulated + a[i] - j, path + [a[i] + j], ans)

	def dp2(i, acc):
		if i >= n:
			return 0
		print('{} {}'.format(i, acc))
		goodTimes = 0
		if l <= acc % h <= r:
			goodTimes += 1
		goodTimes1 = dp2(i + 1, acc + a[i])
		goodTimes2 = dp2(i + 1, acc + a[i] - 1)
		return goodTimes + max(goodTimes1, goodTimes2)
	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))
	return max(dp[n])
[n, h, l, r] = list(map(int, input().strip().split()))
a = list(map(int, input().strip().split()))
print(solve(n, h, l, r, a))


def I():
	return list(map(int, input().split()))
(n, h, l, r) = I()
a = I()
dp = []
for i in range(n):
	dp.append([-1] * h)

def rec(i, j):
	c = 0
	if l <= j and j <= r and (i != 0):
		c = 1
	if i > n - 1:
		return c
	if dp[i][j] != -1:
		return dp[i][j]
	else:
		s = max(rec(i + 1, (j + a[i] - 1) % h), rec(i + 1, (j + a[i]) % h)) + c
		dp[i][j] = s
		return s
print(rec(0, 0))


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
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]))


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
(n, h, l, r) = list(map(int, input().split()))
arr = list(map(int, input().split()))
matrix = []
lis = [0, 0]
if arr[0] - 1 <= r and arr[0] - 1 >= l:
	lis[0] += 1
if arr[0] <= r and arr[0] >= l:
	lis[1] += 1
matrix.append(lis)
matrix1 = [[arr[0] - 1, arr[0]]]
for i in range(1, n):
	a = len(matrix[i - 1])
	temp = (matrix1[i - 1][0] + arr[i] - 1) % h
	lis = []
	num = []
	for j in range(a + 1):
		lis.append((temp + j) % h)
		if j == 0:
			if (temp + j) % h <= r and (temp + j) % h >= l:
				num.append(matrix[i - 1][0] + 1)
			else:
				num.append(matrix[i - 1][0])
		elif j == a:
			if (temp + j) % h <= r and (temp + j) % h >= l:
				num.append(matrix[i - 1][-1] + 1)
			else:
				num.append(matrix[i - 1][-1])
		else:
			b = max(matrix[i - 1][j - 1], matrix[i - 1][j])
			if (temp + j) % h <= r and (temp + j) % h >= l:
				num.append(b + 1)
			else:
				num.append(b)
	matrix.append(num)
	matrix1.append(lis)
print(max(matrix[-1]))


import sys
sys.setrecursionlimit(10 ** 5)

def cal(i, s):
	global n, h, l, r, a, dp
	if i >= n:
		return 0
	else:
		if dp[i][s] != -1:
			return dp[i][s]
		ans = 0
		if l <= (s + a[i]) % h and (s + a[i]) % h <= r:
			ans = max(ans, 1 + cal(i + 1, (s + a[i] + h) % h))
		if l <= (s + a[i] - 1) % h and (s + a[i] - 1) % h <= r:
			ans = max(ans, 1 + cal(i + 1, (s + a[i] + h - 1) % h))
		dp[i][s] = max(ans, cal(i + 1, (s + a[i] + h - 1) % h), cal(i + 1, (s + a[i] + h) % h))
		return dp[i][s]
(n, h, l, r) = [int(i) for i in input().split()]
dp = [[-1 for i in range(h + 3)] for j in range(n + 3)]
a = [int(i) for i in input().split()]
print(cal(0, 0))


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')
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)


import sys
import math
from functools import reduce
import bisect

def getN():
	return int(input())

def getNM():
	return map(int, input().split())

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

def input():
	return sys.stdin.readline().rstrip()

def index(a, x):
	i = bisect.bisect_left(a, x)
	if i != len(a) and a[i] == x:
		return i
	return False
(n, h, l, r) = getNM()
a = getList()
dp = [[-float('inf')] * (n + 1) for _ in range(n + 1)]
dp[0] = [0] * n
s = 0
for i in range(n):
	s += a[i]
	for j in range(i + 1):
		dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + int(l <= (s - j) % h <= r))
		if j < n:
			dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + int(l <= (s - j - 1) % h <= r))
print(max(dp[-1]))


(n, h, l, r) = map(int, input().split())
dp = [[-10 ** 9 for i in range(h + 1)] for j in range(n + 1)]
li = [int(x) for x in input().split()]
dp[0][0] = 0
mx = 0
for i in range(1, n + 1):
	for j in range(0, h + 1):
		c = 0
		new = (j + li[i - 1] - 1) % h
		if new >= l and new <= r:
			c = 1
		dp[i][new] = max(dp[i][new], dp[i - 1][j] + c)
		c = 0
		new = (j + li[i - 1]) % h
		if new >= l and new <= r:
			c = 1
		dp[i][new] = max(dp[i][new], dp[i - 1][j] + c)
print(max(dp[n]))


import sys
import heapq
from math import ceil, floor, gcd, fabs, factorial, fmod
from collections import defaultdict as dd, deque, Counter as c
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(str(var))

def l():
	return list(map(int, data().split()))

def sl():
	return list(map(str, data().split()))

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [[val for i in range(n)] for j in range(m)]

def recur(i, t):
	if i == n:
		return 0
	if dp[i][t] != -1:
		return dp[i][t]
	(t1, t2) = ((t + arr[i]) % h, (t + arr[i] - 1) % h)
	dp[i][t] = max(recur(i + 1, t1) + (left <= t1 <= right), recur(i + 1, t2) + (left <= t2 <= right))
	return dp[i][t]
(n, h, left, right) = sp()
arr = l()
dp = l2d(2001, 2001, -1)
out(recur(0, 0))


(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]))


(nSleeps, mod, lowGood, highGood) = [int(x) for x in input().split()]
delays = [int(x) for x in input().split()]

def nextSleep(nSleep, delay, mod, lowGood, highGood, soFar):
	out = []
	for i in range(mod):
		prev = (i + (mod - delay)) % mod
		a = soFar[prev]
		b = soFar[(prev + 1) % mod]
		plus = 0
		if i >= lowGood and i <= highGood:
			plus += 1
		out.append(max(a, b) + plus)
	return out
goods = [-2048 for _ in range(mod)]
goods[0] = 0
for (i, delay) in enumerate(delays):
	goods = nextSleep(i, delay, mod, lowGood, highGood, goods)
print(max(goods))


from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
from itertools import permutations, combinations, groupby
import functools
import sys
import bisect
import string
import math
import time
import random

def Golf():
	(*a,) = map(int, open(0))

def I():
	return int(input())

def S_():
	return input()

def IS():
	return input().split()

def LS():
	return [i for i in input().split()]

def LI():
	return [int(i) for i in input().split()]

def LI_():
	return [int(i) - 1 for i in input().split()]

def NI(n):
	return [int(input()) for i in range(n)]

def NI_(n):
	return [int(input()) - 1 for i in range(n)]

def StoLI():
	return [ord(i) - 97 for i in input()]

def ItoS(n):
	return chr(n + 97)

def LtoS(ls):
	return ''.join([chr(i + 97) for i in ls])

def GI(V, E, Directed=False, index=0):
	org_inp = []
	g = [[] for i in range(n)]
	for i in range(E):
		inp = LI()
		org_inp.append(inp)
		if index == 0:
			inp[0] -= 1
			inp[1] -= 1
		if len(inp) == 2:
			(a, b) = inp
			g[a].append(b)
			if not Directed:
				g[b].append(a)
		elif len(inp) == 3:
			(a, b, c) = inp
			aa = (inp[0], inp[2])
			bb = (inp[1], inp[2])
			g[a].append(bb)
			if not Directed:
				g[b].append(aa)
	return (g, org_inp)

def GGI(h, w, search=None, replacement_of_found='.', mp_def={'#': 1, '.': 0}):
	mp = [1] * (w + 2)
	found = {}
	for i in range(h):
		s = input()
		for char in search:
			if char in s:
				found[char] = (i + 1) * (w + 2) + s.index(char) + 1
				mp_def[char] = mp_def[replacement_of_found]
		mp += [1] + [mp_def[j] for j in s] + [1]
	mp += [1] * (w + 2)
	return (h + 2, w + 2, mp, found)

def TI(n):
	return GI(n, n - 1)

def bit_combination(k, n=2):
	rt = []
	for tb in range(n ** k):
		s = [tb // n ** bt % n for bt in range(k)]
		rt += [s]
	return rt

def show(*inp, end='\n'):
	if show_flg:
		print(*inp, end=end)
YN = ['YES', 'NO']
Yn = ['Yes', 'No']
mo = 10 ** 9 + 7
inf = float('inf')
l_alp = string.ascii_lowercase
input = lambda : sys.stdin.readline().rstrip()

def ran_input():
	n = random.randint(4, 16)
	(rmin, rmax) = (1, 10)
	a = [random.randint(rmin, rmax) for _ in range(n)]
	return (n, a)
show_flg = False
show_flg = True
for _ in range(1):
	(n, h, l, r) = LI()
	a = LI()
	ans = 0
	dp = [[None] * h for i in range(n + 1)]
	for i in range(n):
		a[i]
	dp[0][0] = 0
	for i in range(n):
		for t in range(h):
			if dp[i][t] == None:
				continue
			nb = (t + a[i]) % h
			if dp[i + 1][nb] == None:
				dp[i + 1][nb] = dp[i][t] + (1 if l <= nb <= r else 0)
			else:
				dp[i + 1][nb] = max(dp[i + 1][nb], dp[i][t] + (1 if l <= nb <= r else 0))
			nb = (t + a[i] - 1) % h
			if dp[i + 1][nb] == None:
				dp[i + 1][nb] = dp[i][t] + (1 if l <= nb <= r else 0)
			else:
				dp[i + 1][nb] = max(dp[i + 1][nb], dp[i][t] + (1 if l <= nb <= r else 0))
	y = [0 if i == None else i for i in dp[n]]
	ans = max(y)
	print(ans)


(n, h, l, r) = map(int, input().split())
ls = list(map(int, input().split()))
(dp, s) = ([[0 for j in range(n + 1)] for i in range(n + 1)], 0)
for i in range(n):
	s += ls[i]
	for j in range(i + 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(max(dp)))


import sys
input = sys.stdin.readline
(n, h, l, r) = map(int, input().split())
a = [int(item) for item in input().split()]
dp = [[-1] * h for _ in range(n + 1)]
dp[0][0] = 0
for (i, item) in enumerate(a):
	for j in range(h):
		if dp[i][j] == -1:
			continue
		nt = (j + item) % h
		if l <= nt <= r:
			dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1)
		else:
			dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j])
		nt = (j + item - 1 + h) % h
		if l <= nt <= r:
			dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j] + 1)
		else:
			dp[i + 1][nt] = max(dp[i + 1][nt], dp[i][j])
print(max(dp[-1]))


from sys import stdin, gettrace
if not gettrace():

	def input():
		return next(stdin)[:-1]

def main():
	(n, h, l, r) = map(int, input().split())
	aa = [int(a) for a in input().split()]
	counts = [0] * h
	values = {0}
	for a in aa:
		nv = set()
		nc = [0] * h
		for v in values:
			mx = (v + a) % h
			nv.add(mx)
			nc[mx] = max(nc[mx], counts[v] + (1 if l <= mx and r >= mx else 0))
			mn = (v + a - 1) % h
			nv.add(mn)
			nc[mn] = max(nc[mn], counts[v] + (1 if l <= mn and r >= mn else 0))
		values = nv
		counts = nc
	print(max(counts))
main()


import sys
input = sys.stdin.readline
(n, h, l, r) = map(int, input().split())
x = [*map(int, input().split())]
from collections import defaultdict as dd
hour = dd(int)
hour[0] = 0
for xi in x:
	new_hour = dd(int)
	for (i, j) in hour.items():
		a = (i + xi) % h
		new_hour[a] = max(new_hour[a], j + (l <= a <= r))
		a = (a - 1) % h
		new_hour[a] = max(new_hour[a], j + (l <= a <= r))
	hour = new_hour
print(max(hour.values()))


import sys
(n, h, l, r) = map(int, input().split())
a = list(map(int, input().split()))
dp = [[0] * h for _ in range(n + 1)]
for i in range(n - 1, -1, -1):
	for k in range(h):
		t1 = (a[i] + k) % h
		t2 = (a[i] + k - 1) % h
		dp[i][k] = max(int(l <= t1 <= r) + dp[i + 1][t1], int(l <= t2 <= r) + dp[i + 1][t2])
print(dp[0][0])


import sys
input = sys.stdin.readline
out = sys.stdout
(n, h, l, r) = map(int, input().split())
a = [0] + [int(x) for x in input().split()]
mat = [[] for i in range(n + 1)]
mat[0] = [0]
t = 0
for i in range(1, n + 1):
	t += a[i]
	for j in range(0, len(mat[i - 1]) + 1):
		p = (t - j) % h
		q = 1 if p <= r and p >= l else 0
		(x, y) = (0, 0)
		if j > 0:
			x = mat[i - 1][j - 1]
		if j < len(mat[i - 1]):
			y = mat[i - 1][j]
		mat[i].append(max(x, y) + q)
out.write(str(max(mat[n])) + '\n')


(n, h, l, r) = map(int, input().split())
a = list(map(int, input().split()))
cur = [-3000] * h
cur[0] = 0
for x in a:
	new = []
	for i in range(h):
		f = max(cur[(i - x) % h], cur[(i - x + 1) % h])
		if l <= i <= r:
			f += 1
		new += [f]
	cur = new
print(max(cur))


def inr(n, l, r):
	if l <= n and n <= r:
		return 1
	else:
		return 0
(n, h, l, r) = map(int, input().split())
ai = [*map(int, input().split())]
dp = [[-2001 for j in range(n + 1)] for i in range(n + 1)]
dp[0][0] = 0
s = 0
for i in range(n):
	s += ai[i]
	for j in range(n + 1):
		dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + inr(abs(s - j) % h, l, r))
		if j < n:
			dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + inr(abs(s - j - 1) % h, l, r))
dp[n].sort()
print(dp[n][n])


from sys import stdin
input = stdin.readline
(n, h, l, r) = map(int, input().split())
a = list(map(int, input().split()))

def isGood(t):
	if l <= r:
		if t >= l and t <= r:
			return True
	elif t <= r or t >= l:
		return True
	return False

def printRow(q):
	for row in q:
		print(str(row))
	print()
q = [[-1 for i in range(n + 1)] for j in range(n)]
q[0][0] = a[0]
for row in range(1, n):
	q[row][0] = (a[row] + q[row - 1][0]) % h
for col in range(1, n + 1):
	for row in range(col - 1, n):
		q[row][col] = (q[row][col - 1] - 1) % h
for row in range(n):
	for col in range(n + 1):
		if isGood(q[row][col]):
			q[row][col] = 1
		else:
			q[row][col] = 0
for row in range(1, n):
	q[row][0] += q[row - 1][0]
for col in range(1, n + 1):
	for row in range(1, n):
		q[row][col] += max(q[row - 1][col], q[row - 1][col - 1])
print(max(q[n - 1]))


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'

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'))

	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)
	answ = max((dp[i][n] for i in range(h)))
	return answ
test_count = 1
for _ in range(test_count):
	input_data = read()
	result = solve(*input_data)
	print(result)


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
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]))


def printit(matrix):
	for i in matrix:
		for j in i:
			print(j, end=' ')
		print()
(n, h, start, end) = map(int, input().split())
l = list(map(int, input().split()))
dp = [[-1 for i in range(h)] for j in range(n)]
if l[0] >= start and l[0] <= end:
	dp[0][l[0]] = 1
else:
	dp[0][l[0]] = 0
if l[0] - 1 >= start and l[0] - 1 <= end:
	dp[0][l[0] - 1] = 1
else:
	dp[0][l[0] - 1] = 0
for i in range(1, n):
	for j in range(h):
		time1 = j + h - l[i]
		time2 = j + h - l[i] + 1
		if time1 >= h:
			time1 -= h
		if time2 >= h:
			time2 -= h
		dp[i][j] = max(dp[i - 1][time1], dp[i - 1][time2])
		if dp[i - 1][time1] != -1 or dp[i - 1][time2] != -1:
			if j >= start and j <= end:
				dp[i][j] += 1
ans = 0
for i in range(start, end + 1):
	for j in range(n):
		if dp[j][i] > ans:
			ans = dp[j][i]
print(ans)


from sys import stdin
from collections import defaultdict
(n, h, l, r) = map(int, stdin.readline().split())
sleep = list(map(int, stdin.readline().split()))
dp = defaultdict(lambda : [float('-inf')] * h)
dp[0][0] = 0
for i in range(n):
	for j in range(h):
		a = sleep[i]
		t = (a + j) % h
		t_ = (a + j - 1) % h
		dp[i + 1][t] = max(dp[i + 1][t], dp[i][j] + (l <= t <= r))
		dp[i + 1][t_] = max(dp[i + 1][t_], dp[i][j] + (l <= t_ <= r))
m = 0
for j in range(h):
	if dp[n][j] > m:
		m = dp[n][j]
print(m)


import sys
import math

def alele():
	return list(map(int, sys.stdin.readline().strip().split()))

def ilele():
	return map(int, sys.stdin.readline().strip().split())

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 Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
import bisect
from collections import defaultdict
INF = 10 ** 18
MOD = 10 ** 9 + 7
(n, h, l, r) = ilele()
A = alele()
count = 0
dp = list2d(n + 1, h + 1, 0)
for i in range(n - 1, -1, -1):
	for t in range(h):
		cost1 = 0
		cost2 = 0
		t1 = (t + A[i]) % h
		t2 = (t + A[i] - 1) % h
		if l <= t1 <= r:
			cost1 += 1
		if l <= t2 <= r:
			cost2 += 1
		dp[i][t] = max(cost1 + dp[i + 1][t1], cost2 + dp[i + 1][t2])
print(dp[0][0])


(n, h, l, r) = (int(x) for x in input().split())
seq = [int(x) for x in input().split()]
dp = [[float('-inf') for j in range(h)] for i in range(n + 1)]
dp[0][0] = 0
for i in range(n):
	for j in range(h):
		bonus = 0
		if l <= j <= r:
			bonus += 1
		dp[i + 1][j] = max(dp[i][(j - seq[i] + h) % h], dp[i][(j - seq[i] + h + 1) % h]) + bonus
print(max(dp[-1]))


import sys
input = sys.stdin.readline
(n, h, l, r) = map(int, input().split())
a = list(map(int, input().split()))
dp0 = [-1] * h
dp0[0] = 0
for i in a:
	dp = [-1] * h
	for j in range(h):
		dp0j = dp0[j]
		if dp0j >= 0:
			k = (i + j - 1) % h
			if l <= k <= r:
				dp[k] = max(dp0j + 1, dp[k])
			else:
				dp[k] = max(dp0j, dp[k])
			k += 1
			k %= h
			if l <= k <= r:
				dp[k] = max(dp0j + 1, dp[k])
			else:
				dp[k] = max(dp0j, dp[k])
	dp0 = dp
ans = max(dp)
print(ans)


(n, h, l, r) = [int(x) for x in input().split()]
sl = [int(x) for x in input().split()]
dp = set([0])
dpc = [0] * h
dpcn = [0] * h
dpn = set()
nowh = 0
for s in sl:
	dpn.clear()
	dpcn = [0] * h
	for hok in dp:
		dpn.add((hok + s - 1) % h)
		dpn.add((hok + s) % h)
		if (hok + s - 1) % h >= l and (hok + s - 1) % h <= r:
			dpcn[(hok + s - 1) % h] = max(dpc[hok] + 1, dpcn[(hok + s - 1) % h])
		else:
			dpcn[(hok + s - 1) % h] = max(dpc[hok], dpcn[(hok + s - 1) % h])
		if (hok + s) % h >= l and (hok + s) % h <= r:
			dpcn[(hok + s) % h] = max(dpc[hok] + 1, dpcn[(hok + s) % h])
		else:
			dpcn[(hok + s) % h] = max(dpc[hok], dpcn[(hok + s) % h])
	dp.clear()
	dp.update(dpn)
	dpc = dpcn[:]
print(max(dpcn))


def S(T, i, arr, h, l, r, dp):
	if i == len(arr):
		return 1 if T <= r and l <= T else 0
	T1 = (T + arr[i]) % h
	T2 = (T + arr[i] - 1) % h
	ans1 = dp[T1][i] if dp[T1][i] != -1 else S(T1, i + 1, arr, h, l, r, dp)
	ans2 = dp[T2][i] if dp[T2][i] != -1 else S(T2, i + 1, arr, h, l, r, dp)
	dp[T1][i] = ans1
	dp[T2][i] = ans2
	count = 1 if l <= T and r >= T else 0
	return count + max(ans1, ans2)
(n, h, l, r) = [int(ele) for ele in input().split()]
arr = [int(ele) for ele in input().split()]
dp = [[-1 for i in range(n)] for i in range(0, h + 1)]
ans = S(0, 0, arr, h, l, r, dp)
if l == 0:
	ans -= 1
print(ans)


import sys, os
from math import sqrt, gcd, ceil, log, floor
from bisect import bisect, bisect_left
from collections import defaultdict, Counter, deque
from heapq import heapify, heappush, heappop
input = sys.stdin.readline
read = lambda : list(map(int, input().strip().split()))
MOD = 10 ** 9 + 7

def main():
	(n, h, l, r) = read()
	arr = read()
	dp = [[-1] * (h + 1) for i in range(n)]
	dp[0][(arr[0] - 1) % h] = 1 if l % h <= arr[0] - 1 <= r % h else 0
	dp[0][arr[0] % h] = 1 if l % h <= arr[0] <= r % h else 0
	for i in range(1, n):
		for j in range(h + 1):
			if dp[i - 1][j] > -1:
				if l <= (j + arr[i]) % h <= r:
					dp[i][(j + arr[i]) % h] = max(dp[i][(j + arr[i]) % h], dp[i - 1][j] + 1)
				else:
					dp[i][(j + arr[i]) % h] = max(dp[i][(j + arr[i]) % h], dp[i - 1][j])
				if l <= (j + arr[i] - 1) % h <= r:
					dp[i][(j + arr[i] - 1) % h] = max(dp[i][(j + arr[i] - 1) % h], dp[i - 1][j] + 1)
				else:
					dp[i][(j + arr[i] - 1) % h] = max(dp[i][(j + arr[i] - 1) % h], dp[i - 1][j])
	print(max(dp[n - 1]))
main()


def bool_fun(s, h):
	if l <= s % h <= r:
		return 1
	else:
		return 0
z = float('-inf')
(n, h, l, r) = map(int, input().split())
l1 = list(map(int, input().split()))
s = [0 for i in range(n + 1)]

def bool_fun(s, h, l, r):
	if l <= s % h <= r:
		return 1
	else:
		return 0
for i in range(1, n + 1):
	s[i] = s[i - 1] + l1[i - 1]
dp = [[float('-inf') for i in range(n + 1)] for j in range(n + 1)]
dp[0][0] = 0
for i in range(n):
	for j in range(i + 1):
		dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + bool_fun(s[i + 1] - j, h, l, r))
		dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + bool_fun(s[i + 1] - j - 1, h, l, r))
print(max(dp[n]))


import math
import collections
import functools
import sys
import os

def solve():
	(n, h, l, r) = list(map(int, input().split()))
	table = list(map(int, input().split()))
	dp = [[-10 ** 9] * h for _ in range(n + 1)]
	dp[0][0] = 0
	res = 0
	for i in range(1, n + 1):
		item = table[i - 1]
		for j in range(0, h):
			dp[i][j] = max(dp[i - 1][(j - item + h) % h], dp[i - 1][(j - item + 1 + h) % h])
			if j >= l and j <= r:
				dp[i][j] += 1
			res = max(res, dp[i][j])
	print(res)

def main():
	T = 1
	while T:
		solve()
		T -= 1
main()


(n, h, l, r) = map(int, input().split())
inp = input()
state = [[-1 for _ in range(h)] for _ in range(n + 1)]

def step(prev, dur):
	for (idx, s) in enumerate(state[prev]):
		if s == -1:
			continue
		for d in [dur, dur - 1]:
			jdx = (idx + d) % h
			ss = s + 1 if l <= jdx <= r else s
			state[prev + 1][jdx] = max(state[prev + 1][jdx], ss)
state[0][0] = 0
for (idx, a) in enumerate(map(int, inp.split())):
	step(idx, a)
print(max(state[-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 + 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)


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


(n, h, l, r) = map(int, input().split())
a = list(map(int, input().split()))
dp = [[0] * h for _ in range(n + 1)]
dp[0][0] = 1
for i in range(0, n):
	for j in range(0, h):
		cur = dp[i][j]
		if cur:
			(nx1, nx2) = ((j + a[i] - 1) % h, (j + a[i]) % h)
			val1 = cur + (1 if l <= nx1 <= r else 0)
			val2 = cur + (1 if l <= nx2 <= r else 0)
			dp[i + 1][nx1] = max(dp[i + 1][nx1], val1)
			dp[i + 1][nx2] = max(dp[i + 1][nx2], val2)
print(max(dp[n]) - 1)


import os, sys, bisect, copy
from collections import defaultdict, Counter, deque
from functools import lru_cache
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())
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 sys
input = sys.stdin.readline

def inp():
	return int(input())

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

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def bin_search(arr, l, r, val):
	if arr[r] <= val:
		return r + 1
	if r - l < 2:
		if arr[l] > val:
			return l
		else:
			return r
	mid = int((l + r) / 2)
	if arr[mid] <= val:
		return bin_search(arr, mid, r, val)
	else:
		return bin_search(arr, l, mid, val)

def pr_list(a):
	print(*a, sep=' ')

def main():
	[n, h, l, r] = inlt()
	a = inlt()
	dp = [[-1 for i in range(h)] for j in range(n + 1)]
	dp[0][0] = 0
	for i in range(n):
		for j in range(h):
			if dp[i][j] != -1:
				dp[i + 1][(j + a[i]) % h] = max(dp[i + 1][(j + a[i]) % h], dp[i][j] + ((j + a[i]) % h >= l and (j + a[i]) % h <= r))
				dp[i + 1][(j + a[i] - 1) % h] = max(dp[i + 1][(j + a[i] - 1) % h], dp[i][j] + ((j + a[i] - 1) % h >= l and (j + a[i] - 1) % h <= r))
	ans = 0
	for i in range(h):
		ans = max(ans, dp[n][i])
	print(ans)
	return 0
main()


(n, h, l, r) = map(int, input().split())
ai = [int(i) for i in input().split()]
dp = [[0] * h for _ in range(n + 1)]
for i in reversed(range(n)):
	for t in range(h):
		a = (t + ai[i]) % h
		b = (a - 1) % h
		dp[i][t] = max(dp[i + 1][a] + (l <= a <= r), dp[i + 1][b] + (l <= b <= r))
print(dp[0][0])


(n, h, l, r) = map(int, input().split())
arr = list(map(int, input().split()))
pre = []
for i in range(n):
	pre.append(arr[i])
	if len(pre) > 1:
		pre[-1] += pre[-2]
dp = [[-1000000000.0 for i in range(n + 1)] for j in range(n + 1)]
dp[0][0] = 0
for i in range(1, n + 1):
	for j in range(i + 1):
		dp[i][j] = dp[i - 1][j]
		if j > 0:
			dp[i][j] = max(dp[i][j], dp[i - 1][j - 1])
		if l <= (pre[i - 1] - j) % h <= r:
			dp[i][j] += 1
print(max(dp[-1]))


import sys
input = sys.stdin.readline
(n, h, l, r) = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
memo = []
for j in range(h):
	temp = []
	if j >= l and j <= r:
		for fuk in range(n):
			temp.append(1)
	else:
		for fuk in range(n):
			temp.append(0)
	memo.append(temp)
for it in range(n - 1, 0, -1):
	for j in range(h):
		inc1 = 0
		time1 = (j - a[it]) % h
		time2 = (j - a[it] + 1) % h
		inc2 = 0
		if time1 >= l and time1 <= r:
			inc1 = 1
		if time2 >= l and time2 <= r:
			inc2 = 1
		memo[time1][it - 1] = max(memo[time1][it - 1], memo[j][it] + inc1)
		memo[time2][it - 1] = max(memo[time2][it - 1], memo[j][it] + inc2)
print(max(memo[a[0]][0], memo[a[0] - 1][0]))


from math import inf

def within(i, L, R, H):
	if L <= i % H and i % H <= R:
		return 1
	else:
		return 0
(N, H, L, R) = list(map(int, input().split()))
aarray = list(map(int, input().split()))
cumu = [aarray[0]]
for i in range(1, N):
	cumu.append(aarray[i] + cumu[-1])
dp = [[-inf for i in range(N + 1)] for i in range(N + 1)]
dp[0][0] = 0
for n in range(N):
	for e in range(n + 1):
		dp[n + 1][e] = max(dp[n + 1][e], dp[n][e] + within(cumu[n] - e, L, R, H))
		dp[n + 1][e + 1] = max(dp[n + 1][e + 1], dp[n][e] + within(cumu[n] - e - 1, L, R, H))
print(max(dp[N]))


from sys import stdin
input = lambda : stdin.readline()
from math import ceil, sqrt, gcd
(n, h, l, r) = map(int, input().split())
z = list(map(int, input().split()))
dp = [[0 for i in range(n + 1)] for i in range(n + 1)]
s = 0
for i in range(n):
	s += z[i]
	for j in range(i + 2):
		if l <= (s - j) % h <= r:
			dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + 1)
			if j != n:
				dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1)
		else:
			dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
			if j != n:
				dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j])
print(max(dp[n]))


from pprint import pprint
(n, h, l, r) = map(int, input().split())
data = list(map(int, input().split()))
t1 = [-1] * h
t1[0] = 0
for i in range(n):
	x = data[i]
	t2 = [-1] * h
	for j in range(h):
		if t1[j] == -1:
			continue
		t2[(j + x) % h] = max(t1[j], t2[(j + x) % h])
		t2[(j + x - 1) % h] = max(t1[j], t2[(j + x - 1) % h])
	x = 0
	for j in range(h):
		if t2[j] == -1:
			continue
		if l <= (j + x) % h <= r:
			t2[(j + x) % h] += 1
	t1 = t2
print(max(t1))


import io
import os
import sys
from collections import Counter, defaultdict, deque

def solve(N, H, L, R, A):
	dp = {}
	for i in range(N):
		nextDp = {}
		for h in range(H):
			isGood = int(L <= h <= R)
			h1 = (h - (A[i] - 1)) % H
			h2 = (h - A[i]) % H
			if i == 0:
				if h1 != 0 and h2 != 0:
					best = float('-inf')
				else:
					best = isGood
			else:
				best = isGood + max(dp[h1], dp[h2])
			nextDp[h] = best
		dp = nextDp
	return max((dp[h] for h in range(H)))
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
(N, H, L, R) = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
ans = solve(N, H, L, R, A)
print(ans)


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


from math import *
(n, h, l, r) = map(int, input().split())
a = [int(i) for i in input().split()]
dp = []
for i in range(n):
	dp.append([0] * (n + 1))
s = sum(a)
for i in range(n - 1, -1, -1):
	for j in range(i + 2):
		if r >= (s - j) % h >= l:
			dp[i][j] += 1
		if i < n - 1:
			dp[i][j] += max(dp[i + 1][j], dp[i + 1][j + 1])
	s -= a[i]
print(max(dp[0][0], dp[0][1]))


(n, h, l, r) = list(map(int, input().split()))
a = list(map(int, input().split()))
dp = [[-float('INF')] * (n + 1) for _ in range(n + 1)]
dp[0][0] = 0

def fit(i):
	ret = 0
	if i >= l and i <= r:
		ret = 1
	return ret
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] + fit((s - j) % h))
		if j < n:
			dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + fit((s - j - 1) % h))
print(max(dp[n]))


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')
(n, h, l, r) = map(int, input().split())
a = list(map(int, input().split()))
dp = [[-99999999999] * h for i in range(n + 1)]
dp[0][0] = 0
for i in range(n):
	for j in range(h):
		plusone = 0
		p2 = 0
		if l <= (a[i] + j - 1) % h <= r:
			plusone = 1
		if l <= (a[i] + j) % h <= r:
			p2 = 1
		dp[i + 1][(a[i] + j - 1) % h] = max(dp[i + 1][(a[i] + j - 1) % h], dp[i][j] + plusone)
		dp[i + 1][(a[i] + j) % h] = max(dp[i + 1][(a[i] + j) % h], dp[i][j] + p2)
ans = -999999999999
for j in range(h):
	ans = max(ans, dp[n][j])
print(ans)


import math
from bisect import bisect_left, bisect_right
from sys import stdin, stdout
from collections import Counter
input = lambda : stdin.readline().strip()
print = stdout.write
(n, h, l, r) = map(int, input().split())
ls = list(map(int, input().split()))
dp = []
for i in range(n):
	dp.append([])
	for j in range(h):
		dp[-1].append(0)
for j in range(h):
	if l <= (j + ls[-1]) % h <= r or l <= (j + ls[-1] - 1) % h <= r:
		dp[0][j] += 1
for i in range(1, n):
	for j in range(h):
		f = dp[i - 1][(j + ls[-(1 + i)]) % h] + int(l <= (j + ls[-(1 + i)]) % h <= r)
		s = dp[i - 1][(j + ls[-(1 + i)] - 1) % h] + int(l <= (j + ls[-(1 + i)] - 1) % h <= r)
		dp[i][j] = max(f, s)
print(str(dp[n - 1][0]) + '\n')


(n, h, l, r) = list(map(int, input().split()))
a = list(map(int, input().split()))
m = [[-1] * h for i in range(n + 1)]
m[0][0] = 0
for i in range(len(a)):
	for hi in range(h):
		if m[i][hi] < 0:
			continue
		m[i + 1][(hi + a[i]) % h] = max(m[i + 1][(hi + a[i]) % h], m[i][hi] + int(l <= (hi + a[i]) % h <= r))
		m[i + 1][(hi + a[i] - 1) % h] = max(m[i + 1][(hi + a[i] - 1) % h], m[i][hi] + int(l <= (hi + a[i] - 1) % h <= r))
print(max(m[-1]))


from sys import stdin
from collections import *

def arr_inp(n):
	if n == 1:
		return [int(x) for x in stdin.readline().split()]
	elif n == 2:
		return [float(x) for x in stdin.readline().split()]
	else:
		return list(stdin.readline()[:-1])

def dp(i, val):
	if i == n:
		return 0
	if mem[i][val] != -1:
		return mem[i][val]
	(v1, v2) = ((a[i] + val) % h, (a[i] - 1 + val) % h)
	mem[i][val] = max((v1 >= l and v1 <= r) + dp(i + 1, v1), (v2 >= l and v2 <= r) + dp(i + 1, v2))
	return mem[i][val]
(n, h, l, r) = arr_inp(1)
(a, ans) = (arr_inp(1), 0)
mem = [[-1 for i in range(h + 1)] for j in range(n + 2)]
dp(0, 0)
print(mem[0][0])


(n, h, l, r) = (int(x) for x in input().split())
A = [int(x) for x in input().split()]
visited = [[0 for i in range(h)] for i in range(n + 1)]
visited[0][0] = 1
for i in range(1, n + 1):
	for j in range(h):
		if visited[i - 1][(j - A[i - 1]) % h] == 1 or visited[i - 1][(j - A[i - 1] + 1) % h] == 1:
			visited[i][j] = 1
DPtable = [[0 for i in range(h)] for i in range(n + 1)]
for i in range(1, n + 1):
	for j in range(h):
		if j >= l and j <= r:
			good = 1
		else:
			good = 0
		if visited[i][j] == 1:
			DPtable[i][j] = max(DPtable[i - 1][(j - A[i - 1]) % h], DPtable[i - 1][(j - A[i - 1] + 1) % h]) + good
print(max(DPtable[-1]))


def flag(x, l, r):
	return 1 if l <= x and x <= r else 0
I = lambda : list(map(int, input().split()))
(n, h, l, r) = I()
ls = I()
dp = [[-1000000000.0 for j in range(n + 1)] for i in range(n + 1)]
dp[0][0] = 0
s = 0
for i in range(n):
	s += ls[i]
	for j in range(n + 1):
		dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + flag((s - j) % h, l, r))
		if j < n:
			dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + flag((s - j - 1) % h, l, r))
print(max(dp[n]))


import sys

def good(t):
	if l <= t <= r:
		return 1
	return 0
(n, h, l, r) = list(map(int, sys.stdin.readline().rstrip().split()))
arr = list(map(int, sys.stdin.readline().rstrip().split()))
dp = [[0] * h for _ in range(n + 1)]
for i in range(n - 1, -1, -1):
	for j in range(h):
		t1 = (j + arr[i]) % h
		t2 = (j + arr[i] - 1) % h
		dp[i][j] = max(good(t1) + dp[i + 1][t1], good(t2) + dp[i + 1][t2])
print(dp[0][0])


(n, h, l, r) = map(int, input().split())
dp = [[0] * h for _ in range(n + 1)]
possible = [set() for i in range(n + 1)]
possible[0].add(0)
for (i, length) in zip(range(1, n + 1), map(int, input().split())):
	for time in possible[i - 1]:
		trans0 = (time + length) % h
		trans1 = (h + time + length - 1) % h
		dp[i][trans0] = max(dp[i][trans0], dp[i - 1][time])
		dp[i][trans1] = max(dp[i][trans1], dp[i - 1][time])
		possible[i].add(trans0)
		possible[i].add(trans1)
	for time in possible[i]:
		if l <= time <= r:
			dp[i][time] += 1
print(max(dp[n]))


import bisect, collections

def solution():
	(n, h, l, r) = [int(x) for x in input().strip().split(' ')]
	a = [int(x) for x in input().strip().split(' ')]
	d = {0: 0}
	for x in a:
		d2 = collections.defaultdict(int)
		for (k, v) in d.items():
			d2[(k + x) % h] = max(d2[(k + x) % h], v + (1 if l <= (k + x) % h <= r else 0))
			d2[(k + x - 1) % h] = max(d2[(k + x - 1) % h], v + (1 if l <= (k + x - 1) % h <= r else 0))
		d = d2
	print(max(d.values()))

def main():
	for _ in range(1):
		solution()
main()


(n, h, l, r) = list(map(int, input().split()))
m = list(map(int, input().split()))
s = set()
s.add(0)
p = [0] * h
for i in range(n):
	v = [0] * h
	t = set()
	for j in s:
		k = (j + m[i] - 1) % h
		t.add(k)
		if k >= l and k <= r:
			v[k] = max(v[k], p[j] + 1)
		else:
			v[k] = max(v[k], p[j])
		k = (j + m[i]) % h
		t.add(k)
		if k >= l and k <= r:
			v[k] = max(v[k], p[j] + 1)
		else:
			v[k] = max(v[k], p[j])
	p = v[:]
	s = t
print(max(v))


import sys
input = sys.stdin.readline
(n, h, l, r) = list(map(int, input().split()))
a = list(map(int, input().split()))
dp = [[-1 for _ in range(h)] for _ in range(n + 1)]
dp[0][0] = 0
for day in range(n):
	for hour in range(h):
		if dp[day][hour] == -1:
			continue
		nex = (hour + a[day]) % h
		if l <= nex <= r:
			increment = 1
		else:
			increment = 0
		dp[day + 1][nex] = max(dp[day + 1][nex], dp[day][hour] + increment)
		nex = (hour + a[day] - 1) % h
		if l <= nex <= r:
			increment = 1
		else:
			increment = 0
		dp[day + 1][nex] = max(dp[day + 1][nex], dp[day][hour] + increment)
print(max(dp[-1][:]))


(n, h, l, r) = map(int, input().rstrip().split())
a = list(map(int, input().rstrip().split()))
s = dict()
s[0] = 0
for i in range(n):
	newS = dict()
	for key in s:
		occ = s[key]
		newH = (key + a[i] + h) % h
		if newH >= l and newH <= r:
			newS[newH] = max(newS.get(newH, 0), occ + 1)
		else:
			newS[newH] = max(newS.get(newH, 0), occ)
		newH = (key + a[i] + h - 1) % h
		if newH >= l and newH <= r:
			newS[newH] = max(newS.get(newH, 0), occ + 1)
		else:
			newS[newH] = max(newS.get(newH, 0), occ)
	s = newS
maxi = 0
for k in s:
	maxi = max(maxi, s[k])
print(maxi)


(n, h, l, r) = map(int, input().split())
a = list(map(int, input().split()))
b = [a[0]]
for i in a[1:]:
	b.append(b[-1] + i)
dp = [(n + 1) * [0] for _ in range(n)]
if l <= a[0] <= r:
	dp[0][0] = 1
if l <= a[0] - 1 <= r:
	dp[0][1] = 1
for i in range(1, n):
	for j in range(i + 2):
		if j == 0:
			dp[i][j] = dp[i - 1][j]
			if l <= b[i] % h <= r:
				dp[i][j] += 1
		m = (b[i] - j) % h
		if l <= m <= r:
			f = 1
		else:
			f = 0
		dp[i][j] = max(dp[i - 1][j - 1], dp[i - 1][j]) + f
print(max(dp[n - 1]))


from collections import defaultdict as dd
(n, h, l, r) = map(int, input().split())
A = list(map(int, input().split()))
curr = {0: 0}
for i in range(n):
	new = dd(int)
	for (time, good) in curr.items():
		for new_time in [(time + A[i]) % h, (time + A[i] - 1) % h]:
			if l <= new_time <= r:
				new_good = good + 1
			else:
				new_good = good
			new[new_time] = max(new[new_time], new_good)
	curr = new
print(max(curr.values()))


import sys
input = sys.stdin.readline
(n, h, l, r) = list(map(int, input().split()))
A = list(map(int, input().split()))
DP = [-1] * h
DP[0] = 0
for a in A:
	NDP = [-1] * h
	for k in range(h):
		if DP[k] == -1:
			continue
		if l <= (a + k) % h <= r:
			NDP[(a + k) % h] = max(NDP[(a + k) % h], DP[k] + 1)
		else:
			NDP[(a + k) % h] = max(NDP[(a + k) % h], DP[k])
		if l <= (a + k - 1) % h <= r:
			NDP[(a + k - 1) % h] = max(NDP[(a + k - 1) % h], DP[k] + 1)
		else:
			NDP[(a + k - 1) % h] = max(NDP[(a + k - 1) % h], DP[k])
	DP = NDP
print(max(DP))


def isGood(hour):
	global l, r
	if hour >= l and hour <= r:
		return 1
	else:
		return 0
debug = False
(n, h, l, r) = map(int, input().split())
hours = list(map(int, input().split()))
dp = [[float('-inf')] * (n + 1) for i in range(n + 1)]
dp[0][0] = 0
sum = 0
for i in range(n):
	sum += hours[i]
	for j in range(n + 1):
		A = dp[i][j] + isGood((sum - j) % h)
		dp[i + 1][j] = max(dp[i + 1][j], A)
		B = dp[i][j] + isGood((sum - j - 1) % h)
		if j < n:
			dp[i + 1][j + 1] = max(dp[i + 1][j + 1], B)
print(max(dp[n]))


from bisect import bisect_left, bisect_right
from typing import List
import collections
import bisect
import itertools
import functools
from fractions import gcd
import heapq
from math import ceil, sqrt, floor
import sys
sys.setrecursionlimit(50000)
import collections
from collections import defaultdict, deque
import collections
(n, h, l, r) = list(map(int, input().split()))
A = list(map(int, input().split()))
t = 0
dp = [[0] * (n + 2) for _ in range(n + 1)]
for i in range(1, n + 1):
	t += A[i - 1]
	for j in range(1, i + 2):
		is_good_sleep = l <= (t - (j - 1)) % h <= r
		dp[i][j] = max(dp[i - 1][j - 1], dp[i - 1][j]) + (1 if is_good_sleep else 0)
print(max(dp[n]))


from bisect import bisect_left, bisect_right
for _ in range(1):
	(n, h, l, r) = map(int, input().split())
	arr = list(map(int, input().split()))
	dp_old = [-1] * h
	dp_old[0] = 0
	for i in range(n):
		dp_new = [-1] * h
		for j in range(h):
			if dp_old[j] != -1:
				for d in [0, 1]:
					dp_new[(j + arr[i] - d) % h] = max(dp_new[(j + arr[i] - d) % h], dp_old[j] + (1 if l <= (j + arr[i] - d) % h <= r else 0))
		dp_old = dp_new
	print(max(dp_old))


import os, sys
from io import BytesIO, IOBase
from math import inf

def main():
	(n, h, l, r) = map(int, input().split())
	dp = [[-inf] * h for _ in range(n + 1)]
	dp[0][0] = 0
	a = list(map(int, input().split()))
	for i in range(1, n + 1):
		for x in range(h):
			jj = (x + a[i - 1]) % h
			jj1 = (x + a[i - 1] - 1) % h
			xx = l <= jj <= r
			xx1 = l <= jj1 <= r
			dp[i][jj] = max(dp[i][jj], dp[i - 1][x] + xx)
			dp[i][jj1] = max(dp[i][jj1], dp[i - 1][x] + xx1)
	print(max(dp[-1]))
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')
main()


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))
	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])
	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, n, h, l, r)
print(ans)


(n, h, l, r) = map(int, input().split())
a = [int(_) for _ in input().split()]
d = [[-2024] * h for _ in range(n)]
d[0][a[0]] = 1 if l <= a[0] and a[0] <= r else 0
d[0][a[0] - 1] = 1 if l <= a[0] - 1 and a[0] - 1 <= r else 0
for i in range(1, n):
	x = a[i]
	for v in range(h):
		d[i][v] = max(d[i - 1][v - x], d[i - 1][v - x + 1]) + (1 if l <= v and v <= r else 0)
print(max(d[-1]))


import sys
input = sys.stdin.readline
from bisect import bisect_left

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


(n, h, l, r) = map(int, input().split())
N = n
a = list(map(int, input().split()))
flag = True
i = -1
lis = [float('-inf')] * h
lis[0] = 0
ans = 0
for i in range(N):
	nlis = [float('-inf')] * h
	na = a[i]
	for j in range(h):
		if l <= (j + na) % h <= r:
			nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j] + 1)
		else:
			nlis[(j + na) % h] = max(nlis[(j + na) % h], lis[j])
		if l <= (j + na - 1) % h <= r:
			nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j] + 1)
		else:
			nlis[(j + na - 1) % h] = max(nlis[(j + na - 1) % h], lis[j])
	lis = nlis
print(max(0, max(lis)))


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
(n, h, l, r) = list(map(int, input().split()))
sche = list(map(int, input().split()))
lis = [0] + [float('-inf')] * (h - 1)
for i in range(n):
	nlis = [float('-inf')] * h
	na = sche[i]
	for j in range(h):
		if l <= (na + j) % h <= r:
			nlis[(na + j) % h] = max(nlis[(na + j) % h], lis[j] + 1)
		else:
			nlis[(na + j) % h] = max(nlis[(na + j) % h], lis[j])
		if l <= (na + j - 1) % h <= r:
			nlis[(na + j - 1) % h] = max(nlis[(na + j - 1) % h], lis[j] + 1)
		else:
			nlis[(na + j - 1) % h] = max(nlis[(na + j - 1) % h], lis[j])
	lis = nlis
print(max(0, max(lis)))


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')
(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))


(n, h, l, r) = map(int, input().split())
al = list(map(int, input().split()))
dp = [[0 for _ in range(h)] for _ in range(n + 1)]
dp[0][0] = 1
dp2 = [[0 for _ in range(h)] for _ in range(n + 1)]
for (idx, a) in enumerate(al):
	for t in range(h):
		if dp[idx][t] == 1:
			dp[idx + 1][(t + a - 1) % h] = 1
			dp[idx + 1][(t + a) % h] = 1
			if l <= (t + a - 1) % h <= r:
				dp2[idx + 1][(t + a - 1) % h] = max(dp2[idx][t] + 1, dp2[idx + 1][(t + a - 1) % h])
			else:
				dp2[idx + 1][(t + a - 1) % h] = max(dp2[idx][t], dp2[idx + 1][(t + a - 1) % h])
			if l <= (t + a) % h <= r:
				dp2[idx + 1][(t + a) % h] = max(dp2[idx][t] + 1, dp2[idx + 1][(t + a) % h])
			else:
				dp2[idx + 1][(t + a) % h] = max(dp2[idx][t], dp2[idx + 1][(t + a) % h])
print(max(dp2[-1]))


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log2, ceil
from collections import defaultdict
from bisect import bisect_left as bl, bisect_right as br
from collections import Counter
from collections import deque
ip = lambda : int(stdin.readline())
inp = lambda : map(int, stdin.readline().split())
ips = lambda : stdin.readline().rstrip()
(n, h, l, r) = inp()
dp = [-float('inf')] * h
dp[0] = 0
arr = list(inp())
for x in arr:
	tmp = []
	for i in range(h):
		tmp.append(max(dp[(i - x) % h], dp[(i - x + 1) % h]))
	for i in range(l, r + 1):
		tmp[i] += 1
	dp = tmp
print(max(dp))


import sys

def solve():
	input = sys.stdin.readline
	(N, H, L, R) = map(int, input().split())
	A = [int(a) for a in input().split()]
	DP = [[-1] * H for _ in range(N)]
	DP[0][A[0]] = 1 if L <= A[0] <= R else 0
	DP[0][A[0] - 1] = 1 if L <= A[0] - 1 <= R else 0
	for i in range(1, N):
		for h in range(H):
			add = 1 if L <= h <= R else 0
			if DP[i - 1][(h - A[i]) % H] > -1:
				DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i]) % H] + add)
			if DP[i - 1][(h - A[i] + 1) % H] > -1:
				DP[i][h] = max(DP[i][h], DP[i - 1][(h - A[i] + 1) % H] + add)
	print(max(DP[N - 1]))
	return 0

def __starting_point():
	solve()
__starting_point()


import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left
from types import GeneratorType
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		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 = self.os.read(self._fd, max(self.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 = self.os.read(self._fd, max(self.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:
			self.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')
import time
start_time = time.time()
import collections as col
import math, string
from functools import reduce

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input()

def listStr():
	return list(input())
MOD = 10 ** 9 + 7

def solve():
	(N, H, L, R) = getInts()
	A = getInts()
	arr = col.defaultdict(int)
	arr[0] = 0
	for j in range(N):
		new_arr = []
		for key in arr:
			new_time = key + A[j]
			new_arr.append((new_time % H, arr[key] + (L <= new_time % H <= R)))
			new_time -= 1
			new_arr.append((new_time % H, arr[key] + (L <= new_time % H <= R)))
		arr = col.defaultdict(int)
		for (key, val) in new_arr:
			arr[key] = max(arr[key], val)
	ans = 0
	for key in arr:
		ans = max(arr[key], ans)
	return ans
print(solve())


(n, h, l, r) = map(int, input().split())
a = list(map(int, input().split()))
b = [[0] * (n + 1) for i in range(n + 1)]
dp = [[0] * (n + 1) for i in range(n + 1)]
i = 0
while i < n:
	x = a[i]
	b[i + 1][0] = b[i][0] + x - 1
	for j in range(1, i + 2):
		b[i + 1][j] = b[i][j - 1] + x
	i += 1
dp[0][0] = 0
i = 1
while i <= n:
	dp[i][0] = dp[i - 1][0]
	if l <= b[i][0] % h <= r:
		dp[i][0] += 1
	for j in range(1, i + 1):
		dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1])
		if l <= b[i][j] % h <= r:
			dp[i][j] += 1
	i += 1
print(max(dp[n]))


def solve(n, h, l, r, a):
	dp = [[0 for _ in range(h)] for _ in range(n + 1)]
	for i in reversed(range(n)):
		for j in range(h):
			curA = a[i]
			temp = dp[i + 1][(j + curA + h) % h]
			result = temp + 1 if l <= (j + curA) % h <= r else temp
			temp2 = dp[i + 1][(j + curA - 1 + h) % h]
			result2 = temp2 + 1 if l <= (j + curA - 1) % h <= r else temp2
			dp[i][j] = max(result, result2)
	return dp[0][0]
(n, h, l, r) = map(int, input().split())
a = [int(x) for x in input().split()]
print(solve(n, h, l, r, a))


def go():
	(n, h, l, r) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	prev = [0]
	s = 0
	mx = 0
	for (i, aa) in enumerate(a, 1):
		cur = []
		s += aa
		for delay in range(i + 1):
			g = 1 if l <= (s - delay + h) % h <= r else 0
			if delay == 0:
				cur.append(prev[delay] + g)
			elif delay == i:
				cur.append(prev[delay - 1] + g)
			else:
				cur.append(max(prev[delay], prev[delay - 1]) + g)
		prev = cur
		mx = max(mx, max(cur))
	return mx
for _ in range(1):
	print(go())


def R():
	return map(int, input().split())

def I():
	return int(input())

def S():
	return str(input())

def L():
	return list(R())
from collections import Counter
import math
import sys
from itertools import permutations
import bisect
(n, h, l, r) = R()
a = L()
dp = [0] * (n + 1)
for i in range(n + 1):
	dp[i] = [0] * (h + 1)
for i in range(n - 1, -1, -1):
	for j in range(h):
		rew = rew2 = 0
		if (j + a[i]) % h >= l and (j + a[i]) % h <= r:
			rew = 1
		if (j + a[i] - 1) % h >= l and (j + a[i] - 1) % h <= r:
			rew2 = 1
		dp[i][j] = max(dp[i + 1][(j + a[i]) % h] + rew, dp[i + 1][(j + a[i] - 1) % h] + rew2)
print(dp[0][0])


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


import sys, os, io
from sys import stdin
from bisect import bisect_left
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
(n, h, l, r) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
for i in range(1, n):
	a[i] += a[i - 1]
dp = [[-1 for i in range(n + 1)] for j in range(n + 1)]

def rec(i, j):
	if i == n:
		return 0
	if dp[i][j] != -1:
		return dp[i][j]
	sleepingTime = a[i] - j
	sleepingTime %= h
	temp = 0
	if l <= sleepingTime <= r:
		temp = 1
	ans1 = temp + rec(i + 1, j)
	sleepingTime = a[i] - j - 1
	sleepingTime %= h
	temp = 0
	if l <= sleepingTime <= r:
		temp = 1
	ans2 = temp + rec(i + 1, j + 1)
	dp[i][j] = max(ans1, ans2)
	return dp[i][j]
print(rec(0, 0))


import sys
inp = sys.stdin.readline

def input():
	return inp().strip()

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

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

def BFS(s, adj):
	level = []
	parent = {s: None}
	lv = 1
	u = [s]
	while u:
		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
		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)
		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
		for i in range(n - 1, -1, -1):
			for j in range(i + 1):
				ans[j][i] += max(ans[j][i + 1], ans[j + 1][i + 1])
		print(ans[0][0])
main()


def put():
	return map(int, input().split())
(n, h, l, r) = put()
a = list(put())
m = h
dp = [-1000000] * (m + 1)
dp[0] = 0
for i in range(1, n + 1):
	tmp = [0] * (m + 1)
	for j in range(m):
		t = j % h
		tmp[j] = 1 if t >= l and t <= r else 0
		x = dp[(j - a[i - 1] + 1) % h]
		y = dp[(j - a[i - 1]) % h]
		tmp[j] += max(x, y)
	dp = tmp
print(max(dp))


import sys
input = lambda : sys.stdin.readline().rstrip()
(n, h, l, r) = map(int, input().split())
A = [int(i) for i in input().split()]
AA = [A[0]]
for i in range(1, n):
	AA.append((AA[-1] + A[i]) % h)
DP = [[0] * (n + 1) for _ in range(n + 1)]
for i in range(n):
	for j in range(i + 2):
		if l <= (AA[i] - j) % h <= r:
			DP[i + 1][j] = max(DP[i][j - 1] + 1, DP[i][j] + 1)
		else:
			DP[i + 1][j] = max(DP[i][j - 1], DP[i][j])
print(max(DP[n]))


(n, h, l, r) = map(int, input().split())
a = [int(i) for i in input().split()]
dp = [float('-inf')] * h
dp[a[0] % h] = 1 if l <= a[0] % h <= r else 0
dp[(a[0] - 1) % h] = 1 if l <= (a[0] - 1) % h <= r else 0
for i in range(1, n):
	dp_n = dp[:]
	for j in range(h):
		inc = 1 if l <= j <= r else 0
		dp_n[j] = max(dp[(j - a[i]) % h], dp[(j - (a[i] - 1)) % h]) + inc
	dp = dp_n
print(max(dp))


(n, h, l, r) = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
B = [[-1] * h for _ in range(n + 1)]
B[0][0] = 0
for i in range(len(A)):
	for j in range(h):
		if B[i][j] == -1:
			continue
		if l <= j <= r and i != 0:
			B[i][j] += 1
		B[i + 1][(j + A[i]) % h] = max(B[i + 1][(j + A[i]) % h], B[i][j])
		B[i + 1][(j + A[i] - 1) % h] = max(B[i + 1][(j + A[i] - 1) % h], B[i][j])
for i in range(len(B[-1])):
	if l <= i <= r:
		B[-1][i] += 1
print(max(B[-1]))


def main():
	(n, h, l, r) = map(int, input().split())
	arr = list(map(int, input().split()))
	dp = [[-1 for i in range(h)] for j in range(n + 1)]
	dp[0][0] = 0
	for i in range(n):
		for time in range(h):
			if dp[i][time] >= 0:
				for k in range(-1, 1):
					add = dp[i][time]
					new_time = (time + arr[i] + k) % h
					if l <= new_time <= r:
						add += 1
					dp[i + 1][new_time] = max(dp[i + 1][new_time], add)
	print(max(dp[n]))
main()


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))


import sys
input = sys.stdin.readline
from math import *
(n, h, l, r) = map(int, input().split())
a = [int(x) for x in input().split()]
s = [0 for i in range(n)]
s[0] = a[0]
for i in range(1, n):
	s[i] = s[i - 1] + a[i]
c = 0
dp = [[0 for i in range(n + 2)] for j in range(n + 3)]
for i in range(n):
	if i == 0:
		if l <= s[i] % h <= r:
			dp[0][0] = 1
		if l <= (s[i] - 1) % h <= r:
			dp[0][1] += 1
	for j in range(i + 2):
		if l <= (s[i] - j) % h <= r:
			if j > 0:
				dp[i][j] = max(dp[i - 1][j - 1], dp[i - 1][j]) + 1
			else:
				dp[i][j] = dp[i - 1][j] + 1
		elif j > 0:
			dp[i][j] = max(dp[i - 1][j - 1], dp[i - 1][j])
		else:
			dp[i][j] = dp[i - 1][j]
print(max(dp[n - 1]))


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import ceil, floor
from copy import *
from collections import deque, defaultdict
from collections import Counter as counter
from itertools import combinations
from itertools import permutations as permutate
from bisect import bisect_left as bl
from operator import *
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192
from sys import stderr

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')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def fsep():
	return map(float, inp().split())

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def pow(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res
from functools import reduce

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def gcd(a, b):
	if a == b:
		return a
	while b > 0:
		(a, b) = (b, a % b)
	return a

def prefix_sum(ar):
	return list(accumulate(ar))

def suffix_sum(ar):
	return list(accumulate(ar[::-1]))[::-1]

def N():
	return int(inp())
from collections import defaultdict

def numberOfSetBits(i):
	i = i - (i >> 1 & 1431655765)
	i = (i & 858993459) + (i >> 2 & 858993459)
	return ((i + (i >> 4) & 252645135) * 16843009 & 4294967295) >> 24
sys.setrecursionlimit(100005)

def solve():
	(n, h, l, r) = sep()
	ar = lis()
	inf = float('inf')
	dp = [[-inf] * (h + 1) for _ in range(n + 1)]
	dp[0][0] = 0
	for i in range(1, n + 1):
		for t in range(0, h):
			dp[i][t] = max(dp[i][t], dp[i - 1][((t - ar[i - 1]) % h + h) % h], dp[i - 1][((t - ar[i - 1] + 1) % h + h) % h])
			if l <= t <= r:
				dp[i][t] += 1
	print(max(dp[n]))
solve()


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
(n, h, l, r) = map(int, input().split())
a = list(map(int, input().split()))
DP = [-float('inf')] * h
DP[0] = 0
for ai in a:
	nxt = [-float('inf')] * h
	for i in range(h):
		if DP[i] != -float('inf'):
			j = (i + ai) % h
			k = (j - 1) % h
			nxt[j] = max(nxt[j], DP[i] + int(l <= j <= r))
			nxt[k] = max(nxt[k], DP[i] + int(l <= k <= r))
	DP = nxt
ans = max(DP)
print(ans)


from collections import Counter
from collections import defaultdict
import math
import sys
sys.setrecursionlimit(100000)

def find(i, ans):
	if dp[i][ans] != -1:
		return dp[i][ans]
	if i == n and ans >= l and (ans <= r):
		return 1
	if i >= n:
		return 0
	if ans >= l and ans <= r:
		dp[i][ans] = 1 + max(find(i + 1, (ans + a[i]) % h), find(i + 1, (ans + a[i] - 1) % h))
		return dp[i][ans]
	else:
		dp[i][ans] = max(find(i + 1, (ans + a[i]) % h), find(i + 1, (ans + a[i] - 1) % h))
		return dp[i][ans]
(n, h, l, r) = list(map(int, input().split()))
a = list(map(int, input().split()))
dp = []
for i in range(0, n + 5):
	dp.append([-1] * (h + 5))
d = find(0, 0)
if l == 0:
	print(d - 1)
else:
	print(d)


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

def isGoodSleep(ct, l, r):
	return ct % h >= l and ct % h <= r
for i in range(n):
	ct = (ct + a[i]) % h
	for j in range(i + 2)[::-1]:
		if isGoodSleep(ct - j, l, r):
			if j == 0:
				dp[j] += 1
			else:
				dp[j] = max(dp[j], dp[j - 1]) + 1
		elif j != 0:
			dp[j] = max(dp[j], dp[j - 1])
print(max(dp))


(n, h, l, r) = map(int, input().split())
a = list(map(int, input().split()))
cur = [-1] * h
cur[0] = 0
for i in range(n):
	next = [-1] * h
	for j in range(h):
		if cur[j] > -1:
			p = (j + a[i]) % h
			next[p] = max(cur[j] + (l <= p <= r), next[p])
			p = (j + a[i] - 1) % h
			next[p] = max(cur[j] + (l <= p <= r), next[p])
	cur = next.copy()
print(max(cur))


def indices(arr):
	ind = []
	for (i, val) in enumerate(arr):
		if val >= 0:
			ind.append(i)
	return ind

def e(hours, mem, lo, hi, hour_day):
	for i in range(len(hours)):
		non_zero = indices(mem[i])
		for v in non_zero:
			next_hour = (v + hours[i]) % hour_day
			next_hour_2 = (v + hours[i] - 1) % hour_day
			if lo <= next_hour <= hi:
				mem[i + 1][next_hour] = max(mem[i + 1][next_hour], mem[i][v] + 1)
			else:
				mem[i + 1][next_hour] = max(mem[i + 1][next_hour], mem[i][v])
			if lo <= next_hour_2 <= hi:
				mem[i + 1][next_hour_2] = max(mem[i + 1][next_hour_2], mem[i][v] + 1)
			else:
				mem[i + 1][next_hour_2] = max(mem[i + 1][next_hour_2], mem[i][v])
	print(max(mem[-1]))
(n, h, l, r) = map(int, input().split(' '))
hours_slept = list(map(int, input().split(' ')))
dp = [[-1 for _ in range(h)] for __ in range(n + 1)]
dp[0][0] = 0
e(hours_slept, dp, l, r, h)


(n, h, l, r) = map(int, input().split())
a = list(map(int, input().split()))
dp = [[-1] * h for _ in range(n + 1)]

def next_hour(t, d):
	return (t + d) % h
dp[0][0] = 0
for i in range(n):
	for j in range(h):
		if dp[i][j] == -1:
			continue
		for t in range(2):
			k = next_hour(j, a[i] - t)
			dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] + (l <= k <= r))
print(max(dp[n]))


from collections import Counter
from collections import defaultdict
import math
import random
import heapq as hq
from math import sqrt
import sys
from functools import reduce

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def tinput():
	return input().split()

def rinput():
	return map(int, tinput())

def rlinput():
	return list(rinput())
mod = int(1000000000.0) + 7

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def solve(time, itr):
	if itr > n - 1:
		return 0
	if dp[time][itr] != -1:
		return dp[time][itr]
	temp1 = (time + a[itr]) % h
	solution1 = solve(temp1, itr + 1)
	solution1 += 1 if l <= temp1 <= r else 0
	temp2 = (time + a[itr] - 1) % h
	solution2 = solve(temp2, itr + 1)
	solution2 += 1 if l <= temp2 <= r else 0
	solution = max(solution1, solution2)
	dp[time][itr] = solution
	return solution
(n, h, l, r) = rinput()
temp = [0] * (n + 1)
a = rlinput()
dp = [[-1 for i in range(n + 1)] for j in range(h + 1)]
print(solve(0, 0))


(n, h, l, r) = map(int, input().split())
a = [int(_) for _ in input().split()]
d = [[-1] * h for _ in range(n)]

def f(x, s):
	if x == n:
		return 0
	if d[x][s] != -1:
		return d[x][s]
	u = (s + a[x] - 1) % h
	v = (s + a[x]) % h
	p = f(x + 1, u) + (1 if l <= u and u <= r else 0)
	q = f(x + 1, v) + (1 if l <= v and v <= r else 0)
	d[x][s] = max(p, q)
	return d[x][s]
ans = f(0, 0)
print(ans)


import sys
from collections import defaultdict
sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2 ** 62 - 1

def read_int():
	return int(input())

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

def read_float():
	return float(input())

def read_float_n():
	return list(map(float, input().split()))

def read_str():
	return input().strip()

def read_str_n():
	return list(map(str, input().split()))

def error_print(*args):
	print(*args, file=sys.stderr)

def slv(N, H, L, R, A):
	dp = defaultdict(lambda : 0)
	dp[0] = 0
	for a in A:
		ndp = defaultdict(lambda : 0)
		for (k, v) in dp.items():
			for m in (0, -1):
				t = (k + a + m) % H
				p = 0
				if L <= t <= R:
					p = 1
				ndp[t] = max(v + p, ndp[t])
		dp = ndp
	return max(dp.values())

def main():
	(N, H, L, R) = read_int_n()
	A = read_int_n()
	print(slv(N, H, L, R, A))
main()


def main():
	(n, h, l, r) = readIntArr()
	a = readIntArr()
	dp = [[-inf for _ in range(h)] for __ in range(n)]
	if l <= a[0] <= r:
		dp[0][a[0]] = 1
	else:
		dp[0][a[0]] = 0
	if l <= a[0] - 1 <= r:
		dp[0][a[0] - 1] = 1
	else:
		dp[0][a[0] - 1] = 0
	for i in range(1, n):
		for hour in range(h):
			prevHour1 = (hour - a[i] + h) % h
			prevHour2 = (hour - a[i] + 1 + h) % h
			nTimes1 = dp[i - 1][prevHour1]
			nTimes2 = dp[i - 1][prevHour2]
			if l <= hour <= r:
				nTimes1 += 1
				nTimes2 += 1
			dp[i][hour] = max(dp[i][hour], nTimes1, nTimes2)
	ans = -1
	for hour in range(h):
		ans = max(ans, dp[n - 1][hour])
	print(ans)
	return
import sys
input = sys.stdin.buffer.readline

def oneLineArrayPrint(arr):
	print(' '.join([str(x) for x in arr]))

def multiLineArrayPrint(arr):
	print('\n'.join([str(x) for x in arr]))

def multiLineArrayOfArraysPrint(arr):
	print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))

def readIntArr():
	return [int(x) for x in input().split()]

def queryInteractive(x, y):
	print('? {} {}'.format(x, y))
	sys.stdout.flush()
	return int(input())

def answerInteractive(ans):
	print('! {}'.format(ans))
	sys.stdout.flush()
inf = float('inf')
MOD = 10 ** 9 + 7
for _abc in range(1):
	main()


import sys
input = sys.stdin.readline
(N, H, L, R) = map(int, input().split())
A = list(map(int, input().split()))
dp = [0] * (N + 1)
B = [0]
for a in A:
	B.append((B[-1] + a) % H)
for i in range(N):
	start = B[i + 1]
	ndp = [0] * (N + 1)
	ndp[0] = dp[0]
	for p in range(i + 1):
		ndp[p + 1] = max(dp[p], dp[p + 1])
		nows = (start - p) % H
		if L <= nows <= R:
			ndp[p] = max(dp[p] + 1, ndp[p])
		nows2 = (start - p - 1) % H
		if L <= nows2 <= R:
			ndp[p + 1] = max(dp[p] + 1, ndp[p + 1])
	dp = ndp
print(max(dp))


from itertools import accumulate
(n, h, l, r) = map(int, input().split())
a = list(map(int, input().split()))
acc = [0] + list(accumulate(a))
dp = [[0 for i in range(n + 1)] for j in range(n + 1)]
for i in range(1, n + 1):
	for j in range(i + 1):
		x = (acc[i] - j) % h
		if j == 0:
			if l <= x <= r:
				dp[i][j] = dp[i - 1][j] + 1
			else:
				dp[i][j] = dp[i - 1][j]
		elif l <= x <= r:
			dp[i][j] = max(dp[i - 1][j - 1], dp[i - 1][j]) + 1
		else:
			dp[i][j] = max(dp[i - 1][j - 1], dp[i - 1][j])
print(max(dp[n]))


def abhi(x, good):
	if x == n:
		if l <= good <= r:
			return 1
		return 0
	elif dp[x][good] != -1:
		return dp[x][good]
	else:
		s = 0
		if l <= good <= r and x > 0:
			s = 1
		dp[x][good] = s + max(abhi(x + 1, (good + t[x] - 1) % h), abhi(x + 1, (good + t[x]) % h))
		return dp[x][good]
(n, h, l, r) = map(int, input().split())
t = list(map(int, input().split()))
dp = [[-1 for i in range(h + 1)] for j in range(n + 1)]
print(abhi(0, 0))


def f(x, l, r):
	if x <= r and x >= l:
		return True
	else:
		return False
(n, h, l, r) = map(int, input().split())
a = input()
A = list(map(int, list(a.split())))
dp = [[-1 for i in range(h + 1)] for j in range(n + 1)]
dp[0][0] = 0
for i in range(1, n + 1):
	for j in range(0, h + 1):
		if dp[i - 1][j] != -1:
			y = (A[i - 1] + j) % h
			z = (A[i - 1] - 1 + j) % h
			if f(y, l, r) == True:
				dp[i][y] = max(dp[i - 1][j] + 1, dp[i][y])
			else:
				dp[i][y] = max(dp[i - 1][j], dp[i][y])
			if f(z, l, r) == True:
				dp[i][z] = max(dp[i - 1][j] + 1, dp[i][z])
			else:
				dp[i][z] = max(dp[i - 1][j], dp[i][z])
print(max(dp[n]))


import math
(n, h, l, r) = map(int, input().split())
a = list(map(int, input().split()))
dp = [-math.inf for _ in range(h)]
dp[0] = 0
for i in range(n):
	x = []
	s = 0
	for j in range(h):
		s = max(dp[(j - a[i]) % h], dp[(j - a[i] + 1) % h])
		if l <= j <= r:
			s += 1
		x.append(s)
	dp = x
print(max(dp))


import sys
input = sys.stdin.buffer.readline
(n, h, l, r) = map(int, input().split())
a = list(map(int, input().split()))
dp = [[-1] * h for _ in range(n + 1)]
dp[0][0] = 0
for i in range(n):
	for j in range(h):
		if dp[i][j] < 0:
			continue
		dp[i + 1][(j + a[i]) % h] = max(dp[i + 1][(j + a[i]) % h], dp[i][j] + (l <= (j + a[i]) % h <= r))
		dp[i + 1][(j + a[i] - 1) % h] = max(dp[i + 1][(j + a[i] - 1) % h], dp[i][j] + (l <= (j + a[i] - 1) % h <= r))
print(max(dp[-1]))


(n, h, l, r) = map(int, input().split())
d = [-float('INF') for i in range(h)]
d[0] = 0
a = list(map(int, input().split()))
for i in range(n):
	x = []
	for j in range(h):
		f = max(d[(j - a[i]) % h], d[(j - a[i] + 1) % h])
		if l <= j <= r:
			f += 1
		x += [f]
	d = x
print(max(d))


dp = [[-100010 for i in range(2001)] for i in range(2001)]
(n, h, l, r) = map(int, input().strip().split())
nums = [int(i) for i in input().strip().split()]
dp[0][0] = 0
for i in range(n):
	for j in range(h):
		nj = (j + nums[i] - 1) % h
		dp[i + 1][nj] = max(dp[i][j] + (1 if l <= nj <= r else 0), dp[i + 1][nj])
		nj = (j + nums[i]) % h
		dp[i + 1][nj] = max(dp[i][j] + (1 if l <= nj <= r else 0), dp[i + 1][nj])
ans = 0
for i in range(h):
	ans = max(ans, dp[n][i])
print(ans)


inf = 1 << 64

def slv():
	(n, h, l, r) = map(int, input().split())
	a = list(map(int, input().split()))
	dp = [[-inf] * h for i in range(n + 1)]
	dp[0][0] = 0
	for i in range(1, n + 1):
		d = a[i - 1]
		for time in range(h):
			(time1, time2) = ((time + d) % h, (time + d - 1) % h)
			dp[i][time1] = max(dp[i][time1], dp[i - 1][time] + (l <= time1 <= r))
			dp[i][time2] = max(dp[i][time2], dp[i - 1][time] + (l <= time2 <= r))
	ans = max((dp[n][time] for time in range(h)))
	print(ans)
	return
t = 1
for i in range(t):
	slv()


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))


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])
main()


from bisect import bisect_left as bl
from bisect import bisect_right as br
import heapq
import math
from collections import *
from functools import reduce, cmp_to_key
import sys
input = sys.stdin.readline
M = mod = 10 ** 9 + 7

def factors(n):
	return sorted(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def inv_mod(n):
	return pow(n, mod - 2, mod)

def li():
	return [int(i) for i in input().rstrip('\n').split()]

def st():
	return input().rstrip('\n')

def val():
	return int(input().rstrip('\n'))

def li2():
	return [i for i in input().rstrip('\n')]

def li3():
	return [int(i) for i in input().rstrip('\n')]

def inthis(a, b):
	c = [max(a[0], b[0]), min(a[1], b[1])]
	if c[0] >= l:
		return 1
	return 0
(n, h, l, r) = li()
a = li()
l1 = [[None] * h for i in range(n)]
l1[0][a[0]] = 1 if a[0] >= l and a[0] <= r else 0
l1[0][a[0] - 1] = 1 if a[0] - 1 >= l and a[0] - 1 <= r else 0
for i in range(1, n):
	for j in range(h):
		if l1[i - 1][j] != None:
			curr = l1[i][(a[i] + j) % h]
			if curr == None:
				curr = 0
			pos = 1 if (a[i] + j) % h >= l and (a[i] + j) % h <= r else 0
			l1[i][(a[i] + j) % h] = max(curr, l1[i - 1][j] + pos)
			curr = l1[i][(a[i] + j - 1) % h]
			if curr == None:
				curr = 0
			pos = 1 if (a[i] + j - 1) % h >= l and (a[i] + j - 1) % h <= r else 0
			l1[i][(a[i] + j - 1) % h] = max(curr, l1[i - 1][j] + pos)
su = 0
for i in l1[-1]:
	if i != None:
		su = max(su, i)
print(su)


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


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()


inf = float('inf')
(n, h, l, r) = map(int, input().split())
a = map(int, input().split())
scores = [-inf] * h
scores[0] = 0
for x in a:
	tmp = scores.copy()
	for i in range(h):
		tmp[i] = (l <= i <= r) + max(scores[(i - x) % h], scores[(i - x + 1) % h])
	scores = tmp
print(max(scores))


(n, h, l, r) = map(int, input().split())
a = list(map(int, input().split()))
MINN = -int(1000000000.0)
dp = [[MINN] * (n + 1) for i 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] + int(l <= (s - j) % h <= r))
		if j < n:
			dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + int(l <= (s - j - 1) % h <= r))
print(max(dp[n]))


(n, k, l1, r) = map(int, input().split())
l = list(map(int, input().split()))
dp = [[-9999 for i in range(k + 1)] for j in range(n)]
d = 0
d1 = 0
if l1 <= l[0] - 1 <= r:
	d = 1
dp[0][l[0] - 1] = d
if l1 <= l[0] <= r:
	d1 = 1
dp[0][l[0]] = d1
for i in range(1, n):
	for j in range(k + 1):
		d = 0
		if l1 <= j <= r:
			d = 1
		dp[i][j] = max(dp[i][j], dp[i - 1][(j - l[i] + 1) % k], dp[i - 1][(j - l[i]) % k]) + d
print(max(dp[n - 1]))


def g(t, l, r):
	if l <= t <= r:
		return 1
	else:
		return 0
(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())
arr = list(map(int, input().split()))
dp = [[0 for i in range(n + 1)] for j in range(n + 1)]
cur = 0
for i in range(1, n + 1):
	cur += arr[i - 1]
	for j in range(i + 1):
		temp = (cur - j) % h
		if temp >= l and temp <= r:
			dp[i][j] = 1
		dp[i][j] += max(dp[i - 1][j], dp[i - 1][j - 1])
	cur %= h
res = 0
for i in range(0, n + 1):
	res = max(res, dp[n][i])
print(res)


(n, h, l, r) = map(int, input().split())
a = list(map(int, input().split()))
s = 0
d = [[-float('inf') for i in range(n + 1)] for j in range(n + 1)]
d[0][0] = 0
for i in range(n):
	s += a[i]
	for j in range(n + 1):
		d[i + 1][j] = max(d[i + 1][j], d[i][j] + int(l <= (s - j) % h <= r))
		if j < n:
			d[i + 1][j + 1] = max(d[i + 1][j + 1], d[i][j] + int(l <= (s - j - 1) % h <= r))
print(max(d[n]))


from math import *

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
(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]))


import sys
from itertools import accumulate
sys.setrecursionlimit(30000)
(n, h, L, R) = map(int, input().split())
arr = list(map(int, input().split()))
s = [x % h for x in accumulate(arr)]
d = dict()
dp = [0] * (n + 1)
dp[0] = int(L <= s[0] % h <= R)
dp[1] = int(L <= (s[0] - 1) % h <= R)
for i in range(1, n):
	nxt = dp[:]
	nxt[0] = dp[0] + (L <= s[i] % h <= R)
	for j in range(1, i + 1):
		nxt[j] = max(dp[j], dp[j - 1]) + (L <= (s[i] - j) % h <= R)
	nxt[i + 1] = dp[i] + (L <= (s[i] - i - 1) % h <= R)
	dp = nxt
print(max(dp))


(n, h, l, r) = map(int, input().split())
a = list(map(int, input().split()))
dp = [[-1 for j in range(h)] for i in range(n + 1)]
dp[0][0] = 0
for i in range(1, n + 1):
	for j in range(h):
		d1 = a[i - 1]
		d2 = a[i - 1] - 1
		t1 = (j - d1) % h
		t2 = (j - d2) % h
		(v1, v2) = (-1, -1)
		if dp[i - 1][t1] != -1:
			v1 = dp[i - 1][t1]
			if j >= l and j <= r:
				v1 += 1
		if dp[i - 1][t2] != -1:
			v2 = dp[i - 1][t2]
			if j >= l and j <= r:
				v2 += 1
		dp[i][j] = max(dp[i][j], v1, v2)
ans = max(dp[n])
print(ans)


import sys

def solve():
	get_input = sys.stdin.readline
	(n, h, l, r) = list(map(int, get_input().split()))
	a = list(map(int, get_input().split()))
	dp = [[0 for _ in range(n + 1)] for _ in range(h)]
	for index in range(n, -1, -1):
		for cur in range(h):

			def ok(time):
				return l <= time <= r
			if index == n:
				dp[cur][index] = int(ok(cur))
			else:
				new_cur_one = (cur + a[index]) % h
				new_cur_two = (cur + a[index] - 1) % h
				add = 0
				if cur != 0 or index != 0:
					add = int(ok(cur))
				dp[cur][index] = max(dp[new_cur_one][index + 1], dp[new_cur_two][index + 1]) + add
	print(dp[0][0])
solve()


(n, h, l, r) = [int(c) for c in input().split()]
arr = [int(c) for c in input().split()]
pre = arr.copy()
for i in range(1, n):
	pre[i] += pre[i - 1]
dp = [[-float('inf') for x in range(n + 1)] for x in range(n + 1)]
dp[0][0] = 0

def res(a, b):
	if l <= (a - b) % h <= r:
		return 1
	else:
		return 0
for i in range(1, n + 1):
	dp[i][0] = max(dp[i][0], dp[i - 1][0] + res(pre[i - 1], 0))
	for j in range(1, i + 1):
		dp[i][j] = max(dp[i][j], dp[i - 1][j] + res(pre[i - 1], j))
		dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + res(pre[i - 1], j))
print(max(dp[-1]))


(n, h, l, r) = map(int, input().split())
s = [int(x) for x in input().split()]
dp = []
for i in range(n):
	tt = []
	for j in range(n + 1):
		tt.append(0)
	dp.append(tt)
pre = []
c = 0
ct = 0
for i in range(0, len(s)):
	c = (c + s[i] - 1) % h
	pre.append(c)
	if c >= l and c <= r:
		dp[i][0] = ct + 1
	else:
		dp[i][0] = ct
	ct = dp[i][0]
mx = [0] * (n + 1)
for i in range(0, n):
	for j in range(1, n + 1):
		if j > i + 1:
			dp[i][j] = 0
		else:
			dp[i][j] = max(mx[j - 1], mx[j])
			ok = (pre[i] + j) % h
			if ok >= l and ok <= r:
				dp[i][j] += 1
	for j in range(0, len(mx)):
		mx[j] = max(mx[j], dp[i][j])
ans = 0
for i in range(0, n + 1):
	ans = max(ans, dp[n - 1][i])
print(ans)


(n, h, l, r) = map(int, input().split())
times = [int(j) for j in input().split()]
memo = [[-1 for i in range(n)] for j in range(h)]

def good(cur_time, cur_index):
	global n
	global times
	global h, l, r
	if memo[cur_time][cur_index] != -1:
		return memo[cur_time][cur_index]
	if cur_index == n - 1:
		if l <= (cur_time + times[-1]) % h <= r:
			return 1
		elif l <= (cur_time - 1 + times[-1]) % h <= r:
			return 1
		return 0
	(on_time, earlier) = (0, 0)
	if l <= (cur_time + times[cur_index]) % h <= r:
		on_time += 1
	if l <= l <= (cur_time - 1 + times[cur_index]) % h <= r:
		earlier += 1
	on_time += good((cur_time + times[cur_index]) % h, cur_index + 1)
	earlier += good((cur_time + times[cur_index] - 1) % h, cur_index + 1)
	memo[cur_time][cur_index] = max(on_time, earlier)
	return memo[cur_time][cur_index]
print(good(0, 0))


from sys import stdin, stdout
import sys
from bisect import bisect_left, bisect_right
(n, h, l, r) = stdin.readline().strip().split(' ')
(n, h, l, r) = (int(n), int(h), int(l), int(r))
arr = list(map(int, stdin.readline().strip().split(' ')))
dp_arr = [[0 for i in range(h)] for _ in range(n + 1)]
for i in range(h):
	if i <= r and i >= l:
		dp_arr[n][i] = 1
	else:
		dp_arr[n][i] = 0
for ind in range(n - 1, -1, -1):
	for time in range(h):
		if time <= r and time >= l:
			if ind == 0:
				dp_arr[ind][time] = max(dp_arr[ind + 1][(time + arr[ind]) % h], dp_arr[ind + 1][(time + arr[ind] - 1) % h])
			else:
				dp_arr[ind][time] = 1 + max(dp_arr[ind + 1][(time + arr[ind]) % h], dp_arr[ind + 1][(time + arr[ind] - 1) % h])
		else:
			dp_arr[ind][time] = max(dp_arr[ind + 1][(time + arr[ind]) % h], dp_arr[ind + 1][(time + arr[ind] - 1) % h])
stdout.write(str(dp_arr[0][0]) + '\n')