You are given an array of positive integers $$$a_1, a_2, \ldots, a_n$$$.Make the product of all the numbers in the array (that is, $$$a_1 \cdot a_2 \cdot \ldots \cdot a_n$$$) divisible by $$$2^n$$$.You can perform the following operation as many times as you like:  select an arbitrary index $$$i$$$ ($$$1 \leq i \leq n$$$) and replace the value $$$a_i$$$ with $$$a_i=a_i \cdot i$$$. You cannot apply the operation repeatedly to a single index. In other words, all selected values of $$$i$$$ must be different.Find the smallest number of operations you need to perform to make the product of all the elements in the array divisible by $$$2^n$$$. Note that such a set of operations does not always exist.

The first line of the input contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4$$$) — the number test cases. Then the descriptions of the input data sets follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains exactly $$$n$$$ integers: $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$). It is guaranteed that the sum of $$$n$$$ values over all test cases in a test does not exceed $$$2 \cdot 10^5$$$.

For each test case, print the least number of operations to make the product of all numbers in the array divisible by $$$2^n$$$. If the answer does not exist, print -1.

NoteIn the first test case, the product of all elements is initially $$$2$$$, so no operations needed.In the second test case, the product of elements initially equals $$$6$$$. We can apply the operation for $$$i = 2$$$, and then $$$a_2$$$ becomes $$$2\cdot2=4$$$, and the product of numbers becomes $$$3\cdot4=12$$$, and this product of numbers is divided by $$$2^n=2^2=4$$$. In the fourth test case, even if we apply all possible operations, we still cannot make the product of numbers divisible by $$$2^n$$$  — it will be $$$(13\cdot1)\cdot(17\cdot2)\cdot(1\cdot3)\cdot(1\cdot4)=5304$$$, which does not divide by $$$2^n=2^4=16$$$.In the fifth test case, we can apply operations for $$$i = 2$$$ and $$$i = 4$$$.

package main

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

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

func main() {
	var t int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &arr[j])
		}
		//s := ""
		//if i == 52 {
		//	for _, x := range arr {
		//		s = s + " " + strconv.FormatInt(int64(x), 10)
		//	}
		//	fmt.Println(s)
		//}
		sum2Pos := 0
		indexes := make([]int, n)

		for i, x := range arr {
			sum2Pos += countPow2(x)
			indexes[i] = countPow2(i + 1)
		}
		sort.Ints(indexes)
		//diff := int(math.Pow(2, float64(n-sum2Pos)))
		diff := n - sum2Pos
		if diff <= 0 {
			fmt.Println(0)
		} else {
			count := 0
			for k := len(indexes) - 1; k >= 0; k-- {
				if diff == 0 {
					break
				}
				x := indexes[k]
				if x > 0 && x <= diff {
					count++
					diff = diff - x
				}
			}
			if diff == 0 {
				fmt.Println(count)
			} else {
				fmt.Println(-1)
			}
		}
	}
}

func countPow2(x int) int {
	temp := x
	count := 0
	for temp%2 == 0 {
		temp = temp / 2
		count++
	}
	return count
}

func GCD(a, b int) int {
	for a != b {
		if a > b {
			a -= b
		} else {
			b -= a
		}
	}

	return a
}

func max[T Ordered](x, y T) T {
	if x > y {
		return x
	}
	return y
}

type Ordered interface {
	int | float64 | ~string
}

package main

import (
	"bufio"
	"fmt"
	"math/bits"
	"os"
	"sort"
)

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 n int
		fmt.Fscan(in, &n)

		var z int
		aa := make([]int, n)
		bb := []int{}
		for i := 0; i < n; i++ {
			var v uint
			fmt.Fscan(in, &v)

			aa[i] += bits.TrailingZeros(v)
			z += aa[i]
			if i%2 != 0 {
				d := bits.TrailingZeros(uint(i + 1))
				bb = append(bb, d)
			}
		}

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

		var c int
		for _, v := range bb {
			if z >= n {
				break
			}
			z += v
			c++
		}
		if z < n {
			fmt.Fprintln(out, -1)
		} else {
			fmt.Fprintln(out, c)
		}
	}
}

// LUOGU_RID: 92429608
package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/bits"
	"os"
	"sort"
)

// https://space.bilibili.com/206214
func CF1744D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T, n, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		s := 0
		c2 := make([]int, n)
		for i := range c2 {
			Fscan(in, &v)
			s += bits.TrailingZeros(uint(v))
			c2[i] = bits.TrailingZeros(uint(i + 1))
		}
		sort.Ints(c2)
		i := n - 1
		for ; i >= 0 && s < n; i-- {
			s += c2[i]
		}
		if i < 0 {
			Fprintln(out, -1)
		} else {
			Fprintln(out, n-1-i)
		}
	}
}
func main() { CF1744D(os.Stdin, os.Stdout) }

package main
import (
	"bufio"
	. "fmt"
	"io"
	"os"
	//. "strconv" Itoa
)
var mp=[200010][22]int{}
func init(){
	for i:=2;i<=200000;i++{
		cn:=i
		cnt:=0
		for (cn%2==0)&&cn!=0{
			cn/=2
			cnt++
		}
		for j:=1;j<=20;j++{
			mp[i][j]=mp[i-1][j]
		}
		mp[i][cnt]++
	}
}
func solve(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)

	defer out.Flush()

	var T,x int
	for Fscan(in,&T);T>0;T--{
		var n int
		Fscan(in,&n)
		cn:=n
		for i:=0;i<n;i++{
			Fscan(in,&x)
			for (x%2)==0&&x!=0{
				x/=2
				cn--
			}
		}
		if cn<=0{
			Fprintln(out,"0")
			continue
		}else{
			ans:=0
			for i:=20;i>=1&&cn>0;i--{
				if mp[n][i]>0{
					mn:=cn/i
					if cn%i!=0{mn++}
					mn=min(mn,mp[n][i])
					cn-=mn*i
					ans+=mn
				}
			}
			if cn<=0{
				Fprintln(out,ans)
			}else{
				Fprintln(out,"-1")
			}
		}
		// 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 20 21 22 23 24
		//   1   2   1   3   1     2     1     4     1  2     1     3
	}
	// Fprintln(out,mp[200000])
}
func main() { solve(os.Stdin, os.Stdout) }

func max(i int, j int) int {
	if (i > j) {
		return i;
	} else {
		return j;
	}
}
func min(i int, j int) int {
	if (i < j) {
		return i;
	} else {
		return j;
	}
}

package main

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

var reader = bufio.NewReader(os.Stdin)

var n, m int

func main() {
	var t int

	fmt.Fscan(reader, &t)

	for t > 0 {
		t--
		var n int
		fmt.Fscan(reader, &n)
		var num int
		needed := n
		have := 0
		divs := make([]int, 0, n/4)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &num)
			for num%2 == 0 {
				have++
				num /= 2
			}
			cur := i + 1
			val := 0
			// 2 4 6 // the middle 4 gives most, we will first multiple by the 4
			for cur%2 == 0 { // cur_index % 2 = 0
				val++    //
				cur /= 2 // cur_index /= 2
			}
			if val > 0 {
				divs = append(divs, val) // index will contribute val,
			}
		}

		if have >= needed { // already satisfied
			fmt.Println(0)
			continue
		}
		sort.Ints(divs)

		ans := 0
		// count the contribution
		for i := len(divs) - 1; i >= 0; i-- {
			have += divs[i]
			ans++
			if have >= needed {
				break
			}
		}
		if have < needed {
			fmt.Println(-1)
			continue
		}
		fmt.Println(ans)

	}
}

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

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

package main

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

func solution_1(input io.Reader, output io.Writer) {
	in := bufio.NewReader(input)
	var n int
	_, err := fmt.Fscanf(in, "%d\n", &n)
	if err != nil {
		panic(err)
	}

	for i := 0; i < n; i++ {
		var narr int
		_, err := fmt.Fscanf(in, "%d\n", &narr)
		if err != nil {
			panic(err)
		}

		arr := make([]int, 0, narr)
		for j := 0; j < narr; j++ {
			var v int
			_, err := fmt.Fscanf(in, "%d", &v)
			if err != nil {
				panic(err)
			}
			arr = append(arr, v)
		}
		fmt.Fscanf(in, "\n")
		var s string
		_, err = fmt.Fscanf(in, "%s\n", &s)
		fmt.Fprintln(output, numberReplacement(arr, s))
	}
}

func solution_2(input io.Reader, output io.Writer) {
	in := bufio.NewReader(input)
	var n int
	_, err := fmt.Fscanf(in, "%d\n", &n)
	if err != nil {
		panic(err)
	}

	for i := 0; i < n; i++ {
		var narr, q int
		_, err := fmt.Fscanf(in, "%d %d\n", &narr, &q)
		if err != nil {
			panic(err)
		}

		arr := make([]int, 0, narr)
		for j := 0; j < narr; j++ {
			var v int
			_, err := fmt.Fscanf(in, "%d", &v)
			if err != nil {
				panic(err)
			}
			arr = append(arr, v)
		}
		fmt.Fscanf(in, "\n")

		nOdd := 0
		nEven := 0
		sum := 0

		for _, n := range arr {
			sum += n
			if n%2 == 0 {
				nEven++
			} else {
				nOdd++
			}
		}
		for j := 0; j < q; j++ {
			var a, b int
			fmt.Fscanf(in, "%d %d\n", &a, &b)
			if a == 0 {
				sum += nEven * b
				if b%2 != 0 {
					nOdd += nEven
					nEven = 0
				}
			}

			if a == 1 {
				sum += nOdd * b
				if b%2 != 0 {
					nEven += nOdd
					nOdd = 0
				}
			}
			fmt.Fprintln(output, sum)
		}

	}
}

func numberReplacement(arr []int, s string) string {
	if len(arr) != len(s) {
		return "NO"
	}

	someMap := make(map[int]byte)
	for i, n := range arr {
		if v, ok := someMap[n]; !ok {
			someMap[n] = s[i]
		} else {
			if v != s[i] {
				return "NO"
			}
		}
	}

	return "YES"
}

func solution_3(input io.Reader, output io.Writer) {
	in := bufio.NewReader(input)
	var n int
	_, err := fmt.Fscanf(in, "%d\n", &n)
	if err != nil {
		panic(err)
	}

	for i := 0; i < n; i++ {
		var narr int
		var colour string
		_, err := fmt.Fscanf(in, "%d %s\n", &narr, &colour)
		if err != nil {
			panic(err)
		}

		var s string
		_, err = fmt.Fscanf(in, "%s\n", &s)
		if err != nil {
			panic(err)
		}
		fmt.Fprintln(output, waitTime(s, colour))
	}
}

func waitTime(s string, c string) int {
	if c == "g" {
		return 0
	}
	maxRes := 0
	firstGreenIndex := -1
	i := len(s) - 1
	curDist := 1

	for {
		if s[i] == 'g' {
			firstGreenIndex = i
			break
		}
		i--
	}

	i--
	if i < 0 {
		i = len(s) - 1
	}
	distances := make([]int, len(s))

	for i != firstGreenIndex {
		if s[i] == 'g' {
			curDist = 0
		}
		distances[i] = curDist
		curDist++
		i--
		if i < 0 {
			i = len(s) - 1
		}
	}

	for i := 0; i < len(s); i++ {
		if s[i] == c[0] {
			curDist := distances[i]
			if curDist > maxRes {
				maxRes = curDist
			}
		}
	}

	return maxRes
}

func solution_4(input io.Reader, output io.Writer) {
	in := bufio.NewReader(input)
	var n int
	_, err := fmt.Fscanf(in, "%d\n", &n)
	if err != nil {
		panic(err)
	}

	for i := 0; i < n; i++ {
		var narr int
		_, err := fmt.Fscanf(in, "%d\n", &narr)
		if err != nil {
			panic(err)
		}

		arr := make([]int, 0, narr)
		for j := 0; j < narr; j++ {
			var v int
			_, err := fmt.Fscanf(in, "%d", &v)
			if err != nil {
				panic(err)
			}
			arr = append(arr, v)
		}
		fmt.Fscanf(in, "\n")
		fmt.Fprintln(output, numOpertions(arr))
	}
}

func numOpertions(arr []int) int {
	N := len(arr)
	l := 0
	r := 0
	pows := make([]int, 0)

	for ii, n := range arr {
		l = 0
		i := ii + 1
		l = getPowerOf2(i)
		r = 0
		for n >= 2 && n%2 == 0 {
			n /= 2
			r++
		}
		if r >= N {
			return 0
		}
		N -= r
		if l > 0 {
			pows = append(pows, l)
		}
	}

	res := 0
	sort.Slice(pows, func(i, j int) bool {
		return pows[i] > pows[j]
	})
	// fmt.Println(N, pows)
	i := 0
	for N > 0 {
		if i >= len(pows) {
			break
		}
		N -= pows[i]
		i++
		res++
	}
	if N > 0 {
		return -1
	}
	return res
}

func getPowerOf2(n int) int {
	power := 0
	for n >= 2 && n%2 == 0 {
		n /= 2
		power++
	}
	return power
}

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

package main

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

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

func main() {
	defer writer.Flush()

	var T int
	fmt.Fscan(reader, &T)

	for ; T > 0; T-- {
		var n int
		fmt.Fscan(reader, &n)

		a := make([]int, n)
		tot := 0
		for i := 0; i < n; i++ {
			var val int
			fmt.Fscan(reader, &val)

			for ; val%2 == 0; val /= 2 {
				tot += 1
			}

			for tmp := i + 1; tmp%2 == 0; tmp /= 2 {
				a[i] += 1
			}
		}

		if tot >= n {
			fmt.Fprintf(writer, "%d\n", 0)
		} else {
			ans := 0

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

			for i := 0; i < n && tot < n; i++ {
				tot += a[i]
				ans += 1
			}

			if tot < n {
				ans = -1
			}
			fmt.Fprintf(writer, "%d\n", ans)
		}
	}
}

package main

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

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

func solve() {
    var n int
    Fscan(in, &n)

    cnt := 0
    var a []int
    for i := 1; i <= n; i++ {
        var x int
        Fscan(in, &x)

        for ; x % 2 == 0; x /= 2 {
            cnt++
        }

        c := 0
        for j := i; j % 2 == 0; j /= 2 {
            c++
        }
        a = append(a, c)
    }

    sort.Sort(sort.Reverse(sort.IntSlice(a)))

    ans := 0
    for i := range a {
        if cnt >= n {
            break
        }

        ans++
        cnt += a[i]
    }

    if cnt >= n {
        Fprintln(out, ans)
    } else {
        Fprintln(out, -1)
    }
}

func main() {
    var T int
    Fscan(in, &T)
    for ; T > 0; T-- {
        solve()
    }
    out.Flush()
}

package main

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

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

func solve() {
	n := _readInt()
	var cnt, res int
	for i := 0; i < n; i++ {
		x := _readInt()
		for x > 0 && x%2 == 0 {
			x /= 2
			cnt++
		}
	}

	if cnt >= n {
		_print(0)
		return
	}

	counts := make([]int, 0)
	for i := 1; i <= n; i++ {
		x := i
		if x%2 != 0 {
			continue
		}
		var tmp int
		for x > 0 && x%2 == 0 {
			x /= 2
			tmp++
		}
		counts = append(counts, tmp)
	}
	sort.Ints(counts)
	_reverseInts(counts)

	for _, v := range counts {
		res++
		cnt += v
		if cnt >= n {
			_print(res)
			return
		}
	}

	_print(-1)
}

func main() {
	defer out.Flush()
	var t int
	for fmt.Fscanln(in, &t); t > 0; t-- {
		solve()
	}
}

// ====
func _readInt() int {
	var x int
	fmt.Fscan(in, &x)
	return x
}
func _readStr() string {
	var x string
	fmt.Fscan(in, &x)
	return x
}
func _readArr() (int, []int) {
	var n int
	fmt.Fscan(in, &n)
	var v = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &v[i])
	}
	return n, v
}
func _print(x ...any) {
	fmt.Fprintln(out, x...)
}
func _max(x, y int) int {
	if x > y {
		return x
	}
	return y
}
func _min(x, y int) int {
	if x < y {
		return x
	}
	return y
}
func _abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}
func _sortStr(x string) string {
	r := []rune(x)
	sort.Slice(r, func(i, j int) bool {
		return r[i] < r[j]
	})
	return string(r)
}
func _reverseInts(x []int) {
	n := len(x)
	for i := 0; i < n/2; i++ {
		x[i], x[n-1-i] = x[n-1-i], x[i]
	}
}
func _reverseStr(s string) string {
	x := []rune(s)
	n := len(x)
	for i := 0; i < n/2; i++ {
		x[i], x[n-1-i] = x[n-1-i], x[i]
	}
	return string(x)
}

package main

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

const cf = true

func solve() {
	var n int
	fmt.Fscan(in, &n)
	var num int
	needed := n
	have := 0
	divs := make([]int, 0, n/4)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &num)
		for num%2 == 0 {
			have++
			num /= 2
		}
		cur := i + 1
		val := 0
		for cur%2 == 0 {
			val++
			cur /= 2
		}
		if val > 0 {
			divs = append(divs, val)
		}
	}
	if have >= needed {
		fmt.Fprintln(out, 0)
		return
	}
	sort.Ints(divs)
	ans := 0
	for i := len(divs) - 1; i >= 0; i-- {
		have += divs[i]
		ans++
		if have >= needed {
			break
		}
	}
	if have < needed {
		fmt.Fprintln(out, -1)
		return
	}
	fmt.Fprintln(out, ans)
}

func main() {
	defer out.Flush()
	t := 1
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		solve()
	}
}

func GCD(a, b int) int {
	for a != b {
		if a > b {
			a -= b
		} else {
			b -= a
		}
	}

	return a
}

type SqrtInt interface {
	SignedSqrt | UnsignedSqrt
}

type SignedSqrt interface {
	~int | ~int32 | ~int64
}

type UnsignedSqrt interface {
	~uint | ~uint32 | ~uint64 | ~uintptr
}

func sqrt[T SqrtInt](x T) T {
	l, r := T(0), T(2000000123)
	for l < r {
		mid := (l + r) / 2
		if mid*mid > x {
			r = mid
		} else {
			l = mid + 1
		}
	}
	return l - 1
}

func ConvertInt(val string, base, toBase int) (string, error) {
	i, err := strconv.ParseInt(val, base, 64)
	if err != nil {
		return "", err
	}
	return strconv.FormatInt(i, toBase), nil
}

type set[T Ordered] struct {
	itms map[T]struct{}
}

func (s *set[T]) init() {
	if s.itms == nil {
		s.itms = make(map[T]struct{})
	}
}

func (s *set[T]) add(item T) *set[T] {
	s.init()
	s.itms[item] = exists
	return s
}

func (s *set[T]) remove(item T) *set[T] {
	s.init()
	delete(s.itms, item)
	return s
}

func (s *set[T]) has(item T) bool {
	s.init()
	_, c := s.itms[item]
	return c
}

func (s *set[T]) sortedItems() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	sort.Slice(items, func(i, j int) bool {
		return items[i] < items[j]
	})
	return items
}

func (s *set[T]) items() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	return items
}

func (s *set[T]) size() int {
	s.init()
	return len(s.itms)
}

var exists = struct{}{}

const MaxUint = ^uint(0)
const MinUint = 0
const MaxInt = int(MaxUint >> 1)
const MinInt = -MaxInt - 1

func reverseStr(s string) string {
	r := []byte(s)
	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
		r[i], r[j] = r[j], r[i]
	}
	return string(r)
}

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

type Ordered interface {
	Integer | Float | ~string
}

func abs[T Signed](x T) T {
	if x < 0 {
		return -x
	}
	return x
}

func min[T Ordered](x, y T) T {
	if x < y {
		return x
	}
	return y
}

func max[T Ordered](x, y T) T {
	if x > y {
		return x
	}
	return y
}

var (
	in  = getReader()
	out = getWriter()
)

func getReader() *bufio.Reader {
	if cf {
		return bufio.NewReader(os.Stdin)
	} else {
		in, _ := os.Open("input.txt")
		return bufio.NewReader(in)
	}
}

func getWriter() *bufio.Writer {
	if cf {
		return bufio.NewWriter(os.Stdout)
	} else {
		out, _ := os.Create("output.txt")
		return bufio.NewWriter(out)
	}
}

package main

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

var sc = bufio.NewScanner(os.Stdin)

const mod int = 998244353
const N int = 5e5 + 5

func main() {
	sc.Buffer(make([]byte, 500000*1+1), 500000)
	sc.Split(bufio.ScanWords)

	T := nextInt()
	for T > 0 {
		T--
		n := nextInt()
		a := make([]int, n)
		b := make([]int, n)
		x, y := 0, 0
		for i := 0; i < n; i++ {
			a[i] = nextInt()
			for a[i]%2 == 0 {
				a[i] /= 2
				x++
			}
			t, tt := i+1, 0
			for t%2 == 0 {
				t /= 2
				tt++
			}
			b[i] = tt
			y += tt
		}
		if x+y < n {
			fmt.Println("-1")
			continue
		}
		sort.Ints(b)
		i := n - 1
		for x < n {
			x += b[i]
			i--
		}
		fmt.Println(n - i - 1)
	}
}

func max(args ...int) int {
	res := args[0]
	for _, v := range args {
		if v > res {
			res = v
		}
	}
	return res
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func nextString() string {
	sc.Scan()
	return sc.Text()
}

package main

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

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

func main() {
	var t int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &arr[j])
		}
		sum2Pos := 0
		indexes := make([]int, n)

		for i, x := range arr {
			sum2Pos += countPow2(x)
			indexes[i] = countPow2(i + 1)
		}
		sort.Ints(indexes)
		diff := int(math.Pow(2, float64(n-sum2Pos)))
		diff = n - sum2Pos
		count := 0
		for k := len(indexes) - 1; k >= 0; k-- {
			if diff == 0 {
				break
			}
			x := indexes[k]
			if x > 0 {
				count++
				diff = diff - x
			}
		}
		if diff == 0 {
			fmt.Println(count)
		} else {
			fmt.Println(-1)
		}
	}
}

func countPow2(x int) int {
	temp := x
	count := 0
	for temp%2 == 0 {
		temp = temp / 2
		count++
	}
	return count
}

func GCD(a, b int) int {
	for a != b {
		if a > b {
			a -= b
		} else {
			b -= a
		}
	}

	return a
}

func max[T Ordered](x, y T) T {
	if x > y {
		return x
	}
	return y
}

type Ordered interface {
	int | float64 | ~string
}

package main

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

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

func main() {
	var t int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &arr[j])
		}
		mul := 1
		for _, x := range arr {
			mul *= x
		}
		if mul%int(math.Pow(2, float64(n))) != 0 {
			kx := 0
			for newMul := mul; newMul%2 == 0; newMul /= 2 {
				kx++
			}
			x := int(math.Pow(2, float64(n-kx)))
			found := false
			s := 0
			for k := len(arr); k > 0; k-- {
				if x == 1 {
					break
				}
				if k == x {
					found = true
					s++
					mul = mul * k
					break
				} else {
					if k < x && x%k == 0 {
						s++
						mul = mul * k
						x = x / k
					}
				}
			}
			st := math.Pow(2, float64(n))
			res := math.Mod(float64(mul), st)
			if found && res == 0.0 {
				fmt.Println(s)
			} else {
				fmt.Println(-1)
			}
		} else {
			fmt.Println(0)
		}
	}
}

func GCD(a, b int) int {
	for a != b {
		if a > b {
			a -= b
		} else {
			b -= a
		}
	}

	return a
}

func max[T Ordered](x, y T) T {
	if x > y {
		return x
	}
	return y
}

type Ordered interface {
	int | float64 | ~string
}

package main

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

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

func main() {
	var t int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &arr[j])
		}
		mul := 1
		for _, x := range arr {
			mul *= x
		}
		if mul%int(math.Pow(2, float64(n))) != 0 {
			kx := 0
			for newMul := mul; newMul%2 == 0; newMul /= 2 {
				kx++
			}
			x := int(math.Pow(2, float64(n-kx)))
			found := false
			s := 0
			for k := len(arr); k > 0; k-- {
				if x == 1 {
					break
				}
				if k == x {
					found = true
					s++
					mul = mul * k
					break
				} else {
					if k < x && GCD(x, k) != 1 {
						s++
						mul = mul * k
						x = x / k
					}
				}
			}
			st := math.Pow(2, float64(n))
			res := math.Mod(float64(mul), st)
			if found && res == 0.0 {
				fmt.Println(s)
			} else {
				fmt.Println(-1)
			}
		} else {
			fmt.Println(0)
		}
	}
}

func GCD(a, b int) int {
	for a != b {
		if a > b {
			a -= b
		} else {
			b -= a
		}
	}

	return a
}

func max[T Ordered](x, y T) T {
	if x > y {
		return x
	}
	return y
}

type Ordered interface {
	int | float64 | ~string
}

package main

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

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

func main() {
	var t int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &arr[j])
		}
		mul := 1
		for _, x := range arr {
			mul *= x
		}
		if mul%int(math.Pow(2, float64(n))) != 0 {
			kx := 0
			for newMul := mul; newMul%2 == 0; newMul /= 2 {
				kx++
			}
			x := int(math.Pow(2, float64(n-kx)))
			found := false
			s := 0
			for k := len(arr); k > 0; k-- {
				if x == 1 {
					break
				}
				if k == x {
					found = true
					s++
					mul = mul * k
					break
				} else {
					if k < x && GCD(x, k) != 1 {
						s++
						mul = mul * k
						x = x / k
					}
				}
			}
			if found && mul%int(math.Pow(2, float64(n))) == 0 {
				fmt.Println(s)
			} else {
				fmt.Println(-1)
			}
		} else {
			fmt.Println(0)
		}
	}
}

func GCD(a, b int) int {
	for a != b {
		if a > b {
			a -= b
		} else {
			b -= a
		}
	}

	return a
}

func max[T Ordered](x, y T) T {
	if x > y {
		return x
	}
	return y
}

type Ordered interface {
	int | float64 | ~string
}

package main

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

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

func main() {
	var t int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &arr[j])
		}
		mul := 1
		for _, x := range arr {
			mul *= x
		}
		if mul%int(math.Pow(2, float64(n))) != 0 {
			kx := 0
			for newMul := mul; newMul%2 == 0; newMul /= 2 {
				kx++
			}
			x := int(math.Pow(2, float64(n-kx)))
			found := false
			s := 0
			for k := len(arr); k > 0; k-- {
				if x == 1 {
					break
				}
				if k == x {
					found = true
					s++
					break
				} else {
					if k < x && GCD(x, k) != 1 {
						s++
						x = x / k
					}
				}
			}
			if found {
				fmt.Println(s)
			} else {
				fmt.Println(-1)
			}
		} else {
			fmt.Println(0)
		}
	}
}

func GCD(a, b int) int {
	for a != b {
		if a > b {
			a -= b
		} else {
			b -= a
		}
	}

	return a
}

func max[T Ordered](x, y T) T {
	if x > y {
		return x
	}
	return y
}

type Ordered interface {
	int | float64 | ~string
}

package main

import (
	"bufio"
	"fmt"
	"math/bits"
	"os"
	"sort"
)

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 n int
		fmt.Fscan(in, &n)

		var z int
		aa := make([]int, n)
		bb := []int{}
		for i := 0; i < n; i++ {
			var v uint
			fmt.Fscan(in, &v)

			aa[i] += bits.TrailingZeros(v)
			z += aa[i]
			if bits.TrailingZeros(uint(i+1)) > 0 {
				if aa[i] == 0 {
					bb = append(bb, bits.TrailingZeros(uint(i+1)))
				} else {
					bb = append(bb, aa[i]*bits.TrailingZeros(uint(i+1)))
				}
			}
		}

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

		var c int
		for _, v := range bb {
			if z >= n {
				break
			}
			z += v
			c++
		}
		if z < n {
			fmt.Fprintln(out, -1)
		} else {
			fmt.Fprintln(out, c)
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"math/bits"
	"os"
	"sort"
)

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 n int
		fmt.Fscan(in, &n)

		var z int
		aa := make([]int, n)
		bb := []int{}
		for i := 0; i < n; i++ {
			var v uint
			fmt.Fscan(in, &v)

			aa[i] += bits.TrailingZeros(v)
			z += aa[i]
			if bits.TrailingZeros(uint(i+1)) > 0 {
				if aa[i] == 0 {
					bb = append(bb, bits.TrailingZeros(uint(i+1)))
				} else {
					bb = append(bb, aa[i]*bits.TrailingZeros(uint(i+1)))
				}
			}
		}

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

		if z == 0 {
			fmt.Fprintln(out, -1)
		} else {
			var c int
			for _, v := range bb {
				if z >= n {
					break
				}
				z += v
				c++
			}
			fmt.Fprintln(out, c)
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"math/bits"
	"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 n int
		fmt.Fscan(in, &n)

		var z int
		for i := 0; i < n; i++ {
			var v uint
			fmt.Fscan(in, &v)

			z += bits.TrailingZeros(v)
		}

		if z == 0 {
			fmt.Fprintln(out, -1)
		} else {
			var c int
			for z < n {
				z *= 2
				c++
			}
			fmt.Fprintln(out, c)
		}
	}
}

package main

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

var reader = bufio.NewReader(os.Stdin)

var n, m int

func main() {
	var t int

	fmt.Fscan(reader, &t)

	for t > 0 {
		t--
		var n int
		fmt.Fscan(reader, &n)
		var num int
		needed := n
		have := 0
		divs := make([]int, 0, n/4)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &num)
			for num%2 == 0 {
				have++
				num /= 2
			}
			cur := i + 1
			val := 0
			// 2 4 6 // the middle 4 gives most, we will first multiple by the 4
			for cur%2 == 0 { // cur_index % 2 = 0
				val++    //
				cur /= 2 // cur_index /= 2
			}
			if val > 0 {
				divs = append(divs, val) // index will contribute val,
			}
		}

		if have >= needed { // already satisfied
			fmt.Println(0)
			continue
		}
		fmt.Println(divs)
		sort.Ints(divs)

		ans := 0
		fmt.Println(divs)
		// count the contribution
		for i := len(divs) - 1; i >= 0; i-- {
			have += divs[i]
			ans++
			if have >= needed {
				break
			}
		}
		if have < needed {
			fmt.Println(-1)
			continue
		}
		fmt.Println(ans)

	}
}

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

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

package main

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

func solution_1(input io.Reader, output io.Writer) {
	in := bufio.NewReader(input)
	var n int
	_, err := fmt.Fscanf(in, "%d\n", &n)
	if err != nil {
		panic(err)
	}

	for i := 0; i < n; i++ {
		var narr int
		_, err := fmt.Fscanf(in, "%d\n", &narr)
		if err != nil {
			panic(err)
		}

		arr := make([]int, 0, narr)
		for j := 0; j < narr; j++ {
			var v int
			_, err := fmt.Fscanf(in, "%d", &v)
			if err != nil {
				panic(err)
			}
			arr = append(arr, v)
		}
		fmt.Fscanf(in, "\n")
		var s string
		_, err = fmt.Fscanf(in, "%s\n", &s)
		fmt.Fprintln(output, numberReplacement(arr, s))
	}
}

func solution_2(input io.Reader, output io.Writer) {
	in := bufio.NewReader(input)
	var n int
	_, err := fmt.Fscanf(in, "%d\n", &n)
	if err != nil {
		panic(err)
	}

	for i := 0; i < n; i++ {
		var narr, q int
		_, err := fmt.Fscanf(in, "%d %d\n", &narr, &q)
		if err != nil {
			panic(err)
		}

		arr := make([]int, 0, narr)
		for j := 0; j < narr; j++ {
			var v int
			_, err := fmt.Fscanf(in, "%d", &v)
			if err != nil {
				panic(err)
			}
			arr = append(arr, v)
		}
		fmt.Fscanf(in, "\n")

		nOdd := 0
		nEven := 0
		sum := 0

		for _, n := range arr {
			sum += n
			if n%2 == 0 {
				nEven++
			} else {
				nOdd++
			}
		}
		for j := 0; j < q; j++ {
			var a, b int
			fmt.Fscanf(in, "%d %d\n", &a, &b)
			if a == 0 {
				sum += nEven * b
				if b%2 != 0 {
					nOdd += nEven
					nEven = 0
				}
			}

			if a == 1 {
				sum += nOdd * b
				if b%2 != 0 {
					nEven += nOdd
					nOdd = 0
				}
			}
			fmt.Fprintln(output, sum)
		}

	}
}

func numberReplacement(arr []int, s string) string {
	if len(arr) != len(s) {
		return "NO"
	}

	someMap := make(map[int]byte)
	for i, n := range arr {
		if v, ok := someMap[n]; !ok {
			someMap[n] = s[i]
		} else {
			if v != s[i] {
				return "NO"
			}
		}
	}

	return "YES"
}

func solution_3(input io.Reader, output io.Writer) {
	in := bufio.NewReader(input)
	var n int
	_, err := fmt.Fscanf(in, "%d\n", &n)
	if err != nil {
		panic(err)
	}

	for i := 0; i < n; i++ {
		var narr int
		var colour string
		_, err := fmt.Fscanf(in, "%d %s\n", &narr, &colour)
		if err != nil {
			panic(err)
		}

		var s string
		_, err = fmt.Fscanf(in, "%s\n", &s)
		if err != nil {
			panic(err)
		}
		fmt.Fprintln(output, waitTime(s, colour))
	}
}

func waitTime(s string, c string) int {
	if c == "g" {
		return 0
	}
	maxRes := 0
	firstGreenIndex := -1
	i := len(s) - 1
	curDist := 1

	for {
		if s[i] == 'g' {
			firstGreenIndex = i
			break
		}
		i--
	}

	i--
	if i < 0 {
		i = len(s) - 1
	}
	distances := make([]int, len(s))

	for i != firstGreenIndex {
		if s[i] == 'g' {
			curDist = 0
		}
		distances[i] = curDist
		curDist++
		i--
		if i < 0 {
			i = len(s) - 1
		}
	}

	for i := 0; i < len(s); i++ {
		if s[i] == c[0] {
			curDist := distances[i]
			if curDist > maxRes {
				maxRes = curDist
			}
		}
	}

	return maxRes
}

func solution_4(input io.Reader, output io.Writer) {
	in := bufio.NewReader(input)
	var n int
	_, err := fmt.Fscanf(in, "%d\n", &n)
	if err != nil {
		panic(err)
	}

	for i := 0; i < n; i++ {
		var narr int
		_, err := fmt.Fscanf(in, "%d\n", &narr)
		if err != nil {
			panic(err)
		}

		arr := make([]int, 0, narr)
		for j := 0; j < narr; j++ {
			var v int
			_, err := fmt.Fscanf(in, "%d", &v)
			if err != nil {
				panic(err)
			}
			arr = append(arr, v)
		}
		fmt.Fscanf(in, "\n")
		fmt.Fprintln(output, numOpertions(arr))
	}
}

func numOpertions(arr []int) int {
	N := len(arr)
	power := 1
	l := 0
	r := 0
	pows := make([]int, 0)

	for ii, n := range arr {
		l = 0
		i := ii + 1
		if i >= 2 && i%2 == 0 {
			l = power
			power++
		}
		r = 0
		for n >= 2 && n%2 == 0 {
			n /= 2
			r++
		}
		if r >= N {
			return 0
		}
		N -= r
		if l > 0 {
			pows = append(pows, l)
		}
	}

	res := 0
	sort.Slice(pows, func(i, j int) bool {
		return pows[i] > pows[j]
	})
	// fmt.Println(N, pows)
	i := 0
	for N > 0 {
		if i >= len(pows) {
			break
		}
		N -= pows[i]
		i++
		res++
	}
	if N > 0 {
		return -1
	}
	return res
}

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


You are given an array of n integer numbers. Let sum(l, r) be the sum of all numbers on positions from l to r non-inclusive (l-th element is counted, r-th element is not counted). For indices l and r holds 0 ≤ l ≤ r ≤ n. Indices in array are numbered from 0. For example, if a = [ - 5, 3, 9, 4], then sum(0, 1) =  - 5, sum(0, 2) =  - 2, sum(1, 4) = 16 and sum(i, i) = 0 for each i from 0 to 4.Choose the indices of three delimiters delim0, delim1, delim2 (0 ≤ delim0 ≤ delim1 ≤ delim2 ≤ n) and divide the array in such a way that the value of res = sum(0, delim0) - sum(delim0, delim1) + sum(delim1, delim2) - sum(delim2, n) is maximal. Note that some of the expressions sum(l, r) can correspond to empty segments (if l = r for some segment).

The first line contains one integer number n (1 ≤ n ≤ 5000). The second line contains n numbers a0, a1, ..., an - 1 ( - 109 ≤ ai ≤ 109).

Choose three indices so that the value of res is maximal. If there are multiple answers, print any of them.

package main

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

// calc d such that s1-s2 is max where s1 is sum[0,d) and s2 is sum[d,n)
func calcDelim(a []int) int {
	d := 0
	maxSum := int64(0)
	currSum := int64(0)
	for idx := 0; idx < len(a); idx++ {
		currSum += int64(a[idx])
		if currSum > maxSum {
			d = idx+1
			maxSum = currSum
		}
		//fmt.Printf("idx: %v, curr: %v, max: %v\n", idx, currSum, maxSum)
	}
	return d
}

func calcDelims(n int, a []int) []int {
	var d0, d1, d2 int

	maxSum := int64(0)
	for idx := range a {
		maxSum -= int64(a[idx])
	}
	tmpd1 := d1
	for tmpd1 <= n {
		tmpd0 := calcDelim(a[0:tmpd1])
		tmpd2 := tmpd1 + calcDelim(a[tmpd1:n])
		idx := 0
		s0 := int64(0)
		s1 := int64(0)
		s2 := int64(0)
		s3 := int64(0)
		for idx < tmpd0 {
			s0 += int64(a[idx])
			idx++
		}
		for idx < tmpd1 {
			s1 += int64(a[idx])
			idx++
		}
		for idx < tmpd2 {
			s2 += int64(a[idx])
			idx++
		}
		for idx < n {
			s3 += int64(a[idx])
			idx++
		}
		curr := s0-s1+s2-s3
		if curr > maxSum {
			maxSum = curr
			d0 = tmpd0
			d1 = tmpd1
			d2 = tmpd2
		}
		//fmt.Printf("tmpd0: %v, tmpd1: %v, tmpd2: %v, s0: %v, s1: %v, s2: %v, s3: %v, curr: %v\n",
		//	tmpd0, tmpd1, tmpd2, s0, s1, s2, s3, curr)
		tmpd1++
	}

	return []int{d0, d1, d2}
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer([]byte{}, int(math.Pow(10, 6)))

	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	scanner.Scan()
	aStrArr := strings.Split(scanner.Text(), " ")
	a := make([]int, len(aStrArr))
	for idx := range aStrArr {
		a[idx], _ = strconv.Atoi(aStrArr[idx])
	}

	resp := calcDelims(n, a)
	for _, d := range resp {
		fmt.Printf("%v ", d)
	}
	fmt.Println()
}

package main

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

// calc d such that s1-s2 is max where s1 is sum[0,d) and s2 is sum[d,n)
func calcDelim(a []int) int {
	d := 0
	maxSum := int64(0)
	currSum := int64(0)
	for idx := 0; idx < len(a); idx++ {
		currSum += int64(a[idx])
		if currSum > maxSum {
			d = idx+1
		}
	}
	return d
}

func calcDelims(n int, a []int) []int {
	var d0, d1, d2 int

	maxSum := int64(0)
	for idx := range a {
		maxSum -= int64(a[idx])
	}
	tmpd1 := d1
	for tmpd1 <= n {
		tmpd0 := calcDelim(a[0:tmpd1])
		tmpd2 := tmpd1 + calcDelim(a[tmpd1:n])
		idx := 0
		s0 := int64(0)
		s1 := int64(0)
		s2 := int64(0)
		s3 := int64(0)
		for idx < tmpd0 {
			s0 += int64(a[idx])
			idx++
		}
		for idx < tmpd1 {
			s1 += int64(a[idx])
			idx++
		}
		for idx < tmpd2 {
			s2 += int64(a[idx])
			idx++
		}
		for idx < n {
			s3 += int64(a[idx])
			idx++
		}
		curr := s0-s1+s2-s3
		if curr > maxSum {
			maxSum = curr
			d0 = tmpd0
			d1 = tmpd1
			d2 = tmpd2
		}
		//fmt.Printf("tmpd0: %v, tmpd1: %v, tmpd2: %v, curr: %v\n", tmpd0, tmpd1, tmpd2, curr)
		tmpd1++
	}

	return []int{d0, d1, d2}
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer([]byte{}, int(math.Pow(10, 6)))

	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	scanner.Scan()
	aStrArr := strings.Split(scanner.Text(), " ")
	a := make([]int, len(aStrArr))
	for idx := range aStrArr {
		a[idx], _ = strconv.Atoi(aStrArr[idx])
	}

	resp := calcDelims(n, a)
	for _, d := range resp {
		fmt.Printf("%v ", d)
	}
	fmt.Println()
}


Egor has a table of size $$$n \times m$$$, with lines numbered from $$$1$$$ to $$$n$$$ and columns numbered from $$$1$$$ to $$$m$$$. Each cell has a color that can be presented as an integer from $$$1$$$ to $$$10^5$$$.Let us denote the cell that lies in the intersection of the $$$r$$$-th row and the $$$c$$$-th column as $$$(r, c)$$$. We define the manhattan distance between two cells $$$(r_1, c_1)$$$ and $$$(r_2, c_2)$$$ as the length of a shortest path between them where each consecutive cells in the path must have a common side. The path can go through cells of any color. For example, in the table $$$3 \times 4$$$ the manhattan distance between $$$(1, 2)$$$ and $$$(3, 3)$$$ is $$$3$$$, one of the shortest paths is the following: $$$(1, 2) \to (2, 2) \to (2, 3) \to (3, 3)$$$. Egor decided to calculate the sum of manhattan distances between each pair of cells of the same color. Help him to calculate this sum.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \le m$$$, $$$n \cdot m \leq 100\,000$$$) — number of rows and columns in the table. Each of next $$$n$$$ lines describes a row of the table. The $$$i$$$-th line contains $$$m$$$ integers $$$c_{i1}, c_{i2}, \ldots, c_{im}$$$ ($$$1 \le c_{ij} \le 100\,000$$$) — colors of cells in the $$$i$$$-th row.

NoteIn the first sample there are three pairs of cells of same color: in cells $$$(1, 1)$$$ and $$$(2, 3)$$$, in cells $$$(1, 2)$$$ and $$$(2, 2)$$$, in cells $$$(1, 3)$$$ and $$$(2, 1)$$$. The manhattan distances between them are $$$3$$$, $$$1$$$ and $$$3$$$, the sum is $$$7$$$.

package main

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

/*
3
1 1
1
2 2
3 2
1 2
2 3
2 3 2
1 1 2
---
0
1
7
=======

colorToRow
1 -> 2
2 -> 1, 2

colorToCol
1 -> 1, 2
2 -> 1, 3

  2 3 2
  1 1 2

2+2 2+1 +1

*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n, m int
	scanf("%d %d\n", &n, &m)

	clrToRows := make(map[int][]int)
	clrToCols := make(map[int][]int)

	for r := 0; r < n; r++ {
		for c := 0; c < m; c++ {
			var clr int
			scanf("%d", &clr)

			clrToRows[clr] = append(clrToRows[clr], r)
			clrToCols[clr] = append(clrToCols[clr], c)
		}
		scanf("\n")
	}

	distSum := int64(0)

	for _, nums := range clrToRows {
		for i, num := range nums {
			if i == 0 {
				continue
			}

			dist := num - nums[i-1]
			cnt := int64(i) * int64(len(nums)-i)

			distSum += int64(dist) * cnt
		}
	}

	for _, nums := range clrToCols {
		sort.Ints(nums)

		for i, num := range nums {
			if i == 0 {
				continue
			}

			dist := num - nums[i-1]
			cnt := int64(i) * int64(len(nums)-i)

			distSum += int64(dist) * cnt
		}
	}

	fmt.Fprintln(writer, distSum)
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	//scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

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

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int) int {
	quot := num / div
	if num%div != 0 {
		quot++
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int64) int64 {
	minimum := nums[0]
	for _, num := range nums {
		if num < minimum {
			minimum = num
		}
	}
	return minimum
}

//goland:noinspection GoUnusedFunction
func max(nums ...int64) (max, idx int64) {
	for i, num := range nums {
		if num > nums[idx] {
			idx = int64(i)
		}
	}

	return nums[idx], idx
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

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

// 1648A - Weird Sum

// Egor has a table of size n×m, with lines numbered from 1 to n and columns numbered from 1 to m. Each cell has a color that can be presented as an integer from 1 to 105.
// Let us denote the cell that lies in the intersection of the r-th row and the c-th column as (r,c). We define the manhattan distance between two cells (r1,c1) and (r2,c2) as the length of a shortest path between them where each consecutive cells in the path must have a common side. The path can go through cells of any color. For example, in the table 3×4 the manhattan distance between (1,2) and (3,3) is 3, one of the shortest paths is the following: (1,2)→(2,2)→(2,3)→(3,3).
// Egor decided to calculate the sum of manhattan distances between each pair of cells of the same color. Help him to calculate this sum.

// Input
// The first line contains two integers n and m (1≤n≤m, n⋅m≤100000) — number of rows and columns in the table.
// Each of next n lines describes a row of the table. The i-th line contains m integers ci1,ci2,…,cim (1≤cij≤100000) — colors of cells in the i-th row.

// Output
// Print one integer — the the sum of manhattan distances between each pair of cells of the same color.

// Examples

// input
// 2 3
// 1 2 3
// 3 2 1
// output
// 7

// input
// 3 4
// 1 1 2 2
// 2 1 1 2
// 2 2 1 1
// output
// 76

// input
// 4 4
// 1 1 2 3
// 2 1 1 2
// 3 1 2 1
// 1 1 2 1
// output
// 129

// Note
// In the first sample there are three pairs of cells of same color: in cells (1,1) and (2,3), in cells (1,2) and (2,2), in cells (1,3) and (2,1). The manhattan distances between them are 3, 1 and 3, the sum is 7.

package main

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

func main() {
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	var t int
	var n, m, i, j, num_colors, ans int64
	var color, row, col [][]int64

	for t, _ = fmt.Fscan(reader, &n, &m); t == 2; t, _ = fmt.Fscan(reader, &n, &m) {
		color = make([][]int64, n)
		num_colors = 0
		ans = 0

		for i = 0; i < n; i++ {
			color[i] = make([]int64, m)

			for j = 0; j < m; j++ {
				fmt.Fscan(reader, &color[i][j])

				if num_colors < color[i][j] {
					num_colors = color[i][j]
				}
			}
		}

		row = make([][]int64, num_colors)
		col = make([][]int64, num_colors)

		for i = 0; i < n; i++ {
			for j = 0; j < m; j++ {
				row[color[i][j]-1] = append(row[color[i][j]-1], i)
				col[color[i][j]-1] = append(col[color[i][j]-1], j)
			}
		}

		for i = 0; i < num_colors; i++ {
			sort.Slice(row[i], func(a, b int) bool {
				return row[i][a] < row[i][b]
			})

			for j = 0; j < int64(len(row[i])); j++ {
				ans += (2*j + 1 - int64(len(row[i]))) * row[i][j]
			}

			sort.Slice(col[i], func(a, b int) bool {
				return col[i][a] < col[i][b]
			})

			for j = 0; j < int64(len(col[i])); j++ {
				ans += (2*j + 1 - int64(len(col[i]))) * col[i][j]
			}
		}

		fmt.Fprintln(writer, ans)
	}

	writer.Flush()
}

package main

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

/*
3
1 1
1
2 2
3 2
1 2
2 3
2 3 2
1 1 2
---
0
1
7
=======

colorToRow
1 -> 2
2 -> 1, 2

colorToCol
1 -> 1, 2
2 -> 1, 3

  2 3 2
  1 1 2

2+2 2+1 +1

*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n, m int
	scanf("%d %d\n", &n, &m)

	clrToRows := make(map[int][]int)
	clrToCols := make(map[int][]int)

	for r := 0; r < n; r++ {
		for c := 0; c < m; c++ {
			var clr int
			scanf("%d", &clr)

			clrToRows[clr] = append(clrToRows[clr], r)
			clrToCols[clr] = append(clrToCols[clr], c)
		}
		scanf("\n")
	}

	distSum := int64(0)

	for _, nums := range clrToRows {
		for i, num := range nums {
			if i == 0 {
				continue
			}

			dist := num - nums[i-1]
			cnt := i * (len(nums) - i)

			distSum += int64(dist) * int64(cnt)
		}
	}

	for _, nums := range clrToCols {
		sort.Ints(nums)

		for i, num := range nums {
			if i == 0 {
				continue
			}

			dist := num - nums[i-1]
			cnt := i * (len(nums) - i)

			distSum += int64(dist) * int64(cnt)
		}
	}

	fmt.Fprintln(writer, distSum)
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	//scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

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

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int) int {
	quot := num / div
	if num%div != 0 {
		quot++
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int64) int64 {
	minimum := nums[0]
	for _, num := range nums {
		if num < minimum {
			minimum = num
		}
	}
	return minimum
}

//goland:noinspection GoUnusedFunction
func max(nums ...int64) (max, idx int64) {
	for i, num := range nums {
		if num > nums[idx] {
			idx = int64(i)
		}
	}

	return nums[idx], idx
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

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

// 1648A - Weird Sum

// Egor has a table of size n×m, with lines numbered from 1 to n and columns numbered from 1 to m. Each cell has a color that can be presented as an integer from 1 to 105.
// Let us denote the cell that lies in the intersection of the r-th row and the c-th column as (r,c). We define the manhattan distance between two cells (r1,c1) and (r2,c2) as the length of a shortest path between them where each consecutive cells in the path must have a common side. The path can go through cells of any color. For example, in the table 3×4 the manhattan distance between (1,2) and (3,3) is 3, one of the shortest paths is the following: (1,2)→(2,2)→(2,3)→(3,3).
// Egor decided to calculate the sum of manhattan distances between each pair of cells of the same color. Help him to calculate this sum.

// Input
// The first line contains two integers n and m (1≤n≤m, n⋅m≤100000) — number of rows and columns in the table.
// Each of next n lines describes a row of the table. The i-th line contains m integers ci1,ci2,…,cim (1≤cij≤100000) — colors of cells in the i-th row.

// Output
// Print one integer — the the sum of manhattan distances between each pair of cells of the same color.

// Examples

// input
// 2 3
// 1 2 3
// 3 2 1
// output
// 7

// input
// 3 4
// 1 1 2 2
// 2 1 1 2
// 2 2 1 1
// output
// 76

// input
// 4 4
// 1 1 2 3
// 2 1 1 2
// 3 1 2 1
// 1 1 2 1
// output
// 129

// Note
// In the first sample there are three pairs of cells of same color: in cells (1,1) and (2,3), in cells (1,2) and (2,2), in cells (1,3) and (2,1). The manhattan distances between them are 3, 1 and 3, the sum is 7.

package main

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

func main() {
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	var t, n, m, i, j, num_colors, ans int
	var color, row, col [][]int

	for t, _ = fmt.Fscan(reader, &n, &m); t == 2; t, _ = fmt.Fscan(reader, &n, &m) {
		color = make([][]int, n)
		num_colors = 0
		ans = 0

		for i = 0; i < n; i++ {
			color[i] = make([]int, m)

			for j = 0; j < m; j++ {
				fmt.Fscan(reader, &color[i][j])

				if num_colors < color[i][j] {
					num_colors = color[i][j]
				}
			}
		}

		row = make([][]int, num_colors)
		col = make([][]int, num_colors)

		for i = 0; i < n; i++ {
			for j = 0; j < m; j++ {
				row[color[i][j]-1] = append(row[color[i][j]-1], i)
				col[color[i][j]-1] = append(col[color[i][j]-1], j)
			}
		}

		for i = 0; i < num_colors; i++ {
			sort.Ints(row[i])

			for j = 0; j < len(row[i]); j++ {
				ans += (2*j + 1 - len(row[i])) * row[i][j]
			}

			sort.Ints(col[i])

			for j = 0; j < len(col[i]); j++ {
				ans += (2*j + 1 - len(col[i])) * col[i][j]
			}
		}

		fmt.Fprintln(writer, ans)
	}

	writer.Flush()
}


There are n employees working in company "X" (let's number them from 1 to n for convenience). Initially the employees didn't have any relationships among each other. On each of m next days one of the following events took place:  either employee y became the boss of employee x (at that, employee x didn't have a boss before);  or employee x gets a packet of documents and signs them; then he gives the packet to his boss. The boss signs the documents and gives them to his boss and so on (the last person to sign the documents sends them to the archive);  or comes a request of type "determine whether employee x signs certain documents". Your task is to write a program that will, given the events, answer the queries of the described type. At that, it is guaranteed that throughout the whole working time the company didn't have cyclic dependencies.

The first line contains two integers n and m (1 ≤ n, m ≤ 105) — the number of employees and the number of events.  Each of the next m lines contains the description of one event (the events are given in the chronological order). The first number of the line determines the type of event t (1 ≤ t ≤ 3).    If t = 1, then next follow two integers x and y (1 ≤ x, y ≤ n) — numbers of the company employees. It is guaranteed that employee x doesn't have the boss currently.  If t = 2, then next follow integer x (1 ≤ x ≤ n) — the number of the employee who got a document packet.  If t = 3, then next follow two integers x and i (1 ≤ x ≤ n; 1 ≤ i ≤ [number of packets that have already been given]) — the employee and the number of the document packet for which you need to find out information. The document packets are numbered started from 1 in the chronological order.  It is guaranteed that the input has at least one query of the third type.

For each query of the third type print "YES" if the employee signed the document package and "NO" otherwise. Print all the words without the quotes.

package main

import (
	"os"
	"io/ioutil"
)

var (
	iptr int = 0
	ibuf []byte
)

func IsSpace(b byte) bool {
	switch b {
	case ' ', '\t', '\n', '\r':
		return true
	default:
		return false
	}
}

func IsDigit(b byte) bool {
	return '0' <= b && b <= '9'
}

func ReadWord() string {
	for ; iptr < len(ibuf) && IsSpace(ibuf[iptr]); iptr++ {
	}
	buf := make([]byte, 0)
	for ; iptr < len(ibuf) && IsSpace(ibuf[iptr]); iptr++ {
		buf = append(buf, ibuf[iptr])
	}
	return string(buf)
}

func ReadInt() int {
	for ; iptr < len(ibuf) && !IsDigit(ibuf[iptr]) && ibuf[iptr] != '-'; iptr++ {
	}
	s := 1
	if ibuf[iptr] == '-' {
		s = -1
		iptr++
	}

	d := 0
	for ; iptr < len(ibuf) && IsDigit(ibuf[iptr]); iptr++ {
		d = 10 * d + int(ibuf[iptr] - '0')
	}
	return s * d
}

func ReadInt64() int64 {
	for ; iptr < len(ibuf) && !IsDigit(ibuf[iptr]) && ibuf[iptr] != '-'; iptr++ {
	}
	s := int64(1)
	if ibuf[iptr] == '-' {
		s = int64(-1)
		iptr++
	}
	d := int64(0)
	for ; iptr < len(ibuf) && IsDigit(ibuf[iptr]); iptr++ {
		d = int64(10) * d + int64(ibuf[iptr] - '0')
	}
	return s * d
}

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

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

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

func LowerBound(a []int64, x int64) int {
	ub, lb := len(a), 0
	for ub - lb > 1 {
		mid := (ub + lb) / 2
		if a[mid] < x {
			lb = mid
		} else {
			ub = mid
		}
	}
	return ub
}

func UpperBound(a []int64, x int64) int {
	ub, lb := len(a), 0
	for ub - lb > 1 {
		mid := (ub + lb) / 2
		if a[mid] > x {
			ub = mid
		} else {
			lb = mid
		}
	}
	return ub
}

type Container []int64

func (a Container) Len() int {
	return len(a)
}

func (a Container) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}

func init() {
	ibuf, _ = ioutil.ReadAll(os.Stdin)
}

type Q3 struct {
	Id, Who int
}

type Msg struct {
	From int
	Time int
	Dests []Q3
}

type Edge struct {
	To, Time int
}

type Graph [][]Edge
type Tree struct {
	N int
	Par []Edge
	Ch Graph
}

type LCA struct {
	Tree
	log_n uint
	depth []int
	dub [][]Edge
}

func NewTree(n int) Tree {
	t := Tree{n, make([]Edge, n), make(Graph, n)}
	for v := 0; v < n; v++ {
		t.Par[v] = Edge{v, 0}
	}
	return t
}

func (t Tree) AddPar(v int, e Edge) {
	if t.Par[v].To != v {
		panic("parent exists")
	}
	t.Par[v] = e
	t.Ch[e.To] = append(t.Ch[e.To], Edge{v, e.Time})
}

func (t Tree) AllDepth() []int {
	par, ch := t.Par, t.Ch
	depth := make([]int, len(ch))
	var dfs func(v, p, d int)
	dfs = func(v, p, d int) {
		depth[v] = d
		for _, e := range ch[v] {
			if (e.To != p) {
				dfs(e.To, v, d + 1)
			}
		}
	}
	for v, p := range par {
		if p.To == v {
			dfs(v, -1, 0)
		}
	}
	return depth
}

func NewLCA(t Tree) LCA {
	lca := LCA{t, 1, nil, nil}
	lca.depth = t.AllDepth()
	lca.log_n = 1
	for (1 << uint(lca.log_n)) < t.N {
		lca.log_n++
	}
	lca.dub = make([][]Edge, lca.log_n)
	lca.dub[0] = t.Par
	for i := uint(1); i < lca.log_n; i++ {
		lca.dub[i] = make([]Edge, t.N)
		for v := 0; v < t.N; v++ {
			e1 := lca.dub[i - 1][v]
			e2 := lca.dub[i - 1][e1.To]
			lca.dub[i][v] = Edge{e2.To, Max(e1.Time, e2.Time)}
		}
	}
	return lca
}

func (lca LCA) IsParAt(v, p, time int) bool {
	if lca.depth[v] < lca.depth[p] {
		return false
	}
	d := lca.depth[v] - lca.depth[p]
	for k := int(lca.log_n) - 1; k >= 0; k-- {
		if (d & (1 << uint(k))) != 0 {
			if lca.dub[k][v].Time > time {
				return false
			}
			v = lca.dub[k][v].To
		}
	}
	return v == p
}

var (
	n, m int
	t Tree
	msgs []Msg
)

func main() {
	n, m := ReadInt(), ReadInt()
	t := NewTree(n)
	msgs := make([]Msg, 0)
	k := 0
	for i := 0; i < m; i++ {
		c := ReadInt()
		switch c {
		case 1:
			u, v := ReadInt() - 1, ReadInt() - 1
			t.AddPar(u, Edge{v, i})
		case 2:
			v := ReadInt() - 1
			msgs = append(msgs, Msg{v, i, nil})
		case 3:
			v, i := ReadInt() - 1, ReadInt() - 1
			msgs[i].Dests = append(msgs[i].Dests, Q3{k, v})
			k++
		default:
			panic("invalid query")
		}
	}
	ans := make([][]byte, k)
	lca := NewLCA(t)
	for _, msg := range msgs {
		for _, q := range msg.Dests {
			b := lca.IsParAt(msg.From, q.Who, msg.Time)
			if b {
				ans[q.Id] = append(ans[q.Id], "YES"...)
			} else {
				ans[q.Id] = append(ans[q.Id], "NO"...)
			}
		}
	}
	buf := make([]byte, 0, 5 * k)
	for _, a := range ans {
		buf = append(buf, a...)
		buf = append(buf, '\n')
	}
	os.Stdout.Write(buf)
}

package main

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

var (
	iptr int = 0
	ibuf []byte
)

func IsSpace(b byte) bool {
	switch b {
	case ' ', '\t', '\n', '\r':
		return true
	default:
		return false
	}
}

func IsDigit(b byte) bool {
	return '0' <= b && b <= '9'
}

func ReadWord() string {
	for ; iptr < len(ibuf) && IsSpace(ibuf[iptr]); iptr++ {
	}
	buf := make([]byte, 0)
	for ; iptr < len(ibuf) && IsSpace(ibuf[iptr]); iptr++ {
		buf = append(buf, ibuf[iptr])
	}
	return string(buf)
}

func ReadInt() int {
	for ; iptr < len(ibuf) && !IsDigit(ibuf[iptr]) && ibuf[iptr] != '-'; iptr++ {
	}
	s := 1
	if ibuf[iptr] == '-' {
		s = -1
		iptr++
	}

	d := 0
	for ; iptr < len(ibuf) && IsDigit(ibuf[iptr]); iptr++ {
		d = 10 * d + int(ibuf[iptr] - '0')
	}
	return s * d
}

func ReadInt64() int64 {
	for ; iptr < len(ibuf) && !IsDigit(ibuf[iptr]) && ibuf[iptr] != '-'; iptr++ {
	}
	s := int64(1)
	if ibuf[iptr] == '-' {
		s = int64(-1)
		iptr++
	}
	d := int64(0)
	for ; iptr < len(ibuf) && IsDigit(ibuf[iptr]); iptr++ {
		d = int64(10) * d + int64(ibuf[iptr] - '0')
	}
	return s * d
}

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

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

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

func LowerBound(a []int64, x int64) int {
	ub, lb := len(a), 0
	for ub - lb > 1 {
		mid := (ub + lb) / 2
		if a[mid] < x {
			lb = mid
		} else {
			ub = mid
		}
	}
	return ub
}

func UpperBound(a []int64, x int64) int {
	ub, lb := len(a), 0
	for ub - lb > 1 {
		mid := (ub + lb) / 2
		if a[mid] > x {
			ub = mid
		} else {
			lb = mid
		}
	}
	return ub
}

type Container []int64

func (a Container) Len() int {
	return len(a)
}

func (a Container) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}

func init() {
	ibuf, _ = ioutil.ReadAll(os.Stdin)
}

type Q3 struct {
	Id, Who int
}

type Msg struct {
	From int
	Time int
	Dests []Q3
}

type Edge struct {
	To, Time int
}

type Graph [][]Edge
type Tree struct {
	N int
	Par []Edge
	Ch Graph
}

type LCA struct {
	Tree
	log_n uint
	depth []int
	dub [][]Edge
}

func NewTree(n int) Tree {
	t := Tree{n, make([]Edge, n), make(Graph, n)}
	for v := 0; v < n; v++ {
		t.Par[v] = Edge{v, 0}
	}
	return t
}

func (t Tree) AddPar(v int, e Edge) {
	if t.Par[v].To != v {
		panic("parent exists")
	}
	t.Par[v] = e
	t.Ch[e.To] = append(t.Ch[e.To], Edge{v, e.Time})
}

func (t Tree) AllDepth() []int {
	par, ch := t.Par, t.Ch
	depth := make([]int, len(ch))
	var dfs func(v, p, d int)
	dfs = func(v, p, d int) {
		depth[v] = d
		for _, e := range ch[v] {
			if (e.To != p) {
				dfs(e.To, v, d + 1)
			}
		}
	}
	for v, p := range par {
		if p.To == v {
			dfs(v, -1, 0)
		}
	}
	return depth
}

func NewLCA(t Tree) LCA {
	lca := LCA{t, 1, nil, nil}
	lca.depth = t.AllDepth()
	lca.log_n = 1
	for (1 << uint(lca.log_n)) < t.N {
		lca.log_n++
	}
	lca.dub = make([][]Edge, lca.log_n)
	lca.dub[0] = t.Par
	for i := uint(1); i < lca.log_n; i++ {
		lca.dub[i] = make([]Edge, t.N)
		for v := 0; v < t.N; v++ {
			e1 := lca.dub[i - 1][v]
			e2 := lca.dub[i - 1][e1.To]
			lca.dub[i][v] = Edge{e2.To, Max(e1.Time, e2.Time)}
		}
	}
	return lca
}

func (lca LCA) IsParAt(v, p, time int) bool {
	if lca.depth[v] < lca.depth[p] {
		return false
	}
	d := lca.depth[v] - lca.depth[p]
	for k := int(lca.log_n) - 1; k >= 0; k-- {
		if (d & (1 << uint(k))) != 0 {
			if lca.dub[k][v].Time > time {
				return false
			}
			v = lca.dub[k][v].To
		}
	}
	return v == p
}

var (
	n, m int
	t Tree
	msgs []Msg
)

func main() {
	n, m := ReadInt(), ReadInt()
	t := NewTree(n)
	msgs := make([]Msg, 0)
	k := 0
	for i := 0; i < m; i++ {
		c := ReadInt()
		switch c {
		case 1:
			u, v := ReadInt() - 1, ReadInt() - 1
			t.AddPar(u, Edge{v, i})
		case 2:
			v := ReadInt() - 1
			msgs = append(msgs, Msg{v, i, nil})
		case 3:
			v, i := ReadInt() - 1, ReadInt() - 1
			msgs[i].Dests = append(msgs[i].Dests, Q3{k, v})
			k++
		default:
			panic("invalid query")
		}
	}
	ans := make([][]byte, k)
	lca := NewLCA(t)
	for _, msg := range msgs {
		for _, q := range msg.Dests {
			b := lca.IsParAt(msg.From, q.Who, msg.Time)
			if b {
				ans[q.Id] = append(ans[q.Id], "YES"...)
			} else {
				ans[q.Id] = append(ans[q.Id], "NO"...)
			}
		}
	}
	for _, a := range ans {
		fmt.Println(string(a))
	}
}

package main

import (
	"os"
	"io/ioutil"
)

var (
	iptr int = 0
	ibuf []byte
)

func IsSpace(b byte) bool {
	switch b {
	case ' ', '\t', '\n', '\r':
		return true
	default:
		return false
	}
}

func IsDigit(b byte) bool {
	return '0' <= b && b <= '9'
}

func ReadWord() string {
	for ; iptr < len(ibuf) && IsSpace(ibuf[iptr]); iptr++ {
	}
	buf := make([]byte, 0)
	for ; iptr < len(ibuf) && IsSpace(ibuf[iptr]); iptr++ {
		buf = append(buf, ibuf[iptr])
	}
	return string(buf)
}

func ReadInt() int {
	for ; iptr < len(ibuf) && !IsDigit(ibuf[iptr]) && ibuf[iptr] != '-'; iptr++ {
	}
	s := 1
	if ibuf[iptr] == '-' {
		s = -1
		iptr++
	}

	d := 0
	for ; iptr < len(ibuf) && IsDigit(ibuf[iptr]); iptr++ {
		d = 10 * d + int(ibuf[iptr] - '0')
	}
	return s * d
}

func ReadInt64() int64 {
	for ; iptr < len(ibuf) && !IsDigit(ibuf[iptr]) && ibuf[iptr] != '-'; iptr++ {
	}
	s := int64(1)
	if ibuf[iptr] == '-' {
		s = int64(-1)
		iptr++
	}
	d := int64(0)
	for ; iptr < len(ibuf) && IsDigit(ibuf[iptr]); iptr++ {
		d = int64(10) * d + int64(ibuf[iptr] - '0')
	}
	return s * d
}

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

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

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

func LowerBound(a []int64, x int64) int {
	ub, lb := len(a), 0
	for ub - lb > 1 {
		mid := (ub + lb) / 2
		if a[mid] < x {
			lb = mid
		} else {
			ub = mid
		}
	}
	return ub
}

func UpperBound(a []int64, x int64) int {
	ub, lb := len(a), 0
	for ub - lb > 1 {
		mid := (ub + lb) / 2
		if a[mid] > x {
			ub = mid
		} else {
			lb = mid
		}
	}
	return ub
}

type Container []int64

func (a Container) Len() int {
	return len(a)
}

func (a Container) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}

func init() {
	ibuf, _ = ioutil.ReadAll(os.Stdin)
}

type Q3 struct {
	Id, Who int
}

type Msg struct {
	From int
	Time int
	Dests []Q3
}

type Edge struct {
	To, Time int
}

type Graph [][]Edge
type Tree struct {
	N int
	Par []Edge
	Ch Graph
}

type LCA struct {
	Tree
	log_n uint
	depth []int
	dub [][]Edge
}

func NewTree(n int) Tree {
	t := Tree{n, make([]Edge, n), make(Graph, n)}
	for v := 0; v < n; v++ {
		t.Par[v] = Edge{v, 0}
	}
	return t
}

func (t Tree) AddPar(v int, e Edge) {
	if t.Par[v].To != v {
		panic("parent exists")
	}
	t.Par[v] = e
	t.Ch[e.To] = append(t.Ch[e.To], Edge{v, e.Time})
}

func (t Tree) AllDepth() []int {
	par, ch := t.Par, t.Ch
	depth := make([]int, len(ch))
	var dfs func(v, p, d int)
	dfs = func(v, p, d int) {
		depth[v] = d
		for _, e := range ch[v] {
			if (e.To != p) {
				dfs(e.To, v, d + 1)
			}
		}
	}
	for v, p := range par {
		if p.To == v {
			dfs(v, -1, 0)
		}
	}
	return depth
}

func NewLCA(t Tree) LCA {
	lca := LCA{t, 1, nil, nil}
	lca.depth = t.AllDepth()
	lca.log_n = 1
	for (1 << uint(lca.log_n)) < t.N {
		lca.log_n++
	}
	lca.dub = make([][]Edge, lca.log_n)
	lca.dub[0] = t.Par
	for i := uint(1); i < lca.log_n; i++ {
		lca.dub[i] = make([]Edge, t.N)
		for v := 0; v < t.N; v++ {
			e1 := lca.dub[i - 1][v]
			e2 := lca.dub[i - 1][e1.To]
			lca.dub[i][v] = Edge{e2.To, Max(e1.Time, e2.Time)}
		}
	}
	return lca
}

func (lca LCA) IsParAt(v, p, time int) bool {
	if lca.depth[v] < lca.depth[p] {
		return false
	}
	d := lca.depth[v] - lca.depth[p]
	for k := int(lca.log_n) - 1; k >= 0; k-- {
		if (d & (1 << uint(k))) != 0 {
			if lca.dub[k][v].Time > time {
				return false
			}
			v = lca.dub[k][v].To
		}
	}
	return v == p
}

var (
	n, m int
	t Tree
	msgs []Msg
)

func main() {
	n, m := ReadInt(), ReadInt()
	t := NewTree(n)
	msgs := make([]Msg, 0)
	k := 0
	for i := 0; i < m; i++ {
		c := ReadInt()
		switch c {
		case 1:
			u, v := ReadInt() - 1, ReadInt() - 1
			t.AddPar(u, Edge{v, i})
		case 2:
			v := ReadInt() - 1
			msgs = append(msgs, Msg{v, i, nil})
		case 3:
			v, i := ReadInt() - 1, ReadInt() - 1
			msgs[i].Dests = append(msgs[i].Dests, Q3{k, v})
			k++
		default:
			panic("invalid query")
		}
	}
	ans := make([][]byte, k)
	lca := NewLCA(t)
	for _, msg := range msgs {
		for _, q := range msg.Dests {
			b := lca.IsParAt(msg.From, q.Who, msg.Time)
			if b {
				ans[q.Id] = append(ans[q.Id], "YES"...)
			} else {
				ans[q.Id] = append(ans[q.Id], "NO"...)
			}
		}
	}
	buf := make([]byte, 5 * k)
	for _, a := range ans {
		buf = append(buf, a...)
		buf = append(buf, "\r\n"...)
	}
	os.Stdout.Write(buf)
	os.Stdout.Sync()
}

package main

import (
	"os"
	"io/ioutil"
)

var (
	iptr int = 0
	ibuf []byte
)

func IsSpace(b byte) bool {
	switch b {
	case ' ', '\t', '\n', '\r':
		return true
	default:
		return false
	}
}

func IsDigit(b byte) bool {
	return '0' <= b && b <= '9'
}

func ReadWord() string {
	for ; iptr < len(ibuf) && IsSpace(ibuf[iptr]); iptr++ {
	}
	buf := make([]byte, 0)
	for ; iptr < len(ibuf) && IsSpace(ibuf[iptr]); iptr++ {
		buf = append(buf, ibuf[iptr])
	}
	return string(buf)
}

func ReadInt() int {
	for ; iptr < len(ibuf) && !IsDigit(ibuf[iptr]) && ibuf[iptr] != '-'; iptr++ {
	}
	s := 1
	if ibuf[iptr] == '-' {
		s = -1
		iptr++
	}

	d := 0
	for ; iptr < len(ibuf) && IsDigit(ibuf[iptr]); iptr++ {
		d = 10 * d + int(ibuf[iptr] - '0')
	}
	return s * d
}

func ReadInt64() int64 {
	for ; iptr < len(ibuf) && !IsDigit(ibuf[iptr]) && ibuf[iptr] != '-'; iptr++ {
	}
	s := int64(1)
	if ibuf[iptr] == '-' {
		s = int64(-1)
		iptr++
	}
	d := int64(0)
	for ; iptr < len(ibuf) && IsDigit(ibuf[iptr]); iptr++ {
		d = int64(10) * d + int64(ibuf[iptr] - '0')
	}
	return s * d
}

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

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

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

func LowerBound(a []int64, x int64) int {
	ub, lb := len(a), 0
	for ub - lb > 1 {
		mid := (ub + lb) / 2
		if a[mid] < x {
			lb = mid
		} else {
			ub = mid
		}
	}
	return ub
}

func UpperBound(a []int64, x int64) int {
	ub, lb := len(a), 0
	for ub - lb > 1 {
		mid := (ub + lb) / 2
		if a[mid] > x {
			ub = mid
		} else {
			lb = mid
		}
	}
	return ub
}

type Container []int64

func (a Container) Len() int {
	return len(a)
}

func (a Container) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}

func init() {
	ibuf, _ = ioutil.ReadAll(os.Stdin)
}

type Q3 struct {
	Id, Who int
}

type Msg struct {
	From int
	Time int
	Dests []Q3
}

type Edge struct {
	To, Time int
}

type Graph [][]Edge
type Tree struct {
	N int
	Par []Edge
	Ch Graph
}

type LCA struct {
	Tree
	log_n uint
	depth []int
	dub [][]Edge
}

func NewTree(n int) Tree {
	t := Tree{n, make([]Edge, n), make(Graph, n)}
	for v := 0; v < n; v++ {
		t.Par[v] = Edge{v, 0}
	}
	return t
}

func (t Tree) AddPar(v int, e Edge) {
	if t.Par[v].To != v {
		panic("parent exists")
	}
	t.Par[v] = e
	t.Ch[e.To] = append(t.Ch[e.To], Edge{v, e.Time})
}

func (t Tree) AllDepth() []int {
	par, ch := t.Par, t.Ch
	depth := make([]int, len(ch))
	var dfs func(v, p, d int)
	dfs = func(v, p, d int) {
		depth[v] = d
		for _, e := range ch[v] {
			if (e.To != p) {
				dfs(e.To, v, d + 1)
			}
		}
	}
	for v, p := range par {
		if p.To == v {
			dfs(v, -1, 0)
		}
	}
	return depth
}

func NewLCA(t Tree) LCA {
	lca := LCA{t, 1, nil, nil}
	lca.depth = t.AllDepth()
	lca.log_n = 1
	for (1 << uint(lca.log_n)) < t.N {
		lca.log_n++
	}
	lca.dub = make([][]Edge, lca.log_n)
	lca.dub[0] = t.Par
	for i := uint(1); i < lca.log_n; i++ {
		lca.dub[i] = make([]Edge, t.N)
		for v := 0; v < t.N; v++ {
			e1 := lca.dub[i - 1][v]
			e2 := lca.dub[i - 1][e1.To]
			lca.dub[i][v] = Edge{e2.To, Max(e1.Time, e2.Time)}
		}
	}
	return lca
}

func (lca LCA) IsParAt(v, p, time int) bool {
	if lca.depth[v] < lca.depth[p] {
		return false
	}
	d := lca.depth[v] - lca.depth[p]
	for k := int(lca.log_n) - 1; k >= 0; k-- {
		if (d & (1 << uint(k))) != 0 {
			if lca.dub[k][v].Time > time {
				return false
			}
			v = lca.dub[k][v].To
		}
	}
	return v == p
}

var (
	n, m int
	t Tree
	msgs []Msg
)

func main() {
	n, m := ReadInt(), ReadInt()
	t := NewTree(n)
	msgs := make([]Msg, 0)
	k := 0
	for i := 0; i < m; i++ {
		c := ReadInt()
		switch c {
		case 1:
			u, v := ReadInt() - 1, ReadInt() - 1
			t.AddPar(u, Edge{v, i})
		case 2:
			v := ReadInt() - 1
			msgs = append(msgs, Msg{v, i, nil})
		case 3:
			v, i := ReadInt() - 1, ReadInt() - 1
			msgs[i].Dests = append(msgs[i].Dests, Q3{k, v})
			k++
		default:
			panic("invalid query")
		}
	}
	ans := make([][]byte, k)
	lca := NewLCA(t)
	for _, msg := range msgs {
		for _, q := range msg.Dests {
			b := lca.IsParAt(msg.From, q.Who, msg.Time)
			if b {
				ans[q.Id] = append(ans[q.Id], "YES"...)
			} else {
				ans[q.Id] = append(ans[q.Id], "NO"...)
			}
		}
	}
	buf := make([]byte, k * 4)
	for _, a := range ans {
		buf = append(buf, a...)
		buf = append(buf, '\n')
	}
	os.Stdout.Write(buf)
}

package main

import (
	"os"
	"io/ioutil"
)

var (
	iptr int = 0
	ibuf []byte
)

func IsSpace(b byte) bool {
	switch b {
	case ' ', '\t', '\n', '\r':
		return true
	default:
		return false
	}
}

func IsDigit(b byte) bool {
	return '0' <= b && b <= '9'
}

func ReadWord() string {
	for ; iptr < len(ibuf) && IsSpace(ibuf[iptr]); iptr++ {
	}
	buf := make([]byte, 0)
	for ; iptr < len(ibuf) && IsSpace(ibuf[iptr]); iptr++ {
		buf = append(buf, ibuf[iptr])
	}
	return string(buf)
}

func ReadInt() int {
	for ; iptr < len(ibuf) && !IsDigit(ibuf[iptr]) && ibuf[iptr] != '-'; iptr++ {
	}
	s := 1
	if ibuf[iptr] == '-' {
		s = -1
		iptr++
	}

	d := 0
	for ; iptr < len(ibuf) && IsDigit(ibuf[iptr]); iptr++ {
		d = 10 * d + int(ibuf[iptr] - '0')
	}
	return s * d
}

func ReadInt64() int64 {
	for ; iptr < len(ibuf) && !IsDigit(ibuf[iptr]) && ibuf[iptr] != '-'; iptr++ {
	}
	s := int64(1)
	if ibuf[iptr] == '-' {
		s = int64(-1)
		iptr++
	}
	d := int64(0)
	for ; iptr < len(ibuf) && IsDigit(ibuf[iptr]); iptr++ {
		d = int64(10) * d + int64(ibuf[iptr] - '0')
	}
	return s * d
}

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

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

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

func LowerBound(a []int64, x int64) int {
	ub, lb := len(a), 0
	for ub - lb > 1 {
		mid := (ub + lb) / 2
		if a[mid] < x {
			lb = mid
		} else {
			ub = mid
		}
	}
	return ub
}

func UpperBound(a []int64, x int64) int {
	ub, lb := len(a), 0
	for ub - lb > 1 {
		mid := (ub + lb) / 2
		if a[mid] > x {
			ub = mid
		} else {
			lb = mid
		}
	}
	return ub
}

type Container []int64

func (a Container) Len() int {
	return len(a)
}

func (a Container) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}

func init() {
	ibuf, _ = ioutil.ReadAll(os.Stdin)
}

type Q3 struct {
	Id, Who int
}

type Msg struct {
	From int
	Time int
	Dests []Q3
}

type Edge struct {
	To, Time int
}

type Graph [][]Edge
type Tree struct {
	N int
	Par []Edge
	Ch Graph
}

type LCA struct {
	Tree
	log_n uint
	depth []int
	dub [][]Edge
}

func NewTree(n int) Tree {
	t := Tree{n, make([]Edge, n), make(Graph, n)}
	for v := 0; v < n; v++ {
		t.Par[v] = Edge{v, 0}
	}
	return t
}

func (t Tree) AddPar(v int, e Edge) {
	if t.Par[v].To != v {
		panic("parent exists")
	}
	t.Par[v] = e
	t.Ch[e.To] = append(t.Ch[e.To], Edge{v, e.Time})
}

func (t Tree) AllDepth() []int {
	par, ch := t.Par, t.Ch
	depth := make([]int, len(ch))
	var dfs func(v, p, d int)
	dfs = func(v, p, d int) {
		depth[v] = d
		for _, e := range ch[v] {
			if (e.To != p) {
				dfs(e.To, v, d + 1)
			}
		}
	}
	for v, p := range par {
		if p.To == v {
			dfs(v, -1, 0)
		}
	}
	return depth
}

func NewLCA(t Tree) LCA {
	lca := LCA{t, 1, nil, nil}
	lca.depth = t.AllDepth()
	lca.log_n = 1
	for (1 << uint(lca.log_n)) < t.N {
		lca.log_n++
	}
	lca.dub = make([][]Edge, lca.log_n)
	lca.dub[0] = t.Par
	for i := uint(1); i < lca.log_n; i++ {
		lca.dub[i] = make([]Edge, t.N)
		for v := 0; v < t.N; v++ {
			e1 := lca.dub[i - 1][v]
			e2 := lca.dub[i - 1][e1.To]
			lca.dub[i][v] = Edge{e2.To, Max(e1.Time, e2.Time)}
		}
	}
	return lca
}

func (lca LCA) IsParAt(v, p, time int) bool {
	if lca.depth[v] < lca.depth[p] {
		return false
	}
	d := lca.depth[v] - lca.depth[p]
	for k := int(lca.log_n) - 1; k >= 0; k-- {
		if (d & (1 << uint(k))) != 0 {
			if lca.dub[k][v].Time > time {
				return false
			}
			v = lca.dub[k][v].To
		}
	}
	return v == p
}

var (
	n, m int
	t Tree
	msgs []Msg
)

func main() {
	n, m := ReadInt(), ReadInt()
	t := NewTree(n)
	msgs := make([]Msg, 0)
	k := 0
	for i := 0; i < m; i++ {
		c := ReadInt()
		switch c {
		case 1:
			u, v := ReadInt() - 1, ReadInt() - 1
			t.AddPar(u, Edge{v, i})
		case 2:
			v := ReadInt() - 1
			msgs = append(msgs, Msg{v, i, nil})
		case 3:
			v, i := ReadInt() - 1, ReadInt() - 1
			msgs[i].Dests = append(msgs[i].Dests, Q3{k, v})
			k++
		default:
			panic("invalid query")
		}
	}
	ans := make([][]byte, k)
	lca := NewLCA(t)
	for _, msg := range msgs {
		for _, q := range msg.Dests {
			b := lca.IsParAt(msg.From, q.Who, msg.Time)
			if b {
				ans[q.Id] = append(ans[q.Id], "YES"...)
			} else {
				ans[q.Id] = append(ans[q.Id], "NO"...)
			}
		}
	}
	buf := make([]byte, k * 4)
	for _, a := range ans {
		buf = append(buf, a...)
		buf = append(buf, "\n\r"...)
	}
	os.Stdout.Write(buf)
}

package main

import (
	"os"
	"io/ioutil"
)

var (
	iptr int = 0
	ibuf []byte
)

func IsSpace(b byte) bool {
	switch b {
	case ' ', '\t', '\n', '\r':
		return true
	default:
		return false
	}
}

func IsDigit(b byte) bool {
	return '0' <= b && b <= '9'
}

func ReadWord() string {
	for ; iptr < len(ibuf) && IsSpace(ibuf[iptr]); iptr++ {
	}
	buf := make([]byte, 0)
	for ; iptr < len(ibuf) && IsSpace(ibuf[iptr]); iptr++ {
		buf = append(buf, ibuf[iptr])
	}
	return string(buf)
}

func ReadInt() int {
	for ; iptr < len(ibuf) && !IsDigit(ibuf[iptr]) && ibuf[iptr] != '-'; iptr++ {
	}
	s := 1
	if ibuf[iptr] == '-' {
		s = -1
		iptr++
	}

	d := 0
	for ; iptr < len(ibuf) && IsDigit(ibuf[iptr]); iptr++ {
		d = 10 * d + int(ibuf[iptr] - '0')
	}
	return s * d
}

func ReadInt64() int64 {
	for ; iptr < len(ibuf) && !IsDigit(ibuf[iptr]) && ibuf[iptr] != '-'; iptr++ {
	}
	s := int64(1)
	if ibuf[iptr] == '-' {
		s = int64(-1)
		iptr++
	}
	d := int64(0)
	for ; iptr < len(ibuf) && IsDigit(ibuf[iptr]); iptr++ {
		d = int64(10) * d + int64(ibuf[iptr] - '0')
	}
	return s * d
}

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

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

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

func LowerBound(a []int64, x int64) int {
	ub, lb := len(a), 0
	for ub - lb > 1 {
		mid := (ub + lb) / 2
		if a[mid] < x {
			lb = mid
		} else {
			ub = mid
		}
	}
	return ub
}

func UpperBound(a []int64, x int64) int {
	ub, lb := len(a), 0
	for ub - lb > 1 {
		mid := (ub + lb) / 2
		if a[mid] > x {
			ub = mid
		} else {
			lb = mid
		}
	}
	return ub
}

type Container []int64

func (a Container) Len() int {
	return len(a)
}

func (a Container) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}

func init() {
	ibuf, _ = ioutil.ReadAll(os.Stdin)
}

type Q3 struct {
	Id, Who int
}

type Msg struct {
	From int
	Time int
	Dests []Q3
}

type Edge struct {
	To, Time int
}

type Graph [][]Edge
type Tree struct {
	N int
	Par []Edge
	Ch Graph
}

type LCA struct {
	Tree
	log_n uint
	depth []int
	dub [][]Edge
}

func NewTree(n int) Tree {
	t := Tree{n, make([]Edge, n), make(Graph, n)}
	for v := 0; v < n; v++ {
		t.Par[v] = Edge{v, 0}
	}
	return t
}

func (t Tree) AddPar(v int, e Edge) {
	if t.Par[v].To != v {
		panic("parent exists")
	}
	t.Par[v] = e
	t.Ch[e.To] = append(t.Ch[e.To], Edge{v, e.Time})
}

func (t Tree) AllDepth() []int {
	par, ch := t.Par, t.Ch
	depth := make([]int, len(ch))
	var dfs func(v, p, d int)
	dfs = func(v, p, d int) {
		depth[v] = d
		for _, e := range ch[v] {
			if (e.To != p) {
				dfs(e.To, v, d + 1)
			}
		}
	}
	for v, p := range par {
		if p.To == v {
			dfs(v, -1, 0)
		}
	}
	return depth
}

func NewLCA(t Tree) LCA {
	lca := LCA{t, 1, nil, nil}
	lca.depth = t.AllDepth()
	lca.log_n = 1
	for (1 << uint(lca.log_n)) < t.N {
		lca.log_n++
	}
	lca.dub = make([][]Edge, lca.log_n)
	lca.dub[0] = t.Par
	for i := uint(1); i < lca.log_n; i++ {
		lca.dub[i] = make([]Edge, t.N)
		for v := 0; v < t.N; v++ {
			e1 := lca.dub[i - 1][v]
			e2 := lca.dub[i - 1][e1.To]
			lca.dub[i][v] = Edge{e2.To, Max(e1.Time, e2.Time)}
		}
	}
	return lca
}

func (lca LCA) IsParAt(v, p, time int) bool {
	if lca.depth[v] < lca.depth[p] {
		return false
	}
	d := lca.depth[v] - lca.depth[p]
	for k := int(lca.log_n) - 1; k >= 0; k-- {
		if (d & (1 << uint(k))) != 0 {
			if lca.dub[k][v].Time > time {
				return false
			}
			v = lca.dub[k][v].To
		}
	}
	return v == p
}

var (
	n, m int
	t Tree
	msgs []Msg
)

func main() {
	n, m := ReadInt(), ReadInt()
	t := NewTree(n)
	msgs := make([]Msg, 0)
	k := 0
	for i := 0; i < m; i++ {
		c := ReadInt()
		switch c {
		case 1:
			u, v := ReadInt() - 1, ReadInt() - 1
			t.AddPar(u, Edge{v, i})
		case 2:
			v := ReadInt() - 1
			msgs = append(msgs, Msg{v, i, nil})
		case 3:
			v, i := ReadInt() - 1, ReadInt() - 1
			msgs[i].Dests = append(msgs[i].Dests, Q3{k, v})
			k++
		default:
			panic("invalid query")
		}
	}
	ans := make([][]byte, k)
	lca := NewLCA(t)
	for _, msg := range msgs {
		for _, q := range msg.Dests {
			b := lca.IsParAt(msg.From, q.Who, msg.Time)
			if b {
				ans[q.Id] = append(ans[q.Id], "YES"...)
			} else {
				ans[q.Id] = append(ans[q.Id], "NO"...)
			}
		}
	}
	buf := make([]byte, 5 * k)
	for _, a := range ans {
		buf = append(buf, a...)
		buf = append(buf, "\r\n"...)
	}
	os.Stdout.Write(buf)
	os.Stdout.Close()
}

package main

import (
	"os"
	"io/ioutil"
)

var (
	iptr int = 0
	ibuf []byte
)

func IsSpace(b byte) bool {
	switch b {
	case ' ', '\t', '\n', '\r':
		return true
	default:
		return false
	}
}

func IsDigit(b byte) bool {
	return '0' <= b && b <= '9'
}

func ReadWord() string {
	for ; iptr < len(ibuf) && IsSpace(ibuf[iptr]); iptr++ {
	}
	buf := make([]byte, 0)
	for ; iptr < len(ibuf) && IsSpace(ibuf[iptr]); iptr++ {
		buf = append(buf, ibuf[iptr])
	}
	return string(buf)
}

func ReadInt() int {
	for ; iptr < len(ibuf) && !IsDigit(ibuf[iptr]) && ibuf[iptr] != '-'; iptr++ {
	}
	s := 1
	if ibuf[iptr] == '-' {
		s = -1
		iptr++
	}

	d := 0
	for ; iptr < len(ibuf) && IsDigit(ibuf[iptr]); iptr++ {
		d = 10 * d + int(ibuf[iptr] - '0')
	}
	return s * d
}

func ReadInt64() int64 {
	for ; iptr < len(ibuf) && !IsDigit(ibuf[iptr]) && ibuf[iptr] != '-'; iptr++ {
	}
	s := int64(1)
	if ibuf[iptr] == '-' {
		s = int64(-1)
		iptr++
	}
	d := int64(0)
	for ; iptr < len(ibuf) && IsDigit(ibuf[iptr]); iptr++ {
		d = int64(10) * d + int64(ibuf[iptr] - '0')
	}
	return s * d
}

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

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

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

func LowerBound(a []int64, x int64) int {
	ub, lb := len(a), 0
	for ub - lb > 1 {
		mid := (ub + lb) / 2
		if a[mid] < x {
			lb = mid
		} else {
			ub = mid
		}
	}
	return ub
}

func UpperBound(a []int64, x int64) int {
	ub, lb := len(a), 0
	for ub - lb > 1 {
		mid := (ub + lb) / 2
		if a[mid] > x {
			ub = mid
		} else {
			lb = mid
		}
	}
	return ub
}

type Container []int64

func (a Container) Len() int {
	return len(a)
}

func (a Container) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}

func init() {
	ibuf, _ = ioutil.ReadAll(os.Stdin)
}

type Q3 struct {
	Id, Who int
}

type Msg struct {
	From int
	Time int
	Dests []Q3
}

type Edge struct {
	To, Time int
}

type Graph [][]Edge
type Tree struct {
	N int
	Par []Edge
	Ch Graph
}

type LCA struct {
	Tree
	log_n uint
	depth []int
	dub [][]Edge
}

func NewTree(n int) Tree {
	t := Tree{n, make([]Edge, n), make(Graph, n)}
	for v := 0; v < n; v++ {
		t.Par[v] = Edge{v, 0}
	}
	return t
}

func (t Tree) AddPar(v int, e Edge) {
	if t.Par[v].To != v {
		panic("parent exists")
	}
	t.Par[v] = e
	t.Ch[e.To] = append(t.Ch[e.To], Edge{v, e.Time})
}

func (t Tree) AllDepth() []int {
	par, ch := t.Par, t.Ch
	depth := make([]int, len(ch))
	var dfs func(v, p, d int)
	dfs = func(v, p, d int) {
		depth[v] = d
		for _, e := range ch[v] {
			if (e.To != p) {
				dfs(e.To, v, d + 1)
			}
		}
	}
	for v, p := range par {
		if p.To == v {
			dfs(v, -1, 0)
		}
	}
	return depth
}

func NewLCA(t Tree) LCA {
	lca := LCA{t, 1, nil, nil}
	lca.depth = t.AllDepth()
	lca.log_n = 1
	for (1 << uint(lca.log_n)) < t.N {
		lca.log_n++
	}
	lca.dub = make([][]Edge, lca.log_n)
	lca.dub[0] = t.Par
	for i := uint(1); i < lca.log_n; i++ {
		lca.dub[i] = make([]Edge, t.N)
		for v := 0; v < t.N; v++ {
			e1 := lca.dub[i - 1][v]
			e2 := lca.dub[i - 1][e1.To]
			lca.dub[i][v] = Edge{e2.To, Max(e1.Time, e2.Time)}
		}
	}
	return lca
}

func (lca LCA) IsParAt(v, p, time int) bool {
	if lca.depth[v] < lca.depth[p] {
		return false
	}
	d := lca.depth[v] - lca.depth[p]
	for k := int(lca.log_n) - 1; k >= 0; k-- {
		if (d & (1 << uint(k))) != 0 {
			if lca.dub[k][v].Time > time {
				return false
			}
			v = lca.dub[k][v].To
		}
	}
	return v == p
}

var (
	n, m int
	t Tree
	msgs []Msg
)

func main() {
	n, m := ReadInt(), ReadInt()
	t := NewTree(n)
	msgs := make([]Msg, 0)
	k := 0
	for i := 0; i < m; i++ {
		c := ReadInt()
		switch c {
		case 1:
			u, v := ReadInt() - 1, ReadInt() - 1
			t.AddPar(u, Edge{v, i})
		case 2:
			v := ReadInt() - 1
			msgs = append(msgs, Msg{v, i, nil})
		case 3:
			v, i := ReadInt() - 1, ReadInt() - 1
			msgs[i].Dests = append(msgs[i].Dests, Q3{k, v})
			k++
		default:
			panic("invalid query")
		}
	}
	ans := make([][]byte, k)
	lca := NewLCA(t)
	for _, msg := range msgs {
		for _, q := range msg.Dests {
			b := lca.IsParAt(msg.From, q.Who, msg.Time)
			if b {
				ans[q.Id] = append(ans[q.Id], "YES"...)
			} else {
				ans[q.Id] = append(ans[q.Id], "NO"...)
			}
		}
	}
	buf := make([]byte, 5 * k)
	for _, a := range ans {
		buf = append(buf, a...)
		buf = append(buf, "\r\n"...)
	}
	os.Stdout.Write(buf)
}


You have a knapsack with the capacity of $$$W$$$. There are also $$$n$$$ items, the $$$i$$$-th one has weight $$$w_i$$$. You want to put some of these items into the knapsack in such a way that their total weight $$$C$$$ is at least half of its size, but (obviously) does not exceed it. Formally, $$$C$$$ should satisfy: $$$\lceil \frac{W}{2}\rceil \le C \le W$$$. Output the list of items you will put into the knapsack or determine that fulfilling the conditions is impossible. If there are several possible lists of items satisfying the conditions, you can output any. Note that you don't have to maximize the sum of weights of items in the knapsack.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains integers $$$n$$$ and $$$W$$$ ($$$1 \le n \le 200\,000$$$, $$$1\le W \le 10^{18}$$$).  The second line of each test case contains $$$n$$$ integers $$$w_1, w_2, \dots, w_n$$$ ($$$1 \le w_i \le 10^9$$$) — weights of the items. The sum of $$$n$$$ over all test cases does not exceed $$$200\,000$$$.

For each test case, if there is no solution, print a single integer $$$-1$$$.  If there exists a solution consisting of $$$m$$$ items, print $$$m$$$ in the first line of the output and $$$m$$$ integers $$$j_1$$$, $$$j_2$$$, ..., $$$j_m$$$ ($$$1 \le j_i \le n$$$, all $$$j_i$$$ are distinct) in the second line of the output  — indices of the items you would like to pack into the knapsack. If there are several possible lists of items satisfying the conditions, you can output any. Note that you don't have to maximize the sum of weights items in the knapsack.

NoteIn the first test case, you can take the item of weight $$$3$$$ and fill the knapsack just right.In the second test case, all the items are larger than the knapsack's capacity. Therefore, the answer is $$$-1$$$.In the third test case, you fill the knapsack exactly in half.

package main

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

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

	var t, n, W, w int64
	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &W)

		m := 0
		currentLoad := int64(0)
		result := make([]int, 0, 1000)
		for i := 0; int64(i) < n; i++ {
			fmt.Fscan(in, &w)
			if currentLoad+w <= W {
				result = append(result, i+1)
				m++
				currentLoad += w
			}
			if w >= (W+1)/2 && w <= W {
				result = []int{i + 1}
				m = 1
				currentLoad = w
			}
			if currentLoad >= (W+1)/2 {
				in.ReadString('\n')
				break
			}
		}
		if currentLoad < (W+1)/2 {
			fmt.Println(-1)
		} else {
			fmt.Println(m)
			for i := range result {
				fmt.Printf("%d ", result[i])
			}
			fmt.Println()
		}
	}
}

package main

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

const (
	SCANNER_BUF_SIZE = 2 << 20

	INF = int64(math.MaxInt64)
	MOD = 1e9 + 7
)

var (
	writer  *bufio.Writer
	scanner *bufio.Scanner
)

func init() {
	writer = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, SCANNER_BUF_SIZE), SCANNER_BUF_SIZE)
}

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

func sint64() int64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

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

func gcd(a, b int64) int64 {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

func lcm(a, b int64) int64 {
	return a * b / gcd(a, b)
}

func abs(a int64) int64 {
	if a < 0 {
		return a * -1
	}
	return a
}

func min(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r < a {
			a = r
		}
	}
	return a
}

func max(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r > a {
			a = r
		}
	}
	return a
}

func main() {
	defer writer.Flush()
	t := sint64()
	for ; t > 0; t-- {
		n, W := sint64(), sint64()
		w := make([]int64, n)
		for i := range w {
			w[i] = sint64()
		}
		res := sol(w, W)
		if res == nil {
			printf("-1\n")
		} else {
			printf("%d\n", len(res))
			for _, r := range res {
				printf("%d ", r)
			}
			printf("\n")
		}
	}
}

func sol(w []int64, W int64) []int64 {
	res := []int64{}
	currSum := int64(0)
	for i := range w {
		if w[i] > W {
			continue
		}
		if within(w[i], W) {
			return []int64{int64(i+1)}
		}
		currSum += w[i]
		res = append(res, int64(i+1))
		if within(currSum, W) {
			return res
		}
	}
	return nil
}

func within(w, W int64) bool {
	if W&1 == 0 {
		return w >= W/2 && w <= W
	}
	return w >= (W+1)/2 && w <= W
}

package main

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

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

	solve := func(Case int) {
		var n int
		var w int64
		Fscan(in, &n, &w)
		low := (w + 1) / 2
		a := make([]int,  n)
		//s := int64(0)
		okI := -1
		for i := range a {
			Fscan(in, &a[i])
			v := int64(a[i])
			if low <= v && v <= w {
				okI = i
			}
			//s += v
		}
		if okI != -1 {
			Fprintln(out, 1)
			Fprintln(out, okI+1)
			return
		}
		s := int64(0)
		ans := []int{}
		for i, V := range a {
			v := int64(V)
			if v > w {
				continue
			}
			s+=v
			ans = append(ans, i+1)
			if low <= s && s <= w {
				Fprintln(out, len(ans))
				for _, v := range ans {
					Fprint(out, v, " ")
				}
				Fprintln(out)
				return
			}
		}
		Fprintln(out, -1)
	}

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

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

package main

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

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

	var t, n, W, w int64
	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &W)
		m := 0
		currentLoad := int64(0)
		result := ""
		for i := 0; int64(i) < n; i++ {
			fmt.Fscan(in, &w)
			if currentLoad < (W+1)/2 && currentLoad+w <= W {
				result += strconv.Itoa(i+1) + " "
				m++
				currentLoad += w
			}
		}
		if currentLoad < (W+1)/2 {
			fmt.Println(-1)
		} else {
			fmt.Println(m)
			fmt.Println(result)
		}
	}
}

package main

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

type item struct {
	number int
	weight int
}

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

	var t, n, W int
	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &W)
		items := make([]item, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &items[i].weight)
			items[i].number = i + 1
		}
		sort.Slice(items, func(i, j int) bool {
			return items[i].weight > items[j].weight
		})
		m := 0
		w := 0
		result := ""
		for i := range items {
			if w+items[i].weight <= W {
				result += strconv.Itoa(items[i].number) + " "
				m++
				w += items[i].weight
				if w >= (W+1)/2 {
					break
				}
			}
		}
		if w < (W+1)/2 {
			fmt.Println(-1)
		} else {
			fmt.Println(m)
			fmt.Println(result)
		}
	}
}


A permutation p is an ordered group of numbers p1,   p2,   ...,   pn, consisting of n distinct positive integers, each is no more than n. We'll define number n as the length of permutation p1,   p2,   ...,   pn.Simon has a positive integer n and a non-negative integer k, such that 2k ≤ n. Help him find permutation a of length 2n, such that it meets this equation: .

The first line contains two integers n and k (1 ≤ n ≤ 50000, 0 ≤ 2k ≤ n).

Print 2n integers a1, a2, ..., a2n — the required permutation a. It is guaranteed that the solution exists. If there are multiple solutions, you can print any of them.

NoteRecord |x| represents the absolute value of number x. In the first sample |1 - 2| - |1 - 2| = 0.In the second sample |3 - 2| + |1 - 4| - |3 - 2 + 1 - 4| = 1 + 3 - 2 = 2.In the third sample |2 - 7| + |4 - 6| + |1 - 3| + |5 - 8| - |2 - 7 + 4 - 6 + 1 - 3 + 5 - 8| = 12 - 12 = 0.

//359B
package main
import "fmt"

func main() {
	var nn, kk int
	var flag bool
	fmt.Scan(&nn, &kk)
		flag=false
		for i:=1; i<=kk; i++ {
			if flag==false {
				flag=true
				fmt.Print(2*i-1, 2*i, " ")
			} else {
				fmt.Print(" ", 2*i-1, 2*i)
			}
		}
		for i:=kk+1; i<=nn; i++ {
			if flag==false {
				fmt.Print(2*i, 2*i-1, " ")
			} else {
				fmt.Print(" ", 2*i, 2*i-1)
			}
		}
		fmt.Println()
}

package main

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

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

	var n, k int
	Fscan(in, &n, &k)
	a := make([]interface{}, 2*n)
	for i := range a {
		a[i] = 2*n - i
	}
	for i := 0; i < k; i++ {
		a[2*i], a[2*i+1] = a[2*i+1], a[2*i]
	}
	Fprint(out, a...)
}

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

// 359B
package main

import (
	"fmt"
)

func main() {
	var n, k int
	fmt.Scan(&n, &k)
	for i := 1; i <= n; i++ {
		if i <= k {
			fmt.Print(i*2, i*2-1, " ")
		} else {
			fmt.Print(i*2-1, i*2, " ")
		}
	}
	fmt.Print("")
}

//359B
package main
import "fmt"

func main() {
	var nn, kk int
	var flag bool
	fmt.Scan(&nn, &kk)
		flag=false
		for i:=1; i<=kk; i++ {
			if flag==false {
				flag=true
				fmt.Print(2*i-1, 2*i)
			} else {
				fmt.Print(" ", 2*i-1, 2*i)
			}
		}
		for i:=kk+1; i<=nn; i++ {
			if flag==false {
				fmt.Print(2*i, 2*i-1)
			} else {
				fmt.Print(" ", 2*i, 2*i-1)
			}
		}
		fmt.Println()
}

//359B
package main
import "fmt"

func main() {
	var nn, kk int
	var flag bool
	fmt.Scan(&nn, &kk)
		flag=false
		for i:=1; i<=kk; i++ {
			if flag==false {
				flag=true
				fmt.Printf("%d %d",2*i-1,2*i)
			} else {
				fmt.Printf(" %d %d",2*i-1,2*i)
			}
		}
		for i:=kk+1; i<=nn; i++ {
			if flag==false {
				fmt.Printf("%d %d",2*i,2*i-1)
			} else {
				fmt.Print(" %d %d", 2*i,2*i-1)
			}
		}
		fmt.Println()
}