Salem gave you $$$n$$$ sticks with integer positive lengths $$$a_1, a_2, \ldots, a_n$$$.For every stick, you can change its length to any other positive integer length (that is, either shrink or stretch it). The cost of changing the stick's length from $$$a$$$ to $$$b$$$ is $$$|a - b|$$$, where $$$|x|$$$ means the absolute value of $$$x$$$.A stick length $$$a_i$$$ is called almost good for some integer $$$t$$$ if $$$|a_i - t| \le 1$$$.Salem asks you to change the lengths of some sticks (possibly all or none), such that all sticks' lengths are almost good for some positive integer $$$t$$$ and the total cost of changing is minimum possible. The value of $$$t$$$ is not fixed in advance and you can choose it as any positive integer. As an answer, print the value of $$$t$$$ and the minimum cost. If there are multiple optimal choices for $$$t$$$, print any of them.

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of sticks. The second line contains $$$n$$$ integers $$$a_i$$$ ($$$1 \le a_i \le 100$$$) — the lengths of the sticks.

Print the value of $$$t$$$ and the minimum possible cost. If there are multiple optimal choices for $$$t$$$, print any of them.

NoteIn the first example, we can change $$$1$$$ into $$$2$$$ and $$$10$$$ into $$$4$$$ with cost $$$|1 - 2| + |10 - 4| = 1 + 6 = 7$$$ and the resulting lengths $$$[2, 4, 4]$$$ are almost good for $$$t = 3$$$.In the second example, the sticks lengths are already almost good for $$$t = 2$$$, so we don't have to do anything.

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	a := make([]int, n)
	for i := range a {
		fmt.Scan(&a[i])
	}
	ansT := 0
	ansCost := 99999999
	for t := 1; t <= 100; t++ {
		cost := 0
		for _, a := range a {
			if a < t {
				cost += t - a - 1
			}
			if a > t {
				cost += a - t - 1
			}
		}
		if ansCost > cost {
			ansCost = cost
			ansT = t
		}
	}
	fmt.Println(ansT, ansCost)
}

package main

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

func dis( t1, p int) int {
	if t1 >= p-1 && t1 <= p+1{
		return 0
	}
	if t1 <p-1{
		return p-1-t1
	}else{
		return t1-(p+1)
	}
}
func main() {
	buf := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscanf(buf, "%d\n", &n)
	mat := make([]int, n+1)

	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &mat[i])
	}
	ans := -1
	w := -1
	for b := 1; b <=100; b++ {
		tmp := 0
		for i := 0; i < n; i++ {
			tmp += dis(mat[i],b)

		}
		if ans == -1 || ans > tmp{
			ans = tmp
			w = b
		}
	}
	fmt.Printf("%d %d\n",w, ans)
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n := 0
	fmt.Fscanf(in, "%d\n", &n)
	a := []int{}
	for i := 0; i < n; i++ {
		t := 0
		fmt.Fscanf(in, "%d", &t)
		a = append(a, t)
	}
	bestT := 1
	best := 99999999
	for t := 1; t <= 100; t++ {
		cur := 0
		for i := 0; i < n; i++ {
			if a[i] > t {
				cur += a[i] - (t + 1)
			} else if a[i] == t {
			} else {
				cur += t - 1 - a[i]
			}
		}
		if cur < best {
			best = cur
			bestT = t
		}
	}
	fmt.Fprintf(out, "%d %d\n", bestT, best)
	return
}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }
func scanf(format string, a ...interface{}) { fmt.Fscanf(reader, format, a...) }

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

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

	v := make([]int, n)
	for i := range v {
		scanf("%d", &v[i])
	}

	res, x := int(1e8), 0
	for i := 1; i <= 100; i++ {
		s := 0
		for _, j := range v {
			if j > i {
				s += j - i - 1
			} else if j < i {
				s += i - j - 1
			}
		}
		if res > s {
			res = s
			x = i
		}
	}

	printf("%d %d", x, res)
}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }
func scanf(format string, a ...interface{}) { fmt.Fscanf(reader, format, a...) }

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

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

	v := make([]int, n)
	for i := range v {
		scanf("%d ", &v[i])
	}

	res, x := int(1e8), 0
	for i := 0; i <= 100; i++ {
		s := 0
		for _, j := range v {
			if j > i {
				s += j - i - 1
			} else if j < i {
				s += i - j - 1
			}
		}
		if res > s {
			res = s
			x = i
		}
	}

	printf("%d %d", x, res)
}


You are given a tree with $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$. The height of the $$$i$$$-th vertex is $$$h_i$$$. You can place any number of towers into vertices, for each tower you can choose which vertex to put it in, as well as choose its efficiency. Setting up a tower with efficiency $$$e$$$ costs $$$e$$$ coins, where $$$e > 0$$$.It is considered that a vertex $$$x$$$ gets a signal if for some pair of towers at the vertices $$$u$$$ and $$$v$$$ ($$$u \neq v$$$, but it is allowed that $$$x = u$$$ or $$$x = v$$$) with efficiencies $$$e_u$$$ and $$$e_v$$$, respectively, it is satisfied that $$$\min(e_u, e_v) \geq h_x$$$ and $$$x$$$ lies on the path between $$$u$$$ and $$$v$$$.Find the minimum number of coins required to set up towers so that you can get a signal at all vertices.

The first line contains an integer $$$n$$$ ($$$2 \le n \le 200\,000$$$) — the number of vertices in the tree. The second line contains $$$n$$$ integers $$$h_i$$$ ($$$1 \le h_i \le 10^9$$$) — the heights of the vertices. Each of the next $$$n - 1$$$ lines contain a pair of numbers $$$v_i, u_i$$$ ($$$1 \le v_i, u_i \le n$$$) — an edge of the tree. It is guaranteed that the given edges form a tree.

Print one integer — the minimum required number of coins.

NoteIn the first test case it's optimal to install two towers with efficiencies $$$2$$$ at vertices $$$1$$$ and $$$3$$$.In the second test case it's optimal to install a tower with efficiency $$$1$$$ at vertex $$$1$$$ and two towers with efficiencies $$$3$$$ at vertices $$$2$$$ and $$$5$$$.In the third test case it's optimal to install two towers with efficiencies $$$6$$$ at vertices $$$1$$$ and $$$2$$$.

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1637F(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, v, w, root, fi, se int
	Fscan(in, &n)
	h := make([]int, n)
	for i := range h {
		Fscan(in, &h[i])
		if h[i] > h[root] {
			root = 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)
	}
	ans := int64(0)
	var f func(int, int) int
	f = func(v, fa int) (max int) {
		for _, w := range g[v] {
			if w != fa {
				if mx := f(w, v); mx > max {
					max = mx
				}
			}
		}
		if h[v] > max {
			ans += int64(h[v] - max)
			max = h[v]
		}
		return
	}
	for _, w := range g[root] {
		if mx := f(w, root); mx > fi {
			fi, se = mx, fi
		} else if mx > se {
			se = mx
		}
	}
	Fprint(out, ans+int64(h[root]*2-fi-se))
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1637F(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, v, w, root, fi, se int
	Fscan(in, &n)
	h := make([]int, n)
	for i := range h {
		Fscan(in, &h[i])
		if h[i] > h[root] {
			root = 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)
	}
	ans := int64(0)
	var f func(int, int) int
	f = func(v, fa int) (mx int) {
		for _, w := range g[v] {
			if w != fa {
				mx = max(mx, f(w, v))
			}
		}
		if mx == 0 {
			ans += int64(h[v])
		} else if h[v] > mx {
			ans += int64(h[v] - mx)
		}
		return max(mx, h[v])
	}
	for _, w := range g[root] {
		if mx := f(w, root); mx > fi {
			fi, se = mx, fi
		} else if mx > se {
			se = mx
		}
	}
	Fprint(out, ans+int64(h[root]*2-fi-se))
}

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

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1637F(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, v, w, mxI, fi, se int
	Fscan(in, &n)
	h := make([]int, n)
	for i := range h {
		Fscan(in, &h[i])
		if h[i] > h[mxI] {
			mxI = 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)
	}
	ans := int64(0)
	var f func(int, int) int
	f = func(v, fa int) (mx int) {
		for _, w := range g[v] {
			if w != fa {
				mx = max(mx, f(w, v))
			}
		}
		if mx == 0 {
			ans += int64(h[v])
		} else if h[v] > mx {
			ans += int64(h[v] - mx)
		}
		return max(mx, h[v])
	}
	for _, w := range g[mxI] {
		if mx := f(w, mxI); mx > fi {
			fi, se = mx, fi
		} else if mx > se {
			se = mx
		}
	}
	Fprint(out, ans+int64(h[v]*2-fi-se))
}

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

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


Let T be arbitrary binary tree — tree, every vertex of which has no more than two children. Given tree is rooted, so there exists only one vertex which doesn't have a parent — it's the root of a tree. Every vertex has an integer number written on it. Following algorithm is run on every value from the tree T:  Set pointer to the root of a tree.  Return success if the value in the current vertex is equal to the number you are looking for  Go to the left child of the vertex if the value in the current vertex is greater than the number you are looking for  Go to the right child of the vertex if the value in the current vertex is less than the number you are looking for  Return fail if you try to go to the vertex that doesn't exist Here is the pseudo-code of the described algorithm: bool find(TreeNode t, int x) {    if (t == null)        return false;    if (t.value == x)        return true;    if (x < t.value)        return find(t.left, x);    else        return find(t.right, x);}find(root, x);The described algorithm works correctly if the tree is binary search tree (i.e. for each node the values of left subtree are less than the value in the node, the values of right subtree are greater than the value in the node). But it can return invalid result if tree is not a binary search tree.Since the given tree is not necessarily a binary search tree, not all numbers can be found this way. Your task is to calculate, how many times the search will fail being running on every value from the tree.If the tree has multiple vertices with the same values on them then you should run algorithm on every one of them separately.

First line contains integer number n (1 ≤ n ≤ 105) — number of vertices in the tree. Each of the next n lines contains 3 numbers v, l, r (0 ≤ v ≤ 109) — value on current vertex, index of the left child of the vertex and index of the right child of the vertex, respectively. If some child doesn't exist then number  - 1 is set instead. Note that different vertices of the tree may contain the same values.

Print number of times when search algorithm will fail.

NoteIn the example the root of the tree in vertex 2. Search of numbers 5 and 15 will return fail because on the first step algorithm will choose the subtree which doesn't contain numbers you are looking for.

package main

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

const (
	max = math.MaxInt32
	min = math.MinInt32
)

var goods map[int]bool
var all map[int]int

func main() {
	goods = map[int]bool{}
	all = map[int]int{}

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	nodes := make([]*node, n)
	notRoot := make([]bool, n)
	for i := 0; i < n; i++ {
		nodes[i] = &node{}
	}
	for i := 0; i < n; i++ {
		var v, l, r int
		scanner.Scan()
		v, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		l, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		r, _ = strconv.Atoi(scanner.Text())
		nodes[i].v = v
		all[v]++
		if l != -1 {
			nodes[i].l = nodes[l-1]
			notRoot[l-1] = true
		}
		if r != -1 {
			nodes[i].r = nodes[r-1]
			notRoot[r-1] = true
		}
	}
	var root *node
	for i, b := range notRoot {
		if !b {
			root = nodes[i]
			break
		}
	}
	dfs(root, min, max)
	total := 0

	for k := range all {
		if !goods[k] {
			total += all[k]
		}
	}
	fmt.Println(total)
}

type node struct {
	v int
	l *node
	r *node
}

func _min(x, y int) int {
	if x < y {
		return x
	}
	return y
}
func _max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func dfs(cur *node, min, max int) {
	if cur == nil {
		return
	}
	if cur.v >= min && cur.v <= max {
		goods[cur.v] = true
	}
	dfs(cur.l, min, _min(cur.v, max))
	dfs(cur.r, _max(cur.v, min), max)
}

package main

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

const (
	max = math.MaxInt32
	min = math.MinInt32
)

var total = 0

func main() {

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	nodes := make([]*node, n)
	notRoot := make([]bool, n)
	for i := 0; i < n; i++ {
		nodes[i] = &node{}
	}
	for i := 0; i < n; i++ {
		var v, l, r int
		scanner.Scan()
		v, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		l, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		r, _ = strconv.Atoi(scanner.Text())
		nodes[i].v = v
		if l != -1 {
			nodes[i].l = nodes[l-1]
			notRoot[l-1] = true
		}
		if r != -1 {
			nodes[i].r = nodes[r-1]
			notRoot[r-1] = true
		}
	}
	var root *node
	for i, b := range notRoot {
		if !b {
			root = nodes[i]
			break
		}
	}
	dfs(root, min, max)
	fmt.Println(n - total)
}

type node struct {
	v int
	l *node
	r *node
}

func _min(x, y int) int {
	if x < y {
		return x
	}
	return y
}
func _max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func dfs(cur *node, min, max int) {
	if cur == nil {
		return
	}
	if cur.v > min && cur.v < max {
		total++
	}
	dfs(cur.l, min, _min(cur.v, max))
	dfs(cur.r, _max(cur.v, min), max)
}

package main

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

const (
	max = math.MaxInt32
	min = math.MinInt32
)

var total = 0

func main() {

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	nodes := make([]*node, n)
	notRoot := make([]bool, n)
	for i := 0; i < n; i++ {
		nodes[i] = &node{}
	}
	for i := 0; i < n; i++ {
		var v, l, r int
		scanner.Scan()
		v, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		l, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		r, _ = strconv.Atoi(scanner.Text())
		nodes[i].v = v
		if l != -1 {
			nodes[i].l = nodes[l-1]
			notRoot[l-1] = true
		}
		if r != -1 {
			nodes[i].r = nodes[r-1]
			notRoot[r-1] = true
		}
	}
	var root *node
	for i, b := range notRoot {
		if !b {
			root = nodes[i]
			break
		}
	}
	dfs(root, min, max)
	fmt.Println(n - total)
}

type node struct {
	v int
	l *node
	r *node
}

func _min(x, y int) int {
	if x < y {
		return x
	}
	return y
}
func _max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func dfs(cur *node, min, max int) {
	if cur == nil {
		return
	}
	if cur.v >= min && cur.v <= max {
		total++
	}
	dfs(cur.l, min, _min(cur.v, max))
	dfs(cur.r, _max(cur.v, min), max)
}

package main

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

const (
	max = math.MaxInt32
	min = math.MinInt32
)

var goods map[int]bool
var all map[int]bool

func main() {
	goods = map[int]bool{}
	all = map[int]bool{}

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	nodes := make([]*node, n)
	notRoot := make([]bool, n)
	for i := 0; i < n; i++ {
		nodes[i] = &node{}
	}
	for i := 0; i < n; i++ {
		var v, l, r int
		scanner.Scan()
		v, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		l, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		r, _ = strconv.Atoi(scanner.Text())
		nodes[i].v = v
		all[v] = true
		if l != -1 {
			nodes[i].l = nodes[l-1]
			notRoot[l-1] = true
		}
		if r != -1 {
			nodes[i].r = nodes[r-1]
			notRoot[r-1] = true
		}
	}
	var root *node
	for i, b := range notRoot {
		if !b {
			root = nodes[i]
			break
		}
	}
	dfs(root, min, max)
	total := 0

	for k := range all {
		if !goods[k] {
			total++
		}
	}
	fmt.Println(total)
}

type node struct {
	v int
	l *node
	r *node
}

func _min(x, y int) int {
	if x < y {
		return x
	}
	return y
}
func _max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func dfs(cur *node, min, max int) {
	if cur == nil {
		return
	}
	if cur.v > min && cur.v < max {
		goods[cur.v] = true
	}
	dfs(cur.l, min, _min(cur.v, max))
	dfs(cur.r, _max(cur.v, min), max)
}

package main

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

const (
	max = math.MaxInt32
	min = math.MinInt32
)

var goods map[int]bool
var all map[int]bool

func main() {
	goods = map[int]bool{}
	all = map[int]bool{}

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	nodes := make([]*node, n)
	notRoot := make([]bool, n)
	for i := 0; i < n; i++ {
		nodes[i] = &node{}
	}
	for i := 0; i < n; i++ {
		var v, l, r int
		scanner.Scan()
		v, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		l, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		r, _ = strconv.Atoi(scanner.Text())
		nodes[i].v = v
		all[v] = true
		if l != -1 {
			nodes[i].l = nodes[l-1]
			notRoot[l-1] = true
		}
		if r != -1 {
			nodes[i].r = nodes[r-1]
			notRoot[r-1] = true
		}
	}
	var root *node
	for i, b := range notRoot {
		if !b {
			root = nodes[i]
			break
		}
	}
	dfs(root, min, max)
	total := 0

	for k := range all {
		if !goods[k] {
			total++
		}
	}
	fmt.Println(total)
}

type node struct {
	v int
	l *node
	r *node
}

func _min(x, y int) int {
	if x < y {
		return x
	}
	return y
}
func _max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func dfs(cur *node, min, max int) {
	if cur == nil {
		return
	}
	if cur.v >= min && cur.v <= max {
		goods[cur.v] = true
	}
	dfs(cur.l, min, _min(cur.v, max))
	dfs(cur.r, _max(cur.v, min), max)
}


You're creating a game level for some mobile game. The level should contain some number of cells aligned in a row from left to right and numbered with consecutive integers starting from $$$1$$$, and in each cell you can either put a platform or leave it empty.In order to pass a level, a player must throw a ball from the left so that it first lands on a platform in the cell $$$p$$$, then bounces off it, then bounces off a platform in the cell $$$(p + k)$$$, then a platform in the cell $$$(p + 2k)$$$, and so on every $$$k$$$-th platform until it goes farther than the last cell. If any of these cells has no platform, you can't pass the level with these $$$p$$$ and $$$k$$$.You already have some level pattern $$$a_1$$$, $$$a_2$$$, $$$a_3$$$, ..., $$$a_n$$$, where $$$a_i = 0$$$ means there is no platform in the cell $$$i$$$, and $$$a_i = 1$$$ means there is one. You want to modify it so that the level can be passed with given $$$p$$$ and $$$k$$$. In $$$x$$$ seconds you can add a platform in some empty cell. In $$$y$$$ seconds you can remove the first cell completely, reducing the number of cells by one, and renumerating the other cells keeping their order. You can't do any other operation. You can not reduce the number of cells to less than $$$p$$$.  Illustration for the third example test case. Crosses mark deleted cells. Blue platform is the newly added. What is the minimum number of seconds you need to make this level passable with given $$$p$$$ and $$$k$$$?

The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of test cases follows. The first line of each test case contains three integers $$$n$$$, $$$p$$$, and $$$k$$$ ($$$1 \le p \le n \le 10^5$$$, $$$1 \le k \le n$$$) — the number of cells you have, the first cell that should contain a platform, and the period of ball bouncing required. The second line of each test case contains a string $$$a_1 a_2 a_3 \ldots a_n$$$ ($$$a_i = 0$$$ or $$$a_i = 1$$$) — the initial pattern written without spaces. The last line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le 10^4$$$) — the time required to add a platform and to remove the first cell correspondingly. The sum of $$$n$$$ over test cases does not exceed $$$10^5$$$.

For each test case output a single integer — the minimum number of seconds you need to modify the level accordingly. It can be shown that it is always possible to make the level passable.

NoteIn the first test case it's best to just remove the first cell, after that all required platforms are in their places: 0101010101. The stroked out digit is removed, the bold ones are where platforms should be located. The time required is $$$y = 2$$$.In the second test case it's best to add a platform to both cells $$$4$$$ and $$$5$$$: 00000 $$$\to$$$ 00011. The time required is $$$x \cdot 2 = 4$$$.In the third test case it's best to to remove the first cell twice and then add a platform to the cell which was initially $$$10$$$-th: 10110011000 $$$\to$$$ 10110011010. The time required is $$$y \cdot 2 + x = 10$$$.

package main

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

func main() {
	defer _w.Flush()
	t := ReadInt()
	for i := 0; i < t; i++ {
		println(solve())
	}
}

func solve() int {
	n, p, k := ReadInt(), ReadInt(), ReadInt()
	a := ReadString()
	x, y := ReadInt(), ReadInt()

	v := make([]int, n)
	for i := n - 1; i >= 0; i-- {
		if a[i] == '0' {
			v[i] += x
		}
		if i+k < n {
			v[i] += v[i+k]
		}
	}

	ans := 1 << 29
	c := 0
	for i := p - 1; i < n; i++ {
		ans = Min(ans, v[i]+y*c)
		c++
	}
	return ans
}

func Min(xs ...int) int {
	min := xs[0]
	for _, x := range xs[1:] {
		if min > x {
			min = x
		}
	}
	return min
}

var _r, _w = bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)

func print(a ...interface{})            { fmt.Fprint(_w, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(_w, f, a...) }
func println(a ...interface{})          { fmt.Fprintln(_w, a...) }
func printInts(a ...int)                { s := fmt.Sprint(a); println(s[1 : len(s)-1]) }
func Scan(a ...interface{}) {
	if _, err := fmt.Fscan(_r, a...); err != nil {
		panic(err)
	}
}
func ReadString() (s string) { Scan(&s); return }
func ReadInt() (i int)       { Scan(&i); return }
func ReadInts(n int) (a []int) {
	for i := 0; i < n; i++ {
		a = append(a, ReadInt())
	}
	return
}
func ReadInt64() (i int64) { Scan(&i); return }
func ReadInt64s(n int) (a []int64) {
	for i := 0; i < n; i++ {
		a = append(a, ReadInt64())
	}
	return
}

/*
URL:
https://codeforces.com/contest/1457/problem/C
*/

package main

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

var (
	t int

	n, p, k int
	S       []rune
	x, y    int
)

func main() {
	defer stdout.Flush()

	t = readi()
	for tc := 0; tc < t; tc++ {
		n, p, k = readi3()
		p--
		S = readrs()
		x, y = readi2()

		solve()
	}
}

func solve() {
	ans := INF_B30

	sum := make([][]int, k)
	A := make([][]rune, k)

	for i := 0; i < n; i++ {
		m := i % k
		A[m] = append(A[m], S[i])
	}

	for i := 0; i < k; i++ {
		sum[i] = make([]int, len(A[i])+1)
	}

	// debugf("A: %v\n", A)
	// debugf("sum: %v\n", sum)

	for i := 0; i < k; i++ {
		for j := 0; j < len(A[i]); j++ {
			if A[i][j] == '0' {
				sum[i][j+1] = sum[i][j] + 1
			} else {
				sum[i][j+1] = sum[i][j]
			}
		}
	}

	for i := p; i < n; i++ {
		ynum := i - p

		m := i % k
		xnum := sum[m][len(A[m])] - sum[m][i/k]

		res := xnum*x + ynum*y
		chmin(&ans, res)
	}

	// iは始点
	// for i := p; i < p+k; i++ {
	// 	num := 0
	// 	for j := i; j < n; j += k {
	// 		if S[j] == '0' {
	// 			num++
	// 		}
	// 	}

	// 	res := num*x + (i-p)*y
	// 	chmin(&ans, res)
	// }

	printf("%d\n", ans)
}

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

/********** common constants **********/

const (
	MOD = 1000000000 + 7
	// MOD          = 998244353
	ALPH_N  = 26
	INF_I64 = math.MaxInt64
	INF_B60 = 1 << 60
	INF_I32 = math.MaxInt32
	INF_B30 = 1 << 30
	NIL     = -1
	EPS     = 1e-10
)

/********** bufio setting **********/

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

// mod can calculate a right residual whether value is positive or negative.
func mod(val, m int) int {
	res := val % m
	if res < 0 {
		res += m
	}
	return res
}

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

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

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

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

// sum returns multiple integers sum.
func sum(integers ...int) int {
	var s int
	s = 0

	for _, i := range integers {
		s += i
	}

	return s
}

// abs is integer version of math.Abs
func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

// pow is integer version of math.Pow
// pow calculate a power by Binary Power (二分累乗法(O(log e))).
func pow(a, e int) int {
	if a < 0 || e < 0 {
		panic(errors.New("[argument error]: PowInt does not accept negative integers"))
	}

	if e == 0 {
		return 1
	}

	if e%2 == 0 {
		halfE := e / 2
		half := pow(a, halfE)
		return half * half
	}

	return a * pow(a, e-1)
}

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

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

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

//str := reads()
//i := readi()
//X := readis(n)
//S := readrs()
//a := readf()
//A := readfs(n)

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

/*********** Input ***********/

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

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

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

// readi returns an integer.
func readi() int {
	return int(_readInt64())
}
func readi2() (int, int) {
	return int(_readInt64()), int(_readInt64())
}
func readi3() (int, int, int) {
	return int(_readInt64()), int(_readInt64()), int(_readInt64())
}
func readi4() (int, int, int, int) {
	return int(_readInt64()), int(_readInt64()), int(_readInt64()), int(_readInt64())
}

// readll returns as integer as int64.
func readll() int64 {
	return _readInt64()
}
func readll2() (int64, int64) {
	return _readInt64(), _readInt64()
}
func readll3() (int64, int64, int64) {
	return _readInt64(), _readInt64(), _readInt64()
}
func readll4() (int64, int64, int64, int64) {
	return _readInt64(), _readInt64(), _readInt64(), _readInt64()
}

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

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

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

// readf returns an float64.
func readf() float64 {
	return float64(_readFloat64())
}

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

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

// readrs returns a rune slice.
func readrs() []rune {
	return []rune(reads())
}

/*********** Output ***********/

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

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

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

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

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

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

	return res
}

package main

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

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

	var t, n, p, k, x, y int
	var s string
	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &p, &k)
		fmt.Fscan(in, &s)
		a := append([]rune{' '}, []rune(s)...) // add dumb first element for convenience
		fmt.Fscan(in, &x, &y)

		minTime := n * x // upper bound of minimal time
		for i := p; i < p+k && i <= n; i++ {
			offset := i - p
			avoidedMisses := 0
			misses := 0
			for j := i; j <= n; j += k {
				if misses*x-(j-p)*y > avoidedMisses*x-offset*y {
					avoidedMisses, offset = misses, j-p
				}
				if a[j] == '0' {
					misses++
				}
			}
			if (misses-avoidedMisses)*x+offset*y < minTime {
				minTime = (misses-avoidedMisses)*x + offset*y
			}
		}

		fmt.Println(minTime)
	}
}

/*
URL:
https://codeforces.com/contest/1457/problem/C
*/

package main

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

var (
	t int

	n, p, k int
	S       []rune
	x, y    int
)

func main() {
	defer stdout.Flush()

	t = readi()
	for tc := 0; tc < t; tc++ {
		n, p, k = readi3()
		p--
		S = readrs()
		x, y = readi2()

		solve()
	}
}

func solve() {
	ans := INF_B30

	// iは始点
	for i := p; i < p+k; i++ {
		num := 0
		for j := i; j < n; j += k {
			if S[j] == '0' {
				num++
			}
		}

		res := num*x + (i-p)*y
		chmin(&ans, res)
	}

	printf("%d\n", ans)
}

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

/********** common constants **********/

const (
	MOD = 1000000000 + 7
	// MOD          = 998244353
	ALPH_N  = 26
	INF_I64 = math.MaxInt64
	INF_B60 = 1 << 60
	INF_I32 = math.MaxInt32
	INF_B30 = 1 << 30
	NIL     = -1
	EPS     = 1e-10
)

/********** bufio setting **********/

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

// mod can calculate a right residual whether value is positive or negative.
func mod(val, m int) int {
	res := val % m
	if res < 0 {
		res += m
	}
	return res
}

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

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

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

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

// sum returns multiple integers sum.
func sum(integers ...int) int {
	var s int
	s = 0

	for _, i := range integers {
		s += i
	}

	return s
}

// abs is integer version of math.Abs
func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

// pow is integer version of math.Pow
// pow calculate a power by Binary Power (二分累乗法(O(log e))).
func pow(a, e int) int {
	if a < 0 || e < 0 {
		panic(errors.New("[argument error]: PowInt does not accept negative integers"))
	}

	if e == 0 {
		return 1
	}

	if e%2 == 0 {
		halfE := e / 2
		half := pow(a, halfE)
		return half * half
	}

	return a * pow(a, e-1)
}

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

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

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

//str := reads()
//i := readi()
//X := readis(n)
//S := readrs()
//a := readf()
//A := readfs(n)

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

/*********** Input ***********/

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

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

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

// readi returns an integer.
func readi() int {
	return int(_readInt64())
}
func readi2() (int, int) {
	return int(_readInt64()), int(_readInt64())
}
func readi3() (int, int, int) {
	return int(_readInt64()), int(_readInt64()), int(_readInt64())
}
func readi4() (int, int, int, int) {
	return int(_readInt64()), int(_readInt64()), int(_readInt64()), int(_readInt64())
}

// readll returns as integer as int64.
func readll() int64 {
	return _readInt64()
}
func readll2() (int64, int64) {
	return _readInt64(), _readInt64()
}
func readll3() (int64, int64, int64) {
	return _readInt64(), _readInt64(), _readInt64()
}
func readll4() (int64, int64, int64, int64) {
	return _readInt64(), _readInt64(), _readInt64(), _readInt64()
}

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

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

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

// readf returns an float64.
func readf() float64 {
	return float64(_readFloat64())
}

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

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

// readrs returns a rune slice.
func readrs() []rune {
	return []rune(reads())
}

/*********** Output ***********/

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

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

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

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

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

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

	return res
}

package main

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

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

	var t, n, p, k, x, y int
	var s string
	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &p, &k)
		fmt.Fscan(in, &s)
		a := append([]rune{' '}, []rune(s)...) // add dumb first element for convenience
		fmt.Fscan(in, &x, &y)

		minTime := n * x // upper bound of minimal time
		for i := p; i < p+k && i <= n; i++ {
			offset := i - p
			avoidedMisses := 0
			misses := 0
			for j := i; j <= n; j += k {
				if misses*x-(j-p)*y > avoidedMisses*x-offset*y {
					avoidedMisses, offset = misses, j-p
					fmt.Println(i, j, avoidedMisses, offset)
				}
				if a[j] == '0' {
					misses++
				}
			}
			if (misses-avoidedMisses)*x+offset*y < minTime {
				minTime = (misses-avoidedMisses)*x + offset*y
			}
		}

		fmt.Println(minTime)
	}
}

package main

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

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

	var t, n, p, k, x, y int
	var s string
	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &p, &k)
		fmt.Fscan(in, &s)
		a := []rune(s)
		fmt.Fscan(in, &x, &y)

		minTime := n*x + 1
		for i := p - 1; i < p-1+k; i++ {
			offset := i - p + 1
			avoidedMisses := 0
			misses := 0
			for j := i; j < n; j += k {
				if misses*x-(j-p+1)*y > avoidedMisses*x-offset*y {
					avoidedMisses, offset = misses, (j - p + 1)
					fmt.Println(i, j, avoidedMisses, offset)
				}
				if a[j] == '0' {
					misses++
				}
			}
			if (misses-avoidedMisses)*x+offset*y < minTime {
				minTime = (misses-avoidedMisses)*x + offset*y
			}
		}

		fmt.Println(minTime)
	}
}

package main

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

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

	var t, n, p, k, x, y int
	var s string
	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &p, &k)
		fmt.Fscan(in, &s)
		a := append([]rune{' '}, []rune(s)...) // add dumb first element for convenience
		fmt.Fscan(in, &x, &y)

		minTime := n * x // upper bound of minimal time
		for i := p; i < p+k && i <= n; i++ {
			offset := i - p
			avoidedMisses := 0
			misses := 0
			for j := i; j <= n; j += k {
				if misses*x-(j-p)*y > avoidedMisses*x-offset*y {
					avoidedMisses, offset = misses, j-p
					fmt.Println(i, j, avoidedMisses, offset)
				}
				if a[j] == '0' {
					misses++
				}
				// lul speshu
				if j+k > n && misses*x-(j-p)*y > avoidedMisses*x-offset*y {
					avoidedMisses, offset = misses, j-p
					fmt.Println(i, j, avoidedMisses, offset)
				}
			}
			if (misses-avoidedMisses)*x+offset*y < minTime {
				minTime = (misses-avoidedMisses)*x + offset*y
			}
		}

		fmt.Println(minTime)
	}
}


During the break, we decided to relax and play dominoes. Our box with Domino was empty, so we decided to borrow the teacher's dominoes.The teacher responded instantly at our request. He put nm dominoes on the table as an n × 2m rectangle so that each of the n rows contained m dominoes arranged horizontally. Each half of each domino contained number (0 or 1).We were taken aback, and the teacher smiled and said: "Consider some arrangement of dominoes in an n × 2m matrix. Let's count for each column of the matrix the sum of numbers in this column. Then among all such sums find the maximum one. Can you rearrange the dominoes in the matrix in such a way that the maximum sum will be minimum possible? Note that it is prohibited to change the orientation of the dominoes, they all need to stay horizontal, nevertheless dominoes are allowed to rotate by 180 degrees. As a reward I will give you all my dominoes".We got even more taken aback. And while we are wondering what was going on, help us make an optimal matrix of dominoes.

The first line contains integers n, m (1 ≤ n, m ≤ 103). In the next lines there is a description of the teachers' matrix. Each of next n lines contains m dominoes. The description of one domino is two integers (0 or 1), written without a space — the digits on the left and right half of the domino.

Print the resulting matrix of dominoes in the format: n lines, each of them contains m space-separated dominoes. If there are multiple optimal solutions, print any of them.

NoteConsider the answer for the first sample. There, the maximum sum among all columns equals 1 (the number of columns is 6, and not 3). Obviously, this maximum can't be less than 1, then such matrix is optimal.Note that the dominoes can be rotated by 180 degrees.

package main

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

var ans [1000][1000]uint8
var domino []string = []string{"00", "01", "10", "11"}

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)
	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	var empty, half, full int
	for i := 0; i < n; i++ {
		for j := 0; j < m-1; j++ {
			var s string
			fmt.Fscanf(reader, "%s ", &s)
			if s == "01" || s == "10" {
				half++
			} else if s == "11" {
				full++
			} else {
				empty++
			}
		}
		var s string
		fmt.Fscanf(reader, "%s\n", &s)
		if s == "01" || s == "10" {
			half++
		} else if s == "11" {
			full++
		} else {
			empty++
		}
	}
	for i := 0; i < n; i++ {
		if full >= m {
			full -= m
			for j := 0; j < m; j++ {
				ans[i][j] = 3
			}
		} else if empty >= m {
			empty -= m
		} else if i == n-1 {
			for j := 0; j < m; j++ {
				if full > 0 {
					ans[i][j] = 3
					full--
				} else if empty > 0 {
					empty--
				} else {
					ans[i][j] = 1
					half--
				}
			}
		} else {
			for j := 0; j < m; j++ {
				if full > 0 && empty > 0 {
					ans[i][j] = 3
					full--
					empty--
				} else if half > 1 {
					ans[i][j], ans[i+1][j] = 1, 2
					half -= 2
				} else if half == 1 && full > 0 {
					ans[i][j], ans[i+1][j] = 3, 1
					full--
					half--
				} else if half == 1 && empty > 0 {
					ans[i][j], ans[i+1][j] = 0, 1
					empty--
					half--
				} else if full > 0 {
					ans[i][j], ans[i+1][j] = 3, 3
					full -= 2
				} else if empty > 0 {
					empty -= 2
				}
			}
			i++
		}
	}
	var b strings.Builder
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			fmt.Fprintf(&b, "%s ", domino[ans[i][j]])
		}
		fmt.Fprintf(&b, "\n")
	}
	fmt.Printf("%s", b.String())
}

package main

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

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)
	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	var half, full int
	for i := 0; i < n; i++ {
		for j := 0; j < m-1; j++ {
			var s string
			fmt.Fscanf(reader, "%s ", &s)
			if s == "01" || s == "10" {
				half++
			} else if s == "11" {
				full++
			}
		}
		var s string
		fmt.Fscanf(reader, "%s\n", &s)
		if s == "01" || s == "10" {
			half++
		} else if s == "11" {
			full++
		}
	}
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if half != 0 && i%2 == 0 {
				fmt.Printf("01 ")
				half--
			} else if half != 0 && i%2 == 1 {
				fmt.Printf("10 ")
				half--
			} else if full != 0 {
				fmt.Printf("11 ")
				full--
			} else {
				fmt.Printf("00 ")
			}
		}
		fmt.Printf("\n")
	}
}


You are given several queries. In the i-th query you are given a single positive integer ni. You are to represent ni as a sum of maximum possible number of composite summands and print this maximum number, or print -1, if there are no such splittings.An integer greater than 1 is composite, if it is not prime, i.e. if it has positive divisors not equal to 1 and the integer itself.

The first line contains single integer q (1 ≤ q ≤ 105) — the number of queries. q lines follow. The (i + 1)-th line contains single integer ni (1 ≤ ni ≤ 109) — the i-th query.

For each query print the maximum possible number of summands in a valid splitting to composite summands, or -1, if there are no such splittings.

Note12 = 4 + 4 + 4 = 4 + 8 = 6 + 6 = 12, but the first splitting has the maximum possible number of summands.8 = 4 + 4, 6 can't be split into several composite summands.1, 2, 3 are less than any composite number, so they do not have valid splittings.

/*
URL:
https://codeforces.com/problemset/problem/870/C
*/

package main

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

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

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

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

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

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

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

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

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

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

var (
	q, n int
)

func main() {
	q = ReadInt()
	for i := 0; i < q; i++ {
		n = ReadInt()

		solve()
	}
}

func solve() {
	if n < 4 {
		fmt.Println(-1)
		return
	}

	ans := n / 4

	if n%4 == 0 {
	} else if n%4 == 1 {
		ans--
	} else if n%4 == 2 {
	} else {
		ans -= 2
		if ans > 0 {
			ans++
		}
	}

	if ans <= 0 {
		ans = -1
	}
	fmt.Println(ans)
}

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

// 約数の列挙: O(sqrt(n))
func divisor(n int) []int {
	var i int

	res := []int{}

	for i = 1; i*i <= n; i++ {
		if n%i == 0 {
			res = append(res, i)
			if i != (n / i) {
				res = append(res, (n / i))
			}
		}
	}

	return res
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

package main

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

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

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

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

var dp [1000]int
var c []int

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	q := toInt(scanner.Bytes())
	dp[1] = -1
	dp[2] = -1
	dp[3] = -1
	for i:=4;i<=500;i++ {
		composite := false
		for j:=2;j<i;j++ {
			if i % j == 0 {
				composite = true
				break
			}
		}
		if composite {
			c = append(c, i)
		}
		dp[i] = -1
		for j:=range c {
			if dp[i - c[j]] != -1 {
				dp[i] = max(dp[i], dp[i-c[j]] + 1)
			}
		}
	}
	for ;q>0;q-- {
		scanner.Scan()
		a := toInt(scanner.Bytes())
		if a<=500 {
			fmt.Println(dp[a])
			continue
		}
		ans := -1
		for i:=1;i<=500;i++ {
			if dp[i] == -1 || (a - i) % 4 != 0 {
				continue
			}
			ans = max(ans, (a - i) / 4 + dp[i])
		}
		fmt.Println(ans)
	}
}

// Codeforces helper functions
// 20 Jan 18
// samadhi

package main

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

const debug = true

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

	n := gsli(reader)[0]

	for i := 0; i < n; i++ {
		x := gsli(reader)[0]
		fmt.Println(maxSplit(x))
	}

}

func maxSplit(x int) (y int) {
	if x == 4 || x == 9 {
		y = 1
	} else if x < 4 || x == 5 || x == 7 || x == 11 {
		y = -1
	} else {
		f := x % 4
		switch f {
		case 0:
			return x / 4
		case 1:
			return ((x - 9) / 4) + 1
		case 2:
			return ((x - 6) / 4) + 1
		case 3:
			return ((x - 6 - 9) / 4) + 2
		}
	}
	return
}

// Input helpers

// If you don't pass in a reference to the reader, and just instantiate a new
// one every time, it will work locally, with IO typed at human-speed, but not
// on Codeforces.

// Get Space-separated Line as Strings
//
// Example input:
// 1 test 382.0
// Example output:
// ["1", "test", "382.0"]

func gsls(reader *bufio.Reader) (arr []string) {
	// Get a full line
	input, _ := reader.ReadString('\n')

	//Strip newline and return chars, if any
	input = strings.TrimSuffix(input, "\n")
	input = strings.TrimSuffix(input, "\r")

	// Split at the spaces into an array
	arr = strings.Split(input, " ")

	return
}

// Get Space-separated Line as Integers
// Any non-valid integers will be 0
//
// Example input:
// test 1 12.3 -117
// Example output:
// [0, 1, 0, -117]

func gsli(reader *bufio.Reader) (arr []int) {
	stringArr := gsls(reader)

	for _, e := range stringArr {
		cur, _ := strconv.Atoi(e)
		arr = append(arr, cur)
	}

	return
}

// Get Space-separated Line as 64-bit Floats
// Any non-valid floats will be 0
//
// Example input:
// test 128.328 12 -11.0
// Example output:
// [0, 128.328, 12, -11]

func gslf(reader *bufio.Reader) (arr []float64) {
	stringArr := gsls(reader)

	for _, e := range stringArr {
		cur, _ := strconv.ParseFloat(e, 64)
		arr = append(arr, cur)
	}

	return
}

// String helpers

// Is Lowercase
// Given some string[i], return whether it is lower case or not.

func isl(b byte) bool {
	var r rune
	r = rune(b)
	return unicode.IsLower(r)
}

// Math helpers

// Positve Modulo
// Returns x mod y, but always positive

func posMod(x, y int) int {
	return (x%y + y) % y
}

// Max Int
// Returns the max of two ints

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

// Distance formule
// Calculates the distance to the origin

func dist(x, y float64) float64 {
	return math.Sqrt(math.Pow(x, 2) + math.Pow(y, 2))
}

// Output helpers

func yes() {
	fmt.Println("Yes")
}

func no() {
	fmt.Println("No")
}

// Debug helpers

// From https://groups.google.com/forum/#!topic/golang-nuts/Qlxs3V77nss

func dbg(s string, a ...interface{}) {
	if debug {
		fmt.Printf(s, a...)
	}
}

/*
URL:
https://codeforces.com/problemset/problem/870/C
*/

package main

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

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

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

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

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

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

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

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

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

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

var (
	q, n int
)

func main() {
	q = ReadInt()
	for i := 0; i < q; i++ {
		n = ReadInt()

		solve()
	}
}

func solve() {
	if n < 4 {
		fmt.Println(-1)
		return
	}

	ans := n / 4

	if n%4 == 0 {
	} else if n%4 == 1 {
		ans--
	} else if n%4 == 2 {
	} else {
		ans -= 2
	}

	if ans < 0 {
		ans = -1
	}
	fmt.Println(ans)
}

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

// 約数の列挙: O(sqrt(n))
func divisor(n int) []int {
	var i int

	res := []int{}

	for i = 1; i*i <= n; i++ {
		if n%i == 0 {
			res = append(res, i)
			if i != (n / i) {
				res = append(res, (n / i))
			}
		}
	}

	return res
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

/*
URL:
https://codeforces.com/problemset/problem/870/C
*/

package main

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

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

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

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

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

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

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

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

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

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

var (
	q, n int
)

func main() {
	q = ReadInt()
	for i := 0; i < q; i++ {
		n = ReadInt()

		solve()
	}
}

func solve() {
	if n < 4 {
		fmt.Println(-1)
		return
	}

	ans := n / 4

	if n%4 == 0 {
	} else if n%4 == 1 {
		ans--
	} else if n%4 == 2 {
	} else {
		ans -= 2
	}

	if ans <= 0 {
		ans = -1
	}
	fmt.Println(ans)
}

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

// 約数の列挙: O(sqrt(n))
func divisor(n int) []int {
	var i int

	res := []int{}

	for i = 1; i*i <= n; i++ {
		if n%i == 0 {
			res = append(res, i)
			if i != (n / i) {
				res = append(res, (n / i))
			}
		}
	}

	return res
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

// Codeforces helper functions
// 20 Jan 18
// samadhi

package main

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

const debug = true

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

	n := gsli(reader)[0]

	for i := 0; i < n; i++ {
		x := gsli(reader)[0]
		fmt.Println(maxSplit(x))
	}

}

func maxSplit(x int) (y int) {
	if x == 4 || x == 9 || x == 13 {
		y = 1
	} else if x < 4 || x == 5 || x == 7 || x == 11 {
		y = -1
	} else {
		f := x % 4
		switch f {
		case 0:
			return x / 4
		case 1:
			return ((x - 9) / 4) + 1
		case 2:
			return ((x - 6) / 4) + 1
		case 3:
			return ((x - 6 - 9) / 4) + 2
		}
	}
	return
}

// Input helpers

// If you don't pass in a reference to the reader, and just instantiate a new
// one every time, it will work locally, with IO typed at human-speed, but not
// on Codeforces.

// Get Space-separated Line as Strings
//
// Example input:
// 1 test 382.0
// Example output:
// ["1", "test", "382.0"]

func gsls(reader *bufio.Reader) (arr []string) {
	// Get a full line
	input, _ := reader.ReadString('\n')

	//Strip newline and return chars, if any
	input = strings.TrimSuffix(input, "\n")
	input = strings.TrimSuffix(input, "\r")

	// Split at the spaces into an array
	arr = strings.Split(input, " ")

	return
}

// Get Space-separated Line as Integers
// Any non-valid integers will be 0
//
// Example input:
// test 1 12.3 -117
// Example output:
// [0, 1, 0, -117]

func gsli(reader *bufio.Reader) (arr []int) {
	stringArr := gsls(reader)

	for _, e := range stringArr {
		cur, _ := strconv.Atoi(e)
		arr = append(arr, cur)
	}

	return
}

// Get Space-separated Line as 64-bit Floats
// Any non-valid floats will be 0
//
// Example input:
// test 128.328 12 -11.0
// Example output:
// [0, 128.328, 12, -11]

func gslf(reader *bufio.Reader) (arr []float64) {
	stringArr := gsls(reader)

	for _, e := range stringArr {
		cur, _ := strconv.ParseFloat(e, 64)
		arr = append(arr, cur)
	}

	return
}

// String helpers

// Is Lowercase
// Given some string[i], return whether it is lower case or not.

func isl(b byte) bool {
	var r rune
	r = rune(b)
	return unicode.IsLower(r)
}

// Math helpers

// Positve Modulo
// Returns x mod y, but always positive

func posMod(x, y int) int {
	return (x%y + y) % y
}

// Max Int
// Returns the max of two ints

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

// Distance formule
// Calculates the distance to the origin

func dist(x, y float64) float64 {
	return math.Sqrt(math.Pow(x, 2) + math.Pow(y, 2))
}

// Output helpers

func yes() {
	fmt.Println("Yes")
}

func no() {
	fmt.Println("No")
}

// Debug helpers

// From https://groups.google.com/forum/#!topic/golang-nuts/Qlxs3V77nss

func dbg(s string, a ...interface{}) {
	if debug {
		fmt.Printf(s, a...)
	}
}


You are given a binary string (i. e. a string consisting of characters 0 and/or 1) $$$s$$$ of length $$$n$$$. You can perform the following operation with the string $$$s$$$ at most once: choose a substring (a contiguous subsequence) of $$$s$$$ having exactly $$$k$$$ characters 1 in it, and shuffle it (reorder the characters in the substring as you wish).Calculate the number of different strings which can be obtained from $$$s$$$ by performing this operation at most once.

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 5000$$$; $$$0 \le k \le n$$$). The second line contains the string $$$s$$$ of length $$$n$$$, consisting of characters 0 and/or 1.

Print one integer — the number of different strings which can be obtained from $$$s$$$ by performing the described operation at most once. Since the answer can be large, output it modulo $$$998244353$$$.

NoteSome strings you can obtain in the first example:  to obtain 0110110, you can take the substring from the $$$1$$$-st character to the $$$4$$$-th character, which is 1100, and reorder its characters to get 0110;  to obtain 1111000, you can take the substring from the $$$3$$$-rd character to the $$$7$$$-th character, which is 00110, and reorder its characters to get 11000;  to obtain 1100101, you can take the substring from the $$$5$$$-th character to the $$$7$$$-th character, which is 110, and reorder its characters to get 101. In the second example, $$$k = 0$$$ so you can only choose the substrings consisting only of 0 characters. Reordering them doesn't change the string at all, so the only string you can obtain is 10010.

package main

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

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

const TAM = 5010
const MOD = 998244353

func add(a, b int) int {
	return (a + b) % MOD
}

func subtract(a, b int) int {
	return (a + (MOD - b)) % MOD
}

var s string
var n, k int
var nCk [TAM][TAM]int
var next1 [TAM]int
var prev1 [TAM]int

// var sufSum [TAM]int

func solve(seg []int, prevSeg []int) int {
	// fmt.Fprintf(out, "solve %s (%v %v)\n", s[seg[0]:seg[1]+1], seg, prevSeg)
	var segLen = seg[1] - seg[0] + 1
	var ans = nCk[segLen][k]
	if len(prevSeg) > 0 {
		ans = subtract(ans, nCk[segLen-(seg[1]-prevSeg[1])][k-1])
	}
	return ans
}

func main() {
	fmt.Fscan(in, &n, &k)
	fmt.Fscan(in, &s)

	if k == 0 {
		fmt.Fprintf(out, "1\n")
		out.Flush()
		return
	}
	prev1[0] = -1
	for i := 0; i < n; i++ {
		if s[i] == '1' {
			prev1[i+1] = i
		} else {
			prev1[i+1] = prev1[i]
		}
	}

	next1[n-1] = n
	for i := n - 1; i > 0; i-- {
		if s[i] == '1' {
			next1[i-1] = i
		} else {
			next1[i-1] = next1[i]
		}
	}

	// sufSum[n] = 0
	// for i := n - 1; i >= 0; i-- {
	// 	sufSum[i] = sufSum[i+1]
	// 	if s[i] == '1' {
	// 		sufSum[i]++
	// 	}
	// }

	for i := 0; i <= n; i++ {
		for j := 0; j <= n; j++ {
			if j == 0 || j == i {
				nCk[i][j] = 1
			} else if j > i {
				nCk[i][j] = 0
			} else {
				nCk[i][j] = add(nCk[i-1][j-1], nCk[i-1][j])
			}
		}
	}

	var ans = 0
	var ones []int
	var prevSegment []int = []int{}
	var curSegment []int = []int{-1, -1}
	for i := 0; i < n; i++ {
		if s[i] == '1' {
			ones = append(ones, i)
			if len(ones) > k {
				ones = ones[1:]
			}

			if len(ones) == k {
				curSegment = []int{
					prev1[ones[0]] + 1,
					next1[ones[k-1]] - 1,
				}
				ans = add(ans, solve(curSegment, prevSegment))
				prevSegment = curSegment
			}
		}
	}

	if len(ones) < k {
		ans = 1
	}

	fmt.Fprintf(out, "%d\n", ans)
	out.Flush()
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	n, k := readTwoNums(reader)
	s, _ := reader.ReadString('\n')
	res := solve(n, k, s)
	fmt.Println(res)
}

func normalize(s string) string {

	for i := len(s); i > 0; i-- {
		if s[i-1] >= 'a' && s[i-1] <= 'z' {
			return s[:i]
		}
	}
	return ""
}

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

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

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

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

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

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

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

	return i
}

const MOD = 998244353

const N = 5010

var F []int64
var I []int64

func pow(a, b int) int {
	A := int64(a)
	R := int64(1)

	for b > 0 {
		if b&1 == 1 {
			R = (R * A) % MOD
		}
		A = (A * A) % MOD
		b >>= 1
	}
	return int(R)
}

func inverse(a int) int {
	return pow(a, MOD-2)
}

func init() {
	F = make([]int64, N)
	I = make([]int64, N)
	F[0] = 1
	for i := 1; i < N; i++ {
		F[i] = mul(int64(i), F[i-1])
	}
	I[N-1] = int64(inverse(int(F[N-1])))

	for i := N - 2; i >= 0; i-- {
		I[i] = mul(int64(i+1), I[i+1])
	}
}

func nCr(n, r int) int64 {
	if n < 0 || n < r {
		return 0
	}
	res := F[n]
	res = mul(res, I[r])
	res = mul(res, I[n-r])
	return res
}

func mul(a, b int64) int64 {
	a *= b
	a %= MOD
	return a
}

func add(a, b int64) int64 {
	a += b
	if a >= MOD {
		a -= MOD
	}
	return a
}

func sub(a, b int64) int64 {
	return add(a, MOD-b)
}

func solve(n int, k int, s string) int {
	var ones []int
	for i := 0; i < n; i++ {
		if s[i] == '1' {
			ones = append(ones, i)
		}
	}

	if len(ones) < k || k == 0 {
		return 1
	}

	var tot int64 = 1
	for i, cur := range ones {
		var start int
		if i > 0 {
			start = ones[i-1] + 1
		}
		var end int = n - 1
		if i+k < len(ones) {
			end = ones[i+k] - 1
		}
		tot = add(tot, nCr(end-start+1, min(k, len(ones)-i)))
		tot = sub(tot, nCr(end-cur, min(k, len(ones)-i)-1))
	}

	return int(tot)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1622D(in io.Reader, out io.Writer) {
	const mod, mx = 998244353, 5000
	F := [mx + 1]int64{1}
	for i := 1; i <= mx; i++ {
		F[i] = F[i-1] * int64(i) % mod
	}
	pow := func(x, n int64) (res int64) {
		res = 1
		for ; n > 0; n >>= 1 {
			if n&1 > 0 {
				res = res * x % mod
			}
			x = x * x % mod
		}
		return
	}
	invF := [...]int64{mx: pow(F[mx], mod-2)}
	for i := mx; i > 0; i-- {
		invF[i-1] = invF[i] * int64(i) % mod
	}
	C := func(n, k int) int64 { return F[n] * invF[k] % mod * invF[n-k] % mod }

	var n, k int
	var s string
	Fscan(bufio.NewReader(in), &n, &k, &s)
	pos := []int{}
	for i, b := range s {
		if b == '1' {
			pos = append(pos, i)
		}
	}
	if k == 0 || len(pos) < k {
		Fprint(out, 1)
		return
	}
	pos = append(pos, n)
	ans := C(pos[k], k)
	for i, p := range pos[:len(pos)-k-1] {
		ans += C(pos[i+k+1]-p-1, k) - C(pos[i+k]-p-1, k-1)
	}
	Fprint(out, (ans%mod+mod)%mod)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1622D(in io.Reader, out io.Writer) {
	const mod, mx = 998244353, 5000
	F := [mx + 1]int64{1}
	for i := 1; i <= mx; i++ {
		F[i] = F[i-1] * int64(i) % mod
	}
	pow := func(x, n int64) (res int64) {
		res = 1
		for ; n > 0; n >>= 1 {
			if n&1 > 0 {
				res = res * x % mod
			}
			x = x * x % mod
		}
		return
	}
	invF := [...]int64{mx: pow(F[mx], mod-2)}
	for i := mx; i > 0; i-- {
		invF[i-1] = invF[i] * int64(i) % mod
	}
	C := func(n, k int) int64 { return F[n] * invF[k] % mod * invF[n-k] % mod }

	var n, k int
	var s string
	Fscan(bufio.NewReader(in), &n, &k, &s)
	pos := []int{}
	for i, b := range s {
		if b == '1' {
			pos = append(pos, i)
		}
	}
	if k == 0 || len(pos) < k {
		Fprint(out, 1)
		return
	}
	pos = append(pos, n)
	ans := C(pos[k], k)
	for i := k + 1; i < len(pos); i++ {
		ans += C(pos[i]-pos[i-k-1]-1, k) - C(pos[i-1]-pos[i-k-1]-1, k-1)
	}
	Fprint(out, (ans%mod+mod)%mod)
}

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

package main

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

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

const MOD int64 = 998244353

func solve(t int) {
	n, k := readInt(), readInt()
	s := readString()
	if k == 0 {
		println(1)
		return
	}

	C := make([][]int64, n+1)
	for i := 0; i <= n; i++ {
		C[i] = make([]int64, k+1)
		for j := 0; j <= k; j++ {
			if j == 0 {
				C[i][j] = 1
			} else if i == 0 {
				C[i][j] = 0
			} else {
				C[i][j] = (C[i-1][j-1] + C[i-1][j]) % MOD
			}
		}
	}

	L := make([]int64, n)
	cur := 0
	sum1 := 0
	for i := 0; i < n; i++ {
		sum1 += int(s[i] - '0')
		for sum1 > k {
			sum1 -= int(s[cur] - '0')
			cur += 1
		}
		// println(i, cur, i-cur+1)
		L[i] = int64(i - cur + 1)
	}

	dp := make([]int64, n)
	sum1 = 0
	for i := 0; i < n; i++ {
		sum1 += int(s[i] - '0')
		if sum1 < k {
			dp[i] = 1
		} else if sum1 == k {
			dp[i] = C[L[i]][k]
		} else {
			if s[i] == '0' {
				dp[i] = (dp[i-1] + C[L[i]-1][k-1]) % MOD
			} else {
				dp[i] = (dp[i-1] + C[L[i]-1][k]) % MOD
			}
		}
	}

	println(dp[n-1])
}

func main() {
	var t int
	// fmt.Fscan(in, &t)
	t = 1

	for i := 0; i < t; i++ {
		solve(i)
	}
}

// READ
func readInt() int {
	var res int
	fmt.Fscan(in, &res)
	return res
}

func readInt64() int64 {
	var res int64
	fmt.Fscan(in, &res)
	return res
}

func readString() string {
	var res string
	fmt.Fscan(in, &res)
	return res
}

// PRINT
func print(args ...interface{}) {
	fmt.Fprint(out, args...)
	out.Flush()
}

func println(args ...interface{}) {
	fmt.Fprintln(out, args...)
	out.Flush()
}

func printErr(args ...interface{}) {
	fmt.Fprint(err, args...)
	err.Flush()
}

func printlnErr(args ...interface{}) {
	fmt.Fprintln(err, args...)
	err.Flush()
}

package main

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

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

const TAM = 5010
const MOD = 998244353

func add(a, b int) int {
	return (a + b) % MOD
}

var s string
var n, k int
var nCk [TAM][TAM]int
var next1 [TAM]int
var prev1 [TAM]int

// var sufSum [TAM]int

func solve(seg []int, prevSeg []int) int {
	// fmt.Fprintf(out, "solve %s (%v %v)\n", s[seg[0]:seg[1]+1], seg, prevSeg)
	var segLen = seg[1] - seg[0] + 1
	var ans = nCk[segLen][k]
	if len(prevSeg) > 0 {
		ans -= nCk[segLen-(seg[1]-prevSeg[1])][k-1]
	}
	return ans
}

func main() {
	fmt.Fscan(in, &n, &k)
	fmt.Fscan(in, &s)

	if k == 0 {
		fmt.Fprintf(out, "1\n")
		out.Flush()
		return
	}
	prev1[0] = -1
	for i := 0; i < n; i++ {
		if s[i] == '1' {
			prev1[i+1] = i
		} else {
			prev1[i+1] = prev1[i]
		}
	}

	next1[n-1] = n
	for i := n - 1; i > 0; i-- {
		if s[i] == '1' {
			next1[i-1] = i
		} else {
			next1[i-1] = next1[i]
		}
	}

	// sufSum[n] = 0
	// for i := n - 1; i >= 0; i-- {
	// 	sufSum[i] = sufSum[i+1]
	// 	if s[i] == '1' {
	// 		sufSum[i]++
	// 	}
	// }

	for i := 0; i <= n; i++ {
		for j := 0; j <= n; j++ {
			if j == 0 || j == i {
				nCk[i][j] = 1
			} else if j > i {
				nCk[i][j] = 0
			} else {
				nCk[i][j] = add(nCk[i-1][j-1], nCk[i-1][j])
			}
		}
	}

	var ans = 0
	var ones []int
	var prevSegment []int = []int{}
	var curSegment []int = []int{-1, -1}
	for i := 0; i < n; i++ {
		if s[i] == '1' {
			ones = append(ones, i)
			if len(ones) > k {
				ones = ones[1:]
			}

			if len(ones) == k {
				curSegment = []int{
					prev1[ones[0]] + 1,
					next1[ones[k-1]] - 1,
				}
				ans += solve(curSegment, prevSegment)
				prevSegment = curSegment
			}
		}
	}

	if len(ones) < k {
		ans = 1
	}

	fmt.Fprintf(out, "%d\n", ans)
	out.Flush()
}

package main

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

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

const TAM = 5010
const MOD = 998244353

func add(a, b int) int {
	return (a + b) % MOD
}

var s string
var n, k int
var nCk [TAM][TAM]int
var next1 [TAM]int
var prev1 [TAM]int

// var sufSum [TAM]int

func solve(seg []int, prevSeg []int) int {
	// fmt.Fprintf(out, "solve %s (%v %v)\n", s[seg[0]:seg[1]+1], seg, prevSeg)
	var segLen = seg[1] - seg[0] + 1
	var ans = nCk[segLen][k]
	if len(prevSeg) > 0 {
		ans -= nCk[segLen-(seg[1]-prevSeg[1])][k-1]
	}
	return ans
}

func main() {
	fmt.Fscan(in, &n, &k)
	fmt.Fscan(in, &s)

	if k == 0 {
		fmt.Fprintf(out, "0\n")
		out.Flush()
		return
	}
	prev1[0] = -1
	for i := 0; i < n; i++ {
		if s[i] == '1' {
			prev1[i+1] = i
		} else {
			prev1[i+1] = prev1[i]
		}
	}

	next1[n-1] = n
	for i := n - 1; i > 0; i-- {
		if s[i] == '1' {
			next1[i-1] = i
		} else {
			next1[i-1] = next1[i]
		}
	}

	// sufSum[n] = 0
	// for i := n - 1; i >= 0; i-- {
	// 	sufSum[i] = sufSum[i+1]
	// 	if s[i] == '1' {
	// 		sufSum[i]++
	// 	}
	// }

	for i := 0; i <= n; i++ {
		for j := 0; j <= n; j++ {
			if j == 0 || j == i {
				nCk[i][j] = 1
			} else if j > i {
				nCk[i][j] = 0
			} else {
				nCk[i][j] = add(nCk[i-1][j-1], nCk[i-1][j])
			}
		}
	}

	var ans = 0
	var ones []int
	var prevSegment []int = []int{}
	var curSegment []int = []int{-1, -1}
	for i := 0; i < n; i++ {
		if s[i] == '1' {
			ones = append(ones, i)
			if len(ones) > k {
				ones = ones[1:]
			}

			if len(ones) == k {
				curSegment = []int{
					prev1[ones[0]] + 1,
					next1[ones[k-1]] - 1,
				}
				ans += solve(curSegment, prevSegment)
				prevSegment = curSegment
			}
		}
	}

	if len(ones) < k {
		ans = 1
	}

	fmt.Fprintf(out, "%d\n", ans)
	out.Flush()
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	n, k := readTwoNums(reader)
	s, _ := reader.ReadString('\n')
	res := solve(n, k, s)
	fmt.Println(res)
}

func normalize(s string) string {

	for i := len(s); i > 0; i-- {
		if s[i-1] >= 'a' && s[i-1] <= 'z' {
			return s[:i]
		}
	}
	return ""
}

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

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

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

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

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

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

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

	return i
}

const MOD = 998244353

const N = 5010

var F []int64
var I []int64

func pow(a, b int) int {
	A := int64(a)
	R := int64(1)

	for b > 0 {
		if b&1 == 1 {
			R = (R * A) % MOD
		}
		A = (A * A) % MOD
		b >>= 1
	}
	return int(R)
}

func inverse(a int) int {
	return pow(a, MOD-2)
}

func init() {
	F = make([]int64, N)
	I = make([]int64, N)
	F[0] = 1
	for i := 1; i < N; i++ {
		F[i] = mul(int64(i), F[i-1])
	}
	I[N-1] = int64(inverse(int(F[N-1])))

	for i := N - 2; i >= 0; i-- {
		I[i] = mul(int64(i+1), I[i+1])
	}
}

func nCr(n, r int) int64 {
	if n < 0 || n < r {
		return 0
	}
	res := F[n]
	res = mul(res, I[r])
	res = mul(res, I[n-r])
	return res
}

func mul(a, b int64) int64 {
	a *= b
	a %= MOD
	return a
}

func add(a, b int64) int64 {
	a += b
	if a >= MOD {
		a -= MOD
	}
	return a
}

func sub(a, b int64) int64 {
	return add(a, MOD-b)
}

func solve(n int, k int, s string) int {
	var ones []int
	for i := 0; i < n; i++ {
		if s[i] == '1' {
			ones = append(ones, i)
		}
	}

	if len(ones) < k {
		return 1
	}

	var tot int64 = 1
	for i, cur := range ones {
		var start int
		if i > 0 {
			start = ones[i-1] + 1
		}
		var end int = n - 1
		if i+k < len(ones) {
			end = ones[i+k] - 1
		}
		tot = add(tot, nCr(end-start+1, min(k, len(ones)-i)))
		tot = sub(tot, nCr(end-cur, min(k, len(ones)-i)-1))
	}

	return int(tot)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1622D(in io.Reader, out io.Writer) {
	const mod, mx = 998244353, 5000
	F := [mx + 1]int64{1}
	for i := 1; i <= mx; i++ {
		F[i] = F[i-1] * int64(i) % mod
	}
	pow := func(x, n int64) (res int64) {
		res = 1
		for ; n > 0; n >>= 1 {
			if n&1 > 0 {
				res = res * x % mod
			}
			x = x * x % mod
		}
		return
	}
	invF := [...]int64{mx: pow(F[mx], mod-2)}
	for i := mx; i > 0; i-- {
		invF[i-1] = invF[i] * int64(i) % mod
	}
	C := func(n, k int) int64 { return F[n] * invF[k] % mod * invF[n-k] % mod }

	var n, k int
	var s string
	Fscan(bufio.NewReader(in), &n, &k, &s)
	pos := []int{}
	for i, b := range s {
		if b == '1' {
			pos = append(pos, i)
		}
	}
	if k == 0 || len(pos) < k {
		Fprint(out, 1)
		return
	}
	pos = append(pos, n)
	ans := C(pos[k], k)
	for i := k + 1; i < len(pos); i++ {
		d := pos[i] - pos[i-k-1] - 1
		if i < len(pos)-1 {
			ans = (ans + C(d, k) - C(d-1, k-1)) % mod
		} else {
			ans = (ans + C(d, k) - C(pos[i-1]-pos[i-k-1]-1, k-1)) % mod
		}
	}
	ans = (ans%mod + mod) % mod
	Fprint(out, ans)
}

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


Berland annual chess tournament is coming!Organizers have gathered 2·n chess players who should be divided into two teams with n people each. The first team is sponsored by BerOil and the second team is sponsored by BerMobile. Obviously, organizers should guarantee the win for the team of BerOil.Thus, organizers should divide all 2·n players into two teams with n people each in such a way that the first team always wins.Every chess player has its rating ri. It is known that chess player with the greater rating always wins the player with the lower rating. If their ratings are equal then any of the players can win.After teams assignment there will come a drawing to form n pairs of opponents: in each pair there is a player from the first team and a player from the second team. Every chess player should be in exactly one pair. Every pair plays once. The drawing is totally random.Is it possible to divide all 2·n players into two teams with n people each so that the player from the first team in every pair wins regardless of the results of the drawing?

The first line contains one integer n (1 ≤ n ≤ 100). The second line contains 2·n integers a1, a2, ... a2n (1 ≤ ai ≤ 1000).

If it's possible to divide all 2·n players into two teams with n people each so that the player from the first team in every pair wins regardless of the results of the drawing, then print "YES". Otherwise print "NO".

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	scanner.Scan()
	args := strings.Split(strings.TrimSpace(scanner.Text()), " ")

	values := make([]int, len(args))
	for i, a := range args {
		values[i], _ = strconv.Atoi(a)
	}

	sort.Ints(values)
	n := len(args) / 2
	if values[n] > values[n - 1] {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}

package main

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

const maxTokenSize = 512 * 1024

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, maxTokenSize), maxTokenSize)

	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())

	a := make([]int, 2*n)

	for i := 0; i < n; i++ {
		scanner.Scan()
		a[2*i], _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		a[2*i+1], _ = strconv.Atoi(scanner.Text())
	}

	sort.Ints(a)
	if a[n-1] < a[n] {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}

package main

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

func main() {
	var n int
	fmt.Scanf("%d\n", &n)
	shahmat := make([]int, n*2)
	for i := 0; i < n*2; i++ {
		fmt.Scanf("%d", &shahmat[i])
	}
	sort.Ints(shahmat)
	for i := 0; i < n; i++ {
		for j := n; j < n*2; j++ {
			if shahmat[j] <= shahmat[i] {
				fmt.Println("NO")
				os.Exit(0)
			}
		}
	}
	fmt.Println("YES")
}

package main

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

func main() {
	var n int
	fmt.Scanf("%d\n", &n)
	shahmat := make([]int, n*2)
	for i := 0; i < n*2; i++ {
		fmt.Scanf("%d", &shahmat[i])
	}
	sort.Ints(shahmat)
	for i, j := 0, n; i < n; i, j = i+1, j+1 {
		for j := n; j < n*2; j++ {
			if shahmat[j] <= shahmat[i] {
				fmt.Println("NO")
				os.Exit(0)
			}
		}
	}
	fmt.Println("YES")
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n int
	fmt.Scan(&n)
	var a = make([]int, 2*n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[2*i],&a[2*i+1])
	}
	sort.Ints(a)
	if a[n-1] < a[n] {
		fmt.Printf("YES\n")
	} else {
		fmt.Printf("NO\n")
	}
}

package main

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

func main() {
	var n int
	fmt.Scanf("%d\n", &n)
	shahmat := make([]int, n*2)
	for i := 0; i < n*2; i++ {
		fmt.Scanf("%d", &shahmat[i])
	}
	sort.Ints(shahmat)
	for i, j := 0, n/2+1; i < n; i, j = i+1, j+1 {
		if shahmat[i] >= shahmat[j] {
			fmt.Println("NO")
			os.Exit(0)
		}
	}
	fmt.Println("YES")
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n int
	fmt.Scanf("%d",&n)
	var a = make([]int, 2*n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d%d",&a[2*i],&a[2*i+1])
	}
	sort.Ints(a)
	if a[n-1] < a[n] {
		fmt.Printf("YES\n")
	} else {
		fmt.Printf("NO\n")
	}
}