Just in case somebody missed it: we have wonderful girls in Arpa’s land.Arpa has a rooted tree (connected acyclic graph) consisting of n vertices. The vertices are numbered 1 through n, the vertex 1 is the root. There is a letter written on each edge of this tree. Mehrdad is a fan of Dokhtar-kosh things. He call a string Dokhtar-kosh, if we can shuffle the characters in string such that it becomes palindrome.  He asks Arpa, for each vertex v, what is the length of the longest simple path in subtree of v that form a Dokhtar-kosh string.

The first line contains integer n (1  ≤  n  ≤  5·105) — the number of vertices in the tree. (n  -  1) lines follow, the i-th of them contain an integer pi + 1 and a letter ci + 1 (1  ≤  pi + 1  ≤  i, ci + 1 is lowercase English letter, between a and v, inclusively), that mean that there is an edge between nodes pi + 1 and i + 1 and there is a letter ci + 1 written on this edge.

Print n integers. The i-th of them should be the length of the longest simple path in subtree of the i-th vertex that form a Dokhtar-kosh string.

package main

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

type Tree []Node

type Node struct {
	Adj     []*Node
	Label   string
	Parent  *Node
	Size    int   // The size of this subtree.
	Big     *Node // The biggest child.
	Subtree []*Node
	Depth   int

	Letter byte
	Mask   int
	Ans    int
}

func (x *Node) String() string {
	return x.Label
}

func (x *Node) InitParent(p *Node) {
	x.Parent = p
	for _, y := range x.Adj {
		if y != p {
			y.InitParent(x)
		}
	}
}

// Depends on InitParent.
func (x *Node) InitSize() {
	x.Size = 1
	x.ForAll(func(y *Node) {
		y.InitSize()
		x.Size += y.Size
	})
}

// Depends on InitParent and InitSize.
func (x *Node) InitBig() {
	// Assumes x.Big == nil
	x.ForAll(func(y *Node) {
		if x.Big == nil || x.Big.Size < y.Size {
			x.Big = y
		}
		y.InitBig()
	})
}

// Depends on InitParent.
func (x *Node) InitSubtree(root *Node) {
	begin := len(root.Subtree)
	root.Subtree = append(root.Subtree, x)
	x.ForAll(func(y *Node) {
		y.InitSubtree(root)
	})
	end := len(root.Subtree)
	x.Subtree = root.Subtree[begin:end]
}

// Depends on InitParent.
func (x *Node) ForAll(handle func(*Node)) {
	for _, y := range x.Adj {
		if y != x.Parent {
			handle(y)
		}
	}
}

// Depends on InitParent and InitBig.
func (x *Node) ForSmall(handle func(*Node)) {
	for _, y := range x.Adj {
		if y != x.Parent && y != x.Big {
			handle(y)
		}
	}
}

func (x *Node) InitDepth() {
	if x.Parent == nil {
		x.Depth = 1
	} else {
		x.Depth = x.Parent.Depth + 1
	}
	x.ForAll(func(y *Node) {
		y.InitDepth()
	})
}

type helper struct {
	Longest []int
	Ans     int
}

func (h *helper) updateLongest(x *Node) {
	if h.Longest[x.Mask] < x.Depth {
		h.Longest[x.Mask] = x.Depth
	}
}

func (h *helper) updateAns(z, x *Node) {
	try := func(candidate int) {
		if candidate != -1 {
			tmp := candidate + z.Depth - 2*x.Depth
			if h.Ans < tmp {
				h.Ans = tmp
			}
		}
	}
	try(h.Longest[z.Mask])
	for b := uint(0); b < 22; b++ {
		try(h.Longest[z.Mask^(1<<b)])
	}
}

func (h *helper) dfsAns(x *Node, keep bool) {
	x.ForSmall(func(y *Node) {
		h.dfsAns(y, false)
		if x.Ans < y.Ans {
			x.Ans = y.Ans
		}
	})
	// Assumes the helper is empty at this point, handles biggest child first to
	// provide it a clean enviroment and keeps its side effect.
	if x.Big != nil {
		h.dfsAns(x.Big, true)
		if x.Ans < x.Big.Ans {
			x.Ans = x.Big.Ans
		}
	}
	// Handles small children.
	x.ForSmall(func(y *Node) {
		for _, z := range y.Subtree {
			h.updateAns(z, x)
		}
		for _, z := range y.Subtree {
			h.updateLongest(z)
		}
	})
	// Handles x itself.
	h.updateAns(x, x)
	h.updateLongest(x)
	if x.Ans < h.Ans {
		x.Ans = h.Ans
	}
	// Cleans up.
	if !keep {
		for _, z := range x.Subtree {
			h.Longest[z.Mask] = -1
		}
		h.Ans = 0
	}
}

func mask(letter byte) int {
	return 1 << (letter - 'a')
}

func (x *Node) InitMask() {
	if x.Parent == nil {
		x.Mask = 0
	} else {
		x.Mask = x.Parent.Mask ^ mask(x.Letter)
	}
	x.ForAll(func(y *Node) {
		y.InitMask()
	})
}

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

	n := 0
	fmt.Fscan(reader, &n)
	tree := Tree(make([]Node, n))
	for i := 0; i < n; i++ {
		tree[i].Label = fmt.Sprint(i + 1)
	}
	for i := 1; i < n; i++ {
		var j int
		var s string
		fmt.Fscan(reader, &j, &s)
		tree[i].Letter = s[0]
		j--
		tree[i].Adj = append(tree[i].Adj, &tree[j])
		tree[j].Adj = append(tree[j].Adj, &tree[i])
	}

	root := &tree[0]
	root.InitParent(nil)
	root.InitSize()
	root.InitBig()
	root.Subtree = make([]*Node, 0, n)
	root.InitSubtree(root)
	root.InitDepth()

	root.InitMask()
	h := helper{
		Longest: make([]int, mask('v'+1)+5),
	}
	for i, _ := range h.Longest {
		h.Longest[i] = -1
	}
	h.dfsAns(root, true)

	for _, x := range tree {
		fmt.Fprint(writer, x.Ans, " ")
	}
	fmt.Fprintln(writer)
}

package main

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

const maxn = 500009

var (
	to    []int    = make([]int, maxn)
	par   []int    = make([]int, maxn)
	sz    []int    = make([]int, maxn)
	mask  []int    = make([]int, maxn)
	ans   []int    = make([]int, maxn)
	depth []int    = make([]int, maxn)
	neigh [][]int  = make([][]int, maxn)
	V     []*[]int = make([]*[]int, maxn)
	M     []int    = make([]int, 1<<22)
)

func dfs(v int, keep bool) {
	for _, u := range neigh[v] {
		if u == to[v] {
			continue
		}
		dfs(u, false)
	}
	if to[v] != 0 {
		dfs(to[v], true)
		V[v] = V[to[v]]
	} else {
		V[v] = new([]int)
	}
	for s := 1 << 21; s >= 0; s /= 2 {
		if M[mask[v]^s] != 0 {
			tmp := M[mask[v]^s] - depth[v]
			if ans[v] < tmp {
				ans[v] = tmp
			}
		}
		if s == 0 {
			break
		}
	}
	if M[mask[v]] < depth[v] {
		M[mask[v]] = depth[v]
	}
	*V[v] = append(*V[v], v)
	for _, u := range neigh[v] {
		if u == to[v] {
			continue
		}
		for _, k := range *V[u] {
			tmp := depth[k] - 2*depth[v]
			for s := 1 << 21; s >= 0; s /= 2 {
				if M[mask[k]^s] != 0 {
					tmp2 := M[mask[k]^s] + tmp
					if ans[v] < tmp2 {
						ans[v] = tmp2
					}
				}
				if s == 0 {
					break
				}
			}
		}
		for _, k := range *V[u] {
			if M[mask[k]] < depth[k] {
				M[mask[k]] = depth[k]
			}
			*V[v] = append(*V[v], k)
		}
	}
	if !keep {
		for _, k := range *V[v] {
			M[mask[k]] = 0
		}
	}
}

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

	n := 0
	fmt.Fscan(reader, &n)
	depth[1] = 1
	for v := 2; v <= n; v++ {
		var s string
		fmt.Fscan(reader, &par[v], &s)
		neigh[par[v]] = append(neigh[par[v]], v)
		mask[v] = mask[par[v]] ^ (1 << (s[0] - 'a'))
		depth[v] = depth[par[v]] + 1
	}
	for v := n; v >= 2; v-- {
		sz[v]++
		sz[par[v]] += sz[v]
		if to[par[v]] == 0 || sz[to[par[v]]] < sz[v] {
			to[par[v]] = v
		}
	}
	sz[1]++
	dfs(1, false)
	for i := n; i >= 2; i-- {
		if ans[par[i]] < ans[i] {
			ans[par[i]] = ans[i]
		}
	}
	for i := 1; i <= n; i++ {
		fmt.Fprint(writer, ans[i], " ")
	}
	fmt.Fprintln(writer)
}

package main

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

type Tree []Node

type Node struct {
	Adj     []*Node
	Label   string
	Parent  *Node
	Size    int   // The size of this subtree.
	Big     *Node // The biggest child.
	Subtree []*Node
	Depth   int

	Letter byte
	Mask   int
	Ans    int
}

func (x *Node) String() string {
	return x.Label
}

func (x *Node) InitParent(p *Node) {
	x.Parent = p
	for _, y := range x.Adj {
		if y != p {
			y.InitParent(x)
		}
	}
}

// Depends on InitParent.
func (x *Node) InitSize() {
	x.Size = 1
	x.ForAll(func(y *Node) {
		y.InitSize()
		x.Size += y.Size
	})
}

// Depends on InitParent and InitSize.
func (x *Node) InitBig() {
	// Assumes x.Big == nil
	x.ForAll(func(y *Node) {
		if x.Big == nil || x.Big.Size < y.Size {
			x.Big = y
		}
		y.InitBig()
	})
}

// Depends on InitParent.
func (x *Node) InitSubtree(root *Node) {
	begin := len(root.Subtree)
	root.Subtree = append(root.Subtree, x)
	x.ForAll(func(y *Node) {
		y.InitSubtree(root)
	})
	end := len(root.Subtree)
	x.Subtree = root.Subtree[begin:end]
}

// Depends on InitParent.
func (x *Node) ForAll(handle func(*Node)) {
	for _, y := range x.Adj {
		if y != x.Parent {
			handle(y)
		}
	}
}

// Depends on InitParent and InitBig.
func (x *Node) ForSmall(handle func(*Node)) {
	for _, y := range x.Adj {
		if y != x.Parent && y != x.Big {
			handle(y)
		}
	}
}

func (x *Node) InitDepth() {
	if x.Parent == nil {
		x.Depth = 1
	} else {
		x.Depth = x.Parent.Depth + 1
	}
	x.ForAll(func(y *Node) {
		y.InitDepth()
	})
}

type helper struct {
	Longest []int
	Ans     int
}

func (h *helper) updateLongest(x *Node) {
	if h.Longest[x.Mask] < x.Depth {
		h.Longest[x.Mask] = x.Depth
	}
}

func (h *helper) updateAns(z, x *Node) {
	try := func(candidate int) {
		if candidate != -1 {
			tmp := candidate + z.Depth - 2*x.Depth
			if h.Ans < tmp {
				h.Ans = tmp
			}
		}
	}
	try(h.Longest[z.Mask])
	for b := uint(0); b < 22; b++ {
		try(h.Longest[z.Mask^(1<<b)])
	}
}

func (h *helper) dfsAns(x *Node, keep bool) {
	x.ForSmall(func(y *Node) {
		h.dfsAns(y, false)
		if x.Ans < y.Ans {
			x.Ans = y.Ans
		}
	})
	// Assumes the helper is empty at this point, handles biggest child first to
	// provide it a clean enviroment and keeps its side effect.
	if x.Big != nil {
		h.dfsAns(x.Big, true)
		if x.Ans < x.Big.Ans {
			x.Ans = x.Big.Ans
		}
	}
	// Handles small children.
	x.ForSmall(func(y *Node) {
		for _, z := range y.Subtree {
			h.updateAns(z, x)
		}
		for _, z := range y.Subtree {
			h.updateLongest(z)
		}
	})
	// Handles x itself.
	h.updateAns(x, x)
	h.updateLongest(x)
	if x.Ans < h.Ans {
		x.Ans = h.Ans
	}
	// Cleans up.
	if !keep {
		for _, z := range x.Subtree {
			h.Longest[z.Mask] = -1
		}
		h.Ans = 0
	}
}

func mask(letter byte) int {
	return 1 << (letter - 'a')
}

func (x *Node) InitMask() {
	if x.Parent == nil {
		x.Mask = 0
	} else {
		x.Mask = x.Parent.Mask ^ mask(x.Letter)
	}
	x.ForAll(func(y *Node) {
		y.InitMask()
	})
}

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

	n := 0
	fmt.Fscan(reader, &n)
	tree := Tree(make([]Node, n))
	for i := 0; i < n; i++ {
		tree[i].Label = fmt.Sprint(i + 1)
	}
	for i := 1; i < n; i++ {
		var j int
		var s string
		fmt.Fscan(reader, &j, &s)
		tree[i].Letter = s[0]
		j--
		tree[i].Adj = append(tree[i].Adj, &tree[j])
		tree[j].Adj = append(tree[j].Adj, &tree[i])
	}

	root := &tree[0]
	root.InitParent(nil)
	root.InitSize()
	root.InitBig()
	root.InitSubtree(root)
	root.InitDepth()

	root.InitMask()
	h := helper{
		Longest: make([]int, mask('v'+1)+5),
	}
	for i, _ := range h.Longest {
		h.Longest[i] = -1
	}
	h.dfsAns(root, true)

	for _, x := range tree {
		fmt.Fprint(writer, x.Ans, " ")
	}
	fmt.Fprintln(writer)
}

package main

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

const maxn = 500009

var (
	to    []int    = make([]int, maxn)
	par   []int    = make([]int, maxn)
	sz    []int    = make([]int, maxn)
	mask  []int    = make([]int, maxn)
	ans   []int    = make([]int, maxn)
	depth []int    = make([]int, maxn)
	neigh [][]int  = make([][]int, maxn)
	V     []*[]int = make([]*[]int, maxn)
	M     []int    = make([]int, 1<<22)
)

func dfs(v int, keep bool) {
	for _, u := range neigh[v] {
		if u == to[v] {
			continue
		}
		dfs(u, false)
	}
	if to[v] != 0 {
		dfs(to[v], true)
		V[v] = V[to[v]]
	} else {
		V[v] = new([]int)
	}
	for s := 1 << 21; s >= 0; s /= 2 {
		if M[mask[v]^s] != 0 {
			tmp := M[mask[v]^s] - depth[v]
			if ans[v] < tmp {
				ans[v] = tmp
			}
		}
		if s == 0 {
			break
		}
	}
	if M[mask[v]] < depth[v] {
		M[mask[v]] = depth[v]
	}
	*V[v] = append(*V[v], v)
	for _, u := range neigh[v] {
		if u == to[v] {
			continue
		}
		for _, k := range *V[u] {
			tmp := depth[k] - 2*depth[v]
			for s := 1 << 21; s >= 0; s /= 2 {
				if M[mask[k]^s] != 0 {
					tmp2 := M[mask[k]^s] + tmp
					if ans[v] < tmp2 {
						ans[v] = tmp2
					}
				}
				if s == 0 {
					break
				}
			}
		}
		for _, k := range *V[u] {
			if M[mask[k]] < depth[k] {
				M[mask[k]] = depth[k]
			}
			*V[v] = append(*V[v], k)
		}
	}
	if !keep {
		for _, k := range *V[v] {
			M[mask[k]] = 0
		}
	}
}

func RapidReader() io.Reader {
	b, _ := ioutil.ReadAll(os.Stdin)
	return bytes.NewReader(b)
}

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

	n := 0
	fmt.Fscan(reader, &n)
	depth[1] = 1
	for v := 2; v <= n; v++ {
		var s string
		fmt.Fscan(reader, &par[v], &s)
		neigh[par[v]] = append(neigh[par[v]], v)
		mask[v] = mask[par[v]] ^ (1 << (s[0] - 'a'))
		depth[v] = depth[par[v]] + 1
	}
	for v := n; v >= 2; v-- {
		sz[v]++
		sz[par[v]] += sz[v]
		if to[par[v]] == 0 || sz[to[par[v]]] < sz[v] {
			to[par[v]] = v
		}
	}
	sz[1]++
	dfs(1, false)
	for i := n; i >= 2; i-- {
		if ans[par[i]] < ans[i] {
			ans[par[i]] = ans[i]
		}
	}
	for i := 1; i <= n; i++ {
		fmt.Fprint(writer, ans[i], " ")
	}
	fmt.Fprintln(writer)
}

package main

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

const maxn = 500009

var (
	to    []int    = make([]int, maxn)
	par   []int    = make([]int, maxn)
	sz    []int    = make([]int, maxn)
	mask  []int    = make([]int, maxn)
	ans   []int    = make([]int, maxn)
	depth []int    = make([]int, maxn)
	neigh [][]int  = make([][]int, maxn)
	V     []*[]int = make([]*[]int, maxn)
	M     []int    = make([]int, 1<<22)
)

func dfs(v int, keep bool) {
	for _, u := range neigh[v] {
		if u == to[v] {
			continue
		}
		dfs(u, false)
	}
	if to[v] != 0 {
		dfs(to[v], true)
		V[v] = V[to[v]]
	} else {
		V[v] = new([]int)
	}
	for s := 1 << 21; s >= 0; s /= 2 {
		if M[mask[v]^s] != 0 {
			tmp := M[mask[v]^s] - depth[v]
			if ans[v] < tmp {
				ans[v] = tmp
			}
		}
		if s == 0 {
			break
		}
	}
	if M[mask[v]] < depth[v] {
		M[mask[v]] = depth[v]
	}
	*V[v] = append(*V[v], v)
	for _, u := range neigh[v] {
		if u == to[v] {
			continue
		}
		for _, k := range *V[u] {
			tmp := depth[k] - 2*depth[v]
			for s := 1 << 21; s >= 0; s /= 2 {
				if M[mask[k]^s] != 0 {
					tmp2 := M[mask[k]^s] + tmp
					if ans[v] < tmp2 {
						ans[v] = tmp2
					}
				}
				if s == 0 {
					break
				}
			}
		}
		for _, k := range *V[u] {
			if M[mask[k]] < depth[k] {
				M[mask[k]] = depth[k]
			}
			*V[v] = append(*V[v], k)
		}
	}
	if !keep {
		for _, k := range *V[v] {
			M[mask[k]] = 0
		}
	}
}

func solve() int {
	b, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewReader(b)

	n := 0
	fmt.Fscan(reader, &n)
	depth[1] = 1
	for v := 2; v <= n; v++ {
		var s string
		fmt.Fscan(reader, &par[v], &s)
		neigh[par[v]] = append(neigh[par[v]], v)
		mask[v] = mask[par[v]] ^ (1 << (s[0] - 'a'))
		depth[v] = depth[par[v]] + 1
	}
	for v := n; v >= 2; v-- {
		sz[v]++
		sz[par[v]] += sz[v]
		if to[par[v]] == 0 || sz[to[par[v]]] < sz[v] {
			to[par[v]] = v
		}
	}
	sz[1]++
	dfs(1, false)
	for i := n; i >= 2; i-- {
		if ans[par[i]] < ans[i] {
			ans[par[i]] = ans[i]
		}
	}
	return n
}

func main() {
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	n := solve()
	for i := 1; i <= n; i++ {
		fmt.Fprint(writer, ans[i], " ")
	}
	fmt.Fprintln(writer)
}

package main

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

type Tree []Node

type Node struct {
	Adj     []*Node
	Label   string
	Parent  *Node
	Size    int   // The size of this subtree.
	Big     *Node // The biggest child.
	Subtree []*Node
	Depth   int

	Letter byte
	Mask   int
	Ans    int
}

func (x *Node) String() string {
	return x.Label
}

func (x *Node) InitParent(p *Node) {
	x.Parent = p
	for _, y := range x.Adj {
		if y != p {
			y.InitParent(x)
		}
	}
}

// Depends on InitParent.
func (x *Node) InitSize() {
	x.Size = 1
	x.ForAll(func(y *Node) {
		y.InitSize()
		x.Size += y.Size
	})
}

// Depends on InitParent and InitSize.
func (x *Node) InitBig() {
	// Assumes x.Big == nil
	x.ForAll(func(y *Node) {
		if x.Big == nil || x.Big.Size < y.Size {
			x.Big = y
		}
		y.InitBig()
	})
}

// Depends on InitParent.
func (x *Node) InitSubtree(root *Node) {
	begin := len(root.Subtree)
	root.Subtree = append(root.Subtree, x)
	x.ForAll(func(y *Node) {
		y.InitSubtree(root)
	})
	end := len(root.Subtree)
	x.Subtree = root.Subtree[begin:end]
}

// Depends on InitParent.
func (x *Node) ForAll(handle func(*Node)) {
	for _, y := range x.Adj {
		if y != x.Parent {
			handle(y)
		}
	}
}

// Depends on InitParent and InitBig.
func (x *Node) ForSmall(handle func(*Node)) {
	for _, y := range x.Adj {
		if y != x.Parent && y != x.Big {
			handle(y)
		}
	}
}

func (x *Node) InitDepth() {
	if x.Parent == nil {
		x.Depth = 1
	} else {
		x.Depth = x.Parent.Depth + 1
	}
	x.ForAll(func(y *Node) {
		y.InitDepth()
	})
}

type helper struct {
	Longest []int
	Ans     int
}

func (h *helper) updateLongest(x *Node) {
	if h.Longest[x.Mask] < x.Depth {
		h.Longest[x.Mask] = x.Depth
	}
}

func (h *helper) updateAns(z, x *Node) {
	try := func(candidate int) {
		if candidate != -1 {
			tmp := candidate + z.Depth - 2*x.Depth
			if h.Ans < tmp {
				h.Ans = tmp
			}
		}
	}
	try(h.Longest[z.Mask])
	for b := uint(0); b < 22; b++ {
		try(h.Longest[z.Mask^(1<<b)])
	}
}

func (h *helper) dfsAns(x *Node, keep bool) {
	x.ForSmall(func(y *Node) {
		h.dfsAns(y, false)
		if x.Ans < y.Ans {
			x.Ans = y.Ans
		}
	})
	// Assumes the helper is empty at this point, handles biggest child first to
	// provide it a clean enviroment and keeps its side effect.
	if x.Big != nil {
		h.dfsAns(x.Big, true)
	}
	// Handles small children.
	x.ForSmall(func(y *Node) {
		for _, z := range y.Subtree {
			h.updateAns(z, x)
		}
		for _, z := range y.Subtree {
			h.updateLongest(z)
		}
	})
	// Handles x itself.
	h.updateAns(x, x)
	h.updateLongest(x)
	if x.Ans < h.Ans {
		x.Ans = h.Ans
	}
	// Cleans up.
	if !keep {
		for _, z := range x.Subtree {
			h.Longest[z.Mask] = -1
		}
		h.Ans = 0
	}
}

func mask(letter byte) int {
	return 1 << (letter - 'a')
}

func (x *Node) InitMask() {
	if x.Parent == nil {
		x.Mask = 0
	} else {
		x.Mask = x.Parent.Mask ^ mask(x.Letter)
	}
	x.ForAll(func(y *Node) {
		y.InitMask()
	})
}

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

	n := 0
	fmt.Fscan(reader, &n)
	tree := Tree(make([]Node, n))
	for i := 0; i < n; i++ {
		tree[i].Label = fmt.Sprint(i + 1)
	}
	for i := 1; i < n; i++ {
		var j int
		var s string
		fmt.Fscan(reader, &j, &s)
		tree[i].Letter = s[0]
		j--
		tree[i].Adj = append(tree[i].Adj, &tree[j])
		tree[j].Adj = append(tree[j].Adj, &tree[i])
	}

	root := &tree[0]
	root.InitParent(nil)
	root.InitSize()
	root.InitBig()
	root.InitSubtree(root)
	root.InitDepth()

	root.InitMask()
	h := helper{
		Longest: make([]int, mask('v'+1)+5),
	}
	for i, _ := range h.Longest {
		h.Longest[i] = -1
	}
	h.dfsAns(root, true)

	for _, x := range tree {
		fmt.Fprint(writer, x.Ans, " ")
	}
	fmt.Fprintln(writer)
}

package main

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

type Tree []Node

type Node struct {
	Adj     []*Node
	Label   string
	Parent  *Node
	Size    int   // The size of this subtree.
	Big     *Node // The biggest child.
	Subtree []*Node
	Depth   int

	Letter byte
	Mask   int
	Ans    int
}

func (x *Node) String() string {
	return x.Label
}

func (x *Node) InitParent(p *Node) {
	x.Parent = p
	for _, y := range x.Adj {
		if y != p {
			y.InitParent(x)
		}
	}
}

// Depends on InitParent.
func (x *Node) InitSize() {
	x.Size = 1
	x.ForAll(func(y *Node) {
		y.InitSize()
		x.Size += y.Size
	})
}

// Depends on InitParent and InitSize.
func (x *Node) InitBig() {
	// Assumes x.Big == nil
	x.ForAll(func(y *Node) {
		if x.Big == nil || x.Big.Size < y.Size {
			x.Big = y
		}
		y.InitBig()
	})
}

// Depends on InitParent.
func (x *Node) InitSubtree(root *Node) {
	begin := len(root.Subtree)
	root.Subtree = append(root.Subtree, x)
	x.ForAll(func(y *Node) {
		y.InitSubtree(root)
	})
	end := len(root.Subtree)
	x.Subtree = root.Subtree[begin:end]
}

// Depends on InitParent.
func (x *Node) ForAll(handle func(*Node)) {
	for _, y := range x.Adj {
		if y != x.Parent {
			handle(y)
		}
	}
}

// Depends on InitParent and InitBig.
func (x *Node) ForSmall(handle func(*Node)) {
	for _, y := range x.Adj {
		if y != x.Parent && y != x.Big {
			handle(y)
		}
	}
}

func (x *Node) InitDepth() {
	if x.Parent == nil {
		x.Depth = 1
	} else {
		x.Depth = x.Parent.Depth + 1
	}
	x.ForAll(func(y *Node) {
		y.InitDepth()
	})
}

type helper struct {
	Longest []int
	Ans     int
}

func (h *helper) updateLongest(x *Node) {
	if h.Longest[x.Mask] < x.Depth {
		h.Longest[x.Mask] = x.Depth
	}
}

func (h *helper) updateAns(z, x *Node) {
	try := func(candidate int) {
		if candidate != -1 {
			tmp := candidate + z.Depth - 2*x.Depth
			if h.Ans < tmp {
				h.Ans = tmp
			}
		}
	}
	try(h.Longest[z.Mask])
	for b := uint(0); b < 22; b++ {
		try(h.Longest[z.Mask^(1<<b)])
	}
}

func (h *helper) dfsAns(x *Node, keep bool) {
	x.ForSmall(func(y *Node) {
		h.dfsAns(y, false)
	})
	// Assumes the helper is empty at this point, handles biggest child first to
	// provide it a clean enviroment and keeps its side effect.
	if x.Big != nil {
		h.dfsAns(x.Big, true)
	}
	// Handles small children.
	x.ForSmall(func(y *Node) {
		for _, z := range y.Subtree {
			h.updateAns(z, x)
		}
		for _, z := range y.Subtree {
			h.updateLongest(z)
		}
	})
	// Handles x itself.
	h.updateAns(x, x)
	h.updateLongest(x)
	x.Ans = h.Ans
	// Cleans up.
	if !keep {
		for _, z := range x.Subtree {
			h.Longest[z.Mask] = -1
		}
		h.Ans = 0
	}
}

func mask(letter byte) int {
	return 1 << (letter - 'a')
}

func (x *Node) InitMask() {
	if x.Parent == nil {
		x.Mask = 0
	} else {
		x.Mask = x.Parent.Mask ^ mask(x.Letter)
	}
	x.ForAll(func(y *Node) {
		y.InitMask()
	})
}

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

	n := 0
	fmt.Fscan(reader, &n)
	tree := Tree(make([]Node, n))
	for i := 0; i < n; i++ {
		tree[i].Label = fmt.Sprint(i + 1)
	}
	for i := 1; i < n; i++ {
		var j int
		var s string
		fmt.Fscan(reader, &j, &s)
		tree[i].Letter = s[0]
		j--
		tree[i].Adj = append(tree[i].Adj, &tree[j])
		tree[j].Adj = append(tree[j].Adj, &tree[i])
	}

	root := &tree[0]
	root.InitParent(nil)
	root.InitSize()
	root.InitBig()
	root.InitSubtree(root)
	root.InitDepth()

	root.InitMask()
	h := helper{
		Longest: make([]int, mask('v'+1)+5),
	}
	for i, _ := range h.Longest {
		h.Longest[i] = -1
	}
	h.dfsAns(root, true)

	for _, x := range tree {
		fmt.Fprint(writer, x.Ans, " ")
	}
	fmt.Fprintln(writer)
}

package main

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

type Tree []Node

type Node struct {
	Adj     []*Node
	Label   string
	Parent  *Node
	Size    int   // The size of this subtree.
	Big     *Node // The biggest child.
	Subtree []*Node
	Depth   int

	Letter byte
	Mask   int
	Ans    int
}

func (x *Node) String() string {
	return x.Label
}

func (x *Node) InitParent(p *Node) {
	x.Parent = p
	for _, y := range x.Adj {
		if y != p {
			y.InitParent(x)
		}
	}
}

// Depends on InitParent.
func (x *Node) InitSize() {
	x.Size = 1
	x.ForAll(func(y *Node) {
		y.InitSize()
		x.Size += y.Size
	})
}

// Depends on InitParent and InitSize.
func (x *Node) InitBig() {
	// Assumes x.Big == nil
	x.ForAll(func(y *Node) {
		if x.Big == nil || x.Big.Size < y.Size {
			x.Big = y
		}
		y.InitBig()
	})
}

// Depends on InitParent.
func (x *Node) InitSubtree(root *Node) {
	begin := len(root.Subtree)
	root.Subtree = append(root.Subtree, x)
	x.ForAll(func(y *Node) {
		y.InitSubtree(root)
	})
	end := len(root.Subtree)
	x.Subtree = root.Subtree[begin:end]
}

// Depends on InitParent.
func (x *Node) ForAll(handle func(*Node)) {
	for _, y := range x.Adj {
		if y != x.Parent {
			handle(y)
		}
	}
}

// Depends on InitParent and InitBig.
func (x *Node) ForSmall(handle func(*Node)) {
	for _, y := range x.Adj {
		if y != x.Parent && y != x.Big {
			handle(y)
		}
	}
}

func (x *Node) InitDepth() {
	if x.Parent == nil {
		x.Depth = 1
	} else {
		x.Depth = x.Parent.Depth + 1
	}
	x.ForAll(func(y *Node) {
		y.InitDepth()
	})
}

type helper struct {
	Longest []int
	Ans     int
}

func (h *helper) updateLongest(x *Node) {
	if h.Longest[x.Mask] < x.Depth {
		h.Longest[x.Mask] = x.Depth
	}
}

func (h *helper) updateAns(z, x *Node) {
	try := func(candidate int) {
		if candidate != -1 {
			tmp := candidate + z.Depth - 2*x.Depth
			if h.Ans < tmp {
				h.Ans = tmp
			}
		}
	}
	try(h.Longest[z.Mask])
	for b := uint(0); b < 22; b++ {
		try(h.Longest[z.Mask^(1<<b)])
	}
}

func (h *helper) dfsAns(x *Node, keep bool) {
	x.ForSmall(func(y *Node) {
		h.dfsAns(y, false)
		if x.Ans < y.Ans {
			x.Ans = y.Ans
		}
	})
	// Assumes the helper is empty at this point, handles biggest child first to
	// provide it a clean enviroment and keeps its side effect.
	if x.Big != nil {
		h.dfsAns(x.Big, true)
	}
	// Handles small children.
	x.ForSmall(func(y *Node) {
		for _, z := range y.Subtree {
			h.updateAns(z, x)
		}
		for _, z := range y.Subtree {
			h.updateLongest(z)
		}
	})
	// Handles x itself.
	h.updateAns(x, x)
	h.updateLongest(x)
	x.Ans = h.Ans
	// Cleans up.
	if !keep {
		for _, z := range x.Subtree {
			h.Longest[z.Mask] = -1
		}
		h.Ans = 0
	}
}

func mask(letter byte) int {
	return 1 << (letter - 'a')
}

func (x *Node) InitMask() {
	if x.Parent == nil {
		x.Mask = 0
	} else {
		x.Mask = x.Parent.Mask ^ mask(x.Letter)
	}
	x.ForAll(func(y *Node) {
		y.InitMask()
	})
}

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

	n := 0
	fmt.Fscan(reader, &n)
	tree := Tree(make([]Node, n))
	for i := 0; i < n; i++ {
		tree[i].Label = fmt.Sprint(i + 1)
	}
	for i := 1; i < n; i++ {
		var j int
		var s string
		fmt.Fscan(reader, &j, &s)
		tree[i].Letter = s[0]
		j--
		tree[i].Adj = append(tree[i].Adj, &tree[j])
		tree[j].Adj = append(tree[j].Adj, &tree[i])
	}

	root := &tree[0]
	root.InitParent(nil)
	root.InitSize()
	root.InitBig()
	root.InitSubtree(root)
	root.InitDepth()

	root.InitMask()
	h := helper{
		Longest: make([]int, mask('v'+1)+5),
	}
	for i, _ := range h.Longest {
		h.Longest[i] = -1
	}
	h.dfsAns(root, true)

	for _, x := range tree {
		fmt.Fprint(writer, x.Ans, " ")
	}
	fmt.Fprintln(writer)
}


You have two positive integers $$$a$$$ and $$$b$$$.You can perform two kinds of operations:  $$$a = \lfloor \frac{a}{b} \rfloor$$$ (replace $$$a$$$ with the integer part of the division between $$$a$$$ and $$$b$$$)  $$$b=b+1$$$ (increase $$$b$$$ by $$$1$$$) Find the minimum number of operations required to make $$$a=0$$$.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The only line of the description of each test case contains two integers $$$a$$$, $$$b$$$ ($$$1 \le a,b \le 10^9$$$).

For each test case, print a single integer: the minimum number of operations required to make $$$a=0$$$.

NoteIn the first test case, one of the optimal solutions is:  Divide $$$a$$$ by $$$b$$$. After this operation $$$a = 4$$$ and $$$b = 2$$$.  Divide $$$a$$$ by $$$b$$$. After this operation $$$a = 2$$$ and $$$b = 2$$$.  Increase $$$b$$$. After this operation $$$a = 2$$$ and $$$b = 3$$$.  Divide $$$a$$$ by $$$b$$$. After this operation $$$a = 0$$$ and $$$b = 3$$$.

package main

import "fmt"

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

func ti(x bool) int {
	if x {
		return 1
	}
	return 0
}

const INF = 1 << 31 - 1

func main() {
	var t int
	fmt.Scan(&t)
	for ;t > 0; t-- {
		var a, b int
		fmt.Scan(&a, &b)
		pls := ti(b == 1)
		if b == 1 { b++ }
		ans := INF
		for i := b; i < b + 17; i++ {
			nw := i - b
			aa := a
			for aa > 0 {
				aa /= i
				nw++
			}
			ans = min(ans, nw)
		}
		fmt.Println(ans + pls)
	}
	return
}

package main

import "fmt"

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

func op1(a1, b1 int) int {
	res := 0
	for b1 < 2 {
		b1++
		res++
	}
	for a1 > 0 {
		a1 = a1 / b1
		res++
	}
	return res
}

func main() {
	t := 0
	fmt.Scan(&t)
	for jjj := 0; jjj < t; jjj++ {
		a1, b1 := 0, 0
		fmt.Scan(&a1, &b1)
		ans := 0x3f3f3f3f
		for i := 0; i < 100; i++ {
			ans = min(ans, i+op1(a1, b1+i))
		}
		fmt.Println(ans)
	}
}

package main

import "fmt"

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

func op1(a1, b1 int) int {
	var res = 0
	for b1 < 2 {
		b1++
		res++
	}
	for a1 > 0 {
		a1 = a1 / b1
		res++
	}
	return res
}

func main() {
	var t = 0
	fmt.Scan(&t)
	for jjj := 0; jjj < t; jjj++ {
		var a1, b1 = 0, 0
		fmt.Scan(&a1, &b1)
		var ans = 0x3f3f3f3f
		for i := 0; i < 100; i++ {
			ans = min(ans, i+op1(a1, b1+i))
		}
		fmt.Println(ans)
	}
}

package main

import (
	"fmt"
)

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

func op1(a1, b1 int) int {
	var res = 0
	for b1 < 2 {
		b1++
		res++
	}
	for a1 > 0 {
		a1 = a1 / b1
		res++
	}
	return res
}

func main() {
	var t, jjj = 0, 0
	fmt.Scan(&t)
	for jjj < t {
		var a1, b1 = 0, 0
		fmt.Scan(&a1, &b1)
		var i, ans = 0, 0
		ans = 0x3f3f3f3f
		for i < 100 {
			ans = min(ans, i+op1(a1, b1+i))
			i++
		}
		fmt.Println(ans)
		jjj++
	}
}

package main

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"math"
	"os"
	"strconv"
	"strings"
)

var fields []string

func init() {
	f, err := os.Open("in.txt")

	if err == nil {
		defer f.Close()
	} else {
		f = os.Stdin
	}

	bytes, err := ioutil.ReadAll(bufio.NewReader(f))

	if err != nil {
		panic(err)
	}

	fields = strings.Fields(string(bytes))
}

func ScanString() string {
	for {
		field := fields[0]
		fields = fields[1:]

		if field != "" {
			return field
		}
	}
}

func ScanInt() int {
	n, err := strconv.Atoi(ScanString())

	if err != nil {
		panic(err)
	}

	return n
}

func ScanInt64() int64 {
	n, err := strconv.ParseInt(ScanString(), 10, 64)

	if err != nil {
		panic(err)
	}

	return n
}

func main() {
	for t := ScanInt(); t > 0; t-- {
		a, b := ScanInt(), ScanInt()
		res := math.MaxInt32

		if b > 1 {
			res = solve(a, b)
		}

		for c := 1; c <= 30; c++ {
			tmp := c + solve(a, b+c)

			if tmp < res {
				res = tmp
			}
		}

		fmt.Println(res)
	}
}

func solve(a, b int) int {
	res := 0

	for a > 0 {
		a /= b
		res++
	}

	return res
}

package main

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"math"
	"os"
	"strconv"
	"strings"
)

var fields []string

func init() {
	f, err := os.Open("in.txt")

	if err == nil {
		defer f.Close()
	} else {
		f = os.Stdin
	}

	bytes, err := ioutil.ReadAll(bufio.NewReader(f))

	if err != nil {
		panic(err)
	}

	fields = strings.Fields(string(bytes))
}

func ScanString() string {
	for {
		field := fields[0]
		fields = fields[1:]

		if field != "" {
			return field
		}
	}
}

func ScanInt() int {
	n, err := strconv.Atoi(ScanString())

	if err != nil {
		panic(err)
	}

	return n
}

func ScanInt64() int64 {
	n, err := strconv.ParseInt(ScanString(), 10, 64)

	if err != nil {
		panic(err)
	}

	return n
}

func main() {
	for t := ScanInt(); t > 0; t-- {
		a, b := ScanInt(), ScanInt()
		res := math.MaxInt32

		if b > 1 {
			res = solve(a, b)
		}

		for c := b + 1; c < b+1000; c++ {
			tmp := c - b + solve(a, c)

			if tmp < res {
				res = tmp
			}
		}

		fmt.Println(res)
	}
}

func solve(a, b int) int {
	res := 0

	for a > 0 {
		a /= b
		res++
	}

	return res
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var T, n1, n2 int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n1)
		Fscan(in, &n2)
		finalAns=10000000
		solve(n1,n2,1)
		Fprintln(out, finalAns)

	}
}
func log(a int, b int) int {
	var i int
	for i = 0; b <= a; i++ {
		a /= b
	}
	return i
}

//var m = make(map[string]bool)
var finalAns = 10000000

func solve(a int, b int, ans int) {
	if a == 0 || b > a || ans > finalAns {
		finalAns = min(finalAns, ans)
		return
	}
	if b != 1{
		solve(a/b, b, ans+1)
	}
	solve(a, b+1, ans+1)
	return
}

func min(x int, y int) int {
	if x < y {
		return x
	}
	return y
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)

	tc := readNum(reader)

	var buf bytes.Buffer

	for tc > 0 {
		tc--
		a, b := readTwoNums(reader)
		buf.WriteString(fmt.Sprintf("%d\n", solve(a, b)))
	}

	fmt.Print(buf.String())
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

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

	if a == b {
		return 2
	}

	var res = 1 << 30
	var i int
	if b == 1 {
		i++
	}
	for ; i <= 6; i++ {
		tmp := i + count(a, b+i)
		if tmp < res {
			res = tmp
		}
	}

	return res
}

func count(a, b int) int {
	var cnt int
	for a > 0 {
		cnt++
		a /= b
	}
	return cnt
}

package main

import "bufio"
import "os"
import . "fmt"

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var T, n1, n2, times int
	var isone bool = false
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n1)
		Fscan(in, &n2)
		if n2 == 1 {
			n2++
			isone = true
			times++
		}
		times += log(n1, n2)
		for i := 1; ; i++ {
			t := i
			if isone {
				t += 1
			}
			t += log(n1, n2+i)
			if t <= times {
				times = t
			} else {
				break
			}

		}
		times++
		Fprintln(out, times)
		times = 0
		isone = false
	}
}
func log(a int, b int) int {
	var i int
	for i = 0; b <= a; i++ {
		a /= b
	}
	return i
}

// https://codeforces.com/problemset/problem/1485/A

// 1485A - Add and Divide

// You have two positive integers a and b.
// You can perform two kinds of operations:
//  • a=⌊a/b⌋ (replace a with the integer part of the division between a and b)
//  • b=b+1  (increase b by 1)
// Find the minimum number of operations required to make a=0.

// Input
// The first line contains a single integer t (1≤t≤100) — the number of test cases.
// The only line of the description of each test case contains two integers a, b (1≤a,b≤109).

// Output
// For each test case, print a single integer: the minimum number of operations required to make a=0.

// Example
// input
// 6
// 9 2
// 1337 1
// 1 1
// 50000000 4
// 991026972 997
// 1234 5678
// output
// 4
// 9
// 2
// 12
// 3
// 1

// Note

// In the first test case, one of the optimal solutions is:
//  1. Divide a by b. After this operation a=4 and b=2.
//  2. Divide a by b. After this operation a=2 and b=2.
//  3. Increase b. After this operation a=2 and b=3.
//  4. Divide a by b. After this operation a=0 and b=3.

// Tutorial

// Hint 1
// Suppose that you can use x operations of type 1 and y operations of type 2. Try to reorder the operations in such a way that a becomes the minimum possible.

// Hint 2
// You should use operations of type 2 first, then moves of type 1. How many operations do you need in the worst case? (a=109, b=1)

// Hint 3
// You need at most 30 operations. Iterate over the number of operations of type 2.

// Solution
// Notice how it is never better to increase b after dividing (⌊a/(b+1)⌋≤⌊a/b⌋).
// So we can try to increase b to a certain value and then divide a by b until it is 0. Being careful as not to do this with b<2, the number of times we divide is going to be O(loga). In particular, if you reach b≥2 (this requires at most 1 move), you need at most ⌊log2(10^9)⌋=29 moves to finish.
// Let y be the number of moves of type 2; we can try all values of y (0≤y≤30) and, for each y, check how many moves of type 1 are necessary.
// Complexity: O(log^2(a)).
// If we notice that it is never convenient to increase b over 6, we can also achieve a solution with better complexity.

package main

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

func num_second_operation(a, b uint) uint {
	var result uint

	for result = 0; a > 0; result++ {
		a /= b
	}

	return result
}

func main() {
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	var c int
	var t, a, b, ans, i, new_ans uint

	for c, _ = fmt.Fscan(reader, &t); c == 1; c, _ = fmt.Fscan(reader, &t) {
		for ; t > 0; t-- {
			fmt.Fscan(reader, &a, &b)
			ans = 1e9

			i = b
			if i < 2 {
				i = 2
			}

			for ; i <= b+ans; i++ {
				new_ans = i - b + num_second_operation(a, i)

				if ans > new_ans {
					ans = new_ans
				}
			}

			fmt.Fprintln(writer, ans)
		}
	}

	writer.Flush()
}

// 00
package main

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

func write_int(f *bufio.Writer, a int) {
	f.Write([]byte(strconv.Itoa(a)))
}
func write_string(f *bufio.Writer, a string) {
	f.Write([]byte(a))
}

func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var a, b int
		fmt.Fscan(reader, &a, &b)
		ans := 100
		prob := 0
		if b == 1 {
			b++
			prob++
		}
		for i := 0; i < 32; i++ {
			tmp := prob + i
			tb := b + i
			ta := a
			for ta > 0 {
				ta = ta / tb
				tmp++
			}
			if tmp < ans {
				ans = tmp
			}
		}

		fmt.Println(ans)

	}
}

package main

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

var sc = bufio.NewScanner(os.Stdin)
var wr = bufio.NewWriter(os.Stdout)

func solve() {
	a, b := getI(), getI()

	ops := int64(math.MaxInt64)
	for i := int64(0); i <= 30; i++ {
		curB := b + i

		if curB == 1 {
			continue
		}

		curA := a
		count := i
		for curA != 0 {
			count++
			curA = int64(math.Floor(float64(curA) / float64(curB)))
		}

		ops = min(count, ops)
	}

	out(ops)
}

func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 4096), 1e9)
	// this template is new version.
	// use getI(), getS(), getInts(), getF()
	for i := getI(); i > 0; i-- {
		solve()
	}
	out()
}

// !------- HELPERS -------!

func out(x ...interface{}) {
	fmt.Fprintln(wr, x...)
}

func printf(f string, a ...interface{}) {
	fmt.Fprintf(wr, f, a...)
}

func getI() int64 {
	s := getS()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func getF() float64 {
	sc.Scan()
	i, e := strconv.ParseFloat(sc.Text(), 64)
	if e != nil {
		panic(e)
	}
	return i
}

func getInts(N int64) []int64 {
	ret := make([]int64, N)
	for i := range ret {
		ret[i] = getI()
	}
	return ret
}

func getS() string {
	if !sc.Scan() {
		panic(sc.Err())
	}
	return sc.Text()
}

func nsort(data ...int64) {
	sort.Slice(data, func(i, j int) bool { return data[i] < data[j] })
}

func nsum(data ...int64) int64 {
	var res int64
	for _, n := range data {
		res += n
	}
	return res
}

func njoin(s ...int64) string {
	return strings.Trim(fmt.Sprintf("%v", s), "[]")
}

func forEach(n int64, fn func(int64)) {
	for i := int64(0); i < n; i++ {
		fn(i)
	}
}

// min, max, asub, abs
func max(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}

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

// min for n entry
func nmin(a ...int64) int64 {
	ret := a[0]
	for _, e := range a {
		ret = min(ret, e)
	}
	return ret
}

// max for n entry
func nmax(a ...int64) int64 {
	ret := a[0]
	for _, e := range a {
		ret = max(ret, e)
	}
	return ret
}

func asub(a, b int64) int64 {
	if a > b {
		return a - b
	}
	return b - a
}

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

func lowerBound(a []int64, x int64) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int64, x int64) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}

package main

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

func tryLog(a, b int64) int64 {
	if b <= 1 {
		panic("unreachable")
	}
	var count int64
	for a > 0 {
		a /= b
		count++
	}
	return count
}

func main() {
	input := bufio.NewReader(os.Stdin)

	var t int
	fmt.Fscanln(input, &t)
	for ; t > 0; t-- {
		var a, b int64
		fmt.Fscanln(input, &a, &b)
		min := a + 1
		for x := int64(0); x < min; x++ {
			if b+x == 1 {
				continue
			}
			v := tryLog(a, b+x) + x
			if v < min {
				min = v
			}
		}
		fmt.Println(min)
	}
}

package main

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

func main() {
	a := bufio.NewReader(os.Stdin)
	l, _ := a.ReadString('\n')
	l = strings.Trim(strings.Trim(l, "\n"), "\r")
	ln, _ := strconv.Atoi(l)
	for i := 0; i < ln; i++ {
		s, _ := a.ReadString('\n')
		las := strings.Split(strings.Trim(strings.Trim(s, "\n"), "\r"), " ")
		n, _ := strconv.ParseInt(las[0], 10, 64)
		k, _ := strconv.ParseInt(las[1], 10, 64)

		fmt.Printf("%d\n", fn(int64(n), int64(k)))
	}
}

func fn(a int64, b int64) (C int64) {
	if b == 1 {
		C++
		b++
 	}
 	if a < b{
 		return 1+C
	}
	bcount:=int64(1)
	c:=howlong(a, b)
 	for bcount < 10{
		c=min(howlong(a, b+bcount) + bcount, c)
		bcount ++
	}
	return C+c
}

func howlong(a int64, b int64) (r int64){
	for a > 0{
		a/=b
		r++
	}
	return r
}

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

package main

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

func main() {
	var tcs int
	in := bufio.NewReader(os.Stdin)
	_, _ = fmt.Fscan(in, &tcs)

	for tc := 0; tc < tcs; tc++ {
		var a, b int64
		_, _ = fmt.Fscan(in, &a, &b)

		var ans int64 = math.MaxInt64
		var start int
		if b == 1 {
			start = 1
		}
		for i := int64(start); i < 100000; i++ {
			temp := reduce(a, b+i) + i
			if temp < ans {
				ans = temp
			}
		}
		fmt.Println(ans)
	}
}

func reduce(a, b int64) int64 {
	var i int64 = 0
	for a > 0 {
		a /= b
		i++
	}
	return i
}

package main

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

var (
	writer  = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
)

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 4096), 1e9)
}

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

func sint64() int64 {
	s := sstring()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func sint64s(n int64) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = sint64()
	}
	return a
}

func sstring() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		a := sint64()
		b := sint64()
		printf("%d\n", sol(a, b))
	}
}

func sol(a, b int64) int64 {
	ret := int64(0)
	if b == 1 {
		b++
		ret++
	}
	minSoFar := int64(math.MaxInt64)
	for i := int64(0); i < a; i++ {
		res := trySolve(a, b+i)
		if res+i > minSoFar {
			break
		}
		minSoFar = res+i
	}
	return ret + minSoFar
}

func trySolve(a, b int64) int64 {
	ret := int64(0)
	for a > 0 {
		a /= b
		ret++
	}
	return ret
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"os"
	"strings"
)

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}
	solve := func(Case int) {
		var a,b int
		Fscan(in, &a, &b)
		ans := int(1e9)
		for i := 0; i < 40; i++ {
			if b == 1 && i == 0 {
				continue
			}
			w := b+i
			c := i
			for v:=a;v>0;v/=w {
				c++
			}
			ans = min(ans, c)
		}
		Fprintln(out, ans)
	}

	var t int
	Fscan(in, &t)
	for Case := 1; Case <= t; Case++ {
		solve(Case)
	}

	_leftData, _ := ioutil.ReadAll(in)
	if _s := strings.TrimSpace(string(_leftData)); _s != "" {
		panic("有未读入的数据：\n" + _s)
	}
}

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

package main

import (
	"fmt"
	"math"
)

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

func ti(x bool) int {
	if x {
		return 1
	}
	return 0
}

func main() {
	var t int
	fmt.Scan(&t)
	for ;t > 0; t-- {
		var a, b int
		fmt.Scan(&a, &b)
		pls := ti(b == 1)
		if b == 1 { b++ }
		ans := int(math.Log(float64(a)) / math.Log(float64(b))) + 1
		for i := b + 1; i < b + 100000; i++ {
			ans = min(ans, int(math.Log(float64(a)) / math.Log(float64(i))) + i - b + 1)
		}
		fmt.Println(ans + pls)
	}
	return
}

package main

import (
	"fmt"
	"math"
)

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

func ti(x bool) int {
	if x {
		return 1
	}
	return 0
}

func main() {
	var t int
	fmt.Scan(&t)
	for ;t > 0; t-- {
		var a, b int
		fmt.Scan(&a, &b)
		pls := ti(b == 1)
		if b == 1 { b++ }
		ans := int(math.Log(float64(a)) / math.Log(float64(b))) + 1
		for i := b + 1; i < b + 20; i++ {
			ans = min(ans, int(math.Log(float64(a)) / math.Log(float64(i))) + i - b + 1)
		}
		fmt.Println(ans + pls)
	}
	return
}

package main

import (
	"fmt"
	"math"
)

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

func ti(x bool) int {
	if x {
		return 1
	}
	return 0
}

func main() {
	var t int
	fmt.Scan(&t)
	for ;t > 0; t-- {
		var a, b int
		fmt.Scan(&a, &b)
		pls := ti(b == 1)
		if b == 1 { b++ }
		ans := int(math.Log(float64(a)) / math.Log(float64(b))) + 1
		for i := b + 1; i < 20; i++ {
			ans = min(ans, int(math.Log(float64(a)) / math.Log(float64(i))) + i - b + 1)
		}
		fmt.Println(ans + pls)
	}
	return
}

package main

import (
	"fmt"
	"math"
)

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

func op1(a1, b1 int) int {
	var res = 0
	for b1 < 2 {
		b1++
		res++
	}
	for a1 > 0 {
		a1 = int(math.Ceil(float64(a1 * 1.0 / b1)))
		res++
	}
	return res
}

func main() {
	var t, jjj = 0, 0
	fmt.Scanf("%d", &t)
	for jjj < t {
		var a1, b1 = 0, 0
		fmt.Scanf("%d%d", &a1, &b1)
		var i, ans = 0, 0
		ans = 0x3f3f3f3f
		for i < 100 {
			ans = min(ans, i+op1(a1, b1+i))
			i++
		}
		fmt.Printf("%d\n", ans)
		jjj++
	}
}

package main

import (
	"fmt"
	"math"
)

func op1(a1, b1 int64) int64 {
	var res int64 = 0
	for b1 < 2 {
		b1++
		res++
	}
	for a1 > 0 {
		a1 = int64(math.Ceil(float64(a1 * 1.0 / b1)))
		res++
	}
	return res
}

func main() {
	var t, jjj int64 = 0, 0
	fmt.Scanf("%d", &t)
	for jjj < t {
		var a1, b1 int64 = 0, 0
		fmt.Scanf("%d%d", &a1, &b1)
		var i, ans int64 = 0, 0
		ans = 0x3f3f3f3f
		for i < 100 {
			ans = int64(int(math.Min(float64(ans), float64(i+op1(a1, b1+i)))))
			i++
		}
		fmt.Printf("%d\n", ans)
		jjj++
	}
}

package main

import (
	"fmt"
	"math"
)

func op1(a1, b1 int64) int64 {
	var res int64 = 0
	for b1 < 2 {
		b1++
		res++
	}
	for a1 > 0 {
		a1 = int64(math.Ceil(float64(a1 * 1.0 / b1)))
		res++
	}
	return res
}

func main() {
	var t, jjj int64
	fmt.Scanf("%d", &t)
	jjj = 0
	for jjj < t {
		var a1, b1 int64
		fmt.Scanf("%d%d", &a1, &b1)
		var i, ans int64
		ans = 0x3f3f3f3f
		for i < 100 {
			ans = int64(int(math.Min(float64(ans), float64(i+op1(a1, b1+i)))))
			i++
		}
		fmt.Printf("%d\n", ans)
		jjj++
	}
}

package main

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"math"
	"os"
	"strconv"
	"strings"
)

var fields []string

func init() {
	f, err := os.Open("in.txt")

	if err == nil {
		defer f.Close()
	} else {
		f = os.Stdin
	}

	bytes, err := ioutil.ReadAll(bufio.NewReader(f))

	if err != nil {
		panic(err)
	}

	fields = strings.Fields(string(bytes))
}

func ScanString() string {
	for {
		field := fields[0]
		fields = fields[1:]

		if field != "" {
			return field
		}
	}
}

func ScanInt() int {
	n, err := strconv.Atoi(ScanString())

	if err != nil {
		panic(err)
	}

	return n
}

func ScanInt64() int64 {
	n, err := strconv.ParseInt(ScanString(), 10, 64)

	if err != nil {
		panic(err)
	}

	return n
}

func main() {
	for t := ScanInt(); t > 0; t-- {
		a, b := ScanInt(), ScanInt()
		res := math.MaxInt32

		if b > 1 {
			res = solve(a, b)
		}

		for c := b + 1; ; c++ {
			tmp := c - b + solve(a, c)

			if tmp < res {
				res = tmp
			} else {
				break
			}
		}

		fmt.Println(res)
	}
}

func solve(a, b int) int {
	res := 0

	for a > 0 {
		a /= b
		res++
	}

	return res
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)

	tc := readNum(reader)

	var buf bytes.Buffer

	for tc > 0 {
		tc--
		a, b := readTwoNums(reader)
		buf.WriteString(fmt.Sprintf("%d\n", solve(a, b)))
	}

	fmt.Print(buf.String())
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func solve(a, b int) int {
	if a <= b {
		return 1
	}

	var res int = 1 << 30
	var i int
	if b == 1 {
		i++
	}
	for ; i <= 6; i++ {
		tmp := i + count(a, b+i)
		if tmp < res {
			res = tmp
		}
	}

	return res
}

func count(a, b int) int {
	var cnt int
	for a > 0 {
		cnt++
		a /= b
	}
	return cnt
}

package main

import "bufio"
import "os"
import . "fmt"

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var T, n1, n2, times int
	var isone bool = false
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n1)
		Fscan(in, &n2)
		if n2 == 1 {
			n2++
			isone = true
		}
		times += log(n1, n2)
		for i := 1; ; i++ {
			t := i
			if isone {
				t += 1
			}
			t += log(n1, n2+i)
			if t <= times {
				times = t
			} else {
				break
			}

		}
		times++
		Fprintln(out, times)
		times = 0
		isone = false
	}
}
func log(a int, b int) int {
	var i int
	for i = 0; b <= a; i++ {
		a /= b
	}
	return i
}


You are a coach at your local university. There are $$$n$$$ students under your supervision, the programming skill of the $$$i$$$-th student is $$$a_i$$$.You have to create a team for a new programming competition. As you know, the more students some team has the more probable its victory is! So you have to create a team with the maximum number of students. But you also know that a team should be balanced. It means that the programming skill of each pair of students in a created team should differ by no more than $$$5$$$.Your task is to report the maximum possible number of students in a balanced team.

The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of students. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is a programming skill of the $$$i$$$-th student.

Print one integer — the maximum possible number of students in a balanced team.

NoteIn the first example you can create a team with skills $$$[12, 17, 15]$$$.In the second example you can take all students in a team because their programming skills are equal.In the third example you can create a team consisting of a single student (and you cannot create a team consisting of at least two students).

package main

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

type SA []int

func (s SA) Len() int {
	return len(s)
}

func (s SA) Swap(a, b int) {
	tmp := s[a]
	s[a] = s[b]
	s[b] = tmp
}
func (s SA) Less(i, j int) bool {
	if s[i] < s[j] {
		return true
	}
	return false
}
func main() {
	buf := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscanf(buf, "%d\n", &n)
	mat := make(SA, n)
	for i := 0; i < n; i++ {

		fmt.Fscanf(buf, "%d", &mat[i])

	}
	sort.Sort(mat)
	ans := 1

	var f int
	for i:= 1;i < n;i++{
		for f<=i&&mat[i]-mat[f] > 5{
			f++
		}
		if i-f+1 > ans{
			ans = i-f+1
		}
	}
	fmt.Printf("%d\n", ans)
}

package main

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

type Prob struct {
    in *bufio.Reader
}

func (prob *Prob) NextLine() string {
    line, _, _ := prob.in.ReadLine()
    return string(line[:])
}

func (prob *Prob) NextByte() byte {
    bt, _ := prob.in.ReadByte()
    return bt
}

func (prob *Prob) Next() string {
    var buff bytes.Buffer
    bt := prob.NextByte()
    for bt == '\n' || bt == ' ' {
        bt = prob.NextByte()
    }
    for bt != '\n' && bt != ' ' {
        buff.WriteByte(bt)
        bt = prob.NextByte()
    }
    return buff.String()
}

func (prob *Prob) NextInt() int {
    var x int
    bt := prob.NextByte()
    for bt > '9' || bt < '0' {
        if bt == '-' {
            return -prob.NextInt()
        }
        bt = prob.NextByte()
    }
    for bt >= '0' && bt <= '9' {
        x = x * 10 + int(bt - '0')
        bt = prob.NextByte()
    }
    return x
}

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

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

func (prob Prob) Solve() {
    n := prob.NextInt()
    x := make([]int, n, n)
    for i := 0; i < n; i++ {
        x[i] = prob.NextInt()
    }
    sort.Ints(x)
    res := 0
    j := 0
    for i := 0; i < n; i++ {
        for ; j < n && x[j] - x[i] <= 5; j++ {}
        res = Max(res, j - i)
    }
    fmt.Printf("%d\n", res)
}

func main() {
    Prob{in: bufio.NewReader(os.Stdin)}.Solve()
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n int
	var cnt int

	fmt.Fscan(reader, &n)
	ar := make([]int, n)
	for a := 0; a < n; a++ {
		fmt.Fscan(reader, &ar[a])
	}
	sort.Ints(ar)
	cnt = 0
	var res = 0
	for x := 0; x < n; x++ {
		for ar[cnt]+5 < ar[x] {
			cnt++
		}
		res = max(res, x-cnt+1)

	}
	fmt.Printf("%d", res)
}
func max(a, b int) int {
	var result int

	if a > b {
		result = a
	} else {
		result = b
	}
	return result
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	//"regexp"
	//"strings"
)

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 getLine() string {
	s, _ := reader.ReadString('\n')
	for len(s) > 0 && (s[len(s)-1] == '\n' || s[len(s)-1] == '\r') {
		s = s[:len(s)-1]
	}
	return s
}

func main() {
	// STDOUT MUST BE FLUSHED MANUALLY!!!
	defer writer.Flush()

	var n int
	scanf("%d\n", &n)
	var ns = make([]int, n)
	for i := 0; i < n; i++ {
		scanf("%d", &ns[i])
	}
	sort.Ints(ns)
	var s, max int
	for e := 0; e < n; e++ {
		for ns[e]-ns[s] > 5 {
			s++
		}
		if max < e-s {
			max = e - s
		}
	}
	fmt.Println(max + 1)
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscan(reader, &n)

	stds := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &stds[i])
	}
	sort.Ints(stds)
	var mx int
	for i := 0; i < n; i++ {
		l, r := 0, n-1

		for l < r {
			// fmt.Println(l, r)
			md := (l + r) / 2
			if stds[md]-stds[i] > 5 {
				r = md
			} else {
				l = md + 1
			}
		}

		if stds[l]-stds[i] > 5 {
			l--
		}
		if mx < (l - i + 1) {
			mx = (l - i + 1)
		}

	}
	fmt.Println(mx)
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscan(reader, &n)

	stds := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &stds[i])
	}
	sort.Ints(stds)
	var mx int
	for i := 0; i < n; i++ {
		l, r := 0, n-1

		for l < r {
			md := (l + r) / 2
			if stds[md]-stds[i] > 5 {
				r = md
			} else {
				l = md + 1
			}
		}
		if mx < l-i {
			mx = (l - i)
		}

	}
	fmt.Println(mx)
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscan(reader, &n)

	stds := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &stds[i])
	}
	sort.Ints(stds)
	var mx, count int
	for i := 0; i < n-1; i++ {
		var found bool
		for j := i + 1; j < n; j++ {
			if stds[j]-stds[i] <= 5 {
				count++
			} else {
				i = j - 1
				found = true
				break
			}
		}

		if !found {
			break
		}
		if mx < count {
			mx = count
		}
		count = 0
	}
	if mx < count {
		mx = count
	}
	fmt.Println(mx + 1)
}