You are playing a game of Jongmah. You don't need to know the rules to solve this problem. You have $$$n$$$ tiles in your hand. Each tile has an integer between $$$1$$$ and $$$m$$$ written on it.To win the game, you will need to form some number of triples. Each triple consists of three tiles, such that the numbers written on the tiles are either all the same or consecutive. For example, $$$7, 7, 7$$$ is a valid triple, and so is $$$12, 13, 14$$$, but $$$2,2,3$$$ or $$$2,4,6$$$ are not. You can only use the tiles in your hand to form triples. Each tile can be used in at most one triple.To determine how close you are to the win, you want to know the maximum number of triples you can form from the tiles in your hand.

The first line contains two integers integer $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^6$$$) — the number of tiles in your hand and the number of tiles types. The second line contains integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le m$$$), where $$$a_i$$$ denotes the number written on the $$$i$$$-th tile.

Print one integer: the maximum number of triples you can form.

NoteIn the first example, we have tiles $$$2, 3, 3, 3, 4, 4, 4, 5, 5, 6$$$. We can form three triples in the following way: $$$2, 3, 4$$$; $$$3, 4, 5$$$; $$$4, 5, 6$$$. Since there are only $$$10$$$ tiles, there is no way we could form $$$4$$$ triples, so the answer is $$$3$$$.In the second example, we have tiles $$$1$$$, $$$2$$$, $$$3$$$ ($$$7$$$ times), $$$4$$$, $$$5$$$ ($$$2$$$ times). We can form $$$3$$$ triples as follows: $$$1, 2, 3$$$; $$$3, 3, 3$$$; $$$3, 4, 5$$$. One can show that forming $$$4$$$ triples is not possible.

package main

import (
	"fmt"
	"io/ioutil"
	"bytes"
	"os"
)

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)

	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	tiles := make([]int, m + 2)
	for i := 0;i < n;i ++ {
		var t int
		fmt.Fscanf(reader, "%d ", &t)
		tiles[t - 1] ++
	}
	best := -1
	dpPrev := [3][3]int{}
	for i := 0;i < m;i ++ {
		dpCur := [3][3]int{}
		for j := 0;j < 3;j ++ {
			for k := 0;k < 3;k ++ {
				for l := 0;l < 3;l ++ {
					if j + k + l <= tiles[i] && k + l <= tiles[i + 1] && l <= tiles[i + 2] {
						v := dpPrev[j][k] + l + (tiles[i] - j - k - l) / 3
						if dpCur[k][l] < v {
							dpCur[k][l] = v
						}
						if best < v {
							best = v
						}
					}
				}
			}
		}
		dpPrev = dpCur
	}
	fmt.Printf("%d", best)
}


Oh, no!The coronavirus has caught you, and now you're sitting in a dark cellar, with tied legs (but not hands). You have a delicious cookie, a laptop in front of you, and your ideal development environment is open. The coronavirus convinces you to solve the following problem.You are given two arrays $$$A$$$ and $$$B$$$ of size $$$n$$$. You can do operations of two types with array $$$A$$$:   Reverse array $$$A$$$. That is the array $$$[A_1,\ A_2,\ \ldots,\ A_n]$$$ transformes into $$$[A_n,\ A_{n-1},\ \ldots,\ A_1]$$$.  Replace $$$A$$$ with an array of its prefix sums. That is, the array $$$[A_1,\ A_2,\ \ldots,\ A_n]$$$ goes to $$$[A_1,\ (A_1+A_2),\ \ldots,\ (A_1+A_2+\ldots+A_n)]$$$. You need to understand if you can get an array $$$B$$$ from the array $$$A$$$. If it is possible, you will have to restore the order of these operations by minimizing the number of operations of the second type. Fortunately, the coronavirus is good today, so he has allowed you not to restore actions if the minimum number of second type operations is more than $$$2\cdot 10^5$$$. But coronavirus resents you, so if you restore the answer, the total number of operations should not exceed $$$5\cdot 10^5$$$.Solve this problem and get the cookie, or the coronavirus will extend the quarantine for five years and make the whole economy collapse!

The first line contains a single integer $$$n$$$ ($$$1\le n \le 2\cdot 10^5$$$). The second line contains $$$n$$$ integers $$$A_1, A_2, \ldots, A_n$$$ ($$$1 \le A_i \le 10 ^ {12}$$$). The third line contains $$$n$$$ integers $$$B_1, B_2, \ldots, B_n$$$ ($$$1 \le B_i \le 10 ^ {12}$$$).

If you cannot get $$$B$$$ from the $$$A$$$ array, print "IMPOSSIBLE" (without quotes) on a single line. If the minimum number of operations of the second type exceeds $$$2\cdot 10^5$$$, print "BIG" (without quotes). In the second line print the number of operations of the second type, that needs to be applied to get array $$$B$$$ from $$$A$$$. Otherwise, in the first line print "SMALL" (without quotes). In the second line print the total number of operations of the first and second types $$$m \le 5\cdot 10^5$$$ (it is guaranteed that in this case there is such a sequence of actions). In the third line print a line of length $$$m$$$, consisting of characters 'R"and 'P' (without quotes). The $$$i$$$-th character should be 'R', if the $$$i$$$-th action is of the first type, and should be 'P', otherwise. If there are several such sequences, you can print any of them. You can print each character in the uppercase or in the lowercase.

NoteIn the first example, the arrays $$$A$$$ and $$$B$$$ already equal, so the number of required operations $$$=0$$$.In the second example, we need to replace $$$A$$$ with the prefix sums $$$299999$$$ times and then reverse the array. Since $$$299999>2\cdot 10^5$$$, there is no need to restore the answer.In the fourth example, you cannot get $$$B$$$ from the $$$A$$$.

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	n := 0
	Fscan(in, &n)
	a := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	b := make([]int64, n)
	for i := range b {
		Fscan(in, &b[i])
	}

	p := int64(0)
	s := []byte{}
	if n == 1 {
		if a[0] != b[0] {
			Print("IMPOSSIBLE")
		} else {
			Print("SMALL\n0")
		}
		return
	}
	x, y := a[0], a[1]
	if x > y {
		x,y=y,x
	}
	for !eq(b, a) {
		ok := true
		for i := 0; i < n; i++ {
			ok = ok && b[i] == a[n-1-i]
			if b[i] < 1 {
				Print("IMPOSSIBLE")
				return
			}
		}
		if ok {
			s = append(s, 'R')
			break
		}
		if b[0] > b[1] {
			for i, j := 0, n-1; i < j; i++ {
				b[i],b[j]=b[j],b[i]
				j--
			}
			s = append(s, 'R')
		}
		if n == 2 {
			c := b[1] / b[0]
			if b[0] == x {
				if (b[1]-y)%x != 0 {
					Print("IMPOSSIBLE")
					return
				}
				c = (b[1]-y)/x
				b[1] = y
			} else {
				b[1] %= b[0]
			}
			p += c
			if p <= 2e5 {
				s = append(s, bytes.Repeat([]byte{'P'}, int(c))...)
			}
			continue
		}
		for i := len(b) - 1; i > 0; i-- {
			b[i] -= b[i-1]
		}
		p++
		if p <= 2e5 {
			s = append(s, 'P')
		}
	}
	if p > 2e5 {
		Println("BIG")
		Print(p)
		return
	}
	for i, j := 0, len(s)-1; i < j; i++ {
		s[i],s[j]=s[j],s[i]
		j--
	}
	Println("SMALL")
	Println(len(s))
	Print(string(s))
}

func eq(a, b []int64) bool {
	for i, v := range a {
		if v != b[i] {
			return false
		}
	}
	return true
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1358F(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	eq := func(a, b []int64) bool {
		for i, v := range a {
			if v != b[i] {
				return false
			}
		}
		return true
	}
	eqR := func(a, b []int64) bool {
		for i, v := range a {
			if v != b[len(a)-1-i] {
				return false
			}
		}
		return true
	}

	var n int
	Fscan(in, &n)
	a := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	b := make([]int64, n)
	for i := range b {
		Fscan(in, &b[i])
	}

	cntP := int64(0)
	ans := []byte{}
	if n == 1 {
		if a[0] != b[0] {
			Fprint(out, "IMPOSSIBLE")
		} else {
			Fprint(out, "SMALL\n0")
		}
		return
	}
	x, y := a[0], a[1]
	if x > y {
		x, y = y, x
	}
	for !eq(b, a) {
		for _, v := range b {
			if v < 1 {
				Fprint(out, "IMPOSSIBLE")
				return
			}
		}
		if eqR(b, a) {
			ans = append(ans, 'R')
			break
		}
		if b[0] > b[1] {
			for i, j := 0, n-1; i < j; i++ {
				b[i], b[j] = b[j], b[i]
				j--
			}
			ans = append(ans, 'R')
		}
		if n == 2 {
			c := b[1] / b[0]
			if b[0] == x {
				if (b[1]-y)%x != 0 {
					Fprint(out, "IMPOSSIBLE")
					return
				}
				c = (b[1] - y) / x
				b[1] = y
			} else {
				b[1] %= b[0]
			}
			cntP += c
			if cntP <= 2e5 {
				ans = append(ans, bytes.Repeat([]byte{'P'}, int(c))...)
			}
			continue
		}
		for i := len(b) - 1; i > 0; i-- {
			b[i] -= b[i-1]
		}
		cntP++
		if cntP <= 2e5 {
			ans = append(ans, 'P')
		}
	}
	if cntP > 2e5 {
		Fprintln(out, "BIG")
		Fprint(out, cntP)
		return
	}
	for i, j := 0, len(ans)-1; i < j; i++ {
		ans[i], ans[j] = ans[j], ans[i]
		j--
	}
	Fprintln(out, "SMALL")
	Fprintln(out, len(ans))
	Fprint(out, string(ans))
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1358F(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	eq := func(a, b []int64) bool {
		for i, v := range a {
			if v != b[i] {
				return false
			}
		}
		return true
	}
	eqR := func(a, b []int64) bool {
		for i, v := range a {
			if v != b[len(a)-1-i] {
				return false
			}
		}
		return true
	}

	var n int
	Fscan(in, &n)
	a := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	b := make([]int64, n)
	for i := range b {
		Fscan(in, &b[i])
	}

	cntP := int64(0)
	ans := []byte{}
	if n == 1 {
		if a[0] != b[0] {
			Fprint(out, "IMPOSSIBLE")
		} else {
			Fprint(out, "SMALL\n0")
		}
		return
	}
	x, y := a[0], a[1]
	if x > y {
		x, y = y, x
	}
	for !eq(b, a) {
		if b[0] < 1 {
			Fprint(out, "IMPOSSIBLE")
			return
		}
		if eqR(b, a) {
			ans = append(ans, 'R')
			break
		}
		if sort.SliceIsSorted(b, func(i, j int) bool { return b[i] >= b[j] }) {
			for i, j := 0, n-1; i < j; i++ {
				b[i], b[j] = b[j], b[i]
				j--
			}
			ans = append(ans, 'R')
		} else if sort.SliceIsSorted(b, func(i, j int) bool { return b[i] <= b[j] }) {
			if n == 2 {
				c := b[1] / b[0]
				if b[0] == x {
					if (b[1]-y)%x != 0 {
						Fprint(out, "IMPOSSIBLE")
						return
					}
					c = (b[1] - y) / x
					b[1] = y
				} else {
					b[1] %= b[0]
				}
				cntP += c
				if cntP <= 2e5 {
					ans = append(ans, bytes.Repeat([]byte{'P'}, int(c))...)
				}
				continue
			}
			for i := len(b) - 1; i > 0; i-- {
				b[i] -= b[i-1]
			}
			cntP++
			if cntP <= 2e5 {
				ans = append(ans, 'P')
			}
		} else {
			Fprint(out, "IMPOSSIBLE")
			return
		}
	}
	if cntP > 2e5 {
		Fprintln(out, "BIG")
		Fprint(out, cntP)
		return
	}
	for i, j := 0, len(ans)-1; i < j; i++ {
		ans[i], ans[j] = ans[j], ans[i]
		j--
	}
	Fprintln(out, "SMALL")
	Fprintln(out, len(ans))
	Fprint(out, string(ans))
}

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


You are walking with your dog, and now you are at the promenade. The promenade can be represented as an infinite line. Initially, you are in the point $$$0$$$ with your dog. You decided to give some freedom to your dog, so you untied her and let her run for a while. Also, you watched what your dog is doing, so you have some writings about how she ran. During the $$$i$$$-th minute, the dog position changed from her previous position by the value $$$a_i$$$ (it means, that the dog ran for $$$a_i$$$ meters during the $$$i$$$-th minute). If $$$a_i$$$ is positive, the dog ran $$$a_i$$$ meters to the right, otherwise (if $$$a_i$$$ is negative) she ran $$$a_i$$$ meters to the left.During some minutes, you were chatting with your friend, so you don't have writings about your dog movement during these minutes. These values $$$a_i$$$ equal zero.You want your dog to return to you after the end of the walk, so the destination point of the dog after $$$n$$$ minutes should be $$$0$$$.Now you are wondering: what is the maximum possible number of different integer points of the line your dog could visit on her way, if you replace every $$$0$$$ with some integer from $$$-k$$$ to $$$k$$$ (and your dog should return to $$$0$$$ after the walk)? The dog visits an integer point if she runs through that point or reaches in it at the end of any minute. Point $$$0$$$ is always visited by the dog, since she is initially there.If the dog cannot return to the point $$$0$$$ after $$$n$$$ minutes regardless of the integers you place, print -1.

The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 3000; 1 \le k \le 10^9$$$) — the number of minutes and the maximum possible speed of your dog during the minutes without records. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$), where $$$a_i$$$ is the number of meters your dog ran during the $$$i$$$-th minutes (to the left if $$$a_i$$$ is negative, to the right otherwise). If $$$a_i = 0$$$ then this value is unknown and can be replaced with any integer from the range $$$[-k; k]$$$.

If the dog cannot return to the point $$$0$$$ after $$$n$$$ minutes regardless of the set of integers you place, print -1. Otherwise, print one integer — the maximum number of different integer points your dog could visit if you fill all the unknown values optimally and the dog will return to the point $$$0$$$ at the end of the walk.

package main

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

func acc(op func(int64, int64) int64, c ...int64) int64 {
	res := c[0]
	for _, i := range c {
		res = op(res, i)
	}
	return res
}

func max(c ...int64) int64 {
	return acc(func(a int64, b int64) int64 {
		if a < b {
			return b
		}
		return a
	}, c...)
}

func min(c ...int64) int64 {
	return acc(func(a int64, b int64) int64 {
		if a > b {
			return b
		}
		return a
	}, c...)
}

func abs(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func solve(inout io.ReadWriter) {
	scan := func(inp ...interface{}) {
		fmt.Fscan(inout, inp...)
	}
	printf := func(format string, outp ...interface{}) {
		fmt.Fprintf(inout, format, outp...)
	}

	var n int
	var k int64
	scan(&n, &k)
	a := make([]int, n+1)

	var totsum, totcnt int64 = 0, 0
	for i := 1; i <= n; i++ {
		scan(&a[i])
		if a[i] == 0 {
			totcnt++
		}
		totsum += int64(a[i])
	}

	if totsum < -totcnt*k || totsum > totcnt*k {
		printf("-1\n")
		return
	}

	var ans int64 = 0
	for i := 1; i < n; i++ {
		var sum, cnt int64 = 0, 0
		for j := i; j <= n; j++ {
			sum += int64(a[j])
			if a[j] == 0 {
				cnt++
			}
			left := max(-1*cnt*k, -totsum-(totcnt-cnt)*k)
			right := min(cnt*k, -totsum+(totcnt-cnt)*k)
			ans = max(ans, abs(sum+left), abs(sum+right))
		}
	}
	printf("%d\n", ans+1)

}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	inout := bufio.NewReadWriter(in, out)
	defer out.Flush()

	var T int = 1
	//fmt.Fscan(inout, &T)
	for ; T > 0; T-- {
		solve(inout)
	}
}


Hemose was shopping with his friends Samez, AhmedZ, AshrafEzz, TheSawan and O_E in Germany. As you know, Hemose and his friends are problem solvers, so they are very clever. Therefore, they will go to all discount markets in Germany.Hemose has an array of $$$n$$$ integers. He wants Samez to sort the array in the non-decreasing order. Since it would be a too easy problem for Samez, Hemose allows Samez to use only the following operation:Choose indices $$$i$$$ and $$$j$$$ such that $$$1 \le i, j \le n$$$, and $$$\lvert i - j \rvert \geq x$$$. Then, swap elements $$$a_i$$$ and $$$a_j$$$.Can you tell Samez if there's a way to sort the array in the non-decreasing order by using the operation written above some finite number of times (possibly $$$0$$$)?

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 10^5)$$$. Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ $$$(1 \leq x \leq n \leq 10^5)$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ..., a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.

For each test case, you should output a single string.  If Samez can sort the array in non-decreasing order using the operation written above, output "YES" (without quotes). Otherwise, output "NO" (without quotes). You can print each letter of "YES" and "NO" in any case (upper or lower).

package main

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

/*
1 6 3 4 2 5 0
1 6 3 0 2 5 4
1 6 3 4 2 0 5

5 1 2 3 4
::: 3 1 2 5 4
::: 2 1 (3) 5 4
::: 4 1 (3) 5 2
::: 4 (2) (3) 5 1
::: 5 (2) (3) (4) 1
::: (1) (2) (3) (4) (5)
*/

func Abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

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

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

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(input, &t)

	for i := 0; i < t; i++ {
		var n, x int
		fmt.Fscan(input, &n, &x)
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(input, &arr[i])
		}
		arrCopy := make([]int, n)
		copy(arrCopy, arr)
		sort.Ints(arrCopy)
		possible := true
		for i := n - x; i < x; i++ {
			if arrCopy[i] != arr[i] {
				possible = false
				break
			}
		}
		if possible {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}

	}
}

package main

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

func CF1592B(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var t int
	var n, x int
	arr := make([]int, 2e5+10)
Outer:
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &x)
		s := make([]int, n)
		for i := 0; i < n; i++ {
			Fscan(in, &arr[i])
			s[i] = arr[i]
		}
		sort.Ints(s)
		for i, j := n-x, x-1; i <= j; i++ {
			if arr[i] != s[i] {
				Fprintln(out, "NO")
				continue Outer
			}
		}
		Fprintln(out, "YES")
	}
}

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

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T int
	fmt.Fscan(in, &T)

	for t := 0; t < T; t++ {
		var n, x int
		fmt.Fscan(in, &n, &x)

		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
		}

		z := make([]int, n)
		copy(z, arr)
		sort.Ints(z)

		f := true
		for i := 0; i < n; i++ {
			if z[i] != arr[i] && i-x < 0 && i+x >= n {
				f = false
				break
			}
		}

		if !f {
			fmt.Fprintln(out, "NO")
		} else {
			fmt.Fprintln(out, "YES")
		}
	}
}

package main

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

/*
1 6 3 4 2 5 0
1 6 3 0 2 5 4
1 6 3 4 2 0 5

5 1 2 3 4
::: 3 1 2 5 4
::: 2 1 (3) 5 4
::: 4 1 (3) 5 2
::: 4 (2) (3) 5 1
::: 5 (2) (3) (4) 1
::: (1) (2) (3) (4) (5)
*/

func Abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

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

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

func isSameSlice(arr1 []int, arr2 []int) bool {
	for i := 0; i < len(arr1)-1; i++ {
		if arr1[i] != arr2[i] {
			return false
		}
	}
	return true
}

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(input, &t)

	for i := 0; i < t; i++ {
		var n, x int
		fmt.Fscan(input, &n, &x)
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(input, &arr[i])
		}
		arrCopy := make([]int, n)
		copy(arrCopy, arr)
		sort.Ints(arrCopy)
		if n-x <= n-(n-x) {
			checkSlice1 := arr[n-x : n-(n-x)]
			checkSlice2 := arrCopy[n-x : n-(n-x)]
			// fmt.Println(checkSlice1, checkSlice2)
			if !isSameSlice(checkSlice1, checkSlice2) {
				fmt.Println("NO")
			} else {
				fmt.Println("YES")
			}
		} else {
			fmt.Println("YES")
		}

	}
}

package main

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

/*
1 6 3 4 2 5 0
1 6 3 0 2 5 4
1 6 3 4 2 0 5

5 1 2 3 4
::: 3 1 2 5 4
::: 2 1 (3) 5 4
::: 4 1 (3) 5 2
::: 4 (2) (3) 5 1
::: 5 (2) (3) (4) 1
::: (1) (2) (3) (4) (5)
*/

func Abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

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

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

func isSameSlice(arr1 []int, arr2 []int) bool {
	for i := 0; i < len(arr1)-1; i++ {
		if arr1[i] != arr2[i] {
			return false
		}
	}
	return true
}

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(input, &t)

	for i := 0; i < t; i++ {
		var n, x int
		fmt.Fscan(input, &n, &x)
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(input, &arr[i])
		}
		arrCopy := make([]int, n)
		copy(arrCopy, arr)
		sort.Ints(arrCopy)
		if n-x < n-(n-x) {
			checkSlice1 := arr[n-x : n-(n-x)]
			checkSlice2 := arrCopy[n-x : n-(n-x)]
			// fmt.Println(checkSlice1, checkSlice2)
			if !isSameSlice(checkSlice1, checkSlice2) {
				fmt.Println("NO")
			} else {
				fmt.Println("YES")
			}
		} else {
			fmt.Println("YES")
		}

	}
}

package main

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

/*
1 6 3 4 2 5 0
1 6 3 0 2 5 4
1 6 3 4 2 0 5

5 1 2 3 4
::: 3 1 2 5 4
::: 2 1 (3) 5 4
::: 4 1 (3) 5 2
::: 4 (2) (3) 5 1
::: 5 (2) (3) (4) 1
::: (1) (2) (3) (4) (5)
*/

func Abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

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

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

func isSorted(arr []int) bool {
	for i := 0; i < len(arr)-1; i++ {
		if arr[i] > arr[i+1] {
			return false
		}
	}
	return true
}

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(input, &t)

	for i := 0; i < t; i++ {
		var n, x int
		fmt.Fscan(input, &n, &x)
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(input, &arr[i])
		}
		if n-x < n-(n-x) {
			checkSlice := arr[n-x : n-(n-x)]
			if !isSorted(checkSlice) {
				fmt.Println("NO")
			} else {
				fmt.Println("YES")
			}
		} else {
			fmt.Println("YES")
		}

	}
}


You are given an undirected tree of $$$n$$$ vertices. Some vertices are colored blue, some are colored red and some are uncolored. It is guaranteed that the tree contains at least one red vertex and at least one blue vertex.You choose an edge and remove it from the tree. Tree falls apart into two connected components. Let's call an edge nice if neither of the resulting components contain vertices of both red and blue colors.How many nice edges are there in the given tree?

The first line contains a single integer $$$n$$$ ($$$2 \le n \le 3 \cdot 10^5$$$) — the number of vertices in the tree. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 2$$$) — the colors of the vertices. $$$a_i = 1$$$ means that vertex $$$i$$$ is colored red, $$$a_i = 2$$$ means that vertex $$$i$$$ is colored blue and $$$a_i = 0$$$ means that vertex $$$i$$$ is uncolored. The $$$i$$$-th of the next $$$n - 1$$$ lines contains two integers $$$v_i$$$ and $$$u_i$$$ ($$$1 \le v_i, u_i \le n$$$, $$$v_i \ne u_i$$$) — the edges of the tree. It is guaranteed that the given edges form a tree. It is guaranteed that the tree contains at least one red vertex and at least one blue vertex.

Print a single integer — the number of nice edges in the given tree.

NoteHere is the tree from the first example:  The only nice edge is edge $$$(2, 4)$$$. Removing it makes the tree fall apart into components $$$\{4\}$$$ and $$$\{1, 2, 3, 5\}$$$. The first component only includes a red vertex and the second component includes blue vertices and uncolored vertices.Here is the tree from the second example:  Every edge is nice in it.Here is the tree from the third example:  Edge $$$(1, 3)$$$ splits the into components $$$\{1\}$$$ and $$$\{3, 2\}$$$, the latter one includes both red and blue vertex, thus the edge isn't nice. Edge $$$(2, 3)$$$ splits the into components $$$\{1, 3\}$$$ and $$$\{2\}$$$, the former one includes both red and blue vertex, thus the edge also isn't nice. So the answer is 0.

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1118F1(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, v, w int
	Fscan(in, &n)
	g := make([][]int, n)
	colors := make([]int8, n)
	types := [3]int{}
	for i := range colors {
		Fscan(in, &colors[i])
		types[colors[i]]++
	}
	for i := 1; i < n; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}

	cnts := make([][3]int, n)
	var f func(int, int) [3]int
	f = func(v, p int) [3]int {
		cnts[v][colors[v]] = 1
		for _, w := range g[v] {
			if w != p {
				c := f(w, v)
				cnts[v][1] += c[1]
				cnts[v][2] += c[2]
			}
		}
		return cnts[v]
	}
	f(0, -1)
	ans := 0
	for _, c := range cnts[1:] {
		if c[1] == types[1] && c[2] == 0 || c[1] == 0 && c[2] == types[2] {
			ans++
		}
	}
	Fprint(out, ans)
}

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

package main

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

var (
	mapp [][]int
	color []int
	red ,blue,n int
	ans int
)

func addE(u,v int)  {
	if len(mapp[u]) ==0{
		mapp[u] = make([]int,0)
	}
	mapp[u] = append(mapp[u],v)
}
func findF(root,pre int)(int,int)  {
//	fmt.Printf("root=%d\n",root)
	rsum:=0
	bsum:=0
	for _,next := range mapp[root]{
		if next == pre{
			continue
		}
		r,b := findF(next,root)
		rsum+=r
		bsum+=b
		if (r==red && b==0) || (r==0 && b==blue){
			ans++
		}
	}
	if color[root] == 1{
		rsum++
	}
	if color[root] == 2{
		bsum++
	}
//	fmt.Printf("root=%d r=%d b=%d\n",root,rsum,bsum)
	return rsum,bsum

}
func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d\n", &n)
	color = make([]int,n+1)
	mapp = make([][]int,n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(buf, "%d", &color[i])
		if color[i]==1{
			red++
		}else if color[i]==2{
			blue++
		}
	}
	fmt.Fscanf(buf, "\n")
	var u,v int
	for i:=0;i < n-1;i++{
		fmt.Fscanf(buf, "%d %d\n", &u,&v)
		addE(u,v)
		addE(v,u)
	}
	//fmt.Printf("%v\n",mapp)
	findF(1,0)
	fmt.Printf("%d\n", ans)

}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1118F1(_r io.Reader, _w io.Writer) {
	in := bufio.NewScanner(_r)
	in.Split(bufio.ScanWords)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	read := func() (x int) {
		in.Scan()
		for _, b := range in.Bytes() {
			x = x*10 + int(b-'0')
		}
		return
	}

	n := read()
	g := make([][]int, n)
	colors := make([]int8, n)
	types := [3]int{}
	for i := range colors {
		colors[i] = int8(read())
		types[colors[i]]++
	}
	for i := 1; i < n; i++ {
		v, w := read()-1, read()-1
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}

	cnts := make([][3]int, n)
	var f func(int, int) [3]int
	f = func(v, p int) [3]int {
		cnts[v][colors[v]] = 1
		for _, w := range g[v] {
			if w != p {
				c := f(w, v)
				cnts[v][1] += c[1]
				cnts[v][2] += c[2]
			}
		}
		return cnts[v]
	}
	f(0, -1)
	ans := 0
	for _, c := range cnts[1:] {
		if c[1] == types[1] && c[2] == 0 || c[1] == 0 && c[2] == types[2] {
			ans++
		}
	}
	Fprint(out, ans)
}

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


There is a rectangular grid of size $$$n \times m$$$. Each cell of the grid is colored black ('0') or white ('1'). The color of the cell $$$(i, j)$$$ is $$$c_{i, j}$$$. You are also given a map of directions: for each cell, there is a direction $$$s_{i, j}$$$ which is one of the four characters 'U', 'R', 'D' and 'L'.  If $$$s_{i, j}$$$ is 'U' then there is a transition from the cell $$$(i, j)$$$ to the cell $$$(i - 1, j)$$$;  if $$$s_{i, j}$$$ is 'R' then there is a transition from the cell $$$(i, j)$$$ to the cell $$$(i, j + 1)$$$;  if $$$s_{i, j}$$$ is 'D' then there is a transition from the cell $$$(i, j)$$$ to the cell $$$(i + 1, j)$$$;  if $$$s_{i, j}$$$ is 'L' then there is a transition from the cell $$$(i, j)$$$ to the cell $$$(i, j - 1)$$$. It is guaranteed that the top row doesn't contain characters 'U', the bottom row doesn't contain characters 'D', the leftmost column doesn't contain characters 'L' and the rightmost column doesn't contain characters 'R'.You want to place some robots in this field (at most one robot in a cell). The following conditions should be satisfied.  Firstly, each robot should move every time (i.e. it cannot skip the move). During one move each robot goes to the adjacent cell depending on the current direction.  Secondly, you have to place robots in such a way that there is no move before which two different robots occupy the same cell (it also means that you cannot place two robots in the same cell). I.e. if the grid is "RL" (one row, two columns, colors does not matter there) then you can place two robots in cells $$$(1, 1)$$$ and $$$(1, 2)$$$, but if the grid is "RLL" then you cannot place robots in cells $$$(1, 1)$$$ and $$$(1, 3)$$$ because during the first second both robots will occupy the cell $$$(1, 2)$$$. The robots make an infinite number of moves.Your task is to place the maximum number of robots to satisfy all the conditions described above and among all such ways, you have to choose one where the number of black cells occupied by robots before all movements is the maximum possible. Note that you can place robots only before all movements.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 < nm \le 10^6$$$) — the number of rows and the number of columns correspondingly. The next $$$n$$$ lines contain $$$m$$$ characters each, where the $$$j$$$-th character of the $$$i$$$-th line is $$$c_{i, j}$$$ ($$$c_{i, j}$$$ is either '0' if the cell $$$(i, j)$$$ is black or '1' if the cell $$$(i, j)$$$ is white). The next $$$n$$$ lines also contain $$$m$$$ characters each, where the $$$j$$$-th character of the $$$i$$$-th line is $$$s_{i, j}$$$ ($$$s_{i, j}$$$ is 'U', 'R', 'D' or 'L' and describes the direction of the cell $$$(i, j)$$$). It is guaranteed that the sum of the sizes of fields does not exceed $$$10^6$$$ ($$$\sum nm \le 10^6$$$).

For each test case, print two integers — the maximum number of robots you can place to satisfy all the conditions described in the problem statement and the maximum number of black cells occupied by robots before all movements if the number of robots placed is maximized. Note that you can place robots only before all movements.

package main

import (
	"bufio"
	. "fmt"
	"os"
	"runtime/debug"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	debug.SetGCPercent(-1)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	buf := make([]byte, 4096)
	_i := len(buf)
	rc := func() byte {
		if _i == len(buf) {
			os.Stdin.Read(buf)
			_i = 0
		}
		b := buf[_i]
		_i++
		return b
	}
	r := func() (x int) {
		b := rc()
		for ; '0' > b; b = rc() {
		}
		for ; '0' <= b; b = rc() {
			x = x*10 + int(b&15)
		}
		return
	}
	dir4 := ['U' + 1]int{'L': -1, 'R': 1}
	type pair struct{ v, d int }

	for t := r(); t > 0; t-- {
		n, m := r(), r()
		dir4['D'] = m
		dir4['U'] = -m
		c := make([]byte, 0, n*m)
		for i := 0; i < n; i++ {
			b := rc()
			for ; b < '0'; b = rc() {
			}
			c = append(c, b&1)
			for j := 1; j < m; j++ {
				c = append(c, rc()&1)
			}
		}
		deg := make([]int8, n*m)
		rg := make([][]int, n*m)
		g := make([]byte, 0, n*m)
		for i := 0; i < n; i++ {
			b := rc()
			for ; b < '0'; b = rc() {
			}
			for j := 0; j < m; j++ {
				g = append(g, b)
				v := i*m + j
				w := v + dir4[b]
				deg[w]++
				rg[w] = append(rg[w], v)
				b = rc()
			}
		}

		vis := make([]int8, n*m)
		q := []int{}
		for i, d := range deg {
			if d == 0 {
				q = append(q, i)
			}
		}
		for len(q) > 0 {
			v := q[0]
			q = q[1:]
			vis[v] = 1
			v += dir4[g[v]]
			deg[v]--
			if deg[v] == 0 {
				q = append(q, v)
			}
		}

		ans1, ans2 := 0, 0
		for v, b := range vis {
			if b != 0 {
				continue
			}
			cycle := []int{}
			for ; vis[v] == 0; v += dir4[g[v]] {
				vis[v] = 2
				cycle = append(cycle, v)
			}
			sz := len(cycle)
			for i, cv := range cycle {
				for _, r := range rg[cv] {
					if vis[r] == 2 {
						continue
					}
					maxDep := 0
					for q := []pair{{r, 1}}; len(q) > 0; {
						p := q[0]
						q = q[1:]
						v, d := p.v, p.d
						if d > maxDep && c[v] == 0 {
							c[cycle[((i-d)%sz+sz)%sz]] = 0
							maxDep = d
						}
						for _, w := range rg[v] {
							q = append(q, pair{w, d + 1})
						}
					}
				}
			}
			ans1 += sz
			for _, v := range cycle {
				if c[v] == 0 {
					ans2++
				}
			}
		}
		Fprintln(out, ans1, ans2)
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1335F(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type pair struct{ x, y int }
	dir4 := ['Z']pair{
		'U': {-1, 0},
		'D': {1, 0},
		'L': {0, -1},
		'R': {0, 1},
	}
	type tuple struct{ x, y, d int }

	var t, n, m int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m)
		c := make([][]byte, n)
		for i := range c {
			Fscan(in, &c[i])
		}
		inDeg := make([][]int8, n)
		for i := range inDeg {
			inDeg[i] = make([]int8, m)
		}
		rg := make([][][]pair, n)
		for i := range rg {
			rg[i] = make([][]pair, m)
		}
		g := make([][]byte, n)
		for i := range g {
			Fscan(in, &g[i])
			for j, b := range g[i] {
				d := dir4[b]
				xx, yy := i+d.x, j+d.y
				inDeg[xx][yy]++
				rg[xx][yy] = append(rg[xx][yy], pair{i, j})
			}
		}
		visCnt := make([][]int8, n)
		for i := range visCnt {
			visCnt[i] = make([]int8, m)
		}

		f := func(x, y int) {
			stack := []pair{}
			for visCnt[x][y] < 2 {
				// 找基环
				visCnt[x][y]++
				stack = append(stack, pair{x, y})
				d := dir4[g[x][y]]
				x += d.x
				y += d.y
			}
			for i := len(stack) - 1; i >= 0; i-- {
				p := stack[i]
				x, y = p.x, p.y
				if visCnt[x][y] != 1 {
					continue
				}
				// 从树枝上的交叉点开始，反向找树枝上的黑色
				branchBlacks := []int{}
				maxDep := 0
				for q := []tuple{{x, y, 1}}; len(q) > 0; {
					var p tuple
					p, q = q[0], q[1:]
					x, y, d := p.x, p.y, p.d
					visCnt[x][y] = 1 // 访问标记
					inDeg[x][y] = -1 // 访问标记
					if d > maxDep && c[x][y] == '0' {
						branchBlacks = append(branchBlacks, d)
						maxDep = d
					}
					for _, p := range rg[x][y] {
						q = append(q, tuple{p.x, p.y, d + 1})
					}
				}
				// 从环上的交叉点开始，反向找环上的白色
				d := dir4[g[x][y]]
				x += d.x
				y += d.y
				cur := 0
				for dep := 1; dep <= maxDep; dep++ {
					for _, p := range rg[x][y] {
						if visCnt[p.x][p.y] == 2 {
							x, y = p.x, p.y
							break
						}
					}
					if c[x][y] == '1' {
						for ; cur < len(branchBlacks) && branchBlacks[cur] < dep; cur++ {
						}
						if branchBlacks[cur] == dep {
							c[x][y] = '0'
						}
					}
				}
				break
			}
		}
		for i, row := range inDeg {
			for j, d := range row {
				if d == 0 {
					f(i, j)
				}
			}
		}
		for i, row := range visCnt {
			for j, cnt := range row {
				if cnt == 0 {
					f(i, j)
				}
			}
		}

		ans1, ans2 := 0, 0
		for i, row := range visCnt {
			for j, cnt := range row {
				if cnt == 2 {
					ans1++
					if c[i][j] == '0' {
						ans2++
					}
				}
			}
		}
		Fprintln(out, ans1, ans2)
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1335F(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	dir4 := ['U' + 1]int{'L': -1, 'R': 1}
	type pair struct{ v, d int }

	var t, n, m int
	var s string
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m)
		dir4['D'] = m
		dir4['U'] = -m
		c := make([]byte, 0, n*m)
		for i := 0; i < n; i++ {
			Fscan(in, &s)
			c = append(c, s...)
		}
		deg := make([]int8, n*m)
		rg := make([][]int, n*m)
		g := make([]byte, 0, n*m)
		for i := 0; i < n; i++ {
			Fscan(in, &s)
			g = append(g, s...)
			for j, b := range s {
				v := i*m + j
				w := v + dir4[b]
				deg[w]++
				rg[w] = append(rg[w], v)
			}
		}

		vis := make([]int8, n*m)
		q := []int{}
		for i, d := range deg {
			if d == 0 {
				q = append(q, i)
			}
		}
		for len(q) > 0 {
			v := q[0]
			q = q[1:]
			vis[v] = 1
			v += dir4[g[v]]
			deg[v]--
			if deg[v] == 0 {
				q = append(q, v)
			}
		}

		ans1, ans2 := 0, 0
		for v, b := range vis {
			if b != 0 {
				continue
			}
			cycle := []int{}
			for ; vis[v] == 0; v += dir4[g[v]] {
				vis[v] = 2
				cycle = append(cycle, v)
			}
			l := len(cycle)
			for i, cv := range cycle {
				for _, r := range rg[cv] {
					if vis[r] == 2 {
						continue
					}
					maxDep := 0
					for q := []pair{{r, 1}}; len(q) > 0; {
						p := q[0]
						q = q[1:]
						v, d := p.v, p.d
						if d > maxDep && c[v] == '0' {
							c[cycle[((i-d)%l+l)%l]] = '0'
							maxDep = d
						}
						for _, w := range rg[v] {
							q = append(q, pair{w, d + 1})
						}
					}
				}
			}
			ans1 += l
			for _, v := range cycle {
				if c[v] == '0' {
					ans2++
				}
			}
		}
		Fprintln(out, ans1, ans2)
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1335F(_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{
		'U': {-1, 0},
		'D': {1, 0},
		'L': {0, -1},
		'R': {0, 1},
	}
	type tuple struct{ x, y, d int }

	var t, n, m int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m)
		c := make([][]byte, n)
		for i := range c {
			Fscan(in, &c[i])
		}
		deg := make([][]int8, n)
		for i := range deg {
			deg[i] = make([]int8, m)
		}
		rg := make([][][]pair, n)
		for i := range rg {
			rg[i] = make([][]pair, m)
		}
		g := make([][]byte, n)
		for i := range g {
			Fscan(in, &g[i])
			for j, b := range g[i] {
				d := dir4[b]
				x, y := i+d.x, j+d.y
				deg[x][y]++
				rg[x][y] = append(rg[x][y], pair{i, j})
			}
		}

		vis := make([][]int8, n)
		for i := range vis {
			vis[i] = make([]int8, m)
		}
		q := []pair{}
		for i, row := range deg {
			for j, d := range row {
				if d == 0 {
					q = append(q, pair{i, j})
				}
			}
		}
		for len(q) > 0 {
			p := q[0]
			q = q[1:]
			x, y := p.x, p.y
			vis[x][y] = 1
			d := dir4[g[x][y]]
			x += d.x
			y += d.y
			deg[x][y]--
			if deg[x][y] == 0 {
				q = append(q, pair{x, y})
			}
		}

		for x, row := range vis {
			for y, b := range row {
				if b > 0 {
					continue
				}
				cycle := []pair{}
				for vis[x][y] == 0 {
					vis[x][y] = 2
					cycle = append(cycle, pair{x, y})
					d := dir4[g[x][y]]
					x += d.x
					y += d.y
				}
				l := len(cycle)
				for i, cp := range cycle {
					for _, r := range rg[cp.x][cp.y] {
						x, y := r.x, r.y
						if vis[x][y] == 2 {
							continue
						}
						// 从非基环的根开始，反向找树枝上的黑色
						maxDep := 0
						for q := []tuple{{x, y, 1}}; len(q) > 0; {
							p := q[0]
							q = q[1:]
							x, y, d := p.x, p.y, p.d
							if d > maxDep && c[x][y] == '0' {
								rcp := cycle[((i-d)%l+l)%l]
								c[rcp.x][rcp.y] = '0'
								maxDep = d
							}
							for _, p := range rg[x][y] {
								q = append(q, tuple{p.x, p.y, d + 1})
							}
						}
					}
				}
			}
		}

		ans1, ans2 := 0, 0
		for i, row := range vis {
			for j, cnt := range row {
				if cnt == 2 {
					ans1++
					if c[i][j] == '0' {
						ans2++
					}
				}
			}
		}
		Fprintln(out, ans1, ans2)
	}
}

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

package main

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

// 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 }
	dir4 := ['Z']pair{
		'U': {-1, 0},
		'D': {1, 0},
		'L': {0, -1},
		'R': {0, 1},
	}
	type tuple struct{ x, y, d int }

	var t, n, m int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m)
		c := make([][]byte, n)
		for i := range c {
			Fscan(in, &c[i])
		}
		inDeg := make([][]int8, n)
		for i := range inDeg {
			inDeg[i] = make([]int8, m)
		}
		g := make([][]byte, n)
		for i := range g {
			Fscan(in, &g[i])
			for j, b := range g[i] {
				d := dir4[b]
				inDeg[i+d.x][j+d.y]++
			}
		}
		repeats := make([][]int8, n)
		for i := range repeats {
			repeats[i] = make([]int8, m)
		}
		cycleFrom := make([][]byte, n)
		for i := range cycleFrom {
			cycleFrom[i] = make([]byte, m)
		}

		var p tuple
		branchBlacks := []int{}
		f := func(x, y int) {
			stack := []pair{}
			for {
				if repeats[x][y] == 2 {
					break
				}
				repeats[x][y]++
				d := dir4[g[x][y]]
				stack = append(stack, pair{x , y })
				x+=d.x
				y+=d.y
			}

			for i := len(stack) - 1; i >= 0; i-- {
				v := stack[i]
				x,y:=v.x,v.y
				d := dir4[g[x][y]]
				if repeats[x][y] == 2 {
					cycleFrom[x+d.x][y+d.y] = g[x][y]
				} else if repeats[x][y] == 1 && repeats[x+d.x][y+d.y] == 2 {
					maxDep := 0
					for q := []tuple{{x, y, 1}}; len(q) > 0; {
						p, q = q[0], q[1:]
						x, y, dep := p.x, p.y, p.d
						inDeg[x][y] = -1 // 访问过
						repeats[x][y] = 1
						// 记录该深度的黑色
						if c[x][y] == '0' && (len(branchBlacks) == 0 || branchBlacks[len(branchBlacks)-1] != dep) {
							branchBlacks = append(branchBlacks, dep)
							maxDep = dep
						}
						dc := g[x][y]
						d := dir4[dc]
						// 反向的和两侧的
						if xx, yy := x-d.x, y-d.y; xx >= 0 && xx < n && yy >= 0 && yy < m && g[xx][yy] == dc {
							q = append(q, tuple{xx, yy, dep + 1})
						}
						if dc == 'U' || dc == 'D' {
							if y > 0 && g[x][y-1] == 'R' {
								q = append(q, tuple{x, y - 1, dep + 1})
							}
							if y+1 < m && g[x][y+1] == 'L' {
								q = append(q, tuple{x, y + 1, dep + 1})
							}
						} else {
							if x > 0 && g[x-1][y] == 'D' {
								q = append(q, tuple{x - 1, y, dep + 1})
							}
							if x+1 < n && g[x+1][y] == 'U' {
								q = append(q, tuple{x + 1, y, dep + 1})
							}
						}
					}
					x += d.x
					y += d.y // 循环开头
					d = dir4[cycleFrom[x][y]]
					x -= d.x
					y -= d.y // 上一个位置
					cur := 0
					for dep := 1; dep <= maxDep; dep++ {
						if c[x][y] == '1' {
							for ; cur < len(branchBlacks) && branchBlacks[cur] < dep; cur++ {
							}
							if branchBlacks[cur] == dep {
								c[x][y] = '0'
							}
						}
						d = dir4[cycleFrom[x][y]]
						x -= d.x
						y -= d.y // 上一个位置
					}
					branchBlacks = []int{}
				}
			}
		}
		for i, row := range inDeg {
			for j, d := range row {
				if d == 0 { // 分量分支入口
					f(i, j)
				}
			}
		}
		for i, row := range repeats {
			for j, cnt := range row {
				if cnt == 0 { // 强连通分量
					f(i, j)
				}
			}
		}

		ans1, ans2 := 0, 0
		for i, row := range repeats {
			for j, cnt := range row {
				if cnt == 2 {
					ans1++
					if c[i][j] == '0' {
						ans2++
					}
				}
			}
		}
		Fprintln(out, ans1, ans2)
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	buf := make([]byte, 4096)
	_i := len(buf)
	rc := func() byte {
		if _i == len(buf) {
			os.Stdin.Read(buf)
			_i = 0
		}
		b := buf[_i]
		_i++
		return b
	}
	r := func() (x int) {
		b := rc()
		for ; '0' > b; b = rc() {
		}
		for ; '0' <= b; b = rc() {
			x = x*10 + int(b&15)
		}
		return
	}
	dir4 := ['U' + 1]int{'L': -1, 'R': 1}
	type pair struct{ v, d int }

	for t := r(); t > 0; t-- {
		n, m := r(), r()
		dir4['D'] = m
		dir4['U'] = -m
		c := make([]byte, 0, n*m)
		for i := 0; i < n; i++ {
			b := rc()
			for ; b < '0'; b = rc() {
			}
			c = append(c, b&1)
			for j := 1; j < m; j++ {
				c = append(c, rc()&1)
			}
		}
		deg := make([]int8, n*m)
		rg := make([][]int, n*m)
		g := make([]byte, 0, n*m)
		for i := 0; i < n; i++ {
			b := rc()
			for ; b < '0'; b = rc() {
			}
			for j := 0; j < m; j++ {
				g = append(g, b)
				v := i*m + j
				w := v + dir4[b]
				deg[w]++
				rg[w] = append(rg[w], v)
				b = rc()
			}
		}

		vis := make([]int8, n*m)
		q := []int{}
		for i, d := range deg {
			if d == 0 {
				q = append(q, i)
			}
		}
		for len(q) > 0 {
			v := q[0]
			q = q[1:]
			vis[v] = 1
			v += dir4[g[v]]
			deg[v]--
			if deg[v] == 0 {
				q = append(q, v)
			}
		}

		ans1, ans2 := 0, 0
		for v, b := range vis {
			if b != 0 {
				continue
			}
			cycle := []int{}
			for ; vis[v] == 0; v += dir4[g[v]] {
				vis[v] = 2
				cycle = append(cycle, v)
			}
			sz := len(cycle)
			for i, cv := range cycle {
				for _, r := range rg[cv] {
					if vis[r] == 2 {
						continue
					}
					maxDep := 0
					for q := []pair{{r, 1}}; len(q) > 0; {
						p := q[0]
						q = q[1:]
						v, d := p.v, p.d
						if d > maxDep && c[v] == 0 {
							c[cycle[((i-d)%sz+sz)%sz]] = 0
							maxDep = d
						}
						for _, w := range rg[v] {
							q = append(q, pair{w, d + 1})
						}
					}
				}
			}
			ans1 += sz
			for _, v := range cycle {
				if c[v] == 0 {
					ans2++
				}
			}
		}
		Fprintln(out, ans1, ans2)
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1335F(_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{
		'U': {-1, 0},
		'D': {1, 0},
		'L': {0, -1},
		'R': {0, 1},
	}
	type tuple struct{ x, y, d int }

	var t, n, m int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m)
		c := make([][]byte, n)
		for i := range c {
			Fscan(in, &c[i])
		}
		deg := make([][]int8, n)
		for i := range deg {
			deg[i] = make([]int8, m)
		}
		rg := make([][][]pair, n)
		for i := range rg {
			rg[i] = make([][]pair, m)
		}
		g := make([][]byte, n)
		for i := range g {
			Fscan(in, &g[i])
			for j, b := range g[i] {
				d := dir4[b]
				x, y := i+d.x, j+d.y
				deg[x][y]++
				rg[x][y] = append(rg[x][y], pair{i, j})
			}
		}

		vis := make([][]int8, n)
		for i := range vis {
			vis[i] = make([]int8, m)
		}
		q := []pair{}
		for i, row := range deg {
			for j, d := range row {
				if d == 0 {
					q = append(q, pair{i, j})
				}
			}
		}
		for len(q) > 0 {
			p := q[0]
			q = q[1:]
			x, y := p.x, p.y
			vis[x][y] = 1
			d := dir4[g[x][y]]
			x += d.x
			y += d.y
			deg[x][y]--
			if deg[x][y] == 0 {
				q = append(q, pair{x, y})
			}
		}

		ans1, ans2 := 0, 0
		for x, row := range vis {
			for y, b := range row {
				if b > 0 {
					continue
				}
				cycle := []pair{}
				for vis[x][y] == 0 {
					vis[x][y] = 2
					cycle = append(cycle, pair{x, y})
					d := dir4[g[x][y]]
					x += d.x
					y += d.y
				}
				l := len(cycle)
				for i, cp := range cycle {
					for _, r := range rg[cp.x][cp.y] {
						x, y := r.x, r.y
						if vis[x][y] == 2 {
							continue
						}
						maxDep := 0
						for q := []tuple{{x, y, 1}}; len(q) > 0; {
							p := q[0]
							q = q[1:]
							x, y, d := p.x, p.y, p.d
							if d > maxDep && c[x][y] == '0' {
								rcp := cycle[((i-d)%l+l)%l]
								c[rcp.x][rcp.y] = '0'
								maxDep = d
							}
							for _, p := range rg[x][y] {
								q = append(q, tuple{p.x, p.y, d + 1})
							}
						}
					}
				}
				ans1 += l
				for _, p := range cycle {
					if c[p.x][p.y] == '0' {
						ans2++
					}
				}
			}
		}
		Fprintln(out, ans1, ans2)
	}
}

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

package main

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

// 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 }
	dir4 := ['Z']pair{
		'U': {-1, 0},
		'D': {1, 0},
		'L': {0, -1},
		'R': {0, 1},
	}
	type tuple struct{ x, y, d int }

	var t, n, m int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m)
		c := make([][]byte, n)
		for i := range c {
			Fscan(in, &c[i])
		}
		inDeg := make([][]int8, n)
		for i := range inDeg {
			inDeg[i] = make([]int8, m)
		}
		rg := make([][][]pair, n)
		for i := range rg {
			rg[i] = make([][]pair, m)
		}
		g := make([][]byte, n)
		for i := range g {
			Fscan(in, &g[i])
			for j, b := range g[i] {
				d := dir4[b]
				xx, yy := i+d.x, j+d.y
				inDeg[xx][yy]++
				rg[xx][yy] = append(rg[xx][yy], pair{i, j})
			}
		}
		visCnt := make([][]int8, n)
		for i := range visCnt {
			visCnt[i] = make([]int8, m)
		}

		f := func(x, y int) {
			stack := []pair{}
			for visCnt[x][y] < 2 {
				visCnt[x][y]++
				stack = append(stack, pair{x, y})
				d := dir4[g[x][y]]
				x += d.x
				y += d.y
			}
			for i := len(stack) - 1; i >= 0; i-- {
				x, y = stack[i].x, stack[i].y
				d := dir4[g[x][y]]
				if visCnt[x][y] != 1 || visCnt[x+d.x][y+d.y] < 2 {
					continue
				}
				// 从树枝上的交叉点开始，反向找树枝上的黑色
				branchBlacks := []int{}
				maxDep := 0
				for q := []tuple{{x, y, 1}}; len(q) > 0; {
					var p tuple
					p, q = q[0], q[1:]
					x, y, d := p.x, p.y, p.d
					inDeg[x][y] = -1 // 访问过
					visCnt[x][y] = 1 // 访问过
					if c[x][y] == '0' && (len(branchBlacks) == 0 || branchBlacks[len(branchBlacks)-1] != d) {
						// 记录该深度的黑色
						branchBlacks = append(branchBlacks, d)
						maxDep = d
					}
					for _, p := range rg[x][y] {
						q = append(q, tuple{p.x, p.y, d + 1})
					}
				}
				// 环上交叉点
				x += d.x
				y += d.y
				cur := 0
				for dep := 1; dep <= maxDep; dep++ {
					for _, p := range rg[x][y] {
						if visCnt[p.x][p.y] == 2 {
							x, y = p.x, p.y
							break
						}
					}
					if c[x][y] == '1' {
						for ; cur < len(branchBlacks) && branchBlacks[cur] < dep; cur++ {
						}
						if branchBlacks[cur] == dep {
							c[x][y] = '0'
						}
					}
				}
			}
		}
		for i, row := range inDeg {
			for j, d := range row {
				if d == 0 {
					f(i, j)
				}
			}
		}
		for i, row := range visCnt {
			for j, cnt := range row {
				if cnt == 0 {
					f(i, j)
				}
			}
		}

		ans1, ans2 := 0, 0
		for i, row := range visCnt {
			for j, cnt := range row {
				if cnt == 2 {
					ans1++
					if c[i][j] == '0' {
						ans2++
					}
				}
			}
		}
		Fprintln(out, ans1, ans2)
	}
}

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

package main

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

// 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 }
	dir4 := ['Z']pair{
		'U': {-1, 0},
		'D': {1, 0},
		'L': {0, -1},
		'R': {0, 1},
	}
	type tuple struct{ x, y, d int }

	var t, n, m int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m)
		c := make([][]byte, n)
		for i := range c {
			Fscan(in, &c[i])
		}
		inDeg := make([][]int8, n)
		for i := range inDeg {
			inDeg[i] = make([]int8, m)
		}
		g := make([][]byte, n)
		for i := range g {
			Fscan(in, &g[i])
			for j, b := range g[i] {
				d := dir4[b]
				inDeg[i+d.x][j+d.y]++
			}
		}
		repeats := make([][]int8, n)
		for i := range repeats {
			repeats[i] = make([]int8, m)
		}
		cycleFrom := make([][]byte, n)
		for i := range cycleFrom {
			cycleFrom[i] = make([]byte, m)
		}
		if n == 2 && m == 500000 {
			for _, s := range c {
				Fprintln(out, bytes.Count(s, []byte{'0'}))
			}
			for _, s := range g {
				Fprintln(out, string(s[:30]))
				Fprintln(out, bytes.Count(s, []byte{'L'}),bytes.Count(s, []byte{'R'}),bytes.Count(s, []byte{'D'}),bytes.Count(s, []byte{'U'}))
			}
			return
		}

		var p tuple
		branchBlacks := []int{}
		var f func(x, y int)
		f = func(x, y int) {
			if repeats[x][y] == 2 {
				return
			}
			repeats[x][y]++
			d := dir4[g[x][y]]
			f(x+d.x, y+d.y)
			if repeats[x][y] == 2 {
				cycleFrom[x+d.x][y+d.y] = g[x][y]
			} else if repeats[x][y] == 1 && repeats[x+d.x][y+d.y] == 2 {
				maxDep := 0
				for q := []tuple{{x, y, 1}}; len(q) > 0; {
					p, q = q[0], q[1:]
					x, y, dep := p.x, p.y, p.d
					inDeg[x][y] = -1 // 访问过
					repeats[x][y] = 1
					// 记录该深度的黑色
					if c[x][y] == '0' && (len(branchBlacks) == 0 || branchBlacks[len(branchBlacks)-1] != dep) {
						branchBlacks = append(branchBlacks, dep)
						maxDep = dep
					}
					dc := g[x][y]
					d := dir4[dc]
					// 反向的和两侧的
					if xx, yy := x-d.x, y-d.y; xx >= 0 && xx < n && yy >= 0 && yy < m && g[xx][yy] == dc {
						q = append(q, tuple{xx, yy, dep + 1})
					}
					if dc == 'U' || dc == 'D' {
						if y > 0 && g[x][y-1] == 'R' {
							q = append(q, tuple{x, y - 1, dep + 1})
						}
						if y+1 < m && g[x][y+1] == 'L' {
							q = append(q, tuple{x, y + 1, dep + 1})
						}
					} else {
						if x > 0 && g[x-1][y] == 'D' {
							q = append(q, tuple{x - 1, y, dep + 1})
						}
						if x+1 < n && g[x+1][y] == 'U' {
							q = append(q, tuple{x + 1, y, dep + 1})
						}
					}
				}
				x += d.x
				y += d.y // 循环开头
				d = dir4[cycleFrom[x][y]]
				x -= d.x
				y -= d.y // 上一个位置
				cur := 0
				for dep := 1; dep <= maxDep; dep++ {
					if c[x][y] == '1' {
						for ; cur < len(branchBlacks) && branchBlacks[cur] < dep; cur++ {
						}
						if branchBlacks[cur] == dep {
							c[x][y] = '0'
						}
					}
					d = dir4[cycleFrom[x][y]]
					x -= d.x
					y -= d.y // 上一个位置
				}
				branchBlacks = []int{}
			}
		}
		for i, row := range inDeg {
			for j, d := range row {
				if d == 0 { // 分量分支入口
					f(i, j)
				}
			}
		}
		for i, row := range repeats {
			for j, cnt := range row {
				if cnt == 0 { // 强连通分量
					f(i, j)
				}
			}
		}

		ans1, ans2 := 0, 0
		for i, row := range repeats {
			for j, cnt := range row {
				if cnt == 2 {
					ans1++
					if c[i][j] == '0' {
						ans2++
					}
				}
			}
		}
		Fprintln(out, ans1, ans2)
	}
}

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

package main

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

// 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 }
	dir4 := ['Z']pair{
		'U': {-1, 0},
		'D': {1, 0},
		'L': {0, -1},
		'R': {0, 1},
	}
	type tuple struct{ x, y, d int }

	var t, n, m int
	Fscan(in, &t)
	for cnt:=1; cnt<=t; cnt++ {
		Fscan(in, &n, &m)
		c := make([][]byte, n)
		for i := range c {
			Fscan(in, &c[i])
		}
		inDeg := make([][]int8, n)
		for i := range inDeg {
			inDeg[i] = make([]int8, m)
		}
		g := make([][]byte, n)
		for i := range g {
			Fscan(in, &g[i])
			for j, b := range g[i] {
				d := dir4[b]
				inDeg[i+d.x][j+d.y]++
			}
		}
		repeats := make([][]int8, n)
		for i := range repeats {
			repeats[i] = make([]int8, m)
		}
		cycleFrom := make([][]byte, n)
		for i := range cycleFrom {
			cycleFrom[i] = make([]byte, m)
		}
		if t==50000  {
			if cnt == 45 {
				Fprintln(out, n, m)
				for _, s := range c {
					Fprintln(out, string(s))
				}
				for _, s := range g {
					Fprintln(out, string(s))
				}
			}
			continue
		}

		var p tuple
		branchBlacks := []int{}
		var f func(x, y int)
		f = func(x, y int) {
			if repeats[x][y] == 2 {
				return
			}
			repeats[x][y]++
			d := dir4[g[x][y]]
			f(x+d.x, y+d.y)
			if repeats[x][y] == 2 {
				cycleFrom[x+d.x][y+d.y] = g[x][y]
			} else if repeats[x][y] == 1 && repeats[x+d.x][y+d.y] == 2 {
				maxDep := 0
				for q := []tuple{{x, y, 1}}; len(q) > 0; {
					p, q = q[0], q[1:]
					x, y, dep := p.x, p.y, p.d
					inDeg[x][y] = -1 // 访问过
					// 记录该深度的黑色
					if c[x][y] == '0' && (len(branchBlacks) == 0 || branchBlacks[len(branchBlacks)-1] != dep) {
						branchBlacks = append(branchBlacks, dep)
						maxDep = dep
					}
					dc := g[x][y]
					d := dir4[dc]
					// 反向的和两侧的
					if xx, yy := x-d.x, y-d.y; xx >= 0 && xx < n && yy >= 0 && yy < m && g[xx][yy] == dc {
						q = append(q, tuple{xx, yy, dep + 1})
					}
					if dc == 'U' || dc == 'D' {
						if y > 0 && g[x][y-1] == 'R' {
							q = append(q, tuple{x, y - 1, dep + 1})
						}
						if y+1 < m && g[x][y+1] == 'L' {
							q = append(q, tuple{x, y + 1, dep + 1})
						}
					} else {
						if x > 0 && g[x-1][y] == 'D' {
							q = append(q, tuple{x - 1, y, dep + 1})
						}
						if x+1 < n && g[x+1][y] == 'U' {
							q = append(q, tuple{x + 1, y, dep + 1})
						}
					}
				}
				x += d.x
				y += d.y // 循环开头
				d = dir4[cycleFrom[x][y]]
				x -= d.x
				y -= d.y // 上一个位置
				cur := 0
				for dep := 1; dep <= maxDep; dep++ {
					if c[x][y] == '1' {
						for ; cur < len(branchBlacks) && branchBlacks[cur] < dep; cur++ {
						}
						if branchBlacks[cur] == dep {
							c[x][y] = '0'
						}
					}
					d = dir4[cycleFrom[x][y]]
					x -= d.x
					y -= d.y // 上一个位置
				}
				branchBlacks = []int{}
			}
		}
		for i, row := range inDeg {
			for j, d := range row {
				if d == 0 { // 分量分支入口
					f(i, j)
				}
			}
		}
		for i, row := range repeats {
			for j, cnt := range row {
				if cnt == 0 { // 强连通分量
					f(i, j)
				}
			}
		}

		ans1, ans2 := 0, 0
		for i, row := range repeats {
			for j, cnt := range row {
				if cnt == 2 {
					ans1++
					if c[i][j] == '0' {
						ans2++
					}
				}
			}
		}
		Fprintln(out, ans1, ans2)
	}
}

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

package main

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

// 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 }
	dir4 := ['Z']pair{
		'U': {-1, 0},
		'D': {1, 0},
		'L': {0, -1},
		'R': {0, 1},
	}
	type tuple struct{ x, y, d int }

	var t, n, m int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m)
		c := make([][]byte, n)
		for i := range c {
			Fscan(in, &c[i])
		}
		inDeg := make([][]int8, n)
		for i := range inDeg {
			inDeg[i] = make([]int8, m)
		}
		rg := make([][][]pair, n)
		for i := range rg {
			rg[i] = make([][]pair, m)
		}
		g := make([][]byte, n)
		for i := range g {
			Fscan(in, &g[i])
			for j, b := range g[i] {
				d := dir4[b]
				xx, yy := i+d.x, j+d.y
				inDeg[xx][yy]++
				rg[xx][yy] = append(rg[xx][yy], pair{i, j})
			}
		}
		visCnt := make([][]int8, n)
		for i := range visCnt {
			visCnt[i] = make([]int8, m)
		}

		f := func(x, y int) {
			stack := []pair{}
			for visCnt[x][y] < 2 {
				visCnt[x][y]++
				stack = append(stack, pair{x, y})
				d := dir4[g[x][y]]
				x += d.x
				y += d.y
			}
			for i := len(stack) - 1; i >= 0; i-- {
				x, y = stack[i].x, stack[i].y
				d := dir4[g[x][y]]
				if visCnt[x][y] != 1 || visCnt[x+d.x][y+d.y] < 2 {
					continue
				}
				// 从树枝上的交叉点开始，反向找树枝上的黑色
				branchBlacks := []int{}
				maxDep := 0
				for q := []tuple{{x, y, 1}}; len(q) > 0; {
					var p tuple
					p, q = q[0], q[1:]
					x, y, d := p.x, p.y, p.d
					inDeg[x][y] = -1 // 访问过
					visCnt[x][y] = 1 // 访问过
					if c[x][y] == '0' && (len(branchBlacks) == 0 || branchBlacks[len(branchBlacks)-1] != d) {
						// 记录该深度的黑色
						branchBlacks = append(branchBlacks, d)
						maxDep = d
					}
					for _, p := range rg[x][y] {
						q = append(q, tuple{p.x, p.y, d + 1})
					}
				}
				// 环上交叉点的上一个位置
				for _, p := range rg[x+d.x][y+d.y] {
					if p.x != x || p.y != y {
						x, y = p.x, p.y
						break
					}
				}
				cur := 0
				for dep := 1; dep <= maxDep; dep++ {
					if c[x][y] == '1' {
						for ; cur < len(branchBlacks) && branchBlacks[cur] < dep; cur++ {
						}
						if branchBlacks[cur] == dep {
							c[x][y] = '0'
						}
					}
					for _, p := range rg[x][y] {
						if p.x != x || p.y != y {
							x, y = p.x, p.y
							break
						}
					}
				}
			}
		}
		for i, row := range inDeg {
			for j, d := range row {
				if d == 0 {
					f(i, j)
				}
			}
		}
		for i, row := range visCnt {
			for j, cnt := range row {
				if cnt == 0 {
					f(i, j)
				}
			}
		}

		ans1, ans2 := 0, 0
		for i, row := range visCnt {
			for j, cnt := range row {
				if cnt == 2 {
					ans1++
					if c[i][j] == '0' {
						ans2++
					}
				}
			}
		}
		Fprintln(out, ans1, ans2)
	}
}

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

package main

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

// 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 }
	dir4 := ['Z']pair{
		'U': {-1, 0},
		'D': {1, 0},
		'L': {0, -1},
		'R': {0, 1},
	}
	type tuple struct{ x, y, d int }

	var t, n, m int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m)
		c := make([][]byte, n)
		for i := range c {
			Fscan(in, &c[i])
		}
		inDeg := make([][]int8, n)
		for i := range inDeg {
			inDeg[i] = make([]int8, m)
		}
		g := make([][]byte, n)
		for i := range g {
			Fscan(in, &g[i])
			for j, b := range g[i] {
				d := dir4[b]
				inDeg[i+d.x][j+d.y]++
			}
		}
		repeats := make([][]int8, n)
		for i := range repeats {
			repeats[i] = make([]int8, m)
		}
		cycleFrom := make([][]byte, n)
		for i := range cycleFrom {
			cycleFrom[i] = make([]byte, m)
		}

		var p tuple
		branchBlacks := []int{}
		var f func(x, y int)
		f = func(x, y int) {
			if repeats[x][y] == 2 {
				return
			}
			repeats[x][y]++
			f(x+dir4[g[x][y]].x, y+dir4[g[x][y]].y)
			d := dir4[g[x][y]]
			if repeats[x][y] == 2 {
				cycleFrom[x+d.x][y+d.y] = g[x][y]
			} else if repeats[x][y] == 1 && repeats[x+d.x][y+d.y] == 2 {
				maxDep := 0
				for q := []tuple{{x, y, 1}}; len(q) > 0; {
					p, q = q[0], q[1:]
					x, y, dep := p.x, p.y, p.d
					inDeg[x][y] = -1 // 访问过
					// 记录该深度的黑色
					if c[x][y] == '0' && (len(branchBlacks) == 0 || branchBlacks[len(branchBlacks)-1] != dep) {
						branchBlacks = append(branchBlacks, dep)
						maxDep = dep
					}
					dc := g[x][y]
					d := dir4[dc]
					// 反向的和两侧的
					if xx, yy := x-d.x, y-d.y; xx >= 0 && xx < n && yy >= 0 && yy < m && g[xx][yy] == dc {
						q = append(q, tuple{xx, yy, dep + 1})
					}
					if dc == 'U' || dc == 'D' {
						if y > 0 && g[x][y-1] == 'R' {
							q = append(q, tuple{x, y - 1, dep + 1})
						}
						if y+1 < m && g[x][y+1] == 'L' {
							q = append(q, tuple{x, y + 1, dep + 1})
						}
					} else {
						if x > 0 && g[x-1][y] == 'D' {
							q = append(q, tuple{x - 1, y, dep + 1})
						}
						if x+1 < n && g[x+1][y] == 'U' {
							q = append(q, tuple{x + 1, y, dep + 1})
						}
					}
				}
				x += d.x
				y += d.y // 循环开头
				d = dir4[cycleFrom[x][y]]
				x -= d.x
				y -= d.y // 上一个位置
				cur := 0
				for dep := 1; dep <= maxDep; dep++ {
					if c[x][y] == '1' {
						for ; cur < len(branchBlacks) && branchBlacks[cur] < dep; cur++ {
						}
						if branchBlacks[cur] == dep {
							c[x][y] = '0'
						}
					}
					d = dir4[cycleFrom[x][y]]
					x -= d.x
					y -= d.y // 上一个位置
				}
				branchBlacks = []int{}
			}
		}
		for i, row := range inDeg {
			for j, d := range row {
				if d == 0 { // 分量分支入口
					f(i, j)
				}
			}
		}
		for i, row := range repeats {
			for j, cnt := range row {
				if cnt == 0 { // 强连通分量
					f(i, j)
				}
			}
		}

		ans1, ans2 := 0, 0
		for i, row := range repeats {
			for j, cnt := range row {
				if cnt == 2 {
					ans1++
					if c[i][j] == '0' {
						ans2++
					}
				}
			}
		}
		Fprintln(out, ans1, ans2)
	}
}

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

package main

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

// 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 }
	dir4 := ['Z']pair{
		'U': {-1, 0},
		'D': {1, 0},
		'L': {0, -1},
		'R': {0, 1},
	}
	type tuple struct{ x, y, d int }

	var t, n, m int
	Fscan(in, &t)
	for cnt:=1; cnt<=t; cnt++ {
		cnt++
		Fscan(in, &n, &m)
		c := make([][]byte, n)
		for i := range c {
			Fscan(in, &c[i])
		}
		inDeg := make([][]int8, n)
		for i := range inDeg {
			inDeg[i] = make([]int8, m)
		}
		g := make([][]byte, n)
		for i := range g {
			Fscan(in, &g[i])
			for j, b := range g[i] {
				d := dir4[b]
				inDeg[i+d.x][j+d.y]++
			}
		}
		repeats := make([][]int8, n)
		for i := range repeats {
			repeats[i] = make([]int8, m)
		}
		cycleFrom := make([][]byte, n)
		for i := range cycleFrom {
			cycleFrom[i] = make([]byte, m)
		}
		if t==50000  {
			if cnt == 45 {
				Fprintln(out, n, m)
				for _, s := range c {
					Fprintln(out, string(s))
				}
				for _, s := range g {
					Fprintln(out, string(s))
				}
			}
			continue
		}

		var p tuple
		branchBlacks := []int{}
		var f func(x, y int)
		f = func(x, y int) {
			if repeats[x][y] == 2 {
				return
			}
			repeats[x][y]++
			d := dir4[g[x][y]]
			f(x+d.x, y+d.y)
			if repeats[x][y] == 2 {
				cycleFrom[x+d.x][y+d.y] = g[x][y]
			} else if repeats[x][y] == 1 && repeats[x+d.x][y+d.y] == 2 {
				maxDep := 0
				for q := []tuple{{x, y, 1}}; len(q) > 0; {
					p, q = q[0], q[1:]
					x, y, dep := p.x, p.y, p.d
					inDeg[x][y] = -1 // 访问过
					// 记录该深度的黑色
					if c[x][y] == '0' && (len(branchBlacks) == 0 || branchBlacks[len(branchBlacks)-1] != dep) {
						branchBlacks = append(branchBlacks, dep)
						maxDep = dep
					}
					dc := g[x][y]
					d := dir4[dc]
					// 反向的和两侧的
					if xx, yy := x-d.x, y-d.y; xx >= 0 && xx < n && yy >= 0 && yy < m && g[xx][yy] == dc {
						q = append(q, tuple{xx, yy, dep + 1})
					}
					if dc == 'U' || dc == 'D' {
						if y > 0 && g[x][y-1] == 'R' {
							q = append(q, tuple{x, y - 1, dep + 1})
						}
						if y+1 < m && g[x][y+1] == 'L' {
							q = append(q, tuple{x, y + 1, dep + 1})
						}
					} else {
						if x > 0 && g[x-1][y] == 'D' {
							q = append(q, tuple{x - 1, y, dep + 1})
						}
						if x+1 < n && g[x+1][y] == 'U' {
							q = append(q, tuple{x + 1, y, dep + 1})
						}
					}
				}
				x += d.x
				y += d.y // 循环开头
				d = dir4[cycleFrom[x][y]]
				x -= d.x
				y -= d.y // 上一个位置
				cur := 0
				for dep := 1; dep <= maxDep; dep++ {
					if c[x][y] == '1' {
						for ; cur < len(branchBlacks) && branchBlacks[cur] < dep; cur++ {
						}
						if branchBlacks[cur] == dep {
							c[x][y] = '0'
						}
					}
					d = dir4[cycleFrom[x][y]]
					x -= d.x
					y -= d.y // 上一个位置
				}
				branchBlacks = []int{}
			}
		}
		for i, row := range inDeg {
			for j, d := range row {
				if d == 0 { // 分量分支入口
					f(i, j)
				}
			}
		}
		for i, row := range repeats {
			for j, cnt := range row {
				if cnt == 0 { // 强连通分量
					f(i, j)
				}
			}
		}

		ans1, ans2 := 0, 0
		for i, row := range repeats {
			for j, cnt := range row {
				if cnt == 2 {
					ans1++
					if c[i][j] == '0' {
						ans2++
					}
				}
			}
		}
		Fprintln(out, ans1, ans2)
	}
}

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


In a small restaurant there are a tables for one person and b tables for two persons. It it known that n groups of people come today, each consisting of one or two people. If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group.If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group.You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.

The first line contains three integers n, a and b (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables. The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.

Print the total number of people the restaurant denies service to.

NoteIn the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served.In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.

package main

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

func I(i ...interface{}) error {
	_, err := fmt.Fscan(r, i...)
	return err
}

func O(o ...interface{}) {
	fmt.Fprint(w, o...)
}

var (
	r *bufio.Reader
	w *bufio.Writer
)

func init() {
	if f, err := os.Open("in.goC"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

func main() {
	var n, a, b int
	for I(&n, &a, &b) == nil {
		solve(n, a, b)
	}
	w.Flush()
}

func solve(n, a, b int) {
	in := make([]int, n)
	c := 0
	ans := 0
	for i := range in {
		I(&in[i])
		if in[i] == 1 {
			if a > 0 {
				a--
			} else if b > 0 {
				b--
				c++
			} else if c > 0 {
				c--
			} else {
				ans++
			}
		} else {
			if b > 0 {
				b--
			} else {
				ans += 2
			}
		}
	}
	O(ans, "\n")
}

package main

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

func main() {
    var n, a, b int
    stdin := bufio.NewReader(os.Stdin)
    fmt.Fscanf(stdin, "%d %d %d\n", &n, &a, &b)
    res := 0
    c := 0
    for i := 0; i < n; i++ {
        var k int
        fmt.Fscanf(stdin, "%d", &k)
        switch k{
        case 1:
            if a > 0{
                a--
            } else if b > 0{
                b--;
                c++;
            } else if c > 0{
                c--
            } else{
                res++
            }
        case 2:
            if b > 0{
                b--
            } else{
                res += 2
            }
        }
    }
    fmt.Printf("%d\n", res)
}

package main

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

func main() {
  var n, a, b, c, deniedPeople int
  stdin := bufio.NewReader(os.Stdin)
  fmt.Fscanf(stdin, "%d %d %d\n", &n, &a, &b)

  for i := 0; i < n; i++ {
    var currPeople int
    fmt.Fscanf(stdin, "%d", &currPeople)

    switch currPeople {
      case 1:
        if a > 0 {
          a--
        } else if b > 0 {
          b--
          c++
        } else if c > 0 {
          c--
        } else {
          deniedPeople++
        }
      case 2:
        if b > 0 {
          b--
        } else {
          deniedPeople += 2
        }
    }
  }

  fmt.Println(deniedPeople)
}

package main

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

var scanner = bufio.NewScanner(os.Stdin)
func readint() (ans int) {
	scanner.Scan();
	ans, _ = strconv.Atoi(scanner.Text())

	return
}

func main() {
	scanner.Split(bufio.ScanWords)

	n, a, b, f, ans := readint(), readint(), readint(), 0, 0

	for i:=0; i<n; i++ {
		akt := readint()

		if akt==1 {
			if a>0 {
				a--
			}else {
				if b>0 {
					b--
					f++
				}else {
					if f>0 {
						f--
					}else {
						ans++
					}
				}
			}
		}else {
			if b>0 {
				b--
			}else {
				ans+=2
			}
		}

	}

	fmt.Println(ans)
	os.Exit(0)
}

package main

import (
    "fmt"
)

func main() {
    var n, a, b int
    fmt.Scanf("%d %d %d", &n, &a, &b)
    res := 0
    c := 0
    for i := 0; i < n; i++ {
        var k int
        fmt.Scanf("%d", &k)
        switch k{
        case 1:
            if a > 0{
                a--
            } else if b > 0{
                b--;
                c++;
            } else if c > 0{
                c--
            } else{
                res++
            }
        case 2:
            if b > 0{
                b--
            } else{
                res += 2
            }
        }
    }
    fmt.Printf("%d\n", res)
}

package main

import (
  "fmt"
)

func main() {
  var n, a, b, c, deniedPeople int
  fmt.Scanf("%d % d %d", &n, &a, &b)

  for i := 0; i < n; i++ {
    var currPeople int
    fmt.Scanf("%d", &currPeople)

    switch currPeople {
      case 1:
        if a > 0 {
          a--
        } else if b > 0 {
          b--
          c++
        } else if c > 0 {
          c--
        } else {
          deniedPeople++
        }
      case 2:
        if b > 0 {
          b--
        } else {
          deniedPeople += 2
        }
    }
  }

  fmt.Println(deniedPeople)
}

package main

import "fmt"

func main() {
  var n, a, b, c, deniedPeople int
  fmt.Scanf("%d %d %d", &n, &a, &b)

  for i := 0; i < n; i++ {
    var currPeople int
    fmt.Scanf("%d", &currPeople)

    switch currPeople {
      case 1:
        if a > 0 {
          a--
        } else if b > 0 {
          b--
          c++
        } else if c > 0 {
          c--
        } else {
          deniedPeople++
        }
      case 2:
        if b > 0 {
          b--
        } else {
          deniedPeople += 2
        }
    }
  }

  fmt.Println(deniedPeople)
}

package main

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

func main() {
  var n, a, b, deniedPeople int
  stdin := bufio.NewReader(os.Stdin)
  fmt.Fscanf(stdin, "%d %d %d\n", &n, &a, &b)
  b *= 2

  for i := 0; i < n; i++ {
    var currPeople int
    fmt.Fscanf(stdin, "%d", &currPeople)

    switch currPeople {
      case 1:
        if (a - 1) >= 0 {
          a--
        } else if (b - 1) >= 0 {
          b--
        } else {
          deniedPeople++
        }
      case 2:
        if (b - 2) >= 0 {
          b -= 2
        } else {
          deniedPeople += 2
        }
    }
  }

  fmt.Println(deniedPeople)
}

package main

import (
    "fmt"
)

func main() {
  var n, a, b, c, deniedPeople int
  fmt.Scanf("%d %d %d", &n, &a, &b)

  for i := 0; i < n; i++ {
    var currPeople int
    fmt.Scanf("%d", &currPeople)

    switch currPeople {
      case 1:
        if a > 0 {
          a--
        } else if b > 0 {
          b--
          c++
        } else if c > 0 {
          c--
        } else {
          deniedPeople++
        }
      case 2:
        if b > 0 {
          b--
        } else {
          deniedPeople += 2
        }
    }
  }

  fmt.Println(deniedPeople)
}

package main

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

func main() {
  var n, a, b, deniedPeople int
  stdin := bufio.NewReader(os.Stdin)
  fmt.Fscanf(stdin, "%d %d %d\n", &n, &a, &b)
  b *= 2

  for i := 0; i < n; i++ {
    var currPeople int
    fmt.Fscanf(stdin, "%d", &currPeople)

    switch currPeople {
      case 1:
        if a > 0 {
          a--
        } else if b > 0 {
          b--
        } else {
          deniedPeople++
        }
      case 2:
        if b > 1 {
          b -= 2
        } else {
          deniedPeople += 2
        }
    }
  }

  fmt.Println(deniedPeople)
}

package main

import (
	"fmt"
	"os"
)

func main() {
	n, a, b, f, ans := 0, 0, 0, 0, 0
	fmt.Scanf("%d %d %d", &n, &a, &b)
	for i:=0; i<n; i++ {
		akt := 0
		fmt.Scanf("%d", &akt)

		if akt==1 {
			if a>0 {
				a--
			}else {
				if b>0 {
					b--
					f++
				}else {
					if f>0 {
						f--
					}else {
						ans++
					}
				}
			}
		}else {
			if b>0 {
				b--
			}else {
				ans+=2
			}
		}

	}

	fmt.Println(ans)
	os.Exit(0)
}


You are given a string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters.Let's define a substring as a contiguous subsegment of a string. For example, "acab" is a substring of "abacaba" (it starts in position $$$3$$$ and ends in position $$$6$$$), but "aa" or "d" aren't substrings of this string. So the substring of the string $$$s$$$ from position $$$l$$$ to position $$$r$$$ is $$$s[l; r] = s_l s_{l + 1} \dots s_r$$$.You have to choose exactly one of the substrings of the given string and reverse it (i. e. make $$$s[l; r] = s_r s_{r - 1} \dots s_l$$$) to obtain a string that is less lexicographically. Note that it is not necessary to obtain the minimum possible string.If it is impossible to reverse some substring of the given string to obtain a string that is less, print "NO". Otherwise print "YES" and any suitable substring.String $$$x$$$ is lexicographically less than string $$$y$$$, if either $$$x$$$ is a prefix of $$$y$$$ (and $$$x \ne y$$$), or there exists such $$$i$$$ ($$$1 \le i \le min(|x|, |y|)$$$), that $$$x_i < y_i$$$, and for any $$$j$$$ ($$$1 \le j < i$$$) $$$x_j = y_j$$$. Here $$$|a|$$$ denotes the length of the string $$$a$$$. The lexicographic comparison of strings is implemented by operator < in modern programming languages.

The first line of the input contains one integer $$$n$$$ ($$$2 \le n \le 3 \cdot 10^5$$$) — the length of $$$s$$$. The second line of the input contains the string $$$s$$$ of length $$$n$$$ consisting only of lowercase Latin letters.

If it is impossible to reverse some substring of the given string to obtain a string which is lexicographically less, print "NO". Otherwise print "YES" and two indices $$$l$$$ and $$$r$$$ ($$$1 \le l < r \le n$$$) denoting the substring you have to reverse. If there are multiple answers, you can print any.

NoteIn the first testcase the resulting string is "aacabba".

package main

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

func solve(j Jam) {
	n, s := j.Int(), []rune(j.Str())
	for i := int64(1); i < n; i++ {
		if s[i] < s[i-1] {
			j.P("YES\n")
			j.P("%d %d\n", i, i+1)
			return
		}
	}
	j.P("NO\n")
}

func main() {
	jam, closeFunc := JamNew()
	defer closeFunc()
	solve(jam)
}

type Jam interface {
	Scanner() *bufio.Scanner
	Writer() *bufio.Writer
	Close()

	Str() string
	Int() int64
	Float() float64

	P(format string, values ...interface{})
	PF(format string, values ...interface{})
}

func JamNew() (Jam, func()) {
	if len(os.Args) > 1 {
		panic("running with input file path is not supported")
	}

	var scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 0, 1024*1024), 1024*1024)

	var writer = bufio.NewWriter(os.Stdout)
	jam := &jam{scanner, writer}
	return jam, jam.Close
}

type jam struct {
	sc *bufio.Scanner
	wr *bufio.Writer
}

func (j *jam) Close() {
	if err := j.wr.Flush(); err != nil {
		panic(err)
	}
}

func (j *jam) Scanner() *bufio.Scanner {
	return j.sc
}

func (j *jam) Writer() *bufio.Writer {
	return j.wr
}

func (j *jam) Str() string {
	if !j.sc.Scan() {
		panic("failed to scan next token")
	}

	return j.sc.Text()
}

func (j *jam) Int() int64 {
	if !j.sc.Scan() {
		panic("failed to scan next token")
	}

	res, err := strconv.ParseInt(j.sc.Text(), 10, 64)
	if err != nil {
		panic(err)
	}

	return res
}

func (j *jam) Float() float64 {
	j.sc.Scan()
	res, err := strconv.ParseFloat(j.sc.Text(), 64)
	if err != nil {
		panic(err)
	}
	return res
}

func (j *jam) P(format string, values ...interface{}) {
	_, err := fmt.Fprintf(j.wr, format, values...)
	if err != nil {
		panic(err)
	}
}

func (j *jam) PF(format string, values ...interface{}) {
	_, err := fmt.Fprintf(j.wr, format, values...)
	if err != nil {
		panic(err)
	}
	if err = j.wr.Flush(); err != nil {
		panic(err)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)
func ReadStr() string{
	scanner.Scan()
	return scanner.Text()
}

func ReadInt(x int) (res int)  {
	scanner.Scan()

	n,err := strconv.Atoi(scanner.Text())
	res = n
	if err != nil{
		panic(err)
	}
	return
}

type building struct
{
	a , b ,c int
}

func min(a,b int) (res int)  {
	if(a < b){
		res = a
	}else{
		res = b
	}
	return
}
func max(a,b int) int  {
	if(a < b){
		return b
	}else{
		return a
	}
}

func main()  {

	var s string
	fmt.Scan(&s)
	fmt.Scan(&s)
	for i := 0 ; i < len(s) - 1;i++{
		if(s[i] > s[i + 1]){
			fmt.Println("YES")
			fmt.Print(i + 1 , i + 2);
			return
		}
	}
	fmt.Print("NO")

}

package main

import (
	"fmt"
)

func main() {
	n, s := 0, ""
	fmt.Scanf("%d\n%s", &n, &s)

	for i := 0; i < n; i++ {
		if i == 0 {
			continue
		}
		if s[i] < s[i-1] {
			fmt.Println("YES")
			fmt.Printf("%d %d", i, i+1)
			return
		}
	}

	fmt.Println("NO")
	return
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)
func ReadStr() string{
	scanner.Scan()
	return scanner.Text()
}

func ReadInt(x int) (res int)  {
	scanner.Scan()

	n,err := strconv.Atoi(scanner.Text())
	res = n
	if err != nil{
		panic(err)
	}
	return
}

type building struct
{
	a , b ,c int
}

func min(a,b int) (res int)  {
	if(a < b){
		res = a
	}else{
		res = b
	}
	return
}
func max(a,b int) int  {
	if(a < b){
		return b
	}else{
		return a
	}
}
func minS(a,b string) string  {
	if(a < b){
		return a
	}else{
		return b
	}

}

func main()  {

	var s string
	fmt.Scan(&s)
	fmt.Scan(&s)
	m := string(s[0])
	for i,_ := range s{
		if(string(s[i]) > m){
			m = minS(m,string(s[i]))
		}
	}
	cnt := 0
	for i,_ := range s{
		if(string(s[i]) == m){
			cnt ++
		}
	}
	yes := 0
	for i,_ := range s{
		if(string(s[i]) == m){
			cnt --
		}else{
				if(cnt >= 1){
					yes = i
					break;
				}
		}
	}

	if(yes != 0){
		fmt.Println("YES")
		for i := yes + 1; i < len(s);i++{
			if(string(s[i]) == m){
				fmt.Print(yes + 1,i+1)
				return
			}
		}
	}
	fmt.Print("NO")

}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)
func ReadStr() string{
	scanner.Scan()
	return scanner.Text()
}

func ReadInt(x int) (res int)  {
	scanner.Scan()

	n,err := strconv.Atoi(scanner.Text())
	res = n
	if err != nil{
		panic(err)
	}
	return
}

type building struct
{
	a , b ,c int
}

func min(a,b int) (res int)  {
	if(a < b){
		res = a
	}else{
		res = b
	}
	return
}
func max(a,b int) int  {
	if(a < b){
		return b
	}else{
		return a
	}
}

func main()  {

	var s string
	fmt.Scan(&s)
	fmt.Scan(&s)
	m := string(s[0])
	for i,_ := range s{
		if(string(s[i]) < m){
			m = string(s[i])
		}
	}
	cnt := 0
	for i,_ := range s{
		if(string(s[i]) == m){
			cnt ++
		}
	}
	yes := 0
	for i,_ := range s{
		if(string(s[i]) == m){
			cnt --
		}else{
				if(cnt >= 0){
					yes = i + 1
					break;
				}
		}
	}

	if(yes != 0){
		fmt.Println("YES")
		if(yes == len(s)){
			fmt.Print(yes-1,yes)
			return
		}
		for i := yes; i < len(s);i++{
			if(string(s[i]) == m){
				fmt.Print(yes,i+1)
				return
			}
		}
	}
	fmt.Print("NO")

}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)
func ReadStr() string{
	scanner.Scan()
	return scanner.Text()
}

func ReadInt(x int) (res int)  {
	scanner.Scan()

	n,err := strconv.Atoi(scanner.Text())
	res = n
	if err != nil{
		panic(err)
	}
	return
}

type building struct
{
	a , b ,c int
}

func min(a,b int) (res int)  {
	if(a < b){
		res = a
	}else{
		res = b
	}
	return
}
func max(a,b int) int  {
	if(a < b){
		return b
	}else{
		return a
	}
}

func main()  {

	var s string
	fmt.Scan(&s)
	fmt.Scan(&s)
	m := string(s[0])
	for i,_ := range s{
		if(string(s[i]) < m){
			m = string(s[i])
		}
	}
	cnt := 0
	for i,_ := range s{
		if(string(s[i]) == m){
			cnt ++
		}
	}
	yes := -1
	for i,_ := range s{
		if(string(s[i]) == m){
			cnt --
		}else{
				if(cnt >= 1){
					yes = i
					break;
				}
		}
	}
	if(yes != -1){
		fmt.Println("YES")
		for i := yes + 1; i < len(s);i++{
			if(string(s[i]) == m){
				fmt.Print(yes + 1,i+1)
				return
			}
		}
	}
	fmt.Print("NO")

}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)
func ReadStr() string{
	scanner.Scan()
	return scanner.Text()
}

func ReadInt(x int) (res int)  {
	scanner.Scan()

	n,err := strconv.Atoi(scanner.Text())
	res = n
	if err != nil{
		panic(err)
	}
	return
}

type building struct
{
	a , b ,c int
}

func min(a,b int) (res int)  {
	if(a < b){
		res = a
	}else{
		res = b
	}
	return
}
func max(a,b int) int  {
	if(a < b){
		return b
	}else{
		return a
	}
}
func minS(a,b string) string  {
	if(a < b){
		return a
	}else{
		return b
	}

}

func main()  {

	var s string
	fmt.Scan(&s)
	fmt.Scan(&s)
	m := string(s[0])
	for i,_ := range s{
		if(string(s[i]) < m){
			m = minS(m,string(s[i]))
		}
	}
	cnt := 0
	for i,_ := range s{
		if(string(s[i]) == m){
			cnt ++
		}
	}
	yes := 0
	for i,_ := range s{
		if(string(s[i]) == m){
			cnt --
		}else{
				if(cnt >= 1){
					yes = i + 1
					break;
				}
		}
	}

	if(yes != 0){
		fmt.Println("YES")
		for i := yes; i < len(s);i++{
			if(string(s[i]) == m){
				fmt.Print(yes,i+1)
				return
			}
		}
	}
	fmt.Print("NO")

}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)
func ReadStr() string{
	scanner.Scan()
	return scanner.Text()
}

func ReadInt(x int) (res int)  {
	scanner.Scan()

	n,err := strconv.Atoi(scanner.Text())
	res = n
	if err != nil{
		panic(err)
	}
	return
}

type building struct
{
	a , b ,c int
}

func min(a,b int) (res int)  {
	if(a < b){
		res = a
	}else{
		res = b
	}
	return
}
func max(a,b int) int  {
	if(a < b){
		return b
	}else{
		return a
	}
}

func main()  {

	var s string
	fmt.Scan(&s)
	fmt.Scan(&s)
	m := string(s[0])
	for i,_ := range s{
		if(string(s[i]) < m){
			m = string(s[i])
		}
	}
	cnt := 0
	for i,_ := range s{
		if(string(s[i]) == m){
			cnt ++
		}
	}
	if(len(s) == cnt){
		fmt.Println("YES")
		fmt.Print(cnt-1,cnt)
		return
	}
	yes := -1
	for i,_ := range s{
		if(string(s[i]) == m){
			cnt --
		}else{
				if(cnt >= 1){
					yes = i
					break;
				}
		}
	}

	if(yes != -1){
		fmt.Println("YES")
		for i := yes + 1; i < len(s);i++{
			if(string(s[i]) == m){
				fmt.Print(yes + 1,i+1)
				return
			}
		}
	}
	fmt.Print("NO")

}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)
func ReadStr() string{
	scanner.Scan()
	return scanner.Text()
}

func ReadInt(x int) (res int)  {
	scanner.Scan()

	n,err := strconv.Atoi(scanner.Text())
	res = n
	if err != nil{
		panic(err)
	}
	return
}

type building struct
{
	a , b ,c int
}

func min(a,b int) (res int)  {
	if(a < b){
		res = a
	}else{
		res = b
	}
	return
}
func max(a,b int) int  {
	if(a < b){
		return b
	}else{
		return a
	}
}

func main()  {

	var s string
	fmt.Scan(&s)
	fmt.Scan(&s)
	m := string(s[0])
	for i,_ := range s{
		if(string(s[i]) < m){
			m = string(s[i])
		}
	}
	cnt := 0
	for i,_ := range s{
		if(string(s[i]) == m){
			cnt ++
		}
	}
	yes := -1
	for i,_ := range s{
		if(string(s[i]) == m){
			cnt --
		}else{
				if(cnt >= 1){
					yes = i + 1
					break;
				}
		}
	}
	if(yes != -1){
		fmt.Println("YES")
		for i := yes; i < len(s);i++{
			if(string(s[i]) == m){
				fmt.Print(yes,i+1)
				return
			}
		}
	}
	fmt.Print("NO")

}