package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
	"sort"
)

// github.com/EndlessCheng/codeforces-go
func CF1513F(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}
	max := func(a, b int) int {
		if a > b {
			return a
		}
		return b
	}

	var n, d int
	Fscan(in, &n)
	type pair struct{ x, y, t int }
	a := make([]pair, n)
	for i := range a {
		Fscan(in, &a[i].x)
	}
	ans := int64(0)
	for i := range a {
		Fscan(in, &a[i].y)
		if a[i].x > a[i].y {
			a[i] = pair{a[i].y, a[i].x, 1}
		}
		ans += int64(a[i].y - a[i].x)
	}
	sort.Slice(a, func(i, j int) bool { a, b := a[i], a[j]; return a.x < b.x || a.x == b.x && a.y > b.y })
	mx := [2]int{}
	for _, p := range a {
		mx[p.t] = max(mx[p.t], p.y)
		d = max(d, min(p.y, mx[p.t^1])-p.x)
	}
	Fprint(out, ans-int64(d*2))
}
func main() { CF1513F(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"os"
	"sort"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	var n, d int
	Fscan(in, &n)
	type pair struct{ x, y, t int }
	a := make([]pair, n)
	for i := range a {
		Fscan(in, &a[i].x)
	}
	ans := int64(0)
	for i := range a {
		Fscan(in, &a[i].y)
		if a[i].x > a[i].y {
			a[i].t = 1
			a[i].x, a[i].y = a[i].y, a[i].x
		}
		ans += int64(a[i].y - a[i].x)
	}
	sort.Slice(a, func(i, j int) bool { a, b := a[i], a[j]; return a.x < b.x || a.x == b.x && a.y > b.y })
	mx := [2]int{}
	for _, p := range a {
		if p.y > mx[p.t] {
			mx[p.t] = p.y
		}
		if mx[p.t^1] < p.y {
			p.y = mx[p.t^1]
		}
		if p.y-p.x > d {
			d = p.y-p.x
		}
	}
	Print(ans - int64(d*2))
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
	"sort"
)

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type pair struct{ x, y int }

	var n int
	Fscan(in, &n)
	a := make([]pair, n)
	for i := range a {
		Fscan(in, &a[i].x)
	}
	for i := range a {
		Fscan(in, &a[i].y)
	}
	sum := int64(0)
	for _, p := range a {
		sum += int64(abs(p.x - p.y))
	}
	ans := sum
	sort.Slice(a, func(i, j int) bool { return a[i].x < a[j].x })

	mi, mx := 0, 0
	for i := 1; i < n; i++ {
		p := a[i]
		if p.x > p.y {
			q := a[mx]
			old := abs(q.x-q.y) + abs(p.x-p.y)
			new := abs(q.x-p.y) + abs(p.x-q.y)
			ans = min(ans, sum-int64(old)+int64(new))
		} else {
			q := a[mi]
			old := abs(q.x-q.y) + abs(p.x-p.y)
			new := abs(q.x-p.y) + abs(p.x-q.y)
			ans = min(ans, sum-int64(old)+int64(new))
		}
		if a[i].y < a[mi].y {
			mi = i
		}
		if a[i].y > a[mx].y {
			mx = i
		}
	}
	Fprintln(out, ans)
}

func main() { run(os.Stdin, os.Stdout) }

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}
func min(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}


package main

import (
	"bufio"
	"os"
	"fmt"
	"container/heap"
)

type Node struct {
	v     int
	depth int
}

type NodeHeap []*Node

func (h NodeHeap) Len() int {
	return len(h)
}

func (h NodeHeap) Less(i, j int) bool {
	return h[i].depth > h[j].depth
}

func (h NodeHeap) Swap(i, j int) {
	h[i], h[j] = h[j], h[i]
}

func (h *NodeHeap) Push(val interface{}) {
	*h = append(*h, val.(*Node))
}

func (h *NodeHeap) Pop() interface{} {
	old := *h
	n := len(old)
	item := old[n-1]
	*h = old[0 : n-1]
	return item
}

var tree [][]int
var depth []int
var parent []int
var childrenCount []int
var cut []bool
var nodes int
var leaves NodeHeap
var ans int

func initialize() {
	sz := nodes + 1
	tree = make([][]int, sz)
	depth = make([]int, sz)
	parent = make([]int, sz)
	childrenCount = make([]int, sz)
	leaves = make(NodeHeap, 0)
	cut = make([]bool, sz)
	heap.Init(&leaves)
}

func readTree() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d\n", &nodes)

	initialize()

	for i := 0; i < nodes-1; i++ {
		var v1, v2 int
		fmt.Fscanf(buf, "%d %d\n", &v1, &v2)
		tree[v1] = append(tree[v1], v2)
		tree[v2] = append(tree[v2], v1)
	}
}

func dfs(v, d int) {
	depth[v] = d
	for _, child := range tree[v] {
		if parent[child] == 0 {
			parent[child] = v
			childrenCount[v]++
			dfs(child, d+1)
		}
	}
	if childrenCount[v] == 0 && d > 2 {
		heap.Push(&leaves, &Node{v, d})
	}
}

func main() {
	readTree()
	parent[1] = -1
	dfs(1, 0)
	for {
		if len(leaves) == 0 {
			break
		}
		top := heap.Pop(&leaves).(*Node)
		p1 := parent[top.v]
		if cut[p1] {
			continue
		}
		depth[p1] = 1
		cut[p1] = true
		childrenCount[p1] = 0
		for _, child := range tree[p1] {
			if depth[child] > 2 {
				depth[child] = 2
			}
		}
		p2 := parent[p1]
		if p2 != -1 {
			childrenCount[p2]--
			p3 := parent[p2]
			if p3 != -1 {
				if childrenCount[p2] == 0 {
					childrenCount[p3]--
				}
				if childrenCount[p2] == 0 && childrenCount[p3] == 0 && depth[p3] > 2 {
					heap.Push(&leaves, &Node{p3, depth[p3]})
				}
			}
		}
		ans++
	}
	fmt.Println(ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func toInt(buf []byte) (n int) {
	for _, v := range buf {
		n = n*10 + int(v-'0')
	}
	return
}

var tree = make(map[int][]int)
var ans = 0

func main() {
	var n int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	fmt.Sscanf(scanner.Text(), "%d", &n)

	var x, y int
	for i := 1; i < n; i++ {
		scanner.Scan()
		fmt.Sscanf(scanner.Text(), "%d %d", &x, &y)
		add(x, y)
		add(y, x)
	}
	dfs(1, 1)
	fmt.Println(ans)
}
func dfs(node, father int) int {
	dis := 2
	for _, to := range tree[node] {
		if to == father {
			continue
		}
		dis = min(dis, dfs(to, node))
	}
	if dis == 0 && father != 1 {
		ans++
	}
	return (dis + 1) % 3
}
func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}
func add(x, y int) {
	if _, ok := tree[x]; ok {
		tree[x] = append(tree[x], y)
	} else {
		tree[x] = []int{y}
	}
}

package main

import (
	"bufio"
	"os"
	"fmt"
	"container/heap"
)

type Node struct {
	v     int
	depth int
}

type NodeHeap []*Node

func (h NodeHeap) Len() int {
	return len(h)
}

func (h NodeHeap) Less(i, j int) bool {
	return h[i].depth > h[j].depth
}

func (h NodeHeap) Swap(i, j int) {
	h[i], h[j] = h[j], h[i]
}

func (h *NodeHeap) Push(val interface{}) {
	*h = append(*h, val.(*Node))
}

func (h *NodeHeap) Pop() interface{} {
	old := *h
	n := len(old)
	item := old[n-1]
	*h = old[0 : n-1]
	return item
}

var tree [][]int
var depth []int
var parent []int
var childrenCount []int
var cut []bool
var nodes int
var leaves NodeHeap
var ans int

func initialize() {
	sz := nodes + 1
	tree = make([][]int, sz)
	depth = make([]int, sz)
	parent = make([]int, sz)
	childrenCount = make([]int, sz)
	leaves = make(NodeHeap, 0)
	cut = make([]bool, sz)
	heap.Init(&leaves)
}

func readTree() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d\n", &nodes)

	initialize()

	for i := 0; i < nodes-1; i++ {
		var v1, v2 int
		fmt.Fscanf(buf, "%d %d\n", &v1, &v2)
		tree[v1] = append(tree[v1], v2)
		tree[v2] = append(tree[v2], v1)
	}
}

func dfs(v, d int) {
	depth[v] = d
	for _, child := range tree[v] {
		if parent[child] == 0 {
			parent[child] = v
			childrenCount[v]++
			dfs(child, d+1)
		}
	}
	if childrenCount[v] == 0 && d > 2 {
		heap.Push(&leaves, &Node{v, d})
	}
}

func main() {
	readTree()
	parent[1] = -1
	dfs(1, 0)
	for {
		if len(leaves) == 0 {
			break
		}
		top := heap.Pop(&leaves).(*Node)
		v, p := top.v, parent[top.v]
		if cut[p] {
			continue
		}
		if p != -1 {
			depth[p] = 1
			cut[p] = true
			for _, child := range tree[v] {
				if child != p {
					depth[child] = 2
					childrenCount[p]--
				}
			}
			p2 := parent[p]
			if p2 != -1 {
				childrenCount[p2]--
				if childrenCount[p2] == 0 && depth[p2] > 2 && !cut[p2] {
					heap.Push(&leaves, &Node{p2, depth[p2]})
				}
			}
		}
		ans++
	}
	fmt.Println(ans)
}

package main

import (
	"bufio"
	"os"
	"fmt"
	"container/heap"
)

type Node struct {
	v     int
	depth int
}

type NodeHeap []*Node

func (h NodeHeap) Len() int {
	return len(h)
}

func (h NodeHeap) Less(i, j int) bool {
	return h[i].depth > h[j].depth
}

func (h NodeHeap) Swap(i, j int) {
	h[i], h[j] = h[j], h[i]
}

func (h *NodeHeap) Push(val interface{}) {
	*h = append(*h, val.(*Node))
}

func (h *NodeHeap) Pop() interface{} {
	old := *h
	n := len(old)
	item := old[n-1]
	*h = old[0 : n-1]
	return item
}

var tree [][]int
var depth []int
var parent []int
var childrenCount []int
var cut []bool
var nodes int
var leaves NodeHeap
var ans int

func initialize() {
	sz := nodes + 1
	tree = make([][]int, sz)
	depth = make([]int, sz)
	parent = make([]int, sz)
	childrenCount = make([]int, sz)
	leaves = make(NodeHeap, 0)
	cut = make([]bool, sz)
	heap.Init(&leaves)
}

func readTree() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d\n", &nodes)

	initialize()

	for i := 0; i < nodes-1; i++ {
		var v1, v2 int
		fmt.Fscanf(buf, "%d %d\n", &v1, &v2)
		tree[v1] = append(tree[v1], v2)
		tree[v2] = append(tree[v2], v1)
	}
}

func dfs(v, d int) {
	depth[v] = d
	for _, child := range tree[v] {
		if parent[child] == 0 {
			parent[child] = v
			childrenCount[v]++
			dfs(child, d+1)
		}
	}
	if childrenCount[v] == 0 && d > 2 {
		heap.Push(&leaves, &Node{v, d})
	}
}

func main() {
	readTree()
	parent[1] = -1
	dfs(1, 0)
	for {
		if len(leaves) == 0 {
			break
		}
		top := heap.Pop(&leaves).(*Node)
		p1 := parent[top.v]
		if cut[p1] {
			continue
		}
		depth[p1] = 1
		cut[p1] = true
		childrenCount[p1] = 0
		for _, child := range tree[p1] {
			if depth[child] > 2 {
				depth[child] = 2
			}
		}
		p2 := parent[p1]
		if p2 != -1 {
			p3 := parent[p2]
			childrenCount[p2]--
			if p3 != -1 {
				if childrenCount[p2] == 0 {
					childrenCount[p3]--
				}
				if childrenCount[p2] == 0 && childrenCount[p3] == 1 && depth[p3] > 2 {
					heap.Push(&leaves, &Node{p3, depth[p3]})
				}
			}
		}
		ans++
	}
	fmt.Println(ans)
}

package main

import (
	"bufio"
	"os"
	"fmt"
	"container/heap"
)

type Node struct {
	v     int
	depth int
}

type NodeHeap []*Node

func (h NodeHeap) Len() int {
	return len(h)
}

func (h NodeHeap) Less(i, j int) bool {
	return h[i].depth > h[j].depth
}

func (h NodeHeap) Swap(i, j int) {
	h[i], h[j] = h[j], h[i]
}

func (h *NodeHeap) Push(val interface{}) {
	*h = append(*h, val.(*Node))
}

func (h *NodeHeap) Pop() interface{} {
	old := *h
	n := len(old)
	item := old[n-1]
	*h = old[0 : n-1]
	return item
}

var tree [][]int
var depth []int
var parent []int
var childrenCount []int
var cut []bool
var nodes int
var leaves NodeHeap
var ans int

func initialize() {
	sz := nodes + 1
	tree = make([][]int, sz)
	depth = make([]int, sz)
	parent = make([]int, sz)
	childrenCount = make([]int, sz)
	leaves = make(NodeHeap, 0)
	cut = make([]bool, sz)
	heap.Init(&leaves)
}

func readTree() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d\n", &nodes)

	initialize()

	for i := 0; i < nodes-1; i++ {
		var v1, v2 int
		fmt.Fscanf(buf, "%d %d\n", &v1, &v2)
		tree[v1] = append(tree[v1], v2)
		tree[v2] = append(tree[v2], v1)
	}
}

func dfs(v, d int) {
	depth[v] = d
	for _, child := range tree[v] {
		if parent[child] == 0 {
			parent[child] = v
			childrenCount[v]++
			dfs(child, d+1)
		}
	}
	if childrenCount[v] == 0 && d > 2 {
		heap.Push(&leaves, &Node{v, d})
	}
}

func main() {
	readTree()
	parent[1] = -1
	dfs(1, 0)
	for {
		if len(leaves) == 0 {
			break
		}
		top := heap.Pop(&leaves).(*Node)
		p := parent[top.v]
		if cut[p] {
			continue
		}
		if p != -1 {
			depth[p] = 1
			cut[p] = true
			for _, child := range tree[p] {
				if child != p {
					depth[child] = 2
					childrenCount[p]--
				}
			}
			p2 := parent[p]
			if p2 != -1 {
				p3 := parent[p2]
				childrenCount[p2]--
				if p3 != -1 && childrenCount[p2] == 0 && childrenCount[p3] == 1 && !cut[p3] && depth[p3] > 2 {
					heap.Push(&leaves, &Node{p3, depth[p3]})
				}
			}
		}
		ans++
	}
	fmt.Println(ans)
}

package main

import (
	"bufio"
	"os"
	"fmt"
	"container/heap"
)

type Node struct {
	v     int
	depth int
}

type NodeHeap []*Node

func (h NodeHeap) Len() int {
	return len(h)
}

func (h NodeHeap) Less(i, j int) bool {
	return h[i].depth > h[j].depth
}

func (h NodeHeap) Swap(i, j int) {
	h[i], h[j] = h[j], h[i]
}

func (h *NodeHeap) Push(val interface{}) {
	*h = append(*h, val.(*Node))
}

func (h *NodeHeap) Pop() interface{} {
	old := *h
	n := len(old)
	item := old[n-1]
	*h = old[0 : n-1]
	return item
}

var tree [][]int
var depth []int
var parent []int
var childrenCount []int
var cut []bool
var nodes int
var leaves NodeHeap
var ans int

func initialize() {
	sz := nodes + 1
	tree = make([][]int, sz)
	depth = make([]int, sz)
	parent = make([]int, sz)
	childrenCount = make([]int, sz)
	leaves = make(NodeHeap, 0)
	cut = make([]bool, sz)
	heap.Init(&leaves)
}

func readTree() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d\n", &nodes)

	initialize()

	for i := 0; i < nodes-1; i++ {
		var v1, v2 int
		fmt.Fscanf(buf, "%d %d\n", &v1, &v2)
		tree[v1] = append(tree[v1], v2)
		tree[v2] = append(tree[v2], v1)
	}
}

func dfs(v, d int) {
	depth[v] = d
	for _, child := range tree[v] {
		if parent[child] == 0 {
			parent[child] = v
			childrenCount[v]++
			dfs(child, d+1)
		}
	}
	if childrenCount[v] == 0 && d > 2 {
		heap.Push(&leaves, &Node{v, d})
	}
}

func main() {
	readTree()
	parent[1] = -1
	dfs(1, 0)
	for {
		if len(leaves) == 0 {
			break
		}
		top := heap.Pop(&leaves).(*Node)
		p := parent[top.v]
		if cut[p] {
			continue
		}
		if p != -1 {
			depth[p] = 1
			cut[p] = true
			for _, child := range tree[p] {
				if child != p {
					depth[child] = 2
					childrenCount[p]--
				}
			}
			p2 := parent[p]
			if p2 != -1 {
				p3 := parent[p2]
				childrenCount[p2]--
				if p3 != -1 && childrenCount[p2] == 0 && childrenCount[p3] == 1 && depth[p3] > 2 {
					heap.Push(&leaves, &Node{p3, depth[p3]})
				}
			}
		}
		ans++
	}
	fmt.Println(ans)
}


package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

var dirx = []int{-1, 1, 0, 0}
var diry = []int{0, 0, -1, 1}

type Coord struct {
	x int
	y int
}

func (c *Coord) move(d int) Coord {
	return Coord{x: c.x + dirx[d], y: c.y + diry[d]}
}

var queue = [1e6 + 100]Coord{}
var queueHead int
var queueTail int

func queuePush(p Coord) {
	queue[queueTail] = p
	queueTail++
}

func queuePop() Coord {
	var r = queue[queueHead]
	queueHead++
	return r
}

func solveCase() {
	var h, w int
	fmt.Fscan(in, &h, &w)
	w += 2
	h += 2
	var g [][]byte = make([][]byte, h)
	g[0] = []byte(strings.Repeat("#", w))
	g[h-1] = g[0]

	queueHead = 0
	queueTail = 0
	for i := 1; i+1 < h; i++ {
		var s string
		fmt.Fscan(in, &s)
		g[i] = []byte("#" + s + "#")
		for j := 0; j < w; j++ {
			if g[i][j] == 'L' {
				queuePush(Coord{x: i, y: j})
			}
		}
	}

	for queueHead < queueTail {
		var p = queuePop()
		for d1 := 0; d1 < 4; d1++ {
			var p1 = p.move(d1)
			if g[p1.x][p1.y] != '.' {
				continue
			}
			var count = 0
			for d2 := 0; d2 < 4; d2++ {
				var p2 = p1.move(d2)
				if g[p2.x][p2.y] == '.' {
					count++
				}
			}
			if count <= 1 {
				g[p1.x][p1.y] = '+'
				queuePush(p1)
			}
		}
	}

	for i := 1; i+1 < h; i++ {
		fmt.Fprintln(out, string(g[i][1:w-1]))
	}
}

func main() {
	var ntc int
	fmt.Fscan(in, &ntc)
	for t := 0; t < ntc; t++ {
		solveCase()
	}
	out.Flush()
}

package main

import (
	"bufio"
	"bytes"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1613E(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type pair struct{ x, y int }
	dir4 := []pair{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}

	var T, n, m int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &m)
		x, y := 0, -1
		g := make([][]byte, n)
		for i := range g {
			Fscan(in, &g[i])
			if y < 0 {
				x, y = i, bytes.IndexByte(g[i], 'L')
			}
		}
		for _, d := range dir4 {
			x, y := x+d.x, y+d.y
			if 0 <= x && x < n && 0 <= y && y < m && g[x][y] == '.' {
			o:
				for {
					dir := pair{}
					for _, d := range dir4 {
						if x, y := x+d.x, y+d.y; 0 <= x && x < n && 0 <= y && y < m && g[x][y] == '.' {
							if dir != (pair{}) {
								break o
							}
							dir = d
						}
					}
					g[x][y] = '+'
					if dir == (pair{}) {
						break
					}
					x += dir.x
					y += dir.y
				}
			}
		}
		for _, r := range g {
			Fprintf(out, "%s\n", r)
		}
	}
}

func main() { CF1613E(os.Stdin, os.Stdout) }


package main

import (
	"fmt"
	"strings"
)

var maxUnits = 50

func generateNames() []string {
	names := make([]string, maxUnits)
	for i := 0; i < maxUnits; i++ {
		var name string
		for j := 0; j < i/26; j++ {
			name += "a"
		}

		names[i] = strings.Title(name + string('a'+i%26))
	}

	return names
}

func main() {
	var n, k int
	fmt.Scanf("%d %d", &n, &k)
	splits := make([]string, n-k+1)

	for i := 0; i < n-k+1; i++ {
		fmt.Scan(&splits[i])
	}

	names := make([]string, n)
	generated := generateNames()

	for i := 0; i < k-1; i++ {
		names[i] = generated[i]
	}

	for i := k - 1; i < n; i++ {
		if splits[i-k+1] == "YES" {
			names[i] = generated[i]
		} else {
			names[i] = names[i-k+1]
		}
	}

	fmt.Println(strings.Join(names, " "))
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func str(now int) string {
	return "A" + string('a'+now/10) + string('a'+now%10)

}
func main() {
	sc := NewScanner()
	N, K := sc.NextInt(), sc.NextInt()

	ans := make([]string, N)
	now := 0
	ans[0] = str(now)

	for i := 1; i <= N-K+1; i++ {
		switch sc.Next() {
		case "NO":
			ans[i+K-2] = ans[i-1]
			fallthrough
		case "YES":
			for j := 0; j < K-1; j++ {
				if ans[i+j] == "" {
					now++
					ans[i+j] = str(now)
				}
			}
		}
	}

	for i := 0; i < N; i++ {
		if i > 0 {
			fmt.Print(" ")
		}
		fmt.Print(ans[i])
	}

}
func max(a int, b int) int {
	if a < b {
		return b
	}
	return a
}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func solve() {
	n := readInt()
	k := readInt()
	m := n - k + 1
	ok := make([]bool, m)
	for i := 0; i < m; i++ {
		ok[i] = readString() == "YES"
	}
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = i
	}
	name := []rune("Aaa")
	names := make([]string, n)
	for i := 0; i < n; i++ {
		names[i] = string(name)
		if name[2] == 'z' {
			name[1]++
			name[2] = 'a'
		} else {
			name[2]++
		}
	}
	for i := 0; i < m; i++ {
		if !ok[i] {
			res[i+k-1] = res[i]
		}
	}
	for i := 0; i < n; i++ {
		if i > 0 {
			fmt.Printf(" ")
		}
		fmt.Printf(names[res[i]])
	}
	fmt.Println()
}

func dfs(a []rune, ok []bool, pos int, k int) {
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

func readString() string {
	scanner.Scan()
	return scanner.Text()
}

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func solve() {
	n := readInt()
	k := readInt()
	m := n - k + 1
	ok := make([]bool, m)
	for i := 0; i < m; i++ {
		ok[i] = readString() == "YES"
	}
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = i
	}
	for i := 0; i < m; i++ {
		if !ok[i] {
			if i+1 != m {
				if ok[i+1] {
					res[i] = res[i+1]
				} else {
					res[i+1] = res[i]
				}
			} else {
				res[len(res)-1] = res[len(res)-2]
			}
		}
	}
	prev := -1
	name := []rune("Aaa")
	for i := 0; i < n; i++ {
		if i > 0 {
			fmt.Printf(" ")
		}
		if prev != res[i] {
			prev = res[i]
			if name[2] == 'z' {
				name[1]++
				name[2] = 'a'
			} else {
				name[2]++
			}
		}
		fmt.Printf(string(name))
	}
	fmt.Println()
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

func readString() string {
	scanner.Scan()
	return scanner.Text()
}

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func solve() {
	n := readInt()
	k := readInt()
	m := n - k + 1
	ok := make([]bool, m)
	for i := 0; i < m; i++ {
		ok[i] = readString() == "YES"
	}
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = i + 1
	}
	for i := 0; i < m; i++ {
		if !ok[i] {
			if i+1 != m {
				if ok[i+1] {
					res[i] = res[i+1]
				} else {
					res[i+1] = res[i]
				}
			} else {
				res[len(res)-1] = res[len(res)-2]
			}
		}
	}
	prev := 0
	name := ""
	for i := 0; i < n; i++ {
		if prev != res[i] {
			prev = res[i]
			num := res[i]
			name = "A"
			for num > 0 {
				name += string('a' + num)
				num /= 26
			}
		}
		if i > 0 {
			fmt.Printf(" ")
		}
		fmt.Printf(name)
	}
	fmt.Println()
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

func readString() string {
	scanner.Scan()
	return scanner.Text()
}

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

package main

import (
	"fmt"
	"strings"
)

var maxUnits = 50

func generateNames() []string {
	names := make([]string, maxUnits)
	for i := 0; i < maxUnits; i++ {
		var name string
		for j := 0; j < i/26; j++ {
			name += "a"
		}

		names[i] = name + string('a'+i%26)
	}

	return names
}

func main() {
	var n, k int
	fmt.Scanf("%d %d", &n, &k)
	splits := make([]string, n-k+1)

	for i := 0; i < n-k+1; i++ {
		fmt.Scan(&splits[i])
	}

	names := make([]string, n)
	generated := generateNames()

	for i := 0; i < k-1; i++ {
		names[i] = generated[i]
	}

	for i := k - 1; i < n; i++ {
		if splits[i-k+1] == "YES" {
			names[i] = generated[i]
		} else {
			names[i] = names[i-k+1]
		}
	}

	fmt.Println(strings.Join(names, " "))
}


package main

import (
	"bufio"
	"fmt"
	"os"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

const MIN = -(1 << 31)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }

func main() {
	defer writer.Flush()
	var n, h, l, r int
	scanf("%d %d %d %d\n", &n, &h, &l, &r)
	hour := make([]int, n)
	for i := range hour {
		scanf("%d", &hour[i])
	}
	solve(n, h, l, r, hour)
}

func solve(n, h, l, r int, hour []int) {
	dp := make([]int, h)
	for i := range dp {
		dp[i] = MIN
	}
	dp[0] = 0
	for _, hr := range hour {
		next := make([]int, h)
		for i := range next {
			next[i] = MIN
		}
		for i := range dp {
			h1, h2 := (i+hr)%h, (i+hr-1)%h
			v1, v2 := 0, 0
			if good(h1, l, r) {
				v1 = 1
			}
			if good(h2, l, r) {
				v2 = 1
			}
			next[h1] = max(next[h1], dp[i]+v1)
			next[h2] = max(next[h2], dp[i]+v2)
		}
		dp = next
	}
	res := 0
	for _, n := range dp {
		res = max(res, n)
	}
	printf("%d\n", res)
}

func good(h, l, r int) bool {
	return h >= l && h <= r
}

func pow(a, b int) int {
	p := 1
	for b > 0 {
		if b&1 != 0 {
			p *= a
		}
		a, b = a*a, b>>1
	}
	return p
}

func powmod(a, b, m int) int {
	a = a % m
	p := 1 % m
	for b > 0 {
		if b&1 != 0 {
			p = (p * a) % m
		}
		a, b = (a*a)%m, b>>1
	}
	return p
}

func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n, h, l, r int
	fmt.Fscan(reader, &n, &h, &l, &r)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i])
	}
	rls := solve(h, l, r, a)
	fmt.Println(rls)
}

func solve(h, l, r int, a []int) int {
	n := len(a)
	dp := make(map[int]int)
	dp[0] = 0
	for i := 0; i < n; i++ {
		newdp := make(map[int]int)
		for k, v := range dp {
			k1 := (k + a[i]) % h
			k2 := (k + a[i] - 1) % h
			if newdp[k1] <= v {
				newdp[k1] = v
			}
			if newdp[k2] <= v {
				newdp[k2] = v
			}
		}
		dp = newdp
		for k, v := range dp {
			if k >= l && k <= r {
				dp[k] = v + 1
			}
		}
	}
	rls := 0
	for _, v := range dp {
		if rls < v {
			rls = v
		}
	}
	return rls
}

/*
URL:
https://codeforces.com/problemset/problem/1324/E
*/

package main

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"math"
	"os"
	"strconv"
)

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

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

const (
	// General purpose
	MOD          = 1000000000 + 7
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

	// for dijkstra, prim, and so on
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

func init() {
	// bufio.ScanWords <---> bufio.ScanLines
	ReadString = newReadString(os.Stdin, bufio.ScanWords)
	stdout = bufio.NewWriter(os.Stdout)
}

var (
	n, h, l, r int
	A          []int

	dp [2000 + 10][2000 + 10]int
)

func main() {
	n, h, l, r = ReadInt4()
	A = ReadIntSlice(n)

	for i := 0; i <= n; i++ {
		for j := 0; j < h; j++ {
			dp[i][j] = -INF_BIT30
		}
	}
	dp[0][0] = 0

	for i := 0; i < n; i++ {
		a := A[i]
		for j := 0; j < h; j++ {
			nj := (j + a) % h
			if l <= nj && nj <= r {
				ChMax(&dp[i+1][nj], dp[i][j]+1)
			} else {
				ChMax(&dp[i+1][nj], dp[i][j])
			}

			nj = (j + a - 1) % h
			if l <= nj && nj <= r {
				ChMax(&dp[i+1][nj], dp[i][j]+1)
			} else {
				ChMax(&dp[i+1][nj], dp[i][j])
			}
		}
	}

	// for i := 0; i <= n; i++ {
	// 	PrintfDebug("dp[%d]: %v\n", i, dp[i][:h])
	// }

	fmt.Println(Max(dp[n][:h]...))
}

// Max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

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

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintfDebug is wrapper of fmt.Fprintf(os.Stderr, format, a...)
func PrintfDebug(format string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, format, a...)
}

// PrintfBufStdout is function for output strings to buffered os.Stdout.
// You may have to call stdout.Flush() finally.
func PrintfBufStdout(format string, a ...interface{}) {
	fmt.Fprintf(stdout, format, a...)
}

/*
URL:
https://codeforces.com/problemset/problem/1324/E
*/

package main

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"math"
	"os"
	"strconv"
)

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

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

const (
	// General purpose
	MOD          = 1000000000 + 7
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

	// for dijkstra, prim, and so on
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

func init() {
	// bufio.ScanWords <---> bufio.ScanLines
	ReadString = newReadString(os.Stdin, bufio.ScanWords)
	stdout = bufio.NewWriter(os.Stdout)
}

var (
	n, h, l, r int
	A          []int

	dp [2000 + 10][2000 + 10]int
)

func main() {
	n, h, l, r = ReadInt4()
	A = ReadIntSlice(n)

	for i := 0; i <= n; i++ {
		for j := 0; j < h; j++ {
			dp[i][j] = -INF_BIT30
		}
	}
	dp[0][0] = 0

	for i := 0; i < n; i++ {
		a := A[i]
		for j := 0; j < h; j++ {
			diff := 0
			// if l <= j && j <= r {
			// 	diff = 1
			// }

			nj := (j + a) % h
			if l <= nj && nj <= r {
				diff = 1
			}
			ChMax(&dp[i+1][nj], dp[i][j]+diff)
			nj = (j + a - 1) % h
			if l <= nj && nj <= r {
				diff = 1
			}
			ChMax(&dp[i+1][nj], dp[i][j]+diff)
		}
	}

	// for i := 0; i <= n; i++ {
	// 	PrintfDebug("dp[%d]: %v\n", i, dp[i][:h])
	// }

	fmt.Println(Max(dp[n][:h]...))
}

// Max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

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

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintfDebug is wrapper of fmt.Fprintf(os.Stderr, format, a...)
func PrintfDebug(format string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, format, a...)
}

// PrintfBufStdout is function for output strings to buffered os.Stdout.
// You may have to call stdout.Flush() finally.
func PrintfBufStdout(format string, a ...interface{}) {
	fmt.Fprintf(stdout, format, a...)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }

func main() {
	defer writer.Flush()
	var n, h, l, r int
	scanf("%d %d %d %d\n", &n, &h, &l, &r)
	hour := make([]int, n)
	for i := range hour {
		scanf("%d", &hour[i])
	}
	solve(n, h, l, r, hour)
}

func solve(n, h, l, r int, hour []int) {
	dp := make([]int, h)
	for _, hr := range hour {
		next := make([]int, h)
		for i := range dp {
			h1, h2 := (i+hr)%h, (i+hr-1)%h
			v1, v2 := 0, 0
			if good(h1, l, r) {
				v1 = 1
			}
			if good(h2, l, r) {
				v2 = 1
			}
			next[h1] = max(next[h1], dp[i]+v1)
			next[h2] = max(next[h2], dp[i]+v2)
		}
		dp = next
	}
	res := 0
	for _, n := range dp {
		res = max(res, n)
	}
	printf("%d\n", res)
}

func good(h, l, r int) bool {
	return h >= l && h <= r
}

func pow(a, b int) int {
	p := 1
	for b > 0 {
		if b&1 != 0 {
			p *= a
		}
		a, b = a*a, b>>1
	}
	return p
}

func powmod(a, b, m int) int {
	a = a % m
	p := 1 % m
	for b > 0 {
		if b&1 != 0 {
			p = (p * a) % m
		}
		a, b = (a*a)%m, b>>1
	}
	return p
}

func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}