You are given two arrays $$$a$$$ and $$$b$$$, consisting of $$$n$$$ integers each.Let's define a function $$$f(a, b)$$$ as follows:   let's define an array $$$c$$$ of size $$$n$$$, where $$$c_i = a_i \oplus b_i$$$ ($$$\oplus$$$ denotes bitwise XOR);  the value of the function is $$$c_1 \mathbin{\&} c_2 \mathbin{\&} \cdots \mathbin{\&} c_n$$$ (i.e. bitwise AND of the entire array $$$c$$$). Find the maximum value of the function $$$f(a, b)$$$ if you can reorder the array $$$b$$$ in an arbitrary way (leaving the initial order is also an option).

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of arrays $$$a$$$ and $$$b$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i < 2^{30}$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$0 \le b_i < 2^{30}$$$). The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case print one integer — the maximum value of the function $$$f(a, b)$$$ if you can reorder the array $$$b$$$ in an arbitrary way.

package main

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

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

func qwq() {
	var n int
	fmt.Fscan(in, &n)
	a := make([]int, n)
	b := make([]int, n)
	for i := range a {
		fmt.Fscan(in, &a[i])
	}
	for i := range b {
		fmt.Fscan(in, &b[i])
	}
	res := 0
	c := make([]int, n)
	d := make([]int, n)

	for i := 30; i >= 0; i-- {
		x := res | (1 << i)
		for j := range a {
			c[j] = x & a[j]
			d[j] = x & (^b[j])
		}
		sort.Ints(c)
		sort.Ints(d)
		if reflect.DeepEqual(c, d) {
			res = x
		}
	}
	fmt.Fprintln(out, res)
}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

var _, _, _ = main, rw, do

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

func rw(r io.Reader, w io.Writer) {
	var t int
	fmt.Fscan(r, &t)
	for ti := 0; ti < t; ti++ {
		var n int
		fmt.Fscan(r, &n)
		a := make([]int, n)
		for i := range a {
			fmt.Fscan(r, &a[i])
		}
		b := make([]int, n)
		for i := range b {
			fmt.Fscan(r, &b[i])
		}
		w1 := do(n, a, b)
		fmt.Fprintln(w, w1)
	}
}

var maxK = 29

func do(n int, a, b []int) int {
	// fmt.Println("a")
	// for _, v := range a {
	// 	fmt.Printf("%3d %05b\n", v)
	// }
	// fmt.Println("b")
	// for _, v := range b {
	// 	fmt.Printf("%3d %05b\n", v)
	// }
	d := [][2][]int{}
	d = append(d, [2][]int{a, b})
	ans := 0
	for k := maxK; k >= 0; k-- {
		ok := true
		for _, v := range d {
			a, b = v[0], v[1]
			x, y := 0, 0
			for i := range a {
				if (a[i]>>k)&1 == 1 {
					x++
				}
				if (b[i]>>k)&1 == 1 {
					y++
				}
			}
			if x != len(a)-y {
				ok = false
				break
			}
		}
		if ok {
			ans |= (1 << k)
		} else {
			continue
		}
		p := [][2][]int{}
		for _, v := range d {
			a, b = v[0], v[1]
			var x, y [2][]int
			for i := range a {
				if (a[i]>>k)&1 == 1 {
					x[0] = append(x[0], a[i])
				} else {
					y[0] = append(y[0], a[i])
				}
				if (b[i]>>k)&1 == 1 {
					y[1] = append(y[1], b[i])
				} else {
					x[1] = append(x[1], b[i])
				}
			}
			if len(x[0]) > 0 {
				p = append(p, x)
			}
			if len(y[0]) > 0 {
				p = append(p, y)
			}
		}
		d = p
	}
	return ans
}

package main

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

var P107 int = 1000000007

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

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

func Abs(a int) int {
	return Max(a, -a)
}

func Gcd(a, b int) int {
	min := Min(a, b)
	max := Max(a, b)
	if min == 0 {
		return max
	}

	for max%min != 0 {
		min, max = max%min, min
	}

	return min
}

func Lcm(a, b int) int {
	return a / Gcd(a, b) * b
}

// var facs []int = make([]int, X + 1)
func FacP(n, p int) int {
	// if facs[n] != 0 {
	//   return facs[n]
	// }
	result := 1
	// facs[0] = 1
	for i := 1; i <= n; i++ {
		result = (result * i) % p
		// facs[i] = result
	}

	return result
}

func ExpP(a, x, p int) int {
	if x == 0 {
		return 1
	}

	if x%2 == 0 {
		half := ExpP(a, x/2, p)
		return (half * half) % p
	} else {
		return ((a % p) * ExpP(a, x-1, p)) % p
	}
}

func CombP(n, c, p int) int {
	// If CombP be called multiple times, you should optimize FacP using memoization.
	return (FacP(n, p) * ((ExpP(FacP(n-c, p), p-2, p) * ExpP(FacP(c, p), p-2, p)) % p)) % p
}

func Solve(reader *bufio.Reader) {
	scanner := bufio.NewScanner(reader)
	scanner.Split(bufio.ScanWords)
	wr := bufio.NewWriter(os.Stdout)
	defer wr.Flush()

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

	for _i := 0; _i < t; _i++ {
		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())

		aArr := make([]int, n)
		for i := 0; i < n; i++ {
			scanner.Scan()
			aArr[i], _ = strconv.Atoi(scanner.Text())
		}
		bArr := make([]int, n)
		for i := 0; i < n; i++ {
			scanner.Scan()
			bArr[i], _ = strconv.Atoi(scanner.Text())
		}

		aGrp := make([][]int, 1)
		aGrp[0] = make([]int, n)
		bGrp := make([][]int, 1)
		bGrp[0] = make([]int, n)
		for i := 0; i < n; i++ {
			aGrp[0][i] = aArr[i]
			bGrp[0][i] = bArr[i]
		}

		result := 0
		for i := 30; i >= 0; i-- {
			ln := len(aGrp)
			check := 0
			for j := 0; j < ln; j++ {
				aCnt := 0
				bCnt := 0
				for k := 0; k < len(aGrp[j]); k++ {
					ithA := ((1 << i) & aGrp[j][k]) >> i
					ithB := ((1 << i) & bGrp[j][k]) >> i

					aCnt += ithA
					bCnt += ithB
				}

				if aCnt == len(aGrp[j])-bCnt {
					check++
				}
			}
			if check != ln {
				continue
			}

			for j := 0; j < ln; j++ {
				aOnes := make([]bool, len(aGrp[j]))
				bOnes := make([]bool, len(aGrp[j]))
				for k := 0; k < len(aGrp[j]); k++ {
					ithA := ((1 << i) & aGrp[j][k]) >> i
					ithB := ((1 << i) & bGrp[j][k]) >> i

					if ithA == 1 {
						aOnes[k] = true
					}
					if ithB == 1 {
						bOnes[k] = true
					}
				}

				var newAGrp0 []int
				var newBGrp0 []int
				var newAGrp1 []int
				var newBGrp1 []int

				for k := 0; k < len(aGrp[j]); k++ {
					if aOnes[k] {
						newAGrp1 = append(newAGrp1, aGrp[j][k])
					} else {
						newAGrp0 = append(newAGrp0, aGrp[j][k])
					}
					if bOnes[k] {
						newBGrp1 = append(newBGrp1, bGrp[j][k])
					} else {
						newBGrp0 = append(newBGrp0, bGrp[j][k])
					}
				}

				if len(newAGrp0) >= len(newAGrp1) {
					aGrp[j] = newAGrp0
					bGrp[j] = newBGrp1
					if len(newAGrp1) != 0 {
						aGrp = append(aGrp, newAGrp1)
						bGrp = append(bGrp, newBGrp0)
					}
				} else {
					aGrp[j] = newAGrp1
					bGrp[j] = newBGrp0
					if len(newAGrp0) != 0 {
						aGrp = append(aGrp, newAGrp0)
						bGrp = append(bGrp, newBGrp1)
					}
				}
				result |= (1 << i)
			}
		}

		wr.WriteString(strconv.Itoa(result) + "\n")
	}
}

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

package main

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

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

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

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

		var g int64
		for x := 31; x >= 0; x-- {
			z := g | (1 << x)
			f := true
			mm := make(map[int64]int, n)
			for _, v := range bb {
				mm[v&z]++
			}
			for _, v := range aa {
				if mm[(v&z)^z] <= 0 {
					f = false
					break
				}
				mm[(v&z)^z]--
			}
			if f {
				g = z
			}
		}
		fmt.Fprintln(out, g)
	}
}

package main

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

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

func main() {
	defer writer.Flush()
	var Q int
	fmt.Fscan(reader, &Q)
	for ; Q > 0; Q-- {
		var n int
		fmt.Fscan(reader, &n)
		a := make([]int, n)
		b := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
		}
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &b[i])
		}

		var ans int64 = 0
		for cur_b := 29; cur_b >= 0; cur_b-- {
      // if cur_b-th bit is in result, there will be
      // prefix in A that match with one prefix in B.
			A := make([]int64, n)
			B := make([]int64, n)
			for i := 0; i < n; i++ {
				A[i] = int64(a[i]) & (ans | (1 << cur_b))
				B[i] = int64(^b[i]) & (ans | (1 << cur_b))
			}
			sort.Slice(A, func(i, j int) bool {
				return A[i] < A[j]
			})
			sort.Slice(B, func(i, j int) bool {
				return B[i] < B[j]
			})

			ok := true
			for i := 0; i < n; i++ {
				if A[i] != B[i] {
					ok = false
					break
				}
			}
			if ok {
				ans |= (1 << cur_b)
			}
		}

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

package main

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

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

type S struct {
	arr []int
	p   int
	m   int
}

func (a *S) Len() int      { return len(a.arr) }
func (a *S) Swap(i, j int) { a.arr[i], a.arr[j] = a.arr[j], a.arr[i] }
func (a *S) Less(i, j int) bool {
	if a.m == 0 {
		return a.arr[i]>>a.p&1 < a.arr[j]>>a.p&1
	}
	return a.arr[i]>>a.p&1 > a.arr[j]>>a.p&1
}

func Sort(a, b *[]int, l, r, p int) {
	if l >= r || p == -1 {
		return
	}
	x, y := 0, 0
	for i := range *a {
		if (*a)[i]>>p&1 == 0 {
			x++
		}
		if (*b)[i]>>p&1 == 1 {
			y++
		}
	}
	if x != y {
		return
	}

	ta := make([]int, len(*a))
	tb := make([]int, len(*a))
	copy(ta, *a)
	copy(tb, *b)

	sort.Sort(&S{ta[l : r+1], p, 0})
	sort.Sort(&S{tb[l : r+1], p, 1})
	pa, pb := l, l
	for pa <= r && (ta[pa]>>p&1) != 1 {
		pa++
	}
	for pb <= r && (tb[pb]>>p&1) != 0 {
		pb++
	}
	// fmt.Println(p, l, r, format(ta))
	// fmt.Println(p, l, r, format(tb))
	// fmt.Println("=============")

	if pa == pb {
		*a, *b = ta, tb
		Sort(a, b, l, pa-1, p-1)
		Sort(a, b, pa, r, p-1)
	} else {
		Sort(a, b, l, r, p-1)
	}
}

func format(a []int) (res string) {
	for _, x := range a {
		f := strconv.FormatInt(int64(x), 2)
		for len(f) < 5 {
			f = "0" + f
		}
		res = res + f + " "
	}
	return res
}

func qwq() {
	var n int
	fmt.Fscan(in, &n)
	a := make([]int, n)
	b := make([]int, n)
	for i := range a {
		fmt.Fscan(in, &a[i])
	}
	for i := range b {
		fmt.Fscan(in, &b[i])
	}
	Sort(&a, &b, 0, n-1, 31)
	var res int64 = (1 << 32) - 1
	for i := range a {
		res &= int64(a[i] ^ b[i])
	}
	// fmt.Fprintln(out, format(a))
	// fmt.Fprintln(out, format(b))
	fmt.Fprintln(out, res)
}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}
	// a := []int{6, 0, 4}

	// fmt.Println(a)

}

package main

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

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

type S struct {
	arr []int
	p   int
	m   int
}

func (a *S) Len() int      { return len(a.arr) }
func (a *S) Swap(i, j int) { a.arr[i], a.arr[j] = a.arr[j], a.arr[i] }
func (a *S) Less(i, j int) bool {
	if a.m == 0 {
		return a.arr[i]>>a.p&1 < a.arr[j]>>a.p&1
	}
	return a.arr[i]>>a.p&1 > a.arr[j]>>a.p&1
}

func Sort(a, b *[]int, l, r, p int) {
	if l >= r || p == -1 {
		return
	}
	ta := make([]int, len(*a))
	tb := make([]int, len(*a))
	copy(ta, *a)
	copy(tb, *b)

	sort.Sort(&S{ta[l : r+1], p, 0})
	sort.Sort(&S{tb[l : r+1], p, 1})
	pa, pb := l, l
	for pa <= r && (ta[pa]>>p&1) != 1 {
		pa++
	}
	for pb <= r && (tb[pb]>>p&1) != 0 {
		pb++
	}
	// fmt.Println(p, l, r, format(ta))
	// fmt.Println(p, l, r, format(tb))
	// fmt.Println("=============")

	if pa == pb {
		*a, *b = ta, tb
		Sort(a, b, l, pa-1, p-1)
		Sort(a, b, pa, r, p-1)
	} else {
		Sort(a, b, l, r, p-1)
	}
}

func format(a []int) (res string) {
	for _, x := range a {
		f := strconv.FormatInt(int64(x), 2)
		for len(f) < 5 {
			f = "0" + f
		}
		res = res + f + " "
	}
	return res
}

func qwq() {
	var n int
	fmt.Fscan(in, &n)
	a := make([]int, n)
	b := make([]int, n)
	for i := range a {
		fmt.Fscan(in, &a[i])
	}
	for i := range b {
		fmt.Fscan(in, &b[i])
	}
	Sort(&a, &b, 0, n-1, 31)
	var res int64 = (1 << 32) - 1
	for i := range a {
		res &= int64(a[i] ^ b[i])
	}
	// fmt.Fprintln(out, format(a))
	// fmt.Fprintln(out, format(b))
	fmt.Fprintln(out, res)
}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}
	// a := []int{6, 0, 4}

	// fmt.Println(a)

}

package main

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

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

type S struct {
	arr []int
	p   int
}

func (a *S) Len() int           { return len(a.arr) }
func (a *S) Swap(i, j int)      { a.arr[i], a.arr[j] = a.arr[j], a.arr[i] }
func (a *S) Less(i, j int) bool { return a.arr[i]>>a.p&1 < a.arr[j]>>a.p&1 }

func Sort(a, b *[]int, l, r, p int) {
	if l >= r || p == -1 {
		return
	}
	ta := make([]int, len(*a))
	tb := make([]int, len(*a))
	copy(ta, *a)
	copy(tb, *b)

	sort.Sort(&S{ta[l : r+1], p})
	sort.Sort(&S{tb[l : r+1], p})
	pa, pb := l, l
	for pa <= r && (ta[pa]>>p&1) != 1 {
		pa++
	}
	for pb <= r && (tb[pb]>>p&1) != 0 {
		pb++
	}
	// fmt.Println(p, l, r, format(ta))
	// fmt.Println(p, l, r, format(tb))
	// fmt.Println("=============")

	if pa == pb {
		*a, *b = ta, tb
		Sort(a, b, l, pa-1, p-1)
		Sort(a, b, pa, r, p-1)
	} else {
		Sort(a, b, l, r, p-1)
	}
}

func format(a []int) (res string) {
	for _, x := range a {
		f := strconv.FormatInt(int64(x), 2)
		for len(f) < 5 {
			f = "0" + f
		}
		res = res + f + " "
	}
	return res
}

func qwq() {
	var n int
	fmt.Fscan(in, &n)
	a := make([]int, n)
	b := make([]int, n)
	for i := range a {
		fmt.Fscan(in, &a[i])
	}
	for i := range b {
		fmt.Fscan(in, &b[i])
	}
	Sort(&a, &b, 0, n-1, 31)
	var res int64 = (1 << 32) - 1
	for i := range a {
		res &= int64(a[i] ^ b[i])
	}
	// fmt.Fprintln(out, format(a))
	// fmt.Fprintln(out, format(b))
	fmt.Fprintln(out, res)
}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}
	// a := []int{6, 0, 4}

	// fmt.Println(a)

}

package main

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

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

func Sort(a, b *[]int, l, r, p int) {
	if l >= r || p == -1 {
		return
	}

	ta := make([]int, len(*a))
	tb := make([]int, len(*a))
	copy(ta, *a)
	copy(tb, *b)

	sort.Slice(ta[l:r+1], func(i, j int) bool {
		return ta[i]>>p&1 < ta[j]>>p&1
	})
	sort.Slice(tb[l:r+1], func(i, j int) bool {
		return tb[i]>>p&1 > tb[j]>>p&1
	})
	pa, pb := l, l
	for pa <= r && (ta[pa]>>p&1) != 1 {
		pa++
	}
	for pb <= r && (tb[pb]>>p&1) != 0 {
		pb++
	}
	// fmt.Println(p, format(ta))
	// fmt.Println(p, format(tb))
	// fmt.Println("=============")

	if pa == pb {
		*a, *b = ta, tb
		Sort(a, b, l, pa-1, p-1)
		Sort(a, b, pa, r, p-1)
	} else {
		Sort(a, b, l, r, p-1)
	}
}

func format(a []int) (res string) {
	for _, x := range a {
		f := strconv.FormatInt(int64(x), 2)
		for len(f) < 5 {
			f = "0" + f
		}
		res = res + f + " "
	}
	return res
}

func qwq() {
	var n int
	fmt.Fscan(in, &n)
	a := make([]int, n)
	b := make([]int, n)
	for i := range a {
		fmt.Fscan(in, &a[i])
	}
	for i := range b {
		fmt.Fscan(in, &b[i])
	}
	Sort(&a, &b, 0, n-1, 31)
	var res int64 = (1 << 32) - 1
	for i := range a {
		res &= int64(a[i] ^ b[i])
	}
	// fmt.Fprintln(out, format(a))
	// fmt.Fprintln(out, format(b))
	fmt.Fprintln(out, res)
}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

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

func Sort(a, b *[]int, l, r, p int) {
	if l >= r || p == -1 {
		return
	}

	ta := make([]int, len(*a))
	tb := make([]int, len(*a))
	copy(ta, *a)
	copy(tb, *b)

	sort.Slice(ta[l:r+1], func(i, j int) bool {
		return ta[i]>>p < ta[j]>>p
	})
	sort.Slice(tb[l:r+1], func(i, j int) bool {
		return tb[i]>>p > tb[j]>>p
	})
	pa, pb := l, l
	for pa <= r && ta[pa] != 1 {
		pa++
	}
	for pb <= r && tb[pb] != 0 {
		pb++
	}
	if pa == pb {
		*a, *b = ta, tb
		Sort(a, b, l, pa-1, p-1)
		Sort(a, b, pa, r, p-1)
	} else {
		Sort(a, b, l, r, p-1)
	}
}

func qwq() {
	var n int
	fmt.Fscan(in, &n)
	a := make([]int, n)
	b := make([]int, n)
	for i := range a {
		fmt.Fscan(in, &a[i])
	}
	for i := range b {
		fmt.Fscan(in, &b[i])
	}
	Sort(&a, &b, 0, n-1, 31)
	var res int64 = (1 << 32) - 1
	for i := range a {
		res &= int64(a[i] ^ b[i])
	}
	fmt.Fprintln(out, res)
}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

var _, _, _ = main, rw, do

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

func rw(r io.Reader, w io.Writer) {
	var t int
	fmt.Fscan(r, &t)
	for ti := 0; ti < t; ti++ {
		var n int
		fmt.Fscan(r, &n)
		a := make([]int, n)
		for i := range a {
			fmt.Fscan(r, &a[i])
		}
		b := make([]int, n)
		for i := range b {
			fmt.Fscan(r, &b[i])
		}
		w1 := do(n, a, b)
		fmt.Fprintln(w, w1)
	}
}

const maxK = 29

func do(n int, a, b []int) int {
	d := [][2][]int{}
	d = append(d, [2][]int{a, b})
	ans := 0
	for k := maxK; k >= 0; k-- {
		ok := true
		for _, v := range d {
			a, b = v[0], v[1]
			x, y := 0, 0
			for i := range a {
				if (a[i]>>k)&1 == 1 {
					x++
				}
				if (b[i]>>k)&1 == 1 {
					y++
				}
			}
			if x != len(a)-y {
				ok = false
				break
			}
		}
		if ok {
			ans |= (1 << k)
		} else {
			continue
		}
		p := [][2][]int{}
		for _, v := range d {
			a, b = v[0], v[1]
			var x, y [2][]int
			for i := range a {
				if (a[i]>>k)&1 == 1 {
					x[0] = append(x[0], a[i])
				} else {
					y[0] = append(y[0], a[i])
				}
				if (b[i]>>k)&1 == 1 {
					y[1] = append(y[1], a[i])
				} else {
					x[1] = append(x[1], a[i])
				}
			}
			if len(x[0]) > 0 {
				p = append(p, x)
			}
			if len(y[0]) > 0 {
				p = append(p, x)
			}
		}
		d = p
	}
	return ans
}

package main

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

var P107 int = 1000000007

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

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

func Abs(a int) int {
	return Max(a, -a)
}

func Gcd(a, b int) int {
	min := Min(a, b)
	max := Max(a, b)
	if min == 0 {
		return max
	}

	for max%min != 0 {
		min, max = max%min, min
	}

	return min
}

func Lcm(a, b int) int {
	return a / Gcd(a, b) * b
}

// var facs []int = make([]int, X + 1)
func FacP(n, p int) int {
	// if facs[n] != 0 {
	//   return facs[n]
	// }
	result := 1
	// facs[0] = 1
	for i := 1; i <= n; i++ {
		result = (result * i) % p
		// facs[i] = result
	}

	return result
}

func ExpP(a, x, p int) int {
	if x == 0 {
		return 1
	}

	if x%2 == 0 {
		half := ExpP(a, x/2, p)
		return (half * half) % p
	} else {
		return ((a % p) * ExpP(a, x-1, p)) % p
	}
}

func CombP(n, c, p int) int {
	// If CombP be called multiple times, you should optimize FacP using memoization.
	return (FacP(n, p) * ((ExpP(FacP(n-c, p), p-2, p) * ExpP(FacP(c, p), p-2, p)) % p)) % p
}

func Solve(reader *bufio.Reader) {
	scanner := bufio.NewScanner(reader)
	scanner.Split(bufio.ScanWords)
	wr := bufio.NewWriter(os.Stdout)
	defer wr.Flush()

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

	for _i := 0; _i < t; _i++ {
		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())

		aArr := make([]int, n)
		for i := 0; i < n; i++ {
			scanner.Scan()
			aArr[i], _ = strconv.Atoi(scanner.Text())
		}
		bArr := make([]int, n)
		for i := 0; i < n; i++ {
			scanner.Scan()
			bArr[i], _ = strconv.Atoi(scanner.Text())
		}

		aGrp := make([][]int, 1)
		aGrp[0] = make([]int, n)
		bGrp := make([][]int, 1)
		bGrp[0] = make([]int, n)
		for i := 0; i < n; i++ {
			aGrp[0][i] = aArr[i]
			bGrp[0][i] = bArr[i]
		}

		result := 0
		for i := 30; i >= 0; i-- {
			ln := len(aGrp)
			for j := 0; j < ln; j++ {
				aOnes := make([]bool, len(aGrp[j]))
				bOnes := make([]bool, len(aGrp[j]))
				aCnt := 0
				bCnt := 0
				for k := 0; k < len(aGrp[j]); k++ {
					ithA := ((1 << i) & aGrp[j][k]) >> i
					ithB := ((1 << i) & bGrp[j][k]) >> i

					aCnt += ithA
					bCnt += ithB
					if ithA == 1 {
						aOnes[k] = true
					}
					if ithB == 1 {
						bOnes[k] = true
					}
				}

				if aCnt == len(aGrp[j])-bCnt {
					var newAGrp0 []int
					var newBGrp0 []int
					var newAGrp1 []int
					var newBGrp1 []int

					for k := 0; k < len(aGrp[j]); k++ {
						if aOnes[k] {
							newAGrp1 = append(newAGrp1, aGrp[j][k])
						} else {
							newAGrp0 = append(newAGrp0, aGrp[j][k])
						}
						if bOnes[k] {
							newBGrp1 = append(newBGrp1, bGrp[j][k])
						} else {
							newBGrp0 = append(newBGrp0, bGrp[j][k])
						}
					}

					if len(newAGrp0) >= len(newAGrp1) {
						aGrp[j] = newAGrp0
						bGrp[j] = newBGrp1
						if len(newAGrp1) != 0 {
							aGrp = append(aGrp, newAGrp1)
							bGrp = append(bGrp, newBGrp0)
						}
					} else {
						aGrp[j] = newAGrp1
						bGrp[j] = newBGrp0
						if len(newAGrp0) != 0 {
							aGrp = append(aGrp, newAGrp0)
							bGrp = append(bGrp, newBGrp1)
						}
					}
					result |= (1 << i)
				}
			}
		}

		wr.WriteString(strconv.Itoa(result) + "\n")
	}
}

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

package main

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

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

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

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

		var g int64
		for x := 31; x >= 0; x-- {
			z := g | (1 << x)
			f := true
			mm := make(map[int64]int, n)
			for _, v := range bb {
				mm[v&z]++
			}
			for _, v := range aa {
				fmt.Println("want: ", v, (v&z)^z)
				if mm[(v&z)^z] <= 0 {
					f = false
					break
				}
				mm[(v&z)^z]--
			}
			if f {
				g = z
			}
		}
		fmt.Fprintln(out, g)
	}
}


Bandits appeared in the city! One of them is trying to catch as many citizens as he can.The city consists of $$$n$$$ squares connected by $$$n-1$$$ roads in such a way that it is possible to reach any square from any other square. The square number $$$1$$$ is the main square.After Sunday walk all the roads were changed to one-way roads in such a way that it is possible to reach any square from the main square.At the moment when the bandit appeared on the main square there were $$$a_i$$$ citizens on the $$$i$$$-th square. Now the following process will begin. First, each citizen that is currently on a square with some outgoing one-way roads chooses one of such roads and moves along it to another square. Then the bandit chooses one of the one-way roads outgoing from the square he is located and moves along it. The process is repeated until the bandit is located on a square with no outgoing roads. The bandit catches all the citizens on that square.The bandit wants to catch as many citizens as possible; the citizens want to minimize the number of caught people. The bandit and the citizens know positions of all citizens at any time, the citizens can cooperate. If both sides act optimally, how many citizens will be caught?

The first line contains a single integer $$$n$$$ — the number of squares in the city ($$$2 \le n \le 2\cdot10^5$$$). The second line contains $$$n-1$$$ integers $$$p_2, p_3 \dots p_n$$$ meaning that there is a one-way road from the square $$$p_i$$$ to the square $$$i$$$ ($$$1 \le p_i < i$$$).  The third line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ — the number of citizens on each square initially ($$$0 \le a_i \le 10^9$$$).

Print a single integer — the number of citizens the bandit will catch if both sides act optimally.

NoteIn the first example the citizens on the square $$$1$$$ can split into two groups $$$2 + 1$$$, so that the second and on the third squares will have $$$3$$$ citizens each.In the second example no matter how citizens act the bandit can catch at least $$$4$$$ citizens.

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

	var n, v int
	Fscan(in, &n)
	g := make([][]int, n+1)
	for w := 2; w <= n; w++ {
		Fscan(in, &v)
		g[v] = append(g[v], w)
	}
	a := make([]int64, n+1)
	for i := 1; i <= n; i++ {
		Fscan(in, &a[i])
	}

	max := func(a, b int64) int64 {
		if a > b {
			return a
		}
		return b
	}
	//g[1] = append(g[1], 0)

	//allMax := int64(0)

	var f func(v, fa int) (sum, maxLeaf int64, leaf int)
	f = func(v, fa int) (sum, maxLeaf int64, leaf int) {
		if len(g[v]) == 0 {
			return a[v], a[v], 1
		}

		for _, w := range g[v] {
			s, mx, l := f(w, v)
			sum += s
			maxLeaf = max(maxLeaf, mx)
			leaf += l
		}
		sum += a[v] //

		full := int64(leaf) * maxLeaf
		if sum > full {
			maxLeaf = (sum-1)/int64(leaf) + 1
		}

		return
	}
	_, ans, _ := f(1, 0)
	Fprintln(out, ans)
}

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


You are given a table $$$a$$$ of size $$$n \times m$$$. We will consider the table rows numbered from top to bottom from $$$1$$$ to $$$n$$$, and the columns numbered from left to right from $$$1$$$ to $$$m$$$. We will denote a cell that is in the $$$i$$$-th row and in the $$$j$$$-th column as $$$(i, j)$$$. In the cell $$$(i, j)$$$ there is written a number $$$(i - 1) \cdot m + j$$$, that is $$$a_{ij} = (i - 1) \cdot m + j$$$.A turtle initially stands in the cell $$$(1, 1)$$$ and it wants to come to the cell $$$(n, m)$$$. From the cell $$$(i, j)$$$ it can in one step go to one of the cells $$$(i + 1, j)$$$ or $$$(i, j + 1)$$$, if it exists. A path is a sequence of cells in which for every two adjacent in the sequence cells the following satisfies: the turtle can reach from the first cell to the second cell in one step. A cost of a path is the sum of numbers that are written in the cells of the path.  For example, with $$$n = 2$$$ and $$$m = 3$$$ the table will look as shown above. The turtle can take the following path: $$$(1, 1) \rightarrow (1, 2) \rightarrow (1, 3) \rightarrow (2, 3)$$$. The cost of such way is equal to $$$a_{11} + a_{12} + a_{13} + a_{23} = 12$$$. On the other hand, the paths $$$(1, 1) \rightarrow (1, 2) \rightarrow (2, 2) \rightarrow (2, 1)$$$ and $$$(1, 1) \rightarrow (1, 3)$$$ are incorrect, because in the first path the turtle can't make a step $$$(2, 2) \rightarrow (2, 1)$$$, and in the second path it can't make a step $$$(1, 1) \rightarrow (1, 3)$$$.You are asked to tell the turtle a minimal possible cost of a path from the cell $$$(1, 1)$$$ to the cell $$$(n, m)$$$. Please note that the cells $$$(1, 1)$$$ and $$$(n, m)$$$ are a part of the way.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The description of test cases follows. A single line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 10^4$$$) — the number of rows and columns of the table $$$a$$$ respectively.

For each test case output a single integer — a minimal possible cost of a path from the cell $$$(1, 1)$$$ to the cell $$$(n, m)$$$.

NoteIn the first test case the only possible path consists of a single cell $$$(1, 1)$$$.The path with the minimal cost in the second test case is shown in the statement.In the fourth and the fifth test cases there is only one path from $$$(1, 1)$$$ to $$$(n, m)$$$. Both paths visit every cell in the table.

package main

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

func path_calc(n, m uint64) uint64 {
	var sum = m * (m + 1) / 2
	sum += m * (n*(n+1)/2 - 1)
	return sum
}

func main() {

	// rd := bufio.NewReader(os.Stdin)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	var line = scanner.Text()
	var num, _ = strconv.Atoi(strings.ReplaceAll(line, "\n", ""))
	ans_arr := make([]uint64, num)
	// fmt.Print(num)
	var i = 0
	for i < num {
		scanner.Scan()
		line = scanner.Text()
		var s = strings.Split(line, " ")
		n, _ := strconv.Atoi(s[0])
		m, _ := strconv.Atoi(strings.ReplaceAll(s[1], "\n", ""))
		// fmt.Println()
		ans_arr[i] = path_calc(uint64(n), uint64(m))
		i++
	}
	for i := 0; i < num; i++ {
		fmt.Fprintf(os.Stdout, "%d\n", ans_arr[i])
	}
	return
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	for ; t > 0; t-- {
		var m, n int64
		fmt.Scanf("%d %d\n", &m, &n)
		val1 := (1+n)*n/2 + n*(1+m)*m/2 - n
		val2 := n*(0+m-1)*m/2 + m + m*n*n - (0+n-2)*(n-1)/2
		ans := int64(math.Min(float64(val1), float64(val2)))
		fmt.Println(ans)
	}
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	for ; t > 0; t-- {
		var m, n int64
		fmt.Scanf("%d%d\n", &m, &n)
		val1 := (1+n)*n/2 + n*(1+m)*m/2 - n
		val2 := n*(0+m-1)*m/2 + m + m*n*n - (0+n-2)*(n-1)/2
		ans := int64(math.Min(float64(val1), float64(val2)))
		fmt.Println(ans)
	}
}

package main

import "fmt"

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

}

package main

import "fmt"

func main() {
	var T int
	var a, b float64
	var res float64

	fmt.Scan(&T)

	for T > 0 {
		res = 0
		fmt.Scan(&a)
		fmt.Scan(&b)
		firstRow := ((1 + b) * (b / 2.0))
		lastColumn := ((b + (a * b)) * (a / 2.0))
		res = firstRow + lastColumn - b
		fmt.Printf("%d\n", int64(res))
		T--
	}

}

package main

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

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

func tc() {
	var n, m int64
	fmt.Fscan(r, &n)
	fmt.Fscan(r, &m)
	var sol int64
	sol = (m * (m + 1)) / 2
	sol += m * (n * (n + 1)) / 2
	sol -= m
	fmt.Fprintln(w, sol)
}

func main() {
	defer w.Flush()
	var t int
	fmt.Fscan(r, &t)
	for i := 0; i < t; i++ {
		tc()
	}
}

package main

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

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

func (in *CF1700A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1700A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1700A
Date: 26/06/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1700/A
Problem: CF1700A
**/
func (in *CF1700A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n, m := in.NextInt64(), in.NextInt64()
		sum := (m * (m + 1)) / 2

		for i := int64(1); i < n; i++ {
			sum += m*(i+1)
		}

		fmt.Println(sum)
	}
}

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

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

package main
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var y,x int
		fmt.Fscan(in,&y,&x)
		sum:= (x*x+x)/2
		for j:=2;j<=y;j++{
			sum += j*x
		}
		fmt.Fprintln(out,sum)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int     { i,e := strconv.Atoi(gs()); if e != nil {panic(e)}; return i }
func gis(n int) []int  { res := make([]int,n); for i:=0;i<n;i++ { res[i] = gi() }; return res }
func gf() float64 { f,e := strconv.ParseFloat(gs(),64); if e != nil {panic(e)}; return f }
func gbs() []byte { return []byte(gs()) }
func gfs(n int) []float64  { res := make([]float64,n); for i:=0;i<n;i++ { res[i] = gf() }; return res }
func gss(n int) []string  { res := make([]string,n); for i:=0;i<n;i++ { res[i] = gs() }; return res }
func gi64() int64     { i,e := strconv.ParseInt(gs(),10,64); if e != nil {panic(e)}; return i }
func gis64(n int) []int64  { res := make([]int64,n); for i:=0;i<n;i++ { res[i] = gi64() }; return res }

func ia(m int) []int { return make([]int,m) }
func iai(m int,v int) []int { a := make([]int,m); for i:=0;i<m;i++ { a[i] = v }; return a }
func twodi(n int,m int,v int) [][]int {
	r := make([][]int,n); for i:=0;i<n;i++ { x := make([]int,m); for j:=0;j<m;j++ { x[j] = v }; r[i] = x }; return r
}
func fill2(m int) ([]int,[]int) { a,b := ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i] = gi(),gi()}; return a,b }
func fill3(m int) ([]int,[]int,[]int) { a,b,c := ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i] = gi(),gi(),gi()}; return a,b,c }
func fill4(m int) ([]int,[]int,[]int,[]int) { a,b,c,d := ia(m),ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i],d[i] = gi(),gi(),gi(),gi()}; return a,b,c,d }
func abs(a int) int { if a < 0 { return -a }; return a }
func rev(a []int) { i,j := 0,len(a)-1; for i < j { a[i],a[j] = a[j],a[i]; i++; j-- } }
func max(a,b int) int { if a > b { return a }; return b }
func min(a,b int) int { if a > b { return b }; return a }
func maxarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa > ans { ans = aa } }; return ans }
func minarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa < ans { ans = aa } }; return ans }
func sumarr(a []int) int { ans := 0; for _,aa := range(a) { ans += aa }; return ans }
func zeroarr(a []int) { for i:=0; i<len(a); i++ { a[i] = 0 } }
func powmod(a,e,mod int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m % mod }; m = m * m % mod; e >>= 1 }; return res }
func powint(a,e int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m }; m = m * m; e >>= 1 }; return res }
func gcd(a,b int) int { for b != 0 { t:=b; b=a%b; a=t }; return a }
func gcdExtended(a,b int) (int,int,int) { if a == 0 { return b,0,1 }; gcd,x1,y1 := gcdExtended(b%a,a); return gcd, y1-(b/a)*x1,x1 }
func modinv(a,m int) (int,bool) { g,x,_ := gcdExtended(a,m); if g != 1 { return 0,false }; return (x % m + m) % m,true  }
func vecintstring(a []int) string { astr := make([]string,len(a)); for i,a := range a { astr[i] = strconv.Itoa(a) }; return strings.Join(astr," ") }
func makefact(n int,mod int) ([]int,[]int) {
	fact,factinv := make([]int,n+1),make([]int,n+1)
	fact[0] = 1; for i:=1;i<=n;i++ { fact[i] = fact[i-1] * i % mod }
	factinv[n] = powmod(fact[n],mod-2,mod); for i:=n-1;i>=0;i-- { factinv[i] = factinv[i+1] * (i+1) % mod }
	return fact,factinv
}

func ia64(m int) []int64 { return make([]int64,m) }
func iai64(m int,v int64) []int64 { a := make([]int64,m); for i:=0;i<m;i++ { a[i] = v }; return a }
func twodi64(n int,m int,v int64) [][]int64 {
	r := make([][]int64,n); for i:=0;i<n;i++ { x := make([]int64,m); for j:=0;j<m;j++ { x[j] = v }; r[i] = x }; return r
}
func fill264(m int) ([]int64,[]int64) { a,b := ia64(m),ia64(m); for i:=0;i<m;i++ {a[i],b[i] = gi64(),gi64()}; return a,b }
func fill364(m int) ([]int64,[]int64,[]int64) { a,b,c := ia64(m),ia64(m),ia64(m); for i:=0;i<m;i++ {a[i],b[i],c[i] = gi64(),gi64(),gi64()}; return a,b,c }
func fill464(m int) ([]int64,[]int64,[]int64,[]int64) { a,b,c,d := ia64(m),ia64(m),ia64(m),ia64(m); for i:=0;i<m;i++ {a[i],b[i],c[i],d[i] = gi64(),gi64(),gi64(),gi64()}; return a,b,c,d }
func abs64(a int64) int64 { if a < 0 { return -a }; return a }
func rev64(a []int64) { i,j := 0,len(a)-1; for i < j { a[i],a[j] = a[j],a[i]; i++; j-- } }
func max64(a,b int64) int64 { if a > b { return a }; return b }
func min64(a,b int64) int64 { if a > b { return b }; return a }
func maxarr64(a []int64) int64 { ans := a[0]; for _,aa := range(a) { if aa > ans { ans = aa } }; return ans }
func minarr64(a []int64) int64 { ans := a[0]; for _,aa := range(a) { if aa < ans { ans = aa } }; return ans }
func sumarr64(a []int64) int64 { ans := int64(0); for _,aa := range(a) { ans += aa }; return ans }
func zeroarr64(a []int64) { for i:=0; i<len(a); i++ { a[i] = 0 } }
func powmod64(a,e,mod int64) int64 { res, m := int64(1), a; for e > 0 { if e&1 != 0 { res = res * m % mod }; m = m * m % mod; e >>= 1 }; return res }
func powint64(a,e int64) int64 { res, m := int64(1), a; for e > 0 { if e&1 != 0 { res = res * m }; m = m * m; e >>= 1 }; return res }
func gcd64(a,b int64) int64 { for b != 0 { t:=b; b=a%b; a=t }; return a }
func gcdExtended64(a,b int64) (int64,int64,int64) { if a == 0 { return b,0,1 }; gcd,x1,y1 := gcdExtended64(b%a,a); return gcd, y1-(b/a)*x1,x1 }
func modinv64(a,m int64) (int64,bool) { g,x,_ := gcdExtended64(a,m); if g != 1 { return 0,false }; return (x % m + m) % m,true  }
func vecint64string(a []int64) string { astr := make([]string,len(a)); for i,a := range a { astr[i] = strconv.FormatInt(a,10) }; return strings.Join(astr," ") }
func makefact64(n int,mod int64) ([]int64,[]int64) {
	fact,factinv := make([]int64,n+1),make([]int64,n+1)
	fact[0] = 1; for i:=1;i<=n;i++ { fact[i] = fact[i-1] * int64(i) % mod }
	factinv[n] = powmod64(fact[n],mod-2,mod); for i:=n-1;i>=0;i-- { factinv[i] = factinv[i+1] * int64(i+1) % mod }
	return fact,factinv
}
func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
	T := gi()
	for tt:=1;tt<=T;tt++ {
		N,M := gi64(),gi64(); fmt.Fprintln(wrtr, M * (M-1) / 2 + M * (N+1) * N / 2 )
	}
}

package main

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

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

	qq := 0
	fmt.Fscan(in, &qq)
	for ii := 0; ii < qq; ii++ {
		n := uint64(0)
		m := uint64(0)
		fmt.Fscan(in, &n)
		fmt.Fscan(in, &m)
		fmt.Fprintln(out, m * (m - 1) / 2 + (m + n * m) * n / 2)
	}
}

package main

import "fmt"

func main() {
	var t, n, m, ans int64
	fmt.Scan(&t)
	for ; t > 0; t-- {
		fmt.Scan(&n, &m)
		ans = m * (m - 1) / 2
		ans += n * (n + 1) / 2 * m
		fmt.Println(ans)
	}
}

package main

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

var (
	reader bufio.Reader
	writer bufio.Writer
)

func solve() {
	var n, m int64

	scan(&n, &m)

	printf("%v\n", m * (m - 1) / 2 + m * n * (n + 1) / 2)
}

func main() {
	inp, err := os.Open("input.txt")
	if err == nil {
		os.Stdin = inp
		outp, _ := os.Create("output.txt")
		os.Stdout = outp
		defer outp.Close()
		defer inp.Close()
	}

	reader = *bufio.NewReader(os.Stdin)
	writer = *bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	T := 1
	scan(&T)

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

func scan(a ...interface{}) (n int, err error) { return fmt.Fscan(&reader, a...) }
func printf(format string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(&writer, format, a...)
}

package main

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

var rr = bufio.NewReader(os.Stdin)
var ww = bufio.NewWriter(os.Stdout)

func solve() {
	var n, m int64
	fmt.Fscan(rr, &n, &m)
	// fmt.Fscan(rr,&m)
	var ans int64 = 0
	ans += (1 + m) * m / 2
	ans += (2*m + n*m) * (n - 1) / 2
	fmt.Println(ans)

}
func main() {
	var T int
	fmt.Fscan(rr, &T)
	for ; T > 0; T-- {
		solve()
	}
}

package main

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

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

func solve(n, m int64) int64 {
	return m*(m-1)/2 + m*(n*(n+1)/2)
}

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

package main

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

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

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

	for t := 0; t < T; t++ {
		var n, m int64
		fmt.Fscan(in, &n, &m)
		fmt.Fprintln(out, m*(1+m+(n+2)*(n-1))>>1)
	}
}

package main

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

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

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

		fmt.Fprintln(out, (m*(m+1)/2)+(m*n*(n+1)/2)-m)

	}
}

package main

import (
	"fmt"
	//"math"
)

func main() {
	var n int
	fmt.Scan(&n)
	for k := 0; k < n; k++ {
		var n int64
		var m int64
		fmt.Scan(&n)
		fmt.Scan(&m)
		res := (m*(m-1)/2 + (m+n*m)*n/2)
		fmt.Println(res)
	}
}

package main

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

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

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

	var T int
	var n int
	var m int

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &m)
		Fscan(in, &n)
		ans := int64(0)
		for i := 0; i < n; i++ {
			ans += int64(i + 1)
		}
		for j := 2; j <= m; j++ {
			ans += int64(j*n)
		}

		Fprintln(out, ans)
	}
}

package main

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

func A1() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	Fscan(in, &cas)
	// max := func(a, b int) int {
	// 	if a > b {
	// 		return a
	// 	}
	// 	return b
	// }
	var sol func()
	// Coding...beg!
	sol = func() {
		var n, m int64
		Fscan(in, &n, &m)
		Fprintln(out, (1+m)*m/2+(m+n*m)*n/2-m)
	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}
func main() { A1() }

package main

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

/*
1
3 4
---
30

1  2  3  4
5  6  7  8
9 10 11 12

1 2 3
4 5 6

1
2 3

*/

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

	pathLen := m * (m + 1) / 2

	for i := int64(2); i <= n; i++ {
		pathLen += i * m
	}

	fmt.Fprintln(writer, pathLen)
}

//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 int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int) int {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func sum(nums ...int) (sum int) {
	for _, num := range nums {
		sum += num
	}
	return
}

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

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

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

	for i := int64(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))
}

//goland:noinspection GoUnusedFunction
func factorize(num int64) []int64 {
	factors := make([]int64, 0)

	for ftr := int64(2); ftr*ftr <= num; ftr++ {
		for num%ftr == 0 {
			num /= ftr
			factors = append(factors, ftr)
		}
	}

	if num > 1 {
		factors = append(factors, num)
	}

	return factors
}

//template from https://github.com/Cody-Wolf/Golang-in-ACM
package main

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

func Solve() {
	n, m := ri(), ri()
	var ans int64
	ans = 0
	for i := 1; i <= m; i++ {
		ans += int64(i)
	}
	for i := m + m; i <= n*m; i += m {
		ans += int64(i)
	}
	println(ans)
}

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

func main() {
	T := 0 // 如果是多组样例，请讲 T := 1 改为 T := 0
	in, out = bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	if T == 0 {
		scan(&T)
	}
	for t := 0; t < T; t++ {
		Solve()
	}
	out.Flush()
}

var in *bufio.Reader
var out *bufio.Writer

// ri 将直接读入并返回一个 int
func ri() (x int) {
	scan(&x)
	return x
}

// ri64 将直接读入并返回一个 int64
func ri64() (x int64) {
	scan(&x)
	return x
}

//rl 将直接读入一行整数，并返回一个 int 切片
func rl(n int) (a []int) {
	for i := 0; i < n; i++ {
		a = append(a, ri())
	}
	return a
}

//rl64 将直接读入一行整数，并返回一个 int64 切片
func rl64(n int) (a []int64) {
	for i := 0; i < n; i++ {
		a = append(a, ri64())
	}
	return a
}

//rs 将直接读入并返回一个 string
func rs() (s string) {
	scan(&s)
	return s
}

//scan 用法与 fmt.scan() 相似，可以传入多个变量的地址进行读入操作
func scan(a ...interface{}) { Fscan(in, a...) }

//print 用法与 fmt.print() 相似，可以传入多个变量进行输出。
func print(a ...interface{}) { Fprint(out, a...) }

//println 用法与 fmt.println() 相似，可以传入多个变量进行输出并在最后换行。
func println(a ...interface{}) { Fprintln(out, a...) }

//min 可以传入多个 int 变量，求出这些变量的最小值
func min(a ...int) int {
	ans := a[0]
	for i := 1; i < len(a); i++ {
		if a[i] < ans {
			ans = a[i]
		}
	}
	return ans
}

//min64 可以传入多个 int64 变量，求出这些变量的最小值
func min64(a ...int64) int64 {
	ans := a[0]
	for i := 1; i < len(a); i++ {
		if a[i] < ans {
			ans = a[i]
		}
	}
	return ans
}

//max 可以传入多个 int 变量，求出这些变量的最大值
func max(a ...int) int {
	ans := a[0]
	for i := 1; i < len(a); i++ {
		if a[i] > ans {
			ans = a[i]
		}
	}
	return ans
}

//max64 可以传入多个 int64 变量，求出这些变量的最大值
func max64(a ...int64) int64 {
	ans := a[0]
	for i := 1; i < len(a); i++ {
		if a[i] > ans {
			ans = a[i]
		}
	}
	return ans
}

//sum 可以传入多个 int 变量，求出这些变量的总和，并返回总和。
//
//你可以使用 ... 运算符打散数组，使得 sum 函数可以求出数组的和。
func sum(a ...int) int {
	ans := 0
	for _, x := range a {
		ans += x
	}
	return ans
}

//sum64 可以传入多个 int64 变量，求出这些变量的总和，并返回总和。
//
//你可以使用 ... 运算符打散数组，使得 sum64 函数可以求出数组的和。
func sum64(a ...int64) int64 {
	ans := int64(0)
	for _, x := range a {
		ans += x
	}
	return ans
}

//reverse 用于翻转 int 切片，所有元素将会变成原切片的倒序
func reverse(a []int) {
	for l, r := 0, len(a)-1; l < r; l, r = l+1, r-1 {
		a[l], a[r] = a[r], a[l]
	}
}

//reverse64 用于翻转 int64 切片，所有元素将会变成原切片的倒序
func reverse64(a []int64) {
	for l, r := 0, len(a)-1; l < r; l, r = l+1, r-1 {
		a[l], a[r] = a[r], a[l]
	}
}

// lowerBound 类似于 C++ 中的 lower_bound，用于二分查找 int 切片中，第一个大于等于 k 的元素在切片中的位置。
//
//如果元素不存在，则返回切片的长度。
//
//注意，lowerBound 作用的 int 切片必须升序。
func lowerBound(a []int, k int) int {
	l, r, pos := 0, len(a)-1, len(a)
	for l <= r {
		mid := (l + r) >> 1
		if a[mid] < k {
			l = mid + 1
		} else {
			pos, r = mid, mid-1
		}
	}
	return pos
}

//lowerBound64 类似于 C++ 中的 lower_bound，用于二分查找 int64 切片中，第一个大于等于 k 的元素在切片中的位置。
//
//如果元素不存在，则返回切片的长度。
//
//注意，lowerBound64 作用的 int64 切片必须升序。
func lowerBound64(a []int64, k int64) int {
	l, r, pos := 0, len(a)-1, len(a)
	for l <= r {
		mid := (l + r) >> 1
		if a[mid] < k {
			l = mid + 1
		} else {
			pos, r = mid, mid-1
		}
	}
	return pos
}

//upperBound 类似于 C++ 中的 upper_bound，用于二分查找 int 切片中，第一个大于 k 的元素在切片中的位置。
//
//如果元素不存在，则返回切片的长度。
//
//注意，upperBound 作用的 int 切片必须升序。
func upperBound(a []int, k int) int {
	l, r, pos := 0, len(a)-1, len(a)
	for l <= r {
		mid := (l + r) >> 1
		if a[mid] <= k {
			l = mid + 1
		} else {
			pos, r = mid, mid-1
		}
	}
	return pos
}

//upperBound64 类似于 C++ 中的 upper_bound，用于二分查找 int64 切片中，第一个大于 k 的元素在切片中的位置。
//
//如果元素不存在，则返回切片的长度。
//
//注意，upperBound64 作用的 int64 切片必须升序。
func upperBound64(a []int64, k int64) int {
	l, r, pos := 0, len(a)-1, len(a)
	for l <= r {
		mid := (l + r) >> 1
		if a[mid] <= k {
			l = mid + 1
		} else {
			pos, r = mid, mid-1
		}
	}
	return pos
}

//unique 类似于 C++ 中的 unique，用于给切片去重。
//
//区别在于: unique 函数接受一个 int 切片，并返回一个新的、去好重的切片
//
//注意: 传入的切片必须升序
func unique(a []int) []int {
	var b []int
	b = append(b, a[0])
	for i := 1; i < len(a); i++ {
		if a[i] != a[i-1] {
			b = append(b, a[i])
		}
	}
	return b
}

//valSet 将传入切片的所有元素，统一赋值为传入的元素 k
func valSet(a []int, k int) {
	for i, _ := range a {
		a[i] = k
	}
}

//Stack 类型是一个 int 类型的栈
type Stack struct {
	s []int
}

//top 获取栈顶元素
func (st *Stack) top() int { return st.s[len(st.s)-1] }

//size 返回栈内的元素个数
func (st *Stack) size() int { return len(st.s) }

//empty 返回一个 bool 类型，表示栈是否为空
func (st *Stack) empty() bool { return st.size() == 0 }

//pop 弹出栈顶元素
func (st *Stack) pop() { st.s = st.s[:len(st.s)-1] }

//push 将一个 int 类型从栈顶添加入栈
func (st *Stack) push(x int) { st.s = append(st.s, x) }

//get 得到栈的内容（一个新的 int切片）
func (st *Stack) get() (ans []int) {
	copy(ans, st.s)
	return ans
}

//Queue 类型是一个 int 类型的队列
type Queue struct {
	q    []int
	head int
}

//front 获取队首元素
func (que *Queue) front() int { return que.q[que.head] }

//size 返回队列的元素个数
func (que *Queue) size() int { return len(que.q) - que.head }

//empty 返回一个 bool 类型，表示队列是否为空
func (que *Queue) empty() bool { return que.size() == 0 }

//pop 弹出队首元素
func (que *Queue) pop() { que.head++ }

//push 将一个 int 类型从队尾添加入队
func (que *Queue) push(x int) { que.q = append(que.q, x) }

//get 得到队列的内容（一个新的 int切片）
func (que *Queue) get() (ans []int) {
	copy(ans, que.q)
	return ans
}

//PriorityQueue 类似是一个 int 型的优先队列
//
//注意：你需要通过 newPriorityQueue 函数来获取一个优先队列实例，而不是直接创建
type PriorityQueue struct {
	heap []int
	cmp  func(int, int) bool
}

//heapifyUp 从 x 结点开始到堆顶，调整结点使得满足堆的性质
//
//注意：你不应该直接使用这个函数
func (pq *PriorityQueue) heapifyUp(x int) {
	for x > 1 && pq.cmp(pq.heap[x-1], pq.heap[x/2-1]) {
		pq.heap[x-1], pq.heap[x/2-1] = pq.heap[x/2-1], pq.heap[x-1]
		x >>= 1
	}
}

//heapifyDown 从 x 结点开始向子节点，调整结点使得满足堆的性质
//
//注意：你不应该直接使用这个函数
func (pq *PriorityQueue) heapifyDown(x int) {
	for (x << 1) <= len(pq.heap) {
		t := x << 1
		if t+1 <= len(pq.heap) && pq.cmp(pq.heap[t+1-1], pq.heap[t-1]) {
			t++
		}
		if !pq.cmp(pq.heap[t-1], pq.heap[x-1]) {
			break
		}
		pq.heap[x-1], pq.heap[t-1] = pq.heap[t-1], pq.heap[x-1]
		x = t
	}
}

//push 向优先队列中加入一个元素（并自动调整优先队列）
func (pq *PriorityQueue) push(x int) {
	pq.heap = append(pq.heap, x)
	pq.heapifyUp(len(pq.heap))
}

//size 返回优先队列内的元素个数
func (pq *PriorityQueue) size() int { return len(pq.heap) }

//top 返回优先队列中优先级最高的元素
func (pq *PriorityQueue) top() int { return pq.heap[1-1] }

//pop 弹出优先队列中优先级最高的元素
func (pq *PriorityQueue) pop() {
	pq.heap[1-1], pq.heap[len(pq.heap)-1] = pq.heap[len(pq.heap)-1], pq.heap[1-1]
	pq.heap = pq.heap[:len(pq.heap)-1]
	pq.heapifyDown(1)
}

//newPriorityQueue 返回一个指向优先队列的指针
//
//第一个形参：传入一个 int 类型切片，创建优先队列时，你可以用一个 int 类型切片作为初始来创建，也可以设为空切片（nil)
//
//第二个形参：传入一个 func(int, int) bool 类型的函数指针作为比较器，以规定优先队列的优先级。
//传入比较器的两个元素如果满足前者比后者大，则返回 true；否则返回 false。
//
//你可以使用已有的 greater 函数指定小的 int 优先，也可以使用已有的 less 函数指定大的 int 优先
func newPriorityQueue(a []int, fun func(int, int) bool) *PriorityQueue {
	var q PriorityQueue
	copy(q.heap, a)
	q.cmp = fun
	for i := len(a); i >= 1; i-- {
		q.heapifyDown(i)
	}
	return &q
}

//less 判断传入参数 a 和 b 是否满足 a > b
func less(a, b int) bool {
	if a > b {
		return true
	} else {
		return false
	}
}

//greater 判断传入参数 a 和 b 是否满足 a < b
func greater(a, b int) bool {
	if a < b {
		return true
	} else {
		return false
	}
}

package main

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

func path_calc(n, m int32) int32 {
	var sum = m * (m + 1) / 2
	sum += m * (n*(n+1)/2 - 1)
	return sum
}

func main() {

	// rd := bufio.NewReader(os.Stdin)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	var line = scanner.Text()
	var num, _ = strconv.Atoi(strings.ReplaceAll(line, "\n", ""))
	ans_arr := make([]int32, num)
	// fmt.Print(num)
	var i = 0
	for i < num {
		scanner.Scan()
		line = scanner.Text()
		var s = strings.Split(line, " ")
		n, _ := strconv.Atoi(s[0])
		m, _ := strconv.Atoi(strings.ReplaceAll(s[1], "\n", ""))
		// fmt.Println()
		ans_arr[i] = path_calc(int32(n), int32(m))
		i++
	}
	for i := 0; i < num; i++ {
		fmt.Fprintf(os.Stdout, "%d\n", ans_arr[i])
	}
	return
}

package main

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

func path_calc(n, m int) int {
	var sum = m * (m + 1) / 2
	sum += m * (n*(n+1)/2 - 1)
	return sum
}

func main() {

	// rd := bufio.NewReader(os.Stdin)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	var line = scanner.Text()
	var num, _ = strconv.Atoi(strings.ReplaceAll(line, "\n", ""))
	ans_arr := make([]int, num)
	// fmt.Print(num)
	var i = 0
	for i < num {
		scanner.Scan()
		line = scanner.Text()
		var s = strings.Split(line, " ")
		n, _ := strconv.Atoi(s[0])
		m, _ := strconv.Atoi(strings.ReplaceAll(s[1], "\n", ""))
		// fmt.Println()
		ans_arr[i] = path_calc(n, m)
		i++
	}
	for i := 0; i < num; i++ {
		fmt.Fprintf(os.Stdout, "%d\n", ans_arr[i])
	}
	return
}

package main

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

func path_calc(n, m int) int {
	var sum = n * (n + 1) / 2
	sum += n * (m*(m+1)/2 - 1)
	return sum
}

func main() {

	// rd := bufio.NewReader(os.Stdin)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	var line = scanner.Text()
	var num, _ = strconv.Atoi(strings.ReplaceAll(line, "\n", ""))
	ans_arr := make([]int, num)
	// fmt.Print(num)
	var i = 0
	for i < num {
		scanner.Scan()
		line = scanner.Text()
		var s = strings.Split(line, " ")
		n, _ := strconv.Atoi(s[0])
		m, _ := strconv.Atoi(strings.ReplaceAll(s[1], "\n", ""))
		// fmt.Println()
		ans_arr[i] = path_calc(n, m)
		i++
	}
	for i := 0; i < num; i++ {
		fmt.Fprintf(os.Stdout, "%d\n", ans_arr[i])
	}
	return
}

package main

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

func path_calc(n, m int) int {
	var sum = n * (n + 1) / 2
	sum += n * (m*(m+1)/2 - 1)
	return sum
}

func main() {

	rd := bufio.NewReader(os.Stdin)
	var line, _ = rd.ReadString('\n')
	var num, _ = strconv.Atoi(strings.ReplaceAll(line, "\n", ""))
	fmt.Print(num)
	var i = 0
	for i < num {
		line, _ = rd.ReadString('\n')
		var s = strings.Split(line, " ")
		n, _ := strconv.Atoi(s[0])
		m, _ := strconv.Atoi(strings.ReplaceAll(s[1], "\n", ""))
		fmt.Println(path_calc(n, m))
		i++
	}
	return
}

package main

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

func path_calc(n, m int) int {
	var sum = n * (n + 1) / 2
	sum += n * (m*(m+1)/2 - 1)
	return sum
}

func main() {

	rd := bufio.NewReader(os.Stdin)
	var line, _ = rd.ReadString('\n')
	var num, _ = strconv.Atoi(strings.ReplaceAll(line, "\n", ""))
	// fmt.Print(num)
	var i = 0
	for i < num {
		line, _ = rd.ReadString('\n')
		var s = strings.Split(line, " ")
		n, _ := strconv.Atoi(s[0])
		m, _ := strconv.Atoi(strings.ReplaceAll(s[1], "\n", ""))
		fmt.Printf("%d/n", path_calc(n, m))
		i++
	}
	return
}

package main

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

func path_calc(n, m int) int {
	var sum = n * (n + 1) / 2
	sum += n * (m*(m+1)/2 - 1)
	return sum
}

func main() {

	rd := bufio.NewReader(os.Stdin)
	var line, _ = rd.ReadString('\n')
	var num, _ = strconv.Atoi(strings.ReplaceAll(line, "\n", ""))
	ans_arr := make([]int, num)
	// fmt.Print(num)
	var i = 0
	for i < num {
		line, _ = rd.ReadString('\n')
		var s = strings.Split(line, " ")
		n, _ := strconv.Atoi(s[0])
		m, _ := strconv.Atoi(strings.ReplaceAll(s[1], "\n", ""))
		// fmt.Println()
		ans_arr[i] = path_calc(n, m)
		i++
	}
	for i := 0; i < num; i++ {
		fmt.Println(ans_arr[i])
	}
	return
}

package main

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

func path_calc(n, m int) int {
	var sum = n * (n + 1) / 2
	sum += n * (m*(m+1)/2 - 1)
	return sum
}

func main() {

	rd := bufio.NewReader(os.Stdin)
	var line, _ = rd.ReadString('\n')
	var num, _ = strconv.Atoi(strings.ReplaceAll(line, "\n", ""))
	// fmt.Print(num)
	var i = 0
	for i < num {
		line, _ = rd.ReadString('\n')
		var s = strings.Split(line, " ")
		n, _ := strconv.Atoi(s[0])
		m, _ := strconv.Atoi(strings.ReplaceAll(s[1], "\n", ""))
		fmt.Println(path_calc(n, m))
		i++
	}
	return
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var t int
	fmt.Scanf("%d", &t)
	for ; t > 0; t-- {
		var m, n int64
		fmt.Scanf("%d%d", &m, &n)
		val1 := (1+n)*n/2 + n*(1+m)*m/2 - n
		val2 := n*(0+m-1)*m/2 + m + m*n*n - (0+n-2)*(n-1)/2
		ans := int64(math.Min(float64(val1), float64(val2)))
		fmt.Println(ans)
	}
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var t int64
	fmt.Scanf("%d", &t)
	for ; t > 0; t-- {
		var m, n int64
		fmt.Scanf("%d %d", &m, &n)
		val1 := (1+n)*n/2 + n*(1+m)*m/2 - n
		val2 := n*(0+m-1)*m/2 + m + m*n*n - (0+n-2)*(n-1)/2
		ans := int64(math.Min(float64(val1), float64(val2)))
		fmt.Println(ans)
	}
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var t int
	fmt.Scanf("%d", &t)
	for ; t > 0; t-- {
		var m, n int64
		fmt.Scanf("%d %d", &m, &n)
		val1 := (1+n)*n/2 + n*(1+m)*m/2 - n
		val2 := n*(0+m-1)*m/2 + m + m*n*n - (0+n-2)*(n-1)/2
		ans := int64(math.Min(float64(val1), float64(val2)))
		fmt.Println(ans)
	}
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var t int
	fmt.Scanf("%d", &t)
	for ; t > 0; t-- {
		var m, n int64
		fmt.Scanf("%d %d", &m, &n)
		val1 := (1+n)*n/2 + n*(1+m)*m/2 - n
		val2 := n*(0+m-1)*m/2 + m + m*n*n - (0+n-2)*(n-1)/2
		ans := int(math.Min(float64(val1), float64(val2)))
		fmt.Println(ans)
	}
}

package main

import "fmt"

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

}

package main

import "fmt"

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

}

package main

import "fmt"

func main() {
	var T int
	var a, b float64
	var res float64

	fmt.Scan(&T)

	for T > 0 {
		res = 0
		fmt.Scan(&a)
		fmt.Scan(&b)
		firstRow := ((1 + b) * (b / 2.0))
		lastColumn := ((b + (a * b)) * (a / 2.0))
		res = firstRow + lastColumn - b
		fmt.Printf("%d\n", int(res))
		T--
	}

}

package main

import "fmt"

func main() {
	var T int
	var a, b float64
	var res float64

	fmt.Scan(&T)

	for T > 0 {
		res = 0
		fmt.Scan(&a)
		fmt.Scan(&b)
		firstRow := ((1 + b) * (b / 2.0))
		lastColumn := ((b + (a * b)) * (a / 2.0))
		res = firstRow + lastColumn - b
		fmt.Println(res)
		T--
	}

}

package main

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

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

func tc() {
	var n int
	fmt.Fscan(r, &n)
	var m int
	fmt.Fscan(r, &m)
	sol := (m * (m + 1)) / 2
	sol += m * (n * (n + 1)) / 2
	sol -= m
	fmt.Fprintln(w, sol)
}

func main() {
	defer w.Flush()
	var t int
	fmt.Fscan(r, &t)
	for i := 0; i < t; i++ {
		tc()
	}
}

package main

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

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

func (in *CF1700A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1700A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1700A
Date: 26/06/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1700/A
Problem: CF1700A
**/
func (in *CF1700A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n, m := in.NextInt(), in.NextInt()
		sum := (m * (m + 1)) / 2

		for i := 1; i < n; i++ {
			sum += m*(i+1)
		}

		fmt.Println(sum)
	}
}

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

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


You are given a tree (a connected undirected graph without cycles) of $$$n$$$ vertices. Each of the $$$n - 1$$$ edges of the tree is colored in either black or red.You are also given an integer $$$k$$$. Consider sequences of $$$k$$$ vertices. Let's call a sequence $$$[a_1, a_2, \ldots, a_k]$$$ good if it satisfies the following criterion:  We will walk a path (possibly visiting same edge/vertex multiple times) on the tree, starting from $$$a_1$$$ and ending at $$$a_k$$$.  Start at $$$a_1$$$, then go to $$$a_2$$$ using the shortest path between $$$a_1$$$ and $$$a_2$$$, then go to $$$a_3$$$ in a similar way, and so on, until you travel the shortest path between $$$a_{k-1}$$$ and $$$a_k$$$. If you walked over at least one black edge during this process, then the sequence is good.   Consider the tree on the picture. If $$$k=3$$$ then the following sequences are good: $$$[1, 4, 7]$$$, $$$[5, 5, 3]$$$ and $$$[2, 3, 7]$$$. The following sequences are not good: $$$[1, 4, 6]$$$, $$$[5, 5, 5]$$$, $$$[3, 7, 3]$$$.There are $$$n^k$$$ sequences of vertices, count how many of them are good. Since this number can be quite large, print it modulo $$$10^9+7$$$.

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$2 \le k \le 100$$$), the size of the tree and the length of the vertex sequence. Each of the next $$$n - 1$$$ lines contains three integers $$$u_i$$$, $$$v_i$$$ and $$$x_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$x_i \in \{0, 1\}$$$), where $$$u_i$$$ and $$$v_i$$$ denote the endpoints of the corresponding edge and $$$x_i$$$ is the color of this edge ($$$0$$$ denotes red edge and $$$1$$$ denotes black edge).

Print the number of good sequences modulo $$$10^9 + 7$$$.

NoteIn the first example, all sequences ($$$4^4$$$) of length $$$4$$$ except the following are good:   $$$[1, 1, 1, 1]$$$ $$$[2, 2, 2, 2]$$$ $$$[3, 3, 3, 3]$$$ $$$[4, 4, 4, 4]$$$ In the second example, all edges are red, hence there aren't any good sequences.

package main

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

var mod int64 = 1000000007;
var n int = 0;
var k int = 0;
var g [][]int;
var visited = make([]int, 123456);

func pot(a int64, m int) int64 {
    if m == 0 {
        return 1;
    }
    sq := pot(a, m/2);
    sq *= sq;
    sq %= mod;
    if m % 2 == 0 {
        return sq;
    }
    return (sq * a) % mod;
}

func dfs(v int) int {
    if (visited[v] != 0) {
        return 0;
    }
    lans := 1;
    visited[v] = 1;
    for _, w := range g[v] {
        lans += dfs(w);
    }
    return lans;
}

func main() {
	in := bufio.NewReader(os.Stdin)
	fmt.Fscanf(in, "%d %d\n", &n, &k);

    g = make([][]int, n);

	for i:=0; i < n - 1; i++ {
        a,b,c := 0,0,0;
        fmt.Fscanf(in, "%d %d %d\n", &a, &b, &c);
        a--; b--;
        if c == 0 {
            g[a] = append(g[a], b);
            g[b] = append(g[b], a);
        }
	}
    ans := pot(int64(n), k);
    for i := 0; i < n; i++ {
        visited[i] = 0;
    }
    for i := 0; i < n; i++ {
        if visited[i] == 0 {
            m := dfs(i);
            ans = ans + mod - pot(int64(m), k);
            ans %= mod;
        }
    }
	fmt.Println(ans);
}

package main

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

func power(a, b, p int64) int64 {
	if b == 0 || a == 1 {
		return 1
	}
	ret := power(a, b/2, p)
	ret = (ret * ret) % p
	if (b % 2) == 1 {
		ret = (ret * a) % p
	}
	return ret % p
}
func eddE(u, v int, edge map[int][]int) {
	if len(edge[u]) <= 0 {
		edge[u] = make([]int, 0)
	}
	edge[u] = append(edge[u], v)
}
func dfs(root int, flag []bool, edge map[int][]int) int {
	flag[root] = true
	ret := 1
	for _, v := range edge[root] {
		if flag[v] == false {
			ret += dfs(v, flag, edge)
		}
	}
	return ret
}
func Cal(n, m, p int64) int64 {
	var a, b int64
	a = 1
	b = 1
	for i := m + 1; i <= n; i++ {
		a = (a * i) % p
	}
	for i := n - m; i >= 1; i-- {
		b = (b * i) % p
	}
	return (a * (power(b, p-2, p))) % p
}
func main() {
	buf := bufio.NewReader(os.Stdin)
	var n, k int
	var p int64 = 1000000007
	fmt.Fscanf(buf, "%d %d\n", &n, &k)
	edge := make(map[int][]int)
	var u, v, b int
	for i := 0; i < n-1; i++ {
		fmt.Fscanf(buf, "%d %d %d\n", &u, &v, &b)
		if b == 1 {
			continue
		}
		eddE(u, v, edge)
		eddE(v, u, edge)
	}
	flag := make([]bool, n+1)
	var del int64 = 0
	for i := 1; i <= n; i++ {
		if flag[i] == false {
			cnt := dfs(i, flag, edge)
		//	fmt.Printf("i=%d cnt=%d\n",i,cnt)
			del += power(int64(cnt), int64(k), p)
			del %= p
		}
	}
	ans := power(int64(n), int64(k), p)
	ans = ans - del + p

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

/**
0 0
0 1
3
UDD
 */

package main

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

const (
	mod = int(1e9 + 7)
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n, k := 0, 0
	fmt.Fscanf(in, "%d %d\n", &n, &k)
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, 0)
	}
	for i := 0; i < n-1; i++ {
		aa, b, c := 0, 0, 0
		fmt.Fscanf(in, "%d %d %d\n", &aa, &b, &c)
		aa--
		b--
		if c == 0 {
			a[aa] = append(a[aa], b)
			a[b] = append(a[b], aa)
		}
	}
	blk := []int{}
	vis := make([]bool, n)
	for i := 0; i < n; i++ {
		if !vis[i] {
			vis[i] = true
			queue := make([]int, 0)
			queue = append(queue, i)
			cnt := 0
			for len(queue) > 0 {
				cnt++
				cur := queue[0]
				queue = queue[1:]
				for j := 0; j < len(a[cur]); j++ {
					if !vis[a[cur][j]] {
						vis[a[cur][j]] = true
						queue = append(queue, a[cur][j])
					}
				}
			}
			blk = append(blk, cnt)
		}
	}
	ans := int64(0)
	q := int64(1)
	for i := 0; i < k; i++ {
		q *= int64(n)
		q %= int64(mod)
	}
	ans = q
	//fmt.Println(blk)
	for i := 0; i < len(blk); i++ {
		m := blk[i]
		q = 1
		for j := 0; j < k; j++ {
			q *= int64(m)
			q %= int64(mod)
		}
		ans -= q
		if ans < 0 {
			ans += int64(mod)
		}
		ans %= int64(mod)
	}
	fmt.Fprintf(out, "%d\n", ans)
	return
}


Suppose you have a special $$$x$$$-$$$y$$$-counter. This counter can store some value as a decimal number; at first, the counter has value $$$0$$$. The counter performs the following algorithm: it prints its lowest digit and, after that, adds either $$$x$$$ or $$$y$$$ to its value. So all sequences this counter generates are starting from $$$0$$$. For example, a $$$4$$$-$$$2$$$-counter can act as follows:  it prints $$$0$$$, and adds $$$4$$$ to its value, so the current value is $$$4$$$, and the output is $$$0$$$;  it prints $$$4$$$, and adds $$$4$$$ to its value, so the current value is $$$8$$$, and the output is $$$04$$$;  it prints $$$8$$$, and adds $$$4$$$ to its value, so the current value is $$$12$$$, and the output is $$$048$$$;  it prints $$$2$$$, and adds $$$2$$$ to its value, so the current value is $$$14$$$, and the output is $$$0482$$$;  it prints $$$4$$$, and adds $$$4$$$ to its value, so the current value is $$$18$$$, and the output is $$$04824$$$. This is only one of the possible outputs; for example, the same counter could generate $$$0246802468024$$$ as the output, if we chose to add $$$2$$$ during each step.You wrote down a printed sequence from one of such $$$x$$$-$$$y$$$-counters. But the sequence was corrupted and several elements from the sequence could be erased.Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string $$$s$$$ — the remaining data of the sequence. For all $$$0 \le x, y < 10$$$, calculate the minimum number of digits you have to insert in the string $$$s$$$ to make it a possible output of the $$$x$$$-$$$y$$$-counter. Note that you can't change the order of digits in string $$$s$$$ or erase any of them; only insertions are allowed.

The first line contains a single string $$$s$$$ ($$$1 \le |s| \le 2 \cdot 10^6$$$, $$$s_i \in \{\text{0} - \text{9}\}$$$) — the remaining data you have. It's guaranteed that $$$s_1 = 0$$$.

Print a $$$10 \times 10$$$ matrix, where the $$$j$$$-th integer ($$$0$$$-indexed) on the $$$i$$$-th line ($$$0$$$-indexed too) is equal to the minimum number of digits you have to insert in the string $$$s$$$ to make it a possible output of the $$$i$$$-$$$j$$$-counter, or $$$-1$$$ if there is no way to do so.

NoteLet's take, for example, $$$4$$$-$$$3$$$-counter. One of the possible outcomes the counter could print is $$$0(4)8(1)4(7)0$$$ (lost elements are in the brackets).One of the possible outcomes a $$$2$$$-$$$3$$$-counter could print is $$$0(35)8(1)4(7)0$$$.The $$$6$$$-$$$8$$$-counter could print exactly the string $$$0840$$$.

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1202B(_r io.Reader, _w io.Writer) {
	s, _ := ioutil.ReadAll(_r)
	s = bytes.TrimSpace(s)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	cnts := [10]int{}
	for i, c := range s[1:] {
		cnts[(c-s[i]+10)%10]++
	}
	ans := [10][10]int{}
	for x := range ans {
	loop:
		for y := range ans {
			ops := [10]int{}
			for i := range ops {
				ops[i] = 99
			}
			for i := 0; i < 10; i++ {
				for j := 0; j < 10; j++ {
					if i == 0 && j == 0 {
						continue
					}
					if v := (i*x + j*y) % 10; i+j < ops[v] {
						ops[v] = i + j
					}
				}
			}
			for i, op := range ops {
				if op == 99 && cnts[i] > 0 {
					ans[x][y] = -1
					continue loop
				}
				ans[x][y] += (op - 1) * cnts[i]
			}
		}
	}
	for _, ai := range ans {
		for _, v := range ai {
			Fprint(out, v, " ")
		}
		Fprintln(out)
	}
}

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

package main

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

type node struct {
	val int64
	typ int //0 for 1 add
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	s := ""
	fmt.Fscanf(in, "%s\n", &s)

	for i := 0; i < 10; i++ {
		for j := 0; j < 10; j++ {
			a := make([][]int, 10)
			for k := 0; k < 10; k++ {
				a[k] = make([]int, 10)
				for q := 0; q < 10; q++ {
					a[k][q] = cal(i, j, k, q)
				}
			}
			ans := 0
			for k := 1; k < len(s); k++ {
				from := int(s[k-1] - '0')
				to := int(s[k] - '0')
				if a[from][to] == -1 {
					ans = -1
					break
				}
				ans += a[from][to]
			}
			if ans == -1 {
				fmt.Fprintf(out, "%d", -1)
			} else {
				fmt.Fprintf(out, "%d", ans-len(s)+1)
			}
			if j < 9 {
				fmt.Fprintf(out, " ")
			}
		}
		fmt.Fprintf(out, "\n")
	}
	return
}

func cal(a, b, x, y int) int {
	// x -> y with a and b
	ans := -1
	for i := 0; i < 10; i++ {
		for j := 0; j < 10; j++ {
			if i+j == 0 {
				continue
			}
			if (i*a+j*b+x)%10 == y {
				if ans == -1 {
					ans = i + j
				} else {
					if i+j < ans {
						ans = i + j
					}
				}
			}
		}
	}
	return ans
}

package main

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

/* solution */

const inf = 1000000000

var d [][][]int

func solve() {

	d = make([][][]int, 100)

	// step 1 (initialize)

	for c := 0; c < 100; c++ {

		d[c] = make([][]int, 10)

		for x := 0; x < 10; x++ {

			d[c][x] = make([]int, 10)

			for y := 0; y < 10; y++ {
				d[c][x][y] = inf
			}

			for y := 0; y < 10; y++ {

				if y < x {
					d[c][x][y] = d[c][y][x]
					continue
				}

				if c == 0 {
					d[c][x][y] = 0
					continue
				}

				cd := gcd(x, y)
				if cd == 0 {
					d[c][x][y] = inf
					continue
				}

				diff := c
				for diff < 100 && diff%cd != 0 {
					diff *= 10
				}

				if diff >= 100 {
					d[c][x][y] = inf
				} else {
					if c-x < 0 && c-y < 0 {
						d[c][x][y] = inf
					} else if c-x < 0 {
						d[c][x][y] = d[c-y][x][y] + 1
					} else if c-y < 0 {
						d[c][x][y] = d[c-x][x][y] + 1
					} else {
						if d[c-y][x][y] > d[c-x][x][y] {
							d[c][x][y] = d[c-x][x][y] + 1
						} else {
							d[c][x][y] = d[c-y][x][y] + 1
						}
					}
				}

			}

		}

	}

	// step 2 (compress)

	for c := 0; c < 10; c++ {
		diff := c
		skip := 0
		if diff == 0 {
			diff = 10
			skip = 1
		}
		for x := 0; x < 10; x++ {
			for y := 0; y < 10; y++ {
				min := d[diff][x][y]
				for j := 1; j < 10-skip; j++ {
					if d[diff+10*j][x][y] < min {
						min = d[diff+10*j][x][y]
					}
				}
				d[c][x][y] = min
			}
		}
	}

	// step 3 (output)

	s := read()

	matrix := make([][]int, 10)

	for x := 0; x < 10; x++ {

		matrix[x] = make([]int, 10)

		for y := 0; y < 10; y++ {

			ans := 0

			if y < x {

				ans = matrix[y][x]

			} else {

				cur := int(s[0] - '0')
				for i := 1; ans < inf && i < len(s); i++ {

					next := int(s[i] - '0')
					diff := (next - cur + 10) % 10

					ans += d[diff][x][y]

					cur = next

				}

				matrix[x][y] = ans

			}

			if ans >= inf {
				printInt(-1)
			} else {
				printInt(ans - len(s) + 1)
			}

		}

		println("")

	}

}

func gcd(a, b int) int {
	if a > b {
		a, b = b, a
	}
	if a == 1 {
		return int(1)
	}
	if a == 0 {
		return b
	}
	return gcd(a, b%a)
}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOROOT() == "C:\\Soft\\Go" {
		f, _ = os.Open("input.txt")
		defer f.Close()
	} else {
		f = os.Stdin
	}

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

	reader = bufio.NewScanner(f)

	const maxCapacity = 4 * 1024 * 1024
	buf := make([]byte, maxCapacity)
	reader.Buffer(buf, maxCapacity)

	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func read() string {
	reader.Scan()
	return reader.Text()
}

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

func print(s string) {
	writer.WriteString(s)
	writer.WriteString(" ")
}

func println(s string) {
	if len(s) > 0 {
		writer.WriteString(s)
	}
	writer.WriteString("\n")
}

func printInt(i int) {
	print(strconv.Itoa(i))
}

func printLnInt(i int) {
	println(strconv.Itoa(i))
}

func readInt64() int64 {
	reader.Scan()
	ret, _ := strconv.ParseInt(reader.Text(), 10, 64)
	return ret
}

package main

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

/* solution */

const inf = 1000000

var d [][][]int

func solve() {

	d = make([][][]int, 100)

	for c := 0; c < 100; c++ {

		d[c] = make([][]int, 10)

		for x := 0; x < 10; x++ {

			d[c][x] = make([]int, 10)

			for y := 0; y < 10; y++ {
				d[c][x][y] = inf
			}

			for y := 0; y < 10; y++ {

				if c == 0 {
					d[c][x][y] = 0
					continue
				}

				cd := gcd(x, y)
				if cd == 0 {
					d[c][x][y] = inf
					continue
				}

				diff := c
				for diff < 100 && diff%cd != 0 {
					diff *= 10
				}

				if diff >= 100 {
					d[c][x][y] = inf
				} else {
					if c-x < 0 && c-y < 0 {
						d[c][x][y] = inf
					} else if c-x < 0 {
						d[c][x][y] = d[c-y][x][y] + 1
					} else if c-y < 0 {
						d[c][x][y] = d[c-x][x][y] + 1
					} else {
						if d[c-y][x][y] > d[c-x][x][y] {
							d[c][x][y] = d[c-x][x][y] + 1
						} else {
							d[c][x][y] = d[c-y][x][y] + 1
						}
					}
				}

			}

		}

	}

	s := read()

	for x := 0; x < 10; x++ {

		for y := 0; y < 10; y++ {

			ans := int64(0)

			cur := int(s[0] - '0')
			for i := 1; ans < inf && i < len(s); i++ {

				next := int(s[i] - '0')
				diff := (next - cur + 10) % 10

				// corner case
				skip := 0
				if diff == 0 {
					diff = 10
					skip = 1
				}

				min := d[diff][x][y]
				for j := 1; j < 10-skip; j++ {
					if d[diff+10*j][x][y] < min {
						min = d[diff+10*j][x][y]
					}
				}

				ans += int64(min)

				cur = next

			}

			if ans >= inf {
				printInt(-1)
			} else {
				printInt64(ans - int64(len(s)+1))
			}

		}

		println("")

	}

}

func gcd(a, b int) int {
	if a > b {
		a, b = b, a
	}
	if a == 1 {
		return int(1)
	}
	if a == 0 {
		return b
	}
	return gcd(a, b%a)
}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOROOT() == "C:\\Soft\\Go" {
		f, _ = os.Open("input.txt")
		defer f.Close()
	} else {
		f = os.Stdin
	}

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

	reader = bufio.NewScanner(f)

	const maxCapacity = 4 * 1024 * 1024
	buf := make([]byte, maxCapacity)
	reader.Buffer(buf, maxCapacity)

	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func read() string {
	reader.Scan()
	return reader.Text()
}

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

func print(s string) {
	writer.WriteString(s)
	writer.WriteString(" ")
}

func println(s string) {
	if len(s) > 0 {
		writer.WriteString(s)
	}
	writer.WriteString("\n")
}

func printInt(i int) {
	print(strconv.Itoa(i))
}

func printInt64(i int64) {
	print(strconv.FormatInt(i, 10))
}

func printLnInt(i int) {
	println(strconv.Itoa(i))
}

func readInt64() int64 {
	reader.Scan()
	ret, _ := strconv.ParseInt(reader.Text(), 10, 64)
	return ret
}

package main

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

/* solution */

const inf = 1000000

var d [][][]int

func solve() {

	d = make([][][]int, 100)

	for c := 0; c < 100; c++ {

		d[c] = make([][]int, 10)

		for x := 0; x < 10; x++ {

			d[c][x] = make([]int, 10)

			for y := 0; y < 10; y++ {
				d[c][x][y] = inf
			}

			for y := 0; y < 10; y++ {

				if c == 0 {
					d[c][x][y] = 0
					continue
				}

				cd := gcd(x, y)
				if cd == 0 {
					d[c][x][y] = inf
					continue
				}

				diff := c
				for diff < 100 && diff%cd != 0 {
					diff *= 10
				}

				if diff >= 100 {
					d[c][x][y] = inf
				} else {
					if c-x < 0 && c-y < 0 {
						d[c][x][y] = inf
					} else if c-x < 0 {
						d[c][x][y] = d[c-y][x][y] + 1
					} else if c-y < 0 {
						d[c][x][y] = d[c-x][x][y] + 1
					} else {
						if d[c-y][x][y] > d[c-x][x][y] {
							d[c][x][y] = d[c-x][x][y] + 1
						} else {
							d[c][x][y] = d[c-y][x][y] + 1
						}
					}
				}

			}

		}

	}

	s := read()

	for x := 0; x < 10; x++ {

		for y := 0; y < 10; y++ {

			ans := int64(0)

			cur := int(s[0] - '0')
			for i := 1; ans < inf && i < len(s); i++ {

				next := int(s[i] - '0')
				diff := (next - cur + 10) % 10

				// corner case
				skip := 0
				if diff == 0 {
					diff = 10
					skip = 1
				}

				min := d[diff][x][y]
				for j := 1; j < 10-skip; j++ {
					if d[diff+10*j][x][y] < min {
						min = d[diff+10*j][x][y]
					}
				}

				ans += int64(min)

				cur = next

			}

			if ans >= inf {
				printInt(-1)
			} else {
				printInt64(ans - int64(len(s)-1))
			}

		}

		println("")

	}

}

func gcd(a, b int) int {
	if a > b {
		a, b = b, a
	}
	if a == 1 {
		return int(1)
	}
	if a == 0 {
		return b
	}
	return gcd(a, b%a)
}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOROOT() == "C:\\Soft\\Go" {
		f, _ = os.Open("input.txt")
		defer f.Close()
	} else {
		f = os.Stdin
	}

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

	reader = bufio.NewScanner(f)

	const maxCapacity = 4 * 1024 * 1024
	buf := make([]byte, maxCapacity)
	reader.Buffer(buf, maxCapacity)

	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func read() string {
	reader.Scan()
	return reader.Text()
}

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

func print(s string) {
	writer.WriteString(s)
	writer.WriteString(" ")
}

func println(s string) {
	if len(s) > 0 {
		writer.WriteString(s)
	}
	writer.WriteString("\n")
}

func printInt(i int) {
	print(strconv.Itoa(i))
}

func printInt64(i int64) {
	print(strconv.FormatInt(i, 10))
}

func printLnInt(i int) {
	println(strconv.Itoa(i))
}

func readInt64() int64 {
	reader.Scan()
	ret, _ := strconv.ParseInt(reader.Text(), 10, 64)
	return ret
}

package main

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

/* solution */

const inf = 36000000

var d [][][]int

func solve() {

	d = make([][][]int, 100)

	// step 1 (initialize)

	for c := 0; c < 100; c++ {

		d[c] = make([][]int, 10)

		for x := 0; x < 10; x++ {

			d[c][x] = make([]int, 10)

			for y := 0; y < 10; y++ {
				d[c][x][y] = inf
			}

			for y := 0; y < 10; y++ {

				if y < x {
					d[c][x][y] = d[c][y][x]
					continue
				}

				if c == 0 {
					d[c][x][y] = 0
					continue
				}

				cd := gcd(x, y)
				if cd == 0 {
					d[c][x][y] = inf
					continue
				}

				diff := c
				for diff < 100 && diff%cd != 0 {
					diff *= 10
				}

				if diff >= 100 {
					d[c][x][y] = inf
				} else {
					if c-x < 0 && c-y < 0 {
						d[c][x][y] = inf
					} else if c-x < 0 {
						d[c][x][y] = d[c-y][x][y] + 1
					} else if c-y < 0 {
						d[c][x][y] = d[c-x][x][y] + 1
					} else {
						if d[c-y][x][y] > d[c-x][x][y] {
							d[c][x][y] = d[c-x][x][y] + 1
						} else {
							d[c][x][y] = d[c-y][x][y] + 1
						}
					}
				}

			}

		}

	}

	// step 2 (compress)

	for c := 0; c < 10; c++ {
		diff := c
		skip := 0
		if diff == 0 {
			diff = 10
			skip = 1
		}
		for x := 0; x < 10; x++ {
			for y := 0; y < 10; y++ {
				min := d[diff][x][y]
				for j := 1; j < 10-skip; j++ {
					if d[diff+10*j][x][y] < min {
						min = d[diff+10*j][x][y]
					}
				}
				d[c][x][y] = min
			}
		}
	}

	// step 3 (output)

	s := read()

	matrix := make([][]int, 10)

	for x := 0; x < 10; x++ {

		matrix[x] = make([]int, 10)

		for y := 0; y < 10; y++ {

			ans := 0

			if y < x {

				ans = matrix[y][x]

			} else {

				cur := int(s[0] - '0')
				for i := 1; ans < inf && i < len(s); i++ {

					next := int(s[i] - '0')
					diff := (next - cur + 10) % 10

					ans += d[diff][x][y]

					cur = next

				}

				matrix[x][y] = ans

			}

			if ans >= inf {
				printInt(-1)
			} else {
				printInt(ans - len(s) + 1)
			}

		}

		println("")

	}

}

func gcd(a, b int) int {
	if a > b {
		a, b = b, a
	}
	if a == 1 {
		return int(1)
	}
	if a == 0 {
		return b
	}
	return gcd(a, b%a)
}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOROOT() == "C:\\Soft\\Go" {
		f, _ = os.Open("input.txt")
		defer f.Close()
	} else {
		f = os.Stdin
	}

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

	reader = bufio.NewScanner(f)

	const maxCapacity = 4 * 1024 * 1024
	buf := make([]byte, maxCapacity)
	reader.Buffer(buf, maxCapacity)

	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func read() string {
	reader.Scan()
	return reader.Text()
}

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

func print(s string) {
	writer.WriteString(s)
	writer.WriteString(" ")
}

func println(s string) {
	if len(s) > 0 {
		writer.WriteString(s)
	}
	writer.WriteString("\n")
}

func printInt(i int) {
	print(strconv.Itoa(i))
}

func printLnInt(i int) {
	println(strconv.Itoa(i))
}

func readInt64() int64 {
	reader.Scan()
	ret, _ := strconv.ParseInt(reader.Text(), 10, 64)
	return ret
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1202B(_r io.Reader, _w io.Writer) {
	s, _ := ioutil.ReadAll(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	cnts := [10]int{}
	for i, c := range s[1:] {
		cnts[(c-s[i]+10)%10]++
	}
	ans := [10][10]int{}
	for x := range ans {
	loop:
		for y := range ans {
			ops := [10]int{}
			for i := range ops {
				ops[i] = 99
			}
			for i := 0; i < 10; i++ {
				for j := 0; j < 10; j++ {
					if i == 0 && j == 0 {
						continue
					}
					if v := (i*x + j*y) % 10; i+j < ops[v] {
						ops[v] = i + j
					}
				}
			}
			for i, op := range ops {
				if op == 99 && cnts[i] > 0 {
					ans[x][y] = -1
					continue loop
				}
				ans[x][y] += (op - 1) * cnts[i]
			}
		}
	}
	for _, ai := range ans {
		for _, v := range ai {
			Fprint(out, v, " ")
		}
		Fprintln(out)
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1202B(_r io.Reader, _w io.Writer) {
	s, _ := ioutil.ReadAll(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	Fprintln(out, s, len(s))

	cnts := [10]int{}
	for i, c := range s[1:] {
		cnts[(c-s[i]+10)%10]++
	}
	ans := [10][10]int{}
	for x := range ans {
	loop:
		for y := range ans {
			ops := [10]int{}
			for i := range ops {
				ops[i] = 99
			}
			for i := 0; i < 10; i++ {
				for j := 0; j < 10; j++ {
					if i == 0 && j == 0 {
						continue
					}
					if v := (i*x + j*y) % 10; i+j < ops[v] {
						ops[v] = i + j
					}
				}
			}
			for i, op := range ops {
				if op == 99 && cnts[i] > 0 {
					ans[x][y] = -1
					continue loop
				}
				ans[x][y] += (op - 1) * cnts[i]
			}
		}
	}
	for _, ai := range ans {
		for _, v := range ai {
			Fprint(out, v, " ")
		}
		Fprintln(out)
	}
}

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


You are given an array $$$s$$$ consisting of $$$n$$$ integers.You have to find any array $$$t$$$ of length $$$k$$$ such that you can cut out maximum number of copies of array $$$t$$$ from array $$$s$$$.Cutting out the copy of $$$t$$$ means that for each element $$$t_i$$$ of array $$$t$$$ you have to find $$$t_i$$$ in $$$s$$$ and remove it from $$$s$$$. If for some $$$t_i$$$ you cannot find such element in $$$s$$$, then you cannot cut out one more copy of $$$t$$$. The both arrays can contain duplicate elements.For example, if $$$s = [1, 2, 3, 2, 4, 3, 1]$$$ and $$$k = 3$$$ then one of the possible answers is $$$t = [1, 2, 3]$$$. This array $$$t$$$ can be cut out $$$2$$$ times.   To cut out the first copy of $$$t$$$ you can use the elements $$$[1, \underline{\textbf{2}}, 3, 2, 4, \underline{\textbf{3}}, \underline{\textbf{1}}]$$$ (use the highlighted elements). After cutting out the first copy of $$$t$$$ the array $$$s$$$ can look like $$$[1, 3, 2, 4]$$$.  To cut out the second copy of $$$t$$$ you can use the elements $$$[\underline{\textbf{1}}, \underline{\textbf{3}}, \underline{\textbf{2}}, 4]$$$. After cutting out the second copy of $$$t$$$ the array $$$s$$$ will be $$$[4]$$$. Your task is to find such array $$$t$$$ that you can cut out the copy of $$$t$$$ from $$$s$$$ maximum number of times. If there are multiple answers, you may choose any of them.

The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$s$$$ and the desired number of elements in $$$t$$$, respectively. The second line of the input contains exactly $$$n$$$ integers $$$s_1, s_2, \dots, s_n$$$ ($$$1 \le s_i \le 2 \cdot 10^5$$$).

Print $$$k$$$ integers — the elements of array $$$t$$$ such that you can cut out maximum possible number of copies of this array from $$$s$$$. If there are multiple answers, print any of them. The required array $$$t$$$ can contain duplicate elements. All the elements of $$$t$$$ ($$$t_1, t_2, \dots, t_k$$$) should satisfy the following condition: $$$1 \le t_i \le 2 \cdot 10^5$$$.

NoteThe first example is described in the problem statement.In the second example the only answer is $$$[7, 3, 1, 3]$$$ and any its permutations. It can be shown that you cannot choose any other array such that the maximum number of copies you can cut out would be equal to $$$2$$$.In the third example the array $$$t$$$ can be cut out $$$5$$$ times.

package main

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

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

	var n, k int
	Fscan(in, &n, &k)
	cnt := make([]int, 2e5+1)
	for i := 0; i < n; i++ {
		var v int
		Fscan(in, &v)
		cnt[v]++
	}

	times := sort.Search(n/k+1, func(times int) bool {
		if times <= 1 {
			return false
		}
		// 不能删除 times 次
		left := k
		for _, c := range cnt {
			left -= c / times
			if left <= 0 {
				return false
			}
		}
		return true
	})
	times--
	for v, c := range cnt {
		for c /= times; c > 0; c-- {
			Fprint(out, v, " ")
			k--
			if k == 0 {
				return
			}
		}
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func Sol1077D(reader io.Reader, writer io.Writer) {
	type bsFunc func(int) bool
	reverse := func(f bsFunc) bsFunc {
		return func(i int) bool {
			return !f(i)
		}
	}

	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n, k int
	Fscan(in, &n, &k)
	cnt := make([]int, 2e5+1)
	for i := 0; i < n; i++ {
		var v int
		Fscan(in, &v)
		cnt[v]++
	}

	times := sort.Search(n/k+1, reverse(func(times int) bool {
		// return true if we can cut out `times` times
		if times <= 1 {
			return true
		}
		left := k
		for _, c := range cnt {
			left -= c / times
			if left <= 0 {
				return true
			}
		}
		return false
	}))
	times--
	for v, c := range cnt {
		for c /= times; c > 0; c-- {
			Fprint(out, v, " ")
			k--
			if k == 0 {
				return
			}
		}
	}
}

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

package main

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

const (
	MaxIterations = 200005
)

var n, t int
var a []int
var counts map[int]int
var numElementsPerIteration [MaxIterations]int
var elemCount [MaxIterations]map[int]int
var maxPossibleIterations int

func main() {
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf, "%d %d\n", &n, &t)
	a = make([]int, n)
	counts = make(map[int]int)
	for i := 0; i < n; i++ {
		fmt.Fscanf(inBuf, "%d", &a[i])
		counts[a[i]]++
	}

	for elem := range counts {
		//for count := counts[elem]; count >= 1; count-- {
		//	iterations := counts[elem] / count
		//	if iterations > maxPossibleIterations {
		//		maxPossibleIterations = iterations
		//	}
		//	if elemCount[iterations] == nil {
		//		elemCount[iterations] = make(map[int]int)
		//	}
		//	if _, ok := elemCount[iterations][elem]; !ok {
		//		elemCount[iterations][elem] = count
		//		numElementsPerIteration[iterations] += count
		//	}
		//}
		for iterations := 1; iterations <= counts[elem]; iterations++ {
			if iterations > maxPossibleIterations {
				maxPossibleIterations = iterations
			}
			if elemCount[iterations] == nil {
				elemCount[iterations] = make(map[int]int)
			}
			count := counts[elem] / iterations
			elemCount[iterations][elem] = count
			numElementsPerIteration[iterations] += count
		}
	}

	r := make([]int, 0)
	for i := maxPossibleIterations; i >= 1; i-- {
		if numElementsPerIteration[i] >= t {
			for element := range elemCount[i] {
				for count := 0; count < elemCount[i][element]; count++ {
					if len(r) < t {
						r = append(r, element)
					} else {
						break
					}
				}
				if len(r) == t {
					break
				}
			}
		}
	}

	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() } ()
	for i := 0; i < t; i++ {
		fmt.Fprintf(outBuf, "%d ", r[i])
	}
}

package main

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

const (
	MaxIterations = 200005
)

var n, t int
var a []int
var counts map[int]int
var numElementsPerIteration [MaxIterations]int
var elemCount [MaxIterations]map[int]int
var maxPossibleIterations int

func main() {
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf, "%d %d\n", &n, &t)
	a = make([]int, n)
	counts = make(map[int]int)
	for i := 0; i < n; i++ {
		fmt.Fscanf(inBuf, "%d", &a[i])
		counts[a[i]]++
	}

	for elem := range counts {
		for iterations := 1; iterations <= counts[elem]; iterations++ {
			if iterations > maxPossibleIterations {
				maxPossibleIterations = iterations
			}
			if elemCount[iterations] == nil {
				elemCount[iterations] = make(map[int]int)
			}
			count := counts[elem] / iterations
			elemCount[iterations][elem] = count
			numElementsPerIteration[iterations] += count
		}
	}

	r := make([]int, 0)
	for i := maxPossibleIterations; i >= 1; i-- {
		if numElementsPerIteration[i] >= t {
			for element := range elemCount[i] {
				for count := 0; count < elemCount[i][element]; count++ {
					if len(r) < t {
						r = append(r, element)
					} else {
						break
					}
				}
				if len(r) == t {
					break
				}
			}
		}
	}

	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() } ()
	for i := 0; i < t; i++ {
		fmt.Fprintf(outBuf, "%d ", r[i])
	}
}

package main

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

const (
	MaxIterations = 200005
)

type pair struct {
	elem, count int
}

var n, t int
var a []int
var counts map[int]int
var numElementsPerIteration [MaxIterations]int
var elemCount [MaxIterations]map[int]int
var maxPossibleIterations int

func main() {
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf, "%d %d\n", &n, &t)
	a = make([]int, n)
	counts = make(map[int]int)
	for i := 0; i < n; i++ {
		fmt.Fscanf(inBuf, "%d", &a[i])
		counts[a[i]]++
	}

	for elem := range counts {
		for count := 1; count <= counts[elem]; count++ {
			iterations := counts[elem] / count
			if iterations > maxPossibleIterations {
				maxPossibleIterations = iterations
			}
			if elemCount[iterations] == nil {
				elemCount[iterations] = make(map[int]int)
			}
			if _, ok := elemCount[iterations][elem]; !ok {
				elemCount[iterations][elem] = count
				numElementsPerIteration[iterations] += count
			}
		}
	}

	r := make([]int, 0)
	for i := maxPossibleIterations; i >= 1; i-- {
		if numElementsPerIteration[i] >= t {
			for element := range elemCount[i] {
				for count := 0; count < elemCount[i][element]; count++ {
					if len(r) < t {
						r = append(r, element)
					} else {
						break
					}
				}
				if len(r) == t {
					break
				}
			}
		}
	}

	if len(r) == 0 {
		r = a
	}

	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() } ()
	for i := 0; i < t; i++ {
		fmt.Fprintf(outBuf, "%d ", r[i])
	}
}

package main

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

const (
	MaxIterations = 200005
)

type pair struct {
	elem, count int
}

var n, t int
var a []int
var counts map[int]int
var numElementsPerIteration [MaxIterations]int
var elemCount [MaxIterations]map[int]int
var maxPossibleIterations int

func main() {
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf, "%d %d\n", &n, &t)
	a = make([]int, n)
	counts = make(map[int]int)
	for i := 0; i < n; i++ {
		fmt.Fscanf(inBuf, "%d", &a[i])
		counts[a[i]]++
	}

	for elem := range counts {
		for count := counts[elem]; count >= 1; count-- {
			iterations := counts[elem] / count
			if iterations > maxPossibleIterations {
				maxPossibleIterations = iterations
			}
			if elemCount[iterations] == nil {
				elemCount[iterations] = make(map[int]int)
			}
			if _, ok := elemCount[iterations][elem]; !ok {
				elemCount[iterations][elem] = count
				numElementsPerIteration[iterations] += count
			}
		}
		for i := counts[elem] / 2 + 1; i <= counts[elem]; i++ {
			if elemCount[i] == nil {
				elemCount[i] = make(map[int]int)
			}
			if _, ok := elemCount[i][elem]; !ok {
				elemCount[i][elem] = 1
				numElementsPerIteration[i] += 1
			}
		}
	}

	r := make([]int, 0)

	for i := maxPossibleIterations; i >= 1; i-- {
		if numElementsPerIteration[i] >= t {
			for element := range elemCount[i] {
				for count := 0; count < elemCount[i][element]; count++ {
					if len(r) < t {
						r = append(r, element)
					} else {
						break
					}
				}
				if len(r) == t {
					break
				}
			}
		}
	}

	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() } ()
	for i := 0; i < t; i++ {
		fmt.Fprintf(outBuf, "%d ", r[i])
	}
}

package main

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

const (
	MaxIterations = 200005
)

type pair struct {
	elem, count int
}

var n, t int
var a []int
var counts map[int]int
var numElementsPerIteration [MaxIterations]int
var elemCount [MaxIterations]map[int]int
var maxPossibleIterations int

func main() {
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf, "%d %d\n", &n, &t)
	a = make([]int, n)
	counts = make(map[int]int)
	for i := 0; i < n; i++ {
		fmt.Fscanf(inBuf, "%d", &a[i])
		counts[a[i]]++
	}

	for elem := range counts {
		for count := 1; count <= counts[elem]; count++ {
			iterations := counts[elem] / count
			if iterations > maxPossibleIterations {
				maxPossibleIterations = iterations
			}
			if elemCount[iterations] == nil {
				elemCount[iterations] = make(map[int]int)
			}
			if _, ok := elemCount[iterations][elem]; !ok {
				elemCount[iterations][elem] = count
				numElementsPerIteration[iterations] += count
			}
		}
	}

	r := make([]int, 0)
	for i := maxPossibleIterations; i >= 1; i-- {
		if numElementsPerIteration[i] >= t {
			for element := range elemCount[i] {
				for count := 0; count < elemCount[i][element]; count++ {
					if len(r) < t {
						r = append(r, element)
					} else {
						break
					}
				}
				if len(r) == t {
					break
				}
			}
		}
	}

	if len(r) == 0 {
		r = a
	}

	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() } ()
	fmt.Print(len(r))
	for i := 0; i < t; i++ {
		fmt.Fprintf(outBuf, "%d ", r[i])
	}
}

package main

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

const (
	MaxIterations = 200005
)

type pair struct {
	elem, count int
}

var n, t int
var a []int
var counts map[int]int
var numElementsPerIteration [MaxIterations]int
var elemCount [MaxIterations]map[int]int
var maxPossibleIterations int

func main() {
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf, "%d %d\n", &n, &t)
	a = make([]int, n)
	counts = make(map[int]int)
	for i := 0; i < n; i++ {
		fmt.Fscanf(inBuf, "%d", &a[i])
		counts[a[i]]++
	}

	for elem := range counts {
		for count := counts[elem]; count >= 1; count-- {
			iterations := counts[elem] / count
			if iterations > maxPossibleIterations {
				maxPossibleIterations = iterations
			}
			if elemCount[iterations] == nil {
				elemCount[iterations] = make(map[int]int)
			}
			if _, ok := elemCount[iterations][elem]; !ok {
				elemCount[iterations][elem] = count
				numElementsPerIteration[iterations] += count
			}
		}
	}

	r := make([]int, 0)
	for i := maxPossibleIterations; i >= 1; i-- {
		if numElementsPerIteration[i] >= t {
			for element := range elemCount[i] {
				for count := 0; count < elemCount[i][element]; count++ {
					if len(r) < t {
						r = append(r, element)
					} else {
						break
					}
				}
				if len(r) == t {
					break
				}
			}
		}
	}

	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() } ()
	for i := 0; i < t; i++ {
		fmt.Fprintf(outBuf, "%d ", r[i])
	}
}