Bertown is a city with $$$n$$$ buildings in a straight line.The city's security service discovered that some buildings were mined. A map was compiled, which is a string of length $$$n$$$, where the $$$i$$$-th character is "1" if there is a mine under the building number $$$i$$$ and "0" otherwise.Bertown's best sapper knows how to activate mines so that the buildings above them are not damaged. When a mine under the building numbered $$$x$$$ is activated, it explodes and activates two adjacent mines under the buildings numbered $$$x-1$$$ and $$$x+1$$$ (if there were no mines under the building, then nothing happens). Thus, it is enough to activate any one mine on a continuous segment of mines to activate all the mines of this segment. For manual activation of one mine, the sapper takes $$$a$$$ coins. He can repeat this operation as many times as you want.Also, a sapper can place a mine under a building if it wasn't there. For such an operation, he takes $$$b$$$ coins. He can also repeat this operation as many times as you want.The sapper can carry out operations in any order.You want to blow up all the mines in the city to make it safe. Find the minimum number of coins that the sapper will have to pay so that after his actions there are no mines left in the city.

The first line contains one positive integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case begins with a line containing two integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le 1000$$$) — the cost of activating and placing one mine, respectively. The next line contains a map of mines in the city — a string consisting of zeros and ones. The sum of the string lengths for all test cases does not exceed $$$10^5$$$.

For each test case, output one integer — the minimum number of coins that the sapper will have to pay.

NoteIn the second test case, if we place a mine under the fourth building and then activate it, then all mines on the field are activated. The cost of such operations is six, $$$b=1$$$ coin for placing a mine and $$$a=5$$$ coins for activating.

package main

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

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

	return b
}

func main() {
	var t int
	fmt.Scanf("%d\n", &t)

	for {
		if t == 0 {
			break
		}
		t--

		var a, b int
		fmt.Scanf("%d %d\n", &a, &b)

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

		s = strings.TrimLeft(s, "0")
		s = strings.TrimRight(s, "0")

		if len(s) == 0 {
			fmt.Println(0)
			continue
		}

		var B []int

		var count int
		for _, si := range s {
			if si == '1' {
				if count != 0 {
					B = append(B, count)
					count = 0
				}
			} else {
				count += 1
			}
		}
		if count != 0 {
			B = append(B, count)
			count = 0
		}

		fmt.Fprintln(os.Stderr, "B", B)

		var F = make([]int, len(B)+1)

		for i, _ := range F {

			if i == 0 {
				F[i] = a
				continue
			}

			bCost := b * B[i-1]
			if i == 1 {
				F[i] = F[i-1] + min(a, bCost)
			} else {
				F[i] = min(F[i-1]+min(a, bCost), F[i-2]+(a+bCost))
			}
			println("F", i, F[i])
		}

		fmt.Println(F[len(F)-1])
	}
}

// You can edit this code!
// Click here and start typing.
package main

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

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

	var t int
	fmt.Fscan(reader, &t)
	for i := 1; i <= t; i++ {
		var a, b int
		var instr string
		fmt.Fscan(reader, &a, &b, &instr)

		vecGap := []int{}
		pre := -1
		for i := 0; i < len(instr); {
			if instr[i] == '1' {
				if pre != -1 {
					vecGap = append(vecGap, i-pre-1)
				}
				for i < len(instr) && instr[i] == '1' {
					pre = i
					i++
				}
			} else {
				i++
			}
		}

		ans := 0
		if pre != -1 {
			ans = (len(vecGap) + 1) * a
			for i := 0; i < len(vecGap); i++ {
				d := vecGap[i]
				if d*b < a {
					ans = ans - a + d*b
				}
			}
		}
		fmt.Println(ans)
	}
}

// You can edit this code!
// Click here and start typing.
package main

import (
	"fmt"
)

func main() {
	//scanner := bufio.NewScanner(os.Stdin)

	var t int
	/*scanner.Scan()
	fmt.Sscan(scanner.Text(), &t)*/
	fmt.Scan(&t)
	for i := 1; i <= t; i++ {
		var a, b int
		var instr string
		fmt.Scan(&a, &b, &instr)
		/*scanner.Scan()
		fmt.Sscan(scanner.Text(), &a, &b)
		scanner.Scan()
		fmt.Sscan(scanner.Text(), &instr)*/

		vecGap := []int{}
		pre := -1
		for i := 0; i < len(instr); {
			if instr[i] == '1' {
				if pre != -1 {
					vecGap = append(vecGap, i-pre-1)
				}
				for i < len(instr) && instr[i] == '1' {
					pre = i
					i++
				}
			} else {
				i++
			}
		}

		ans := 0
		if pre != -1 {
			ans = (len(vecGap) + 1) * a
			for i := 0; i < len(vecGap); i++ {
				d := vecGap[i]
				if d*b < a {
					ans = ans - a + d*b
				}
			}
		}
		fmt.Println(ans)
	}
}

/*
URL:
https://codeforces.com/contest/1443/problem/B
*/

package main

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

var (
	t    int
	a, b int64
	S    []rune
)

func main() {
	defer stdout.Flush()

	t = readi()
	for tc := 0; tc < t; tc++ {
		a, b = readll2()
		S = readrs()

		solve()
	}
}

func solve() {
	comp, cnts := RunLengthEncoding(S)

	// 0だけのときは例外処理
	if len(comp) == 1 && comp[0] == '0' {
		printf("%d\n", 0)
		return
	}

	if comp[len(comp)-1] == '0' {
		comp = comp[:len(comp)-1]
		cnts = cnts[:len(cnts)-1]
	}
	if comp[0] == '0' {
		comp = comp[1:]
		cnts = cnts[1:]
	}

	l := len(comp)

	// one := int64(l / 2)
	one := Kiriage(int64(l), 2)
	Z := []int64{}
	for i := 0; i < l; i++ {
		if comp[i] == '0' {
			Z = append(Z, int64(cnts[i]))
		}
	}
	// debugf("Z: %v\n", Z)

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

	zs := make([]int64, len(Z)+1)
	for i := 0; i < len(Z); i++ {
		zs[i+1] = zs[i] + Z[i]
	}

	ans := a * one
	for i := int64(1); i < int64(len(zs)); i++ {
		val := b*zs[i] + a*(one-i)
		ChMin(&ans, val)
	}

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

// Kiriage returns Ceil(a/b)
// a >= 0, b > 0
func Kiriage(a, b int64) int64 {
	return (a + (b - 1)) / b
}

// 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 *int64, target int64) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

// RunLengthEncoding returns encoded slice of an input.
// Time complexity: O(|S|)
func RunLengthEncoding(S []rune) (comp []rune, cnts []int) {
	comp = []rune{}
	cnts = []int{}

	l := 0
	for i := 0; i < len(S); i++ {
		if i == 0 {
			l = 1
			continue
		}

		if S[i-1] == S[i] {
			l++
		} else {
			comp = append(comp, S[i-1])
			cnts = append(cnts, l)
			l = 1
		}
	}
	comp = append(comp, S[len(S)-1])
	cnts = append(cnts, l)

	return
}

// RunLengthDecoding decodes RLE results.
// Time complexity: O(|S|)
func RunLengthDecoding(comp []rune, cnts []int) (S []rune) {
	if len(comp) != len(cnts) {
		panic("S, L are not RunLengthEncoding results")
	}

	S = []rune{}

	for i := 0; i < len(comp); i++ {
		for j := 0; j < cnts[i]; j++ {
			S = append(S, comp[i])
		}
	}

	return
}

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

/********** 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"
)

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

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

func min_int(a int, b int) int { if a < b { return a } else { return b } }
func min_int64(a int64, b int64) int64 { if a < b { return a } else { return b } }

func max_int(a int, b int) int { if a > b { return a } else { return b } }
func max_int64(a int64, b int64) int64 { if a > b { return a } else { return b } }

type LeftRightPair struct {
	l, r int
}

func main() {
	defer writer.Flush()

	var T int = 0
	scanf("%d\n", &T)

	for t := 0; t < T; t++ {

		var a, b int
		scanf("%d %d\n", &a, &b);

		var s string
		scanf("%s\n", &s)

		pairs := make([]LeftRightPair, 0)
		for i := 0; i < len(s); i++ {

			if string(s[i]) == "0" {
				continue
			}

			if string(s[i]) == "1" {
				var ones_start_index int = i
				var ones_stop_index int = len(s)
				for j := i + 1; j < len(s); j++ {
					if string(s[j]) == "0" {
						ones_stop_index = j
						break
					}
				}
				i = ones_stop_index
				pairs = append(pairs, LeftRightPair{ones_start_index, ones_stop_index})
			}

		}

		if len(pairs) == 0 {
			printf("0\n")
			continue
		}

		var res int = a
		for i := 1; i < len(pairs); i++ {
			res += min_int((pairs[i].l - pairs[i-1].r)*b, a)
		}

		printf("%d\n", res)

	}

}

package main

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

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

func solveSingle() {
	a := readInt()
	b := readInt()
	s := readString()
	values := make([][]int, 0)
	start := 0
	for start < len(s) {
		cnt := CountValues(s[start], s, start)
		if cnt > 0 {
			values = append(values, []int{int(s[start] - '0'), cnt})
		} else {
			break
		}
		start += cnt
	}
	cost := 0
	for i := 0; i < len(values) - 2; i++ {
		if values[i][0] == 1 {
			cost1 := 2 * a
			cost2 := a + b * values[i+1][1]
			if cost1 > cost2 {
				cost += b * values[i+1][1]
				values[i+2][1] += values[i][1]
				values[i][0] = 0
			}
		}
	}
	for _, value := range values {
		if value[0] == 1 {
			cost += a
		}
	}
	fmt.Println(cost)
}

func CountValues(ex uint8, str string, start int) int {
	cnt := 0
	for i := start; i < len(str) && str[i] == ex; i++ {
		cnt++
	}
	return cnt
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

package main

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

func main() {
	//rd, _ := os.Open("input")
	//in := bufio.NewReader(rd)
	in := bufio.NewReader(os.Stdin)

	var t, a, b int
	var s string
	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		fmt.Fscan(in, &a, &b)
		fmt.Fscan(in, &s)

		s = strings.Trim(s, "0")
		sum := 0
		emptyStreak := 2 * a
		for i := range s {
			if s[i] == '1' {
				if emptyStreak > 0 {
					if b*emptyStreak < a {
						sum += b * emptyStreak
					} else {
						sum += a
					}
				}
				emptyStreak = 0
			} else {
				emptyStreak++
			}
		}
		fmt.Println(sum)
	}
}

package main

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

var reader = bufio.NewReader(os.Stdin)

type segment struct {
	start, end int
}

func main() {
	var t int
	fmt.Fscan(reader, &t)

	for i := 0; i < t; i++ {
		var a, b int64
		fmt.Fscan(reader, &a, &b)

		var s string
		fmt.Fscan(reader, &s)

		prev := int64(-1e9)
		var ans int64 = 0
		for j := 0; j < len(s); j++ {
			if s[j] == '0' {
				continue
			}

			len := int64(j) - prev - 1

			// fmt.Println("len: ", len)

			if b*len < a {
				ans += b * len
			} else {
				ans += a
			}

			prev = int64(j)
		}

		fmt.Println(ans)
	}
}

package main

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

/*
2
1 1
01000010
5 1
011011100001

2
6
*/

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

	var T, put, bang int64
	var target string
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &bang)
		Fscan(in, &put)
		Fscan(in, &target)

		bangIdx := make([]int, len(target))
		flag := false
		lastBangIdx := -1
		for idx, content := range target {
			if content == '1' {
				if lastBangIdx != -1 {
					bangIdx[idx] = idx - lastBangIdx - 1
				}
				lastBangIdx = idx
				flag = true
			}
		}

		if !flag {
			Fprintln(out, 0)
			continue
		}

		sum := bang
		for idx, content := range target {
			if content == '1' && bangIdx[idx] != 0 {
				sum += min(bang, int64(bangIdx[idx])*put)
			}
		}

		Fprintln(out, sum)
	}
}

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

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

package main

import (
	"fmt"
	"strings"
)

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

	return b
}

func main() {
	var t int
	fmt.Scanf("%d\n", &t)

	for {
		if t == 0 {
			break
		}

		var a, b int
		fmt.Scanf("%d %d\n", &a, &b)

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

		s = strings.TrimLeft(s, "0")
		s = strings.TrimRight(s, "0")

		var B []int

		var count int
		for _, si := range s {
			if si == '1' {
				if count != 0 {
					B = append(B, count)
					count = 0
				}
			} else {
				count += 1
			}
		}
		if count != 0 {
			B = append(B, count)
			count = 0
		}

		var F = make([]int, len(B)+1)

		for i, _ := range F {
			if i == 0 {
				F[i] = a
			} else if i == 1 {
				F[i] = min(a+a, a+b*B[i-1])
			} else {
				F[i] = min(F[i-1]+min(a, b*B[i-1]), F[i-2]+(a+b*B[i-1]))
			}
		}

		fmt.Println(F[len(F)-1])

		t--
	}
}

package main

import (
	"fmt"
	"math"
	"strings"
)

func main() {
	var t int
	fmt.Scanf("%d\n", &t)

	for {
		if t == 0 {
			break
		}

		var a, b int
		fmt.Scanf("%d %d\n", &a, &b)

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

		s = strings.TrimLeft(s, "0")
		s = strings.TrimRight(s, "0")

		var B []int

		var count int
		for _, si := range s {
			if si == '1' {
				if count != 0 {
					B = append(B, count)
					count = 0
				}
			} else {
				count += 1
			}
		}
		if count != 0 {
			B = append(B, count)
			count = 0
		}

		var F = make([]int, len(B)+1)

		for i, _ := range F {
			if i == 0 {
				F[i] = a
			} else if i == 1 {
				F[i] = int(math.Min(float64(a+a), float64(a+b)))
			} else {
				F[i] = int(math.Min(float64(F[i-1]+a), float64(F[i-2]+(a+b))))
			}
		}

		fmt.Println(F[len(F)-1])

		t--
	}
}

package main

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

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

	return b
}

func main() {
	var t int
	fmt.Scanf("%d\n", &t)

	for {
		if t == 0 {
			break
		}

		var a, b int
		fmt.Scanf("%d %d\n", &a, &b)

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

		s = strings.TrimLeft(s, "0")
		s = strings.TrimRight(s, "0")

		var B []int

		var count int
		for _, si := range s {
			if si == '1' {
				if count != 0 {
					B = append(B, count)
					count = 0
				}
			} else {
				count += 1
			}
		}
		if count != 0 {
			B = append(B, count)
			count = 0
		}

		fmt.Fprintln(os.Stderr, "B", B)

		var F = make([]int, len(B)+1)

		for i, _ := range F {

			if i == 0 {
				F[i] = a
				continue
			}

			bCost := b * B[i-1]
			if i == 1 {
				F[i] = F[i-1] + min(a, bCost)
			} else {
				F[i] = min(F[i-1]+min(a, bCost), F[i-2]+(a+bCost))
			}
			println("F", i, F[i])
		}

		fmt.Println(F[len(F)-1])

		t--
	}
}

package main

import (
	"fmt"
	"math"
	"strings"
)

func main() {
	var t int
	fmt.Scanf("%d\n", &t)

	for {
		if t == 0 {
			break
		}

		var a, b int
		fmt.Scanf("%d %d\n", &a, &b)

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

		s = strings.TrimLeft(s, "0")
		s = strings.TrimRight(s, "0")

		var B []int

		var count int
		for _, si := range s {
			if si == '1' {
				if count != 0 {
					B = append(B, count)
					count = 0
				}
			} else {
				count += 1
			}
		}
		if count != 0 {
			B = append(B, count)
			count = 0
		}

		var F = make([]int, len(B)+1)

		for i, _ := range F {
			if i == 0 {
				F[i] = a
			} else if i == 1 {
				F[i] = int(math.Min(float64(a+a), float64(a+b*B[i-1])))
			} else {
				F[i] = int(math.Min(float64(F[i-1]+a), float64(F[i-2]+(a+b*B[i-1]))))
			}
		}

		fmt.Println(F[len(F)-1])

		t--
	}
}

// You can edit this code!
// Click here and start typing.
package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)

	var t int
	scanner.Scan()
	fmt.Sscan(scanner.Text(), &t)
	for i := 1; i <= t; i++ {
		var a, b int
		var instr string
		scanner.Scan()
		fmt.Sscan(scanner.Text(), &a, &b)
		scanner.Scan()
		fmt.Sscan(scanner.Text(), &instr)

		vecGap := []int{}
		pre := -1
		for i := 0; i < len(instr); {
			if instr[i] == '1' {
				if pre != -1 {
					vecGap = append(vecGap, i-pre-1)
				}
				for i < len(instr) && instr[i] == '1' {
					pre = i
					i++
				}
			} else {
				i++
			}
		}

		ans := 0
		if pre != -1 {
			ans = (len(vecGap) + 1) * a
			for i := 0; i < len(vecGap); i++ {
				d := vecGap[i]
				if d*b < a {
					ans = ans - a + d*b
				}
			}
		}
		fmt.Println(ans)
	}
}

// You can edit this code!
// Click here and start typing.
package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)

	var t int
	scanner.Scan()
	fmt.Sscan(scanner.Text(), &t)
	for i := 1; i <= t; i++ {
		var a, b int
		var instr string
		scanner.Scan()
		fmt.Sscan(scanner.Text(), &a, &b)
		scanner.Scan()
		fmt.Sscan(scanner.Text(), &instr)

		vecGap := []int{}
		pre := -1
		for i := 0; i < len(instr); {
			if instr[i] == '1' {
				if pre != -1 {
					vecGap = append(vecGap, i-pre-1)
				}
				for i < len(instr) && instr[i] == '1' {
					pre = i
					i++
				}
			} else {
				i++
			}
		}

		ans := (len(vecGap) + 1) * a
		for i := 0; i < len(vecGap); i++ {
			d := vecGap[i]
			if d*b < a {
				ans = ans - a + d*b
			}
		}
		fmt.Println(ans)
	}
}

package main

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

func main() {
	//rd, _ := os.Open("input")
	//in := bufio.NewReader(rd)
	in := bufio.NewReader(os.Stdin)

	var t, a, b int
	var s string
	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		fmt.Fscan(in, &a, &b)
		fmt.Fscan(in, &s)

		s = strings.Trim(s, "0")
		sum := 0
		emptyStreak := 2 * a / b
		for i := range s {
			if s[i] == '1' {
				if emptyStreak > 0 {
					sum += int(math.Min(float64(b*emptyStreak), float64(a)))
				}
				emptyStreak = 0
			} else {
				emptyStreak++
			}
		}
		fmt.Println(s)
	}
}

package main

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

func main() {
	//rd, _ := os.Open("input")
	//in := bufio.NewReader(rd)
	in := bufio.NewReader(os.Stdin)

	var t, a, b int
	var s string
	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		fmt.Fscan(in, &a, &b)
		fmt.Fscan(in, &s)

		s = strings.Trim(s, "0")
		sum := 0
		emptyStreak := 2 * a / b
		for i := range s {
			if s[i] == '1' {
				if emptyStreak > 0 {
					sum += int(math.Min(float64(b*emptyStreak), float64(a)))
				}
				emptyStreak = 0
			} else {
				emptyStreak++
			}
		}
		fmt.Println(sum)
	}
}

package main

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

func main() {
	//rd, _ := os.Open("input")
	//in := bufio.NewReader(rd)
	in := bufio.NewReader(os.Stdin)

	var t, a, b int
	var s string
	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		fmt.Fscan(in, &a, &b)
		fmt.Fscan(in, &s)

		s = strings.Trim(s, "0")
		sum := 0
		emptyStreak := 2 * a / b
		for i := range s {
			if s[i] == '1' {
				if emptyStreak > 0 {
					if b*emptyStreak < a {
						sum += b * emptyStreak
					} else {
						sum += a
					}
				}
				emptyStreak = 0
			} else {
				emptyStreak++
			}
		}
		fmt.Println(sum)
	}
}

package main

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

var reader = bufio.NewReader(os.Stdin)

type segment struct {
	start, end int
}

func main() {
	var t int
	fmt.Fscan(reader, &t)

	for i := 0; i < t; i++ {
		var a, b int
		fmt.Fscan(reader, &a, &b)

		var s string
		fmt.Fscan(reader, &s)

		prev := int(-1e9)
		ans := 0
		for j := 0; j < len(s); j++ {
			if s[j] == '0' {
				continue
			}

			len := j - prev - 1

			// fmt.Println("len: ", len)

			if b*len < a {
				ans += b * len
			} else {
				ans += a
			}

			prev = j
		}

		fmt.Println(ans)
	}
}

package main

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

var reader = bufio.NewReader(os.Stdin)

type segment struct {
	start, end int
}

func main() {
	var t int
	fmt.Fscan(reader, &t)

	for i := 0; i < t; i++ {
		var a, b int
		fmt.Fscan(reader, &a, &b)

		var s string
		fmt.Fscan(reader, &s)

		prev := int(-1e9)
		ans := 0
		for j := 0; j < len(s); j++ {
			if s[j] == '0' {
				continue
			}

			len := j - prev -1

			fmt.Println("len: ", len)

			if b*len < a {
				ans += b * len
			} else {
				ans += a
			}

			prev = j
		}

		fmt.Println(ans)
	}
}

package main

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

/*
2
1 1
01000010
5 1
011011100001

2
6
*/

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

	var T, put, bang int64
	var target string
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &bang)
		Fscan(in, &put)
		Fscan(in, &target)

		bangIdx := make([]int, len(target))
		flag := false
		lastBangIdx := -1
		for idx, content := range target {
			if content == '1' {
				if lastBangIdx != -1 {
					bangIdx[idx] = idx - lastBangIdx - 1
				}
				lastBangIdx = idx
				flag = true
			}
		}

		if !flag {
			Fprintln(out, 0)
			return
		}

		sum := bang
		for idx, content := range target {
			if content == '1' && bangIdx[idx] != 0 {
				sum += min(bang, int64(bangIdx[idx])*put)
			}
		}

		Fprintln(out, sum)
	}
}

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

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

package main

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

/*
2
1 1
01000010
5 1
011011100001

2
6
*/

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

	var T, put, bang int64
	var target string
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &bang)
		Fscan(in, &put)
		Fscan(in, &target)

		bangIdx := make([]int, len(target))
		flag := false
		lastBangIdx := -1
		for idx, content := range target {
			if content == '1' {
				if lastBangIdx != -1 {
					bangIdx[idx] = idx - lastBangIdx - 1
				}
				lastBangIdx = idx
				flag = true
			}
		}

		if !flag {
			Fprintln(out, 0)
		}

		sum := bang
		for idx, content := range target {
			if content == '1' && bangIdx[idx] != 0 {
				sum += min(bang, int64(bangIdx[idx])*put)
			}
		}

		Fprintln(out, sum)
	}
}

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

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

package main

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

/*
2
1 1
01000010
5 1
01101110

2
6
*/

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

	var T, put, bang int64
	var target string
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &bang)
		Fscan(in, &put)
		Fscan(in, &target)

		bangIdx := make([]int, len(target))
		lastBangIdx := -1
		for idx, content := range target {
			if content == '1' {
				if lastBangIdx != -1 {
					bangIdx[idx] = idx - lastBangIdx - 1
				}
				lastBangIdx = idx
			}

		}

		sum := bang
		for idx, content := range target {
			if content == '1' && bangIdx[idx] != 0 {
				sum += min(bang, int64(bangIdx[idx])*put)
			}
		}

		Fprintln(out, sum)
	}
}

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

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

package main

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

/*
2
1 1
01000010
5 1
011011100001

2
6
*/

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

	var T, put, bang int64
	var target string
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &bang)
		Fscan(in, &put)
		Fscan(in, &target)

		bangIdx := make([]int, len(target))
		flag := false
		lastBangIdx := -1
		for idx, content := range target {
			if content == '1' {
				if lastBangIdx != -1 {
					bangIdx[idx] = idx - lastBangIdx - 1
				}
				lastBangIdx = idx
			} else {
				flag = true
			}
		}

		if flag {
			Fprintln(out, 0)
		}

		sum := bang
		for idx, content := range target {
			if content == '1' && bangIdx[idx] != 0 {
				sum += min(bang, int64(bangIdx[idx])*put)
			}
		}

		Fprintln(out, sum)
	}
}

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

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


Harry, Ron and Hermione have figured out that Helga Hufflepuff's cup is a horcrux. Through her encounter with Bellatrix Lestrange, Hermione came to know that the cup is present in Bellatrix's family vault in Gringott's Wizarding Bank. The Wizarding bank is in the form of a tree with total n vaults where each vault has some type, denoted by a number between 1 to m. A tree is an undirected connected graph with no cycles.The vaults with the highest security are of type k, and all vaults of type k have the highest security.There can be at most x vaults of highest security. Also, if a vault is of the highest security, its adjacent vaults are guaranteed to not be of the highest security and their type is guaranteed to be less than k.Harry wants to consider every possibility so that he can easily find the best path to reach Bellatrix's vault. So, you have to tell him, given the tree structure of Gringotts, the number of possible ways of giving each vault a type such that the above conditions hold.

The first line of input contains two space separated integers, n and m — the number of vaults and the number of different vault types possible. (1 ≤ n ≤ 105, 1 ≤ m ≤ 109). Each of the next n - 1 lines contain two space separated integers ui and vi (1 ≤ ui, vi ≤ n) representing the i-th edge, which shows there is a path between the two vaults ui and vi. It is guaranteed that the given graph is a tree. The last line of input contains two integers k and x (1 ≤ k ≤ m, 1 ≤ x ≤ 10), the type of the highest security vault and the maximum possible number of vaults of highest security.

Output a single integer, the number of ways of giving each vault a type following the conditions modulo 109 + 7.

NoteIn test case 1, we cannot have any vault of the highest security as its type is 1 implying that its adjacent vaults would have to have a vault type less than 1, which is not allowed. Thus, there is only one possible combination, in which all the vaults have type 2.

package main

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

var in = bufio.NewScanner(os.Stdin)

func readInt() int {
	if !in.Scan() {
		panic(in.Err())
	}
	res := 0
	for _, b := range in.Bytes() {
		res = res*10 + int(b-'0')
	}
	return res
}

const mod = 1e9 + 7

var (
	edges   [][]int
	m, k, x int
)

type typ int

const (
	lo typ = iota
	yes
	hi
)

func merge(ans, cur []int64) {
	for i := x; i >= 0; i-- {
		ans[i] = (ans[i] * cur[0]) % mod
		for j := 1; j <= i; j++ {
			ans[i] = (ans[i] + ans[i-j]*cur[j]) % mod
		}
	}
}

type state struct {
	v int
	typ
}

var mem = make(map[state][]int64)

func dfs(v, parent int, t typ) []int64 {
	if res, ok := mem[state{v, t}]; ok {
		return res
	}

	ans := make([]int64, x+1)
	switch t {
	case yes:
		ans[1] = 1
		for _, u := range edges[v] {
			if u == parent {
				continue
			}
			merge(ans, dfs(u, v, lo))
		}
	case lo:
		ans[0] = int64(k - 1)
		for _, u := range edges[v] {
			if u == parent {
				continue
			}
			cur := append([]int64(nil), dfs(u, v, lo)...)
			for i, z := range dfs(u, v, yes) {
				cur[i] += z
			}
			for i, z := range dfs(u, v, hi) {
				cur[i] += z
			}
			merge(ans, cur)
		}
	case hi:
		ans[0] = int64(m - k)
		for _, u := range edges[v] {
			if u == parent {
				continue
			}
			cur := append([]int64(nil), dfs(u, v, lo)...)
			for i, z := range dfs(u, v, hi) {
				cur[i] += z
			}
			merge(ans, cur)
		}
	}
	mem[state{v, t}] = ans
	return ans
}

func main() {
	in.Split(bufio.ScanWords)

	n := readInt()
	m = readInt()

	edges = make([][]int, n)

	for i := 1; i < n; i++ {
		u := readInt() - 1
		v := readInt() - 1
		edges[u] = append(edges[u], v)
		edges[v] = append(edges[v], u)
	}

	k = readInt()
	x = readInt()

	ans := int64(0)
	for _, v := range dfs(0, 0, lo) {
		ans = (ans + v) % mod
	}
	for _, v := range dfs(0, 0, yes) {
		ans = (ans + v) % mod
	}
	for _, v := range dfs(0, 0, hi) {
		ans = (ans + v) % mod
	}
	fmt.Println(ans)
}


Polycarp loves geometric progressions very much. Since he was only three years old, he loves only the progressions of length three. He also has a favorite integer k and a sequence a, consisting of n integers.He wants to know how many subsequences of length three can be selected from a, so that they form a geometric progression with common ratio k.A subsequence of length three is a combination of three such indexes i1, i2, i3, that 1 ≤ i1 < i2 < i3 ≤ n. That is, a subsequence of length three are such groups of three elements that are not necessarily consecutive in the sequence, but their indexes are strictly increasing.A geometric progression with common ratio k is a sequence of numbers of the form b·k0, b·k1, ..., b·kr - 1.Polycarp is only three years old, so he can not calculate this number himself. Help him to do it.

The first line of the input contains two integers, n and k (1 ≤ n, k ≤ 2·105), showing how many numbers Polycarp's sequence has and his favorite number. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — elements of the sequence.

Output a single number — the number of ways to choose a subsequence of length three, such that it forms a geometric progression with a common ratio k.

NoteIn the first sample test the answer is four, as any of the two 1s can be chosen as the first element, the second element can be any of the 2s, and the third element of the subsequence must be equal to 4.

package main

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

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

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

	n, k := nextInt64(), nextInt64()
	a := make([]int64, n)
	cntNumR := map[int64]int64{}
	cntNumL := map[int64]int64{}
	ret := int64(0)

	for i := range a {
		a[i] = nextInt64()

		if _, ok := cntNumR[a[i]]; !ok {
			cntNumR[a[i]] = 1
			cntNumL[a[i]] = 0
		} else {
			cntNumR[a[i]]++
		}
	}
	for i := range a {
		cntNumR[a[i]]--
		// println(cntNumR)

		if a[i]%k == 0 {
			l, okL := cntNumL[a[i]/k]
			r, okR := cntNumR[a[i]*k]
			// println(ret, l, r)
			if okL && okR {
				ret += l * r
			}
		}
		cntNumL[a[i]]++

	}

	println(ret)
}

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

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

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

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

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

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

package main

import "fmt"
import "bufio"
import "strconv"
import "os"

const maxn int = 200000

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

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

	var array[maxn] int64
	var n int
	var k int64

	n = nextInt()
	k = nextInt64()

	var m1 map[int64]int64
	var m2 map[int64]int64
	m1 = make(map[int64]int64)
	m2 = make(map[int64]int64)

	for i := 0; i < n; i++ {
		array[i] = nextInt64()
		m2[array[i]] += 1
	}

	var ans int64 = 0
	for i := 0; i < n; i++ {
		var cur int64 = array[i]
		m2[cur] -= 1

		if cur % k == 0 {
			ans += m1[cur / k] * m2[cur * k]
		}

		m1[cur] += 1
	}

	println(ans)
}

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

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

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

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

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

package main

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

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

	var n, k, v int
	Fscan(in, &n, &k)
	if n < 3 {
		Fprint(out, 0)
		return
	}

	ans := int64(0)
	if k == 1 {
		cnt := map[int]int64{}
		for i := 0; i < n; i++ {
			Fscan(in, &v)
			if cnt[v] >= 2 {
				ans += cnt[v] * (cnt[v] - 1) / 2
			}
			cnt[v]++
		}
		Fprint(out, ans)
		return
	}

	cnt0 := map[int]int64{}
	cnt1 := map[int]int64{}
	for i := 0; i < n; i++ {
		Fscan(in, &v)
		if v%k == 0 {
			if v/k%k == 0 {
				ans += cnt1[v/k]
			}
			cnt1[v] += cnt0[v/k]
		}
		cnt0[v]++
	}
	Fprint(out, ans)
}

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

package main

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

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

	var n, k, v int
	Fscan(in, &n, &k)
	if n < 3 {
		Fprint(out, 0)
		return
	}

	ans := int64(0)
	if k == 1 {
		cnt := map[int]int{}
		for i := 0; i < n; i++ {
			Fscan(in, &v)
			if cnt[v] >= 2 {
				ans += int64(cnt[v]) * int64(cnt[v]-1) / 2
			}
			cnt[v]++
		}
		Fprint(out, ans)
		return
	}

	cnt0 := map[int]int{}
	cnt1 := map[int]int64{}
	for i := 0; i < n; i++ {
		Fscan(in, &v)
		if v%k == 0 {
			if v/k%k == 0 {
				ans += cnt1[v/k]
			}
			cnt1[v] += int64(cnt0[v/k])
		}
		cnt0[v]++
	}
	Fprint(out, ans)
}

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

package main

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

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

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

	n, k := nextInt64(), nextInt64()
	a := make([]int64, n)
	cntNumR := map[int64]int{}
	cntNumL := map[int64]int{}
	ret := 0

	for i := range a {
		a[i] = nextInt64()

		if _, ok := cntNumR[a[i]]; !ok {
			cntNumR[a[i]] = 1
			cntNumL[a[i]] = 0
		} else {
			cntNumR[a[i]]++
		}
	}
	for i := range a {
		cntNumR[a[i]]--
		// println(cntNumR)

		if a[i]%k == 0 {
			l, okL := cntNumL[a[i]/k]
			r, okR := cntNumR[a[i]*k]
			// println(ret, l, r)
			if okL && okR {
				ret += l * r
			}
		}
		cntNumL[a[i]]++

	}

	println(ret)
}

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

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

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

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

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

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

package main

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

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

	var n, k, v int
	Fscan(in, &n, &k)
	if n < 3 {
		Fprint(out, 0)
		return
	}

	ans := int64(0)
	if k == 1 {
		cnt := map[int]int{}
		for i := 0; i < n; i++ {
			Fscan(in, &v)
			if cnt[v] >= 2 {
				ans += int64(cnt[v]) * int64(cnt[v]-1) / 2
			}
			cnt[v]++
		}
		Fprint(out, ans)
		return
	}

	cnt0 := map[int]int{}
	cnt1 := map[int]int{}
	for i := 0; i < n; i++ {
		Fscan(in, &v)
		if v%k == 0 {
			if v/k%k == 0 {
				ans += int64(cnt1[v/k])
			}
			cnt1[v] += cnt0[v/k]
		}
		cnt0[v]++
	}
	Fprint(out, ans)
}

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


Mahmoud and Ehab are on the third stage of their adventures now. As you know, Dr. Evil likes sets. This time he won't show them any set from his large collection, but will ask them to create a new set to replenish his beautiful collection of sets.Dr. Evil has his favorite evil integer x. He asks Mahmoud and Ehab to find a set of n distinct non-negative integers such the bitwise-xor sum of the integers in it is exactly x. Dr. Evil doesn't like big numbers, so any number in the set shouldn't be greater than 106.

The only line contains two integers n and x (1 ≤ n ≤ 105, 0 ≤ x ≤ 105) — the number of elements in the set and the desired bitwise-xor, respectively.

If there is no such set, print "NO" (without quotes). Otherwise, on the first line print "YES" (without quotes) and on the second line print n distinct integers, denoting the elements in the set is any order. If there are multiple solutions you can print any of them.

NoteYou can read more about the bitwise-xor operation here: https://en.wikipedia.org/wiki/Bitwise_operation#XORFor the first sample .For the second sample .

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"math/rand"
	"os"
	"strconv"
)

const MAX = 1e6

func solve() {
	n := readInt()
	x := readInt()
	if n == 2 && x == 0 {
		fmt.Println("NO")
		return
	}
	a := make([]int, MAX)
	v := make([]bool, MAX)
	for i := 0; i < MAX; i++ {
		a[i] = i
	}
	shuffleArray(a)
	res := make([]int, n)
	res[0] = x
	start := 0
	for i := 0; i < n-1; i++ {
		for start < MAX {
			next := a[start]
			start++
			if !v[next] {
				need := next ^ res[i]
				if need < MAX && !v[need] {
					res[i] = next
					res[i+1] = need
					v[need] = true
					v[next] = true
					break
				}
			}
		}
		if start == MAX {
			fmt.Println("NO")
			return
		}
	}
	fmt.Println("YES")
	var buffer bytes.Buffer
	for i := 0; i < len(res); i++ {
		if i != 0 {
			buffer.WriteString(" ")
		}
		buffer.WriteString(strconv.Itoa(res[i]))
	}
	fmt.Println(buffer.String())
}

func shuffleArray(a []int) {
	n := len(a)
	for i := 0; i < n; i++ {
		pos := i + rand.Intn(n-i)
		a[i], a[pos] = a[pos], a[i]
	}
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

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

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

package main

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

const MAX = 1e6

func solve() {
	n := readInt()
	x := readInt()
	if n == 2 && x == 0 {
		fmt.Println("NO")
		return
	}
	v := make([]bool, MAX)
	res := make([]int, n)
	res[0] = x
	v[x] = true
	start := 0
	for i := 0; i < n-1; i++ {
		for start < MAX {
			next := start
			start++
			if !v[next] {
				need := next ^ res[i]
				if need < MAX && !v[need] {
					res[i] = next
					res[i+1] = need
					v[need] = true
					v[next] = true
					break
				}
			}
		}
		if start == MAX {
			fmt.Println("NO")
			return
		}
	}
	fmt.Println("YES")
	var buffer bytes.Buffer
	for i := 0; i < len(res); i++ {
		if i != 0 {
			buffer.WriteString(" ")
		}
		buffer.WriteString(strconv.Itoa(res[i]))
	}
	fmt.Println(buffer.String())
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

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

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

package main

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

const MAX = 1e6

func solve() {
	n := readInt()
	x := readInt()
	if n == 2 && x == 0 {
		fmt.Println("NO")
		return
	}
	v := make(map[int]bool)
	res := make([]int, n)
	res[0] = x
	v[x] = true
	start := 0
	for i := 0; i < n-1; i++ {
		for start < MAX {
			next := start
			start++
			if !v[next] {
				need := next ^ res[i]
				if need < MAX && !v[need] {
					res[i] = next
					res[i+1] = need
					v[need] = true
					v[next] = true
					break
				}
			}
		}
		if start == MAX {
			fmt.Println("NO")
			return
		}
	}
	fmt.Println("YES")
	var buffer bytes.Buffer
	for i := 0; i < len(res); i++ {
		if i != 0 {
			buffer.WriteString(" ")
		}
		buffer.WriteString(strconv.Itoa(res[i]))
	}
	fmt.Println(buffer.String())
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

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

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

package main

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

const MAX = 1e6

func solve() {
	n := readInt()
	x := readInt()
	if n == 2 && x == 0 {
		fmt.Println("NO")
		return
	}
	v := make(map[int]bool)
	res := make([]int, n)
	res[0] = x
	v[x] = true
	start := 0
	for i := 0; i < n-1; i++ {
		for start < MAX {
			if !v[start] {
				need := start ^ res[i]
				if need < MAX && !v[need] {
					res[i] = start
					res[i+1] = need
					v[need] = true
					v[start] = true
					start++
					break
				}
			}
			start++
		}
		if start == MAX {
			fmt.Println("NO")
			return
		}
	}
	fmt.Println("YES")
	var buffer bytes.Buffer
	for i := 0; i < len(res); i++ {
		if i != 0 {
			buffer.WriteString(" ")
		}
		buffer.WriteString(strconv.Itoa(res[i]))
	}
	fmt.Println(buffer.String())
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

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

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

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"math/rand"
	"os"
	"strconv"
)

const MAX = 1e6

func solve() {
	n := readInt()
	x := readInt()
	if n == 2 && x == 0 {
		fmt.Println("NO")
		return
	}
	a := make([]int, MAX)
	v := make([]bool, MAX)
	for i := 0; i < MAX; i++ {
		a[i] = i
	}
	shuffleArray(a)
	res := make([]int, n)
	res[0] = x
	v[x] = true
	start := 0
	for i := 0; i < n-1; i++ {
		for start < MAX {
			next := a[start]
			start++
			if !v[next] {
				need := next ^ res[i]
				if need < MAX && !v[need] {
					res[i] = next
					res[i+1] = need
					v[need] = true
					v[next] = true
					break
				}
			}
		}
		if start == MAX {
			fmt.Println("NO")
			return
		}
	}
	fmt.Println("YES")
	var buffer bytes.Buffer
	for i := 0; i < len(res); i++ {
		if i != 0 {
			buffer.WriteString(" ")
		}
		buffer.WriteString(strconv.Itoa(res[i]))
	}
	fmt.Println(buffer.String())
}

func shuffleArray(a []int) {
	n := len(a)
	for i := 0; i < n; i++ {
		pos := i + rand.Intn(n-i)
		a[i], a[pos] = a[pos], a[i]
	}
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

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

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

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"math/rand"
	"os"
	"strconv"
)

const MAX = 1e6

func solve() {
	n := readInt()
	x := readInt()
	if n == 2 && x == 0 {
		fmt.Println("NO")
		return
	}
	a := make([]int, MAX)
	v := make([]bool, MAX)
	for i := 0; i < MAX; i++ {
		a[i] = i
	}
	// shuffleArray(a)
	res := make([]int, n)
	res[0] = x
	v[x] = true
	start := 0
	for i := 0; i < n-1; i++ {
		for start < MAX {
			next := a[start]
			start++
			if !v[next] {
				need := next ^ res[i]
				if need < MAX && !v[need] {
					res[i] = next
					res[i+1] = need
					v[need] = true
					v[next] = true
					break
				}
			}
		}
		if start == MAX {
			fmt.Println("NO")
			return
		}
	}
	fmt.Println("YES")
	var buffer bytes.Buffer
	for i := 0; i < len(res); i++ {
		if i != 0 {
			buffer.WriteString(" ")
		}
		buffer.WriteString(strconv.Itoa(res[i]))
	}
	fmt.Println(buffer.String())
}

func shuffleArray(a []int) {
	n := len(a)
	for i := 0; i < n; i++ {
		pos := i + rand.Intn(n-i)
		a[i], a[pos] = a[pos], a[i]
	}
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

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

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 CF862C(in io.Reader, _w io.Writer) {
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n, x int
	Fscan(bufio.NewReader(in), &n, &x)

	var a []int
	f := func(m int) {
		mask := 0
		for i := 0; i < m-1; i++ {
			v := 1 << (18 - i)
			mask |= v
			a = append(a, v)
		}
		a = append(a, mask^x)
		n -= m
	}
	switch n % 4 {
	case 0:
		f(4)
	case 2:
		if n == 2 {
			if x == 0 {
				Fprint(out, "NO")
			} else {
				Fprint(out, "YES\n0 ", x)
			}
			return
		}
		f(6)
	default:
		f(n % 4)
	}

	Fprintln(out, "YES")
	has := map[int]bool{}
	for _, v := range a {
		has[v] = true
		Fprint(out, v, " ")
	}
	for i := 0; n > 0; i += 4 {
		if has[i] || has[i+1] || has[i+2] || has[i+3] {
			continue
		}
		Fprint(out, i, i+1, i+2, i+3, " ")
		n -= 4
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF862C(in io.Reader, _w io.Writer) {
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n, x int
	Fscan(bufio.NewReader(in), &n, &x)

	var a []int
	f := func(m int) {
		for i := 0; i < m-1; i++ {
			v := 1 << (18 - i)
			x ^= v
			a = append(a, v)
		}
		a = append(a, x)
		n -= m
	}
	switch n % 4 {
	case 0:
		f(4)
	case 2:
		if n == 2 {
			if x == 0 {
				Fprint(out, "NO")
			} else {
				Fprint(out, "YES\n0 ", x)
			}
			return
		}
		f(6)
	default:
		f(n % 4)
	}

	Fprintln(out, "YES")
	has := map[int]bool{}
	for _, v := range a {
		has[v] = true
		Fprint(out, v, " ")
	}
	for i := 0; n > 0; i += 4 {
		if has[i] || has[i+1] || has[i+2] || has[i+3] {
			continue
		}
		Fprint(out, i, i+1, i+2, i+3, " ")
		n -= 4
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF862C(in io.Reader, _w io.Writer) {
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n, x int
	Fscan(bufio.NewReader(in), &n, &x)

	var a []int
	f := func(m int) {
		mask := 0
		for i := 0; i < m-1; i++ {
			v := 1 << (18 - i)
			mask |= v
			a = append(a, v)
		}
		a = append(a, mask)
		a[0] ^= x
		n -= m
	}
	switch n % 4 {
	case 0:
		f(4)
	case 2:
		if n == 2 {
			if x == 0 {
				Fprint(out, "NO")
			} else {
				Fprint(out, "YES\n0 ", x)
			}
			return
		}
		f(6)
	default:
		f(n % 4)
	}

	Fprintln(out, "YES")
	has := map[int]bool{}
	for _, v := range a {
		has[v] = true
		Fprint(out, v, " ")
	}
	for i := 0; n > 0; i += 4 {
		if has[i] || has[i+1] || has[i+2] || has[i+3] {
			continue
		}
		Fprint(out, i, i+1, i+2, i+3, " ")
		n -= 4
	}
}

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

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"math/rand"
	"os"
	"strconv"
)

const MAX = 1e6

func solve() {
	n := readInt()
	x := readInt()
	a := make([]int, MAX)
	v := make([]bool, MAX)
	for i := 0; i < MAX; i++ {
		a[i] = i
	}
	shuffleArray(a)
	res := make([]int, n)
	res[0] = x
	start := 0
	for i := 0; i < n-1; i++ {
		for start < MAX {
			next := a[start]
			start++
			if !v[next] {
				need := next ^ res[i]
				if need < MAX && next != need && !v[need] {
					res[i] = next
					res[i+1] = need
					v[need] = true
					v[next] = true
					break
				}
			}
		}
		if start == MAX {
			fmt.Println("NO")
			return
		}
	}
	fmt.Println("YES")
	var buffer bytes.Buffer
	for i := 0; i < len(res); i++ {
		if i != 0 {
			buffer.WriteString(" ")
		}
		buffer.WriteString(strconv.Itoa(res[i]))
	}
	fmt.Println(buffer.String())
}

func shuffleArray(a []int) {
	n := len(a)
	for i := 0; i < n; i++ {
		pos := i + rand.Intn(n-i)
		a[i], a[pos] = a[pos], a[i]
	}
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

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

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

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"math/rand"
	"os"
	"strconv"
)

const MAX = 1e6

func solve() {
	n := readInt()
	x := readInt()
	if n == 2 && x == 0 {
		fmt.Println("NO")
		return
	}
	a := make([]int, MAX)
	v := make([]bool, MAX)
	for i := 0; i < MAX; i++ {
		a[i] = i
	}
	shuffleArray(a)
	res := make([]int, n)
	res[0] = x
	start := 0
	for i := 0; i < n-1; i++ {
		for start < MAX {
			next := a[start]
			start++
			if !v[next] {
				need := next ^ res[i]
				if need < MAX && next != need && !v[need] {
					res[i] = next
					res[i+1] = need
					v[need] = true
					v[next] = true
					break
				}
			}
		}
		if start == MAX {
			fmt.Println("NO")
			return
		}
	}
	fmt.Println("YES")
	var buffer bytes.Buffer
	for i := 0; i < len(res); i++ {
		if i != 0 {
			buffer.WriteString(" ")
		}
		buffer.WriteString(strconv.Itoa(res[i]))
	}
	fmt.Println(buffer.String())
}

func shuffleArray(a []int) {
	n := len(a)
	for i := 0; i < n; i++ {
		pos := i + rand.Intn(n-i)
		a[i], a[pos] = a[pos], a[i]
	}
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

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

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

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"math/rand"
	"os"
	"strconv"
)

func solve() {
	a := make([]int, 1e6)
	v := make([]bool, len(a))
	n := readInt()
	x := readInt()
	for i := 0; i < len(a); i++ {
		a[i] = i
	}
	shuffleArray(a)
	res := make([]int, 1)
	res[0] = x
	nextIndex := 0
	for i := 0; i < n-1; i++ {
		last := res[len(res)-1]
		for nextIndex < len(a) {
			next := a[nextIndex]
			nextIndex++
			if !v[next] {
				looking := next ^ last
				if looking < len(v) && !v[looking] {
					res[len(res)-1] = next
					res = append(res, looking)
					v[looking] = true
					v[next] = true
					break
				}
			}
		}
		if nextIndex == len(a) {
			fmt.Println("NO")
			return
		}
	}
	fmt.Println("YES")
	var buffer bytes.Buffer
	for i := 0; i < len(res); i++ {
		if i != 0 {
			buffer.WriteString(" ")
		}
		buffer.WriteString(strconv.Itoa(res[i]))
	}
	fmt.Println(buffer.String())
}

func shuffleArray(a []int) {
	n := len(a)
	for i := 0; i < n; i++ {
		pos := i + rand.Intn(n-i)
		a[i], a[pos] = a[pos], a[i]
	}
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

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

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 CF862C(in io.Reader, _w io.Writer) {
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n, x int
	Fscan(bufio.NewReader(in), &n, &x)

	var a []int
	f := func(m int) {
		mask := 0
		for i := 0; i < m-1; i++ {
			v := 1 << (19 - i)
			mask ^= v
			a = append(a, v)
		}
		a = append(a, mask)
		a[0] ^= x
		n -= m
	}
	switch n % 4 {
	case 0:
		f(4)
	case 2:
		if n == 2 {
			if x == 0 {
				Fprint(out, "NO")
			} else {
				Fprint(out, "YES\n0 ", x)
			}
			return
		}
		f(6)
	default:
		f(n % 4)
	}

	Fprintln(out, "YES")
	has := map[int]bool{}
	for _, v := range a {
		has[v] = true
		Fprint(out, v, " ")
	}
	for i := 0; n > 0; i += 4 {
		if has[i] || has[i+1] || has[i+2] || has[i+3] {
			continue
		}
		Fprint(out, i, i+1, i+2, i+3, " ")
		n -= 4
	}
}

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


RedDreamer has an array $$$a$$$ consisting of $$$n$$$ non-negative integers, and an unlucky integer $$$T$$$.Let's denote the misfortune of array $$$b$$$ having length $$$m$$$ as $$$f(b)$$$ — the number of pairs of integers $$$(i, j)$$$ such that $$$1 \le i < j \le m$$$ and $$$b_i + b_j = T$$$. RedDreamer has to paint each element of $$$a$$$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $$$c$$$ and $$$d$$$ so that all white elements belong to $$$c$$$, and all black elements belong to $$$d$$$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $$$f(c) + f(d)$$$ is minimum possible.For example:  if $$$n = 6$$$, $$$T = 7$$$ and $$$a = [1, 2, 3, 4, 5, 6]$$$, it is possible to paint the $$$1$$$-st, the $$$4$$$-th and the $$$5$$$-th elements white, and all other elements black. So $$$c = [1, 4, 5]$$$, $$$d = [2, 3, 6]$$$, and $$$f(c) + f(d) = 0 + 0 = 0$$$;  if $$$n = 3$$$, $$$T = 6$$$ and $$$a = [3, 3, 3]$$$, it is possible to paint the $$$1$$$-st element white, and all other elements black. So $$$c = [3]$$$, $$$d = [3, 3]$$$, and $$$f(c) + f(d) = 0 + 1 = 1$$$. Help RedDreamer to paint the array optimally!

The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$T$$$ ($$$1 \le n \le 10^5$$$, $$$0 \le T \le 10^9$$$) — the number of elements in the array and the unlucky integer, respectively.  The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the elements of the array.  The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case print $$$n$$$ integers: $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ (each $$$p_i$$$ is either $$$0$$$ or $$$1$$$) denoting the colors. If $$$p_i$$$ is $$$0$$$, then $$$a_i$$$ is white and belongs to the array $$$c$$$, otherwise it is black and belongs to the array $$$d$$$. If there are multiple answers that minimize the value of $$$f(c) + f(d)$$$, print any of them.

package main

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

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

	tc := readNum(reader)
	var buf bytes.Buffer
	for tc > 0 {
		tc--
		n, T := readTwoNums(reader)
		A := readNNums(reader, n)
		res := solve(n, T, A)

		for i := 0; i < n; i++ {
			buf.WriteString(fmt.Sprintf("%d ", res[i]))
		}
		buf.WriteByte('\n')
	}
	fmt.Print(buf.String())
}

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

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

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

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

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

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

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

	return i
}

func solve(n int, T int, A []int) []int {
	ans := make([]int, n)
	var cnt int

	for i := 0; i < n; i++ {
		if A[i]*2 < T {
			ans[i] = 1
		} else if A[i]*2 == T {
			cnt++
			ans[i] = -1
		}
	}
	if cnt > 0 {
		// divides half
		h1 := cnt / 2
		for i := 0; i < n; i++ {
			if ans[i] == -1 {
				if h1 > 0 {
					ans[i] = 1
					h1--
				} else {
					ans[i] = 0
				}
			}
		}
	}
	return ans
}

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T, n, s, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &s)
		c := make([]interface{}, n)
		ps := []int{}
		for i := range c {
			if Fscan(in, &v); v <= s/2 {
				c[i] = 0
				if v == s/2 {
					ps = append(ps, i)
				}
			} else {
				c[i] = 1
			}
		}
		if s&1 == 0 {
			for _, p := range ps[:len(ps)/2] {
				c[p] = 1
			}
		}
		Fprintln(out, c...)
	}
}

package main

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

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

	tc := readNum(reader)
	var buf bytes.Buffer
	for tc > 0 {
		tc--
		n, T := readTwoNums(reader)
		A := readNNums(reader, n)
		res := solve(n, T, A)

		for i := 0; i < n; i++ {
			buf.WriteString(fmt.Sprintf("%d ", res[i]))
		}
		buf.WriteByte('\n')
	}
	fmt.Print(buf.String())
}

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

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

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

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

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

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

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

	return i
}

func solve(n int, T int, A []int) []int {
	ps := make([]Pair, n)

	for i := 0; i < n; i++ {
		ps[i] = Pair{A[i], i}
	}

	sort.Sort(Pairs(ps))

	ans := make([]int, n)

	var i, j = 0, n - 1
	var cnt int
	for i < j {
		tmp := ps[i].first + ps[j].first
		if tmp == T {
			if ps[i].first != ps[j].first {
				ans[ps[i].second] = 1
				ans[ps[j].second] = 0
			} else {
				ans[ps[i].second] = -1
				ans[ps[j].second] = -1
				cnt += 2
			}
			i++
			j--
		} else if tmp > T {
			ans[ps[j].second] = 0
			j--
		} else {
			ans[ps[i].second] = 1
			i++
		}
	}

	if i == j {
		if ps[i].first*2 != T || cnt == 0 {
			ans[ps[i].second] = 1
		} else {
			ans[ps[i].second] = -1
			cnt++
		}
	}

	if cnt > 0 {
		// divides half
		h1 := cnt / 2
		for i := 0; i < n; i++ {
			if ans[i] == -1 {
				if h1 > 0 {
					ans[i] = 1
					h1--
				} else {
					ans[i] = 0
				}
			}
		}
	}
	return ans
}

type Pair struct {
	first, second int
}

type Pairs []Pair

func (ps Pairs) Len() int {
	return len(ps)
}

func (ps Pairs) Less(i, j int) bool {
	return ps[i].first < ps[j].first
}

func (ps Pairs) Swap(i, j int) {
	ps[i], ps[j] = ps[j], ps[i]
}


The ancient Berlanders believed that the longer the name, the more important its bearer is. Thus, Berland kings were famous for their long names. But long names are somewhat inconvenient, so the Berlanders started to abbreviate the names of their kings. They called every king by the first letters of its name. Thus, the king, whose name was Victorious Vasily Pupkin, was always called by the berlanders VVP.In Berland over its long history many dynasties of kings replaced each other, but they were all united by common traditions. Thus, according to one Berland traditions, to maintain stability in the country, the first name of the heir should be the same as the last name his predecessor (hence, the first letter of the abbreviated name of the heir coincides with the last letter of the abbreviated name of the predecessor). Berlanders appreciate stability, so this tradition has never been broken. Also Berlanders like perfection, so another tradition requires that the first name of the first king in the dynasty coincides with the last name of the last king in this dynasty (hence, the first letter of the abbreviated name of the first king coincides with the last letter of the abbreviated name of the last king). This tradition, of course, has also been always observed.The name of a dynasty is formed by very simple rules: we take all the short names of the kings in the order in which they ruled, and write them in one line. Thus, a dynasty of kings "ab" and "ba" is called "abba", and the dynasty, which had only the king "abca", is called "abca".Vasya, a historian, has recently found a list of abbreviated names of all Berland kings and their relatives. Help Vasya to find the maximally long name of the dynasty that could have existed in Berland.Note that in his list all the names are ordered by the time, that is, if name A is earlier in the list than B, then if A and B were kings, then king A ruled before king B.

The first line contains integer n (1 ≤ n ≤ 5·105) — the number of names in Vasya's list. Next n lines contain n abbreviated names, one per line. An abbreviated name is a non-empty sequence of lowercase Latin letters. Its length does not exceed 10 characters.

Print a single number — length of the sought dynasty's name in letters. If Vasya's list is wrong and no dynasty can be found there, print a single number 0.

NoteIn the first sample two dynasties can exist: the one called "abcca" (with the first and second kings) and the one called "abccba" (with the first and third kings). In the second sample there aren't acceptable dynasties.The only dynasty in the third sample consists of one king, his name is "c".

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  n := getI()
  snake := make([][]int, 26)
  for a := range snake {
    snake[a] = make([]int, 26)
  }

  for i := 0; i < n; i++ {
    name := getS()
    x := len(name)
    b := int(name[0] - 'a')
    c := int(name[x - 1] - 'a')
    for a := range snake {
      if snake[a][b] != 0 && snake[a][b] + x > snake[a][c] {
        snake[a][c] = snake[a][b] + x
      }
    }
    if x > snake[b][c] {
      snake[b][c] = x
    }
  }

  best := 0
  for a := range snake {
    if snake[a][a] > best {
      best = snake[a][a]
    }
  }
  writer.WriteString(fmt.Sprintf("%d\n", best))
}


This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.The jury has chosen a string $$$s$$$ consisting of $$$n$$$ characters; each character of $$$s$$$ is a lowercase Latin letter. Your task is to guess this string; initially, you know only its length.You may ask queries of two types:  $$$1$$$ $$$i$$$ — the query of the first type, where $$$i$$$ is an integer from $$$1$$$ to $$$n$$$. In response to this query, the jury will tell you the character $$$s_i$$$;  $$$2$$$ $$$l$$$ $$$r$$$ — the query of the second type, where $$$l$$$ and $$$r$$$ are integers such that $$$1 \le l \le r \le n$$$. In response to this query, the jury will tell you the number of different characters among $$$s_l, s_{l+1}, \dots, s_r$$$. You are allowed to ask no more than $$$26$$$ queries of the first type, and no more than $$$6000$$$ queries of the second type. Your task is to restore the string $$$s$$$.For each test in this problem, the string $$$s$$$ is fixed beforehand, and will be the same for every submission.

Initially, the jury program sends one integer $$$n$$$ on a separate line — the size of $$$s$$$ ($$$1 \le n \le 1000$$$).

To give the answer, print one line ! s with a line break in the end, where $$$s$$$ should be the string picked by the jury. After that, your program should flush the output and terminate gracefully.

NoteLet's analyze the example of interaction.The string chosen by the jury is guess, so initially the jury sends one integer $$$5$$$.  the first query is ? 2 1 5, which means "count the number of different characters among $$$s_1, s_2, \dots, s_5$$$". The answer to it is $$$4$$$.  the second query is ? 1 2, which means "tell which character is $$$s_2$$$". The answer to it is u.  the third query is ? 2 1 2, which means "count the number of different characters among $$$s_1$$$ and $$$s_2$$$". The answer to it is $$$2$$$.  the fourth query is ? 1 1, which means "tell which character is $$$s_1$$$". The answer to it is g.  the fifth query is ? 1 3, which means "tell which character is $$$s_3$$$". The answer to it is e.  the sixth query is ? 1 4, which means "tell which character is $$$s_4$$$". The answer to it is s.  the seventh query is ? 2 4 5, which means "count the number of different characters among $$$s_4$$$ and $$$s_5$$$". The answer to it is $$$1$$$, so it's possible to deduce that $$$s_4$$$ is the same as $$$s_5$$$.In the end, the answer is submitted as ! guess, and it is deduced correctly.

package main

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

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

	q1 := func(n int) rune {
		fmt.Fprintf(out, "? 1 %d\n", n+1)
		out.Flush()

		var s string
		fmt.Fscan(in, &s)
		return rune(s[0])
	}

	q2 := func(l, r int) int {
		fmt.Fprintf(out, "? 2 %d %d\n", l+1, r+1)
		out.Flush()

		var n int
		fmt.Fscan(in, &n)
		return n
	}

	var n int
	fmt.Fscan(in, &n)
	s := make([]rune, n)
	m := make([]int, 26)
	var cnt int
	for i := 0; i < n; i++ {
		l, r := -1, cnt
		for r-l > 1 {
			mid := (l + r) / 2
			if q2(m[mid], i) < cnt-mid+1 {
				l = mid
			} else {
				r = mid
			}
		}

		if l == -1 {
			s[i] = q1(i)
			m[cnt] = i
			cnt++
		} else {
			s[i] = s[m[l]]
			for j := l; j+1 < cnt; j++ {
				m[j] = m[j+1]
			}
			m[cnt-1] = i
		}
	}
	fmt.Fprintf(out, "! %s\n", string(s))
}

package main

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

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

type entry struct {
	a byte
	i int
}

func main() {
	defer w.Flush()
	var n int
	fmt.Fscan(r, &n)
	last := make([]entry, 0)
	fmt.Fprintln(w, "? 1 1")
	w.Flush()
	var ans string
	fmt.Fscan(r, &ans)
	last = append(last, entry{ans[0], 0})
	for i:=1;i<n;i++ {
		if len(last) != 26 {
			fmt.Fprintf(w, "? 2 %d %d\n", 1, i+1)
			w.Flush()
			var cnt int
			fmt.Fscan(r, &cnt)
			if cnt == len(last) {
				left, right := 0, len(last)-1
				for left < right {
					m := (left + right) / 2
					fmt.Fprintf(w, "? 2 %d %d\n", last[m].i+2, i+1)
					w.Flush()
					var cnt int
					fmt.Fscan(r, &cnt)
					if cnt == len(last) - m - 1 {
						left = m + 1
					} else {
						right = m
					}
				}
				ans += string([]byte{last[left].a})
				last[left].i = i
				sort.Slice(last, func(i, j int) bool {
					return last[i].i < last[j].i
				})
			} else {
				fmt.Fprintf(w, "? 1 %d\n", i+1)
				w.Flush()
				var input string
				fmt.Fscan(r, &input)
				last = append(last, entry{input[0], i})
				sort.Slice(last, func(i, j int) bool {
					return last[i].i < last[j].i
				})
				ans += string([]byte{input[0]})
			}
		} else {
			left, right := 0, 25
			for left < right {
				m := (left + right) / 2
				fmt.Fprintf(w, "? 2 %d %d\n", last[m].i+2, i+1)
				w.Flush()
				var cnt int
				fmt.Fscan(r, &cnt)
				if cnt == 25 - m {
					left = m + 1
				} else {
					right = m
				}
			}
			ans += string([]byte{last[left].a})
			last[left].i = i
			sort.Slice(last, func(i, j int) bool {
				return last[i].i < last[j].i
			})
		}
	}
	fmt.Fprintf(w, "! %s", ans)
}

package main

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

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

	q1 := func(n int) rune {
		fmt.Fprintf(out, "? 1 %d\n", n+1)
		out.Flush()

		var s string
		fmt.Fscan(in, &s)
		return rune(s[0])
	}

	q2 := func(l, r int) int {
		fmt.Fprintf(out, "? 2 %d %d\n", l+1, r+1)
		out.Flush()

		var n int
		fmt.Fscan(in, &n)
		return n
	}

	var n int
	fmt.Fscan(in, &n)
	s := make([]rune, n)
	m := make([]int, 26)
	var cnt int
	for i := 0; i < n; i++ {
		l, r := -1, cnt
		for r-l > 1 {
			mid := (l + r) / 2
			if q2(m[mid], i) < cnt-mid+1 {
				l = mid
			} else {
				r = mid
			}
		}

		if l == -1 {
			s[i] = q1(i)
			cnt++
			m[cnt] = i
		} else {
			s[i] = s[m[l]]
			for j := l; j+1 < cnt; j++ {
				m[j] = m[j+1]
			}
			m[cnt-1] = i
		}
	}
	fmt.Fprintf(out, "! %s\n", string(s))
}

package main

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

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

	q1 := func(n int) rune {
		fmt.Fprintf(out, "? 1 %d\n", n)
		out.Flush()

		var s string
		fmt.Fscan(in, &s)
		return rune(s[0])
	}

	q2 := func(l, r int) int {
		fmt.Fprintf(out, "? 2 %d %d\n", l+1, r+1)
		out.Flush()

		var n int
		fmt.Fscan(in, &n)
		return n
	}

	var n int
	fmt.Fscan(in, &n)
	s := make([]rune, n)
	m := make([]int, 26)
	var cnt int
	for i := 0; i < n; i++ {
		l, r := -1, cnt
		for r-l > 1 {
			mid := (l + r) / 2
			if q2(m[mid], i) < cnt-mid+1 {
				l = mid
			} else {
				r = mid
			}
		}

		if l == -1 {
			s[i] = q1(i)
			cnt++
			m[cnt] = i
		} else {
			s[i] = s[m[l]]
			for j := l; j+1 < cnt; j++ {
				m[j] = m[j+1]
			}
			m[cnt-1] = i
		}
	}
	fmt.Fprintf(out, "! %s\n", string(s))
}

package main

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

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

	q1 := func(n int) rune {
		fmt.Fprintf(out, "? 1 %d\n", n)
		out.Flush()

		var s string
		fmt.Fscan(in, &s)
		return rune(s[0])
	}

	q2 := func(l, r int) int {
		fmt.Fprintf(out, "? 2 %d %d\n", l+1, r+1)
		out.Flush()

		var n int
		fmt.Fscan(in, &n)
		return n
	}

	var n int
	fmt.Fscan(in, &n)
	s := make([]rune, n)
	m := make([]int, 26)
	var cnt int
	for i := 0; i < n; i++ {
		l, r := -1, cnt
		for r-l > 1 {
			mid := (l + r) / 2
			if q2(m[mid], i) < cnt-mid+1 {
				l = mid
			} else {
				r = mid
			}
		}

		if l == -1 {
			s[i] = q1(i)
			cnt++
			m[cnt] = i
		} else {
			s[i] = s[m[l]]
			for j := l; j+1 < cnt; j++ {
				m[j] = m[j+1]
			}
			m[cnt-1] = i
		}
	}
	fmt.Fprintln(out, string(s))
}

package main

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

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

type entry struct {
	a byte
	i int
}

func main() {
	defer w.Flush()
	var n int
	fmt.Fscan(r, &n)
	last := make([]entry, 0)
	fmt.Fprintln(w, "? 1 1")
	w.Flush()
	var ans string
	fmt.Fscan(r, &ans)
	last = append(last, entry{ans[0], 0})
	for i:=1;i<n;i++ {
		if len(last) != 26 {
			fmt.Fprintf(w, "? 2 %d %d\n", 1, i+1)
			w.Flush()
			var cnt int
			fmt.Fscan(r, &cnt)
			if cnt == len(last) {
				left, right := 0, len(last)-1
				for left < right {
					m := (left + right) / 2
					fmt.Fprintf(w, "? 2 %d %d\n", last[m].i+2, i+1)
					w.Flush()
					var cnt int
					fmt.Fscan(r, &cnt)
					if cnt == len(last) - m - 1 {
						left = m + 1
					} else {
						right = m
					}
				}
				ans += string([]byte{last[left].a})
				last[left].i = i
				sort.Slice(last, func(i, j int) bool {
					return last[i].i < last[j].i
				})
			} else {
				fmt.Fprintf(w, "? 1 %d\n", i+1)
				w.Flush()
				var input string
				fmt.Fscan(r, &input)
				last = append(last, entry{input[0], i})
				sort.Slice(last, func(i, j int) bool {
					return last[i].i < last[j].i
				})
				ans += string([]byte{input[0]})
			}
		} else {
			left, right := 0, 25
			for left < right {
				m := (left + right) / 2
				fmt.Fprintf(w, "? 2 %d %d\n", last[m].i+1, i+1)
				w.Flush()
				var cnt int
				fmt.Fscan(r, &cnt)
				if cnt == 25 - m {
					left = m + 1
				} else {
					right = m
				}
			}
			ans += string([]byte{last[left].a})
			last[left].i = i
			sort.Slice(last, func(i, j int) bool {
				return last[i].i < last[j].i
			})
		}
	}
	fmt.Fprintf(w, "! %s", ans)
}


You have matrix a of size n × n. Let's number the rows of the matrix from 1 to n from top to bottom, let's number the columns from 1 to n from left to right. Let's use aij to represent the element on the intersection of the i-th row and the j-th column. Matrix a meets the following two conditions:   for any numbers i, j (1 ≤ i, j ≤ n) the following inequality holds: aij ≥ 0;  . Matrix b is strictly positive, if for any numbers i, j (1 ≤ i, j ≤ n) the inequality bij > 0 holds. You task is to determine if there is such integer k ≥ 1, that matrix ak is strictly positive.

The first line contains integer n (2 ≤ n ≤ 2000) — the number of rows and columns in matrix a. The next n lines contain the description of the rows of matrix a. The i-th line contains n non-negative integers ai1, ai2, ..., ain (0 ≤ aij ≤ 50). It is guaranteed that .

If there is a positive integer k ≥ 1, such that matrix ak is strictly positive, print "YES" (without the quotes). Otherwise, print "NO" (without the quotes).

package main

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

const maxn = 2010

var s [2][maxn]int
var a [maxn][maxn]int
var n int

func dfs(x int, left int) int {
	ans := 1
	s[left][x] = 1
	for i := 0; i < n; i++ {
		if s[left][i] == 0 {
			if left == 1 && a[i][x] != 0 {
				ans += dfs(i, left)
			} else if left == 0 && a[x][i] != 0 {
				ans += dfs(i, left)
			}
		}
	}
	return ans
}

func main() {
	scanner.Split(bufio.ScanWords)
	n = scanner.NextInt()
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			a[i][j] = scanner.NextInt()
		}
		s[1][i] = 0
		s[0][i] = 0
	}
	if dfs(0, 1) == n && dfs(0, 0) == n {
		fmt.Printf("YES")
	} else {
		fmt.Printf("NO")
	}
}

type Scanner struct {
	*bufio.Scanner
}

var scanner = Scanner{bufio.NewScanner(os.Stdin)}

func (self *Scanner) Next() string {
	if !self.Scan() {
		panic(self.Err())
	}
	return self.Text()
}

func (self *Scanner) NextInt() int {
	x, err := strconv.Atoi(self.Next())
	if err != nil {
		panic(err)
	}
	return x
}

func (self *Scanner) NextDouble() float64 {
	x, err := strconv.ParseFloat(self.Next(), 64)
	if err != nil {
		panic(err)
	}
	return x
}