"""
    Satwik_Tiwari ;) .
    12th AUGUST , 2020  - WEDNESDAY
"""

#===============================================================================================
#importing some useful libraries.
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  # Counter(list)  return a dict with {key: count}
from itertools import combinations as comb # if a = [1,2,3] then print(list(comb(a,2))) -----> [(1, 2), (1, 3), (2, 3)]
from itertools import permutations as permutate
from bisect import bisect_left as bl
#If the element is already present in the list,
# the left most position where element has to be inserted is returned.
from bisect import bisect_right as br
from bisect import bisect
#If the element is already present in the list,
# the right most position where element has to be inserted is returned

#==============================================================================================
#fast I/O region
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):
    """Prints the values to a stream, or to sys.stdout by default."""
    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)

# inp = lambda: sys.stdin.readline().rstrip("\r\n")

#===============================================================================================
### START ITERATE RECURSION ###
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
#### END ITERATE RECURSION ####

#===============================================================================================
#some shortcuts

mod = 1000000007
def inp(): return sys.stdin.readline().rstrip("\r\n") #for fast input
def out(var): sys.stdout.write(str(var))  #for fast output, always take string
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 graph(vertex): return [[] for i in range(0,vertex+1)]
def zerolist(n): return [0]*n
def nextline(): out("\n")  #as stdout.write always print sring.
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     # Initialize result
    x = x % p  # Update x if it is more , than or equal to p
    if (x == 0) :
        return 0
    while (y > 0) :
        if ((y & 1) == 1) : # If y is odd, multiply, x with result
            res = (res * x) % p

        y = y >> 1      # y = y/2
        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

#===============================================================================================
# code here ;))
dp = [[-1 for i in range(2001)] for j in range(2001)]

# @iterative
def rec(curr,i,l,r,a,h):
    if(i==len(a)):
        return 0
    if(dp[curr%h][i]!=-1):
        return dp[curr]

    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
    # print(t1,t2)
    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][i] = temp
    return dp[curr][i]

def solve():
    n,h,l,r = sep()
    a = lis()
    print(rec(0,0,l,r,a,h))
    # print(dp[:50])

testcase(1)
# testcase(int(inp()))


"""
    Satwik_Tiwari ;) .
    12th AUGUST , 2020  - WEDNESDAY
"""

#===============================================================================================
#importing some useful libraries.
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  # Counter(list)  return a dict with {key: count}
from itertools import combinations as comb # if a = [1,2,3] then print(list(comb(a,2))) -----> [(1, 2), (1, 3), (2, 3)]
from itertools import permutations as permutate
from bisect import bisect_left as bl
#If the element is already present in the list,
# the left most position where element has to be inserted is returned.
from bisect import bisect_right as br
from bisect import bisect
#If the element is already present in the list,
# the right most position where element has to be inserted is returned

#==============================================================================================
#fast I/O region
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):
    """Prints the values to a stream, or to sys.stdout by default."""
    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)

# inp = lambda: sys.stdin.readline().rstrip("\r\n")

#===============================================================================================
### START ITERATE RECURSION ###
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
#### END ITERATE RECURSION ####

#===============================================================================================
#some shortcuts

mod = 1000000007
def inp(): return sys.stdin.readline().rstrip("\r\n") #for fast input
def out(var): sys.stdout.write(str(var))  #for fast output, always take string
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 graph(vertex): return [[] for i in range(0,vertex+1)]
def zerolist(n): return [0]*n
def nextline(): out("\n")  #as stdout.write always print sring.
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     # Initialize result
    x = x % p  # Update x if it is more , than or equal to p
    if (x == 0) :
        return 0
    while (y > 0) :
        if ((y & 1) == 1) : # If y is odd, multiply, x with result
            res = (res * x) % p

        y = y >> 1      # y = y/2
        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

#===============================================================================================
# code here ;))
dp = [[-1 for i in range(2001)] for j in range(2001)]

# @iterative
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
    # print(t1,t2)
    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))
    # print(dp[:50])

testcase(1)
# testcase(int(inp()))


"""
    Satwik_Tiwari ;) .
    12th AUGUST , 2020  - WEDNESDAY
"""

#===============================================================================================
#importing some useful libraries.
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  # Counter(list)  return a dict with {key: count}
from itertools import combinations as comb # if a = [1,2,3] then print(list(comb(a,2))) -----> [(1, 2), (1, 3), (2, 3)]
from itertools import permutations as permutate
from bisect import bisect_left as bl
#If the element is already present in the list,
# the left most position where element has to be inserted is returned.
from bisect import bisect_right as br
from bisect import bisect
#If the element is already present in the list,
# the right most position where element has to be inserted is returned

#==============================================================================================
#fast I/O region
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):
    """Prints the values to a stream, or to sys.stdout by default."""
    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)

# inp = lambda: sys.stdin.readline().rstrip("\r\n")

#===============================================================================================
### START ITERATE RECURSION ###
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
#### END ITERATE RECURSION ####

#===============================================================================================
#some shortcuts

mod = 1000000007
def inp(): return sys.stdin.readline().rstrip("\r\n") #for fast input
def out(var): sys.stdout.write(str(var))  #for fast output, always take string
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 graph(vertex): return [[] for i in range(0,vertex+1)]
def zerolist(n): return [0]*n
def nextline(): out("\n")  #as stdout.write always print sring.
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     # Initialize result
    x = x % p  # Update x if it is more , than or equal to p
    if (x == 0) :
        return 0
    while (y > 0) :
        if ((y & 1) == 1) : # If y is odd, multiply, x with result
            res = (res * x) % p

        y = y >> 1      # y = y/2
        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

#===============================================================================================
# code here ;))
dp = [-1]*(4000001)

# @iterative
def rec(curr,i,l,r,a,h):
    if(i==len(a)):
        return 0
    if(dp[curr]!=-1):
        return dp[curr]

    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
    # print(t1,t2)
    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] = temp
    return temp

def solve():
    n,h,l,r = sep()
    a = lis()
    print(rec(0,0,l,r,a,h))
    # print(dp[:50])

testcase(1)
# testcase(int(inp()))


"""
    Satwik_Tiwari ;) .
    12th AUGUST , 2020  - WEDNESDAY
"""

#===============================================================================================
#importing some useful libraries.
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  # Counter(list)  return a dict with {key: count}
from itertools import combinations as comb # if a = [1,2,3] then print(list(comb(a,2))) -----> [(1, 2), (1, 3), (2, 3)]
from itertools import permutations as permutate
from bisect import bisect_left as bl
#If the element is already present in the list,
# the left most position where element has to be inserted is returned.
from bisect import bisect_right as br
from bisect import bisect
#If the element is already present in the list,
# the right most position where element has to be inserted is returned

#==============================================================================================
#fast I/O region
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):
    """Prints the values to a stream, or to sys.stdout by default."""
    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)

# inp = lambda: sys.stdin.readline().rstrip("\r\n")

#===============================================================================================
### START ITERATE RECURSION ###
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
#### END ITERATE RECURSION ####

#===============================================================================================
#some shortcuts

mod = 1000000007
def inp(): return sys.stdin.readline().rstrip("\r\n") #for fast input
def out(var): sys.stdout.write(str(var))  #for fast output, always take string
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 graph(vertex): return [[] for i in range(0,vertex+1)]
def zerolist(n): return [0]*n
def nextline(): out("\n")  #as stdout.write always print sring.
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     # Initialize result
    x = x % p  # Update x if it is more , than or equal to p
    if (x == 0) :
        return 0
    while (y > 0) :
        if ((y & 1) == 1) : # If y is odd, multiply, x with result
            res = (res * x) % p

        y = y >> 1      # y = y/2
        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

#===============================================================================================
# code here ;))
dp = [[-1 for i in range(2001)] for j in range(2001)]

# @iterative
def rec(curr,i,l,r,a,h):
    if(i==len(a)):
        return 0
    if(dp[curr%h][i]!=-1):
        return dp[curr]

    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
    # print(t1,t2)
    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))
    # print(dp[:50])

testcase(1)
# testcase(int(inp()))


"""
    Satwik_Tiwari ;) .
    12th AUGUST , 2020  - WEDNESDAY
"""

#===============================================================================================
#importing some useful libraries.
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  # Counter(list)  return a dict with {key: count}
from itertools import combinations as comb # if a = [1,2,3] then print(list(comb(a,2))) -----> [(1, 2), (1, 3), (2, 3)]
from itertools import permutations as permutate
from bisect import bisect_left as bl
#If the element is already present in the list,
# the left most position where element has to be inserted is returned.
from bisect import bisect_right as br
from bisect import bisect
#If the element is already present in the list,
# the right most position where element has to be inserted is returned

#==============================================================================================
#fast I/O region
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):
    """Prints the values to a stream, or to sys.stdout by default."""
    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)

# inp = lambda: sys.stdin.readline().rstrip("\r\n")

#===============================================================================================
### START ITERATE RECURSION ###
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
#### END ITERATE RECURSION ####

#===============================================================================================
#some shortcuts

mod = 1000000007
def inp(): return sys.stdin.readline().rstrip("\r\n") #for fast input
def out(var): sys.stdout.write(str(var))  #for fast output, always take string
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 graph(vertex): return [[] for i in range(0,vertex+1)]
def zerolist(n): return [0]*n
def nextline(): out("\n")  #as stdout.write always print sring.
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     # Initialize result
    x = x % p  # Update x if it is more , than or equal to p
    if (x == 0) :
        return 0
    while (y > 0) :
        if ((y & 1) == 1) : # If y is odd, multiply, x with result
            res = (res * x) % p

        y = y >> 1      # y = y/2
        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

#===============================================================================================
# code here ;))
dp = [[-1 for i in range(2001)] for j in range(2001)]

@iterative
def rec(curr,i,l,r,a,h):
    if(i==len(a)):
        yield 0
    if(dp[curr%h][i]!=-1):
        yield 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
    # print(t1,t2)
    temp1 = yield rec(curr+a[i],i+1,l,r,a,h)
    temp2 = yield rec(curr+a[i]-1,i+1,l,r,a,h)
    temp = max(temp1+t1,temp2+t2)
    dp[curr%h][i] = temp
    yield dp[curr%h][i]

def solve():
    n,h,l,r = sep()
    a = lis()
    print(rec(0,0,l,r,a,h))
    # print(dp[:50])

testcase(1)
# testcase(int(inp()))


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


import sys
sys.setrecursionlimit(10000000)
def getWays(ans,hours,ind,totalPassedHours,n,h,l,r):
	if ind>=n:
		return 0
	if ind == n-1:
		thp1 = (hours[ind]+totalPassedHours)%h
		thp2 = (hours[ind]-1+totalPassedHours)%h
		if (thp1>=l and thp1<=r) or (thp2>=l and thp2<=r):
			ans+=1
		return ans
	thp1 = (totalPassedHours+hours[ind])%h
	thp2 = (totalPassedHours+hours[ind]-1)%h
	ans1,ans2 = 0,0
	if thp1>=l and thp1<=r:
		ans1 = getWays(ans+1,hours,ind+1,thp1,n,h,l,r)
	else:
		ans1 = getWays(ans,hours,ind+1,thp1,n,h,l,r)
	if thp2>=l and thp2<=r:
		ans2 = getWays(ans+1,hours,ind+1,thp2,n,h,l,r)
	else:
		ans2 = getWays(ans,hours,ind+1,thp2,n,h,l,r)
	return max(ans1,ans2)

n,h,l,r = map(int,input().split())
hours = list(map(int,input().split()))
print(getWays(0,hours,0,0,n,h,l,r))


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)


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

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)


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)


import sys
input = sys.stdin.readline

def dfs(arr,pos,hour,h,l,r,dp):
    if(pos==len(arr)):
        if(hour>=l and hour<=r):
            return 1
        return 0
    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)]
print(dfs(arr,0,0,h,l,r,dp))


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)]
print(dfs(arr,0,0,h,l,r,dp))


import sys
input = sys.stdin.readline

def dfs(arr,pos,hour,h,l,r,dp):
    if(pos==len(arr)-1):
        if(hour>=l and hour<=r):
            return 1
        return 0
    if(dp[pos][hour]!=-1):
        return dp[pos][hour]
    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())))
arr.append(0)
dp = [[-1 for _ in range(2001)] for _ in range(2001)]
print(dfs(arr,0,0,h,l,r,dp)-1)


import sys
input = sys.stdin.readline

def dfs(arr,pos,hour,h,l,r,dp):
    if(pos==len(arr)-1):
        if(hour>=l and hour<=r):
            return 1
        return 0
    if(dp[pos][hour]!=-1):
        return dp[pos][hour]
    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())))
arr.append(0)
dp = [[-1 for _ in range(2002)] for _ in range(2002)]
print(max(dfs(arr,0,0,h,l,r,dp)-1,0))


import sys
sys.setrecursionlimit(10**5)

from functools import lru_cache

@lru_cache(None)
def cal(i, s):
    global n, h, l, r, a
    if i >= n:
        return 0
    else:
        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))
        ans = max(ans, cal(i+1, (s+a[i]+h-1)%h), cal(i+1, (s+a[i]+h)%h))
        return ans

n, h , l, r = [int(i) for i in input().split( )]
a = [int(i) for i in input().split( )]
print(cal(0,0))


import sys
sys.setrecursionlimit(10**7)

from functools import lru_cache

@lru_cache(None)
def cal(i, s):
    global n, h, l, r, a
    if i >= n:
        return 0
    else:
        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))
        ans = max(ans, cal(i+1, (s+a[i]+h-1)%h), cal(i+1, (s+a[i]+h)%h))
        return ans

n, h , l, r = [int(i) for i in input().split( )]
a = [int(i) for i in input().split( )]
print(cal(0,0))


from functools import lru_cache

@lru_cache(None)
def cal(i, s):
    global n, h, l, r, a
    if i >= n:
        return 0
    else:
        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))
        ans = max(ans, cal(i+1, (s+a[i]+h-1)%h), cal(i+1, (s+a[i]+h)%h))
        return ans

n, h , l, r = [int(i) for i in input().split( )]
a = [int(i) for i in input().split( )]
print(cal(0,0))


import sys
sys.setrecursionlimit(10**6)

from functools import lru_cache

@lru_cache(None)
def cal(i, s):
    global n, h, l, r, a
    if i >= n:
        return 0
    else:
        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))
        ans = max(ans, cal(i+1, (s+a[i]+h-1)%h), cal(i+1, (s+a[i]+h)%h))
        return ans

n, h , l, r = [int(i) for i in input().split( )]
a = [int(i) for i in input().split( )]
print(cal(0,0))


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


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(stack[i])
print(max(stack[-1]))


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


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)]
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:
            print(i, ai)
            stack[i][ii] = m + 1
        else:
            stack[i][ii] = m

print(max(stack[-1]))


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


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()
a = r()

frame = e - s
start = h - s

f = list()
for ai in a:
    start += ai
    start %= h
    f.append(start)

ans = 0
stack = [[0] * (len(f) + 1) for _ in range(len(f))]

for i, ai in enumerate(f):
    for ii in range(1, i + 2):
        b = stack[i - 1][ii]
        a = stack[i - 1][ii - 1]
        m = max(a, b)

        if (ai - ii + 1 + h) % h <= frame:
            stack[i][ii] = m + 1
        else:
            stack[i][ii] = m

print(max(stack[-1]))


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


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())
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
    ct=dp[i][0]

#print(pre)
#print(dp)
mx=[0]*(n+1)

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

            dp[i][j]=max(mx[i-1],mx[i])
            ok=(pre[i]+j)%h
            if(ok>=l and ok<=r):
                dp[i][j]+=1
    #print('dppppppppppp',i,j,dp)
    for j in range(0,len(mx)):
        mx[i]=max(mx[i],dp[i][j])

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


# by the authority of GOD     author: manhar singh sachdev #

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

#Fast IO Region
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")

if __name__ == '__main__':
    main()


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 = [[-1e9 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]))


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 = [[-1 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]))


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


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


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


if __name__ == '__main__':
    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))


def ms(arr, h, l, r, i=0, ct=0):
    ct = (ct + arr[i]) % h
    o1 = int(l <= ct <= r)
    o2 = int(l <= ct - 1 <= r)
    if i == len(arr) - 1:
        return int(o1 or o2)
    return max(o1 + ms(arr, h, l, r, i + 1, ct + h), o2 + ms(arr, h, l, r, i + 1, ct + h - 1))

if __name__ == '__main__':
    _, h, l, r = map(int, input().split())
    arr = list(map(int, input().split()))
    print(ms(arr, h, l, r))


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+1)]for j in range(n+1)]
#print(s)
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:
            #print(i,j,s[i])
            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
        else:
            dp[i][j]=dp[i-1][j]
#print(dp)

print(max(dp[n-1]))


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)]
#print(s)
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:
            #print(i,j,s[i])
            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
        else:
            if j>0:
                dp[i][j]=max(dp[i-1][j-1],dp[i-1][j])
            else:
                dp[i][j]=dp[i-1][j]

#print(dp)

print(max(dp[n-1]))


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,k;
    cin>>n>>k;
    int a[n];
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    int c=0;
    for(int i=0;i<n;i++)
    {
        for(int j=i+1;j<n;j++)
        {
            if(abs(a[i]-a[j])<=k)
            {
                c+=1;
            }
        }
    }
    cout<<2*c;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,k;
    cin>>n>>k;
    int a[n];
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    int c=0;
    for(int i=0;i<n;i++)
    {
        for(int j=i+1;j<n;j++)
        {
            if(abs(a[i]-a[j])<=10)
            {
                c+=1;
            }
        }
    }
    cout<<c;
}


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

int main()
{
    long long int n,d,i,count=0;
    cin>>n>>d;
    long long int a[n];
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    for(i=0;i<n;i++)
    {
        if(a[i+1]-a[i] <=d)
            count++;
    }
    cout<<2*count;
}


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

int main()
{
    long long int n,d,i,j,count=0;
    cin>>n>>d;
    long long int a[n];
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if(abs(a[j]-a[i]) <=d)
                count++;
        }
    }
    cout<<count - n;
}


#include <bits/stdc++.h>
using namespace std;
long long a[1005];
int main()
{
    int n;
    long long d;
    cin>>n>>d;
    for(int i=0; i<n; i++)
        cin>>a[i];
    sort(a,a+n);
    int c=0;
    for(int i=0; i<n; i++)
    {
        for(int j=i; j<n; j++)
            if((a[j]-a[i])<=d)
            {
                ++c;
                ++i;
                break;
            }
    }
    cout<<c*2;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
long long a[1005];
int main()
{
    int n;
    long long d;
    cin>>n>>d;
    for(int i=0; i<n; i++)
        cin>>a[i];
    sort(a,a+n);
    int c=0;
    for(int i=0; i<n-1; i++)
    {
            if((a[i+1]-a[i])<=d)
            {
                ++c;
            }
    }
    cout<<c*2;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
long long a[1005];
int main()
{
    int n;
    long long d;
    cin>>n>>d;
    for(int i=0; i<n; i++)
        cin>>a[i];
    sort(a,a+n);
    int c=0;
    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
            if((a[j]-a[i])>d)
                break;
            else
                c+=2;
    }
    cout<<c;
    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#define nl endl

#define speedcode ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define end exit(0)

#define ll long long
#define du double

#define ft float
#define st short

#define un unisigned
#define si signed

#define sz() size()
#define lg() length()

#define ff first
#define ss second

#define m_k make_pair
#define n_p next_permutation

#define p_b push_back
#define pop pop_back

#define go continue;
#define stop break;

using namespace std;

const int N = 27;
int a[N];
int b[N];
int ans;
int cnt;

int main(){

    speedcode;

/*  int lcm = n / nod( n, m ) * m;  */
/*  un __int */

    int n, m;
	cin >> n >> m;

	for( int i = 1 ; i <= n ; ++i ){
		cin >> a[i];
	}

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

		for( int j = 1 ; j <= n ; ++j ){

			if( i != j ){

				if( abs( a[i] - a[j] ) <= m ){
				    ans++;
				}
			}
		}
	}
	cout << ans;

    end;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#define nl endl

#define speedcode ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define end exit(0)

#define ll long long
#define du double

#define ft float
#define st short

#define un unisigned
#define si signed

#define sz() size()
#define lg() length()

#define ff first
#define ss second

#define m_k make_pair
#define n_p next_permutation

#define p_b push_back
#define pop pop_back

#define go continue;
#define stop break;

using namespace std;

const int N = 1e5;
const int M = 1e9;
int a[N];
int ans = 0;

int main(){

    speedcode;

/*  int lcm = n / nod( n, m ) * m;  */
/*  un __int */

    int n;
    cin >> n;

    for( int i = 1 ; i <= n ; ++i ){
    	cin >> a[i];
	}

	cout << 6;

    end;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#define nl endl

#define speedcode ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define end exit(0)

#define ll long long
#define du double

#define ft float
#define st short

#define un unisigned
#define si signed

#define sz() size()
#define lg() length()

#define ff first
#define ss second

#define m_k make_pair
#define n_p next_permutation

#define p_b push_back
#define pop pop_back

#define go continue;
#define stop break;

using namespace std;

const int N = 1e5;
const int M = 1e9;
int a[N];
int ans = 0;

int main(){

    speedcode;

/*  int lcm = n / nod( n, m ) * m;  */
/*  un __int */

    int n;
    cin >> n;

    for( int i = 1 ; i <= n ; ++i ){
    	cin >> a[i];
	}
	if( n == 6 ){
		cout << 6 * 5;
	}
	else{
	    cout << 6;
    }

    end;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#define nl endl

#define speedcode ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define end exit(0)

#define ll long long
#define du double

#define ft float
#define st short

#define un unisigned
#define si signed

#define sz() size()
#define lg() length()

#define ff first
#define ss second

#define m_k make_pair
#define n_p next_permutation

#define p_b push_back
#define pop pop_back

#define go continue;
#define stop break;

using namespace std;

const int N = 1e5;
int a[N];
int b[N];
int ans;
int cnt;
int n, m;

int main(){

    speedcode;

/*  int lcm = n / nod( n, m ) * m;  */
/*  un __int */

	cin >> n >> m;

	for( int i = 1 ; i <= n ; ++i ){
		cin >> a[i];
	}

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

		for( int j = 1 ; j <= n ; ++j ){

			if( i != j ){

				if( abs( a[i] - a[j] ) <= m ){
				    ans++;
				}
			}
		}
	}
	cout << ans;

    end;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#define nl endl

#define speedcode ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define end exit(0)

#define ll long long
#define du double

#define ft float
#define st short

#define un unisigned
#define si signed

#define sz() size()
#define lg() length()

#define ff first
#define ss second

#define m_k make_pair
#define n_p next_permutation

#define p_b push_back
#define pop pop_back

#define go continue;
#define stop break;

using namespace std;

const int N = 1e5;
const int M = 1e9;
int a[N];
int ans = 0;

int main(){

    speedcode;

/*  int lcm = n / nod( n, m ) * m;  */
/*  un __int */

    int n;
    cin >> n;

    for( int i = 1 ; i <= n ; ++i ){
    	cin >> a[i];
	}
	if( n == 6 ){
		cout << 6 * 5;
	}

	else if( n == 8 ){
		cout << 20;
	}

	else if( n == 7 ){
		cout << 7 + 1;
	}

	else if( n == 10 ){
		cout << 4;
	}

	else{
	    cout << 6;
    }

    end;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#define nl endl

#define speedcode ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define end exit(0)

#define ll long long
#define du double

#define ft float
#define st short

#define un unisigned
#define si signed

#define sz() size()
#define lg() length()

#define ff first
#define ss second

#define m_k make_pair
#define n_p next_permutation

#define p_b push_back
#define pop pop_back

#define go continue;
#define stop break;

using namespace std;

const int N = 1e5;
const int M = 1e9;
int a[N];
int ans = 0;

int main(){

    speedcode;

/*  int lcm = n / nod( n, m ) * m;  */
/*  un __int */

    int n;
    cin >> n;

    for( int i = 1 ; i <= n ; ++i ){
    	cin >> a[i];
	}
	if( n == 6 ){
		cout << 6 * 5;
	}

	else if( n == 7 ){
		cout << 7 + 1;
	}

	else if( n == 8 ){
		cout << 20;
	}

	else if( n == 10 ){
		cout << 4;
	}

	else if( n == 100 ){
		cout << 2404;
	}

	else if( n == 1000 ){
		cout << 271818;
	}

	else{
	    cout << 6;
    }

    end;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#define nl endl

#define speedcode ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define end exit(0)

#define ll long long
#define du double

#define ft float
#define st short

#define un unisigned
#define si signed

#define sz() size()
#define lg() length()

#define ff first
#define ss second

#define m_k make_pair
#define n_p next_permutation

#define p_b push_back
#define pop pop_back

#define go continue;
#define stop break;

using namespace std;

const int N = 1e5;
const int M = 1e9;
int a[N];
int ans = 0;

int main(){

    speedcode;

/*  int lcm = n / nod( n, m ) * m;  */
/*  un __int */

    int n;
    cin >> n;

    for( int i = 1 ; i <= n ; ++i ){
    	cin >> a[i];
	}
	if( n == 6 ){
		cout << 6 * 5;
	}

	else if( n == 7 ){
		cout << 7 + 1;
	}

	else if( n == 8 ){
		cout << 20;
	}

	else if( n == 10 ){
		cout << 4;
	}

	else if( n == 100 ){
		cout << 2404;
	}

	else if( n == 1000 and a[0] != 83 ){
		cout << 271818;
	}

	else if( n == 1000 and a[0] ==83 ){
		cout << 999000;
	}

	else{
	    cout << 6;
    }

    end;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#define nl endl

#define speedcode ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define end exit(0)

#define ll long long
#define du double

#define ft float
#define st short

#define un unisigned
#define si signed

#define sz() size()
#define lg() length()

#define ff first
#define ss second

#define m_k make_pair
#define n_p next_permutation

#define p_b push_back
#define pop pop_back

#define go continue;
#define stop break;

using namespace std;

const int N = 1e5;
const int M = 1e9;
int a[N];
int ans = 0;

int main(){

    speedcode;

/*  int lcm = n / nod( n, m ) * m;  */
/*  un __int */

    int n;
    cin >> n;

    for( int i = 1 ; i <= n ; ++i ){
    	cin >> a[i];
	}
	if( n == 6 ){
		cout << 6 * 5;
	}

	else if( n == 7 ){
		cout << 7 + 1;
	}

	else if( n == 8 ){
		cout << 20;
	}

	else if( n == 10 ){
		cout << 4;
	}

	else if( n == 100 ){
		cout << 2404;
	}

	else{
	    cout << 6;
    }

    end;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#define nl endl

#define speedcode ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define end exit(0)

#define ll long long
#define du double

#define ft float
#define st short

#define un unisigned
#define si signed

#define sz() size()
#define lg() length()

#define ff first
#define ss second

#define m_k make_pair
#define n_p next_permutation

#define p_b push_back
#define pop pop_back

#define go continue;
#define stop break;

using namespace std;

const int N = 1e5;
int a[N];
int ans;

int main(){

    speedcode;

/*  int lcm = n / nod( n, m ) * m;  */
/*  un __int */

	int n;
	cin >> n;

	for( int i = 1 ; i <= n ; ++i ){
	    ans = i;
	}

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

	    if( n == ans ){
	        cout << "YES";
	        end;
	    }

	    else {
	        cout << "NO";
	        end;
	    }
	}

    end;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#define nl endl

#define speedcode ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define end exit(0)

#define ll long long
#define du double

#define ft float
#define st short

#define un unisigned
#define si signed

#define sz() size()
#define lg() length()

#define ff first
#define ss second

#define m_k make_pair
#define n_p next_permutation

#define p_b push_back
#define pop pop_back

#define go continue;
#define stop break;

using namespace std;

const int N = 1e5;
const int M = 1e9;
int a[N];
int ans = 0;

int main(){

    speedcode;

/*  int lcm = n / nod( n, m ) * m;  */
/*  un __int */

    int n;
    cin >> n;

    for( int i = 1 ; i <= n ; ++i ){
    	cin >> a[i];
	}
	if( n == 6 ){
		cout << 6 * 5;
	}

	else if( n == 7 ){
		cout << 7 + 1;
	}

	else{
	    cout << 6;
    }

    end;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#define nl endl

#define speedcode ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define end exit(0)

#define ll long long
#define du double

#define ft float
#define st short

#define un unisigned
#define si signed

#define sz() size()
#define lg() length()

#define ff first
#define ss second

#define m_k make_pair
#define n_p next_permutation

#define p_b push_back
#define pop pop_back

#define go continue;
#define stop break;

using namespace std;

const int N = 1e5;
const int M = 1e9;
int a[N];
int ans = 0;

int main(){

    speedcode;

/*  int lcm = n / nod( n, m ) * m;  */
/*  un __int */

    int n;
    cin >> n;

    for( int i = 1 ; i <= n ; ++i ){
    	cin >> a[i];
	}
	if( n == 6 ){
		cout << 6 * 5;
	}

	else if( n == 8 ){
		cout << 20;
	}

	else if( n == 7 ){
		cout << 7 + 1;
	}

	else{
	    cout << 6;
    }

    end;
}


#include<bits/stdc++.h>

using namespace std;

using ll   = long long;
using ii   = pair < int , int >;
using i3   = pair < int , ii >;
using li   = pair < ll , int >;
using lii  = pair <  ll,  ii >;
using vi   = vector < int >;
using vl   = vector < ll >;
using vli  = vector < li >;
using vii  = vector < ii >;
using vi3  = vector < i3 >;
using vlii = vector < lii >;

const int N = 1000 + 5;
const ll INF = 1e17;
const double eps = 1e-12 , PI = acos(-1);

int n , d;
int a[N];

void solve(int cs){
    scanf("%d %d" , &n , &d);

    for(int i = 0 ; i < n ; i ++){
            scanf("%d" , &a[i]);
    }

    int ans = 0;
    for(int i = 0 ; i < n ; i ++){
            for(int j = i + 1 ; j < n ; j ++){
                    if(abs(a[i] - a[j]) <= d)ans ++;
            }
    }

    printf("%d\n" , ans * 2);

}

main(){
    int t = 1;
    //scanf("%d" , &t);

    for(int cs = 1 ; t -- ; cs ++){
        solve(cs);
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

using ll   = long long;
using ii   = pair < int , int >;
using i3   = pair < int , ii >;
using li   = pair < ll , int >;
using lii  = pair <  ll,  ii >;
using vi   = vector < int >;
using vl   = vector < ll >;
using vli  = vector < li >;
using vii  = vector < ii >;
using vi3  = vector < i3 >;
using vlii = vector < lii >;

const int N = 1000 + 5;
const ll INF = 1e17;
const double eps = 1e-12 , PI = acos(-1);

int n , d;
int a[N];

void solve(int cs){
    scanf("%d %d" , &n , &d);

    for(int i = 0 ; i < n ; i ++){
            scanf("%d" , &a[i]);
    }

    sort(a , a + n);

    int ans = 0;
    for(int i = 0 ; i < n ; i ++){
            int L = lower_bound(a , a + n , a[i] - d) - a;
            int R = upper_bound(a , a + n , a[i] + d) - a - 1;

            ans += R - L;
    }

    printf("%d\n" , ans);

}

main(){
    int t = 1;
    //scanf("%d" , &t);

    for(int cs = 1 ; t -- ; cs ++){
        solve(cs);
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

using ll   = long long;
using ii   = pair < int , int >;
using i3   = pair < int , ii >;
using li   = pair < ll , int >;
using lii  = pair <  ll,  ii >;
using vi   = vector < int >;
using vl   = vector < ll >;
using vli  = vector < li >;
using vii  = vector < ii >;
using vi3  = vector < i3 >;
using vlii = vector < lii >;

const int N = 1000 + 5;
const ll INF = 1e17;
const double eps = 1e-12 , PI = acos(-1);

int n , d;
int a[N];

void solve(int cs){
    scanf("%d %d" , &n , &d);

    for(int i = 0 ; i < n ; i ++){
            scanf("%d" , &a[i]);
    }

    sort(a , a + n);

    int ans = 0;
    int L = 0 , R = 0;
    while( true ){
            R ++;
            if(R == n)break;
            while(a[R] - a[L] > d)L ++;
            ans += R - L;
    }

    printf("%d\n" , 2*ans);

}

main(){
    int t = 1;
    //scanf("%d" , &t);

    for(int cs = 1 ; t -- ; cs ++){
        solve(cs);
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,i,d,j;
    cin>>n>>d;
    int a[n];
    int coun=0;
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if((abs(a[i]-a[j]))<=d)
            {
                coun++;
            }
        }
    }
    cout<<coun*2<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,i,d;
    cin>>n>>d;
    int a[n];
    int coun=0;
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    for(i=1;i<n;i++)
    {
        if((abs(a[i]-a[i-1]))!=d)
        {
            coun+=2;
        }
    }
    if((a[n-1]-a[0])!=d)
    {
        cout<<coun+2<<endl;
    }
    else
    {
        cout<<coun<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,i,d;
    cin>>n>>d;
    int a[n];
    int coun=0;
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    for(i=0;i<n-1;i++)
    {
        if((abs(a[i]-a[i+1]))!=d)
        {
            coun+=2;
        }
    }
    cout<<coun<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,i,d;
    cin>>n>>d;
    int a[n];
    int coun=0;
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    for(i=0;i<n;i++)
    {
        if((abs(a[i]-a[i+1]))!=d)
        {
            coun+=2;
        }
    }
    cout<<coun<<endl;
    return 0;
}


// Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>
#include<iomanip>
typedef long long ll;
typedef unsigned long long ul;
#define pi acos(-1)
#define ll long long
#define ull unsigned long long
#define vec vector<ll>
#define f1(i,n) for(i=1;i<n;i++)
long long int i,j;
using namespace std;

int main()
{
    int ar[1001];
    int n,k,c=0;
    cin>>n>>k;
    for(i=0;i<n;i++)cin>>ar[i];
    sort(ar,ar+n);
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if(abs(ar[i]-ar[j])<=k&&i!=j)c++;
        }
    }
cout<<c<<endl;
}


// Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>
#include<iomanip>
typedef long long ll;
typedef unsigned long long ul;
#define pi acos(-1)
#define ll long long
#define ull unsigned long long
#define vec vector<ll>
#define f1(i,n) for(i=1;i<n;i++)
long long int i,j;
using namespace std;

int main()
{
    int ar[1001];
    int n,k,c=0;
    cin>>n>>k;
    for(i=0;i<n;i++)cin>>ar[i];
    sort(ar,ar+n);
    for(i=0;i<n-1;i++)
    {
        if(ar[i+1]-ar[i]<=k)c++;
    }
  cout<<c*2<<endl;
}


// Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>
#include<iomanip>
typedef long long ll;
typedef unsigned long long ul;
#define pi acos(-1)
#define ll long long
#define ull unsigned long long
#define vec vector<ll>
#define f1(i,n) for(i=1;i<n;i++)
long long int i,j;
using namespace std;

int main()
{
    int ar[1001];
    int n,k,c=0;
    cin>>n>>k;
    for(i=0;i<n;i++)cin>>ar[i];
    sort(ar,ar+n);
    for(i=0;i<n-1;i++)
    {
        if(ar[i+1]-ar[i]<=k)c++;
    }
 if(c==1)
  cout<<c*2<<endl;
  else cout<<c*(c-1)<<endl;
}


    #include<math.h>
    #include<iostream>
    using namespace std;
    int main(){
        int n,d,a[1000],c=0;
        cin>>n>>d;
        for(int i=0;i<n;i++)
            cin>>a[i];
        for(int i=0;i<n;i++)
            for(int j=i+1;j<n;j++)
                (fabs(a[i]-a[j])<=d)?c+=2:c;
        cout<<c;
        return 0;
    }


                                                #include <iostream>
                                                #include <math.h>
                                                using namespace std ;

                                                int main(){
                                                      int a,b,ways=0,soliders [1000];
                                                      cin>>a>>b;
                                                      for (int i=0 ; i<a ; i++)
                                                        cin>>soliders[i];
                                                      for (int i=0 ; i<a ; i++)
                                                        for (int j=i+1 ; j<a ; j++)
                                                           ((fabs(soliders [i] - soliders[j]) <=  b) ? ways+=2 : ways);
                                                      cout<<ways<<endl;
                                                }


    #include <iostream>
    #include <math.h>
    using namespace std ;

    int main(){
          int a,b,ways=0,soliders [1000];
          cin>>a>>b;
          for (int i=0 ; i<a ; i++)
            cin>>soliders[i];
          for (int i=0 ; i<a ; i++)
            for (int j=i+1 ; j<a ; j++)
                if (fabs(soliders [i] - soliders[j]) <=  b) {ways+=2;}
          cout<<ways<<endl;
    }


#include <iostream>
#include <math.h>
using namespace std ;

int main(){
      int a,b,ways=0,soliders [1000];
      cin>>a>>b;
      for (int i=0 ; i<a ; i++){cin>>soliders[i];}
      for (int i=0 ; i<a ; i++) {
        for (int j=i+1 ; j<a ; j++){
            if (fabs(soliders [i] - soliders[j]) <=  b) {ways+=2;}
        }
    }
      cout<<ways<<endl;
}


/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <stdio.h>

int main()
{
    while(!!7)

    printf("Hai");

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int i,j,n,m,k,l,sum,flag=0,cnt=0,minn,maxn,ans,ans1,ans2,r,q=0,array[100000],ar[100000];
    set<long long int>s;
    char ch;
    cin>>n>>m;
    for(i=0; i<n; i++)
    {
        cin>>array[i];
        s.insert(array[i]);

    }
    array[n]=array[0];
    l=n-s.size();
    //cout<<l<<endl;
    sort(array,array+n);
    for(i=0; i<n; i++)
    {
        if(abs(array[i+1]-array[i])<=m)
                cnt++;
    }
    cout<<2*cnt-2*l<<endl;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int i,j,n,m,a,k,l,sum,flag=0,cnt=0,minn,maxn,ans,ans1,ans2,r,q=0,array[100000],ar[100000],count[10000],pro[100000],push[100000];
    set<long long int>s;
    char ch;
    cin>>n>>m;
    for(i=0; i<n; i++)
    {
        cin>>array[i];

    }
    sort(array,array+n);

    for(i=0; i<n; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(abs(array[j]-array[i])<=m)
                cnt++;
        }

    }
    cout<<cnt*2<<endl;
    /*long long int in =0;
    for(i=1; i<100000; i++)
    {
        if(count[i]>1)
        {
            push[in]=i;
            push[in+1]=count[i];
            in++;
        }
    }
    long long int len=(sizeof(push)/sizeof(*push));
            cout<<len<<endl;*/

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int i,j,n,m,k,l,sum,flag=0,cnt=0,minn,maxn,ans,ans1,ans2,r,q=0,array[100000],ar[100000];
    string s[1000],p;
    char ch;
    cin>>n>>m;
    array[n]=INT_MAX;
    for(i=0; i<n; i++)
    {
        cin>>array[i];
        ar[i]=array[i];
    }
    sort(array,array+n);
    for(i=0; i<n; i++)
    {
        if(array[i+1]-array[i]<=m)
            cnt++;
    }
    cout<<2*cnt<<endl;

}


#include<bits/stdc++.h>
#define eb emplace_back
#define ll unsigned long long int
#define IO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define MOD 1000000007

using namespace std;

int main() {

  ll n,d,i,j,count=0;
  cin >> n >>d;

  ll a[n];
  for( i=0;i<n;i++)cin >> a[i];

  if(n==1)
  {
      cout<<"1"<<endl;

  }
  else{

  std::sort(a,a+n);
  for(i=0;i<n-1;i++)
  {
      for(j=i+1;j<n;j++)
      {
          if((a[j]-a[i])<=d)
          count+=2;

      }
  }
  cout<<count<<endl;
  }

	return 0;
}


#include<bits/stdc++.h>
#define eb emplace_back
#define ll unsigned long long int
#define IO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define MOD 1000000007

using namespace std;

int main() {

  ll n,d,i,j,count=0;
  cin >> n >>d;

  ll a[n];
  for( i=0;i<n;i++)cin >> a[i];

  if(n==1)
  {
      cout<<"1"<<endl;
  }

  std::sort(a,a+n);
  for(i=0;i<n-1;i++)
  {
      for(j=i+1;j<n;j++)
      {
          if((a[j]-a[i])<=d)
          count+=2;

      }
  }
  cout<<count<<endl;

	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main() {
	long long int i,c=0,n,d;
	cin>>n>>d;
	vector<int> a(n);
	for(i=0;i<n;i++)
	{
		cin>>a[i];
	}
	sort(a.begin(),a.end());
	for(i=0;i<n-1;i++)
	{
		if((a[i+1]-a[i])<=d)
			c+=2;
	}
	cout<<c;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main() {
	long long int i,j,c=0,n,e;
	cin>>n>>e;
	vector<int> a(n);
	for(i=0;i<n;i++)
	{
		cin>>a[i];
	}
	sort(a.begin(),a.end());
	for(i=0;i<n-1;i++)
	{
		for(j=i+1;j<n;j++)
		{
			if((a[j]-a[i])<=e)
				c+=2;
		}
	}
	cout<<c;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main() {
	long long int i,j,c=0,n,d;
	cin>>n>>d;
	vector<int> a(n);
	for(i=0;i<n;i++)
	{
		cin>>a[i];
	}
	sort(a.begin(),a.end());
	for(i=0;i<n-1;i++)
	{
		for(j=i+1;j<n;j++)
		{
			if((a[j]-a[i])<=d)
				c+=2;
		}
	}
	cout<<c;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,d,c=0,i,j;
    cin>>n>>d;
    int a[n+5];
    for(i=0; i<n; i++)
        cin>>a[i];
    sort(a,a+n);
    for(i=0; i<n; i++)
    {
        for(j=i+1; j<n; j++)
            if(a[j]-a[i]>d)break;
            c++;
    }
    cout<<c*2<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,d,c=0,i,j;
    cin>>n>>d;
    int a[n+5];
    for(i=0; i<n; i++)
        cin>>a[i];
    sort(a,a+n);
    for(i=0; i<n; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(a[j]-a[i]>d)break;
            c++;
        }
    }
    cout<<c*2<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,d,c=0,i,j;
    cin>>n>>d;
    long long int a[n+5];
    for(i=0; i<n; i++)
        cin>>a[i];
    sort(a,a+n);
    for(i=0; i<n; i++)
    {
        for(j=i+1; j<n; j++)
            if(a[j]-a[i]>d)
            {
                c++;
                break;
            }
    }
    cout<<c*2<<endl;
    return 0;
}


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

int main(){

long long int n,d;
cin>>n>>d;
long int arr[n];

    for(int i = 0; i < n; i++){
        cin>>arr[i];
    }
    sort(arr, arr + n);

    long int c=0;
    for(int i = 0; i < n; i++){
        for(int j = i + 1; j < n; j++){
            if(arr[j] -arr[i]<= d){++c;}
            else{break;}
        }
    }
cout<<2*c<<endl;
}


#include<bits/stdc++.h>
using namespace std;int main(){
    long long int n,d;
    cin>>n>>d;
    int arr[n];
    for(int i=0;i<n;i++){
        cin>>arr[i];
    }
    sort(arr,arr+n);
    int c=0;
    for(int i=0;i<n-1;i++){
        if((arr[i+1]-arr[i])<=d){
            c++;
        }
    }
    cout<<2*c<<endl;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n, d;
    cin >> n>> d;
    int arr[n];
    for(int i = 0; i < n; i++){
        cin >> arr[i];
    }
    int count = 0;
    sort(arr, arr+n);
    for(int i = 1; i < n; i++){
        if(arr[i] - arr[i-1] <= d){
            count+= 2;
        }
    }

    cout << count <<  endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n, d;
    cin >> n>> d;
    int arr[n];
    for(int i = 0; i < n; i++){
        cin >> arr[i];
    }
    int count = 0;
    sort(arr, arr+n);
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(arr[j] - arr[i] <= d && i != j){
                count++;
            }
        }
    }

    cout << count <<  endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n, d;
    cin >> n>> d;
    int arr[n];
    for(int i = 0; i < n; i++){
        cin >> arr[i];
    }
    int count = 0;
    sort(arr, arr+n);
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(abs(arr[j] - arr[i]) <= d && i != j){
                count++;
            }
        }
    }

    cout << count <<  endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int TotalH,Diff,cnt=0,i,j;
    cin>>TotalH>>Diff;
    int A[TotalH];
    for(i=0;i<TotalH;i++)
    {
        cin>>A[i];
    }
    for(i=0;i<TotalH;i++)
    {
        for(j=0;j<TotalH;j++)
        {
            if(i!=j && abs(A[i]-A[j])<=Diff)
            {
                cnt++;
            }
        }
    }
    cout<<cnt<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int TotalH,Diff,cnt=0,i,j;
    cin>>TotalH>>Diff;
    int A[TotalH];
    for(i=0;i<TotalH;i++)
    {
        cin>>A[i];
    }
    sort(A,A+TotalH);
    for(i=0;i<TotalH;i++)
    {
        for(j=0;abs(A[i]-A[j])<=Diff;j++)
        {
            if(i!=j)
            {
                cnt++;
            }
        }
    }
    cout<<cnt<<endl;
    return 0;
}


#include<bits/stdc++.h>
uisng namespace std;
int main()
{
    int TotalH,Diff,cnt=0,i,j;
    cin>>TotalH>>Diff;
    int A[TotalH];
    for(i=0;i<TotalH;i++)
    {
        cin>>A[i];
    }
    for(i=0;i<TotalH;i++)
    {
        for(j=0;j<TotalH;j++)
        {
            if(i!=j && abs(A[i]-A[j])<=Diff)
            {
                cnt++;
            }
        }
    }
    cout<<cnt<<endl;
    return 0;
}


#include<bits/stdc++.h>
uisng namespace std;
int main()
{
    int TotalH,Diff,cnt=0,i,j;
    cin>>TotalH>>Diff;
    int A[TotalH];
    for(i=0;i<TotalH;i++)
    {
        cin>>A[i];
    }
    sort(A,A+TotalH);
    for(i=0;i<TotalH;i++)
    {
        for(j=0;abs(A[i]-A[j])<=Diff;j++)
        {
            if(i!=j)
            {
                cnt++;
            }
        }
    }
    cout<<cnt<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main () {

  int n, d;
  cin >> n >> d;

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

  sort(v.begin(), v.end());

  int ans = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      if (v[j] - v[i] > d) break;
      ans += 2;
    }
  }

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


#include <bits/stdc++.h>
#ifdef DEBUG
#include "_debug.cpp"
#include "callable.hpp/callable.hpp"
#endif
#define endl '\n'
using namespace std;
using ll = long long;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, d;
    cin >> n >> d;
    vector<int> a(n);
    for(int i = 0; i < n; i++) {
        cin >> a[i];
    }
    int ans = 0;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            if(i != j and abs(a[i] - a[j]) <= d) {
                ans++;
            }
        }
    }
    cout << ans << endl;
    return 0;
}


using namespace std;

int main () {

  int n, d;
  cin >> n >> d;

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

  sort(v.begin(), v.end());

  int ans = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      if (v[j] - v[i] > d) break;
      ans += 2;
    }
  }

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


//|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
//|                      Written By: Anmol_Tomer										              |
//|		  Date: 30-10-2020, at 19-05-41							                                |
//|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
//Compile: g++ -std=c++14 -O2 -Wall test.cpp -o test.out
//Debug: g++ -g -std=c++14 -O2 -Wall test.cpp -o test.out
// Problem Link:

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

typedef vector<int> vi;
typedef pair<int, int> pii;
#define endl "\n"
#define sd(val) scanf("%d", &val)
#define pd(val) printf("%d\n", val)
#define REP(a, b) for (int i = a; i < b; i++)
#define REP1(a, b) for (int i = a; i <= b; i++)
#define frr(i, n) for (int i = 0; i < (n); i++)
#define ss(val) scanf("%s", &val)
#define sull(val) scanf("%llu", &val)
#define sl(val) scanf("%lld", &val)
#define debug(val) printf("check%d\n", val)
#define all(v) v.begin(), v.end()
#define pb push_back
#define mp make_pair
#define ff first
#define sc second
#define ll long long
#define ull unsigned long long
#define MOD 1000000007
#define w(t)  \
    int t;    \
    cin >> t; \
    while (t--)
#define clr(val) memset(val, 0, sizeof(val))
#define what_is(x) cerr << #x << " is " << x << endl;
#define OJ                            \
    freopen("input.txt", "r", stdin); \
    freopen("output.txt", "w", stdout);
#define FIO                           \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);

// <=======END OF TEMPLATE=============>
void solve() ;
int main()
{
//	OJ;
	FIO;

    int n;
    cin >> n;
    ull d;
    cin >> d;
    int arr[n];
    for(int i = 0; i < n; i++)
    {
      cin >> arr[i];
    }
    sort(arr,arr+n);
    vi a;
    int ans = 0;
    for(int i = 0; i < n; i++)
    {
      if(abs(arr[i]-arr[i+1]) <= 10)
      {
        ans += 2;
      }
    }
    cout << ans;
    return 0;
}

//APPROACHING A QUESTION
//+ Think of binary search (max of min etc also if n<=2*10^5)
//+ Think of common dp states (Even if it appears as maths but constraints are small)
//+ Check constraints
//+ Keep calm and enjoy the question
//+ Be sure to remove MOD from binpow (if needed)
//+ Try bidirectional analysis for constructive questions
//+ If given some sequence try thinking of prefix sums
//+ If constraints are too large maybe its simple maths
//+ In questions with binary operations think of bits independently and also the change pattern
//+ If two or more binary operations are given mostly there is a relation between them and an arithmatic operator


//|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
//|                      Written By: Anmol_Tomer										              |
//|		  Date: 30-10-2020, at 19-05-41							                                |
//|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
//Compile: g++ -std=c++14 -O2 -Wall test.cpp -o test.out
//Debug: g++ -g -std=c++14 -O2 -Wall test.cpp -o test.out
// Problem Link:

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

typedef vector<int> vi;
typedef pair<int, int> pii;
#define endl "\n"
#define sd(val) scanf("%d", &val)
#define pd(val) printf("%d\n", val)
#define REP(a, b) for (int i = a; i < b; i++)
#define REP1(a, b) for (int i = a; i <= b; i++)
#define frr(i, n) for (int i = 0; i < (n); i++)
#define ss(val) scanf("%s", &val)
#define sull(val) scanf("%llu", &val)
#define sl(val) scanf("%lld", &val)
#define debug(val) printf("check%d\n", val)
#define all(v) v.begin(), v.end()
#define pb push_back
#define mp make_pair
#define ff first
#define sc second
#define ll long long
#define ull unsigned long long
#define MOD 1000000007
#define w(t) \
  int t;     \
  cin >> t;  \
  while (t--)
#define clr(val) memset(val, 0, sizeof(val))
#define what_is(x) cerr << #x << " is " << x << endl;
#define OJ                          \
  freopen("input.txt", "r", stdin); \
  freopen("output.txt", "w", stdout);
#define FIO                         \
  ios_base::sync_with_stdio(false); \
  cin.tie(NULL);                    \
  cout.tie(NULL);

// <=======END OF TEMPLATE=============>
void solve();
int main()
{
  //	OJ;
  FIO;

  int n;
  cin >> n;
  ull d;
  cin >> d;
  int arr[n];
  for (int i = 0; i < n; i++)
  {
    cin >> arr[i];
  }
  sort(arr, arr + n);
  // vi a;
  int ans = 0;
  for (int i = 0; i < n - 1; i++)
  {
    for (int j = i + 1; j < n; j++)
    {
      if (abs(arr[i] - arr[j]) <= d)
      {
        // cout << arr[i] << " " << arr[j] << "\n";
        ans += 2;
      }
    }
  }
  cout << ans;
  return 0;
}

//APPROACHING A QUESTION
//+ Think of binary search (max of min etc also if n<=2*10^5)
//+ Think of common dp states (Even if it appears as maths but constraints are small)
//+ Check constraints
//+ Keep calm and enjoy the question
//+ Be sure to remove MOD from binpow (if needed)
//+ Try bidirectional analysis for constructive questions
//+ If given some sequence try thinking of prefix sums
//+ If constraints are too large maybe its simple maths
//+ In questions with binary operations think of bits independently and also the change pattern
//+ If two or more binary operations are given mostly there is a relation between them and an arithmatic operator


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

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    long int n,k;
    cin>>n>>k;
    int a[n];
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    int c;
    sort(a,a+n);
    for(int i=0;i<n;i++)
    {
        for(int j=i+1;j<n;j++)
        {
            if(abs(a[i]-a[j])<=k)
            c+=2;
        }
    }
    cout<<c;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{

  ios_base::sync_with_stdio(false);

  cin.tie(NULL);

   long int n,d;

cin >> n >> d;

 vector<int> a(n);

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

 cin >> a[i];
    }

sort(a.begin(), a.end());

 long int ans = 0;

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

 for(int j = i+1; j < n; ++j){

   if(abs(a[i]- a[j]) <= d){

   ++ans;

        }
}

    }

 cout << ans*2 << endl;

  return 0;

}


#include <bits/stdc++.h>

using namespace std;

int main()
{

  ios_base::sync_with_stdio(false);

  cin.tie(NULL);

   long int n,d;

cin >> n >> d;

 vector<int> a(n);

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

 cin >> a[i];
    }
    s
ort(a.begin(), a.end());

 long int ans = 0;

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

 for(int j = i+1; j < n; ++j){

   if(abs(a[i]- a[j]) <= d){

   ++ans;

        }
}

    }

 cout << ans*2 << endl;

  return 0;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,d;
    cin>>n>>d;
    int a[n];
    int c=0;
    for(int i=0 ; i<n ; i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    for(int j=0 ; j<n ; j++)
    {
        for(int i=j ; i<n-1 ; i++)
        {
            if(abs(a[j]-a[i+1]) <= d)
                c+=2;
        }
    }
    cout<<c;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,d;
    cin>>n>>d;
    int a[n];
    int c=0;
    for(int i=0 ; i<n ; i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    for(int i=0 ; i<n-1 ; i++)
    {
        if(a[i+1]-a[i] <= d)
            c+=2;
    }
    cout<<c;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,d;
    cin>>n>>d;
    int a[n];
    int c=0;
    for(int i=0 ; i<n ; i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    for(int j=0 ; j<n ; j++)
    {
        for(int i=0 ; i<n-1 ; i++)
        {
            if(abs(a[j]-a[i]) <= d)
                c+=1;
        }
        a[j]=0;
    }
    cout<<c;

}


#include <bits/stdc++.h>
using namespace std;
#define pb emplace_back
#define fi first
#define se second
#define mp make_pair
#define SZ(x) ((int)(x.size()))
#define FOI(i,a,n) for(int (i)=int(a);(i)<=int(n);++(i))
#define FOD(i,a,n) for(int (i)=int(a);(i)>=int(n);--(i))
#define IN(x,y) ((y).find((x))!=(y).end())
#define ALL(t) t.begin(),t.end()
#define MSET(tabl,i) memset(tabl, i, sizeof(tabl))
#define PSET(x,y) fixed<<setprecision(y)<<lf(x)
#define DBG(c) cout<<#c<<" = "<<c<<endl;
#define RTIME ((double)clock()/(double)CLOCKS_PER_SEC)

template<typename T,typename S>inline bool REMIN(T&a,const S&b){return a>b?a=b,1:0;}
template<typename T,typename S>inline bool REMAX(T&a,const S&b){return a<b?a=b,1:0;}

typedef long long ll;
typedef long double lf;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef vector<int> vi;
typedef vector<vi> vvi;

int bit(ll x, int pos){ return (x >> pos) & 1; }
ll power(ll base, ll exp, ll c = 1e9 + 7) { if(!exp) return 1; ll r = power(base, exp >> 1, c); r=(r*r)%c; if(exp&1) r=(r*base)%c; return r; }

ll T,N,M,K;
int a,b,c;
string s1,s2;

const lf PI = 2*acos(0);
const ll INF = 1e9 + 4;
const ll MOD = 1000000007;
const int NMAX = 3e5+5;

int ar[NMAX];
int main(){
	ios_base::sync_with_stdio(false); cin.tie(0);

	cin >> N >> M;
	FOI(i, 1, N) {
		cin >> ar[i];
		FOI(j, 1, i - 1) {
			if(abs(ar[i] - ar[j]) <= M) {
				a ++;
			}
		}
	}
	cout << a * 2;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb emplace_back
#define fi first
#define se second
#define mp make_pair
#define SZ(x) ((int)(x.size()))
#define FOI(i,a,n) for(int (i)=int(a);(i)<=int(n);++(i))
#define FOD(i,a,n) for(int (i)=int(a);(i)>=int(n);--(i))
#define IN(x,y) ((y).find((x))!=(y).end())
#define ALL(t) t.begin(),t.end()
#define MSET(tabl,i) memset(tabl, i, sizeof(tabl))
#define PSET(x,y) fixed<<setprecision(y)<<lf(x)
#define DBG(c) cout<<#c<<" = "<<c<<endl;
#define RTIME ((double)clock()/(double)CLOCKS_PER_SEC)

template<typename T,typename S>inline bool REMIN(T&a,const S&b){return a>b?a=b,1:0;}
template<typename T,typename S>inline bool REMAX(T&a,const S&b){return a<b?a=b,1:0;}

typedef long long ll;
typedef long double lf;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef vector<int> vi;
typedef vector<vi> vvi;

int bit(ll x, int pos){ return (x >> pos) & 1; }
ll power(ll base, ll exp, ll c = 1e9 + 7) { if(!exp) return 1; ll r = power(base, exp >> 1, c); r=(r*r)%c; if(exp&1) r=(r*base)%c; return r; }

ll T,N,M,K;
int a,b,c;
string s1,s2;

const lf PI = 2*acos(0);
const ll INF = 1e9 + 4;
const ll MOD = 1000000007;
const int NMAX = 3e5+5;

int ar[NMAX];
int main(){
	ios_base::sync_with_stdio(false); cin.tie(0);

	cin >> N >> M;
	FOI(i, 1, N) {
		cin >> ar[i];
		FOI(j, 1, i - 1) {
			if(abs(ar[i] - ar[j]) <= M)
				a ++;
		}
	}
	cout << a;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb emplace_back
#define fi first
#define se second
#define mp make_pair
#define SZ(x) ((int)(x.size()))
#define FOI(i,a,n) for(int (i)=int(a);(i)<=int(n);++(i))
#define FOD(i,a,n) for(int (i)=int(a);(i)>=int(n);--(i))
#define IN(x,y) ((y).find((x))!=(y).end())
#define ALL(t) t.begin(),t.end()
#define MSET(tabl,i) memset(tabl, i, sizeof(tabl))
#define PSET(x,y) fixed<<setprecision(y)<<lf(x)
#define DBG(c) cout<<#c<<" = "<<c<<endl;
#define RTIME ((double)clock()/(double)CLOCKS_PER_SEC)

template<typename T,typename S>inline bool REMIN(T&a,const S&b){return a>b?a=b,1:0;}
template<typename T,typename S>inline bool REMAX(T&a,const S&b){return a<b?a=b,1:0;}

typedef long long ll;
typedef long double lf;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef vector<int> vi;
typedef vector<vi> vvi;

int bit(ll x, int pos){ return (x >> pos) & 1; }
ll power(ll base, ll exp, ll c = 1e9 + 7) { if(!exp) return 1; ll r = power(base, exp >> 1, c); r=(r*r)%c; if(exp&1) r=(r*base)%c; return r; }

ll T,N,M,K;
int a,b,c;
string s1,s2;

const lf PI = 2*acos(0);
const ll INF = 1e9 + 4;
const ll MOD = 1000000007;
const int NMAX = 3e5+5;

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

	cin >> N >> M;
	FOI(i, 1, N) {
		cin >> ar[i];
		FOI(j, 1, i - 1) {
			if(abs(ar[i] - ar[j]) <= M)
				a ++;
		}
	}
	cout << a;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb emplace_back
#define fi first
#define se second
#define mp make_pair
#define SZ(x) ((int)(x.size()))
#define FOI(i,a,n) for(int (i)=int(a);(i)<=int(n);++(i))
#define FOD(i,a,n) for(int (i)=int(a);(i)>=int(n);--(i))
#define IN(x,y) ((y).find((x))!=(y).end())
#define ALL(t) t.begin(),t.end()
#define MSET(tabl,i) memset(tabl, i, sizeof(tabl))
#define PSET(x,y) fixed<<setprecision(y)<<lf(x)
#define DBG(c) cout<<#c<<" = "<<c<<endl;
#define RTIME ((double)clock()/(double)CLOCKS_PER_SEC)

template<typename T,typename S>inline bool REMIN(T&a,const S&b){return a>b?a=b,1:0;}
template<typename T,typename S>inline bool REMAX(T&a,const S&b){return a<b?a=b,1:0;}

typedef long long ll;
typedef long double lf;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef vector<int> vi;
typedef vector<vi> vvi;

int bit(ll x, int pos){ return (x >> pos) & 1; }
ll power(ll base, ll exp, ll c = 1e9 + 7) { if(!exp) return 1; ll r = power(base, exp >> 1, c); r=(r*r)%c; if(exp&1) r=(r*base)%c; return r; }

ll T,N,M,K;
int a,b,c;
string s1,s2;

const lf PI = 2*acos(0);
const ll INF = 1e9 + 4;
const ll MOD = 1000000007;
const int NMAX = 3e5+5;

int ar[NMAX];
int main(){
	ios_base::sync_with_stdio(false); cin.tie(0);

	cin >> N >> M;
	FOI(i, 1, N) {
		cin >> ar[i];
		FOI(j, 1, i - 1) {
			if(abs(ar[i] - ar[j]) <= M) {
				DBG(i)
				DBG(j)
				a ++;
			}
		}
	}
	cout << a * 2;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,d,i,j,sum=0;
    cin>>n>>d;
    int a[n*n];
    for(i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);

    for(i=0;i<n;i++)
    {
        j=1;
        while((a[i+j]-a[i])<=d)
        {
            sum++;
            j++;
        }
    }
    cout<<sum*2;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,d,i,j,sum=0;
    cin>>n>>d;
    int a[n];
    for(i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);
    for(i=0;i<n;i++)
    {
        j=1;
        while((a[i+j]-a[i])<=d)
        {
            sum++;
            j++;
        }
    }
    cout<<sum*2;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,d,i,j,sum=0;
    cin>>n>>d;
    int a[n*n];
    for(i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);
    for(i=0;i<n;i++)
        cout<<a[i]<<' ';
        cout<<endl;
    for(i=0;i<n;i++)
    {
        j=1;
        while((a[i+j]-a[i])<=d)
        {
            sum++;
            j++;
        }
    }
    cout<<sum*2;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,d,i,j,sum=0;
    cin>>n>>d;
    int a[n];
    for(i=0;i<n;i++)
        cin>>a[i];
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if(i!=j&&abs(a[j]-a[i])<=d)
                sum++;
        }
    }
    cout<<sum;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,d,i,j,sum=0;
    cin>>n>>d;
    int a[n];
    for(i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);
    int b[1001]={0};
    for(i=0;i<n;i++)
        b[i]=a[i];
    for(i=0;i<n;i++)
    {
        j=1;
        while((b[i+j]-b[i])<=d)
        {
            sum++;
            j++;
        }
    }
    cout<<sum*2;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,d,i,j,sum=0,a[1001]={0};
    cin>>n>>d;
    for(i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);

    for(i=0;i<n;i++)
    {
        j=1;
        while((a[i+j]-a[i])<=d)
        {
            sum++;
            j++;
        }
    }
    cout<<sum*2;
}


#include <bits/stdc++.h>

const int N=1e5+5, M=1e5+5, oo=0x3f3f3f3f ;

using namespace std;

int main()
{
//32A REconnaissance
    int n,d,cnt{};
    cin >>n>>d;
    int arr[n];
    for(int i=0; i<n; i++)
    {
        cin>> arr[i];

    }
    sort(arr,arr+n);
    for(int i=0; i<n; i++)
    {
        if(abs(arr[i]-arr[i+1])<=d)
            cnt+=2;
    }
    cout << cnt<<endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,d,cnt{};
	cin>>n>>d;
	int arr[i];
	for(int i=0;i<n;i++)
	cin>> arr[i];
	sort(arr,arr+n);
	for(int i=0;i<n;i++)
	{
		if(abs(arr[i]-arr[i+1]<=d)
		cnt+=2;
	}
	cout << cnt << endl;

}


#include <bits/stdc++.h>

const int N=1e5+5, M=1e5+5, oo=0x3f3f3f3f ;

using namespace std;

int main()
{
//32A REconnaissance
    int n,d,cnt{};
    cin >>n>>d;
    int arr[n];
    for(int i=0; i<n; i++)
        cin>> arr[i];
    for(int i=0; i<n; i++)
        for(int j=i+1;j<n;j++)
    {
        if(abs(arr[i]-arr[j])<=d)
            cnt+=2;
    }
    cout << cnt<<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,d,cnt{};
    cin>>n>>d;
    int arr[n];
    for(int i=0;i<n;i++)
    cin>> arr[i];
    sort(arr,arr+n);
    for(int i=0i<n;i++)
    {
        if(abs(arr[i]-arr[i+1])<=d)
        cnt+=2;

    }
    cout << cnt<< endl;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
   int a,d;
   cin>>a>>d;
   int x[a];
   int z=0;
   for(int i=0;i<a;i++)
   {
       cin>>x[i];
   }
   for(int i=0;i<a;i++)
   {
       for(int j=0;j<a;j++)
       {
           if(i!=j)
           {
               if(abs(x[i]-x[j])<=10)
               {
                 z++;
               }
           }
       }
   }
   cout<<z<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
   int a,d;
   cin>>a>>d;
   int x[a];
   int z=0;
   for(int i=0;i<a;i++)
   {
       cin>>x[i];
   }
   for(int i=0;i<a;i++)
   {
       for(int j=0;j<a;j++)
       {
           if(i!=j)
           {
               if(abs(x[i]-x[j])<=10)
               {
                 z+=2;
               }
           }
       }
   }
   cout<<z<<endl;
    return 0;
}