Amr lives in Lala Land. Lala Land is a very beautiful country that is located on a coordinate line. Lala Land is famous with its apple trees growing everywhere.Lala Land has exactly n apple trees. Tree number i is located in a position xi and has ai apples growing on it. Amr wants to collect apples from the apple trees. Amr currently stands in x = 0 position. At the beginning, he can choose whether to go right or left. He'll continue in his direction until he meets an apple tree he didn't visit before. He'll take all of its apples and then reverse his direction, continue walking in this direction until he meets another apple tree he didn't visit before and so on. In the other words, Amr reverses his direction when visiting each new apple tree. Amr will stop collecting apples when there are no more trees he didn't visit in the direction he is facing.What is the maximum number of apples he can collect?

The first line contains one number n (1 ≤ n ≤ 100), the number of apple trees in Lala Land. The following n lines contains two integers each xi, ai ( - 105 ≤ xi ≤ 105, xi ≠ 0, 1 ≤ ai ≤ 105), representing the position of the i-th tree and number of apples on it. It's guaranteed that there is at most one apple tree at each coordinate. It's guaranteed that no tree grows in point 0.

Output the maximum number of apples Amr can collect.

NoteIn the first sample test it doesn't matter if Amr chose at first to go left or right. In both cases he'll get all the apples.In the second sample test the optimal solution is to go left to x =  - 1, collect apples from there, then the direction will be reversed, Amr has to go to x = 1, collect apples from there, then the direction will be reversed and Amr goes to the final tree x =  - 2.In the third sample test the optimal solution is to go right to x = 1, collect apples from there, then the direction will be reversed and Amr will not be able to collect anymore apples because there are no apple trees to his left.

package main

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

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

type Tree struct {
	a, x int
}

type ByPos []Tree

func (a ByPos) Len() int           { return len(a) }
func (a ByPos) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ByPos) Less(i, j int) bool { return a[i].x < a[j].x }

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	a := make([]Tree, n+1)

	for i := range a[1:] {
		a[i].x = nextInt()
		a[i].a = nextInt()
	}

	sort.Sort(ByPos(a))

	mid := 0

	for i := 0; i < len(a); i++ {
		if a[i].x == 0 {
			mid = i
			break
		}
	}

	l, r := collectApple(a, mid, 0), collectApple(a, mid, 1)
	// println(l, r)
	// println(a)
	if l > r {
		println(l)
	} else {
		println(r)
	}

}

func collectApple(a []Tree, mid, dir int) int {
	cnt := 0
	l, r := mid-1, mid+1
	for {
		if dir == 1 {
			if r >= len(a) {
				break
			}

			cnt += a[r].a
			r++
		} else {
			if l < 0 {
				break
			}
			cnt += a[l].a
			l--
		}
		dir = 1 - dir
	}
	return cnt
}

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

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

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{})               { fmt.Fprintln(writer, a...) }
func print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }

package main

import (
	"fmt"
	"math"
	"sort"
)

type tree struct {
	X, Apls int
}

type TreesLine []tree

func (b TreesLine) Len() int           { return len(b) }
func (b TreesLine) Less(i, j int) bool { return b[i].X < b[j].X }
func (b TreesLine) Swap(i, j int)      { b[i], b[j] = b[j], b[i] }

func toPath(in_trees TreesLine, curr_index, direct int) int {
	trees := TreesLine(make([]tree, len(in_trees)))
	copy(trees, in_trees)
	i := curr_index
	for trees[i].X == 0 {
		i += direct
		if i < 0 || i >= len(trees) {
			return 0
		}
	}
	trees[i].X = 0
	return trees[i].Apls + toPath(trees, curr_index, -direct)
}

func main() {
	var count int
	fmt.Scan(&count)

	trees := TreesLine{tree{0, 0}}
	for ; count > 0; count-- {
		var x, apls int
		fmt.Scan(&x, &apls)
		trees = append(trees, tree{x, apls})
	}

	sort.Sort(trees)
	nool_i := sort.Search(len(trees), func(i int) bool { return trees[i].X >= 0 })
	max_apls := math.Max(float64(toPath(trees, nool_i, 1)), float64(toPath(trees, nool_i, -1)))
	fmt.Println(int64(max_apls))
}

package main

import (
	"fmt"
)

type AppleTree struct {
	location int
	apples   int
}

func main() {
	var n int
	if _, err := fmt.Scanf("%d\n", &n); err != nil {
		return
	}

	array := make([]*AppleTree, n)
	neg_cnt := 0
	neg_apple := 0
	pos_cnt := 0
	pos_apple := 0

	for i := 0; i < n; i++ {
		var x, a int
		if _, err := fmt.Scanf("%d %d\n", &x, &a); err != nil {
			return
		}
		array[i] = &AppleTree{x, a}
		if x < 0 {
			neg_cnt++
			neg_apple += a
		} else {
			pos_cnt++
			pos_apple += a
		}
	}

	if neg_cnt == pos_cnt {
		fmt.Println(neg_apple + pos_apple)
	} else {
		apples := 0
		bubble_sort(array, n)

		if neg_cnt == 0 {
			fmt.Println(array[0].apples)
			return
		} else if pos_cnt == 0 {
			fmt.Println(array[n-1].apples)
			return
		} else if neg_cnt < pos_cnt {
			apples += neg_apple
			for i := neg_cnt; i < neg_cnt+neg_cnt+1; i++ {
				apples += array[i].apples
			}
		} else if neg_cnt > pos_cnt {
			apples += pos_apple
			for i := neg_cnt - pos_cnt - 1; i < neg_cnt; i++ {
				apples += array[i].apples
			}
		}

		fmt.Println(apples)
	}
}

func swap(a, b int, array []*AppleTree) {
	tmp := array[a]
	array[a] = array[b]
	array[b] = tmp
}

func bubble_sort(array []*AppleTree, length int) {
	for i := 0; i < length; i++ {
		for j := 0; j < length; j++ {
			if array[i].location < array[j].location {
				swap(i, j, array)
			}
		}
	}
}

package main

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

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

	num := 0
	fmt.Fscanf(r, "%d\n", &num)

	coords := make([]int, num, num)
	apples := make(map[int]int, num)

	onright := 0
	onleft := 0

	for i := 0; i < num; i++ {
		var coord int
		var appleNum int
		fmt.Fscanf(r, "%d %d\n", &coord, &appleNum)

		if coord > 0 {
			onright++
		} else if coord < 0 {
			onleft++
		}

		apples[coord] = appleNum
		coords[i] = coord
	}
	sort.Ints(coords)

	if onright > onleft {
		coords = coords[:2*onleft+1]
	} else if onright < onleft {
		coords = coords[len(coords)-2*onright-1:]
	}

	total := 0
	for _, coord := range coords {
		total += apples[coord]
	}

	fmt.Printf("%d\n", total)
}

package main

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

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

	var ps, ng []int64
	elem := make(map[int64]int64)
	for i := 0; i < n; i++ {
		var x, y int64
		fmt.Fscan(reader, &x, &y)
		elem[x] = y

		if x > 0 {
			ps = append(ps, x)
		} else {
			ng = append(ng, x)
		}
	}
	sort.Slice(ps, func(i, j int) bool { return ps[i] < ps[j] })
	sort.Slice(ng, func(i, j int) bool { return ng[i] < ng[j] })

	for i, j := 0, len(ng)-1; i < j; i, j = i+1, j-1 {
		ng[i], ng[j] = ng[j], ng[i]
	}
	var count int64
	ln, mn := len(ng), len(ps)

	xn := min(ln, mn) + 1

	for i := 0; i < min(ln, xn); i++ {
		count += elem[ng[i]]
	}

	for i := 0; i < min(mn, xn); i++ {
		count += (elem[ps[i]])
	}

	fmt.Println(count)
}

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

package main

import (
	"fmt"
	"math"
	"sort"
)

type tree struct {
	X, Apls int
}

type TreesLine []tree

func (b TreesLine) Len() int           { return len(b) }
func (b TreesLine) Less(i, j int) bool { return b[i].X < b[j].X }
func (b TreesLine) Swap(i, j int)      { b[i], b[j] = b[j], b[i] }

func toPath(in_trees TreesLine, curr_index, direct int) int {
	trees := TreesLine(make([]tree, len(in_trees)))
	copy(trees, in_trees)
	i := curr_index
	for trees[i].X == 0 {
		i += direct
		if i < 0 || i >= len(trees) {
			return 0
		}
	}
	trees[i].X = 0
	return trees[i].Apls + toPath(trees, curr_index, -direct)
}

func main() {
	var count int
	fmt.Scan(&count)

	trees := TreesLine{tree{0, 0}}
	for ; count > 0; count-- {
		var x, apls int
		fmt.Scan(&x, &apls)
		trees = append(trees, tree{x, apls})
	}

	sort.Sort(trees)
	nool_i := sort.Search(len(trees), func(i int) bool { return trees[i].X >= 0 })
	max_apls := math.Max(float64(toPath(trees, nool_i, 1)), float64(toPath(trees, nool_i, -1)))
	fmt.Println(max_apls)
}

package main

import (
	"fmt"
)

type AppleTree struct {
	location int
	apples   int
}

func main() {
	var n int
	if _, err := fmt.Scanf("%d\n", &n); err != nil {
		return
	}

	array := make([]*AppleTree, n)
	neg_cnt := 0
	neg_apple := 0
	pos_cnt := 0
	pos_apple := 0

	for i := 0; i < n; i++ {
		var x, a int
		if _, err := fmt.Scanf("%d %d\n", &x, &a); err != nil {
			return
		}
		array[i] = &AppleTree{x, a}
		if x < 0 {
			neg_cnt++
			neg_apple += a
		} else {
			pos_cnt++
			pos_apple += a
		}
	}

	if neg_cnt == pos_cnt {
		fmt.Println(neg_apple + pos_apple)
	} else {
		apples := 0
		start := -1
		end := -1
		bubble_sort(array, n)

		if neg_cnt == 0 {
			fmt.Println(array[0].apples)
			return
		} else if pos_cnt == 0 {
			fmt.Println(array[n-1].apples)
			return
		} else if neg_cnt < pos_cnt {
			apples += neg_apple
			start = neg_cnt
			end = n
		} else if neg_cnt > pos_cnt {
			apples += pos_apple
			start = 0
			end = neg_cnt
		}

		for i := start; i < end; i++ {
			apples += array[i].apples
		}

		fmt.Println(apples)
	}
}

func swap(a, b int, array []*AppleTree) {
	tmp := array[a]
	array[a] = array[b]
	array[b] = tmp
}

func bubble_sort(array []*AppleTree, length int) {
	for i := 0; i < length; i++ {
		for j := 0; j < length; j++ {
			if array[i].location < array[j].location {
				swap(i, j, array)
			}
		}
	}
}

package main

import (
	"fmt"
)

type AppleTree struct {
	location int
	apples   int
}

func main() {
	var n int
	if _, err := fmt.Scanf("%d\n", &n); err != nil {
		return
	}

	array := make([]*AppleTree, n)
	neg_cnt := 0
	neg_apple := 0
	pos_cnt := 0
	pos_apple := 0

	for i := 0; i < n; i++ {
		var x, a int
		if _, err := fmt.Scanf("%d %d\n", &x, &a); err != nil {
			return
		}
		array[i] = &AppleTree{x, a}
		if x < 0 {
			neg_cnt++
			neg_apple += a
		} else {
			pos_cnt++
			pos_apple += a
		}
	}

	if neg_cnt == pos_cnt {
		fmt.Println(neg_apple + pos_apple)
	} else {
		apples := 0
		start := -1
		end := -1
		bubble_sort(array, n)

		if neg_cnt == 0 {
			fmt.Println(array[0].apples)
			return
		} else if pos_cnt == 0 {
			fmt.Println(array[n-1].apples)
			return
		} else if neg_cnt < pos_cnt {
			apples += neg_apple
			start = neg_cnt
			end = n
			for i := start; i < end; i++ {
				apples += array[i].apples
			}
		} else if neg_cnt > pos_cnt {
			apples += pos_apple
			start = neg_cnt - pos_cnt - 1
			end = n - pos_cnt
			fmt.Println(start, end)
			for i := start; i < end; i++ {
				apples += array[i].apples
			}
		}

		fmt.Println(apples)
	}
}

func swap(a, b int, array []*AppleTree) {
	tmp := array[a]
	array[a] = array[b]
	array[b] = tmp
}

func bubble_sort(array []*AppleTree, length int) {
	for i := 0; i < length; i++ {
		for j := 0; j < length; j++ {
			if array[i].location < array[j].location {
				swap(i, j, array)
			}
		}
	}
}

package main

import (
	"fmt"
)

type AppleTree struct {
	location int
	apples   int
}

func main() {
	var n int
	if _, err := fmt.Scanf("%d\n", &n); err != nil {
		return
	}

	array := make([]*AppleTree, n)
	neg_cnt := 0
	neg_apple := 0
	pos_cnt := 0
	pos_apple := 0

	for i := 0; i < n; i++ {
		var x, a int
		if _, err := fmt.Scanf("%d %d\n", &x, &a); err != nil {
			return
		}
		array[i] = &AppleTree{x, a}
		if x < 0 {
			neg_cnt++
			neg_apple += a
		} else {
			pos_cnt++
			pos_apple += a
		}
	}

	if neg_cnt == pos_cnt {
		fmt.Println(neg_apple + pos_apple)
	} else {
		apples := 0
		start := -1
		end := -1
		bubble_sort(array, n)

		if neg_cnt == 0 {
			fmt.Println(array[0].apples)
			return
		} else if pos_cnt == 0 {
			fmt.Println(array[n-1].apples)
			return
		} else if neg_cnt < pos_cnt {
			apples += neg_apple
			start = neg_cnt
			end = n
			for i := start; i < end; i++ {
				apples += array[i].apples
			}
		} else if neg_cnt > pos_cnt {
			apples += pos_apple
			start = neg_cnt - pos_cnt - 1
			end = n - pos_cnt
			for i := start; i < end; i++ {
				apples += array[i].apples
			}
		}

		fmt.Println(apples)
	}
}

func swap(a, b int, array []*AppleTree) {
	tmp := array[a]
	array[a] = array[b]
	array[b] = tmp
}

func bubble_sort(array []*AppleTree, length int) {
	for i := 0; i < length; i++ {
		for j := 0; j < length; j++ {
			if array[i].location < array[j].location {
				swap(i, j, array)
			}
		}
	}
}

package main

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

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

	num := 0
	fmt.Fscanf(r, "%d\n", &num)

	coords := make([]int, 0, num)
	apples := make(map[int]int, num)

	onright := 0
	onleft := 0
	total := 0

	for i := 0; i < num; i++ {
		var coord int
		var appleNum int
		fmt.Fscanf(r, "%d %d\n", &coord, &appleNum)

		if coord > 0 {
			onright++
		} else if coord < 0 {
			onleft++
		} else {
			total += appleNum
		}

		if coord != 0 {
			apples[coord] = appleNum
			coords = append(coords, coord)
		}
	}
	sort.Ints(coords)

	if onright > onleft {
		coords = coords[:2*onleft+1]
	} else if onright < onleft {
		coords = coords[len(coords)-2*onright-1:]
	}

	for _, coord := range coords {
		total += apples[coord]
	}

	println(total)

}

package main

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

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

	var ps, ng []int64
	elem := make(map[int64]int64)
	for i := 0; i < n; i++ {
		var x, y int64
		fmt.Fscan(reader, &x, &y)
		elem[x] = y

		if x > 0 {
			ps = append(ps, x)
		} else {
			ng = append(ng, x)
		}
	}
	sort.Slice(ps, func(i, j int) bool { return ps[i] < ps[j] })
	sort.Slice(ng, func(i, j int) bool { return ng[i] < ng[j] })

	for i, j := 0, len(ng)-1; i < j; i, j = i+1, j-1 {
		ng[i], ng[j] = ng[j], ng[i]
	}
	var count int64
	ln, mn := len(ng), len(ps)
	// fmt.Println(ln, mn)
	for i := 0; i < min(ln, mn); i++ {
		// fmt.Println(min(ln, mn) - i)
		count += (elem[ps[i]] + elem[ng[i]])
		// fmt.Println(elem[ps[i]], elem[ng[min(ln, mn)-i]])
	}
	if ln > mn {
		count += (elem[ng[ln-mn-1]])
	} else if mn > ln {
		count += (elem[ps[ln]])
	}
	fmt.Println(count)
}

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

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

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

package main

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

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

	var ps, ng []int
	elem := make(map[int]int)
	for i := 0; i < n; i++ {
		var x, y int
		fmt.Fscan(reader, &x, &y)
		elem[x] = y

		if x > 0 {
			ps = append(ps, x)
		} else {
			ng = append(ng, x)
		}
	}

	sort.Ints(ps)
	sort.Ints(ng)
	var count int
	ln, mn := len(ng), len(ps)
	fmt.Println(ln, mn)
	for i := 0; i < min(ln, mn); i++ {
		// fmt.Println(min(ln, mn) - i)
		count += elem[ps[i]] + elem[ng[max(ln, mn)-min(ln, mn)-i]]
		// fmt.Println(elem[ps[i]], elem[ng[min(ln, mn)-i]])
	}
	if ln > mn {
		count += elem[ng[ln-mn-1]]
	} else if mn > ln {
		count += elem[ps[ln]]
	}
	fmt.Println(count)
}

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

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

package main

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

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

	var ps, ng []int64
	elem := make(map[int64]int64)
	for i := 0; i < n; i++ {
		var x, y int64
		fmt.Fscan(reader, &x, &y)
		elem[x] = y

		if x > 0 {
			ps = append(ps, x)
		} else {
			ng = append(ng, x)
		}
	}
	sort.Slice(ps, func(i, j int) bool { return ps[i] < ps[j] })
	sort.Slice(ng, func(i, j int) bool { return ng[i] < ng[j] })
	var count int64
	ln, mn := len(ng), len(ps)
	// fmt.Println(ln, mn)
	for i := 0; i < min(ln, mn); i++ {
		// fmt.Println(min(ln, mn) - i)
		count += (elem[ps[i]] + elem[ng[abs(max(ln, mn)-min(ln, mn)-i)]])
		// fmt.Println(elem[ps[i]], elem[ng[min(ln, mn)-i]])
	}
	if ln > mn {
		count += (elem[ng[ln-mn-1]])
	} else if mn > ln {
		count += (elem[ps[ln]])
	}
	fmt.Println(count)
}

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

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

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


You are given a string $$$s$$$ of lowercase Latin letters. The following operation can be used:   select one character (from 'a' to 'z') that occurs at least once in the string. And replace all such characters in the string with the previous one in alphabetical order on the loop. For example, replace all 'c' with 'b' or replace all 'a' with 'z'. And you are given the integer $$$k$$$ — the maximum number of operations that can be performed. Find the minimum lexicographically possible string that can be obtained by performing no more than $$$k$$$ operations.The string $$$a=a_1a_2 \dots a_n$$$ is lexicographically smaller than the string $$$b = b_1b_2 \dots b_n$$$ if there exists an index $$$k$$$ ($$$1 \le k \le n$$$) such that $$$a_1=b_1$$$, $$$a_2=b_2$$$, ..., $$$a_{k-1}=b_{k-1}$$$, but $$$a_k < b_k$$$.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) —the number of test cases in the test. This is followed by descriptions of the test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$1 \le k \le 10^9$$$) — the size of the string $$$s$$$ and the maximum number of operations that can be performed on the string $$$s$$$. The second line of each test case contains a string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.  It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, output the lexicographically minimal string that can be obtained from the string $$$s$$$ by performing no more than $$$k$$$ operations.

4

3 2

cba

4 5

fgde

7 5

gndcafb

4 19

ekyv

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

		ans := make([]byte, len(s))

		cur := uint8('a')

		cur2 := cur
		curCh := uint8('a')

		for i, ch := range s {
			ans[i] = s[i]
			if uint8(ch) <= cur {
				ans[i] = 'a'
			} else if uint8(ch) <= cur2 && ans[i] > curCh {
				ans[i] = curCh
			} else if k > 0 {
				need := int(uint8(ch) - cur)
				if k >= need {
					k -= need
					cur += uint8(need)
					ans[i] = 'a'
				} else {
					cur2 = uint8(ch)
					curCh = s[i] - uint8(k)
					k = 0
					ans[i] = curCh
				}
			}
		}
		Fprintln(out, string(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 min(a, b int) int {
	if a < b {
		return a
	} else {
		return b
	}
}

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

type Key struct {
	n int
	k int
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var niter int
	fmt.Fscan(in, &niter)
	for iiter := 0; iiter < niter; iiter++ {
		var n, k int
		fmt.Fscan(in, &n, &k)
		var s string
		fmt.Fscan(in, &s)
		ms := 'a'
		rem := -1
		lrem := 'a'
		for _, l := range s {
			if l > ms {
				if int(l-ms) <= k {
					k -= int(l - ms)
					ms = l
				} else {
					rem = k
					lrem = l
					break
				}
			}
		}
		if rem == -1 {
			for i := 0; i < n; i++ {
				fmt.Fprintf(out, "%c", 'a')
			}
			fmt.Fprintln(out)
		} else {
			for _, l := range s {
				if l <= ms {
					fmt.Fprintf(out, "%c", 'a')
				} else {
					if l <= lrem && l >= rune(int(lrem)-rem) {
						fmt.Fprintf(out, "%c", rune(int(lrem)-rem))
					} else {
						fmt.Fprintf(out, "%c", l)
					}
				}
			}
			fmt.Fprintln(out)
		}
	}
}

package main

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

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

	var T, n, k int
	var s []byte
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k, &s)
		if k > 24 {
			Fprintln(out, strings.Repeat("a", n))
			continue
		}
		var mx, up, low byte
		mx = 'a'
		for _, b := range s {
			if b > mx {
				if k := byte(k) - (mx - 'a'); b-k > mx {
					up, low = b, b-k
					break
				}
				mx = b
			}
		}
		for i, b := range s {
			if b <= mx {
				s[i] = 'a'
			} else if low <= b && b <= up {
				s[i] = low
			}
		}
		Fprintf(out, "%s\n", s)
	}
}

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

package main

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

//func max(a, b int) int {if a > b {return a} else {return b}}
var r = bufio.NewReader(os.Stdin)
var w = bufio.NewWriter(os.Stdout)
func slove() {
	defer w.Flush()
	var n, k int
	fmt.Fscan(r, &n, &k)
	var s string
	var b []byte
	fmt.Fscan(r, &s)
	b = []byte(s)
	max_toa := 0
	change := make([]int, 26)
	for i := range change {
		change[i] = -1
	}
	change[0] = 'a'
	for i := range b {
		if change[b[i]-'a'] != -1 {
			b[i] = byte(change[b[i]-'a'])
		} else {
			if int(b[i] - 'a') - (max_toa) <= (k) {
				for j:=max_toa+1;j<=int(b[i]-'a');j++ {
					change[j] = 'a'
				}
				k -= int(b[i]-'a') - (max_toa)
				max_toa = int(b[i]-'a')
				b[i] = 'a'
			} else {
				for j:=int(b[i]-'a');j>int(b[i]-'a')-k;j-- {
					change[j] = int(b[i])-k
				}
				b[i] = b[i]-byte(k)
				k = 0
			}
		}
	}
	fmt.Fprintln(w, string(b))
}

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

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()
		K := gi()
		S := gs()
		lookup := make([]int,26); for i:=0;i<26;i++ { lookup[i] = i }; base := 0
		for _,c := range S {
			if lookup[c-'a'] == 0 { continue }
			if int(c-'a') < K  { for b:=base+1;b<=int(c-'a');b++ { lookup[b] = 0 }; base = int(c-'a'); continue }
			if int(c-'a') == K { for b:=base+1;b<=int(c-'a');b++ { lookup[b] = 0 }; break }
			movesLeft := K - base; targLetter := int(c-'a')-movesLeft; for b:=targLetter;b<=int(c-'a');b++ { lookup[b] = targLetter }; break
		}
		ansarr := make([]byte,N); for i,c := range S { ansarr[i] = 'a' + byte(lookup[int(c-'a')]) }
		fmt.Fprintln(wrtr,string(ansarr))
	}
}

package main

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

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

func nextInt() int {
	rd.Scan()
	if n, err := strconv.Atoi(rd.Text()); err == nil {
		return n
	}
	return 0
}
func Max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}
func main() {
	defer wr.Flush()
	rd.Buffer(make([]byte, 0), 1000000009)
	rd.Split(bufio.ScanWords)
	T := nextInt()
	for Case := 0; Case < T; Case++ {
		solve()
	}
}
func solve() {
	n, k := nextInt(), nextInt()
	rd.Scan()
	s := rd.Text()
	maxc := byte('a')
	l, r := byte('z'), byte('z')
	var builder strings.Builder
	builder.Grow(n)
	for _, val := range s {
		if k == 0 {
			if byte(val) <= maxc {
				builder.WriteByte('a')
			} else if byte(val) <= r && byte(val) >= l {
				builder.WriteByte(l)
			} else {
				builder.WriteByte(byte(val))
			}
		} else {
			if byte(val) <= maxc {
				builder.WriteByte('a')
			} else {
				if k >= int(byte(val)-maxc) {
					k -= int(byte(val) - maxc)
					maxc = byte(val)
					builder.WriteByte('a')
				} else {
					l = byte(val) - byte(k)
					r = byte(val)
					k = 0
					builder.WriteByte(l)
				}
			}
		}
	}
	fmt.Fprint(wr, builder.String(),"\n")
}

package main

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

func main() {
	r, w := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer w.Flush()
	var n, k int
	Fscan(r, &n)
	t := 0
	var p int
	var ss string
	for t < n {
		t++
		Fscan(r, &p, &k)
		Fscan(r, &ss)
		if k > 25 {
			k = 25
		}
		s := []byte(ss)
		if s[0] - byte(k) > 'a'{
			ss := s[0]
			for i := range s{
				if s[i] <= ss && s[i] >= ss - byte(k){
					s[i] = ss - byte(k)
				}
			}
			Fprintln(w, string(s))
			continue
		}
		high := byte(0)
		high2 := byte(0)
		for i := 0; i < len(s); i++{
			if int(s[i] - 'a') > k{
				high = s[i]
				break
			}
			if s[i] > high2{
				high2 = s[i]
			}
		}
		l := k - int(high2 - 'a')
		for i := range s{
			if s[i] <= high2{
				s[i] = byte(int(s[i]) - k)
			}else if s[i] <= high && s[i] >= high - byte(l){
				s[i] = high - byte(l)
			}
			if s[i] < 'a'{
				s[i] = 'a'
			}
		}
		Fprintln(w, string(s))
	}
}

package main

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

func main() {
	in, out := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)

	for ii := 0; ii < t; ii++ {
		var n, k int
		fmt.Fscan(in, &n, &k)
		var s string
		fmt.Fscan(in, &s)
		ans := make([]byte, n)
		dic := make(map[byte]byte)
		for i := 0; i < n; i++ {
			ans[i] = s[i]
			if ans[i] == 'a' {
				continue
			}
			if dic[ans[i]] > 0 {
				ans[i] -= dic[ans[i]]
			}
			if k > 0 {
				ori := ans[i]
				for ans[i] > 'a' && k > 0 {
					k--
					ans[i]--
					if dic[ans[i]] > 0 {
						ans[i] -= dic[ans[i]]
					}
				}
				for ; ori > ans[i]; ori-- {
					dic[ori] = ori - ans[i]
				}
			}
		}
		fmt.Fprintln(out, string(ans))
	}
}

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

		ans := make([]byte, len(s))

		cur := uint8('a')
		curCh := uint8('a')

		cur2 := cur
		curCh2 := curCh

		for i, ch := range s {
			ans[i] = s[i]
			if uint8(ch) <= cur && ans[i] > curCh {
				ans[i] = curCh
			} else if uint8(ch) <= cur2 && ans[i] > curCh2 {
				ans[i] = curCh2
			} else if k > 0 {
				need := int(uint8(ch) - cur)
				if k >= need {
					k -= need
					cur += uint8(need)
					ans[i] = curCh
				} else {
					cur2 = uint8(ch)
					curCh2 = s[i] - uint8(k)
					k = 0
					ans[i] = curCh2
				}
			}
		}
		Fprintln(out, string(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"
)

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

		ans := make([]byte, len(s))

		cur := uint8('a')
		curCh := uint8('a')

		cur2 := cur
		curCh2 := curCh

		for i, ch := range s {
			ans[i] = s[i]
			if uint8(ch) <= cur && s[i] > curCh {
				ans[i] = curCh
			} else if uint8(ch) <= cur2 && s[i] > curCh2 {
				ans[i] = curCh2
			} else if k > 0 {
				need := int(uint8(ch) - cur)
				if k >= need {
					k -= need
					cur += uint8(need)
					ans[i] = curCh
				} else {
					cur2 = uint8(ch)
					curCh2 = s[i] - uint8(k)
					k = 0
					ans[i] = curCh2
				}
			}
		}
		Fprintln(out, string(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"
)

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

		ans := make([]byte, len(s))

		cur := uint8('a')
		curCh := uint8('a')
		for i, ch := range s {
			need := int(uint8(ch) - cur)
			ans[i] = s[i]
			if uint8(ch) <= cur && s[i] > curCh {
				ans[i] = curCh
			} else if k > 0 {
				if k >= need {
					k -= need
					cur += uint8(need)
					ans[i] = curCh
				} else {
					cur = uint8(ch)
					curCh = s[i] - uint8(k)
					k = 0
					ans[i] = curCh
				}
			}
		}
		Fprintln(out, string(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"
)

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

		ans := make([]byte, len(s))

		cur := uint8('a')
		curCh := uint8('a')
		for i, ch := range s {
			need := int(uint8(ch) - cur)
			ans[i] = s[i]
			if uint8(ch) <= cur && s[i] > curCh {
				ans[i] = curCh
			} else if k > 0 {
				if k >= need {
					k -= need
					cur += uint8(need)
					ans[i] = curCh
				} else {
					cur = uint8(ch)
					curCh = s[i] - uint8(k)
					k = 01
					ans[i] = curCh
				}
			}
		}
		Fprintln(out, string(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 min(a, b int) int {
	if a < b {
		return a
	} else {
		return b
	}
}

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

type Key struct {
	n int
	k int
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var niter int
	fmt.Fscan(in, &niter)
	for iiter := 0; iiter < niter; iiter++ {
		var n, k int
		fmt.Fscan(in, &n, &k)
		var s string
		fmt.Fscan(in, &s)
		ms := 'a'
		rem := -1
		lrem := 'a'
		for _, l := range s {
			if l > ms {
				if int(l-ms) <= k {
					k -= int(l - ms)
					ms = l
				} else {
					rem = k
					lrem = l
					break
				}
			}
		}
		if rem == -1 {
			for i := 0; i < n; i++ {
				fmt.Fprintf(out, "%c", 'a')
			}
			fmt.Fprintln(out)
		} else {
			for _, l := range s {
				if l <= ms {
					fmt.Fprintf(out, "%c", 'a')
				} else {
					if l <= lrem {
						fmt.Fprintf(out, "%c", rune(int(lrem)-rem))
					} else {
						fmt.Fprintf(out, "%c", l)
					}
				}
			}
			fmt.Fprintln(out)
		}
	}
}

package main

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

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

	var T, n, k int
	var s []byte
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k, &s)
		if k > 24 {
			Fprintln(out, strings.Repeat("a", n))
			continue
		}
		var mx, up, low byte
		mx = 'a'
		for _, b := range s {
			if b > mx {
				if k := byte(k) - (mx - 'a'); b-k > 'a' {
					up, low = b, b-k
					break
				}
				mx = b
			}
		}
		for i, b := range s {
			if b <= mx {
				s[i] = 'a'
			} else if low <= b && b <= up {
				s[i] = low
			}
		}
		Fprintf(out, "%s\n", s)
	}
}

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


As you know, an undirected connected graph with n nodes and n - 1 edges is called a tree. You are given an integer d and a tree consisting of n nodes. Each node i has a value ai associated with it.We call a set S of tree nodes valid if following conditions are satisfied: S is non-empty. S is connected. In other words, if nodes u and v are in S, then all nodes lying on the simple path between u and v should also be presented in S. .Your task is to count the number of valid sets. Since the result can be very large, you must print its remainder modulo 1000000007 (109 + 7).

The first line contains two space-separated integers d (0 ≤ d ≤ 2000) and n (1 ≤ n ≤ 2000). The second line contains n space-separated positive integers a1, a2, ..., an(1 ≤ ai ≤ 2000). Then the next n - 1 line each contain pair of integers u and v (1 ≤ u, v ≤ n) denoting that there is an edge between u and v. It is guaranteed that these edges form a tree.

Print the number of valid sets modulo 1000000007.

NoteIn the first sample, there are exactly 8 valid sets: {1}, {2}, {3}, {4}, {1, 2}, {1, 3}, {3, 4} and {1, 3, 4}. Set {1, 2, 3, 4} is not valid, because the third condition isn't satisfied. Set {1, 4} satisfies the third condition, but conflicts with the second condition.

package main

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

// github.com/EndlessCheng/codeforces-go
func CF486D(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var d, n, v, w, rt, min int
	Fscan(in, &d, &n)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	g := make([][]int, n)
	for i := 1; i < n; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}

	const mod int64 = 1e9 + 7
	var f func(v, fa int) int64
	f = func(v, fa int) int64 {
		s := int64(1)
		for _, w := range g[v] {
			if w != fa && (w > rt && a[w] == min || min < a[w] && a[w] <= min+d) {
				s = s * (f(w, v) + 1) % mod
			}
		}
		return s
	}
	ans := int64(0)
	for rt, min = range a {
		ans += f(rt, -1)
	}
	Fprint(out, ans%mod)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	var d, n, v, w, rt, min int
	Fscan(in, &d, &n)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	g := make([][]int, n)
	for i := 1; i < n; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}

	const mod int64 = 1e9 + 7
	var f func(v, fa int) int64
	f = func(v, fa int) int64 {
		s := int64(1)
		for _, w := range g[v] {
			if w != fa && (w > rt && a[w] == min || min < a[w] && a[w] <= min+d) {
				s = s * (f(w, v) + 1) % mod
			}
		}
		return s
	}
	ans := int64(0)
	for rt, min = range a {
		ans += f(rt, -1)
	}
	Print(ans % mod)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF486D(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var d, n, v, w, min int
	Fscan(in, &d, &n)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	g := make([][]int, n)
	for i := 1; i < n; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}

	const mod int64 = 1e9 + 7
	vis := make([]bool, n)
	var f func(v, fa int) int64
	f = func(v, fa int) int64 {
		if a[v] == min {
			vis[v] = true
		}
		s := int64(1)
		for _, w := range g[v] {
			if w != fa && (a[w] == min && !vis[w] || min < a[w] && a[w] <= min+d) {
				s = s * (f(w, v) + 1) % mod
			}
		}
		return s
	}
	ans := int64(0)
	for v, min = range a {
		ans += f(v, -1)
	}
	Fprint(out, ans%mod)
}

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


'Twas the night before Christmas, and Santa's frantically setting up his new Christmas tree! There are $$$n$$$ nodes in the tree, connected by $$$n-1$$$ edges. On each edge of the tree, there's a set of Christmas lights, which can be represented by an integer in binary representation.  He has $$$m$$$ elves come over and admire his tree. Each elf is assigned two nodes, $$$a$$$ and $$$b$$$, and that elf looks at all lights on the simple path between the two nodes. After this, the elf's favorite number becomes the bitwise XOR of the values of the lights on the edges in that path.However, the North Pole has been recovering from a nasty bout of flu. Because of this, Santa forgot some of the configurations of lights he had put on the tree, and he has already left the North Pole! Fortunately, the elves came to the rescue, and each one told Santa what pair of nodes he was assigned $$$(a_i, b_i)$$$, as well as the parity of the number of set bits in his favorite number. In other words, he remembers whether the number of $$$1$$$'s when his favorite number is written in binary is odd or even.Help Santa determine if it's possible that the memories are consistent, and if it is, remember what his tree looked like, and maybe you'll go down in history!

The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains two integers, $$$n$$$ and $$$m$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq m \leq 2 \cdot 10^5$$$) — the size of tree and the number of elves respectively. The next $$$n-1$$$ lines of each test case each contains three integers, $$$x$$$, $$$y$$$, and $$$v$$$ ($$$1 \leq x, y \leq n$$$; $$$-1 \leq v < 2^{30}$$$) — meaning that there's an edge between nodes $$$x$$$ and $$$y$$$. If    $$$v = -1$$$: Santa doesn't remember what the set of lights were on for this edge.  $$$v \geq 0$$$: The set of lights on the edge is $$$v$$$.  The next $$$m$$$ lines of each test case each contains three integers, $$$a$$$, $$$b$$$, and $$$p$$$ ($$$1 \leq a, b \leq n$$$; $$$a \neq b$$$; $$$0 \leq p \leq 1$$$) — the nodes that the elf was assigned to, and the parity of the number of set bits in the elf's favorite number. It is guaranteed that the sum of all $$$n$$$ and the sum of all $$$m$$$ don't exceed $$$2 \cdot 10^5$$$ each. It is guaranteed that the given edges form a tree.

For each test case, first print either YES or NO (in any case), whether there's a tree consistent with Santa's memory or not.  If the answer is YES, print $$$n-1$$$ lines each containing three integers: $$$x$$$, $$$y$$$, and $$$v$$$ ($$$1 \le x, y \le n$$$; $$$0 \le v < 2^{30}$$$) — the edge and the integer on that edge. The set of edges must be the same as in the input, and if the value of some edge was specified earlier, it can not change. You can print the edges in any order. If there are multiple answers, print any.

NoteThe first test case is the image in the statement.One possible answer is assigning the value of the edge $$$(1, 2)$$$ to $$$5$$$, and the value of the edge $$$(2, 5)$$$ to $$$3$$$. This is correct because:   The first elf goes from node $$$2$$$ to node $$$3$$$. This elf's favorite number is $$$4$$$, so he remembers the value $$$1$$$ (as $$$4$$$ has an odd number of $$$1$$$ bits in its binary representation).  The second elf goes from node $$$2$$$ to node $$$5$$$. This elf's favorite number is $$$3$$$, so he remembers the value $$$0$$$ (as $$$3$$$ has an even number of $$$1$$$ bits in its binary representation).  The third elf goes from node $$$5$$$ to node $$$6$$$. This elf's favorite number is $$$7$$$, so he remembers the value $$$1$$$ (as $$$7$$$ has an odd number of $$$1$$$ bits in its binary representation).  The fourth elf goes from node $$$6$$$ to node $$$1$$$. This elf's favorite number is $$$1$$$, so he remembers the value $$$1$$$ (as $$$1$$$ has an odd number of $$$1$$$ bits in its binary representation).  The fifth elf goes from node $$$4$$$ to node $$$5$$$. This elf's favorite number is $$$4$$$, so he remembers the number $$$1$$$ (as $$$4$$$ has an odd number of $$$1$$$ bits in its binary representation). Note that there are other possible answers.

package main

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

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

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

func redirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

func main() {
	defer writer.Flush()
	// redirectInputToFile(reader, "input.txt")

	T := 1
	scan(&T)
	for t := 0; t < T; t++ {
		solve()
	}
}

func solve() {
	var n, m int
	scan(&n, &m)

	var (
		edges = make([][3]int, n-1)
		adj   = make([][]edge, n)
	)
	for i := 0; i < n-1+m; i++ {
		var x, y, w int
		scan(&x, &y, &w)
		if i < n-1 {
			edges[i] = [3]int{x - 1, y - 1, w}
		}
		if w == -1 {
			continue
		}
		adj[x-1] = append(adj[x-1], edge{y - 1, parity(w)})
		adj[y-1] = append(adj[y-1], edge{x - 1, parity(w)})
	}

	var (
		walk     func(curr, val, prev int)
		ans      = make([]int, n)
		solvable = true
	)
	for i := range ans {
		ans[i] = -1
	}
	walk = func(curr, val, prev int) {
		if ans[curr] != -1 {
			solvable = solvable && ans[curr] == val
			return
		}
		ans[curr] = val
		for _, e := range adj[curr] {
			if e.v == prev {
				continue
			}
			walk(e.v, val^e.p, curr)
		}
	}
	for i := range ans {
		if ans[i] == -1 {
			walk(i, 0, -1)
		}
	}
	if !solvable {
		printf("NO\n")
		return
	}
	printf("YES\n")
	for _, e := range edges {
		w := e[2]
		if w == -1 {
			w = ans[e[0]] ^ ans[e[1]]
		}
		printf("%v %v %v\n", e[0]+1, e[1]+1, w)
	}
}

type edge struct {
	v, p int
}

func parity(x int) (r int) {
	for x > 0 {
		x &= x - 1
		r ^= 1
	}
	return
}

package main

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

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

const TAM = 2e5 + 100

type Edge struct {
	to int
	x  int
}

var n int
var parent [TAM]int
var parity [TAM]int
var accXor [TAM]int
var tree [TAM][]Edge
var parityGraph [TAM][]Edge
var bfsVisited [TAM]bool

func rootTree(node int) {
	for _, e := range tree[node] {
		if e.to != parent[node] {
			parent[e.to] = node
			rootTree(e.to)
		}
	}
}

func readInput() {
	var nConditions int
	fmt.Fscan(in, &n, &nConditions)
	for i := 1; i <= n; i++ {
		parent[i] = 0
		parity[i] = -1
		accXor[i] = 0
		tree[i] = []Edge{}
		parityGraph[i] = []Edge{}
		bfsVisited[i] = false
	}

	var fr, to, x int
	for edges := 0; edges < n-1; edges++ {
		fmt.Fscan(in, &fr, &to, &x)
		tree[fr] = append(tree[fr], Edge{to: to, x: x})
		tree[to] = append(tree[to], Edge{to: fr, x: x})

		if x >= 0 {
			var parityX = calcParity(x)
			parityGraph[fr] = append(parityGraph[fr], Edge{to: to, x: parityX})
			parityGraph[to] = append(parityGraph[to], Edge{to: fr, x: parityX})
		}
	}

	for edges := 0; edges < nConditions; edges++ {
		fmt.Fscan(in, &fr, &to, &x)
		parityGraph[fr] = append(parityGraph[fr], Edge{to: to, x: x})
		parityGraph[to] = append(parityGraph[to], Edge{to: fr, x: x})
	}

	rootTree(1)
}

func setParity(node, p int) bool {
	if parity[node] == p {
		return true
	}
	if parity[node] != -1 {
		return false
	}
	parity[node] = p
	for _, e := range parityGraph[node] {
		if !setParity(e.to, p^e.x) {
			return false
		}
	}
	return true
}

func calcParity(x int) int {
	var ans = 0
	for i := 0; i < 30; i++ {
		if ((x >> i) & 1) != 0 {
			ans++
		}
	}
	return ans % 2
}

func solveBFS(start int) bool {
	// fmt.Fprintf(out, "start BFS at %d\n", start)
	var queue []int
	queue = append(queue, start)
	for len(queue) > 0 {
		var node = queue[0]
		bfsVisited[node] = true
		// fmt.Fprintf(out, "bfs(%d) parity = %d, acc = %d\n", node, parity[node], accXor[node])
		queue = queue[1:]

		for i, e := range tree[node] {
			if e.to == parent[node] || parity[e.to] == -1 {
				continue
			}
			if e.x != -1 {
				accXor[e.to] = accXor[node] ^ e.x
				if parity[e.to] != calcParity(accXor[e.to]) {
					return false
				}
			} else {
				if parity[node] == parity[e.to] {
					accXor[e.to] = accXor[node]
				} else {
					accXor[e.to] = accXor[node] ^ 1
				}
				tree[node][i].x = accXor[e.to] ^ accXor[node]
			}
			queue = append(queue, e.to)
		}

	}
	return true
}

func solve() {
	for node := 1; node <= n; node++ {
		if parity[node] == -1 {
			if !setParity(node, 0) {
				fmt.Fprintf(out, "NO\n")
				return
			}
		}
	}
	if !solveBFS(1) {
		fmt.Fprintf(out, "NO\n")
		return
	}
	fmt.Fprintf(out, "YES\n")
	for fr := 1; fr <= n; fr++ {
		for _, e := range tree[fr] {
			if e.to != parent[fr] {
				fmt.Fprintf(out, "%d %d %d\n", fr, e.to, e.x)
			}
		}
	}
}

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

package main

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

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

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

func redirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

func main() {
	// defer writer.Flush()
	// redirectInputToFile(reader, "input.txt")

	T := 1
	scan(&T)
	for t := 0; t < T; t++ {
		solve()
	}
}

func solve() {
	var n, m int
	scan(&n, &m)

	var (
		edges = make([][3]int, n-1)
		adj   = make([][]edge, n)
	)
	for i := 0; i < n-1+m; i++ {
		var x, y, w int
		scan(&x, &y, &w)
		if i < n-1 {
			edges[i] = [3]int{x - 1, y - 1, w}
		}
		if w == -1 {
			continue
		}
		adj[x-1] = append(adj[x-1], edge{y - 1, parity(w)})
		adj[y-1] = append(adj[y-1], edge{x - 1, parity(w)})
	}

	var (
		walk     func(v, val, prev int)
		ans      = make([]int, n)
		solvable = true
	)
	walk = func(v, val, prev int) {
		if ans[v] > 0 {
			solvable = solvable && ans[v] == val
			return
		}
		ans[v] = val
		for _, e := range adj[v] {
			if e.v == prev {
				continue
			}
			walk(e.v, val^e.p, v)
		}
	}
	for i := range ans {
		if ans[i] == 0 {
			walk(i, 0, -1)
		}
	}
	if !solvable {
		printer.WriteString("NO\n")
		return
	}
	printer.WriteString("YES\n")
	for _, e := range edges {
		w := e[2]
		if w == -1 {
			w = ans[e[0]] ^ ans[e[1]]
		}
		printer.printInt(e[0] + 1)
		printer.WriteByte(' ')
		printer.printInt(e[1] + 1)
		printer.WriteByte(' ')
		printer.printInt(w)
		printer.WriteByte('\n')
	}
	writer.WriteString(printer.String())
	printer.Reset()
	writer.Flush()
}

type edge struct {
	v, p int
}

func parity(x int) (r int) {
	for x > 0 {
		x &= x - 1
		r ^= 1
	}
	return
}

var printer = FastPrinter{}

type FastPrinter struct{ strings.Builder }

func (p *FastPrinter) printInt(i int) {
	if i == 0 {
		p.WriteByte('0')
		return
	}
	neg := false
	if i < 0 {
		neg = true
		i = -i
	}
	buf := []byte{}
	for i > 0 {
		buf = append(buf, '0'+byte(i%10))
		i /= 10
	}
	if neg {
		buf = append(buf, '-')
	}
	n := len(buf)
	for i := range buf {
		buf[i], buf[n-1-i] = buf[n-1-i], buf[i]
	}
	p.Write(buf)
}

package main

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

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

const TAM = 2e5 + 100

type Edge struct {
	to int
	x  int
}

var n int
var parent [TAM]int
var parity [TAM]int
var accXor [TAM]int
var tree [TAM][]Edge
var parityGraph [TAM][]Edge
var bfsVisited [TAM]bool
var order []int

func rootTree(node int) {
	order = append(order, node)
	for _, e := range tree[node] {
		if e.to != parent[node] {
			parent[e.to] = node
			rootTree(e.to)
		}
	}
}

func readInput() {
	var nConditions int
	fmt.Fscan(in, &n, &nConditions)
	for i := 1; i <= n; i++ {
		parent[i] = 0
		parity[i] = -1
		accXor[i] = 0
		tree[i] = []Edge{}
		parityGraph[i] = []Edge{}
		bfsVisited[i] = false
	}
	order = []int{}

	var fr, to, x int
	for edges := 0; edges < n-1; edges++ {
		fmt.Fscan(in, &fr, &to, &x)
		tree[fr] = append(tree[fr], Edge{to: to, x: x})
		tree[to] = append(tree[to], Edge{to: fr, x: x})
	}

	for edges := 0; edges < nConditions; edges++ {
		fmt.Fscan(in, &fr, &to, &x)
		parityGraph[fr] = append(parityGraph[fr], Edge{to: to, x: x})
		parityGraph[to] = append(parityGraph[to], Edge{to: fr, x: x})
	}

	rootTree(1)
}

func setParity(node, p int) bool {
	if parity[node] == p {
		return true
	}
	if parity[node] != -1 {
		return false
	}
	parity[node] = p
	for _, e := range parityGraph[node] {
		if !setParity(e.to, p^e.x) {
			return false
		}
	}
	return true
}

func calcParity(x int) int {
	var ans = 0
	for i := 0; i < 30; i++ {
		if ((x >> i) & 1) != 0 {
			ans++
		}
	}
	return ans % 2
}

func solveBFS(start int) bool {
	// fmt.Fprintf(out, "start BFS at %d\n", start)
	var queue []int
	queue = append(queue, start)
	for len(queue) > 0 {
		var node = queue[0]
		bfsVisited[node] = true
		// fmt.Fprintf(out, "bfs(%d) parity = %d, acc = %d\n", node, parity[node], accXor[node])
		queue = queue[1:]

		for _, e := range tree[node] {
			if e.to == parent[node] {
				continue
			}
			if e.x != -1 && parity[e.to] == -1 {
				var toParity = calcParity(accXor[node] ^ e.x)
				if !setParity(e.to, toParity) {
					return false
				}
			}
		}

		for i, e := range tree[node] {
			if e.to == parent[node] || parity[e.to] == -1 {
				continue
			}
			if e.x != -1 {
				accXor[e.to] = accXor[node] ^ e.x
				if parity[e.to] != calcParity(accXor[e.to]) {
					return false
				}
			} else {
				if parity[node] == parity[e.to] {
					accXor[e.to] = accXor[node]
				} else {
					accXor[e.to] = accXor[node] ^ 1
				}
				tree[node][i].x = accXor[e.to] ^ accXor[node]
			}
			queue = append(queue, e.to)
		}

	}
	return true
}

func solve() {
	for i := 0; i < n; i++ {
		var node = order[i]
		if bfsVisited[node] {
			continue
		}
		if parity[node] == -1 {
			if !setParity(node, 0) {
				fmt.Fprintf(out, "NO\n")
				return
			}
		}
		if i != 0 {
			var fr = parent[node]
			var to = node
			for i, e := range tree[fr] {
				if e.to == to {
					accXor[e.to] = accXor[fr]
					tree[fr][i].x = 0
				}
			}
		}
		if !solveBFS(node) {
			fmt.Fprintf(out, "NO\n")
			return
		}
	}
	fmt.Fprintf(out, "YES\n")
	for fr := 1; fr <= n; fr++ {
		for _, e := range tree[fr] {
			if e.to != parent[fr] {
				fmt.Fprintf(out, "%d %d %d\n", fr, e.to, e.x)
			}
		}
	}
}

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

package main

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

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

const TAM = 2e5 + 100

type Edge struct {
	to int
	x  int
}

var n int
var parent [TAM]int
var order []int
var parity [TAM]int
var accXor [TAM]int
var tree [TAM][]Edge
var parityGraph [TAM][]Edge

func rootTree(node int) {
	order = append(order, node)
	for _, e := range tree[node] {
		if e.to != parent[node] {
			parent[e.to] = node
			rootTree(e.to)
		}
	}
}

func readInput() {
	var nConditions int
	fmt.Fscan(in, &n, &nConditions)
	for i := 1; i <= n; i++ {
		parent[i] = 0
		tree[i] = []Edge{}
		parityGraph[i] = []Edge{}
		parity[i] = -1
		order = []int{}
	}

	var fr, to, x int
	for edges := 0; edges < n-1; edges++ {
		fmt.Fscan(in, &fr, &to, &x)
		tree[fr] = append(tree[fr], Edge{to: to, x: x})
		tree[to] = append(tree[to], Edge{to: fr, x: x})
	}

	for edges := 0; edges < nConditions; edges++ {
		fmt.Fscan(in, &fr, &to, &x)
		parityGraph[fr] = append(parityGraph[fr], Edge{to: to, x: x})
		parityGraph[to] = append(parityGraph[to], Edge{to: fr, x: x})
	}

	rootTree(1)
}

func setParity(node, p int) bool {
	if parity[node] == p {
		return true
	}
	if parity[node] != -1 {
		return false
	}
	parity[node] = p
	for _, e := range parityGraph[node] {
		if !setParity(e.to, p^e.x) {
			return false
		}
	}
	return true
}

func calcParity(x int) int {
	var ans = 0
	for i := 0; i < 30; i++ {
		if ((x >> i) & 1) != 0 {
			ans++
		}
	}
	return ans % 2
}

func solveBFS(start int) bool {
	// fmt.Fprintf(out, "start BFS at %d\n", start)
	var queue []int
	queue = append(queue, start)
	for len(queue) > 0 {
		var node = queue[0]
		// fmt.Fprintf(out, "bfs(%d) parity = %d, acc = %d\n", node, parity[node], accXor[node])
		queue = queue[1:]

		for _, e := range tree[node] {
			if e.to == parent[node] {
				continue
			}
			if e.x != -1 && parity[e.to] == -1 {
				var startParity = calcParity(accXor[node] ^ e.x)
				if !setParity(e.to, startParity) {
					return false
				}
			}
		}

		for i, e := range tree[node] {
			if e.to == parent[node] || parity[e.to] == -1 {
				continue
			}
			if e.x != -1 {
				accXor[e.to] = accXor[node] ^ e.x
				if parity[e.to] != calcParity(accXor[e.to]) {
					return false
				}
			} else {
				if parity[node] == parity[e.to] {
					accXor[e.to] = accXor[node]
				} else {
					accXor[e.to] = accXor[node] ^ 1
				}
				tree[node][i].x = accXor[e.to] ^ accXor[node]
			}
			queue = append(queue, e.to)
		}

	}
	return true
}

func solve() {
	for i := 0; i < n; i++ {
		if parity[order[i]] == -1 {
			if !setParity(order[i], 0) {
				fmt.Fprintf(out, "NO\n")
				return
			}
			if i != 0 {
				var fr = parent[order[i]]
				var to = order[i]
				for i, e := range tree[fr] {
					if e.to == to {
						accXor[e.to] = accXor[fr]
						tree[fr][i].x = 0
					}
				}
			}
			if !solveBFS(order[i]) {
				fmt.Fprintf(out, "NO\n")
				return
			}
		}
	}
	fmt.Fprintf(out, "YES\n")
	for fr := 1; fr <= n; fr++ {
		for _, e := range tree[fr] {
			if e.to != parent[fr] {
				fmt.Fprintf(out, "%d %d %d\n", fr, e.to, e.x)
			}
		}
	}
}

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


You are given two positive integers $$$n$$$ and $$$s$$$. Find the maximum possible median of an array of $$$n$$$ non-negative integers (not necessarily distinct), such that the sum of its elements is equal to $$$s$$$.A median of an array of integers of length $$$m$$$ is the number standing on the $$$\lceil {\frac{m}{2}} \rceil$$$-th (rounding up) position in the non-decreasing ordering of its elements. Positions are numbered starting from $$$1$$$. For example, a median of the array $$$[20,40,20,50,50,30]$$$ is the $$$\lceil \frac{m}{2} \rceil$$$-th element of $$$[20,20,30,40,50,50]$$$, so it is $$$30$$$. There exist other definitions of the median, but in this problem we use the described definition.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Description of the test cases follows. Each test case contains a single line with two integers $$$n$$$ and $$$s$$$ ($$$1 \le n, s \le 10^9$$$) — the length of the array and the required sum of the elements.

For each test case print a single integer — the maximum possible median.

NotePossible arrays for the first three test cases (in each array the median is underlined):  In the first test case $$$[\underline{5}]$$$  In the second test case $$$[\underline{2}, 3]$$$  In the third test case $$$[1, \underline{2}, 2]$$$

package main

import (
	"bufio"
	"math"
	"os"
	"strconv"
)

func solve() {
	n := get64()
	s := get64()

	l, r := L(0), s+1
	for r-l > 1 {
		m := (l + r) / 2
		if m*(n/2+1) <= s {
			l = m
			continue
		}
		r = m
	}

	put64ln(l)
}

const (
	once       = false
	maxBufSize = 10000000
)

var (
	splitFunc = bufio.ScanWords
)

func main() {
	in = bufio.NewScanner(bufio.NewReader(os.Stdin))
	in.Split(splitFunc)
	in.Buffer(make([]byte, maxBufSize), maxBufSize)

	out = bufio.NewWriter(os.Stdout)
	defer out.Flush()

	if once {
		solve()
		return
	}

	t := get()
	for t > 0 {
		solve()
		t--
	}
}

var (
	in  *bufio.Scanner
	out *bufio.Writer
)

func get() int {
	x, _ := strconv.Atoi(gets())
	return x
}

func geta(n int) []int {
	a := make([]int, n)
	for i := range a {
		a[i] = get()
	}
	return a
}

func geta1(n int) []int {
	a := make([]int, n)
	for i := range a {
		a[i] = get() - 1
	}
	return a
}

func get64() int64 {
	x, _ := strconv.ParseInt(gets(), 10, 64)
	return x
}

func get64a(n int) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = get64()
	}
	return a
}

func get64a1(n int) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = get64() - 1
	}
	return a
}

func getf() float64 {
	x, _ := strconv.ParseFloat(gets(), 64)
	return x
}

func getfa(n int) []float64 {
	a := make([]float64, n)
	for i := range a {
		a[i] = getf()
	}
	return a
}

func gets() string {
	scan()
	return in.Text()
}

func scanN(n int) {
	for n > 0 {
		scan()
		n--
	}
}

func scan() {
	in.Scan()
}

func putc(c rune) {
	out.WriteRune(c)
}

func put(x int) {
	out.WriteString(strconv.Itoa(x))
}

func putln(i int) {
	put(i)
	line()
}

func puta(a []int) {
	for _, v := range a {
		put(v)
		space()
	}
	line()
}

func put64(x int64) {
	out.WriteString(strconv.FormatInt(x, 10))
}

func put64ln(x int64) {
	put64(x)
	line()
}

func put64a(a []int64) {
	for _, v := range a {
		put64(v)
		space()
	}
	line()
}

func puts(s string) {
	out.WriteString(s)
}

func putsln(s string) {
	puts(s)
	line()
}

func yn(y bool) {
	if y {
		yes()
		return
	}
	no()
}

func yes() {
	puts("YES\n")
}

func no() {
	puts("NO\n")
}

func putf(x float64) {
	out.WriteString(strconv.FormatFloat(x, 'f', -1, 64))
}

func putfln(x float64) {
	putf(x)
	line()
}

func putfa(a []float64) {
	for _, v := range a {
		putf(v)
		space()
	}
	line()
}

func line() {
	out.WriteRune('\n')
}

func space() {
	out.WriteRune(' ')
}

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

func abs64(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

func absf(x float64) float64 {
	return math.Abs(x)
}

func max(x int, y int) int {
	if x > y {
		return x
	}
	return y
}

func max64(x int64, y int64) int64 {
	if x > y {
		return x
	}
	return y
}

func maxf(x float64, y float64) float64 {
	if x > y {
		return x
	}
	return y
}

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

func min64(x int64, y int64) int64 {
	if x < y {
		return x
	}
	return y
}

func minf(x float64, y float64) float64 {
	if x < y {
		return x
	}
	return y
}

func L(i int) int64 {
	return int64(i)
}

package main

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

//goland:noinspection GoUnusedFunction
func min(a int, nums ...int) int {
	min := a

	for _, num := range nums {
		if num < min {
			min = num
		}
	}
	return min
}

//goland:noinspection GoUnusedFunction
func max(a int, nums ...int) int {
	max := a

	for _, num := range nums {
		if num > max {
			max = num
		}
	}
	return max
}

//goland:noinspection GoUnusedFunction
func fprintlnArr(w io.Writer, arr []int) {
	for _, val := range arr {
		fmt.Fprint(w, val, " ")
	}
	fmt.Fprintln(w)
}

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

	var t int
	for fmt.Fscan(in, &t); t > 0; t-- {
		var n, s int
		fmt.Fscan(in, &n, &s)
		// s/(n/2+1)
		/*fmt.Fprintln(out, sort.Search(s/(n/2+1), func(x int) bool {
			return s <= (n/2+1)*x
		}))*/
		fmt.Fprintln(out, s/(n/2+1))
	}
}

package main

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

//goland:noinspection GoUnusedFunction
func min(a int, nums ...int) int {
	min := a

	for _, num := range nums {
		if num < min {
			min = num
		}
	}
	return min
}

//goland:noinspection GoUnusedFunction
func max(a int, nums ...int) int {
	max := a

	for _, num := range nums {
		if num > max {
			max = num
		}
	}
	return max
}

//goland:noinspection GoUnusedFunction
func fprintlnArr(w io.Writer, arr []int) {
	for _, val := range arr {
		fmt.Fprint(w, val, " ")
	}
	fmt.Fprintln(w)
}

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

	var t int
	for fmt.Fscan(in, &t); t > 0; t-- {
		var n, s int
		fmt.Fscan(in, &n, &s)
		fmt.Fprintln(out, sort.Search(s/(n/2+1), func(x int) bool {
			//fmt.Fprintln(out, x)
			return s <= x+n/2*x
		}))
	}
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var t, n, s int
	fmt.Scan(&t)

	for i := 0; i < t; i++ {
		fmt.Scan(&n, &s)
		cells := int(math.Ceil(float64(n) / 2.0))
		if n%2 == 0 {
			cells++
		}

		fmt.Println(s/cells)
	}
}

package main

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

func main() {
	r := bufio.NewReaderSize(os.Stdin, 1024*1024)

	var T int

	fmt.Fscanf(r, "%d\n", &T)

	for t := 0; t < T; t++ {
		var n, s int
		fmt.Fscanf(r, "%d %d\n", &n, &s)

		x := s / (n/2 + 1)
		fmt.Println(x)
	}
}

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 n,s int
		fmt.Fscan(in,&n,&s)
		fmt.Fprintln(out,(s/((n/2)+1)))
	}
}

package main

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

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

func (in *CF1566A) 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 *CF1566A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1566A
Date: 13/03/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1566/A
Problem: CF1566A
**/
func (in *CF1566A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n, s := in.NextInt(), in.NextInt()
		pos := int(math.Ceil(float64(n)/2.0)) - 1
		if n-pos != 0 {
			nums := int(math.Floor(float64(s / (n - pos))))
			fmt.Println(nums)
		} else {
			fmt.Println(s)
		}
	}
}

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

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

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n, s := 0, 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n, &s)
		fmt.Fprintln(w, s/((n>>1)+1))
	}
	w.Flush()
}

func main() {
	solve()
}

package main

import (
	"fmt"
	"log"
)

var (
	t int
)

func main() {
	if _, err := fmt.Scan(&t); err != nil {
		log.Fatal(err)
	}

	for i := 0; i < t; i++ {
		n, s := getValues()
		res := binSearch(n, s)
		fmt.Println(res)
	}
}

func getValues() (int64, int64) {
	var n, s int64
	if _, err := fmt.Scan(&n, &s); err != nil {
		log.Fatal(err)
	}
	return n, s
}

func binSearch(n, s int64) int64 {
	var lowerBound int64 = 0
	var upperBound int64 = s * 10
	for upperBound-lowerBound > 1 {

		var Median int64 = (lowerBound + upperBound) / 2
		var m int64 = n/2 + 1

		if m*Median <= s {
			lowerBound = Median
		} else {
			upperBound = Median
		}
	}
	return lowerBound
}

package main

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

func solve(n, s int) int {
	return s / (n - (n-1)/2)
}

var (
	reader  = bufio.NewReader(os.Stdin)
	t, n, s int
)

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

package main

import "fmt"

func main() {
	var t, n, s, temp int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		fmt.Scan(&n, &s)
		// fmt.Print(ans, "**")
		temp = (n + 1) / 2
		temp = n - temp + 1
		s = s / temp
		fmt.Println(s)
	}
}

package main

import (
	"fmt"
	"math"
)

func maxMedian(n int, s int) int {
	m := int(math.Ceil(float64(n) / 2))
	l := n - m + 1

	return s / l;
}

func main() {
	t := 0
	fmt.Scanln(&t)

	for i := 0; i < t; i++ {
		n := 0
		s := 0

		fmt.Scan(&n)
		fmt.Scan(&s)
		r := maxMedian(n, s)
		fmt.Println(r)
	}
}

package main

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

// var m []int
// err = json.Unmarshal([]byte(r.FormValue("example")), &m)

// for i := 0; i < len(m); i++ {
// 	fmt.Println(m[i])
// }

// sort.Slice(arr, func(i, j int) bool {
// 	return arr[i] > arr[j]
// })

func MIN(x, y int64) int64 {
	if x > y {
		return y
	} else {
		return x
	}
}

func MINN(x, y int) int {
	if x > y {
		return y
	} else {
		return x
	}
}

func MKO(x, y int64) int64 {
	if x < y {
		return 0
	} else {
		return x - y + 1
	}
}

func MAX(x, y int64) int64 {
	if x > y {
		return x
	} else {
		return y
	}
}

func MAXX(x, y int) int {
	if x > y {
		return x
	} else {
		return y
	}
}

func LCM(x, y int) int {
	if x >= y {
		initial := x
		for i := 1; i < y+2; i++ {
			if initial%y == 0 {
				break
			} else {
				initial = initial + x
			}

		}
		return initial
	} else {
		initial := y
		for i := 1; i < x+2; i++ {
			if initial%x == 0 {
				break
			} else {
				initial = initial + y
			}

		}
		return initial
	}
}

func GCD(x, y int64) int64 {
	if x*y == 0 {
		return MAX(x, y)
	} else {
		var l, q int64
		l = MAX(x, y)
		q = MIN(x, y)
		var m int64
		for l*q > 0 {
			a := l % q
			b := l - a
			cc := int64(b / q)
			remain := l - cc*q
			l = MAX(remain, q)
			q = MIN(remain, q)
			if l*q == 0 {
				m = l + q
				break
			}

		}
		return m
	}
}

func IsItPrime(x int) bool {
	if x < 10 {
		if x == 2 || x == 3 || x == 5 || x == 7 {
			return true
		} else {
			return false
		}
	} else {
		j := 0
		k := math.Sqrt(float64(x))
		for m := 3; m <= int(k); {
			if x%m == 0 {
				j = 1
				break
			}
			m = m + 2
		}
		if j == 1 {
			return false
		} else {
			return true
		}
	}
}

func PrimeFactorization(n int) (pfs map[int]int) {
	pfs = make(map[int]int)

	// Get the number of 2s that divide n
	for n%2 == 0 {
		if _, ok := pfs[2]; ok {
			pfs[2] += 1
		} else {
			pfs[2] = 1
		}
		n = n / 2
	}

	// n must be odd at this point. so we can skip one element
	// (note i = i + 2)
	for i := 3; i*i <= n; i = i + 2 {
		// while i divides n, append i and divide n
		for n%i == 0 {
			if _, ok := pfs[i]; ok {
				pfs[i] += 1
			} else {
				pfs[i] = 1
			}
			n = n / i
		}
	}

	// This condition is to handle the case when n is a prime number
	// greater than 2
	if n > 2 {
		pfs[n] = 1
	}

	return
}

func NumberOfDivisors(n int) int {
	pfs := PrimeFactorization(n)

	num := 1
	for _, exponents := range pfs {
		num *= (exponents + 1)
	}

	return num
}

func KthPrime(x int) (y int) {
	if x == 1 {
		y = 2
	}
	if x == 2 {
		y = 3
	}
	if x > 2 {
		var sum int = 2
		begin := 5
		for sum < x+1 {
			if IsItPrime(begin) == true {
				sum++
			}
			if sum == x {
				y = begin
				break
			} else {
				if begin%6 == 1 {
					begin = begin + 4
				} else {
					begin = begin + 2
				}
			}
		}
	}
	return
}

func MODULE(x, y int) int {
	if x > y {
		return x - y
	} else {
		return y - x
	}
}

func BITAND(x, y int64) int64 {
	var ans int64
	begin := x
	sec := y
	var count int64 = 1
	for 1 == 1 {
		if (begin%2)*(sec%2) == 1 {
			ans = ans + count
			count = count * 2
			begin = int64(begin / 2)
			sec = int64(sec / 2)
			if begin*sec == 0 {
				break
			}

		} else {
			count = count * 2
			begin = int64(begin / 2)
			sec = int64(sec / 2)
			if begin*sec == 0 {
				break
			}
		}
	}
	return ans
}

func Sum(n, m int) int {
	var ans int

	if n == 1 {
		return 0
	} else {
		for i := 2; i < n; i++ {
			ans = (ans + Sum(i, m)) % m
		}
		for j := 2; j < n+1; j++ {
			ans = (ans + Sum(n/j, m)) % m
		}
		return ans
	}
}

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &t)
	for o := 0; o < t; o++ {
		var n, s int
		fmt.Fscan(input, &n, &s)

		// ARRAY := make([]int, n)

		// STRING := make([]byte, 0)
		// fmt.Fscan(input, &STRING)

		// for i := 0; i < n; i++ {
		// 	fmt.Fscan(input, &ARRAY[i])
		// }

		if n%2 == 0 {
			k := (n / 2) + 1
			q := s / k
			fmt.Println(q)

		} else {
			k := (n / 2) + 1
			q := s / k
			fmt.Println(q)
		}

	}
}

package main

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

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(n, s int64) int64 {
	if n == 1 {
		return s
	}

	return s / (n/2 + 1)
}

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

	for i := 0; i < tests; i++ {
		n, s := stdin.Int64(), stdin.Int64()
		fmt.Println(solve(n, s))
	}
}

package main

import (
	"bufio"
	"math"
	"os"
	"strconv"
)

func solve() {
	n := get()
	s := get()

	l, r := 0, s+1
	for r-l > 1 {
		m := (l + r) / 2
		if m*(n/2+1) <= s {
			l = m
			continue
		}
		r = m
	}

	putln(l)
}

const (
	once       = false
	maxBufSize = 10000000
)

var (
	splitFunc = bufio.ScanWords
)

func main() {
	in = bufio.NewScanner(bufio.NewReader(os.Stdin))
	in.Split(splitFunc)
	in.Buffer(make([]byte, maxBufSize), maxBufSize)

	out = bufio.NewWriter(os.Stdout)
	defer out.Flush()

	if once {
		solve()
		return
	}

	t := get()
	for t > 0 {
		solve()
		t--
	}
}

var (
	in  *bufio.Scanner
	out *bufio.Writer
)

func get() int {
	x, _ := strconv.Atoi(gets())
	return x
}

func geta(n int) []int {
	a := make([]int, n)
	for i := range a {
		a[i] = get()
	}
	return a
}

func geta1(n int) []int {
	a := make([]int, n)
	for i := range a {
		a[i] = get() - 1
	}
	return a
}

func get64() int64 {
	x, _ := strconv.ParseInt(gets(), 10, 64)
	return x
}

func get64a(n int) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = get64()
	}
	return a
}

func get64a1(n int) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = get64() - 1
	}
	return a
}

func getf() float64 {
	x, _ := strconv.ParseFloat(gets(), 64)
	return x
}

func getfa(n int) []float64 {
	a := make([]float64, n)
	for i := range a {
		a[i] = getf()
	}
	return a
}

func gets() string {
	scan()
	return in.Text()
}

func scanN(n int) {
	for n > 0 {
		scan()
		n--
	}
}

func scan() {
	in.Scan()
}

func putc(c rune) {
	out.WriteRune(c)
}

func put(x int) {
	out.WriteString(strconv.Itoa(x))
}

func putln(i int) {
	put(i)
	line()
}

func puta(a []int) {
	for _, v := range a {
		put(v)
		space()
	}
	line()
}

func put64(x int64) {
	out.WriteString(strconv.FormatInt(x, 10))
}

func put64ln(x int64) {
	put64(x)
	line()
}

func put64a(a []int64) {
	for _, v := range a {
		put64(v)
		space()
	}
	line()
}

func puts(s string) {
	out.WriteString(s)
}

func putsln(s string) {
	puts(s)
	line()
}

func yn(y bool) {
	if y {
		yes()
		return
	}
	no()
}

func yes() {
	puts("YES\n")
}

func no() {
	puts("NO\n")
}

func putf(x float64) {
	out.WriteString(strconv.FormatFloat(x, 'f', -1, 64))
}

func putfln(x float64) {
	putf(x)
	line()
}

func putfa(a []float64) {
	for _, v := range a {
		putf(v)
		space()
	}
	line()
}

func line() {
	out.WriteRune('\n')
}

func space() {
	out.WriteRune(' ')
}

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

func abs64(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

func absf(x float64) float64 {
	return math.Abs(x)
}

func max(x int, y int) int {
	if x > y {
		return x
	}
	return y
}

func max64(x int64, y int64) int64 {
	if x > y {
		return x
	}
	return y
}

func maxf(x float64, y float64) float64 {
	return math.Max(x, y)
}

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

func min64(x int64, y int64) int64 {
	if x < y {
		return x
	}
	return y
}

func minf(x float64, y float64) float64 {
	return math.Min(x, y)
}

func L(i int) int64 {
	return int64(i)
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var t, n, s int
	fmt.Scan(&t)

	for i := 0; i < t; i++ {
		fmt.Scan(&n, &s)
		cells := int(math.Ceil(float64(n) / 2.0))
		if n%2 == 0 {
			cells++
		}

		fmt.Println("fin", s/cells)
	}
}


Each employee of the "Blake Techologies" company uses a special messaging app "Blake Messenger". All the stuff likes this app and uses it constantly. However, some important futures are missing. For example, many users want to be able to search through the message history. It was already announced that the new feature will appear in the nearest update, when developers faced some troubles that only you may help them to solve.All the messages are represented as a strings consisting of only lowercase English letters. In order to reduce the network load strings are represented in the special compressed form. Compression algorithm works as follows: string is represented as a concatenation of n blocks, each block containing only equal characters. One block may be described as a pair (li, ci), where li is the length of the i-th block and ci is the corresponding letter. Thus, the string s may be written as the sequence of pairs .Your task is to write the program, that given two compressed string t and s finds all occurrences of s in t. Developers know that there may be many such occurrences, so they only ask you to find the number of them. Note that p is the starting position of some occurrence of s in t if and only if tptp + 1...tp + |s| - 1 = s, where ti is the i-th character of string t.Note that the way to represent the string in compressed form may not be unique. For example string "aaaa" may be given as , , ...

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of blocks in the strings t and s, respectively. The second line contains the descriptions of n parts of string t in the format "li-ci" (1 ≤ li ≤ 1 000 000) — the length of the i-th part and the corresponding lowercase English letter. The second line contains the descriptions of m parts of string s in the format "li-ci" (1 ≤ li ≤ 1 000 000) — the length of the i-th part and the corresponding lowercase English letter.

Print a single integer — the number of occurrences of s in t.

NoteIn the first sample, t = "aaabbccccaaacc", and string s = "aabbc". The only occurrence of string s in string t starts at position p = 2.In the second sample, t = "aaabbbbbbaaaaaaacccceeeeeeeeaa", and s = "aaa". The occurrences of s in t start at positions p = 1, p = 10, p = 11, p = 12, p = 13 and p = 14.

package main

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

var debug bool = false
var debugCounter int = 0
var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func Debug(a ...interface{}) {
	if !debug {
		return
	}
	debugCounter++
	fmt.Fprint(writer, debugCounter, " ")
	fmt.Fprintln(writer, a...)
	writer.Flush()
}

type Unit struct {
	char  byte
	count int64
}

func (this *Unit) Equals(that *Unit) bool {
	return this.char == that.char && this.count == that.count
}

func (this *Unit) Contains(that *Unit) int64 {
	if this.char != that.char || this.count < that.count {
		return 0
	}
	return this.count - that.count + 1
}

func Unify(unit []Unit) []Unit {
	last := 0
	for i := 1; i < len(unit); i++ {
		if unit[i].char == unit[i-1].char {
			unit[last].count += unit[i].count
		} else {
			last++
			unit[last] = unit[i]
		}
	}
	return unit[:last+1]
}

type MatchCounter struct {
	text, pattern []Unit
}

func (this *MatchCounter) generatePi() []int {
	var n int = len(this.pattern)
	pi := make([]int, n)
	for i, k := 2, 0; i < n-1; i++ {
		for k > 0 && !this.pattern[i].Equals(&this.pattern[k+1]) {
			k = pi[k+1]
		}
		if this.pattern[i].Equals(&this.pattern[k+1]) {
			k++
		}
		pi[i] = k
	}
	return pi
}

func (this *MatchCounter) CountMatches() int64 {
	var m, n int = len(this.text), len(this.pattern)
	if m < n {
		return 0
	}

	Debug("MATCH START")

	occurrences := int64(0)
	if n <= 2 {
		for i := 0; i < m; i++ {
			if n == 1 {
				occurrences += this.text[i].Contains(&this.pattern[0])
				continue
			}
			if i == m-1 {
				continue
			}
			if this.text[i].Contains(&this.pattern[0]) > 0 && this.text[i+1].Contains(&this.pattern[1]) > 0 {
				occurrences++
			}
		}
		Debug("CASE 1")
		return occurrences
	}

	pi := this.generatePi()
	Debug("PI OK")

	for i, j := 1, 1; i < m-1; i++ {
		for j > 1 && !this.text[i].Equals(&this.pattern[j]) {
			j = pi[j] + 1
		}
		if j < n-1 && this.text[i].Equals(&this.pattern[j]) {
			j++
		}
		if j == n-1 {
			Debug("FOUND", j, m, n, i+1, n-1, i-n+2)
			if this.text[i+1].Contains(&this.pattern[n-1]) > 0 && this.text[i-n+2].Contains(&this.pattern[0]) > 0 {
				occurrences++
			}
			j = pi[j-1] + 1
			Debug("PROCESSED", j, occurrences)
		}
	}
	return occurrences
}

func readUnits(units []Unit) {
	var tmp string
	n := len(units)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &units[i].count, &tmp)
		units[i].char = tmp[1]
	}
}

func main() {
	var m, n int

	fmt.Fscan(reader, &m, &n)
	if m == 200000 && n == 100000 {
		// debug = true
	}
	// debug = true

	text := make([]Unit, m)
	pattern := make([]Unit, n)
	readUnits(text)
	readUnits(pattern)
	Debug("READ OK")

	text = Unify(text)
	pattern = Unify(pattern)
	Debug("UNIFY OK")

	matcher := MatchCounter{text, pattern}
	Debug("INIT OK")

	fmt.Fprintln(writer, matcher.CountMatches())
	writer.Flush()
}

package main

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

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

func read(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

func write(a ...interface{}) {
	fmt.Fprint(writer, a...)
}

type unit struct {
	char  byte
	count int64
}

func (unt *unit) contains(that *unit) int64 {
	if unt.char != that.char || unt.count < that.count {
		return 0
	}
	return unt.count - that.count + 1
}

func unify(unit []unit) []unit {
	last := 0
	for i := 1; i < len(unit); i++ {
		if unit[i].char == unit[i-1].char {
			unit[last].count += unit[i].count
		} else {
			last++
			unit[last] = unit[i]
		}
	}
	return unit[:last+1]
}

type matcher struct {
	text, pattern []unit
}

func (mcr *matcher) generatePi() []int {
	var n = len(mcr.pattern)
	pi := make([]int, n)
	for i, k := 2, 0; i < n-1; i++ {
		for k > 0 && mcr.pattern[i].contains(&mcr.pattern[k+1]) != 1 {
			k = pi[k+1]
		}
		if mcr.pattern[i].contains(&mcr.pattern[k+1]) == 1 {
			k++
		}
		pi[i] = k
	}
	return pi
}

func (mcr *matcher) containsIds(i, j int) int64 {
	return mcr.text[i].contains(&mcr.pattern[j])
}

func (mcr *matcher) countMatches() int64 {
	var m, n int = len(mcr.text), len(mcr.pattern)
	if m < n {
		return 0
	}

	occurrences := int64(0)
	if n <= 2 {
		for i := 0; i < m; i++ {
			if n == 1 {
				occurrences += mcr.containsIds(i, 0)
				continue
			}
			if i == m-1 {
				continue
			}
			if mcr.containsIds(i, 0) > 0 && mcr.containsIds(i+1, 1) > 0 {
				occurrences++
			}
		}
		return occurrences
	}

	pi := mcr.generatePi()

	for i, j := 1, 1; i < m-1; i++ {
		for j > 1 && mcr.containsIds(i, j) != 1 {
			j = pi[j] + 1
		}
		if j < n-1 && mcr.containsIds(i, j) == 1 {
			j++
		}
		if j == n-1 {
			if mcr.containsIds(i+1, n-1) > 0 && mcr.containsIds(i-n+2, 0) > 0 {
				occurrences++
			}
			j = pi[j-1] + 1
		}
	}
	return occurrences
}

func readUnits(units []unit) {
	var tmp string
	n := len(units)
	for i := 0; i < n; i++ {
		read(&units[i].count, &tmp)
		units[i].char = tmp[1]
	}
}

func main() {
	defer writer.Flush()

	var m, n int
	read(&m, &n)

	text := make([]unit, m)
	pattern := make([]unit, n)

	readUnits(text)
	readUnits(pattern)
	text = unify(text)
	pattern = unify(pattern)

	matcher := matcher{text, pattern}

	write(matcher.countMatches())
}

package main

import (
	"fmt"
)

type Unit struct {
	char  byte
	count int
}

func (this *Unit) Equals(that *Unit) bool {
	return this.char == that.char && this.count == that.count
}

func (this *Unit) Contains(that *Unit) int {
	if this.char != that.char {
		return 0
	}
	return this.count - that.count + 1
}

func Unify(unit []Unit) []Unit {
	last := 0
	for i := 1; i < len(unit); i++ {
		if unit[i].char == unit[i-1].char {
			unit[last].count += unit[i].count
		} else {
			last++
			unit[last] = unit[i]
		}
	}
	return unit[:last+1]
}

type MatchCounter struct {
	text, pattern []Unit
}

func (this *MatchCounter) generatePi() []int {
	var n int = len(this.pattern)
	pi := make([]int, n)
	for i, k := 1, 2; i < n-1; i++ {
		for k > 0 && !this.pattern[i].Equals(&this.pattern[k]) {
			k = pi[k]
		}
		if this.pattern[i].Equals(&this.pattern[k]) {
			k++
		}
		pi[i] = k
	}
	return pi
}

func (this *MatchCounter) CountMatches() int64 {
	var m, n int = len(this.text), len(this.pattern)

	occurrences := int64(0)
	if n == 1 {
		for _, unit := range this.text {
			occurrences += int64(unit.Contains(&this.pattern[0]))
		}
		return occurrences
	}

	pi := this.generatePi()

	for i, j := 0, 1; i < m-1; i++ {
		for j > 1 && !this.text[i].Equals(&this.pattern[j]) {
			j = pi[j] + 1
		}
		if j < n-1 && this.text[i].Equals(&this.pattern[j]) {
			j++
		}
		if j == n-1 {
			if this.text[i+1].Contains(&this.pattern[n-1]) > 0 && this.text[i-n+2].Contains(&this.pattern[0]) > 0 {
				occurrences++
			}
			j = pi[j] + 1
		}
	}
	return occurrences
}

func main() {
	var m, n int

	fmt.Scan(&m, &n)
	text := make([]Unit, m)
	pattern := make([]Unit, n)

	var tmp string

	for i := 0; i < m; i++ {
		fmt.Scan(&text[i].count, &tmp)
		text[i].char = tmp[1]
	}

	for i := 0; i < n; i++ {
		fmt.Scan(&pattern[i].count, &tmp)
		pattern[i].char = tmp[1]
	}

	text = Unify(text)
	pattern = Unify(pattern)

	matcher := MatchCounter{text, pattern}

	fmt.Println(matcher.CountMatches())
}

package main

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

var debug bool = false
var debugCounter int = 0
var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func Debug(a ...interface{}) {
	if !debug {
		return
	}
	debugCounter++
	fmt.Fprint(writer, debugCounter, " ")
	fmt.Fprintln(writer, a...)
	writer.Flush()
}

type Unit struct {
	char  byte
	count int
}

func (this *Unit) Equals(that *Unit) bool {
	return this.char == that.char && this.count == that.count
}

func (this *Unit) Contains(that *Unit) int {
	if this.char != that.char || this.count < that.count {
		return 0
	}
	return this.count - that.count + 1
}

func Unify(unit []Unit) []Unit {
	last := 0
	for i := 1; i < len(unit); i++ {
		if unit[i].char == unit[i-1].char {
			unit[last].count += unit[i].count
		} else {
			last++
			unit[last] = unit[i]
		}
	}
	return unit[:last+1]
}

type MatchCounter struct {
	text, pattern []Unit
}

func (this *MatchCounter) generatePi() []int {
	var n int = len(this.pattern)
	pi := make([]int, n)
	for i, k := 2, 0; i < n-1; i++ {
		for k > 0 && !this.pattern[i].Equals(&this.pattern[k+1]) {
			k = pi[k+1]
		}
		if this.pattern[i].Equals(&this.pattern[k+1]) {
			k++
		}
		pi[i] = k
	}
	return pi
}

func (this *MatchCounter) CountMatches() int64 {
	var m, n int = len(this.text), len(this.pattern)
	if m < n {
		return 0
	}

	Debug("MATCH START")

	occurrences := int64(0)
	if n <= 2 {
		for i := 0; i < m; i++ {
			if n == 1 {
				occurrences += int64(this.text[i].Contains(&this.pattern[0]))
				continue
			}
			if i == m-1 {
				continue
			}
			if this.text[i].Contains(&this.pattern[0]) > 0 && this.text[i+1].Contains(&this.pattern[1]) > 0 {
				occurrences++
			}
		}
		Debug("CASE 1")
		return occurrences
	}

	pi := this.generatePi()
	Debug("PI OK")

	for i, j := 1, 1; i < m-1; i++ {
		for j > 1 && !this.text[i].Equals(&this.pattern[j]) {
			j = pi[j] + 1
		}
		if j < n-1 && this.text[i].Equals(&this.pattern[j]) {
			j++
		}
		if j == n-1 {
			Debug("FOUND", j, m, n, i+1, n-1, i-n+2)
			if this.text[i+1].Contains(&this.pattern[n-1]) > 0 && this.text[i-n+2].Contains(&this.pattern[0]) > 0 {
				occurrences++
			}
			j = pi[j-1] + 1
			Debug("PROCESSED", j, occurrences)
		}
	}
	return occurrences
}

func readUnits(units []Unit) {
	var tmp string
	n := len(units)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &units[i].count, &tmp)
		units[i].char = tmp[1]
	}
}

func main() {
	var m, n int

	fmt.Fscan(reader, &m, &n)
	if m == 200000 && n == 100000 {
		// debug = true
	}
	// debug = true

	text := make([]Unit, m)
	pattern := make([]Unit, n)
	readUnits(text)
	readUnits(pattern)
	Debug("READ OK")

	text = Unify(text)
	pattern = Unify(pattern)
	Debug("UNIFY OK")

	matcher := MatchCounter{text, pattern}
	Debug("INIT OK")

	fmt.Fprintln(writer, matcher.CountMatches())
	writer.Flush()
}

package main

import (
	"fmt"
)

type Unit struct {
	char  byte
	count int
}

func (this *Unit) Equals(that *Unit) bool {
	return this.char == that.char && this.count == that.count
}

func (this *Unit) Contains(that *Unit) int {
	if this.char != that.char {
		return 0
	}
	return this.count - that.count + 1
}

func Unify(unit []Unit) []Unit {
	last := 0
	for i := 1; i < len(unit); i++ {
		if unit[i].char == unit[i-1].char {
			unit[last].count += unit[i].count
		} else {
			last++
			unit[last] = unit[i]
		}
	}
	return unit[:last+1]
}

type MatchCounter struct {
	text, pattern []Unit
}

func (this *MatchCounter) generatePi() []int {
	var n int = len(this.pattern)
	pi := make([]int, n)
	for i, k := 1, 2; i < n-1; i++ {
		for k > 0 && !this.pattern[i].Equals(&this.pattern[k]) {
			k = pi[k]
		}
		if this.pattern[i].Equals(&this.pattern[k]) {
			k++
		}
		pi[i] = k
	}
	return pi
}

func (this *MatchCounter) CountMatches() int {
	var m, n int = len(this.text), len(this.pattern)

	occurrences := 0
	if n == 1 {
		for _, unit := range this.text {
			occurrences += unit.Contains(&this.pattern[0])
		}
		return occurrences
	}

	pi := this.generatePi()

	for i, j := 0, 1; i < m-1; i++ {
		for j > 1 && !this.text[i].Equals(&this.pattern[j]) {
			j = pi[j] + 1
		}
		if j < n-1 && this.text[i].Equals(&this.pattern[j]) {
			j++
		}
		if j == n-1 {
			if this.text[i+1].Contains(&this.pattern[n-1]) > 0 && this.text[i-n+2].Contains(&this.pattern[0]) > 0 {
				occurrences++
			}
			j = pi[j] + 1
		}
	}
	return occurrences
}

func main() {
	var m, n int

	fmt.Scan(&m, &n)
	text := make([]Unit, m)
	pattern := make([]Unit, n)

	var tmp string

	for i := 0; i < m; i++ {
		fmt.Scan(&text[i].count, &tmp)
		text[i].char = tmp[1]
	}

	for i := 0; i < n; i++ {
		fmt.Scan(&pattern[i].count, &tmp)
		pattern[i].char = tmp[1]
	}

	text = Unify(text)
	pattern = Unify(pattern)

	matcher := MatchCounter{text, pattern}

	fmt.Println(matcher.CountMatches())
}

package main

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

var debug bool = false
var debugCounter int = 0
var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func Debug(a ...interface{}) {
	if !debug {
		return
	}
	debugCounter++
	fmt.Fprint(writer, debugCounter, " ")
	fmt.Fprintln(writer, a...)
	writer.Flush()
}

type Unit struct {
	char  byte
	count int
}

func (this *Unit) Equals(that *Unit) bool {
	return this.char == that.char && this.count == that.count
}

func (this *Unit) Contains(that *Unit) int {
	if this.char != that.char || this.count < that.count {
		return 0
	}
	return this.count - that.count + 1
}

func Unify(unit []Unit) []Unit {
	last := 0
	for i := 1; i < len(unit); i++ {
		if unit[i].char == unit[i-1].char {
			unit[last].count += unit[i].count
		} else {
			last++
			unit[last] = unit[i]
		}
	}
	return unit[:last+1]
}

type MatchCounter struct {
	text, pattern []Unit
}

func (this *MatchCounter) generatePi() []int {
	var n int = len(this.pattern)
	pi := make([]int, n)
	for i, k := 2, 0; i < n-1; i++ {
		for k > 0 && !this.pattern[i].Equals(&this.pattern[k+1]) {
			k = pi[k+1]
		}
		if this.pattern[i].Equals(&this.pattern[k+1]) {
			k++
		}
		pi[i] = k
	}
	return pi
}

func (this *MatchCounter) CountMatches() int64 {
	var m, n int = len(this.text), len(this.pattern)
	if m < n {
		return 0
	}

	Debug("MATCH START")

	occurrences := int64(0)
	if n == 1 {
		for _, unit := range this.text {
			occurrences += int64(unit.Contains(&this.pattern[0]))
		}
		Debug("CASE 1")
		return occurrences
	}

	pi := this.generatePi()
	Debug("PI OK")

	for i, j := 1, 1; i < m-1; i++ {
		for j > 1 && !this.text[i].Equals(&this.pattern[j]) {
			j = pi[j] + 1
		}
		if j < n-1 && this.text[i].Equals(&this.pattern[j]) {
			j++
		}
		if j == n-1 {
			Debug("FOUND", j, m, n, i+1, n-1, i-n+2)
			if this.text[i+1].Contains(&this.pattern[n-1]) > 0 && this.text[i-n+2].Contains(&this.pattern[0]) > 0 {
				occurrences++
			}
			j = pi[j-1] + 1
			Debug("PROCESSED", j)
		}
	}
	return occurrences
}

func readUnits(units []Unit) {
	var tmp string
	n := len(units)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &units[i].count, &tmp)
		units[i].char = tmp[1]
	}
}

func main() {
	var m, n int

	fmt.Fscan(reader, &m, &n)
	if m == 200000 && n == 100000 {
		// debug = true
	}

	text := make([]Unit, m)
	pattern := make([]Unit, n)
	readUnits(text)
	readUnits(pattern)
	Debug("READ OK")

	text = Unify(text)
	pattern = Unify(pattern)
	Debug("UNIFY OK")

	matcher := MatchCounter{text, pattern}
	Debug("INIT OK")

	fmt.Fprintln(writer, matcher.CountMatches())
	writer.Flush()
}