You are given a table consisting of n rows and m columns.Numbers in each row form a permutation of integers from 1 to m.You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order.You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order.

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 20) — the number of rows and the number of columns in the given table.  Each of next n lines contains m integers — elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m.

If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes).

NoteIn the first sample, one can act in the following way:   Swap second and third columns. Now the table is 1 2 3 4 1 4 3 2  In the second row, swap the second and the fourth elements. Now the table is 1 2 3 4 1 2 3 4

package main

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

func I(i interface{}, delim string) {
	fmt.Fscanf(r, "%v"+delim, i)
}

func O(o interface{}, delim string) {
	fmt.Fprintf(w, "%v"+delim, o)
}

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

func main() {
	r = bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)
	defer w.Flush()
	var n, m int
	I(&n, " ")
	I(&m, "\n")
	in := make([][]int, n)
	for i := 0; i < n; i++ {
		in[i] = make([]int, m)
		for j := 0; j < m; j++ {
			I(&in[i][j], " ")
		}
	}
	for i := 0; i < m; i++ {
		for j := 0; j < m; j++ {
			if i != j || (i == 0 && j == 0) {
				for u := 0; u < n; u++ {
					in[u][i], in[u][j] = in[u][j], in[u][i]
				}
				for u := 0; u < n; u++ {
					z := 0
					for v := 0; v < m; v++ {
						if in[u][v] != v+1 {
							z++
						}
					}
					if z > 2 {
						break
					}
					if u == n-1 {
						O("YES", "\n")
						return
					}
				}
				for u := 0; u < n; u++ {
					in[u][i], in[u][j] = in[u][j], in[u][i]
				}
			}
		}
	}
	O("NO", "\n")
}

package main

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

var bufin *bufio.Reader
var bufout *bufio.Writer

func diff(m int, a []int) int {
	cnt := 0
	for i := 0; i < m; i++ {
		if a[i] != i+1 {
			cnt++
		}
	}
	return cnt
}

func dup(a []int) []int {
	b := make([]int, len(a))
	copy(b, a)
	return b
}

func good(m int, a []int, i int, j int) bool {
	a[i], a[j] = a[j], a[i]
	return diff(m, a) <= 2
}

func can(n int, m int, a [][]int, i int, j int) bool {
	for r := 0; r < n; r++ {
		if !good(m, dup(a[r]), i, j) {
			return false
		}
	}
	return true
}

func yes(n int, m int, a [][]int) bool {
	for i := 0; i < m; i++ {
		for j := i; j < m; j++ {
			if can(n, m, a, i, j) {
				return true
			}
		}
	}
	return false
}

func main() {
	bufin = bufio.NewReader(os.Stdin)
	bufout = bufio.NewWriter(os.Stdout)
	defer bufout.Flush()

	var n, m int
	fmt.Fscanf(bufin, "%d %d\n", &n, &m)

	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		for j := 0; j < m; j++ {
			fmt.Fscanf(bufin, "%d\n", &a[i][j])
		}
	}

	if yes(n, m, a) {
		fmt.Fprintf(bufout, "YES\n")
	} else {
		fmt.Fprintf(bufout, "NO\n")
	}
}

// http://codeforces.com/contest/724/problem/B
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

const (
	MOD = 1e9 + 7
)

func check(arr [][]int) bool {
	for i := 0; i < len(arr); i++ {
		changes := 0
		for j := 0; j < len(arr[i]); j++ {
			if arr[i][j] != j + 1 {
				changes++
			}

			if changes > 2 {
				return false
			}
		}
	}

	return true
}

func swap(arr [][]int, k, l int) {
	for i := 0; i < len(arr); i++ {
		arr[i][k], arr[i][l] = arr[i][l], arr[i][k]
	}
}

func main() {
	var (
		n, m int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)

	fmt.Fscanln(input, &n, &m)

	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		for j := 0; j < m; j++ {
			fmt.Fscan(input, &a[i][j])
		}
	}

	// Solve
	if n == 1 && m == 1 {
		if a[0][0] == 1 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}

		return
	}

	if check(a) {
		fmt.Println("YES")
		return
	}

	for i := 0; i < m; i++ {
		for j := i + 1; j < m; j++ {
			swap(a, i, j)
			if check(a) {
				fmt.Println("YES")
				return
			}
			swap(a, i, j)
		}
	}

	fmt.Println("NO")
}

// http://codeforces.com/contest/724/problem/B
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

const (
	MOD = 1e9 + 7
)

func check(arr [][]int) bool {
	for i := 0; i < len(arr); i++ {
		changes := 0
		for j := 0; j < len(arr[i]); j++ {
			if arr[i][j] != j + 1 {
				changes++
			}

			if changes > 2 {
				return false
			}
		}
	}

	return true
}

func swap(arr [][]int, k, l int) {
	for i := 0; i < len(arr); i++ {
		arr[i][k], arr[i][l] = arr[i][l], arr[i][k]
	}
}

func main() {
	var (
		n, m int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)

	fmt.Fscanln(input, &n, &m)

	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		for j := 0; j < m; j++ {
			fmt.Fscan(input, &a[i][j])
		}
	}

	// Solve
	/*if n == 1 && m == 1 {
		if a[0][0] == 1 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}

		return
	}*/

	if check(a) {
		fmt.Println("YES")
		return
	}

	for i := 0; i < m; i++ {
		for j := i + 1; j < m; j++ {
			swap(a, i, j)
			if check(a) {
				fmt.Println("YES")
				return
			}
			swap(a, i, j)
		}
	}

	fmt.Println("NO")
}

package main

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

func I(i interface{}, delim string) {
	fmt.Fscanf(r, "%v"+delim, i)
}

func O(o interface{}, delim string) {
	fmt.Fprintf(w, "%v"+delim, o)
}

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

func main() {
	r = bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)
	defer w.Flush()

	var n, m int
	I(&n, " ")
	I(&m, "\n")
	var a [][]int
	for i := 0; i < n; i++ {
		a = append(a, make([]int, m))
		for j := 0; j < m-1; j++ {
			I(&a[i][j], " ")
		}
		I(&a[i][m-1], "\n")
	}
	o := make([]int, m)
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if a[i][j] != j+1 {
				for v := 0; v < m; v++ {
					if a[i][v] != v+1 && a[i][v] == j+1 {
						o[j]++
						if a[i][j] == v+1 && v > j {
							o[v]--
						}
					}
				}
			}
		}
	}
	ans := 0
	for i := 0; i < m; i++ {
		ans += o[i] % n
	}
	if ans <= n+1 {
		O("YES", "\n")
	} else {
		O("NO", "\n")
	}
}

package main

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

var bufin *bufio.Reader
var bufout *bufio.Writer

func diff(m int, a []int) int {
	cnt := 0
	for i := 0; i < m; i++ {
		if a[i] != i+1 {
			cnt++
		}
	}
	return cnt
}

func dup(m int, arr []int) []int {
	a := make([]int, m)
	for k := 0; k < m; k++ {
		a[k] = arr[k]
	}
	return a
}

func ok1(m int, arr []int, i int, j int, k int) bool {
	a := dup(m, arr)
	a[i], a[j], a[k] = a[j], a[k], a[i]
	return diff(m, a) == 0
}

func ok2(m int, arr []int, i int, j int, k int) bool {
	a := dup(m, arr)
	a[i], a[j], a[k] = a[k], a[i], a[j]
	return diff(m, a) == 0
}

func before(m int, arr []int, i int, j int) bool {
	for k := 0; k < m; k++ {
		if (k != i) && (k != j) {
			if ok1(m, arr, i, j, k) || ok2(m, arr, i, j, k) {
				return true
			}
		}
	}
	return false
}

func after(m int, arr []int, i int, j int) bool {
	a := dup(m, arr)
	if diff(m, a) <= 2 {
		return true
	}
	a[i], a[j] = a[j], a[i]
	return diff(m, a) <= 2
}

func good(m int, a []int, i int, j int) bool {
	if after(m, a, i, j) {
		return true
	}
	return before(m, a, i, j)
}

func can(n int, m int, a [][]int, i int, j int) bool {
	for r := 0; r < n; r++ {
		if !good(m, a[r], i, j) {
			return false
		}
	}
	return true
}

func yes(n int, m int, a [][]int) bool {
	for i := 0; i < m; i++ {
		for j := i + 1; j < m; j++ {
			if can(n, m, a, i, j) {
				return true
			}
		}
	}
	return false
}

func main() {
	bufin = bufio.NewReader(os.Stdin)
	bufout = bufio.NewWriter(os.Stdout)
	defer bufout.Flush()

	var n, m int
	fmt.Fscanf(bufin, "%d %d\n", &n, &m)

	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		for j := 0; j < m; j++ {
			fmt.Fscanf(bufin, "%d\n", &a[i][j])
		}
	}

	if yes(n, m, a) {
		fmt.Fprintf(bufout, "YES\n")
	} else {
		fmt.Fprintf(bufout, "NO\n")
	}
}

package main

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

var bufin *bufio.Reader
var bufout *bufio.Writer

func diff(m int, a []int) int {
	cnt := 0
	for i := 0; i < m; i++ {
		if a[i] != i+1 {
			cnt++
		}
	}
	return cnt
}

func dup(a []int) []int {
	b := make([]int, len(a))
	copy(b, a)
	return b
}

func good(m int, a []int, i int, j int) bool {
	a[i], a[j] = a[j], a[i]
	return diff(m, a) <= 2
}

func can(n int, m int, a [][]int, i int, j int) bool {
	for r := 0; r < n; r++ {
		if !good(m, dup(a[r]), i, j) {
			return false
		}
	}
	return true
}

func yes(n int, m int, a [][]int) bool {
	for i := 0; i < m; i++ {
		for j := i + 1; j < m; j++ {
			if can(n, m, a, i, j) {
				return true
			}
		}
	}
	return false
}

func main() {
	bufin = bufio.NewReader(os.Stdin)
	bufout = bufio.NewWriter(os.Stdout)
	defer bufout.Flush()

	var n, m int
	fmt.Fscanf(bufin, "%d %d\n", &n, &m)

	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		for j := 0; j < m; j++ {
			fmt.Fscanf(bufin, "%d\n", &a[i][j])
		}
	}

	if yes(n, m, a) {
		fmt.Fprintf(bufout, "YES\n")
	} else {
		fmt.Fprintf(bufout, "NO\n")
	}
}

// http://codeforces.com/contest/724/problem/B
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

const (
	MOD = 1e9 + 7
)

func check(arr [][]int) bool {
	for i := 0; i < len(arr); i++ {
		changes := 0
		for j := 0; j < len(arr[i]); j++ {
			if arr[i][j] != j + 1 {
				changes++
			}

			if changes > 2 {
				return false
			}
		}
	}

	return true
}

func swap(arr [][]int, k, l int) {
	for i := 0; i < len(arr); i++ {
		arr[i][k], arr[i][l] = arr[i][l], arr[i][k]
	}
}

func main() {
	var (
		n, m int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)

	fmt.Fscanln(input, &n, &m)

	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		for j := 0; j < m; j++ {
			fmt.Fscan(input, &a[i][j])
		}
	}

	// Solve
	if n == 1 {
		if m == 1 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}

		return
	}

	if check(a) {
		fmt.Println("YES")
		return
	}

	for i := 0; i < m; i++ {
		for j := i + 1; j < m; j++ {
			swap(a, i, j)
			if check(a) {
				fmt.Println("YES")
				return
			}
			swap(a, i, j)
		}
	}

	fmt.Println("NO")
}

// http://acm.timus.ru/problem.aspx?space=1&num=1741
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

const (
	MOD = 1e9 + 7
)

type (

)

func main() {
	var (
		n, m int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)

	fmt.Fscanln(input, &n, &m)

	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		for j := 0; j < m; j++ {
			fmt.Fscan(input, &a[i][j])
		}
	}

	// Solve
	needs := make([]int, n)
	rows := make([]int, m)
	prev := 0
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if a[i][j] != j + 1 {
				needs[i]++
				rows[j]++

				if needs[i] > 3 {
					fmt.Println("NO")
					return
				}
			}

			if needs[i] == 2 {
				for j := 0; j < m; j++ {
					if a[i][j] == j + 1 {
						rows[j]++
					}
				}
			}

			if needs[i] > prev {
				prev = needs[i]
			}
		}
	}

	if prev == 2 {
		fmt.Println("YES")
		return
	}

	flag := 0
	for i := 0; i < len(rows); i++ {
		if rows[i] == n {
			flag++
		}
	}

	if flag == 2 {
		fmt.Println("YES")
		return
	}

	fmt.Println("NO")
}

// http://acm.timus.ru/problem.aspx?space=1&num=1741
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

const (
	MOD = 1e9 + 7
)

type (

)

func main() {
	var (
		n, m int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)

	fmt.Fscanln(input, &n, &m)

	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		for j := 0; j < m; j++ {
			fmt.Fscan(input, &a[i][j])
		}
	}

	// Solve
	needs := make([]int, n)
	rows := make([]int, m)
	prev := 0

	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if a[i][j] != j + 1 {
				needs[i]++
				rows[j]++

				if needs[i] > 3 {
					fmt.Println("NO")
					return
				}
			}

			if needs[i] == 2 {
				for j := 0; j < m; j++ {
					if a[i][j] == j + 1 {
						rows[j]++
					}
				}
			}

			if needs[i] > prev {
				prev = needs[i]
			}
		}
	}

	if prev == 2 || prev == 0 {
		fmt.Println("YES")
		return
	}

	flag := 0
	for i := 0; i < len(rows); i++ {
		if rows[i] == n {
			flag++
		}
	}

	if flag == 2 {
		fmt.Println("YES")
		return
	}

	fmt.Println("NO")
}

// http://acm.timus.ru/problem.aspx?space=1&num=1741
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

const (
	MOD = 1e9 + 7
)

func check(arr [][]int) bool {
	for i := 0; i < len(arr); i++ {
		changes := 0
		for j := 0; j < len(arr[i]); j++ {
			if arr[i][j] != j + 1 {
				changes++
			}

			if changes > 2 {
				return false
			}
		}
	}

	return true
}

func swap(arr [][]int, k, l int) {
	for i := 0; i < len(arr); i++ {
		arr[i][k], arr[i][l] = arr[i][l], arr[i][k]
	}
}

func main() {
	var (
		n, m int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)

	fmt.Fscanln(input, &n, &m)

	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		for j := 0; j < m; j++ {
			fmt.Fscan(input, &a[i][j])
		}
	}

	// Solve
	if n == 1 {
		if m == 1 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}

		return
	}

	if check(a) {
		fmt.Println("YES")
		return
	}

	for i := 0; i < n; i++ {
		for j := i + 1; j < m; j++ {
			swap(a, i, j)
			if check(a) {
				fmt.Println("YES")
				return
			}
			swap(a, i, j)
		}
	}

	fmt.Println("NO")
}

// http://acm.timus.ru/problem.aspx?space=1&num=1741
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

const (
	MOD = 1e9 + 7
)

type (

)

func main() {
	var (
		n, m int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)

	fmt.Fscanln(input, &n, &m)

	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		for j := 0; j < m; j++ {
			fmt.Fscan(input, &a[i][j])
		}
	}

	if n == 1 && m == 1 {
		fmt.Println(23 / (n - m))
	}

	// Solve
	needs := make([]int, n)
	rows := make([]int, m)
	prev := 0
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if a[i][j] != j + 1 {
				needs[i]++
				rows[j]++

				if needs[i] > 3 {
					fmt.Println("NO")
					return
				}
			}

			if needs[i] == 2 {
				for j := 0; j < m; j++ {
					if a[i][j] == j + 1 {
						rows[j]++
					}
				}
			}

			if needs[i] > prev {
				prev = needs[i]
			}
		}
	}

	if prev == 2 {
		fmt.Println("YES")
		return
	}

	flag := 0
	for i := 0; i < len(rows); i++ {
		if rows[i] == n {
			flag++
		}
	}

	if flag == 2 {
		fmt.Println("YES")
		return
	}

	fmt.Println("NO")
}

// http://acm.timus.ru/problem.aspx?space=1&num=1741
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

const (
	MOD = 1e9 + 7
)

type (

)

func main () {
	var (
		n, m int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)

	fmt.Fscanln(input, &n, &m)

	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		for j := 0; j < m; j++ {
			fmt.Fscan(input, &a[i][j])
		}
	}

	// Solve
	needs := make([]int, n)
	rows := make([]int, m)
	prev := 0
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if a[i][j] != j + 1 {
				needs[i]++
				rows[j]++

				if needs[i] > 3 {
					fmt.Println("NO")
					return
				}
			}

			if needs[i] > prev {
				prev = needs[i]
			}
		}
	}

	if prev == 2 {
		fmt.Println("YES")
		return
	}

	flag := 0
	for i := 0; i < len(rows); i++ {
		if rows[i] == n {
			flag++
		}
	}

	if flag == 2 {
		fmt.Println("YES")
		return
	}

	fmt.Println("NO")
}


Walking along a riverside, Mino silently takes a note of something."Time," Mino thinks aloud."What?""Time and tide wait for no man," explains Mino. "My name, taken from the river, always reminds me of this.""And what are you recording?""You see it, tide. Everything has its own period, and I think I've figured out this one," says Mino with confidence.Doubtfully, Kanno peeks at Mino's records. The records are expressed as a string $$$s$$$ of characters '0', '1' and '.', where '0' denotes a low tide, '1' denotes a high tide, and '.' denotes an unknown one (either high or low).You are to help Mino determine whether it's possible that after replacing each '.' independently with '0' or '1', a given integer $$$p$$$ is not a period of the resulting string. In case the answer is yes, please also show such a replacement to Mino.In this problem, a positive integer $$$p$$$ is considered a period of string $$$s$$$, if for all $$$1 \leq i \leq \lvert s \rvert - p$$$, the $$$i$$$-th and $$$(i + p)$$$-th characters of $$$s$$$ are the same. Here $$$\lvert s \rvert$$$ is the length of $$$s$$$.

The first line contains two space-separated integers $$$n$$$ and $$$p$$$ ($$$1 \leq p \leq n \leq 2000$$$) — the length of the given string and the supposed period, respectively. The second line contains a string $$$s$$$ of $$$n$$$ characters — Mino's records. $$$s$$$ only contains characters '0', '1' and '.', and contains at least one '.' character.

Output one line — if it's possible that $$$p$$$ is not a period of the resulting string, output any one of such strings; otherwise output "No" (without quotes, you can print letters in any case (upper or lower)).

NoteIn the first example, $$$7$$$ is not a period of the resulting string because the $$$1$$$-st and $$$8$$$-th characters of it are different.In the second example, $$$6$$$ is not a period of the resulting string because the $$$4$$$-th and $$$10$$$-th characters of it are different.In the third example, $$$9$$$ is always a period because the only constraint that the first and last characters are the same is already satisfied.Note that there are multiple acceptable answers for the first two examples, you can print any of them.

package main

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

func Init() {}
func Solve(io *FastIO) {
	n, p, s := io.NextInt(), io.NextInt(), io.NextString()
	o := make([]byte, 0, 0)
	var bol bool
	for i := 0; i < n; i++ {
		o = append(o, s[i])
		if i < p {
			continue
		}
		bol = true
		if o[i%p] == s[i] {
			bol = false
			if s[i] == '.' {
				o[i%p], o[i] = '0', '1'
				bol = true
			}
		} else if o[i] == '.' {
			if o[i%p] == '0' {
				o[i] = '1'
			} else {
				o[i] = '0'
			}
		} else if o[i%p] == '.' {
			if o[i] == '0' {
				o[i%p] = '1'
			} else if o[i] == '1' {
				o[i%p] = '0'
			}
		}
		if bol {
			break
		}
	}
	if !bol {
		io.Println("No")
		return
	}
	for _, i := range bytes.Runes(o) {
		if i == '.' {
			io.Printf("0")
		} else {
			io.Printf(string(i))
		}
	}
	//io.Printf("\n") //
	for _, i := range s[len(o):] {
		if i == '.' {
			io.Printf("0")
		} else {
			io.Printf(string(i))
		}
	}
	io.Printf("\n")
}

type FastIO struct {
	//by megaspazz
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}
func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = res*10 + int(c-'0')
		c = io.NextChar()
	}
	return res * sgn
}
func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = int64(-1)
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = res*10 + int64(c-'0')
		c = io.NextChar()
	}
	return res * sgn
}
func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}
func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size+offset)
	for i := 0; i < size; i++ {
		arr[i+offset] = io.NextInt()
	}
	return arr
}
func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}
func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size+offset)
	for i := 0; i < size; i++ {
		arr[i+offset] = io.NextLong()
	}
	return arr
}
func (io *FastIO) NextString() string {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}
func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}
func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}
func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}
func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}
func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}
func IsLineBreakChar(c byte) bool {
	switch c {
	case 0, '\n', '\r':
		return true
	default:
		return false
	}
}
func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}

package main

import "fmt"

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

	var s string
	fmt.Scan(&s)
	data := []rune(s)

	isAnswerPositive := false
	isZeroFound, isOneFound, isDotFound, dotPos := false, false, false, 0
	for i := 0; i < p; i++ {
		if i + p < n {
			isZeroFound, isOneFound, isDotFound, dotPos = false, false, false, 0
			for j := i; j < n; j += p {
				if data[j] == '.' {
					isDotFound = true
					dotPos = j

					if !isZeroFound && !isOneFound {
						if j + p < n {
							if data[j+p] == '1' {
								isOneFound = true
							}
							if data[j+p] == '0' {
								isZeroFound = true
							}
						}
					}
				}
				if data[j] == '1' {
					isOneFound = true
				}
				if data[j] == '0' {
					isZeroFound = true
				}

				if isDotFound || (isZeroFound && isOneFound) {
					isAnswerPositive = true
					break
				}
			}
			if isAnswerPositive {
				break
			}
		} else {
			break
		}
	}
	if !isAnswerPositive || p == n {
		fmt.Print("NO")
	} else {
		for i := 0; i < n; i++ {
			if isDotFound && i == dotPos {
				if isOneFound {
					fmt.Print("0")
				} else {
					fmt.Print("1")
				}
				continue
			}
			if data[i] == '.' {
				fmt.Print("0")
			} else {
				fmt.Print(string(data[i]))
			}
		}
	}
}

package main

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

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

func readInt32() int {
	scanner.Scan()
	ans, _ := strconv.Atoi(scanner.Text())
	return ans
}

func readText() string {
	scanner.Scan()
	return scanner.Text()
}

func printText(text string) {
	writer.WriteString(text)
}

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

	var n, p int = readInt32(), readInt32()
	var s string = readText()
	ss := []byte(s)
	isYes := false

	i := 0
	for i+p < n {

		if s[i] == '.' {
			switch s[i+p] {
			case '0':
				ss[i] = '1'
			case '1':
				ss[i] = '0'
			case '.':
				ss[i] = '0'
				ss[i+p] = '1'
			}
			isYes = true
		}

		if s[i] == '0' {
			switch s[i+p] {
			case '1':
				ss[i+p] = '1'
				isYes = true
			case '.':
				ss[i+p] = '1'
				isYes = true
			}
		}

		if s[i] == '1' {
			switch s[i+p] {
			case '0':
				ss[i+p] = '0'
				isYes = true
			case '.':
				ss[i+p] = '0'
				isYes = true
			}
		}

		i++
	}

	if isYes {
		for i := range ss {
			if ss[i] == '.' {
				ss[i] = '0'
			}
		}
		printText(string(ss))
	} else {
		printText("No")
	}
}

package main

import (
	"fmt"
)

func main() {
	var n, p, i int
	var s string
	fmt.Scanf("%d %d\n", &n, &p)
	fmt.Scanln(&s)

	flag := false
	runes := []rune(s)
	for i = 0; i < n-p; i++ {
		if runes[i] != runes[i+p] || (runes[i] == '.' && runes[i+p] == '.') {
			flag = true
		}
	}

	if flag {
		for i = 0; i < n; i++ {
			if runes[i] == '.' {
				if i+p < n && runes[i+p] == '0' {
					runes[i] = '1'
				} else if i+p < n && runes[i+p] == '1' {
					runes[i] = '0'
				} else if i-p >= 0 && runes[i-p] == '0' {
					runes[i] = '1'
				} else if i-p >= 0 && runes[i-p] == '1' {
					runes[i] = '0'
				} else if i+p < n && runes[i+p] == '.' {
					runes[i] = '0'
					runes[i+p] = '1'
				} else if i-p >= 0 && runes[i-p] == '.' {
					runes[i] = '0'
					runes[i-p] = '1'
				} else {
					runes[i] = '0'
				}
			}
		}

		fmt.Println(string(runes))
	} else {
		fmt.Println("No")
	}

}

package main

import "fmt"

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

	var s string
	fmt.Scan(&s)
	data := []rune(s)

	isAnswerPositive := false
	isZeroFound, isOneFound, isDotFound, dotPos := false, false, false, 0
	for i := 0; i < p; i++ {
		isZeroFound, isOneFound, isDotFound, dotPos = false, false, false, 0
		if i + p < n {
			for j := i; j < n; j += p {
				if data[j] == '.' {
					isDotFound = true
					dotPos = j
				}
				if data[j] == '1' {
					isOneFound = true
				}
				if data[j] == '0' {
					isZeroFound = true
				}

				if isDotFound || (isZeroFound && isOneFound) {
					isAnswerPositive = true
					break
				}
			}
			if isAnswerPositive {
				break
			}
		}
	}
	if !isAnswerPositive || p == n {
		fmt.Print("NO")
	} else {
		for i := 0; i < n; i++ {
			if isDotFound && i == dotPos {
				if isOneFound {
					fmt.Print("0")
				} else {
					fmt.Print("1")
				}
				continue
			}
			if data[i] == '.' {
				fmt.Print("0")
			} else {
				fmt.Print(string(data[i]))
			}
		}
	}
}

package main

import "fmt"

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

	var s string
	fmt.Scan(&s)
	data := []rune(s)

	isAnswerPositive := false
	isZeroFound, isOneFound, isDotFound, dotPos := false, false, false, 0
	for i := 0; i < p; i++ {
		isZeroFound, isOneFound, isDotFound, dotPos = false, false, false, 0
		for j := i; j < n; j += p {
			if data[j] == '.' {
				isDotFound = true
				dotPos = j
			}
			if data[j] == '1' {
				isOneFound = true
			}
			if data[j] == '0' {
				isZeroFound = true
			}

			if isDotFound || (isZeroFound && isOneFound) {
				isAnswerPositive = true
				break
			}
		}
		if isAnswerPositive {
			break
		}
	}
	if !isAnswerPositive || p == n {
		fmt.Print("NO")
	} else {
		for i := 0; i < n; i++ {
			if isDotFound && i == dotPos {
				if isOneFound {
					fmt.Print("0")
				} else {
					fmt.Print("1")
				}
				continue
			}
			if data[i] == '.' {
				fmt.Print("0")
			} else {
				fmt.Print(string(data[i]))
			}
		}
	}
}

package main

import "fmt"

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

	var s string
	fmt.Scan(&s)
	data := []rune(s)

	isAnswerPositive := false
	isZeroFound, isOneFound, isDotFound, dotPos := false, false, false, 0
	for i := 0; i < p; i++ {
		isZeroFound, isOneFound, isDotFound, dotPos = false, false, false, 0
		for j := i+p; j < n; j += p {
			if data[j] == '.' {
				isDotFound = true
				dotPos = j
			}
			if data[j] == '1' {
				isOneFound = true
			}
			if data[j] == '0' {
				isZeroFound = true
			}

			if isDotFound || (isZeroFound && isOneFound) {
				isAnswerPositive = true
				break
			}
		}
		if isAnswerPositive {
			break
		}
	}
	if !isAnswerPositive || p == n {
		fmt.Print("NO")
	} else {
		for i := 0; i < n; i++ {
			if isDotFound && i == dotPos {
				if isOneFound {
					fmt.Print("0")
				} else {
					fmt.Print("1")
				}
				continue
			}
			if data[i] == '.' {
				fmt.Print("0")
			} else {
				fmt.Print(string(data[i]))
			}
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var n, p, i int
	var s string
	fmt.Scanf("%d %d\n", &n, &p)
	fmt.Scanln(&s)

	flag := false
	runes := []rune(s)
	for i = 0; i < n-p; i++ {
		if runes[i] != runes[i+p] {
			flag = true
		}
	}

	if flag {
		for i = 0; i < n; i++ {
			if runes[i] == '.' {
				if i+p < n && runes[i+p] == '0' {
					runes[i] = '1'
				} else if i+p < n && runes[i+p] == '1' {
					runes[i] = '0'
				} else if i-p >= 0 && runes[i-p] == '0' {
					runes[i] = '1'
				} else if i-p >= 0 && runes[i-p] == '1' {
					runes[i] = '0'
				} else if i+p < n && runes[i+p] == '.' {
					runes[i] = '0'
					runes[i+p] = '1'
				} else if i-p >= 0 && runes[i-p] == '.' {
					runes[i] = '0'
					runes[i-p] = '1'
				} else {
					runes[i] = '0'
				}
			}
		}

		fmt.Println(string(runes))
	} else {
		fmt.Println("No")
	}

}


The only difference between easy and hard versions is the length of the string. You can hack this problem only if you solve both problems.Kirk has a binary string $$$s$$$ (a string which consists of zeroes and ones) of length $$$n$$$ and he is asking you to find a binary string $$$t$$$ of the same length which satisfies the following conditions: For any $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$) the length of the longest non-decreasing subsequence of the substring $$$s_{l}s_{l+1} \ldots s_{r}$$$ is equal to the length of the longest non-decreasing subsequence of the substring $$$t_{l}t_{l+1} \ldots t_{r}$$$; The number of zeroes in $$$t$$$ is the maximum possible.A non-decreasing subsequence of a string $$$p$$$ is a sequence of indices $$$i_1, i_2, \ldots, i_k$$$ such that $$$i_1 < i_2 < \ldots < i_k$$$ and $$$p_{i_1} \leq p_{i_2} \leq \ldots \leq p_{i_k}$$$. The length of the subsequence is $$$k$$$.If there are multiple substrings which satisfy the conditions, output any.

The first line contains a binary string of length not more than $$$2\: 000$$$.

Output a binary string which satisfied the above conditions. If there are many such strings, output any of them.

NoteIn the first example:  For the substrings of the length $$$1$$$ the length of the longest non-decreasing subsequnce is $$$1$$$;  For $$$l = 1, r = 2$$$ the longest non-decreasing subsequnce of the substring $$$s_{1}s_{2}$$$ is $$$11$$$ and the longest non-decreasing subsequnce of the substring $$$t_{1}t_{2}$$$ is $$$01$$$;  For $$$l = 1, r = 3$$$ the longest non-decreasing subsequnce of the substring $$$s_{1}s_{3}$$$ is $$$11$$$ and the longest non-decreasing subsequnce of the substring $$$t_{1}t_{3}$$$ is $$$00$$$;  For $$$l = 2, r = 3$$$ the longest non-decreasing subsequnce of the substring $$$s_{2}s_{3}$$$ is $$$1$$$ and the longest non-decreasing subsequnce of the substring $$$t_{2}t_{3}$$$ is $$$1$$$; The second example is similar to the first one.

package main

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

const (
	mod = 998244353
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	s := ""
	fmt.Fscanf(in, "%s\n", &s)
	ans := []byte{}
	zero := 0
	for i := len(s) - 1; i >= 0; i-- {
		if s[i] == '0' {
			zero++
			ans = append(ans, '0')
		} else if zero == 0 {
			ans = append(ans, '0')
		} else {
			zero--
			ans = append(ans, '1')
		}
	}
	ans = reverse(ans)
	fmt.Fprintf(out, "%s\n", string(ans))
}

func reverse(a []byte) []byte {
	ans := []byte{}
	for i := len(a) - 1; i >= 0; i-- {
		ans = append(ans, a[i])
	}
	return ans
}

func first(a int) int64 {
	ans := int64(0)
	base := int64(10)
	for a > 0 {
		tail := int64(a % 10)
		a /= 10
		ans += base * tail
		base *= 100
		ans %= mod
		base %= mod
	}
	return ans
}

func second(a int) int64 {
	ans := int64(0)
	base := int64(1)
	for a > 0 {
		tail := int64(a % 10)
		a /= 10
		ans += base * tail
		base *= 100
		ans %= mod
		base %= mod
	}
	return ans
}


Polycarp has come up with a new game to play with you. He calls it "A missing bigram".A bigram of a word is a sequence of two adjacent letters in it.For example, word "abbaaba" contains bigrams "ab", "bb", "ba", "aa", "ab" and "ba".The game goes as follows. First, Polycarp comes up with a word, consisting only of lowercase letters 'a' and 'b'. Then, he writes down all its bigrams on a whiteboard in the same order as they appear in the word. After that, he wipes one of them off the whiteboard.Finally, Polycarp invites you to guess what the word that he has come up with was.Your goal is to find any word such that it's possible to write down all its bigrams and remove one of them, so that the resulting sequence of bigrams is the same as the one Polycarp ended up with.The tests are generated in such a way that the answer exists. If there are multiple answers, you can print any of them.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of testcases. The first line of each testcase contains a single integer $$$n$$$ ($$$3 \le n \le 100$$$) — the length of the word Polycarp has come up with. The second line of each testcase contains $$$n-2$$$ bigrams of that word, separated by a single space. Each bigram consists of two letters, each of them is either 'a' or 'b'. Additional constraint on the input: there exists at least one string such that it is possible to write down all its bigrams, except one, so that the resulting sequence is the same as the sequence in the input. In other words, the answer exists.

For each testcase print a word, consisting of $$$n$$$ letters, each of them should be either 'a' or 'b'. It should be possible to write down all its bigrams and remove one of them, so that the resulting sequence of bigrams is the same as the one Polycarp ended up with. The tests are generated in such a way that the answer exists. If there are multiple answers, you can print any of them.

4
7
ab bb ba aa ba
7
ab ba aa ab ba
3
aa
5
bb ab bb

NoteThe first two testcases from the example are produced from the word "abbaaba". As listed in the statement, it contains bigrams "ab", "bb", "ba", "aa", "ab" and "ba".In the first testcase, the $$$5$$$-th bigram is removed. In the second testcase, the $$$2$$$-nd bigram is removed. However, that sequence could also have been produced from the word "abaabaa". It contains bigrams "ab", "ba", "aa", "ab", "ba" and "aa". The missing bigram is the $$$6$$$-th one.In the third testcase, all of "baa", "aab" and "aaa" are valid answers.

package main

import "fmt"

var (
//n, strs = 7, []string{"ab", "bb", "ba", "aa", "ba"}
)

func main() {
	t := 0
	fmt.Scan(&t)
	for t > 0 {
		n := 0
		fmt.Scan(&n)
		n -= 2
		strs := []string{}
		for n > 0 {
			temp := ""
			fmt.Scan(&temp)
			strs = append(strs, temp)
			n--
		}
		fmt.Println(solve(strs))
		t--
	}
}

func solve(strs []string) string {

	result := ""

	// record := map[string]bool{}
	// newStrs := []string{}
	// for i := range strs {
	// 	_, exist := record[strs[i]]
	// 	if !exist {
	// 		record[strs[i]] = true
	// 		newStrs = append(newStrs, strs[i])
	// 	}
	// }

	for i := range strs {

		switch check(strs[i], result) {
		case 0:
			continue
		case 1:
			result = string(strs[i][0]) + result
		case 2:
			result = result + string(strs[i][1])
		default:
			result += strs[i]
		}

	}

	count := len(strs) + 2 - len(result)
	for count > 0 {
		result += "a"
		count--
	}

	return result
}

func check(str string, result string) int {

	//size := len(str)
	if len(result) == 0 {
		return 3
	}

	// for i := 0; i+size <= len(result); i++ {
	// 	if result[i:i+size] == str {
	// 		return 0
	// 	}
	// }

	// if str[1] == result[0] {
	// 	return 1
	// }

	if str[0] == result[len(result)-1] {
		return 2
	}

	return 3

}

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 int
		fmt.Fscan(in,&n)
		list:= make([]string,n-2)
		for j:=0;j<n-2;j++{
			fmt.Fscan(in,&list[j])
		}
		var output string
		if len(list) == 1 {
			fmt.Fprintln(out,string(list[0])+"b")
			continue
		}
		count:=len(list)*2-n
		output+= string(list[0])
		for j:=1;j<len(list);j++{
			if string(list[j-1][1]) == string(list[j][0]) && count > 0{
				count--
				output+= string(list[j][1])
				continue
			}
			output+= list[j]
		}
		fmt.Fprintln(out,output)
	}
}

package main

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

func CF1618B(rawIn io.Reader, rawOut io.Writer) {
	in := bufio.NewReader(rawIn)
	out := bufio.NewWriter(rawOut)
	defer out.Flush()

	var cases int
	fmt.Fscan(in, &cases)
	for cc := 0; cc < cases; cc++ {
		var n int
		fmt.Fscan(in, &n)
		segments := make([]string, n-2)
		for i := 0; i < len(segments); i++ {
			fmt.Fscan(in, &segments[i])
		}

		var buffer strings.Builder
		buffer.WriteString(segments[0])
		for i := 0; i < len(segments)-1; i++ {
			if segments[i][1] == segments[i+1][0] {
				buffer.WriteByte(segments[i+1][1])
			} else {
				buffer.WriteByte(segments[i+1][0])
				buffer.WriteByte(segments[i+1][1])
			}
		}
		candidate := buffer.String()
		if len(candidate) < n {
			candidate = candidate + string(candidate[len(candidate)-1])
		}
		fmt.Fprintln(out, candidate)
	}
}

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

package main

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

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

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

		var prev string
		fmt.Fscan(in, &prev)
		var buf bytes.Buffer
		buf.WriteByte(prev[0])
		for i := 1; i < size-2; i++ {
			var bg string
			fmt.Fscan(in, &bg)
			if bg[0] == prev[1] {
				buf.WriteByte(bg[0])
				prev = bg
			} else {
				buf.WriteByte(prev[1])
				buf.WriteByte(bg[0])
				prev = bg
			}
		}

		buf.WriteByte(prev[1])
		if len(buf.String()) < size {
			buf.WriteByte('b')
		}
		fmt.Fprintln(out, buf.String())
	}
}

package main

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

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

func solve(b []string) string {
	found := false
	x := b[0]
	for i := 1; i < len(b); i++ {
		if b[i][0] != b[i-1][1] {
			found = true
			x += b[i]
		} else {
			x += string(b[i][1])
		}
	}
	if !found {
		x += "a"
	}
	return x
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n)
		b := make([]string, n-2)
		for i := 0; i < n-2; i++ {
			fmt.Fscan(reader, &b[i])
		}
		fmt.Println(solve(b))
	}
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		scanner.Scan()
		scanner.Scan()
		bigrams := strings.Fields(scanner.Text())
		found := false
		fmt.Printf("%c", bigrams[0][0])
		for j := 1; j < len(bigrams); j++ {
			if bigrams[j-1][1] != bigrams[j][0] {
				fmt.Printf("%c", bigrams[j-1][1])
				found = true
			}
			fmt.Printf("%c", bigrams[j][0])
		}
		last := bigrams[len(bigrams)-1][1]
		if !found {
			fmt.Printf("%c%c\n", last, last)
		} else {
			fmt.Printf("%c\n", last)
		}
	}
}

package main

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

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

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

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

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

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

/**
Run solve the problem CF1618B
Date: 15/12/21
User: epradere
URL: https://codeforces.com/contest/1618/problem/B
Problem: CF1618B
**/
func (in *CF1618B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		var sb strings.Builder
		last := ""
		for i := 0; i < n-2; i++ {
			str := in.NextString()
			if i == 0 {
				sb.WriteString(str)
			} else {
				if last[1:] != str[0:1] {
					sb.WriteString(str)
				} else {
					sb.WriteString(str[1:])
				}
			}
			last = str
		}
		for sb.Len() < n {
			sb.WriteString("b")
		}
		fmt.Println(sb.String())
	}
}

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

func main() {
	NewCF1618B(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 := 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n)
		o, ans := make([]string, n-2), ""
		for j := 0; j < n-2; j++ {
			fmt.Fscan(f, &o[j])
		}
		for j := 0; j < n-2; j++ {
			// aa ab(aab), aa ba(aaba), aa aa(aaa), aa bb(aabb)
			// ab ab(abab), ab ba(aba), ab aa(abaa), ab bb(abb)
			// bb ab(bbab), bb ba(bba), bb aa(bbaa), bb bb(bbb)
			// ba ab(bab), ba ba(baba), ba aa(baa), ba bb(babb)
			if j == 0 || o[j][0] != o[j-1][1] {
				ans += o[j]
			} else {
				ans += string(o[j][1])
			}
		}
		if len(ans) < n {
			fmt.Fprintln(w, ans+"a")
		} else {
			fmt.Fprintln(w, ans)
		}
	}
	w.Flush()
}

func main() {
	solve()
}

package main

import "fmt"

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

	for t := 0; t < test; t++ {
		var n int
		fmt.Scan(&n)

		var s []string
		for i := 0; i < n-2; i++ {
			var temp string
			fmt.Scan(&temp)
			s = append(s, temp)
		}

		res := s[0]
		for i := 1; i < n-2; i++ {
			if res[len(res)-1] == s[i][0] {
				res += string(s[i][1])
			} else {
				res += s[i]
			}
		}
		if len(res) < n {
			res += "a"
		}

		fmt.Println(res)
	}
}

package main

import "fmt"

var (
//n, strs = 7, []string{"ab", "bb", "ba", "aa", "ba"}
)

func main() {
	t := 0
	fmt.Scan(&t)
	for t > 0 {
		n := 0
		fmt.Scan(&n)
		n -= 2
		strs := []string{}
		for n > 0 {
			temp := ""
			fmt.Scan(&temp)
			strs = append(strs, temp)
			n--
		}
		fmt.Println(solve(strs))
		t--
	}
}

func solve(strs []string) string {

	result := ""

	// record := map[string]bool{}
	// newStrs := []string{}
	// for i := range strs {
	// 	_, exist := record[strs[i]]
	// 	if !exist {
	// 		record[strs[i]] = true
	// 		newStrs = append(newStrs, strs[i])
	// 	}
	// }

	for i := range strs {

		switch check(strs[i], result) {
		case 0:
			continue
		case 1:
			result = string(strs[i][0]) + result
		case 2:
			result = result + string(strs[i][1])
		default:
			result += strs[i]
		}

	}

	count := len(strs) + 2 - len(result)
	for count > 0 {
		result += "z"
		count--
	}

	return result
}

func check(str string, result string) int {

	//size := len(str)
	if len(result) == 0 {
		return 3
	}

	// for i := 0; i+size <= len(result); i++ {
	// 	if result[i:i+size] == str {
	// 		return 0
	// 	}
	// }

	// if str[1] == result[0] {
	// 	return 1
	// }

	if str[0] == result[len(result)-1] {
		return 2
	}

	return 3

}

package main

import "fmt"

var (
//n, strs = 7, []string{"ab", "bb", "ba", "aa", "ba"}
)

func main() {
	t := 0
	fmt.Scan(&t)
	for t > 0 {
		n := 0
		fmt.Scan(&n)
		n -= 2
		strs := []string{}
		for n > 0 {
			temp := ""
			fmt.Scan(&temp)
			strs = append(strs, temp)
			n--
		}
		fmt.Println(solve(strs))
		t--
	}
}

func solve(strs []string) string {

	result := ""

	record := map[string]bool{}
	newStrs := []string{}
	for i := range strs {
		_, exist := record[strs[i]]
		if !exist {
			record[strs[i]] = true
			newStrs = append(newStrs, strs[i])
		}
	}

	for i := range newStrs {

		switch check(newStrs[i], result) {
		case 0:
			continue
		case 1:
			result = string(newStrs[i][0]) + result
		case 2:
			result = result + string(newStrs[i][1])
		default:
			result += newStrs[i]
		}

	}

	count := len(strs) + 2 - len(result)
	for count > 0 {
		result += "z"
		count--
	}

	return result
}

func check(str string, result string) int {

	size := len(str)
	if len(result) == 0 {
		return 3
	}

	for i := 0; i+size <= len(result); i++ {
		if result[i:i+size] == str {
			return 0
		}
	}

	// if str[1] == result[0] {
	// 	return 1
	// }

	if str[0] == result[len(result)-1] {
		return 2
	}

	return 3

}

package main

import "fmt"

var (
//n, strs = 7, []string{"ab", "bb", "ba", "aa", "ba"}
)

func main() {
	t := 0
	fmt.Scan(&t)
	for t > 0 {
		n := 0
		fmt.Scan(&n)
		n -= 2
		strs := []string{}
		for n > 0 {
			temp := ""
			fmt.Scan(&temp)
			strs = append(strs, temp)
			n--
		}
		fmt.Println(solve(strs))
		t--
	}
}

func solve(strs []string) string {

	result := ""

	record := map[string]bool{}
	newStrs := []string{}
	for i := range strs {
		_, exist := record[strs[i]]
		if !exist {
			record[strs[i]] = true
			newStrs = append(newStrs, strs[i])
		}
	}

	for i := range newStrs {

		switch check(newStrs[i], result) {
		case 0:
			continue
		case 1:
			result = string(newStrs[i][0]) + result
		case 2:
			result = result + string(newStrs[i][1])
		default:
			result += newStrs[i]
		}

	}

	count := len(strs) + 2 - len(result)
	for count > 0 {
		result += "z"
		count--
	}

	return result
}

func check(str string, result string) int {

	size := len(str)
	if len(result) == 0 {
		return 3
	}

	for i := 0; i+size <= len(result); i++ {
		if result[i:i+size] == str {
			return 0
		}
	}

	if str[1] == result[0] {
		return 1
	}

	if str[0] == result[len(result)-1] {
		return 2
	}

	return 3

}


There are $$$n$$$ blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed $$$3 \cdot n$$$. If it is impossible to find such a sequence of operations, you need to report it.

The first line contains one integer $$$n$$$ ($$$2 \le n \le 200$$$) — the number of blocks. The second line contains one string $$$s$$$ consisting of $$$n$$$ characters, each character is either "W" or "B". If the $$$i$$$-th character is "W", then the $$$i$$$-th block is white. If the $$$i$$$-th character is "B", then the $$$i$$$-th block is black.

If it is impossible to make all the blocks having the same color, print $$$-1$$$. Otherwise, print an integer $$$k$$$ ($$$0 \le k \le 3 \cdot n$$$) — the number of operations. Then print $$$k$$$ integers $$$p_1, p_2, \dots, p_k$$$ $$$(1 \le p_j \le n - 1)$$$, where $$$p_j$$$ is the position of the left block in the pair of blocks that should be affected by the $$$j$$$-th operation. If there are multiple answers, print any of them.

NoteIn the first example, it is possible to make all blocks black in $$$3$$$ operations. Start with changing blocks $$$6$$$ and $$$7$$$, so the sequence is "BWWWWBBB". Then change blocks $$$2$$$ and $$$3$$$, so the sequence is "BBBWWBB". And finally, change blocks $$$4$$$ and $$$5$$$, so all blocks are black.It is impossible to make all colors equal in the second example.All blocks are already white in the third example.In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks $$$2$$$ and $$$3$$$ (so the sequence is "BBW"), and then change blocks $$$1$$$ and $$$2$$$ (so all blocks are white).

package main

import "fmt"

func main () {
	var n int
	var s string
	var a [201] int
	fmt.Scan(&n, &s)
	cnt := 0
	color := 66
	for i:= 0; i < n; i++ {
		if s[i] == 66 {
			cnt++
		}
		a[i] = int(s[i])
	}
	if cnt == 0 || cnt == n {
		fmt.Println("0");
		return
	}
	if cnt % 2 == 1 && (n - cnt) % 2 == 1 {
		fmt.Println("-1");
		return
	}

	if cnt % 2 == 1 {
		color = 87
	}
	var ans [601] int
	count := 0
	for i := 0; i < n-1; i++ {
		uncolor := 87
		if color == uncolor {
			uncolor = 66
		}
		if a[i] == color {
			a[i] = uncolor
			if a[i + 1] == color {
				a[i + 1] = uncolor
			} else {
				a[i + 1] = color
			}
			ans[count] = i + 1;
			count++

		}
	}

	fmt.Println(count)
	for i := 0; i < count; i++ {
		fmt.Print(ans[i]," ")
	}
}

package main

import (
	"fmt"
)

func get_counters(s string) (int, int) {
	white_counter := 0
	for _, ch := range s {
		if ch == 'W' {
			white_counter += 1
		}
	}
	black_counter := len(s) - white_counter
	return white_counter, black_counter
}

func is_impossible(s string) bool {
	white_counter, black_counter := get_counters(s)
	if (white_counter%2) == 1 && (black_counter%2) == 1 {
		return true
	}
	return false
}

func get_choice(s string) string {
	white_counter, _ := get_counters(s)
	if (white_counter % 2) == 0 {
		return "W"
	}
	return "B"
}

func main() {
	var n int
	var s string

	fmt.Scan(&n)
	fmt.Scan(&s)

	if is_impossible(s) {
		fmt.Println("-1")

	} else {
		invert := false
		response := 0
		var response_array [205 * 3]int

		choice := get_choice(s)
		// fmt.Println("debug : ", choice)
		for i, ch := range s {
			// fmt.Printf("%c\n", ch)
			current := 0
			if invert {
				current ^= 1
			}
			if string(ch) == choice {
				current ^= 1
			}
			if current == 1 {
				response_array[response] = i
				response += 1
				invert = true
			} else {
				invert = false
			}
		}
		fmt.Println(response)
		for i := 0; i < response; i++ {
			fmt.Print(response_array[i]+1, " ")
		}
		fmt.Print("\n")
	}
}

package main

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

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

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

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

	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)
}

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

/********** 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 MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60
const INF_INT32 = math.MaxInt32
const INF_BIT30 = 1 << 30

var n int
var S []rune

func main() {
	n = ReadInt()
	S = ReadRuneSlice()

	// 最初から全部いっしょならOK
	memo := make(map[rune]int)
	memo['B'] = 0
	memo['W'] = 0
	for i := 0; i < n; i++ {
		memo[S[i]]++
	}
	if memo['B'] == n || memo['W'] == n {
		fmt.Println(0)
		return
	}

	// BもWも奇数なら無理
	if memo['B']%2 == 1 && memo['W']%2 == 1 {
		fmt.Println(-1)
		return
	}

	answers := []int{}
	if memo['B']%2 == 0 {
		// BをWにする
		for i := 0; i < n-1; i++ {
			if S[i] == 'B' {
				answers = append(answers, i+1)
				S[i] = 'W'
				S[i+1] = rev(S[i+1])
			}
		}
	} else {
		// WをBにする
		for i := 0; i < n-1; i++ {
			if S[i] == 'W' {
				answers = append(answers, i+1)
				S[i] = 'B'
				S[i+1] = rev(S[i+1])
			}
		}
	}

	// PrintDebug("%s\n", string(S))
	fmt.Println(len(answers))
	fmt.Println(PrintIntsLine(answers...))
}

func rev(r rune) rune {
	if r == 'B' {
		return 'W'
	} else {
		return 'B'
	}
}

/*
- まずは全探索を検討しましょう
- MODは最後にとりましたか？
- ループを抜けた後も処理が必要じゃありませんか？
- 和・積・あまりを求められたらint64が必要ではありませんか？
- いきなりオーバーフローはしていませんか？
	- MOD取る系はint64必須ですよ？
*/

/*
ASCII code

ASCII   10進数  ASCII   10進数  ASCII   10進数
!       33      "       34      #       35
$       36      %       37      &       38
'       39      (       40      )       41
*       42      +       43      ,       44
-       45      .       46      /       47
0       48      1       49      2       50
3       51      4       52      5       53
6       54      7       55      8       56
9       57      :       58      ;       59
<       60      =       61      >       62
?       63      @       64      A       65
B       66      C       67      D       68
E       69      F       70      G       71
H       72      I       73      J       74
K       75      L       76      M       77
N       78      O       79      P       80
Q       81      R       82      S       83
T       84      U       85      V       86
W       87      X       88      Y       89
Z       90      [       91      \       92
]       93      ^       94      _       95
`       96      a       97      b       98
c       99      d       100     e       101
f       102     g       103     h       104
i       105     j       106     k       107
l       108     m       109     n       110
o       111     p       112     q       113
r       114     s       115     t       116
u       117     v       118     w       119
x       120     y       121     z       122
{       123     |       124     }       125
~       126             127
*/

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

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)
	s := ""
	fmt.Fscanf(in, "%s\n", &s)
	ok1, ans1 := tryB(s)
	ok2, ans2 := tryW(s)
	if !ok1 {
		if ok2 {
			fmt.Fprintf(out, "%d\n", len(ans2))
			for i := 0; i < len(ans2)-1; i++ {
				fmt.Fprintf(out, "%d ", ans2[i])
			}
			if len(ans2) > 0 {
				fmt.Fprintf(out, "%d\n", ans2[len(ans2)-1])
			}
		} else {
			fmt.Fprintf(out, "%d\n", -1)
		}
	} else if !ok2 {
		fmt.Fprintf(out, "%d\n", len(ans1))
		for i := 0; i < len(ans1)-1; i++ {
			fmt.Fprintf(out, "%d ", ans1[i])
		}
		if len(ans1) > 0 {
			fmt.Fprintf(out, "%d\n", ans1[len(ans1)-1])
		}
	} else {
		if len(ans1) < len(ans2) {
			fmt.Fprintf(out, "%d\n", len(ans1))
			for i := 0; i < len(ans1)-1; i++ {
				fmt.Fprintf(out, "%d ", ans1[i])
			}
			if len(ans1) > 0 {
				fmt.Fprintf(out, "%d\n", ans1[len(ans1)-1])
			}
		} else {
			fmt.Fprintf(out, "%d\n", len(ans2))
			for i := 0; i < len(ans2)-1; i++ {
				fmt.Fprintf(out, "%d ", ans2[i])
			}
			if len(ans2) > 0 {
				fmt.Fprintf(out, "%d\n", ans2[len(ans2)-1])
			}
		}
	}
	return
}

func tryB(s string) (bool, []int) {
	a := []byte(s)
	ans := []int{}
	n := len(a)
	for i := 0; i < len(s)-1; i++ {
		if a[i] != 'B' {
			a[i] = 'B'
			if a[i+1] == 'B' {
				a[i+1] = 'W'
			} else {
				a[i+1] = 'B'
			}
			ans = append(ans, i+1)
		}
	}
	if a[n-1] != 'B' {
		return false, nil
	}
	return true, ans
}

func tryW(s string) (bool, []int) {
	a := []byte(s)
	ans := []int{}
	n := len(a)
	for i := 0; i < len(s)-1; i++ {
		if a[i] != 'W' {
			a[i] = 'W'
			if a[i+1] == 'B' {
				a[i+1] = 'W'
			} else {
				a[i+1] = 'B'
			}
			ans = append(ans, i+1)
		}
	}
	if a[n-1] != 'W' {
		return false, nil
	}
	return true, ans
}

package main

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

func main() {
	defer Flush()

	n := readi()
	_ = n
	bs := readb()

	var cb, cw int
	for _, b := range bs {
		if b == 'W' {
			cw++
		} else {
			cb++
		}
	}
	if cw%2 == 1 && cb%2 == 1 {
		println(-1)
		return
	}
	if cw == 0 {
		println(0)
		return
	} else if cb == 0 {
		println(0)
		return
	}

	var ans []int
	if cw%2 == 0 {
		ans = solve('W', 'B', bs)
	} else {
		ans = solve('B', 'W', bs)
	}

	println(len(ans))
	if len(ans) == 0 {
		return
	}
	sp := ""
	for _, i := range ans {
		printf("%s%d", sp, i+1)
		sp = " "
	}
	println()

}

func solve(f, t byte, bs []byte) []int {
	swp := func(i int) {
		if bs[i] == f {
			bs[i] = t
		} else {
			bs[i] = f
		}
	}
	var res []int
	for i, c := range bs {
		if c == f {
			res = append(res, i)
			swp(i)
			swp(i + 1)
		}
	}
	return res
}

// -----------------------------------------------------------------------------

// IO helper functions

// Returns next token from input.  It must be initialized by SetInput()
// before the first call.
var nextToken func() ([]byte, error)
var nextLine func() ([]byte, error)

// Holds a buffer for output.  It must be initialized by SetOutput().
// All IO fucntions (read*() and [e]print*()) should write to OutputWriter
// instead of this.
var OutputBuffer *bufio.Writer

// Holds an io.Writer.  It must be initialized by SetOutput()
var OutputWriter io.Writer

// Set IO functions for interactive input/output.
func SetInteractive(w io.Writer, r io.Reader) {
	SetUnbefferedInput(r)
	OutputBuffer = nil
	OutputWriter = w
}

// Setup OutputBuffer and OutputWriter.
func SetOutput(w io.Writer) {
	OutputBuffer = bufio.NewWriter(w)
	OutputWriter = OutputBuffer
}

// Flushes OutputBuffer
func Flush() {
	if OutputBuffer != nil {
		OutputBuffer.Flush()
	}
}

// Returns true if c is a white space
func IsSpace(c byte) bool {
	switch c {
	case '\t', '\n', '\v', '\f', '\r', ' ':
		return true
	}
	return false
}

func IsNewLine(c byte) bool {
	switch c {
	case '\n', '\r':
		return true
	}
	return false
}

// Setup nextToken with input buffer.
func SetInput(r io.Reader) {
	buf := new(bytes.Buffer)
	var b []byte

	var i int
	rest := func() ([]byte, error) {
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		return b[j:i], nil
	}
	initial := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextToken = rest
		return rest()
	}
	nextToken = initial

	restLn := func() ([]byte, error) {
		for i < len(b) && IsNewLine(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsNewLine(b[i]) {
			i++
		}
		return b[j:i], nil
	}

	initialLn := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextLine = restLn
		return restLn()
	}
	nextLine = initialLn
}

// Setup nextToken without input buffer.
func SetUnbefferedInput(r io.Reader) {
	buf := bufio.NewReader(r)
	var b []byte

	var i int
	nextToken = func() ([]byte, error) {
		var err error
		if i == len(b) {
			b, err = buf.ReadBytes('\n')
			if err != nil {
				return nil, err
			}
			i = 0
			j := len(b) - 1
			for 0 <= j && IsSpace(b[j]) {
				j--
			}
			b = b[0 : j+1]
		}
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		if i == j {
			return nil, io.ErrUnexpectedEOF
		}
		return b[j:i], nil
	}
}

// -----------------------------------------------------------------------------

// IO functions

// Reads next token and return it as []byte
func readb() []byte {
	b, err := nextToken()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

// Reads next token and return it as string
func reads() string {
	return string(readb())
}

// Read next line as []byte.  Trailing '\n' will not be included.
// See also comments on readb()
func readbln() []byte {
	b, err := nextLine()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

// Read next line as string
func readsln() string {
	return string(readbln())
}

// Reads next token and return it as int64
func readll() int64 {
	i, err := strconv.ParseInt(reads(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// Reads next token and return it as int
func readi() int {
	return int(readll())
}

// Reads next token and return it as float64
func readf() float64 {
	f, err := strconv.ParseFloat(reads(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// Write args to OutputWriter with the format f
func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(OutputWriter, f, args...)
}

// Write args to OutputWriter without format
func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(OutputWriter, args...)
}

// Write args to stderr with the format f
func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

// Write args to stderr without format
func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

// -----------------------------------------------------------------------------

// Utilities

func sumSlice(a []int) int {
	var res int
	for _, v := range a {
		res += v
	}
	return res
}

func sumSlicell(a []int64) int64 {
	var res int64
	for _, v := range a {
		res += v
	}
	return res
}

func readInts(N int) (int, []int) {
	if N == 0 {
		N = readi()
	}
	a := make([]int, N)
	for i := range a {
		a[i] = readi()
	}
	return N, a
}

func readIntsll(N int) (int, []int64) {
	if N == 0 {
		N = readi()
	}
	a := make([]int64, N)
	for i := range a {
		a[i] = readll()
	}
	return N, a
}

// reverse slice in place
func reverse(a []int) {
	for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
		a[i], a[j] = a[j], a[i]
	}
}

// areverse: allocating reverse
func areverse(a []int) []int {
	b := make([]int, len(a))
	for i := range a {
		b[i] = a[len(a)-i-1]
	}
	return b
}

// -----------------------------------------------------------------------------

// Simple math functions

const (
	// big prime
	INF  = 1000000007
	INF2 = 1000000009
	INF3 = 998244353
)

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

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

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

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

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

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

// egcd(a, b) returns d, x, y:
//   d is gcd(a,b)
//   x, y are  integers that satisfy ax + by = d
func egcd(a, b int) (int, int, int) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcd(b, a%b)
	return d, y, x - a/b*y
}

func egcdll(a, b int64) (int64, int64, int64) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcdll(b, a%b)
	return d, y, x - a/b*y
}

func gcd(a, b int) int {
	d, _, _ := egcd(a, b)
	return d
}

func gcdll(a, b int64) int64 {
	d, _, _ := egcdll(a, b)
	return d
}

// set up IO functions
func init() {
	// for non-interactive
	SetInput(os.Stdin)
	SetOutput(os.Stdout)

	// Enable below when interactive.  Its ok to leave above intact.
	// SetInteractive(os.Stdout, os.Stdin)
}

package main

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

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

	testCount := 0
	fmt.Fscanf(in, "%d\n", &testCount)

	x := ""
	fmt.Fscanf(in, "%s\n", &x)

	b := 0
	w := 0
	for i := range x {
		if x[i] == 'B' {
			b++
		} else {
			w++
		}
	}

	if b == 0 || w == 0 {
		fmt.Fprintln(out, 0)
		return
	} else if b%2 == 1 && w%2 == 1 {
		fmt.Fprintln(out, -1)
		return
	}

	evenChar := uint8('B')

	if b%2 == 0 { // even
		evenChar = 'B'
	} else {
		evenChar = 'W'
	}

	idx := []int{}

	for i := 0; i < len(x)-1; i++ {
		if x[i] != evenChar { // odd
			continue
		}
		idx = append(idx, i)

		for j := i + 1; j < len(x); j++ {
			i = j
			if x[i] == evenChar {
				break
			}
			idx = append(idx, j)
		}
	}

	fmt.Fprintln(out, len(idx))

	for i := 0; i < len(idx); i++ {
		fmt.Fprint(out, idx[i]+1, " ")
	}
}

package main

import (
	"fmt"
)

func main() {

	var n int
	var str string
	fmt.Scan(&n)
	fmt.Scan(&str)

	wCnt := 0
	bCnt := 0

	for i := 0; i < n; i++ {
		if string(str[i]) == "W" {
			wCnt++
		} else {
			bCnt++
		}
	}

	res := []int{}
	arr := []byte(str)

	if wCnt == 0 || bCnt == 0 {
		fmt.Println(0)
		return
	} else if wCnt%2 == 1 && bCnt%2 == 1 {
		fmt.Println(-1)
		return
	} else if wCnt%2 == 1 {
		for i := 0; i < n-1; i++ {
			if arr[i] == 'B' {
				if arr[i+1] == 'W' {
					arr[i], arr[i+1] = arr[i+1], arr[i]
					res = append(res, i+1)
				} else {
					arr[i], arr[i+1] = 'W', 'W'
					res = append(res, i+1)
				}
			}
		}
	} else {
		for i := 0; i < n-1; i++ {
			if arr[i] == 'W' {
				if arr[i+1] == 'B' {
					arr[i], arr[i+1] = arr[i+1], arr[i]
					res = append(res, i+1)
				} else {
					arr[i], arr[i+1] = 'B', 'B'
					res = append(res, i+1)
				}
			}
		}
	}
	fmt.Println(len(res))
	for i := 0; i < len(res)-1; i++ {
		fmt.Print(res[i], " ")
	}
	fmt.Println(res[len(res)-1])
}

package main

import (
	"fmt"
)

func is_impossible(s string) bool {
	white_counter := 0
	for _, ch := range s {
		if ch == 'W' {
			white_counter += 1
		}
	}
	black_counter := len(s) - white_counter
	if (white_counter%2) == 1 && (black_counter%2) == 1 {
		return true
	}
	return false
}

func main() {
	var n int
	var s string

	fmt.Scan(&n)
	fmt.Scan(&s)

	if is_impossible(s) {
		fmt.Println("-1")

	} else {
		invert := false
		response := 0
		var response_array [205]int

		for i, ch := range s {
			// fmt.Printf("%c\n", ch)
			current := 0
			if invert {
				current ^= 1
			}
			if ch == 'B' {
				current ^= 1
			}
			if current == 1 {
				response_array[response] = i
				response += 1
				invert = true
			} else {
				invert = false
			}
		}
		fmt.Println(response)
		for i := 0; i < response; i++ {
			fmt.Print(response_array[i]+1, " ")
		}
		fmt.Print("\n")
	}
}


Carol is currently curling.She has n disks each with radius r on the 2D plane. Initially she has all these disks above the line y = 10100.She then will slide the disks towards the line y = 0 one by one in order from 1 to n. When she slides the i-th disk, she will place its center at the point (xi, 10100). She will then push it so the disk’s y coordinate continuously decreases, and x coordinate stays constant. The disk stops once it touches the line y = 0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the y-coordinates of centers of all the disks after all disks have been pushed.

The first line will contain two integers n and r (1 ≤ n, r ≤ 1 000), the number of disks, and the radius of the disks, respectively. The next line will contain n integers x1, x2, ..., xn (1 ≤ xi ≤ 1 000) — the x-coordinates of the disks.

Print a single line with n numbers. The i-th number denotes the y-coordinate of the center of the i-th disk. The output will be accepted if it has absolute or relative error at most 10 - 6. Namely, let's assume that your answer for a particular value of a coordinate is a and the answer of the jury is b. The checker program will consider your answer correct if  for all coordinates.

NoteThe final positions of the disks will look as follows:  In particular, note the position of the last disk.

package main

import (
	"fmt"
	"math"
)

func main() {
	var n int
	var r float64
	fmt.Scan(&n, &r)
	land := make([][2]float64, 0, n)
	for i := 0; i < n; i++ {
		var x float64
		fmt.Scan(&x)
		y := r
		for _, l := range land {
			if math.Abs(l[0]-x)-1e-6 > 2*r {
				continue
			}
			y = math.Max(y, solve(l[0], l[1], x, r))
		}
		land = append(land, [2]float64{x, y})
		if i+1 == n {
			fmt.Printf("%.10f\n", y)
		} else {
			fmt.Printf("%.10f ", y)
		}
	}
}

func solve(x0, y0, x1, r float64) float64 {
	d := 4*y0*y0 - 4*(y0*y0+(x0-x1)*(x0-x1)-4*r*r)
	if d < 0 {
		d = 0
	}
	return (2*y0 + math.Sqrt(d)) / 2
}

package main

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

func main() {
	sc := NewScanner()
	N, R := sc.NextInt(), sc.NextInt()
	x := sc.NextIntArray()

	fix := make([]float64, 0, N)
	fix = append(fix, float64(R))

	for i := 1; i < N; i++ {
		P := x[i]
		q := float64(R)

		for j := len(fix) - 1; j >= 0; j-- {

			d := 2*R*2*R - (x[j]-P)*(x[j]-P)
			if d >= 0 {
				p := math.Sqrt(float64(d)) + fix[j]
				if q < p {
					q = p
				}
			}
		}
		fix = append(fix, q)
	}
	ans := ""
	for i := 0; i < N; i++ {
		if len(ans) > 0 {
			ans += " "
		}
		ans += fmt.Sprintf("%0.9f", fix[i])
	}
	fmt.Println(ans)
}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

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

func main() {
	sc := NewScanner()
	N, R := sc.NextInt(), sc.NextInt()
	x := sc.NextIntArray()

	fix := make([]float64, 0, N)
	fix = append(fix, float64(R))

	for i := 1; i < N; i++ {
		P := x[i]
		q := 0.0

		for j := len(fix) - 1; j >= 0; j-- {
			if P == x[j] {
				p := fix[j] + float64(R+R)
				if q < p {
					q = p
				}
			}
			d := 2*R*2*R - (x[j]-P)*(x[j]-P)
			if d >= 0 {
				p := math.Sqrt(float64(d)) + fix[j]
				if q < p {
					q = p
				}
			}
		}
		fix = append(fix, q)
	}
	ans := ""
	for i := 0; i < N; i++ {
		if len(ans) > 0 {
			ans += " "
		}
		ans += fmt.Sprintf("%0.9f", fix[i])
	}
	fmt.Println(ans)
}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}