package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n := 0
	fmt.Fscanf(in, "%d\n", &n)
	numF := make(map[int]int)
	numB := make(map[int]int)
	perfect := 0
	for i := 0; i < n; i++ {
		s := ""
		fmt.Fscanf(in, "%s\n", &s)
		f := 0
		b := 0
		valid := true
		for _, ch := range s {
			if ch == '(' {
				f++
			} else {
				b++
			}
			if b > f {
				valid = false
			}
		}
		if valid {
			if f == b {
				perfect++
			} else if f > b {
				numF[f-b]++
			}
		} else {
			valid = true
			f = 0
			b = 0
			for i := range s {
				if s[len(s)-1-i] == ')' {
					f++
				} else {
					b++
				}
				if b > f {
					valid = false
				}
			}
			if valid {
				if f == b {
					perfect++
				} else if f > b {
					numB[f-b]++
				}
			}
		}
	}
	ans := 0
	ans += perfect / 2
	for k, v := range numF {
		ans += min(v, numB[k])
	}
	fmt.Fprintf(out, "%d\n", ans)
	return
}

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

// For Parenthesis like "))))(())(((("
// We can scan twice(backward, forward) to get value like 4, 4 (producing 4 extra ")" and 4 extra "(")
// (Be careful "((): and ")((" is very different.)
// After get the meta data, we can start working on pairs.
package main

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

var (
	open  = "("[0]
	close = ")"[0]
)

func calcSequence(s string) (int, int) {
	oCounter, cCounter, extraO, extraC := 0, 0, 0, 0

	for i := 0; i < len(s); i++ {
		if s[i] == close {
			cCounter++
		}
		if s[i] == open {
			if oCounter < cCounter {
				extraC, oCounter, cCounter = extraC+(cCounter-oCounter), 0, 0
			}
			oCounter++
		}
	}
	if cCounter > oCounter {
		extraC += cCounter - oCounter
	}

	oCounter, cCounter = 0, 0
	for i := len(s) - 1; i >= 0; i-- {
		if s[i] == open {
			oCounter++
		}
		if s[i] == close {
			if cCounter < oCounter {
				extraO, oCounter, cCounter = extraO+(oCounter-cCounter), 0, 0
			}
			cCounter++
		}
	}
	if oCounter > cCounter {
		extraO += oCounter - cCounter
	}

	return extraO, extraC
}

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

func main() {
	n := 0
	fmt.Scan(&n)
	reader := bufio.NewReaderSize(os.Stdin, 5*int(1e5))
	p := make([]string, n)
	for i := 0; i < n; i++ {
		s := ""
		fmt.Fscan(reader, &s)
		p[i] = s
	}

	pMap := map[[2]int]int{}
	for _, s := range p {
		o, c := calcSequence(s)
		if (o == 0) || (c == 0) {
			pMap[[2]int{o, c}]++
		}
	}

	sum := pMap[[2]int{0, 0}] / 2
	for k, v := range pMap {
		if k == [2]int{0, 0} {
			continue
		}
		negK := [2]int{k[1], k[0]}
		pairCount := min(v, pMap[negK])
		sum, pMap[k], pMap[negK] = sum+pairCount, pMap[k]-pairCount, pMap[negK]-pairCount
	}
	fmt.Println(sum)
}

package main

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

const (
	BIG = 1024 * 1024 * 10
)

func readLine(r *bufio.Reader) string {
	bytes, _, err := r.ReadLine()
	if err != nil {
		log.Fatalln("Failed to read line")
	}

	return string(bytes)
}

func readInts(r *bufio.Reader) []int {
	elems := strings.Fields(readLine(r))

	ret := make([]int, len(elems))
	for i, el := range elems {
		tmp, err := strconv.ParseInt(el, 10, 64)
		if err != nil {
			log.Fatalf("Failed to parse number %v", el)
		}
		ret[i] = int(tmp)
	}

	return ret
}

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

func main() {
	r := bufio.NewReaderSize(os.Stdin, BIG)
	var opening rune
	open := "("
	for _, el := range open {
		opening = el
		break
	}

	n := readInts(r)[0]

	pos := make(map[int]int)
	neg := make(map[int]int)
	good := 0
	for i := 0; i < n; i++ {
		seq := readLine(r)

		curr := 0
		isNeg := false
		min := 0
		for _, el := range seq {
			if el == opening {
				curr += 1
			} else {
				curr -= 1
			}
			if curr < 0 {
				isNeg = true
			}
			if curr < min {
				min = curr
			}
		}
		if isNeg {
			if min != curr {
				continue
			}
			if _, ok := neg[-curr]; ok {
				neg[-curr] += 1
			} else {
				neg[-curr] = 1
			}
		} else if curr == 0 {
			good += 1
		} else {
			if _, ok := pos[curr]; ok {
				pos[curr] += 1
			} else {
				pos[curr] = 1
			}
		}
	}

	res := good / 2
	for key, val := range pos {
		if nval, ok := neg[key]; ok {
			res += GetMin(val, nval)
		}
	}
	fmt.Println(res)
}

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/1097/C

package main

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

type SequenceMetadata struct {
	CanStart   bool
	CanEnd     bool
	IsBalanced bool
	LeftCount  int
	RightCount int
}

func canStart(sequence string) bool {
	leftCount := 0
	rightCount := 0
	for i := 0; i < len(sequence); i++ {
		if string(sequence[i]) == "(" {
			leftCount += 1
		} else if string(sequence[i]) == ")" {
			rightCount += 1
		}
		if rightCount > leftCount {
			return false
		}
	}
	return true
}

func canEnd(sequence string) bool {
	leftCount := 0
	rightCount := 0
	for i := len(sequence) - 1; i >= 0; i-- {
		if string(sequence[i]) == "(" {
			leftCount += 1
		} else if string(sequence[i]) == ")" {
			rightCount += 1
		}
		if leftCount > rightCount {
			return false
		}
	}
	return true
}

func analyzeSequence(sequence string) SequenceMetadata {
	canStart := canStart(sequence)
	canEnd := canEnd(sequence)
	s := SequenceMetadata{
		CanStart:   canStart,
		CanEnd:     canEnd,
		IsBalanced: canStart && canEnd,
		LeftCount:  0,
		RightCount: 0,
	}
	for i := len(sequence) - 1; i >= 0; i-- {
		if string(sequence[i]) == "(" {
			s.LeftCount += 1
		} else if string(sequence[i]) == ")" {
			s.RightCount += 1
		}
	}
	return s
}

func Solve(n int, sequences []string) int {
	sequenceMetadata := make([]SequenceMetadata, n)
	balancedCount := 0
	var startingDeltas []int
	var endingDeltas = make(map[int]int)
	for i := 0; i < n; i++ {
		sequenceMetadata[i] = analyzeSequence(sequences[i])
		if sequenceMetadata[i].IsBalanced {
			balancedCount += 1
		} else if sequenceMetadata[i].CanStart {
			startingDeltas = append(startingDeltas, sequenceMetadata[i].LeftCount-sequenceMetadata[i].RightCount)
		} else if sequenceMetadata[i].CanEnd {
			endingDeltas[sequenceMetadata[i].RightCount-sequenceMetadata[i].LeftCount] += 1
		}
	}
	matches := 0
	for i := 0; i < len(startingDeltas); i++ {
		if endingDeltas[startingDeltas[i]] > 0 {
			matches += 1
			endingDeltas[startingDeltas[i]] -= 1
		}
	}
	return matches + balancedCount/2
}

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

	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	sequences := make([]string, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%s\n", &sequences[i])
	}
	fmt.Fprintf(writer, "%d\n", Solve(n, sequences))
}

package main

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

func main() {
	var N int
	scan(&N)

	m := make(map[int]int, 128)
	for i, s := 0, ""; i < N; i++ {
		scan(&s)
		lx, cnt := 0, 0
		for _, val := range s {
			switch val {
			case '(':
				cnt++
			case ')':
				cnt--
			}

			if lx > cnt {
				lx = cnt
			}
		}

		if cnt < 0 && lx < cnt || cnt >= 0 && lx < 0 {
			continue
		}
		m[cnt]++
	}

	ans := 0
	for idx, val := range m {
		if idx > 0 {
			ans += min2(val, m[-idx])
		} else if idx == 0 {
			ans += val / 2
		}
	}

	fmt.Println(ans)
}

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

// buffered
var bufin = bufio.NewReader(os.Stdin)

func scan(a ...interface{}) { fmt.Fscan(bufin, a...) }

// For Parenthesis like "))))(())(((("
// We can scan twice(backward, forward) to get value like 4, 4 (producing 4 extra ")" and 4 extra "(")
// (Be careful "((): and ")((" is very different.)
// After get the meta data, we can start working on pairs.
package main

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

var (
	open  = "("[0]
	close = ")"[0]
)

func calcSequence(s string) (int, int) {
	oCounter, cCounter, extraO, extraC := 0, 0, 0, 0

	for i := 0; i < len(s); i++ {
		if s[i] == close {
			cCounter++
		}
		if s[i] == open {
			if oCounter < cCounter {
				extraC, oCounter, cCounter = extraC+(cCounter-oCounter), 0, 0
			}
			oCounter++
		}
	}
	if cCounter > oCounter {
		extraC += cCounter - oCounter
	}

	oCounter, cCounter = 0, 0
	for i := len(s) - 1; i >= 0; i-- {
		if s[i] == open {
			oCounter++
		}
		if s[i] == close {
			if cCounter < oCounter {
				extraO, oCounter, cCounter = extraO+(oCounter-cCounter), 0, 0
			}
			cCounter++
		}
	}
	if oCounter > cCounter {
		extraO += oCounter - cCounter
	}

	return extraO, extraC
}

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

func main() {
	n := 0
	fmt.Scan(&n)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	p := make([]string, n)
	for i := 0; i < n; i++ {
		scanner.Scan()
		p[i] = scanner.Text()
	}

	pMap := map[[2]int]int{}
	for _, s := range p {
		o, c := calcSequence(s)
		if (o == 0) || (c == 0) {
			pMap[[2]int{o, c}]++
		}
	}

	sum := pMap[[2]int{0, 0}] / 2

	for k, v := range pMap {
		if k == [2]int{0, 0} {
			continue
		}
		negK := [2]int{k[1], k[0]}
		pairCount := min(v, pMap[negK])
		sum, pMap[k], pMap[negK] = sum+pairCount, pMap[k]-pairCount, pMap[negK]-pairCount
	}
	fmt.Println(sum)
}

package main

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

const (
	BIG = 1024 * 1024 * 10
)

func readLine(r *bufio.Reader) string {
	bytes, _, err := r.ReadLine()
	if err != nil {
		log.Fatalln("Failed to read line")
	}

	return string(bytes)
}

func readInts(r *bufio.Reader) []int {
	elems := strings.Fields(readLine(r))

	ret := make([]int, len(elems))
	for i, el := range elems {
		tmp, err := strconv.ParseInt(el, 10, 64)
		if err != nil {
			log.Fatalf("Failed to parse number %v", el)
		}
		ret[i] = int(tmp)
	}

	return ret
}

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

func main() {
	r := bufio.NewReaderSize(os.Stdin, BIG)
	var opening rune
	open := "("
	for _, el := range open {
		opening = el
		break
	}

	n := readInts(r)[0]

	pos := make(map[int]int)
	neg := make(map[int]int)
	good := 0
	for i := 0; i < n; i++ {
		seq := readLine(r)

		curr := 0
		isNeg := false
		min := 0
		for _, el := range seq {
			if el == opening {
				curr += 1
			} else {
				curr -= 1
			}
			if curr < 0 {
				isNeg = true
			}
			if curr < min {
				min = curr
			}
		}
		if isNeg {
			if _, ok := neg[min]; ok {
				neg[-min] += 1
			} else {
				neg[-min] = 1
			}
		} else if curr == 0 {
			good += 1
		} else {
			if _, ok := pos[curr]; ok {
				pos[curr] += 1
			} else {
				pos[curr] = 1
			}
		}
	}

	res := good / 2
	for key, val := range pos {
		if nval, ok := neg[key]; ok {
			res += GetMin(val, nval)
		}
	}
	fmt.Println(res)
}


package main

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

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

	var n, lca, tar int
	Fscan(in, &n)
	edges := map[[2]int]bool{}
	for i := 0; i < n-1; i++ {
		var v, w int
		Fscan(in, &v, &w)
		v--
		w--
		edges[[2]int{v, w}] = true
	}

	for {
		g := make([][]int, n)
		for e := range edges {
			v, w := e[0], e[1]
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
		}
		if len(g[lca]) == 0 {
			break
		}

		var u, maxD int
		var vis []bool
		var f func(int, int)
		f = func(v, d int) {
			vis[v] = true
			if d > maxD {
				maxD = d
				u = v
			}
			for _, w := range g[v] {
				if !vis[w] {
					f(w, d+1)
				}
			}
		}
		maxD = -1
		vis = make([]bool, n)
		f(lca, 0)
		dv := u
		maxD = -1
		vis = make([]bool, n)
		f(dv, 0)
		dw := u
		Fprintln(out, "?", dv+1, dw+1)
		out.Flush()
		Fscan(in, &lca)
		lca--
		if lca == dv || lca == dw {
			break
		}

		var del func(v, fa int) bool
		del = func(v, fa int) bool {
			if v == tar {
				delete(edges, [2]int{v, fa})
				delete(edges, [2]int{fa, v})
				return true
			}
			for _, w := range g[v] {
				if w != fa {
					if del(w, v) {
						delete(edges, [2]int{v, w})
						delete(edges, [2]int{w, v})
						return true
					}
				}
			}
			return false
		}
		tar = dv
		del(lca, -1)
		tar = dw
		del(lca, -1)
	}
	Fprintln(out, "!", lca+1)
}

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

package main

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

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

	var n int
	Fscan(in, &n)
	edges := map[[2]int]bool{}
	for i := 0; i < n-1; i++ {
		var v, w int
		Fscan(in, &v, &w)
		v--
		w--
		edges[[2]int{v, w}] = true
	}
	//
	//var fa []int
	//initFa := func(n int) { // n+1
	//	fa = make([]int, n)
	//	for i := range fa {
	//		fa[i] = i
	//	}
	//}
	//var find func(int) int
	//find = func(x int) int {
	//	if fa[x] != x {
	//		fa[x] = find(fa[x])
	//	}
	//	return fa[x]
	//}
	//merge := func(from, to int) { fa[find(from)] = find(to) }
	//same := func(x, y int) bool { return find(x) == find(y) }
	//countRoots := func() (cnt int) {
	//	for i := range fa {
	//		if find(i) == i {
	//			cnt++
	//		}
	//	}
	//	return
	//}

	//a := make([]int, n)
	//for i := range a {
	//	a[i] = i
	//}
	//rand.Shuffle(n, func(i, j int) {a[i], a[j] = a[j], a[i]})
	var lca int
	for len(edges) > 0 {
		//initFa(n)
		g := make([][]int, n)

		for e := range edges {
			v, w := e[0], e[1]
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
			//merge(v,w)
		}
		//if countRoots() != 1 {
		//	break
		//}
		if len(g[lca]) == 0 {
			break
		}

		var u, maxD int
		var vis []bool
		var f func(int, int)
		f = func(v, d int) {
			vis[v] = true
			if d > maxD {
				maxD = d
				u = v
			}
			for _, w := range g[v] {
				if !vis[w] {
					f(w, d+1)
				}
			}
		}

		maxD = -1
		vis = make([]bool, n)
		f(lca, 0)
		dv := u

		maxD = -1
		vis = make([]bool, n)
		f(dv, 0)
		dw := u

		Fprintf(out, "? %d %d\n", dv+1, dw+1)
		out.Flush()
		Fscan(in, &lca)
		lca--
		if lca == dv || lca == dw {
			break
		}

		var tar int
		var f2 func(v, fa int) bool
		f2 = func(v, fa int) bool {
			if v == tar {
				delete(edges, [2]int{v, fa})
				delete(edges, [2]int{fa, v})
				return true
			}
			for _, w := range g[v] {
				if w != fa {
					if f2(w, v) {
						delete(edges, [2]int{v, w})
						delete(edges, [2]int{w, v})
						return true
					}
				}
			}
			return false
		}
		tar = dv
		f2(lca, -1)
		tar = dw
		f2(lca, -1)
		//println()
	}
	Fprintf(out, "! %d\n", lca+1)
}

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

package main

import (
	"bufio"
	"fmt"
	"io"
	"math/rand"
	"os"
	"strconv"
	"time"
)

var reader io.Reader
var scanner *bufio.Scanner

func Init(io io.Reader, max_token_size int) {
	reader = bufio.NewReader(io)
	scanner = bufio.NewScanner(reader)
	// max token size
	scanner.Split(bufio.ScanWords)
	scanner.Buffer([]byte{}, max_token_size)
}

func stderr(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
}

func die(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
	os.Exit(1)
}

func NextInt() int {
	bytes := NextBytes()
	i, err := Atoi(bytes)
	if err != nil {
		die(err)
	}
	return i
}

func NextBytes() []byte {
	if !scanner.Scan() {
		die(scanner.Err())
	}
	bytes := scanner.Bytes()
	copied := make([]byte, len(bytes))
	copy(copied, bytes)
	return copied
}

func Atoi(b []byte) (int, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	n := 0
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func ExtendByteArray(a []byte, size int) []byte {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]byte, size-len(a))...)
	}
}

func ExtendIntArray(a []int, size int) []int {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]int, size-len(a))...)
	}
}

type Graph struct {
	n     int
	m     int
	edges int
	fst   []int
	nxt   []int
	end   []int
}

func (g *Graph) Add(a, b int) {
	g.nxt[g.edges] = g.fst[a]
	g.fst[a] = g.edges
	g.end[g.edges] = b
	g.edges++
}

func (g *Graph) Adjacent(a int, fn func(b int)) {
	for u := g.fst[a]; u != -1; u = g.nxt[u] {
		fn(g.end[u])
	}
}

func NewGraph(n, m int) *Graph {
	g := &Graph{n, m, 0, make([]int, n), make([]int, m), make([]int, m)}
	for i := 0; i < n; i++ {
		g.fst[i] = -1
	}
	return g
}

var leaf []int
var parent []int

func (g *Graph) Dfs(v int, par int) {
	leaf[v] = v
	parent[v] = par
	g.Adjacent(v, func(to int) {
		if to == par {
			return
		}
		g.Dfs(to, v)
		if leaf[v] == v || rand.Intn(2) == 0 {
			leaf[v] = leaf[to]
		}
	})
}

func lca(out io.Writer, u, v int) int {
	fmt.Fprintln(out, "?", u+1, v+1)
	// out.Flush()
	return NextInt() - 1
}

func found(out io.Writer, root int) {
	fmt.Fprintln(out, "!", root+1)
	// out.Flush()
}

func main() {
	inf := os.Stdin
	out := os.Stdout
	// inf, err := os.Open("a.in")
	// if err != nil {
	// 	die(err)
	// }
	// out, err := os.Create("a.out")
	// if err != nil {
	// 	die(err)
	// }
	// defer out.Close()
	Init(inf, 1000010)

	n := NextInt()
	leaf = make([]int, n)
	parent = make([]int, n)

	g := NewGraph(n, 2*n)
	for i := 0; i < n-1; i++ {
		u, v := NextInt()-1, NextInt()-1
		g.Add(u, v)
		g.Add(v, u)
	}
	rand.Seed(time.Now().UnixNano())
	root := rand.Intn(n)

	// stderr("root", root)
	g.Dfs(root, -1)

	l1, l2 := -1, -1
	possible := make([]int, 0)
	for {
		possible = possible[:0]
		leaf[root] = root
		g.Adjacent(root, func(to int) {
			if to == parent[root] {
				return
			}
			if leaf[to] == l1 || leaf[to] == l2 {
				return
			}
			possible = append(possible, to)
		})
		found := false
		if len(possible)%2 == 1 {
			possible = append(possible, root)
		}
		// stderr("possible", possible)
		for i := 0; i < len(possible); i += 2 {
			// stderr(possible[i], leaf[possible[i]])
			// stderr(possible[i+1], leaf[possible[i+1]])
			result := lca(out, leaf[possible[i]], leaf[possible[i+1]])
			if result != root {
				l1 = leaf[possible[i]]
				l2 = leaf[possible[i+1]]
				root = result
				found = true
				break
			}
		}
		if found {
			continue
		}
		break
	}

	found(out, root)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	var n, v, w, lca, tar int
	Fscan(in, &n)
	edges := map[[2]int]bool{}
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		edges[[2]int{v, w}] = true
	}

	for {
		g := make([][]int, n)
		for e := range edges {
			v, w := e[0], e[1]
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
		}
		if len(g[lca]) == 0 {
			break
		}

		// 求直径
		var u, maxD int
		var f func(v, fa, d int)
		f = func(v, fa, d int) {
			if d > maxD {
				maxD = d
				u = v
			}
			for _, w := range g[v] {
				if w != fa {
					f(w, v, d+1)
				}
			}
		}
		maxD = -1
		f(lca, -1, 0)
		dv := u
		maxD = -1
		f(dv, -1, 0)
		dw := u
		Println("?", dv+1, dw+1)
		Fscan(in, &lca)
		lca--
		if lca == dv || lca == dw {
			break
		}

		// 删除 LCA 到直径上的边
		var del func(v, fa int) bool
		del = func(v, fa int) bool {
			if v == tar {
				delete(edges, [2]int{v, fa})
				delete(edges, [2]int{fa, v})
				return true
			}
			for _, w := range g[v] {
				if w != fa {
					if del(w, v) {
						delete(edges, [2]int{v, w})
						delete(edges, [2]int{w, v})
						return true
					}
				}
			}
			return false
		}
		tar = dv
		del(lca, -1)
		tar = dw
		del(lca, -1)
	}
	Println("!", lca+1)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1305D() {
	in := bufio.NewReader(os.Stdin)
	var n, v, w, lca, tar int
	Fscan(in, &n)
	edges := map[[2]int]bool{}
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		edges[[2]int{v, w}] = true
	}

	for {
		g := make([][]int, n)
		for e := range edges {
			v, w := e[0], e[1]
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
		}
		if len(g[lca]) == 0 {
			break
		}

		// 求直径
		var u, maxD int
		var vis []bool
		var f func(int, int)
		f = func(v, d int) {
			vis[v] = true
			if d > maxD {
				maxD = d
				u = v
			}
			for _, w := range g[v] {
				if !vis[w] {
					f(w, d+1)
				}
			}
		}
		maxD = -1
		vis = make([]bool, n)
		f(lca, 0)
		dv := u
		maxD = -1
		vis = make([]bool, n)
		f(dv, 0)
		dw := u
		Println("?", dv+1, dw+1)
		Fscan(in, &lca)
		lca--
		if lca == dv || lca == dw {
			break
		}

		// 删除 LCA 到直径上的边
		var del func(v, fa int) bool
		del = func(v, fa int) bool {
			if v == tar {
				delete(edges, [2]int{v, fa})
				delete(edges, [2]int{fa, v})
				return true
			}
			for _, w := range g[v] {
				if w != fa {
					if del(w, v) {
						delete(edges, [2]int{v, w})
						delete(edges, [2]int{w, v})
						return true
					}
				}
			}
			return false
		}
		tar = dv
		del(lca, -1)
		tar = dw
		del(lca, -1)
	}
	Println("!", lca+1)
}

func main() { CF1305D() }

package main

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

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

	var n, lca, tar int
	Fscan(in, &n)
	edges := map[[2]int]bool{}
	for i := 0; i < n-1; i++ {
		var v, w int
		Fscan(in, &v, &w)
		v--
		w--
		edges[[2]int{v, w}] = true
	}

	for {
		g := make([][]int, n)
		for e := range edges {
			v, w := e[0], e[1]
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
		}
		if len(g[lca]) == 0 {
			break
		}

		var u, maxD int
		var vis []bool
		var f func(int, int)
		f = func(v, d int) {
			vis[v] = true
			if d > maxD {
				maxD = d
				u = v
			}
			for _, w := range g[v] {
				if !vis[w] {
					f(w, d+1)
				}
			}
		}
		maxD = -1
		vis = make([]bool, n)
		f(0, 0)
		dv := u
		maxD = -1
		vis = make([]bool, n)
		f(dv, 0)
		dw := u
		Fprintln(out, "?", dv+1, dw+1)
		out.Flush()
		Fscan(in, &lca)
		lca--
		if lca == dv || lca == dw {
			break
		}

		var del func(v, fa int) bool
		del = func(v, fa int) bool {
			if v == tar {
				delete(edges, [2]int{v, fa})
				delete(edges, [2]int{fa, v})
				return true
			}
			for _, w := range g[v] {
				if w != fa {
					if del(w, v) {
						delete(edges, [2]int{v, w})
						delete(edges, [2]int{w, v})
						return true
					}
				}
			}
			return false
		}
		tar = dv
		del(lca, -1)
		tar = dw
		del(lca, -1)
	}
	Fprintln(out, "!", lca+1)
}

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

package main

import (
	"bufio"
	"fmt"
	"io"
	"math/rand"
	"os"
	"strconv"
	"time"
)

var reader io.Reader
var scanner *bufio.Scanner

func Init(io io.Reader, max_token_size int) {
	reader = bufio.NewReader(io)
	scanner = bufio.NewScanner(reader)
	// max token size
	scanner.Split(bufio.ScanWords)
	scanner.Buffer([]byte{}, max_token_size)
}

func stderr(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
}

func die(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
	os.Exit(1)
}

func NextInt() int {
	bytes := NextBytes()
	i, err := Atoi(bytes)
	if err != nil {
		die(err)
	}
	return i
}

func NextBytes() []byte {
	if !scanner.Scan() {
		die(scanner.Err())
	}
	bytes := scanner.Bytes()
	copied := make([]byte, len(bytes))
	copy(copied, bytes)
	return copied
}

func Atoi(b []byte) (int, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	n := 0
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func ExtendByteArray(a []byte, size int) []byte {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]byte, size-len(a))...)
	}
}

func ExtendIntArray(a []int, size int) []int {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]int, size-len(a))...)
	}
}

type Graph struct {
	n     int
	m     int
	edges int
	fst   []int
	nxt   []int
	end   []int
}

func (g *Graph) Add(a, b int) {
	g.nxt[g.edges] = g.fst[a]
	g.fst[a] = g.edges
	g.end[g.edges] = b
	g.edges++
}

func (g *Graph) Adjacent(a int, fn func(b int)) {
	for u := g.fst[a]; u != -1; u = g.nxt[u] {
		fn(g.end[u])
	}
}

func NewGraph(n, m int) *Graph {
	g := &Graph{n, m, 0, make([]int, n), make([]int, m), make([]int, m)}
	for i := 0; i < n; i++ {
		g.fst[i] = -1
	}
	return g
}

var leaf []int
var parent []int

func (g *Graph) Dfs(v int, par int) {
	leaf[v] = v
	parent[v] = par
	g.Adjacent(v, func(to int) {
		if to == par {
			return
		}
		g.Dfs(to, v)
		if leaf[v] == v || rand.Intn(2) == 0 {
			leaf[v] = leaf[to]
		}
	})
}

func lca(out *bufio.Writer, u, v int) int {
	fmt.Fprintln(out, u+1, v+1)
	out.Flush()
	return NextInt() - 1
}

func found(out *bufio.Writer, root int) {
	fmt.Fprintln(out, "!", root+1)
	out.Flush()
}

func main() {
	inf := os.Stdin
	out := bufio.NewWriter(os.Stdout)
	// inf, err := os.Open("a.in")
	// if err != nil {
	// 	die(err)
	// }
	// out, err := os.Create("a.out")
	// if err != nil {
	// 	die(err)
	// }
	// defer out.Close()
	Init(inf, 1000010)

	n := NextInt()
	leaf = make([]int, n)
	parent = make([]int, n)

	g := NewGraph(n, 2*n)
	for i := 0; i < n-1; i++ {
		u, v := NextInt()-1, NextInt()-1
		g.Add(u, v)
		g.Add(v, u)
	}
	rand.Seed(time.Now().UnixNano())
	root := rand.Intn(n)

	// stderr("root", root)
	g.Dfs(root, -1)

	l1, l2 := -1, -1
	possible := make([]int, 0)
	for {
		possible = possible[:0]
		leaf[root] = root
		g.Adjacent(root, func(to int) {
			if to == parent[root] {
				return
			}
			if leaf[to] == l1 || leaf[to] == l2 {
				return
			}
			possible = append(possible, to)
		})
		found := false
		if len(possible)%2 == 1 {
			possible = append(possible, root)
		}
		// stderr("possible", possible)
		for i := 0; i < len(possible); i += 2 {
			// stderr(possible[i], leaf[possible[i]])
			// stderr(possible[i+1], leaf[possible[i+1]])
			result := lca(out, leaf[possible[i]], leaf[possible[i+1]])
			if result != root {
				l1 = leaf[possible[i]]
				l2 = leaf[possible[i+1]]
				root = result
				found = true
				break
			}
		}
		if found {
			continue
		}
		break
	}

	found(out, root)
}


package main

import (
    "bytes"
    "fmt"
)

func g ( s1, s2 string ) ( ret int ) {
    for i, len := 0, len(s1); i < len; i++ {
        if s1[i] == s2[i] {
            ret += 1
        }
    }
    return
}

func randByte ( a, b byte ) ( ret byte ) {
    for ret = 'a'; ret <= 'z' && (ret == a || ret == b); ret++ {}
    return
}

func solve ( s1, s2 string, cnt int ) (ret string) {
    c1 := cnt
    c2 := cnt
    var buf bytes.Buffer
    for i, n := 0, len(s1); i < n; i++ {
        if s1[i] != s2[i] {
            if c1 > 0 {
                buf.WriteByte(s1[i])
                c1--
            } else if c2 > 0 {
                buf.WriteByte(s2[i])
                c2--
            } else {
                buf.WriteByte(randByte(s1[i],s2[i]))
            }
        } else {
            if cnt >= 0 {
                buf.WriteByte(s1[i])
            } else {
                buf.WriteByte(randByte(s1[i],s2[i]))
                cnt++
            }
        }
    }
    return buf.String()
}

func main() {
    var x, y int
    var s1, s2 string
    fmt.Scan(&x,&y,&s1,&s2)
    y = x - y
    eq := g(s1,s2);

    if x - eq >= 2 * ( y - eq ) {
        fmt.Println(solve(s1,s2,y-eq))
    } else {
        fmt.Println(-1)
    }
}

package main

import (
	"fmt"
)

var (
	n, t int
	a, b string
)

func getDiff(a byte, b byte) byte {
	for i := 'a'; i <= 'b'; i++ {
		if byte(i) != a && byte(i) != b {
			return byte(i)
		}
	}
	return byte('c')
}

func solve() string {
	c := []byte(a)
	diffPos := make([]int, 0, n)
	samePos := make([]int, 0, n)
	for i := 0; i < n; i++ {
		if a[i] != b[i] {
			diffPos = append(diffPos, i)
		} else {
			samePos = append(samePos, i)
		}
	}
	lenDiff := len(diffPos)
	lenSame := len(samePos)
	needSame := n - t
	if needSame <= lenSame {
		for i := needSame; i < len(samePos); i++ {
			pos := samePos[i]
			c[pos] = getDiff(a[pos], b[pos])
		}
		for i := 0; i < lenDiff; i++ {
			pos := diffPos[i]
			c[pos] = getDiff(a[pos], b[pos])
		}
		return string(c)
	}
	needSame -= lenSame
	if needSame*2 > lenDiff {
		return "-1"
	}
	for i := 0; i < needSame; i++ {
		c[diffPos[i]] = a[diffPos[i]]
		c[diffPos[i+needSame]] = b[diffPos[i+needSame]]
	}
	for i := needSame * 2; i < len(diffPos); i++ {
		pos := diffPos[i]
		c[pos] = getDiff(a[pos], b[pos])
	}
	return string(c)
}

func main() {
	fmt.Scanf("%d %d ", &n, &t)
	fmt.Scanln(&a)
	fmt.Scanln(&b)
	//fmt.Println(" ", a, " ", b)
	fmt.Println(solve())
}

package main

import (
	"fmt"
)

var (
	n, t int
	a, b string
)

func getDiff(a byte, b byte) byte {
	for i := 'a'; i <= 'b'; i++ {
		if byte(i) != a && byte(i) != b {
			return byte(i)
		}
	}
	return byte('c')
}

func solve() string {
	c := []byte(a)
	diffPos := make([]int, 0, n)
	samePos := make([]int, 0, n)
	for i := 0; i < n; i++ {
		if a[i] != b[i] {
			diffPos = append(diffPos, i)
		} else {
			samePos = append(samePos, i)
		}
	}
	lenDiff := len(diffPos)
	lenSame := len(samePos)
	needSame := n - t
	if needSame <= lenSame {
		for i := needSame; i < lenSame; i++ {
			pos := samePos[i]
			c[pos] = getDiff(a[pos], b[pos])
		}
		for i := 0; i < lenDiff; i++ {
			pos := diffPos[i]
			c[pos] = getDiff(a[pos], b[pos])
		}
		return string(c)
	}
	needSame -= lenSame
	if needSame*2 > lenDiff {
		return "-1"
	}
	for i := 0; i < needSame; i++ {
		c[diffPos[i]] = a[diffPos[i]]
		c[diffPos[i+needSame]] = b[diffPos[i+needSame]]
	}
	for i := needSame * 2; i < lenDiff; i++ {
		pos := diffPos[i]
		c[pos] = getDiff(a[pos], b[pos])
	}
	return string(c)
}

func main() {
	fmt.Scanf("%d %d ", &n, &t)
	fmt.Scanln(&a)
	fmt.Scanln(&b)
	//fmt.Println(" ", a, " ", b)
	fmt.Println(solve())
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF584C(_r io.Reader, out io.Writer) {
	var n, needSame int
	var s, t []byte
	Fscan(bufio.NewReader(_r), &n, &needSame, &s, &t)
	needSame = n - needSame
	same := 0
	for i, b := range s {
		if b == t[i] {
			same++
		}
	}
	ans := make([]byte, n)
	if needSame <= same {
		for i, b := range s {
			c := t[i]
			if needSame > 0 && b == c {
				ans[i] = b
				needSame--
				continue
			}
			ans[i] = 'a'
			if ans[i] == b || ans[i] == c {
				ans[i]++
			}
			if ans[i] == b || ans[i] == c {
				ans[i]++
			}
		}
	} else if (needSame-same)*2 > n-same {
		Fprint(out, -1)
		return
	} else {
		needSame = (needSame - same) * 2
		cnt := 0
		for i, b := range s {
			c := t[i]
			if b == c {
				ans[i] = b
				continue
			}
			if cnt*2 < needSame {
				cnt++
				ans[i] = b
			} else if cnt < needSame {
				cnt++
				ans[i] = c
			} else {
				ans[i] = 'a'
				if ans[i] == b || ans[i] == c {
					ans[i]++
				}
				if ans[i] == b || ans[i] == c {
					ans[i]++
				}
			}
		}
	}
	Fprint(out, string(ans))
}

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

package main

import (
    "fmt"
)

func g ( s1, s2 string ) ( ret int ) {
    for i, len := 0, len(s1); i < len; i++ {
        if s1[i] == s2[i] {
            ret += 1
        }
    }
    return
}

func randByte ( a, b byte ) ( ret byte ) {
    for ret = 'a'; ret <= 'z' && (ret == a || ret == b); ret++ {}
    return
}

func solve ( s1, s2 string, cnt int ) (ret string) {
    c1 := cnt
    c2 := cnt
    for i, n := 0, len(s1); i < n; i++ {
        if s1[i] != s2[i] {
            if c1 > 0 {
                ret += string(s1[i])
                c1--
            } else if c2 > 0 {
                ret += string(s2[i])
                c2--
            } else {
                ret += string(randByte(s1[i],s2[i]))
            }
        } else {
            ret += string(s1[i])
        }
    }
    return
}

func main() {
    var x, y int
    var s1, s2 string
    fmt.Scan(&x,&y,&s1,&s2)
    y = x - y

    fmt.Println(g(s1,s2),y,y-g(s1,s2))

    if x - g(s1,s2) >= 2 * ( y - g(s1,s2) ) {
        fmt.Println(solve(s1,s2,y-g(s1,s2)))
    } else {
        fmt.Println(-1)
    }
}

package main

import (
    "fmt"
)

func g ( s1, s2 string ) ( ret int ) {
    for i, len := 0, len(s1); i < len; i++ {
        if s1[i] == s2[i] {
            ret += 1
        }
    }
    return
}

func randByte ( a, b byte ) ( ret byte ) {
    for ret = 'a'; ret <= 'z' && (ret == a || ret == b); ret++ {}
    return
}

func solve ( s1, s2 string, cnt int ) (ret string) {
    c1 := cnt
    c2 := cnt
    for i, n := 0, len(s1); i < n; i++ {
        if s1[i] != s2[i] {
            if c1 > 0 {
                ret += string(s1[i])
                c1--
            } else if c2 > 0 {
                ret += string(s2[i])
                c2--
            } else {
                ret += string(randByte(s1[i],s2[i]))
            }
        } else {
            if cnt >= 0 {
                ret += string(s1[i])
            } else {
                ret += string(randByte(s1[i],s2[i]))
            }
        }
    }
    return
}

func main() {
    var x, y int
    var s1, s2 string
    fmt.Scan(&x,&y,&s1,&s2)
    y = x - y

    if x - g(s1,s2) >= 2 * ( y - g(s1,s2) ) {
        fmt.Println(solve(s1,s2,y-g(s1,s2)))
    } else {
        fmt.Println(-1)
    }
}

package main

import (
    "fmt"
)

func g ( s1, s2 string ) ( ret int ) {
    for i, len := 0, len(s1); i < len; i++ {
        if s1[i] == s2[i] {
            ret += 1
        }
    }
    return
}

func randByte ( a, b byte ) ( ret byte ) {
    for ret = 'a'; ret <= 'z' && (ret == a || ret == b); ret++ {}
    return
}

func solve ( s1, s2 string, cnt int ) (ret string) {
    c1 := cnt
    c2 := cnt
    for i, n := 0, len(s1); i < n; i++ {
        if s1[i] != s2[i] {
            if c1 > 0 {
                ret += string(s1[i])
                c1--
            } else if c2 > 0 {
                ret += string(s2[i])
                c2--
            } else {
                ret += string(randByte(s1[i],s2[i]))
            }
        } else {
            if cnt > 0 {
                ret += string(s1[i])
            } else {
                ret += string(randByte(s1[i],s2[i]))
            }
        }
    }
    return
}

func main() {
    var x, y int
    var s1, s2 string
    fmt.Scan(&x,&y,&s1,&s2)
    y = x - y

    if x - g(s1,s2) >= 2 * ( y - g(s1,s2) ) {
        fmt.Println(solve(s1,s2,y-g(s1,s2)))
    } else {
        fmt.Println(-1)
    }
}

package main

import (
    "fmt"
)

func g ( s1, s2 string ) ( ret int ) {
    for i, len := 0, len(s1); i < len; i++ {
        if s1[i] == s2[i] {
            ret += 1
        }
    }
    return
}

func randByte ( a, b byte ) ( ret byte ) {
    for ret = 'a'; ret <= 'z' && (ret == a || ret == b); ret++ {}
    return
}

func solve ( s1, s2 string, cnt int ) (ret string) {
    c1 := cnt
    c2 := cnt
    for i, n := 0, len(s1); i < n; i++ {
        if s1[i] != s2[i] {
            if c1 > 0 {
                ret += string(s1[i])
                c1--
            } else if c2 > 0 {
                ret += string(s2[i])
                c2--
            } else {
                ret += string(randByte(s1[i],s2[i]))
            }
        } else {
            ret += string(s1[i])
        }
    }
    return
}

func main() {
    var x, y int
    var s1, s2 string
    fmt.Scan(&x,&y,&s1,&s2)
    y = x - y

    if x - g(s1,s2) >= 2 * ( y - g(s1,s2) ) {
        fmt.Println(solve(s1,s2,y-g(s1,s2)))
    } else {
        fmt.Println(-1)
    }
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF584C(_r io.Reader, out io.Writer) {
	var n, needSame int
	var s, t []byte
	Fscan(bufio.NewReader(_r), &n, &needSame, &s, &t)
	needSame = n - needSame
	same := 0
	for i, b := range s {
		if b == t[i] {
			same++
		}
	}
	ans := make([]byte, n)
	if needSame <= same {
		for i, b := range s {
			c := t[i]
			if needSame > 0 && b == c {
				ans[i] = b
				needSame--
				continue
			}
			ans[i] = 'a'
			if ans[i] == b || ans[i] == c {
				ans[i]++
			}
			if ans[i] == b || ans[i] == c {
				ans[i]++
			}
		}
	} else if (needSame-same)*2 != n-same {
		Fprint(out, -1)
		return
	} else {
		needSame -= same
		for i, b := range s {
			c := t[i]
			if b == c {
				ans[i] = b
				continue
			}
			if needSame > 0 {
				needSame--
				ans[i] = b
			} else {
				ans[i] = c
			}
		}
	}
	Fprint(out, string(ans))
}

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


package main

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

var (
	E    int = 1 << 20
	line     = make([]Point, 4000100)
	t    int64
)
type Point struct {
	t,c int64

}
type Edge struct {
	to, value int
}

func addEdge(g [][]*Edge, from, to, value int) {
	//fmt.Printf("fromt = %d to=%d\n", from, to)
	if len(g[from]) == 0 {
		g[from] = make([]*Edge, 0)
	}
	g[from] = append(g[from], &Edge{
		to:    to,
		value: value,
	})
}
func location(p int) int {
	return E + p-1
}
func update(pre int, t,c int64) {
//fmt.Printf("update pre=%d t=%d c=%d\n",pre,t,c)
	//tp := pre
	pre = location(pre)
	line[pre].c+=c
	line[pre].t += t
	pre>>=1
	for pre > 0 {
		line[pre].c = line[pre<<1].c+line[1+(pre<<1)].c
		line[pre].t = line[pre<<1].t+line[1+(pre<<1)].t
		pre >>= 1
	}
	//fmt.Printf("--update pre=%d t=%d c=%d\n",pre,line[1].t,line[1].c)
	//fmt.Printf("--update pre=%d t=%d c=%d\n",pre,line[2].t,line[2].c)
	//fmt.Printf("--update pre=%d t=%d c=%d\n",pre,line[3].t,line[3].c)
}
func query(tt int64) (int64) {
	var sumt int64 = 0
	var sumc int64 = 0
	var root int = 1
	for  {
		if line[root*2].t+sumt > tt {
			root <<= 1
		} else {
			sumt += line[root*2].t
			sumc += line[root*2].c
			root = (root << 1) + 1
		}
		if root >= E{
		//	fmt.Printf("query tt=%d sumt=%d sumc=%d line.c=%d line.t=%d\n",tt,sumt,sumc,line[root].c,line[root].t)
			if sumt+line[root].t <= tt{
				return sumc+line[root].c
			}else{
				return sumc + (tt-sumt)*line[root].c/line[root].t
			}
		}
	}
}
func dfs(root int, g [][]*Edge, ti, cookies []int, costT int64) int64 {
	if costT > t{
		return 0
	}
	update(ti[root],int64(cookies[root])*int64(ti[root]), int64(cookies[root]))
	max := query(t - costT)
//	fmt.Printf("root =%d max1=%d tt=%d\n",root,max,t-costT)
	var max2 int64 = -1
	var max1 int64 = -1
	for _, next := range g[root] {
		tmp := dfs(next.to, g, ti, cookies, costT+int64(next.value*2))
		if max2 == -1 || max2 < tmp {
			max2 = tmp
			if max2 > max1 {
				sw := max1
				max1 = max2
				max2 = sw
			}
		}
	}

	update(ti[root],-int64(cookies[root])*int64(ti[root]), -int64(cookies[root]))
	if root == 1 {
		if max1!=-1 && max1 > max{
			max = max1
		}
	} else {
		if max2!=-1 && max2 > max{
			max = max2
		}
	}
//	fmt.Printf("====>res root =%d ans=%d\n",root,max)
	return max
}
func main() {
	buf := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscanf(buf, "%d %d\n", &n, &t)
	cookies := make([]int, n+1)
	g := make([][]*Edge, n+1)
	ti := make([]int, n+1)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &cookies[i+1])
	}
	fmt.Fscanf(buf, "\n")
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &ti[i+1])
	}
	//	fmt.Print(ti)
	fmt.Fscanf(buf, "\n")
	for i := 2; i <= n; i++ {
		var value int = 0
		var from int = 0
		fmt.Fscanf(buf, "%d %d\n", &from, &value)
		addEdge(g, from, i, value)
	}
	fmt.Println(dfs(1, g, ti, cookies, 0))

}

/**
12 10
1 2
1 3
1 4
2 5
2 6
2 7
3 8
5 9
5 10
7 11

 */

package main

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

var (
	E    int = 1 << 20
	line     = make([]Point, 4000100)
	t    int64
)
type Point struct {
	t,c int64

}
type Edge struct {
	to, value int
}

func addEdge(g [][]*Edge, from, to, value int) {
	//fmt.Printf("fromt = %d to=%d\n", from, to)
	if len(g[from]) == 0 {
		g[from] = make([]*Edge, 0)
	}
	g[from] = append(g[from], &Edge{
		to:    to,
		value: value,
	})
}
func location(p int) int {
	return E + p-1
}
func update(pre int, t,c int64) {
//fmt.Printf("update pre=%d t=%d c=%d\n",pre,t,c)
	//tp := pre
	pre = location(pre)
	line[pre].c+=c
	line[pre].t += t
	pre>>=1
	for pre > 0 {
		line[pre].c = line[pre<<1].c+line[1+(pre<<1)].c
		line[pre].t = line[pre<<1].t+line[1+(pre<<1)].t
		pre >>= 1
	}
	//fmt.Printf("--update pre=%d t=%d c=%d\n",pre,line[1].t,line[1].c)
	//fmt.Printf("--update pre=%d t=%d c=%d\n",pre,line[2].t,line[2].c)
	//fmt.Printf("--update pre=%d t=%d c=%d\n",pre,line[3].t,line[3].c)
}
func query(tt int64) (int64) {
	var sumt int64 = 0
	var sumc int64 = 0
	var root int = 1
	for  {
		if line[root*2].t+sumt > tt {
			root <<= 1
		} else {
			sumt += line[root*2].t
			sumc += line[root*2].c
			root = (root << 1) + 1
		}
		if root >= E{
		//	fmt.Printf("query tt=%d sumt=%d sumc=%d line.c=%d line.t=%d\n",tt,sumt,sumc,line[root].c,line[root].t)
			if line[root].t ==0 || sumt+line[root].t <= tt{
				return sumc+line[root].c
			}else{
				return sumc + (tt-sumt)*line[root].c/line[root].t
			}
		}
	}
}
func dfs(root int, g [][]*Edge, ti, cookies []int, costT int64) int64 {
	//if costT > t{
	//	return 0
	//}
	update(ti[root],int64(cookies[root])*int64(ti[root]), int64(cookies[root]))
	max := query(t - costT)
//	fmt.Printf("root =%d max1=%d tt=%d\n",root,max,t-costT)
	var max2 int64 = -1
	var max1 int64 = -1
	for _, next := range g[root] {
		tmp := dfs(next.to, g, ti, cookies, costT+int64(next.value*2))
		if max2 == -1 || max2 < tmp {
			max2 = tmp
			if max2 > max1 {
				sw := max1
				max1 = max2
				max2 = sw
			}
		}
	}

	update(ti[root],-int64(cookies[root])*int64(ti[root]), -int64(cookies[root]))
	if root == 1 {
		if max1!=-1 && max1 > max{
			max = max1
		}
	} else {
		if max2!=-1 && max2 > max{
			max = max2
		}
	}
//	fmt.Printf("====>res root =%d ans=%d\n",root,max)
	return max
}
func main() {
	buf := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscanf(buf, "%d %d\n", &n, &t)
	cookies := make([]int, n+1)
	g := make([][]*Edge, n+1)
	ti := make([]int, n+1)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &cookies[i+1])
	}
	fmt.Fscanf(buf, "\n")
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &ti[i+1])
	}
	//	fmt.Print(ti)
	fmt.Fscanf(buf, "\n")
	for i := 2; i <= n; i++ {
		var value int = 0
		var from int = 0
		fmt.Fscanf(buf, "%d %d\n", &from, &value)
		//if n == 77&&t == 63{
		//	fmt.Printf("%d %d\n",from,value)
		//}
		addEdge(g, from, i, value)
	}
	fmt.Println(dfs(1, g, ti, cookies, 0))

}

/**
77 63
6 3 10 5 9 6 6 1 1 5 7 7 1 8 4 4 9 6 3 10 1 1 7 4 5 2 3 6 10 4 5 7 5 8 3 1 10 5 8 5 3 2 3 6 10 10 2 4 4 8 8 5 2 1 2 3 6 9 8 6 4 1 2 7 1 1 5 3 1 1 8 10 10 8 6 7 5
5 6 3 8 6 10 8 5 2 4 4 8 2 1 10 7 1 4 3 4 5 7 5 4 1 8 8 7 5 10 10 9 2 7 4 10 5 2 2 4 4 2 10 2 4 2 9 10 6 2 4 2 10 5 7 10 1 3 1 4 2 8 9 4 7 6 10 10 9 5 6 3 1 4 1 3 3
1 6
1 7
2 6
3 6
2 2
2 8
5 7
4 2
5 5
8 6
2 5
8 8
5 8
13 1
1 1
7 7
13 3
1 0
13 8
4 1
10 9
1 5
14 3
22 2
6 7
10 9
26 9
7 1
8 10
12 10
14 9
31 5
13 0
4 3
1 5
4 3
34 10
24 2
18 9
27 3
5 1
41 8
38 10
25 0
26 4
30 8
14 1
18 2
34 3
1 2
31 0
36 0
52 4
29 3
13 2
35 9
22 10
21 6
34 0
10 5
55 6
57 5
47 5
48 10
3 3
17 3
67 1
39 5
39 7
52 5
22 0
31 9
56 1
43 0
14 3
46 10

 */

package main

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

var (
	E    int = 1 << 20
	line     = make([]Point, 4000100)
	t    int64
)
type Point struct {
	t,c int64

}
type Edge struct {
	to, value int
}

func addEdge(g [][]*Edge, from, to, value int) {
	//fmt.Printf("fromt = %d to=%d\n", from, to)
	if len(g[from]) == 0 {
		g[from] = make([]*Edge, 0)
	}
	g[from] = append(g[from], &Edge{
		to:    to,
		value: value,
	})
}
func location(p int) int {
	return E + p-1
}
func update(pre int, t,c int64) {
//fmt.Printf("update pre=%d t=%d c=%d\n",pre,t,c)
	//tp := pre
	pre = location(pre)
	line[pre].c+=c
	line[pre].t += t
	pre>>=1
	for pre > 0 {
		line[pre].c = line[pre<<1].c+line[1+(pre<<1)].c
		line[pre].t = line[pre<<1].t+line[1+(pre<<1)].t
		pre >>= 1
	}
	//fmt.Printf("--update pre=%d t=%d c=%d\n",pre,line[1].t,line[1].c)
	//fmt.Printf("--update pre=%d t=%d c=%d\n",pre,line[2].t,line[2].c)
	//fmt.Printf("--update pre=%d t=%d c=%d\n",pre,line[3].t,line[3].c)
}
func query(tt int64) (int64) {
	var sumt int64 = 0
	var sumc int64 = 0
	var root int = 1
	for  {
		if line[root*2].t+sumt > tt {
			root <<= 1
		} else {
			sumt += line[root*2].t
			sumc += line[root*2].c
			root = (root << 1) + 1
		}
		if root >= E{
		//	fmt.Printf("query tt=%d sumt=%d sumc=%d line.c=%d line.t=%d\n",tt,sumt,sumc,line[root].c,line[root].t)
			if sumt+line[root].t <= tt{
				return sumc+line[root].c
			}else{
				return sumc + (tt-sumt)*line[root].c/line[root].t
			}
		}
	}
}
func dfs(root int, g [][]*Edge, ti, cookies []int, costT int64) int64 {
	if costT > t{
		return 0
	}
	update(ti[root],int64(cookies[root])*int64(ti[root]), int64(cookies[root]))
	max := query(t - costT)
//	fmt.Printf("root =%d max1=%d tt=%d\n",root,max,t-costT)
	var max2 int64 = -1
	var max1 int64 = -1
	for _, next := range g[root] {
		tmp := dfs(next.to, g, ti, cookies, costT+int64(next.value*2))
		if max2 == -1 || max2 < tmp {
			max2 = tmp
			if max2 > max1 {
				sw := max1
				max1 = max2
				max2 = sw
			}
		}
	}

	update(ti[root],-int64(cookies[root])*int64(ti[root]), -int64(cookies[root]))
	if root == 1 {
		if max1!=-1 && max1 > max{
			max = max1
		}
	} else {
		if max2!=-1 && max2 > max{
			max = max2
		}
	}
//	fmt.Printf("====>res root =%d ans=%d\n",root,max)
	return max
}
func main() {
	buf := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscanf(buf, "%d %d\n", &n, &t)
	cookies := make([]int, n+1)
	g := make([][]*Edge, n+1)
	ti := make([]int, n+1)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &cookies[i+1])
	}
	fmt.Fscanf(buf, "\n")
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &ti[i+1])
	}
	//	fmt.Print(ti)
	fmt.Fscanf(buf, "\n")
	for i := 2; i <= n; i++ {
		var value int = 0
		var from int = 0
		fmt.Fscanf(buf, "%d %d\n", &from, &value)
		if n == 77&&t == 63{
			fmt.Printf("%d %d\n",from,value)
		}
		addEdge(g, from, i, value)
	}
	fmt.Println(dfs(1, g, ti, cookies, 0))

}

/**
12 10
1 2
1 3
1 4
2 5
2 6
2 7
3 8
5 9
5 10
7 11

 */


package main

import (
	"fmt"
)

func solve() {
	var s string
	fmt.Scanf("%s\n", &s)

	x := make(map[byte]bool)
	rs := []byte(s)
	for i := 0; i < 6; i += 1 {
		if rs[i] >= 'a' && rs[i] <= 'z' {
			x[rs[i]] = true
		} else {
			c := 'a' + rs[i] - 'A'
			if !x[c] {
				fmt.Printf("NO\n")
				return
			}
		}
	}
	fmt.Printf("YES\n")

}

func main() {
	var T int
	for fmt.Scanf("%d\n", &T); T > 0; T-- {
		solve()
	}

}

package main
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var input string
		fmt.Fscan(in,&input)
		if string(input[0]) == "R" || string(input[0]) == "B" || string(input[0]) == "G" {
			fmt.Fprintln(out,"NO")
		} else {
			var r,b,g int
			for j:=0;j<6;j++{
				if string(input[j]) == "r" {
					r = j
				} else if string(input[j]) == "b" {
					b = j
				} else if string(input[j]) == "g" {
					g = j
				}
			}
			counter:=0
			for j:=0;j<6;j++{
				if string(input[j]) == "R" {
					if r > j {
						counter++
					}
				} else if string(input[j]) == "G" {
					if g > j {
						counter++
					}
				} else if string(input[j]) == "B" {
					if b > j {
						counter++
					}
				}
			}
			if counter > 0 {
				fmt.Fprintln(out,"NO")
			} else {
				fmt.Fprintln(out,"YES")
			}
		}
	}
}

package main

import "fmt"
import "strings"

func main() {
    var t int
    fmt.Scanln(&t)

    var s string
    for i := 0; i < t; i++ {
        fmt.Scanln(&s)

        a := strings.Index(s, "r") < strings.Index(s, "R")
        b := strings.Index(s, "g") < strings.Index(s, "G")
        c := strings.Index(s, "b") < strings.Index(s, "B")

        if a && b && c {
            fmt.Println("YES")
        } else {
            fmt.Println("NO")
        }
    }
}

//nolint
package main

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

//{
var reader = bufio.NewReader(os.Stdin)
var 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 print(a ...interface{})            { fmt.Fprint(writer, a...) }
func println(a ...interface{})          { fmt.Fprintln(writer, a...) }
func scan(a ...interface{})             { fmt.Fscan(reader, a...) }

func main() {
	defer writer.Flush()

	Main()
}

func Main() {
	var t int
	scan(&t)
	for ; t > 0; t-- {
		Solve()
	}
}

//}

var cR, cG, cB int

func Solve() {
	var st string
	cR, cG, cB = 0, 0, 0
	scan(&st)
	for _, c := range st {
		switch c {
		case 'r':
			cR++
		case 'g':
			cG++
		case 'b':
			cB++
		case 'R':
			cR--
		case 'G':
			cG--
		case 'B':
			cB--
		}
		if isFail() {
			println("NO")
			return
		}
	}
	println("YES")
}

func isFail() bool {
	if cR < 0 || cG < 0 || cB < 0 {
		return true
	}
	return false
}

package main

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

type CF1644A struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1644A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1644A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1644A) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1644A) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1644A) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1644A
Date: 6/03/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1644/A
Problem: CF1644A
**/
func (in *CF1644A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		str := in.NextString()
		r,g,b := false, false, false
		ans := true
		for i:=0;i<len(str);i++ {
			if str[i] == 'r' {
				r = true
			}else if str[i] == 'g' {
				g = true
			} else if str[i] == 'b' {
				b = true
			} else if str[i] == 'R' {
				if !r {
					ans = false
				}else {
					r = false
				}
			}else if str[i] == 'G' {
				if !g {
					ans = false
				}else {
					g = false
				}
			} else if str[i] == 'B' {
				if !b {
					ans = false
				} else {
					b = false
				}
			}
		}
		if ans {
			fmt.Println("YES")
		}else {
			fmt.Println("NO")
		}
	}
}

func NewCF1644A(r *bufio.Reader) *CF1644A {
	return &CF1644A{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1644A(bufio.NewReader(os.Stdin)).Run()
}

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 solve()  {
	var s string
	set := make(map[string]bool) // New empty set
	scanf("%s\n", &s)
	for _, ch := range s {
		if ch >= 'a' && ch <= 'z' {
			set[string(ch)] = true
		} else {
			if !set[string(ch+32)]  {
				printf("NO\n")
				return
			}
		}
	}
	printf("YES\n")
}

func main() {
	// STDOUT MUST BE FLUSHED MANUALLY!!!
	defer writer.Flush()
	var t int
	scanf("%d\n", &t)
	for i := 1 ; i <= t; i++ {
		solve()
	}
}

package main

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

type Scanner struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) *Scanner {
	scanner := bufio.NewScanner(reader)
	scanner.Split(bufio.ScanWords)
	return &Scanner{scanner}
}

func (s *Scanner) Word() string {
	s.Scan()
	return s.Text()
}

func (s *Scanner) Int() int {
	s.Scan()
	n, _ := strconv.Atoi(s.Text())
	return n
}

func (s *Scanner) Int64() int64 {
	s.Scan()
	n, _ := strconv.ParseInt(s.Text(), 10, 64)
	return n
}

func (s *Scanner) Words(count int) []string {
	words := make([]string, count)
	for i := range words {
		words[i] = s.Word()
	}
	return words
}

func (s *Scanner) Ints(count int) []int {
	ints := make([]int, count)
	for i := range ints {
		ints[i] = s.Int()
	}
	return ints
}

func (s *Scanner) Int64s(count int) []int64 {
	int64s := make([]int64, count)
	for i := range int64s {
		int64s[i] = s.Int64()
	}
	return int64s
}

func solve(str string) bool {
	keys := make(map[rune]struct{})
	for _, ch := range []rune(str) {
		if ch == unicode.ToLower(ch) {
			keys[unicode.ToUpper(ch)] = struct{}{}
		} else if _, has := keys[ch]; !has {
			return false
		}
	}
	return true
}

func main() {
	stdin := NewWordScanner(os.Stdin)
	tests := stdin.Int()

	for i := 0; i < tests; i += 1 {
		if str := stdin.Word(); solve(str) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

func solve(scanner *bufio.Scanner) {
	scanner.Scan()
	cur := scanner.Text()
	r, g, b := false, false, false
	for _, ru := range cur {
		switch rune(ru) {
		case 'r':
			r = true
			continue
		case 'g':
			g = true
			continue
		case 'b':
			b = true
			continue
		case 'R':
			if r {
				continue
			} else {
				fmt.Println("NO")
				return
			}
		case 'G':
			if g {
				continue
			} else {
				fmt.Println("NO")
				return
			}
		case 'B':
			if b {
				continue
			} else {
				fmt.Println("NO")
				return
			}
		default:
			break
		}
	}
	fmt.Println("YES")
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < t; i++ {
		solve(scanner)
	}

}

package main

import (
	"fmt"
	"unicode"
)

func main() {
	t := 0
	fmt.Scan(&t)
	strings := make([]string, t)
	for i := 0; i < t; i++ {
		fmt.Scan(&strings[i])
	}
	for i := 0; i < t; i++ {
		solve(strings[i])
	}
}

func solve(hallway string) {
	keys := map[rune]bool{
		'r': false,
		'g': false,
		'b': false,
	}
	canReach := true
	for _, c := range hallway {
		switch c {
		case 'r':
			keys['r'] = true
		case 'g':
			keys['g'] = true
		case 'b':
			keys['b'] = true
		default:
			if !keys[unicode.ToLower(c)] {
				canReach = false
			}
		}
	}
	if canReach {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}

}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	t      int
	x      string
)

func solve(x string) string {
	y := make(map[byte]int)
	for j := range x {
		y[x[j]] = j
	}
	if (y['r'] > y['R']) || (y['g'] > y['G']) || (y['b'] > y['B']) {
		return "NO"
	}
	return "YES"
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &x)
		fmt.Println(solve(x))
	}
}

package main

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

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

func CF1644A(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriter(_w)
	defer out.Flush()
	const keys = "rgb"
	const doors = "RGB"
	c := 0
	fmt.Fscan(in, &c)
	for ; c > 0; c-- {
		str := ""
		fmt.Fscan(in, &str)
		mp := make(map[rune]int, 3)
		for _, ch := range str {
			for _, chd := range doors {
				if ch == chd {
					if mp[ch+32] > 0 {
						break
					} else {
						fmt.Fprintln(out, "NO")
						goto out
					}
				}
			}
			for _, chk := range keys {
				if ch == chk {
					mp[chk]++
				}
			}
		}
		fmt.Fprintln(out, "YES")
	out:
	}
}

package main

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

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readString(in *bufio.Reader) string {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	return nStr
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

func Solve(in string) string {
	var keys []string

	for _, v := range in {
		char := string(v)
		if char == "r" || char == "g" || char == "b" {
			keys = append(keys, char)
		} else {
			found := false

			for _, c := range keys {
				if strings.ToLower(char) == string(c) {
					found = true
				}
			}

			if !found {
				return "NO"
			}
		}
	}

	return "YES"
}

func main() {
	in := bufio.NewReader(os.Stdin)
	n := readInt(in)

	for i := 0; i < n; i++ {
		input := readString(in)

		res := Solve(input)

		fmt.Printf("%s\n", res)
	}
}

package main

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

/*

 */

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var s string
	scanf("%s\n", &s)

	hm := make(map[rune]int)

	for i, r := range s {
		hm[r] = i
	}

	if hm['r'] < hm['R'] && hm['g'] < hm['G'] && hm['b'] < hm['B'] {
		fmt.Fprintln(writer, "YES")
	} else {
		fmt.Fprintln(writer, "NO")
	}
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

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

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int) int {
	quot := num / div
	if num%div != 0 {
		quot++
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int64) int64 {
	minimum := nums[0]
	for _, num := range nums {
		if num < minimum {
			minimum = num
		}
	}
	return minimum
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) (max, idx int) {
	for i, num := range nums {
		if num > nums[idx] {
			idx = i
		}
	}

	return nums[idx], idx
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

package main

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

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 s string
		fmt.Fscan(in, &s)

		k := make([]bool, 3)
		f := true
		for _, ch := range s {
			if ch == 'R' && !k[0] {
				f = false
				break
			} else if ch == 'G' && !k[1] {
				f = false
				break
			} else if ch == 'B' && !k[2] {
				f = false
				break
			} else if ch == 'r' {
				k[0] = true
			} else if ch == 'g' {
				k[1] = true
			} else if ch == 'b' {
				k[2] = true
			}
		}

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

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, s := 0, ""
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &s)
		ok, r, g, b := true, false, false, false
		for j := 0; j < len(s); j++ {
			if string(s[j]) == "r" {
				r = true
			}
			if string(s[j]) == "b" {
				b = true
			}
			if string(s[j]) == "g" {
				g = true
			}
			if string(s[j]) == "R" {
				if !r {
					ok = false
				}
			} else if string(s[j]) == "G" {
				if !g {
					ok = false
				}
			} else if string(s[j]) == "B" {
				if !b {
					ok = false
				}
			}
		}
		if ok {
			fmt.Fprintln(w, "YES")
		} else {
			fmt.Fprintln(w, "NO")
		}
	}
	w.Flush()
}

func main() {
	solve()
}


package main

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

func main() {
	var reader *bufio.Reader = bufio.NewReader(os.Stdin)
	var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var T int
	fmt.Scan(&T)
	var n int
	var k int
	var s string
	for t := 0; t < T; t++ {
		fmt.Fscan(reader, &n, &s, &k)
		// fmt.Println(n, s, k)
		ks := make(map[byte]bool)
		var ki string
		for i := 0; i < k; i++ {
			fmt.Fscan(reader, &ki)
			ks[ki[0]] = true
		}
		ans := 0
		count := 0
		for i := 0; i < n; i++ {
			if ks[s[i]] {
				if count > ans {
					ans = count
				}
				count = 1
			} else {
				count++
			}
		}
		fmt.Fprintln(writer, ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int     { i,e := strconv.Atoi(gs()); if e != nil {panic(e)}; return i }
func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
	T := gi()
	for tt:=1;tt<=T;tt++ {
		N := gi(); S := gs(); K := gi()
		special := make([]bool,26); for i:=0;i<K;i++ { ks := gs(); special[ks[0]-'a'] = true }
		streaks := make([]int,0); found := false; pre := 0
		for i:=N-1;i>=0;i-- {
			c := S[i]
			if special[c-'a'] {
				if !found { found = true; pre = 0 } else { streaks = append(streaks,pre); pre = 0 }
			} else {
				pre++
			}
		}
		if found { streaks = append(streaks,pre) }
		ans := 0
		if found {
			for i:=len(streaks)-1;i>=0;i-- {
				if i == len(streaks)-1 { ans = streaks[i]; continue }
				if streaks[i] < ans { continue }
				ans = streaks[i]+1
			}
		}
		fmt.Fprintln(wrtr,ans)
	}
}

package main

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

var r = bufio.NewReader(os.Stdin)
var w = bufio.NewWriter(os.Stdout)

func slove(T int) {
	defer w.Flush()
	var SSS int
	fmt.Fscan(r, &SSS)
	var s string
	fmt.Fscan(r, &s)
	var m int
	fmt.Fscan(r, &m)
	cnt := map[byte]bool{}
	for i:=0;i<m;i++ {
		var ss string
		fmt.Fscan(r, &ss)
		cnt[ss[0]] = true
	}
	idx := make([]int, 0)
	last := 0
	ans := 0
	for i := range s {
		if _, ok := cnt[s[i]]; ok {
			idx = append(idx, i)
			ans = max(ans, i - last)
			last = i
		}
	}
	fmt.Fprintln(w, ans)
}

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

func main() {
	var n int
	fmt.Fscan(r, &n)
	for i:=1;i<=n;i++ {
		slove(i)
	}
}

package main

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

/*
4
7
jhasdfg
3 a s d
7
ssfjasd
2 h f
7
klhjgfd
2 l j
12
aafbbbgccch
3 f g h
---
2 jhasdfg -> jdfg -> dfg
2 ssfjasd -> sfjasd -> fjasd
2
4 aafbbbgccch -> afbbgcch -> fbgch -> fgh -> h

klhjgfd

0 1
*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int
	var s string
	scanf("%d\n", &n)
	scanf("%s\n", &s)

	var m int
	scanf("%d", &m)

	runeToSpec := make(map[rune]bool)
	for i := 0; i < m; i++ {
		var r rune
		scanf(" %c", &r)

		runeToSpec[r] = true
	}
	scanf("\n")

	lastSpecIdx := -1
	maxSpecRange := 0

	for i, r := range s {
		if runeToSpec[r] {
			rng := i - lastSpecIdx
			if lastSpecIdx == -1 {
				rng--
			}
			maxSpecRange = max(maxSpecRange, rng)
			lastSpecIdx = i
		}
	}

	fmt.Fprintln(writer, maxSpecRange)
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

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

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int) int {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func sum(nums ...int) (sum int) {
	for _, num := range nums {
		sum += num
	}
	return
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int64) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := int64(5); i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

//goland:noinspection GoUnusedFunction
func factorize(num int64) []int64 {
	factors := make([]int64, 0)

	for ftr := int64(2); ftr*ftr <= num; ftr++ {
		for num%ftr == 0 {
			num /= ftr
			factors = append(factors, ftr)
		}
	}

	if num > 1 {
		factors = append(factors, num)
	}

	return factors
}

package main

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

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

func main() {
	defer out.Flush()

	var tc int
	for fmt.Fscan(in, &tc); tc > 0; tc-- {
		var n, k int
		var s string
		var special [26]bool
		fmt.Fscan(in, &n, &s, &k)
		for ; k > 0; k-- {
			var c string
			fmt.Fscan(in, &c)
			special[c[0]-'a'] = true
		}

		curRun, maxRun := -1, 0
		for i := len(s) - 1; i >= 0; i-- {
			if curRun != -1 {
				curRun++
				if curRun > maxRun {
					maxRun = curRun
				}
			}
			if special[s[i]-'a'] {
				curRun = 0
			}
		}
		fmt.Fprintln(out, maxRun)
	}
}

package main

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

func main() {
	in := bufio.NewScanner(os.Stdin)
	in.Split(bufio.ScanWords)
	in.Buffer(nil, 1e9)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	nextInt := func() int {
		in.Scan()
		vv, _ := strconv.Atoi(in.Text())
		return vv
	}

	nextStr := func() string {
		in.Scan()
		return in.Text()
	}

	t := nextInt()
	sol := func() int {
		n := nextInt()
		s := nextStr()
		m := nextInt()
		special := make([]bool, 128)
		for i := 0; i < m; i++ {
			s1 := nextStr()
			special[s1[0]] = true
		}
		// fmt.Println(special)
		c := 0
		l := 0
		ans := 0
		for i := 0; i < n; i++ {
			if special[s[i]] {
				res := l
				if c > 0 {
					res++
				}
				if ans < res {
					ans = res
				}
				l = 0
				c++
			} else {
				l++
			}
		}
		return ans
	}
	for ; t > 0; t-- {
		fmt.Fprintln(out, sol())
	}
}

package main

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

func main() {
	in := bufio.NewScanner(os.Stdin)
	in.Split(bufio.ScanWords)
	in.Buffer(nil, 1e9)

	nextInt := func() int {
		in.Scan()
		vv, _ := strconv.Atoi(in.Text())
		return vv
	}

	nextStr := func() string {
		in.Scan()
		return in.Text()
	}

	t := nextInt()
	sol := func() int {
		n := nextInt()
		s := nextStr()
		m := nextInt()
		special := make([]bool, 128)
		for i := 0; i < m; i++ {
			s1 := nextStr()
			special[s1[0]] = true
		}
		// fmt.Println(special)
		c := 0
		l := 0
		ans := 0
		for i := 0; i < n; i++ {
			if special[s[i]] {
				res := l
				if c > 0 {
					res++
				}
				if ans < res {
					ans = res
				}
				l = 0
				c++
			} else {
				l++
			}
		}
		return ans
	}
	for ; t > 0; t-- {
		fmt.Println(sol())
	}
}

package main

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

type Int int64

const (
	INF = 3e5 + 10
	L0  = Int(0)
	L1  = Int(1)
)

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

	var t int
	Fscan(in, &t)

	for tt := 0; tt < t; tt++ {
		var n, k int
		var s string

		Fscan(in, &n, &s, &k)

		vis := make([]bool, 30)
		for i := 0; i < k; i++ {
			var ch string
			Fscan(in, &ch)
			vis[ch[0]-'a'] = true
		}

		cnt := L0
		ans := L0
		for _, ch := range s {
			if !vis[ch-'a'] {
				cnt++
			} else {
				ans = Max(ans, cnt)
				cnt = 1
			}
		}

		Fprintln(out, ans)

	}

}

func MakeGrid(h, w int) [][]Int {
	index := make([][]Int, h)
	data := make([]Int, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

func Abs(x Int) Int {
	if x > 0 {
		return x
	}
	return -x
}

func Min(nums ...Int) Int {
	return nums[MinIdx(nums...)]
}

func MinIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num < nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

func Max(nums ...Int) Int {
	return nums[MaxIdx(nums...)]
}

func MaxIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

var (
	mod  Int
	fmod func(Int) Int = doNothing
)

// SetMod sets the mod. It must be called first.
func SetMod(newMod Int) {
	mod = newMod
	fmod = dynamicMod
}

func dynamicMod(m Int) Int {
	m %= mod
	if m < 0 {
		return m + mod
	}
	return m
}

func doNothing(m Int) Int {
	return m
}

// Mod returns m % mod.
func (m Int) Mod() Int {
	return fmod(m)
}

// Inv returns modular multiplicative inverse
func (m Int) Inv() Int {
	return m.Pow(Int(0).Sub(2))
}

// Pow returns m^n
func (m Int) Pow(n Int) Int {
	p := Int(1)
	for n > 0 {
		if n&1 == 1 {
			p.MulAs(m)
		}
		m.MulAs(m)
		n >>= 1
	}
	return p
}

// Add returns m+x
func (m Int) Add(x Int) Int {
	return (m + x).Mod()
}

// Sub returns m-x
func (m Int) Sub(x Int) Int {
	return (m - x).Mod()
}

// Mul returns m*x
func (m Int) Mul(x Int) Int {
	return (m * x).Mod()
}

// Div returns m/x
func (m Int) Div(x Int) Int {
	return m.Mul(x.Inv())
}

// AddAs assigns *m + x to *m and returns m
func (m *Int) AddAs(x Int) *Int {
	*m = m.Add(x)
	return m
}

// SubAs assigns *m - x to *m and returns m
func (m *Int) SubAs(x Int) *Int {
	*m = m.Sub(x)
	return m
}

// MulAs assigns *m * x to *m and returns m
func (m *Int) MulAs(x Int) *Int {
	*m = m.Mul(x)
	return m
}

// DivAs assigns *m / x to *m and returns m
func (m *Int) DivAs(x Int) *Int {
	*m = m.Div(x)
	return m
}

func (m *Int) MaxAs(x ...Int) *Int {
	*m = Max(append(x, *m)...)
	return m
}

func (m *Int) MinAs(x ...Int) *Int {
	*m = Min(append(x, *m)...)
	return m
}

package main

import (
	"bufio"
	"container/list"
	. "fmt"
	"os"
)

type Int int64

const (
	INF = 3e5 + 10
	L0  = Int(0)
	L1  = Int(1)
)

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

	var t int
	Fscan(in, &t)

	for tt := 0; tt < t; tt++ {
		var n, k int
		Fscan(in, &n)
		var s string
		Fscan(in, &s)

		Fscan(in, &k)

		a := make([]bool, 30)
		for i := 0; i < k; i++ {
			var ch string
			Fscan(in, &ch)
			a[ch[0]-'a'] = true
		}

		vis := make(map[int]bool)
		queue := list.New()

		for i, ch := range s {
			if a[ch-'a'] && i > 0 {
				vis[i] = true
				queue.PushBack(i)
			}
		}

		ans := 0
		for queue.Len() > 0 {
			ans++
			nVis := make(map[int]bool)
			nQueue := list.New()
			cnt := 1
			for e := queue.Front(); e != nil; e = e.Next() {
				m := e.Value.(int) - cnt
				if m > 0 && nVis[m] == false {
					nVis[m] = true
					nQueue.PushBack(m)
				}
				cnt++
			}
			vis = nVis
			queue = nQueue
		}

		Fprintln(out, ans)
	}

}

func MakeGrid(h, w int) [][]Int {
	index := make([][]Int, h)
	data := make([]Int, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

func Abs(x Int) Int {
	if x > 0 {
		return x
	}
	return -x
}

func Min(nums ...Int) Int {
	return nums[MinIdx(nums...)]
}

func MinIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num < nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

func Max(nums ...Int) Int {
	return nums[MaxIdx(nums...)]
}

func MaxIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

var (
	mod  Int
	fmod func(Int) Int = doNothing
)

// SetMod sets the mod. It must be called first.
func SetMod(newMod Int) {
	mod = newMod
	fmod = dynamicMod
}

func dynamicMod(m Int) Int {
	m %= mod
	if m < 0 {
		return m + mod
	}
	return m
}

func doNothing(m Int) Int {
	return m
}

// Mod returns m % mod.
func (m Int) Mod() Int {
	return fmod(m)
}

// Inv returns modular multiplicative inverse
func (m Int) Inv() Int {
	return m.Pow(Int(0).Sub(2))
}

// Pow returns m^n
func (m Int) Pow(n Int) Int {
	p := Int(1)
	for n > 0 {
		if n&1 == 1 {
			p.MulAs(m)
		}
		m.MulAs(m)
		n >>= 1
	}
	return p
}

// Add returns m+x
func (m Int) Add(x Int) Int {
	return (m + x).Mod()
}

// Sub returns m-x
func (m Int) Sub(x Int) Int {
	return (m - x).Mod()
}

// Mul returns m*x
func (m Int) Mul(x Int) Int {
	return (m * x).Mod()
}

// Div returns m/x
func (m Int) Div(x Int) Int {
	return m.Mul(x.Inv())
}

// AddAs assigns *m + x to *m and returns m
func (m *Int) AddAs(x Int) *Int {
	*m = m.Add(x)
	return m
}

// SubAs assigns *m - x to *m and returns m
func (m *Int) SubAs(x Int) *Int {
	*m = m.Sub(x)
	return m
}

// MulAs assigns *m * x to *m and returns m
func (m *Int) MulAs(x Int) *Int {
	*m = m.Mul(x)
	return m
}

// DivAs assigns *m / x to *m and returns m
func (m *Int) DivAs(x Int) *Int {
	*m = m.Div(x)
	return m
}

func (m *Int) MaxAs(x ...Int) *Int {
	*m = Max(append(x, *m)...)
	return m
}

func (m *Int) MinAs(x ...Int) *Int {
	*m = Min(append(x, *m)...)
	return m
}

package main

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

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 int
		var s string
		fmt.Fscan(in, &n, &s)

		var k int
		fmt.Fscan(in, &k)
		m := make([]bool, 26)
		for i := 0; i < k; i++ {
			var v string
			fmt.Fscan(in, &v)

			m[v[0]-'a'] = true
		}

		var max int
		last := -1
		for i := 1; i < n; i++ {
			if m[s[i]-'a'] {
				if last < 0 {
					max = i
				} else {
					if i-last > max {
						max = i - last
					}
				}
				last = i
			}
		}

		fmt.Fprintln(out, max)
	}
}


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{ t, x, y int }
	abs := func(x int) int {
		if x < 0 {
			return -x
		}
		return x
	}
	dis := func(p, q pair) int {
		return abs(p.x-q.x) + abs(p.y-q.y)
	}
	max := func(a, b int) int {
		if a > b {
			return a
		}
		return b
	}

	var r, n int
	Fscan(in, &r, &n)
	dp := make([]int, n)
	for _i := range dp {
		dp[_i] = -1
	}
	ps := make([]pair, n)
	mx := -1
	for i := range ps {
		var t, x, y int
		Fscan(in, &t, &x, &y)
		ps[i] = pair{t, x, y}
		p := ps[i]
		if d := dis(p, pair{0,1,1});d <=t {
			dp[i] = 1
		}

		if i <= 1000 {
			for j, q := range ps[:i] {
				if dp[j] == -1 {
					continue
				}
				if d := dis(p, q); d <= t-q.t {
					dp[i] = max(dp[i], dp[j]+1)
				}
			}
		} else {
			for j := i - 1000; j < i; j++ {
				if dp[j] == -1 {
					continue
				}
				q := ps[j]
				if d := dis(p, q); d <= t-q.t {
					dp[i] = max(dp[i], dp[j]+1)
				}
			}
			mx = max(mx, dp[i-1001])
			dp[i] = max(dp[i], mx+1)
		}
	}

	// all dp max
	mx = 0
	for _, v := range dp {
		mx = max(mx, v)
	}
	Fprintln(out, mx)
}

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

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)
var INF = 0
func abs(n int) int {
	if n>0{
		return n
	}
	return -n
}
func getAbs(x1,y1,x2,y2 int) int {
	return abs(x1-x2)+abs(y1-y2)
}
func max(a,b int) int {
	if a>b{
		return a
	}
	return b
}
func solve(_r io.Reader,_w io.Writer){
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var r,n int
	fmt.Fscan(in,&r,&n)
	t,x,y := make([]int,n),make([]int,n),make([]int,n)
	for i:=0;i<n;i++{
		fmt.Fscan(in,&t[i],&x[i],&y[i])
	}
	ans := make([]int,n)
	pre := make([]int,n)
	if getAbs(1,1,x[0],y[0])<=t[0]{
		ans[0] = 1
	}else {
		ans[0] = INF
	}
	pre[0] = ans[0]
	for i:=1;i<n;i++{
		if getAbs(1,1,x[i],y[i]) <=t[i]{
			ans[i] = 1
			if i>1000{
				ans[i] = max(ans[i],1+pre[i-1001])
				for j:=i-1000;j<i;j++{
					if getAbs(x[i],y[i],x[j],y[j])<= t[i]-t[j]{
						ans[i] = max(ans[i],1+ans[j])
					}
				}
			}else{
				for j:=0;j<i;j++{
					if getAbs(x[i],y[i],x[j],y[j])<= t[i]-t[j]{
						ans[i] = max(ans[i],1+ans[j])
					}
				}
			}
		}else{
			ans[i] = INF
		}
		pre[i] = max(pre[i-1],ans[i])
	}
	//fmt.Println(ans)
	//fmt.Println(x)
	//fmt.Println(y)
	//fmt.Println(t)
	//fmt.Println(pre[n-1])
	fmt.Fprintln(out,pre[n-1])
	//fmt.Println(pre[n-1])
}
func main() {
	var T int
	T = 1
	//fmt.Scanf("%d",&T)
	for i:=0;i<T;i++{
		solve(os.Stdin,os.Stdout)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)
var INF = 0
func abs(n int) int {
	if n>0{
		return n
	}
	return -n
}
func getAbs(x1,y1,x2,y2 int) int {
	return abs(x1-x2)+abs(y1-y2)
}
func max(a,b int) int {
	if a>b{
		return a
	}
	return b
}
func solve(){
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	var r,n int
	fmt.Fscan(in,&r,&n)
	t,x,y := make([]int,n),make([]int,n),make([]int,n)
	for i:=0;i<n;i++{
		fmt.Fscan(in,&t[i],&x[i],&y[i])
	}
	ans := make([]int,n)
	pre := make([]int,n)
	if getAbs(1,1,x[0],y[0])<=t[0]{
		ans[0] = 1
	}else {
		ans[0] = INF
	}
	pre[0] = ans[0]
	for i:=1;i<n;i++{
		ans[i] = INF
		if getAbs(1,1,x[i],y[i]) <=t[i]{
			if i>=2*r{
				ans[i] = max(ans[i],1+pre[i-2*r])
				for j:=i-2*r+ 1;j<i;j++{
					if getAbs(x[i],y[i],x[j],y[j])<= t[i]-t[j]{
						ans[i] = max(ans[i],1+ans[j])
					}
				}
			}else{
				for j:=0;j<i;j++{
					if getAbs(x[i],y[i],x[j],y[j])<= t[i]-t[j]{
						ans[i] = max(ans[i],1+ans[j])
					}
				}
			}
		}else{
			ans[i] = INF
		}
		pre[i] = max(pre[i-1],ans[i])
	}
	//fmt.Println(ans)
	//fmt.Println(x)
	//fmt.Println(y)
	//fmt.Println(t)
	//fmt.Println(pre[n-1])
	fmt.Fprintln(out,pre[n-1])
}
func main() {
	var T int
	T = 1
	//fmt.Scanf("%d",&T)
	for i:=0;i<T;i++{
		solve()
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)
var INF = 0
func abs(n int) int {
	if n>0{
		return n
	}
	return -n
}
func getAbs(x1,y1,x2,y2 int) int {
	return abs(x1-x2)+abs(y1-y2)
}
func max(a,b int) int {
	if a>b{
		return a
	}
	return b
}
func solve(_r io.Reader,_w io.Writer){
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var r,n int
	fmt.Fscan(in,&r,&n)
	t,x,y := make([]int,n),make([]int,n),make([]int,n)
	for i:=0;i<n;i++{
		fmt.Fscan(in,&t[i],&x[i],&y[i])
	}
	ans := make([]int,n)
	pre := make([]int,n)
	if getAbs(1,1,x[0],y[0])<=t[0]{
		ans[0] = 1
	}else {
		ans[0] = INF
	}
	pre[0] = ans[0]
	for i:=1;i<n;i++{
		ans[i] = INF
		if getAbs(1,1,x[i],y[i]) <=t[i]{
			if i>=1000{
				ans[i] = max(ans[i],1+pre[i-1000])
				for j:=i-1000+ 1;j<i;j++{
					if getAbs(x[i],y[i],x[j],y[j])<= t[i]-t[j]{
						ans[i] = max(ans[i],1+ans[j])
					}
				}
			}else{
				for j:=0;j<i;j++{
					if getAbs(x[i],y[i],x[j],y[j])<= t[i]-t[j]{
						ans[i] = max(ans[i],1+ans[j])
					}
				}
			}
		}else{
			ans[i] = INF
		}
		pre[i] = max(pre[i-1],ans[i])
	}
	//fmt.Println(ans)
	//fmt.Println(x)
	//fmt.Println(y)
	//fmt.Println(t)
	//fmt.Println(pre[n-1])
	fmt.Fprintln(out,pre[n-1])
	//fmt.Println(pre[n-1])
}
func main() {
	var T int
	T = 1
	//fmt.Scanf("%d",&T)
	for i:=0;i<T;i++{
		solve(os.Stdin,os.Stdout)
	}
}

package main

import (
	"fmt"
)
var INF = 0
func abs(n int) int {
	if n>0{
		return n
	}
	return -n
}
func getAbs(x1,y1,x2,y2 int) int {
	return abs(x1-x2)+abs(y1-y2)
}
func max(a,b int) int {
	if a>b{
		return a
	}
	return b
}
func solve(){
	var r,n int
	fmt.Scanf("%d %d",&r,&n)
	t,x,y := make([]int,n),make([]int,n),make([]int,n)
	for i:=0;i<n;i++{
		fmt.Scanf("%d %d %d",&t[i],&x[i],&y[i])
	}
	ans := make([]int,n)
	pre := make([]int,n)
	if getAbs(1,1,x[0],y[0])<=t[0]{
		ans[0] = 1
	}else {
		ans[0] = INF
	}
	pre[0] = ans[0]
	for i:=1;i<n;i++{
		ans[i] = INF
		if getAbs(1,1,x[i],y[i]) <=t[i]{
			if i>=2*r{
				ans[i] = max(ans[i],1+pre[i-2*r])
				for j:=i-2*r+ 1;j<i;j++{
					if getAbs(x[i],y[i],x[j],y[j])<= t[i]-t[j]{
						ans[i] = max(ans[i],1+ans[j])
					}
				}
			}else{
				for j:=0;j<i;j++{
					if getAbs(x[i],y[i],x[j],y[j])<= t[i]-t[j]{
						ans[i] = max(ans[i],1+ans[j])
					}
				}
			}
		}else{
			ans[i] = INF
		}
		pre[i] = max(pre[i-1],ans[i])
	}
	//fmt.Println(ans)
	//fmt.Println(x)
	//fmt.Println(y)
	//fmt.Println(t)
	println(pre[n-1])
}
func main() {
	var T int
	T = 1
	//fmt.Scanf("%d",&T)
	for i:=0;i<T;i++{
		solve()
	}
}

package main

import (
	"fmt"
)
var INF = 0
func abs(n int) int {
	if n>0{
		return n
	}
	return -n
}
func getAbs(x1,y1,x2,y2 int) int {
	return abs(x1-x2)+abs(y1-y2)
}
func max(a,b int) int {
	if a>b{
		return a
	}
	return b
}
func solve(){
	var r,n int
	fmt.Scanf("%d %d",&r,&n)
	t,x,y := make([]int,n),make([]int,n),make([]int,n)
	for i:=0;i<n;i++{
		fmt.Scanf("%d %d %d",&t[i],&x[i],&y[i])
	}
	ans := make([]int,n)
	pre := make([]int,n)
	if getAbs(1,1,x[0],y[0])<=t[0]{
		ans[0] = 1
	}else {
		ans[0] = INF
	}
	pre[0] = ans[0]
	for i:=1;i<n;i++{
		ans[i] = INF
		if getAbs(1,1,x[i],y[i]) <=t[i]{
			if i>=2*r{
				ans[i] = max(ans[i],1+pre[i-2*r])
				for j:=i-2*r+ 1;j<i;j++{
					if getAbs(x[i],y[i],x[j],y[j])<= t[i]-t[j]{
						ans[i] = max(ans[i],1+ans[j])
					}
				}
			}else{
				for j:=0;j<i;j++{
					if getAbs(x[i],y[i],x[j],y[j])<= t[i]-t[j]{
						ans[i] = max(ans[i],1+ans[j])
					}
				}
			}
		}else{
			ans[i] = INF
		}
		pre[i] = max(pre[i-1],ans[i])
	}
	//fmt.Println(ans)
	//fmt.Println(x)
	//fmt.Println(y)
	//fmt.Println(t)
	fmt.Println(pre[n-1])
}
func main() {
	var T int
	T = 1
	//fmt.Scanf("%d",&T)
	for i:=0;i<T;i++{
		solve()
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)
var INF = 0
func abs(n int) int {
	if n>0{
		return n
	}
	return -n
}
func getAbs(x1,y1,x2,y2 int) int {
	return abs(x1-x2)+abs(y1-y2)
}
func max(a,b int) int {
	if a>b{
		return a
	}
	return b
}
func solve(_r io.Reader,_w io.Writer){
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var r,n int
	fmt.Fscan(in,&r,&n)
	t,x,y := make([]int,n),make([]int,n),make([]int,n)
	for i:=0;i<n;i++{
		fmt.Fscan(in,&t[i],&x[i],&y[i])
	}
	ans := make([]int,n)
	pre := make([]int,n)
	if getAbs(1,1,x[0],y[0])<=t[0]{
		ans[0] = 1
	}else {
		ans[0] = INF
	}
	pre[0] = ans[0]
	for i:=1;i<n;i++{
		ans[i] = INF
		if getAbs(1,1,x[i],y[i]) <=t[i]{
			if i>1000{
				ans[i] = max(ans[i],1+pre[i-1001])
				for j:=i-1000;j<i;j++{
					if getAbs(x[i],y[i],x[j],y[j])<= t[i]-t[j]{
						ans[i] = max(ans[i],1+ans[j])
					}
				}
			}else{
				for j:=0;j<i;j++{
					if getAbs(x[i],y[i],x[j],y[j])<= t[i]-t[j]{
						ans[i] = max(ans[i],1+ans[j])
					}
				}
			}
		}else{
			ans[i] = INF
		}
		pre[i] = max(pre[i-1],ans[i])
	}
	//fmt.Println(ans)
	//fmt.Println(x)
	//fmt.Println(y)
	//fmt.Println(t)
	//fmt.Println(pre[n-1])
	fmt.Fprintln(out,pre[n-1])
	//fmt.Println(pre[n-1])
}
func main() {
	var T int
	T = 1
	//fmt.Scanf("%d",&T)
	for i:=0;i<T;i++{
		solve(os.Stdin,os.Stdout)
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)
var INF = 0
func abs(n int) int {
	if n>0{
		return n
	}
	return -n
}
func getAbs(x1,y1,x2,y2 int) int {
	return abs(x1-x2)+abs(y1-y2)
}
func max(a,b int) int {
	if a>b{
		return a
	}
	return b
}
func solve(_r io.Reader,_w io.Writer){
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var r,n int
	fmt.Fscan(in,&r,&n)
	t,x,y := make([]int,n),make([]int,n),make([]int,n)
	for i:=0;i<n;i++{
		fmt.Fscan(in,&t[i],&x[i],&y[i])
	}
	ans := make([]int,n)
	pre := make([]int,n)
	if getAbs(1,1,x[0],y[0])<=t[0]{
		ans[0] = 1
	}else {
		ans[0] = INF
	}
	pre[0] = ans[0]
	for i:=1;i<n;i++{
		ans[i] = INF
		if getAbs(1,1,x[i],y[i]) <=t[i]{
			if i>=2*r{
				ans[i] = max(ans[i],1+pre[i-2*r])
				for j:=i-2*r+ 1;j<i;j++{
					if getAbs(x[i],y[i],x[j],y[j])<= t[i]-t[j]{
						ans[i] = max(ans[i],1+ans[j])
					}
				}
			}else{
				for j:=0;j<i;j++{
					if getAbs(x[i],y[i],x[j],y[j])<= t[i]-t[j]{
						ans[i] = max(ans[i],1+ans[j])
					}
				}
			}
		}else{
			ans[i] = INF
		}
		pre[i] = max(pre[i-1],ans[i])
	}
	//fmt.Println(ans)
	//fmt.Println(x)
	//fmt.Println(y)
	//fmt.Println(t)
	//fmt.Println(pre[n-1])
	fmt.Fprintln(out,pre[n-1])
	//fmt.Println(pre[n-1])
}
func main() {
	var T int
	T = 1
	//fmt.Scanf("%d",&T)
	for i:=0;i<T;i++{
		solve(os.Stdin,os.Stdout)
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)
var INF = 0
func abs(n int) int {
	if n>0{
		return n
	}
	return -n
}
func getAbs(x1,y1,x2,y2 int) int {
	return abs(x1-x2)+abs(y1-y2)
}
func max(a,b int) int {
	if a>b{
		return a
	}
	return b
}
func solve(_r io.Reader,_w io.Writer){
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var r,n int
	fmt.Fscan(in,&r,&n)
	t,x,y := make([]int,n),make([]int,n),make([]int,n)
	for i:=0;i<n;i++{
		fmt.Fscan(in,&t[i],&x[i],&y[i])
	}
	ans := make([]int,n)
	pre := make([]int,n)
	if getAbs(1,1,x[0],y[0])<=t[0]{
		ans[0] = 1
	}else {
		ans[0] = INF
	}
	pre[0] = ans[0]
	for i:=1;i<n;i++{
		ans[i] = INF
		if getAbs(1,1,x[i],y[i]) <=t[i]{
			if i>1000{
				ans[i] = max(ans[i],1+pre[i-2*r])
				for j:=i-1000+ 1;j<i;j++{
					if getAbs(x[i],y[i],x[j],y[j])<= t[i]-t[j]{
						ans[i] = max(ans[i],1+ans[j])
					}
				}
			}else{
				for j:=0;j<i;j++{
					if getAbs(x[i],y[i],x[j],y[j])<= t[i]-t[j]{
						ans[i] = max(ans[i],1+ans[j])
					}
				}
			}
		}else{
			ans[i] = INF
		}
		pre[i] = max(pre[i-1],ans[i])
	}
	//fmt.Println(ans)
	//fmt.Println(x)
	//fmt.Println(y)
	//fmt.Println(t)
	//fmt.Println(pre[n-1])
	fmt.Fprintln(out,pre[n-1])
	//fmt.Println(pre[n-1])
}
func main() {
	var T int
	T = 1
	//fmt.Scanf("%d",&T)
	for i:=0;i<T;i++{
		solve(os.Stdin,os.Stdout)
	}
}