Polycarp found on the street an array $$$a$$$ of $$$n$$$ elements.Polycarp invented his criterion for the beauty of an array. He calls an array $$$a$$$ beautiful if at least one of the following conditions must be met for each different pair of indices $$$i \ne j$$$:   $$$a_i$$$ is divisible by $$$a_j$$$;  or $$$a_j$$$ is divisible by $$$a_i$$$. For example, if:   $$$n=5$$$ and $$$a=[7, 9, 3, 14, 63]$$$, then the $$$a$$$ array is not beautiful (for $$$i=4$$$ and $$$j=2$$$, none of the conditions above is met);  $$$n=3$$$ and $$$a=[2, 14, 42]$$$, then the $$$a$$$ array is beautiful;  $$$n=4$$$ and $$$a=[45, 9, 3, 18]$$$, then the $$$a$$$ array is not beautiful (for $$$i=1$$$ and $$$j=4$$$ none of the conditions above is met); Ugly arrays upset Polycarp, so he wants to remove some elements from the array $$$a$$$ so that it becomes beautiful. Help Polycarp determine the smallest number of elements to remove to make the array $$$a$$$ beautiful.

The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ numbers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 2 \cdot 10^5$$$) — elements of the array $$$a$$$.

For each test case output one integer — the minimum number of elements that must be removed to make the array $$$a$$$ beautiful.

NoteIn the first test case, removing $$$7$$$ and $$$14$$$ will make array $$$a$$$ beautiful.In the second test case, the array $$$a$$$ is already beautiful.In the third test case, removing one of the elements $$$45$$$ or $$$18$$$ will make the array $$$a$$$ beautiful.In the fourth test case, the array $$$a$$$ is beautiful.

package main

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

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

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

func redirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

func main() {
	defer writer.Flush()
// 	redirectInputToFile(reader, "input.txt")

	T := 1
	scan(&T)
	for t := 0; t < T; t++ {
		solve()
	}
}

func solve() {
	var n int
	scan(&n)
	var cnt = make([]int, 2e5+1)
	for i := 0; i < n; i++ {
		var x int
		scan(&x)
		cnt[x]++
	}
	var dp = make([]int, 2e5+1)
	for i := int(2e5); i >= 1; i-- {
		if cnt[i] == 0 {
			continue
		}
		for j := i; j <= 2e5; j += i {
			if cnt[j] == 0 {
				continue
			}
			dp[i] = max(dp[i], dp[j]+cnt[i])
		}
	}
	printf("%v\n", n-max(dp...))
}

func max(s ...int) int {
	r := s[0]
	for _, i := range s {
		if r < i {
			r = i
		}
	}
	return r
}

package main

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

func UniqueInts(v []int) []int {
	prev := int(0)
	if len(v) > 0 && v[0] == prev {
		prev = -1
	}

	i := 0
	for j := range v {
		if v[j] != prev {
			prev = v[j]
			v[i], v[j] = v[j], v[i]
			i++
		}
	}

	return v[:i]
}

func solve(a []int) {
	n := len(a)
	sort.Slice(a, func(i, j int) bool { return a[i] < a[j] })

	cnt := make([]int, a[n-1]+1)
	for i := range a {
		cnt[a[i]]++
	}

	a = UniqueInts(a)
	prevN := n
	n = len(a)

	dp := make([]int, a[n-1]+1)
	dp[a[n-1]] = cnt[a[n-1]]

	ans := dp[a[n-1]]
	for i := n - 2; i >= 0; i-- {
		dp[a[i]] = cnt[a[i]]
		for j := 2 * a[i]; j <= a[n-1]; j += a[i] {
			if dp[a[i]] < dp[j]+cnt[a[i]] {
				dp[a[i]] = dp[j] + cnt[a[i]]
			}
		}
		if ans < dp[a[i]] {
			ans = dp[a[i]]
		}
	}

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

func main() {
	defer writer.Flush()
	//RedirectInputToFile(reader, "input.txt")

	T := 1
	scan(&T)

	var n int
	var a []int

	for t := 0; t < T; t++ {
		scan(&n)

		a = make([]int, n)
		for i := 0; i < n; i++ {
			scan(&a[i])
		}

		solve(a)
	}
}

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

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

func RedirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

package main

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

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

	tc := readNum(reader)

	var buf bytes.Buffer

	for tc > 0 {
		tc--
		n := readNum(reader)
		A := readNNums(reader, n)
		res := solve(n, A)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}

	fmt.Print(buf.String())
}

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

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

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

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

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

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

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

	return i
}

func solve(n int, A []int) int {
	var mx int
	for i := 0; i < n; i++ {
		mx = max(mx, A[i])
	}

	mx++

	cnt := make([]int, mx)
	dp := make([]int, mx)

	for i := 0; i < n; i++ {
		cnt[A[i]]++
	}
	var best int
	for i := 1; i < mx; i++ {
		dp[i] += cnt[i]
		for j := 2 * i; j < mx; j += i {
			dp[j] = max(dp[j], dp[i])
		}
		best = max(best, dp[i])
	}

	return n - best
}

func solve1(n int, A []int) int {
	sort.Ints(A)
	dp := make([]int, A[n-1]+1)

	var best = 1

	for i := 0; i < n; i++ {
		cur := A[i]
		if i == 0 || A[i] != A[i-1] {
			// find divisors of cur
			for x := 1; x <= cur/x; x++ {
				if cur%x == 0 {
					y := cur / x
					dp[cur] = max(dp[cur], max(dp[y], dp[x])+1)
				}
			}
		} else {
			dp[cur]++
		}
		// if we keep cur
		best = max(best, dp[cur])
	}
	return n - best
}

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

package main

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

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

	tc := readNum(reader)

	var buf bytes.Buffer

	for tc > 0 {
		tc--
		n := readNum(reader)
		A := readNNums(reader, n)
		res := solve(n, A)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}

	fmt.Print(buf.String())
}

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

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

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

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

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

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

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

	return i
}

func solve(n int, A []int) int {
	sort.Ints(A)
	dp := make([]int, A[n-1]+1)

	var best = 1

	for i := 0; i < n; i++ {
		cur := A[i]
		if i == 0 || A[i] != A[i-1] {
			// find divisors of cur
			for x := 1; x <= cur/x; x++ {
				if cur%x == 0 {
					y := cur / x
					dp[cur] = max(dp[cur], max(dp[y], dp[x])+1)
				}
			}
		} else {
			dp[cur]++
		}
		// if we keep cur
		best = max(best, dp[cur])
	}
	return n - best
}

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

package main

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

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

	const mx int = 2e5
	divisors := [mx + 1][]int{}
	for i := 1; i <= mx; i++ {
		for j := i; j <= mx; j += i {
			divisors[j] = append(divisors[j], i)
		}
	}

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

		dp := make([]int, 2e5+1)

		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		sort.Ints(a)
		mx := 0
		for i := 0; i < n; {
			st := i
			v := a[st]
			for ; i < n && a[i] == v; i++ {
			}
			if v == 1 {
				dp[1] = i-st
				mx = max(mx, dp[1])
				continue
			}
			for _, d := range divisors[v] {
				if d == v {
					continue
				}
				dp[v] = max(dp[v], dp[d]+i-st)
				mx = max(mx, dp[v])
			}
		}
		Fprintln(out, n-mx)
	}

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

	_leftData, _ := ioutil.ReadAll(in)
	if _s := strings.TrimSpace(string(_leftData)); _s != "" {
		panic("有未读入的数据：\n" + _s)
	}
}

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

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

package main

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

func UniqueInts(v []int) []int {
	prev := int(0)
	if len(v) > 0 && v[0] == prev {
		prev = -1
	}

	i := 0
	for j := range v {
		if v[j] != prev {
			prev = v[j]
			v[i], v[j] = v[j], v[i]
			i++
		}
	}

	return v[:i]
}

func solve(a []int) {
	n := len(a)
	sort.Slice(a, func(i, j int) bool { return a[i] < a[j] })

	cnt := make([]int, a[n-1]+1)
	for i := range a {
		cnt[a[i]]++
	}

	a = UniqueInts(a)
	prevN := n
	n = len(a)

	dp := make([]int, a[n-1]+1)
	dp[a[n-1]] = cnt[a[n-1]]

	ans := 1
	for i := n - 2; i >= 0; i-- {
		dp[a[i]] = cnt[a[i]]
		for j := 2 * a[i]; j <= a[n-1]; j += a[i] {
			if dp[a[i]] < dp[j]+cnt[a[i]] {
				dp[a[i]] = dp[j] + cnt[a[i]]
			}
		}
		if ans < dp[a[i]] {
			ans = dp[a[i]]
		}
	}

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

func main() {
	defer writer.Flush()
	//RedirectInputToFile(reader, "input.txt")

	T := 1
	scan(&T)

	var n int
	var a []int

	for t := 0; t < T; t++ {
		scan(&n)

		a = make([]int, n)
		for i := 0; i < n; i++ {
			scan(&a[i])
		}

		solve(a)
	}
}

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

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

func RedirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

package main

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

var n int
var a []int
var dp [2e5 + 1]int
var ans int = 1

func solve() {
	dp[a[n-1]] = 1
	for i := n - 2; i >= 0; i-- {
		dp[a[i]] = 1
		for j := a[i]; j <= a[n-1]; j += a[i] {
			if dp[a[i]] < dp[j]+1 {
				dp[a[i]] = dp[j] + 1
			}
		}
		if ans < dp[a[i]] {
			ans = dp[a[i]]
		}
	}
	printf("%d\n", n-ans)
}

func main() {
	defer writer.Flush()
	//RedirectInputToFile(reader, "input.txt")

	T := 1
	scan(&T)

	for t := 0; t < T; t++ {
		scan(&n)

		a = make([]int, n)
		for i := 0; i < n; i++ {
			scan(&a[i])
		}

		solve()
	}
}

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

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

func RedirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

package main

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

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

	const mx int = 2e5
	divisors := [mx + 1][]int{}
	for i := 1; i <= mx; i++ {
		for j := i; j <= mx; j += i {
			divisors[j] = append(divisors[j], i)
		}
	}

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

		dp := make([]int, 2e5+1)

		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		sort.Ints(a)
		mx := 0
		for i := 0; i < n; {
			st := i
			v := a[st]
			for ; i < n && a[i] == v; i++ {
			}
			if v == 1 {
				dp[1] = i-st
				continue
			}
			for _, d := range divisors[v] {
				if d == v {
					continue
				}
				dp[v] = max(dp[v], dp[d]+i-st)
				mx = max(mx, dp[v])
			}
		}
		Fprintln(out, n-mx)
	}

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

	_leftData, _ := ioutil.ReadAll(in)
	if _s := strings.TrimSpace(string(_leftData)); _s != "" {
		panic("有未读入的数据：\n" + _s)
	}
}

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

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

package main

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

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

	const mx int = 2e5
	divisors := [mx + 1][]int{}
	for i := 1; i <= mx; i++ {
		for j := i; j <= mx; j += i {
			divisors[j] = append(divisors[j], i)
		}
	}

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

		dp := make([]int, 2e5+1)

		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		sort.Ints(a)
		mx := 0
		for i := 0; i < n; {
			st := i
			v := a[st]
			for ; i < n && a[i] == v; i++ {
			}
			for _, d := range divisors[v] {
				if d == v {
					continue
				}
				dp[v] = max(dp[v], dp[d]+i-st)
				mx = max(mx, dp[v])
			}
		}
		Fprintln(out, n-mx)
	}

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

	_leftData, _ := ioutil.ReadAll(in)
	if _s := strings.TrimSpace(string(_leftData)); _s != "" {
		panic("有未读入的数据：\n" + _s)
	}
}

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

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


This is the hard version of the problem. The only difference is maximum value of $$$a_i$$$.Once in Kostomuksha Divan found an array $$$a$$$ consisting of positive integers. Now he wants to reorder the elements of $$$a$$$ to maximize the value of the following function: $$$$$$\sum_{i=1}^n \operatorname{gcd}(a_1, \, a_2, \, \dots, \, a_i),$$$$$$ where $$$\operatorname{gcd}(x_1, x_2, \ldots, x_k)$$$ denotes the greatest common divisor of integers $$$x_1, x_2, \ldots, x_k$$$, and $$$\operatorname{gcd}(x) = x$$$ for any integer $$$x$$$.Reordering elements of an array means changing the order of elements in the array arbitrary, or leaving the initial order.Of course, Divan can solve this problem. However, he found it interesting, so he decided to share it with you.

The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_{1}, \, a_{2}, \, \dots, \, a_{n}$$$ ($$$1 \le a_{i} \le 2 \cdot 10^7$$$) — the array $$$a$$$.

Output the maximum value of the function that you can get by reordering elements of the array $$$a$$$.

NoteIn the first example, it's optimal to rearrange the elements of the given array in the following order: $$$[6, \, 2, \, 2, \, 2, \, 3, \, 1]$$$:$$$$$$\operatorname{gcd}(a_1) + \operatorname{gcd}(a_1, \, a_2) + \operatorname{gcd}(a_1, \, a_2, \, a_3) + \operatorname{gcd}(a_1, \, a_2, \, a_3, \, a_4) + \operatorname{gcd}(a_1, \, a_2, \, a_3, \, a_4, \, a_5) + \operatorname{gcd}(a_1, \, a_2, \, a_3, \, a_4, \, a_5, \, a_6) = 6 + 2 + 2 + 2 + 1 + 1 = 14.$$$$$$ It can be shown that it is impossible to get a better answer.In the second example, it's optimal to rearrange the elements of a given array in the following order: $$$[100, \, 10, \, 10, \, 5, \, 1, \, 3, \, 3, \, 7, \, 42, \, 54]$$$.

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	const mx int = 2e7
	lpf := [mx + 1]int{}
	primes := make([]int, 0, 1270607)
	for i := 2; i <= mx; i++ {
		if lpf[i] == 0 {
			lpf[i] = i
			primes = append(primes, i)
		}
		for _, p := range primes {
			if p*i > mx {
				break
			}
			lpf[p*i] = p
			if i%p == 0 {
				break
			}
		}
	}
	DS := [1024]int{1}
	cache := map[int][]int{}
	divisors := func(x int) []int {
		if ds := cache[x]; ds != nil {
			return ds
		}
		ds := DS[:1]
		for x := x; x > 1; {
			p := lpf[x]
			e := 1
			for x /= p; lpf[x] == p; x /= p {
				e++
			}
			d := ds
			for pp := p; e > 0; e-- {
				for _, d := range d {
					ds = append(ds, d*pp)
				}
				pp *= p
			}
		}
		cache[x] = append([]int(nil), ds...)
		return ds
	}

	cd := [mx + 1]int{}
	var n, v int
	for Fscan(in, &n); n > 0; n-- {
		Fscan(in, &v)
		for _, d := range divisors(v) {
			cd[d]++
		}
	}

	ans := int64(0)
	f := [mx + 1]int64{}
	for i := 1; i <= mx; i++ {
		if cd[i] == 0 {
			continue
		}
		mx := int64(0)
		for _, d := range divisors(i) {
			mx = max(mx, f[d]-int64(d)*int64(cd[i]))
		}
		f[i] = int64(i)*int64(cd[i]) + mx
		ans = max(ans, f[i])
	}
	Print(ans)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	const mx int = 2e7
	lpf := [mx + 1]int{}
	primes := make([]int, 0, 1270607)
	for i := 2; i <= mx; i++ {
		if lpf[i] == 0 {
			lpf[i] = i
			primes = append(primes, i)
		}
		for _, p := range primes {
			if p*i > mx {
				break
			}
			lpf[p*i] = p
			if i%p == 0 {
				break
			}
		}
	}
	DS := [1024]int{1}
	divisors := func(x int) []int {
		ds := DS[:1]
		for x > 1 {
			p := lpf[x]
			e := 1
			for x /= p; lpf[x] == p; x /= p {
				e++
			}
			d := ds
			for pp := p; e > 0; e-- {
				for _, d := range d {
					ds = append(ds, d*pp)
				}
				pp *= p
			}
		}
		return ds
	}

	cd := [mx + 1]int{}
	var n int
	Fscan(in, &n)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
		for _, d := range divisors(a[i]) {
			cd[d]++
		}
	}

	dp := [mx + 1]int64{}
	for i := range dp {
		dp[i] = -1
	}
	var f func(x int) int64
	f = func(x int) int64 {
		if dp[x] != -1 {
			return dp[x]
		}
		mx := int64(0)
		ds := divisors(x)
		for _, d := range ds[:len(ds)-1] {
			mx = max(mx, f(d)-int64(d)*int64(cd[x]))
		}
		dp[x] = int64(x)*int64(cd[x]) + mx
		return dp[x]
	}
	ans := int64(0)
	for _, v := range a {
		ans = max(ans, f(v))
	}
	Print(ans)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	const mx int = 2e7
	lpf := [mx + 1]int{}
	primes := make([]int, 0, 1270607)
	for i := 2; i <= mx; i++ {
		if lpf[i] == 0 {
			lpf[i] = i
			primes = append(primes, i)
		}
		for _, p := range primes {
			if p*i > mx {
				break
			}
			lpf[p*i] = p
			if i%p == 0 {
				break
			}
		}
	}
	DS := [1024]int{1}
	divisors := func(x int) []int {
		ds := DS[:1]
		for x > 1 {
			p := lpf[x]
			e := 1
			for x /= p; x%p == 0; x /= p {
				e++
			}
			d := ds
			for pp := p; e > 0; e-- {
				for _, d := range d {
					ds = append(ds, d*pp)
				}
				pp *= p
			}
		}
		return ds
	}

	cd := [mx + 1]int{}
	var n, v int
	for Fscan(in, &n); n > 0; n-- {
		Fscan(in, &v)
		for _, d := range divisors(v) {
			cd[d]++
		}
	}

	ans := int64(0)
	f := [mx + 1]int64{}
	for i := 1; i <= mx; i++ {
		if cd[i] == 0 {
			continue
		}
		mx := int64(0)
		for _, d := range divisors(i) {
			mx = max(mx, f[d]-int64(d)*int64(cd[i]))
		}
		f[i] = int64(i)*int64(cd[i]) + mx
		ans = max(ans, f[i])
	}
	Print(ans)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	const mx int = 2e7
	lpf := [mx + 1]int{}
	primes := make([]int, 0, 1270607)
	for i := 2; i <= mx; i++ {
		if lpf[i] == 0 {
			lpf[i] = i
			primes = append(primes, i)
		}
		for _, p := range primes {
			if p*i > mx {
				break
			}
			lpf[p*i] = p
			if i%p == 0 {
				break
			}
		}
	}
	DS := [1024]int{1}
	divisors := func(x int) []int {
		ds := DS[:1]
		for x > 1 {
			p := lpf[x]
			e := 1
			for x /= p; lpf[x] == p; x /= p {
				e++
			}
			d := ds
			for pp := p; e > 0; e-- {
				for _, d := range d {
					ds = append(ds, d*pp)
				}
				pp *= p
			}
		}
		return ds
	}

	cd := [mx + 1]int{}
	var n, v int
	for Fscan(in, &n); n > 0; n-- {
		Fscan(in, &v)
		for _, d := range divisors(v) {
			cd[d]++
		}
	}

	ans := int64(0)
	f := [mx + 1]int64{}
	for i := 1; i <= mx; i++ {
		if cd[i] == 0 {
			continue
		}
		mx := int64(0)
		for _, d := range divisors(i) {
			mx = max(mx, f[d]-int64(d)*int64(cd[i]))
		}
		f[i] = int64(i)*int64(cd[i]) + mx
		ans = max(ans, f[i])
	}
	Print(ans)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	const mx int = 2e7
	lpf := [mx + 1]int{}
	primes := make([]int, 0, 1270607)
	for i := 2; i <= mx; i++ {
		if lpf[i] == 0 {
			lpf[i] = i
			primes = append(primes, i)
		}
		for _, p := range primes {
			if p*i > mx {
				break
			}
			lpf[p*i] = p
			if i%p == 0 {
				break
			}
		}
	}
	divisors := func(x int) []int {
		ds := []int{1}
		for x > 1 {
			p := lpf[x]
			e := 1
			for x /= p; lpf[x] == p; x /= p {
				e++
			}
			d := ds
			for pp := p; e > 0; e-- {
				for _, d := range d {
					ds = append(ds, d*pp)
				}
				pp *= p
			}
		}
		return ds
	}

	cd := [mx + 1]int{}
	var n, v int
	for Fscan(in, &n); n > 0; n-- {
		Fscan(in, &v)
		for _, d := range divisors(v) {
			cd[d]++
		}
	}

	ans := int64(0)
	f := [mx + 1]int64{}
	for i := 1; i <= mx; i++ {
		if cd[i] == 0 {
			continue
		}
		mx := int64(0)
		for _, d := range divisors(i) {
			mx = max(mx, f[d]-int64(d)*int64(cd[i]))
		}
		f[i] = int64(i)*int64(cd[i]) + mx
		ans = max(ans, f[i])
	}
	Print(ans)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	const mx int = 2e7
	lpf := [mx + 1]int{}
	for i := 2; i <= mx; i++ {
		if lpf[i] == 0 {
			for j := i; j <= mx; j += i {
				if lpf[j] == 0 {
					lpf[j] = i
				}
			}
		}
	}
	divisors := func(x int) []int {
		ds := []int{1}
		for x > 1 {
			p := lpf[x]
			e := 1
			for x /= p; lpf[x] == p; x /= p {
				e++
			}
			d := ds
			for pp := p; e > 0; e-- {
				for _, d := range d {
					ds = append(ds, d*pp)
				}
				pp *= p
			}
		}
		return ds
	}

	var n, v int
	Fscan(in, &n)
	cd := [mx + 1]int{}
	for ; n > 0; n-- {
		Fscan(in, &v)
		for _, d := range divisors(v) {
			cd[d]++
		}
	}

	ans := int64(0)
	f := [mx + 1]int64{}
	for i := 1; i <= mx; i++ {
		if cd[i] == 0 {
			continue
		}
		mx := int64(0)
		for _, d := range divisors(i) {
			mx = max(mx, f[d]-int64(d)*int64(cd[i]))
		}
		f[i] = int64(i)*int64(cd[i]) + mx
		ans = max(ans, f[i])
	}
	Print(ans)
}

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

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()
	//t0 := time.Now()

	const mx int = 2e7
	//ds := [mx + 1][]int{}
	//for i := 2; i <= mx; i++ {
	//	for j := i; j <= mx; j += i {
	//		ds[j] = append(ds[j], i)
	//	}
	//}
	//Println(time.Since(t0))
	var n, v int
	Fscan(in, &n)
	cd := [mx + 1]int{}
	for i := 0; i < n; i++ {
		Fscan(in, &v)
		//for _, d := range ds[v] {
		//	cnt[d]++
		//}
		for d := 1; d*d <= v; d++ {
			if v%d == 0 {
				cd[d]++
				if d*d < v {
					cd[v/d]++
				}
			}
		}
	}
	//Println(time.Since(t0))

	ans := int64(0)
	f := [mx + 1]int64{}
	for i := 1; i <= mx; i++ {
		if cd[i] == 0 {
			continue
		}
		//mx := f[1] - int64(cd[i])
		mx := int64(0)
		for d := 1; d*d <= i; d++ {
			if i%d == 0 {
				mx = max(mx, f[d]-int64(d)*int64(cd[i]))
				if d*d < i {
					d := i / d
					mx = max(mx, f[d]-int64(d)*int64(cd[i]))
				}
			}
		}
		//for j := 0; j < len(dd)-1; j++ {
		//	d := dd[j]
		//	mx = max(mx, f[d]-int64(d)*int64(cnt[i]))
		//}
		f[i] = int64(i)*int64(cd[i]) + mx
		ans = max(ans, f[i])
	}
	//Println(time.Since(t0))

	Fprintln(out, ans)
}

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

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

package main

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

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

const MAX_VALUE = int(2e7 + 3)

var smallesDiv [MAX_VALUE]int
var cnt [MAX_VALUE]int
var dp [MAX_VALUE]int64

func runSieve() {
	var i, j int
	for i = 2; i*i < MAX_VALUE; i += 1 {
		if smallesDiv[i] != 0 {
			continue
		}
		smallesDiv[i] = i
		for j = i * i; j < MAX_VALUE; j += i {
			if smallesDiv[j] == 0 {
				smallesDiv[j] = i
			}
		}
	}
}

var divisors []int = make([]int, 1000)
var nDivisors int

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

func getDivisors(n int) {
	if n == 1 {
		divisors[0] = 1
		nDivisors = 1
		return
	}
	var p = smallesDiv[n]
	if p == 0 {
		divisors[0] = 1
		divisors[1] = n
		nDivisors = 2
		return
	}
	var e = 0
	for ; n%p == 0; n /= p {
		e++
	}
	getDivisors(n)
	var pp = 1
	for i := 1; i <= e; i++ {
		pp = pp * p
		for j, jj := nDivisors*i, 0; jj < nDivisors; j++ {
			divisors[j] = divisors[jj] * pp
			jj++
		}
	}
	nDivisors *= e + 1
}

func main() {
	runSieve()
	var n, a, maxA int
	fmt.Fscan(in, &n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &a)
		if a > maxA {
			maxA = a
		}
		getDivisors(a)
		for _, j := range divisors[:nDivisors] {
			cnt[j]++
		}
	}

	var ans int64 = int64(n)
	for i := 1; i <= maxA; i++ {
		if cnt[i] == 0 {
			continue
		}
		var r int64 = 0
		getDivisors(i)
		for _, j := range divisors[:nDivisors] {
			r = max(r, dp[j]+int64(j)*int64(cnt[j]-cnt[i]))
		}

		var ansi = r + int64(cnt[i])*int64(i)
		ans = max(ans, ansi)
		dp[i] = r
	}
	fmt.Fprintf(out, "%d\n", ans)
	out.Flush()
}

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	const mx int = 2e7
	lpf := [mx + 1]int{}
	primes := make([]int, 0, 1270607)
	for i := 2; i <= mx; i++ {
		if lpf[i] == 0 {
			lpf[i] = i
			primes = append(primes, i)
		}
		for _, p := range primes {
			if p*i > mx {
				break
			}
			lpf[p*i] = p
			if i%p == 0 {
				break
			}
		}
	}
	DS := [1024]int{1}
	cache := map[int][]int{}
	divisors := func(x int) []int {
		if ds := cache[x]; ds != nil {
			return ds
		}
		ds := DS[:1]
		for x > 1 {
			p := lpf[x]
			e := 1
			for x /= p; lpf[x] == p; x /= p {
				e++
			}
			d := ds
			for pp := p; e > 0; e-- {
				for _, d := range d {
					ds = append(ds, d*pp)
				}
				pp *= p
			}
		}
		cache[x] = append([]int(nil), ds...)
		return ds
	}

	cd := [mx + 1]int{}
	var n, v int
	for Fscan(in, &n); n > 0; n-- {
		Fscan(in, &v)
		for _, d := range divisors(v) {
			cd[d]++
		}
	}

	ans := int64(0)
	f := [mx + 1]int64{}
	for i := 1; i <= mx; i++ {
		if cd[i] == 0 {
			continue
		}
		mx := int64(0)
		for _, d := range divisors(i) {
			mx = max(mx, f[d]-int64(d)*int64(cd[i]))
		}
		f[i] = int64(i)*int64(cd[i]) + mx
		ans = max(ans, f[i])
	}
	Print(ans)
}

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


There are $$$n$$$ cities in Berland. Some pairs of cities are connected by roads. All roads are bidirectional. Each road connects two different cities. There is at most one road between a pair of cities. The cities are numbered from $$$1$$$ to $$$n$$$.It is known that, from the capital (the city with the number $$$1$$$), you can reach any other city by moving along the roads.The President of Berland plans to improve the country's road network. The budget is enough to repair exactly $$$n-1$$$ roads. The President plans to choose a set of $$$n-1$$$ roads such that:  it is possible to travel from the capital to any other city along the $$$n-1$$$ chosen roads,  if $$$d_i$$$ is the number of roads needed to travel from the capital to city $$$i$$$, moving only along the $$$n-1$$$ chosen roads, then $$$d_1 + d_2 + \dots + d_n$$$ is minimized (i.e. as minimal as possible). In other words, the set of $$$n-1$$$ roads should preserve the connectivity of the country, and the sum of distances from city $$$1$$$ to all cities should be minimized (where you can only use the $$$n-1$$$ chosen roads).The president instructed the ministry to prepare $$$k$$$ possible options to choose $$$n-1$$$ roads so that both conditions above are met.Write a program that will find $$$k$$$ possible ways to choose roads for repair. If there are fewer than $$$k$$$ ways, then the program should output all possible valid ways to choose roads.

The first line of the input contains integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 2\cdot10^5, n-1 \le m \le 2\cdot10^5, 1 \le k \le 2\cdot10^5$$$), where $$$n$$$ is the number of cities in the country, $$$m$$$ is the number of roads and $$$k$$$ is the number of options to choose a set of roads for repair. It is guaranteed that $$$m \cdot k \le 10^6$$$. The following $$$m$$$ lines describe the roads, one road per line. Each line contains two integers $$$a_i$$$, $$$b_i$$$ ($$$1 \le a_i, b_i \le n$$$, $$$a_i \ne b_i$$$) — the numbers of the cities that the $$$i$$$-th road connects. There is at most one road between a pair of cities. The given set of roads is such that you can reach any city from the capital.

Print $$$t$$$ ($$$1 \le t \le k$$$) — the number of ways to choose a set of roads for repair. Recall that you need to find $$$k$$$ different options; if there are fewer than $$$k$$$ of them, then you need to find all possible different valid options. In the following $$$t$$$ lines, print the options, one per line. Print an option as a string of $$$m$$$ characters where the $$$j$$$-th character is equal to '1' if the $$$j$$$-th road is included in the option, and is equal to '0' if the road is not included. The roads should be numbered according to their order in the input. The options can be printed in any order. All the $$$t$$$ lines should be different. Since it is guaranteed that $$$m \cdot k \le 10^6$$$, the total length of all the $$$t$$$ lines will not exceed $$$10^6$$$. If there are several answers, output any of them.

package main

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

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

	var n, m, k, v, w int
	Fscan(in, &n, &m, &k)
	edges := make([][2]int, m)
	g := make([][][2]int, n)
	for i := range edges {
		Fscan(in, &v, &w)
		v--
		w--
		edges[i] = [2]int{v, w}
		g[v] = append(g[v], [2]int{w, i})
		g[w] = append(g[w], [2]int{v, i})
	}

	fromEdges := make([][]int, n)
	dep := make([]int, n)
	for i := range dep {
		dep[i] = -1
	}
	dep[0] = 0
	q := []int{0}
	for len(q) > 0 {
		v, q = q[0], q[1:]
		for _, e := range g[v] {
			if w := e[0]; dep[w] < 0 {
				dep[w] = dep[v] + 1
				q = append(q, w)
				fromEdges[w] = append(fromEdges[w], e[1])
			} else if dep[w] == dep[v]+1 {
				fromEdges[w] = append(fromEdges[w], e[1])
			}
		}
	}
	ways := 1
	for _, ids := range fromEdges[1:] {
		ways *= len(ids)
		if ways >= k {
			ways = k
			break
		}
	}
	Fprintln(out, ways)
	ans := make([]byte, m)
	for i := range ans {
		ans[i] = '0'
	}
	var printRoads func(p int)
	printRoads = func(p int) {
		if ways == 0 {
			return
		}
		if p == n {
			Fprintln(out, string(ans))
			ways--
			return
		}
		for _, eid := range fromEdges[p] {
			ans[eid] = '1'
			printRoads(p + 1)
			ans[eid] = '0'
		}
	}
	printRoads(1)
}

package main

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

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

	var n, m, k, v, w int
	Fscan(in, &n, &m, &k)
	edges := make([][2]int, m)
	g := make([][][2]int, n)
	for i := range edges {
		Fscan(in, &v, &w)
		v--
		w--
		edges[i] = [2]int{v, w}
		g[v] = append(g[v], [2]int{w, i})
		g[w] = append(g[w], [2]int{v, i})
	}

	fromEdges := make([][]int, n)
	dep := make([]int, n)
	for i := range dep {
		dep[i] = -1
	}
	dep[0] = 0
	q := []int{0}
	for len(q) > 0 {
		v, q = q[0], q[1:]
		for _, e := range g[v] {
			if w := e[0]; dep[w] < 0 {
				dep[w] = dep[v] + 1
				q = append(q, w)
				fromEdges[w] = append(fromEdges[w], e[1])
			} else if dep[w] == dep[v]+1 {
				fromEdges[w] = append(fromEdges[w], e[1])
			}
		}
	}
	ways := 1
	for _, ids := range fromEdges[1:] {
		ways *= len(ids)
		if ways >= k {
			ways = k
			break
		}
	}
	Fprintln(out, ways)
	ans := make([]byte, m)
	for i := range ans {
		ans[i] = '0'
	}
	var printRoads func(p int)
	printRoads = func(p int) {
		if ways == 0 {
			return
		}
		if p == n {
			Fprintf(out, "%s\n", ans)
			ways--
			return
		}
		for _, eid := range fromEdges[p] {
			ans[eid] = '1'
			printRoads(p + 1)
			ans[eid] = '0'
		}
	}
	printRoads(1)
}

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

package main

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

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

	var n, m, k, v, w int
	Fscan(in, &n, &m, &k)
	edges := make([][2]int, m)
	g := make([][][2]int, n)
	for i := range edges {
		Fscan(in, &v, &w)
		v--
		w--
		edges[i] = [2]int{v, w}
		g[v] = append(g[v], [2]int{w, i})
		g[w] = append(g[w], [2]int{v, i})
	}

	fromEdges := make([][]int, n)
	dep := make([]int, n)
	for i := range dep {
		dep[i] = -1
	}
	dep[0] = 0
	q := []int{0}
	for len(q) > 0 {
		v, q = q[0], q[1:]
		for _, e := range g[v] {
			if w := e[0]; dep[w] < 0 {
				dep[w] = dep[v] + 1
				q = append(q, w)
				fromEdges[w] = append(fromEdges[w], e[1])
			} else if dep[w] == dep[v]+1 {
				fromEdges[w] = append(fromEdges[w], e[1])
			}
		}
	}
	ways := 1
	for _, ids := range fromEdges[1:] {
		ways *= len(ids)
		if ways >= k {
			ways = k
			break
		}
	}
	Fprintln(out, ways)
	ans := make([]byte, m)
	for i := range ans {
		ans[i] = '0'
	}
	var printRoads func(p int)
	printRoads = func(p int) {
		if ways == 0 {
			return
		}
		if p == n {
			Fprintln(out, string(ans))
			ways--
			return
		}
		for _, eid := range fromEdges[p] {
			ans[eid] = '1'
			printRoads(p + 1)
			ans[eid] = '0'
		}
	}
	printRoads(1)
}

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


You are playing a game similar to Sokoban on an infinite number line. The game is discrete, so you only consider integer positions on the line.You start on a position $$$0$$$. There are $$$n$$$ boxes, the $$$i$$$-th box is on a position $$$a_i$$$. All positions of the boxes are distinct. There are also $$$m$$$ special positions, the $$$j$$$-th position is $$$b_j$$$. All the special positions are also distinct.In one move you can go one position to the left or to the right. If there is a box in the direction of your move, then you push the box to the next position in that direction. If the next position is taken by another box, then that box is also pushed to the next position, and so on. You can't go through the boxes. You can't pull the boxes towards you.You are allowed to perform any number of moves (possibly, zero). Your goal is to place as many boxes on special positions as possible. Note that some boxes can be initially placed on special positions.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of testcases. Then descriptions of $$$t$$$ testcases follow. The first line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 2 \cdot 10^5$$$) — the number of boxes and the number of special positions, respectively. The second line of each testcase contains $$$n$$$ distinct integers in the increasing order $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_1 < a_2 < \dots < a_n \le 10^9$$$; $$$a_i \neq 0$$$) — the initial positions of the boxes. The third line of each testcase contains $$$m$$$ distinct integers in the increasing order $$$b_1, b_2, \dots, b_m$$$ ($$$-10^9 \le b_1 < b_2 < \dots < b_m \le 10^9$$$; $$$b_i \neq 0$$$) — the special positions. The sum of $$$n$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. The sum of $$$m$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$.

For each testcase print a single integer — the maximum number of boxes that can be placed on special positions.

NoteIn the first testcase you can go $$$5$$$ to the right: the box on position $$$1$$$ gets pushed to position $$$6$$$ and the box on position $$$5$$$ gets pushed to position $$$7$$$. Then you can go $$$6$$$ to the left to end up on position $$$-1$$$ and push a box to $$$-2$$$. At the end, the boxes are on positions $$$[-2, 6, 7, 11, 15]$$$, respectively. Among them positions $$$[-2, 6, 7, 15]$$$ are special, thus, the answer is $$$4$$$.In the second testcase you can push the box from $$$-1$$$ to $$$-10^9$$$, then the box from $$$1$$$ to $$$10^9$$$ and obtain the answer $$$2$$$.The third testcase showcases that you are not allowed to pull the boxes, thus, you can't bring them closer to special positions.In the fourth testcase all the boxes are already on special positions, so you can do nothing and still obtain the answer $$$3$$$.In the fifth testcase there are fewer special positions than boxes. You can move either $$$8$$$ or $$$9$$$ to the right to have some box on position $$$10$$$.

package main

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

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

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

	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &m)
		a := make([]int, n)
		aPositiveStart := n
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			if a[i] > 0 && aPositiveStart == n {
				aPositiveStart = i
			}
		}
		b := make([]int, m)
		bPositiveStart := m
		for i := 0; i < m; i++ {
			fmt.Fscan(in, &b[i])
			if b[i] > 0 && bPositiveStart == m {
				bPositiveStart = i
			}
		}

		pos, neg := 0, 0
		alreadyFit := make([]bool, m)
		for i, j := 0, 0; j < m; j++ {
			if a[i] < b[j] {
				for ; i < n && a[i] < b[j]; i++ {
				}
				if i == n {
					break
				}
			}
			if a[i] == b[j] {
				if b[j] > 0 {
					pos++
				} else {
					neg++
				}
				alreadyFit[j] = true
			}
		}
		maxPos, maxNeg := pos, neg

		if aPositiveStart < n && bPositiveStart < m {
			trainLen := 0
			next := bPositiveStart
			for i, j := aPositiveStart, bPositiveStart; j < m; j++ {
				if j > bPositiveStart && trainLen > 0 {
					pos--
				}
				for ; i < n && a[i] <= b[j]+trainLen; i++ {
					trainLen++
				}
				for next = max(j, next); next < m && b[next] < b[j]+trainLen; next++ {
					if !alreadyFit[next] {
						pos++
						if pos > maxPos {
							maxPos = pos
						}
					}
				}
			}
		}
		if aPositiveStart > 0 && bPositiveStart > 0 {
			trainLen := 0
			next := bPositiveStart - 1
			for i, j := aPositiveStart-1, bPositiveStart-1; j >= 0; j-- {
				if j < bPositiveStart-1 && trainLen > 0 {
					neg--
				}
				for ; i >= 0 && a[i] >= b[j]-trainLen; i-- {
					trainLen++
				}
				for next = min(j, next); next >= 0 && b[next] > b[j]-trainLen; next-- {
					if !alreadyFit[next] {
						neg++
						if neg > maxNeg {
							maxNeg = neg
						}
					}
				}
			}
		}

		fmt.Println(maxPos + maxNeg)
	}
}

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

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

package main

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

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

	var T, n, m int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &m)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		b := make([]int, m)
		for i := range b {
			Fscan(in, &b[i])
		}
		f := func(a, b []int) int {
			same, i, n := 0, 0, len(a)
			for _, v := range b {
				for i < n && a[i] < v {
					i++
				}
				if i < n && a[i] == v {
					same++
					i++
				}
			}
			res := same
			i, left := 0, 0
			for right, v := range b {
				for i < n && a[i] < v {
					i++
				}
				if i < n && a[i] == v {
					same--
					i++
				}
				for left <= right && v-b[left]+1 > i { // b[left] 到 b[right] 的特殊位置无法被 i 个连续箱子覆盖
					left++
				}
				res = max(res, right-left+1+same)
			}
			return res
		}
		x, y := sort.SearchInts(a, 0), sort.SearchInts(b, 0)
		ans := f(a[x:], b[y:])
		// 负数变正数且递增，方便复用 f
		for i := 0; i < (x+1)/2; i++ {
			a[i], a[x-1-i] = -a[x-1-i], -a[i]
		}
		for i := 0; i < (y+1)/2; i++ {
			b[i], b[y-1-i] = -b[y-1-i], -b[i]
		}
		ans += f(a[:x], b[:y])
		Fprintln(out, ans)
	}
}

func main() { CF1494C(os.Stdin, os.Stdout) }
func max(a, b int) int { if b > a { return b }; return a }

package main

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

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

	var T, n, m int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &m)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		b := make([]int, m)
		for i := range b {
			Fscan(in, &b[i])
		}
		f := func(a, b []int) int {
			same, i, n := 0, 0, len(a)
			for _, v := range b {
				for i < n && a[i] < v {
					i++
				}
				if i < n && a[i] == v {
					same++
				}
			}
			res := same
			i, left := 0, 0
			for right, v := range b {
				for i < n && a[i] < v {
					i++
				}
				if i < n && a[i] == v {
					same--
					i++
				}
				for left <= right && v-b[left]+1 > i { // b[left] 到 b[right] 的特殊位置无法被 i 个连续箱子覆盖
					left++
				}
				res = max(res, right-left+1+same)
			}
			return res
		}
		x, y := sort.SearchInts(a, 0), sort.SearchInts(b, 0)
		ans := f(a[x:], b[y:])
		// 负数变正数且递增，方便复用 f
		for i := 0; i < (x+1)/2; i++ {
			a[i], a[x-1-i] = -a[x-1-i], -a[i]
		}
		for i := 0; i < (y+1)/2; i++ {
			b[i], b[y-1-i] = -b[y-1-i], -b[i]
		}
		ans += f(a[:x], b[:y])
		Fprintln(out, ans)
	}
}

func main() { CF1494C(os.Stdin, os.Stdout) }
func max(a, b int) int { if b > a { return b }; return a }

package main

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

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

	var T, n, m int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &m)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		b := make([]int, m)
		for i := range b {
			Fscan(in, &b[i])
		}
		f := func(a, b []int) int {
			same, i, n := 0, 0, len(a)
			for _, v := range b {
				for i < n && a[i] < v {
					i++
				}
				if i < n && a[i] == v {
					same++
				}
			}
			res := same
			i = 0
			l := 0
			for r, v := range b {
				for i < n && a[i] < v {
					i++
				}
				if i < n && a[i] == v {
					same--
					i++
				}
				for l <= r && v-b[l] >= i {
					l++
				}
				res = max(res, r-l+1+same)
			}
			return res
		}
		x, y := sort.SearchInts(a, 0), sort.SearchInts(b, 0)
		ans := f(a[x:], b[y:])
		for i := 0; i < (x+1)/2; i++ {
			a[i], a[x-1-i] = -a[x-1-i], -a[i]
		}
		for i := 0; i < (y+1)/2; i++ {
			b[i], b[y-1-i] = -b[y-1-i], -b[i]
		}
		ans += f(a[:x], b[:y])
		Fprintln(out, ans)
	}
}

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

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

package main

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

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

	var T, n, m int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &m)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		b := make([]int, m)
		for i := range b {
			Fscan(in, &b[i])
		}
		f := func(a, b []int) (res int) {
			mp := map[int]bool{}
			for _, v := range a {
				mp[v] = true
			}
			same := 0
			for _, v := range b {
				if mp[v] {
					same++
				}
			}
			for i, v := range b {
				if mp[v] {
					same--
				}
				c := sort.SearchInts(a, v+1)
				j := sort.SearchInts(b, v-c+1)
				if i-j+1+same > res {
					res = i - j + 1 + same
				}
			}
			return
		}
		x, y := sort.SearchInts(a, 0), sort.SearchInts(b, 0)
		ans := f(a[x:], b[y:])
		for i := 0; i < (x+1)/2; i++ {
			a[i], a[x-1-i] = -a[x-1-i], -a[i]
		}
		for i := 0; i < (y+1)/2; i++ {
			b[i], b[y-1-i] = -b[y-1-i], -b[i]
		}
		ans += f(a[:x], b[:y])
		Fprintln(out, ans)
	}
}
func main() { CF1494C(os.Stdin, os.Stdout) }

package main

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

// github.com/EndlessCheng/codeforces-go
func f(a, b []int) (mx int) {
	h := map[int]bool{}
	for _, v := range a {
		h[v] = true
	}
	s := 0
	for _, v := range b {
		if h[v] {
			s++
		}
	}
	for i, v := range b {
		if h[v] {
			s--
		}
		j := SearchInts(b, v-SearchInts(a, v+1)+1)
		if i-j+1+s > mx {
			mx = i-j+1+s
		}
	}
	return
}

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

	var T, n, m int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &m)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		b := make([]int, m)
		for i := range b {
			Fscan(in, &b[i])
		}
		x, y := SearchInts(a, 0), SearchInts(b, 0)
		c := f(a[x:], b[y:])
		for i := 0; i < (x+1)/2; i++ {
			a[i], a[x-1-i] = -a[x-1-i], -a[i]
		}
		for i := 0; i < (y+1)/2; i++ {
			b[i], b[y-1-i] = -b[y-1-i], -b[i]
		}
		c += f(a[:x], b[:y])
		Fprintln(out, c)
	}
}

package main

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

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

	var T, n, m int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &m)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		b := make([]int, m)
		for i := range b {
			Fscan(in, &b[i])
		}
		f := func(a, b []int) (mx int) {
			h := map[int]bool{}
			for _, v := range a {
				h[v] = true
			}
			s := 0
			for _, v := range b {
				if h[v] {
					s++
				}
			}
			for i, v := range b {
				if h[v] {
					s--
				}
				j := SearchInts(b, v-SearchInts(a, v+1)+1)
				if i-j+1+s > mx {
					mx = i-j+1+s
				}
			}
			return
		}
		x, y := SearchInts(a, 0), SearchInts(b, 0)
		c := f(a[x:], b[y:])
		for i := 0; i < (x+1)/2; i++ {
			a[i], a[x-1-i] = -a[x-1-i], -a[i]
		}
		for i := 0; i < (y+1)/2; i++ {
			b[i], b[y-1-i] = -b[y-1-i], -b[i]
		}
		c += f(a[:x], b[:y])
		Fprintln(out, c)
	}
}

package main

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

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

func CF1494C(in io.Reader, out io.Writer) {
	r := bufio.NewReader(in)
	w := bufio.NewWriter(out)
	defer w.Flush()

	f := func(a, b []int) (mx int) {
		h := map[int]bool{}
		for _, v := range a {
			h[v] = true
		}
		s := 0
		for _, v := range b {
			if h[v] {
				s++
			}
		}
		for i, v := range b {
			if h[v] {
				s--
			}
			j := sort.SearchInts(b, v-sort.SearchInts(a, v+1)+1)
			if i-j+1+s > mx {
				mx = i - j + 1 + s
			}
		}
		return
	}
	var T, n, m int64
	for fmt.Fscan(r, &T); T > 0; T-- {
		fmt.Fscan(r, &n, &m)
		a, b := make([]int, n), make([]int, m)
		for i := range a {
			fmt.Fscan(r, &a[i])
		}
		for i := range b {
			fmt.Fscan(r, &b[i])
		}
		x, y := sort.SearchInts(a, 0), sort.SearchInts(b, 0)
		c := f(a[x:], b[y:])
		for i := 0; i < (x+1)/2; i++ {
			a[i], a[x-1-i] = -a[x-1-i], -a[i]
		}
		for i := 0; i < (y+1)/2; i++ {
			b[i], b[y-1-i] = -b[y-1-i], -b[i]
		}
		c += f(a[:x], b[:y])
		fmt.Fprintln(out, c)
	}

}

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

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

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

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

package main

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

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

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

	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &m)
		a := make([]int, n)
		aPositiveStart := n
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			if a[i] > 0 && aPositiveStart == n {
				aPositiveStart = i
			}
		}
		b := make([]int, m)
		bPositiveStart := m
		for i := 0; i < m; i++ {
			fmt.Fscan(in, &b[i])
			if b[i] > 0 && bPositiveStart == m {
				bPositiveStart = i
			}
		}

		pos, neg := 0, 0
		alreadyFit := make([]bool, m)
		for i, j := 0, 0; j < m; j++ {
			if a[i] < b[j] {
				for ; i < n && a[i] < b[j]; i++ {
				}
				if i == n {
					break
				}
			}
			if a[i] == b[j] {
				if b[j] > 0 {
					pos++
				} else {
					neg++
				}
				alreadyFit[j] = true
			}
		}
		maxPos, maxNeg := pos, neg

		if aPositiveStart < n && bPositiveStart < m {
			trainLen := 0
			next := bPositiveStart
			for i, j := aPositiveStart, bPositiveStart; j < m; j++ {
				if j > bPositiveStart && trainLen > 0 {
					pos--
				}
				for ; i < n && a[i] <= b[j]; i++ {
					trainLen++
				}
				for next = max(j, next); next < m && b[next] < b[j]+trainLen; next++ {
					if !alreadyFit[next] {
						pos++
						if pos > maxPos {
							maxPos = pos
						}
					}
				}
			}
		}
		if aPositiveStart > 0 && bPositiveStart > 0 {
			trainLen := 0
			next := bPositiveStart - 1
			for i, j := aPositiveStart-1, bPositiveStart-1; j >= 0; j-- {
				if j < bPositiveStart-1 && trainLen > 0 {
					neg--
				}
				for ; i >= 0 && a[i] >= b[j]; i-- {
					trainLen++
				}
				for next = min(j, next); next >= 0 && b[next] > b[j]-trainLen; next-- {
					if !alreadyFit[next] {
						neg++
						if neg > maxNeg {
							maxNeg = neg
						}
					}
				}
			}
		}
		if 1000-t == 77 {
			fmt.Println(a, b)
		}

		fmt.Println(maxPos + maxNeg)
	}
}

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

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

package main

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

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

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

	debug := t == 1000
	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &m)
		a := make([]int, n)
		aPositiveStart := n
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			if a[i] > 0 && aPositiveStart == n {
				aPositiveStart = i
			}
		}
		b := make([]int, m)
		bPositiveStart := m
		for i := 0; i < m; i++ {
			fmt.Fscan(in, &b[i])
			if b[i] > 0 && bPositiveStart == m {
				bPositiveStart = i
			}
		}

		pos, neg := 0, 0
		alreadyFit := make([]bool, m)
		for i, j := 0, 0; j < m; j++ {
			if a[i] < b[j] {
				for ; i < n && a[i] < b[j]; i++ {
				}
				if i == n {
					break
				}
			}
			if a[i] == b[j] {
				if b[j] > 0 {
					pos++
				} else {
					neg++
				}
				alreadyFit[j] = true
			}
		}
		maxPos, maxNeg := pos, neg

		if aPositiveStart < n && bPositiveStart < m {
			trainLen := 0
			next := bPositiveStart
			for i, j := aPositiveStart, bPositiveStart; j < m; j++ {
				if j > bPositiveStart && trainLen > 0 {
					pos--
				}
				for ; i < n && a[i] <= b[j]; i++ {
					trainLen++
				}
				for next = max(j, next); next < m && b[next] < b[j]+trainLen; next++ {
					if !alreadyFit[next] && trainLen > 0 {
						pos++
						if pos > maxPos {
							maxPos = pos
						}
					}
				}
			}
		}
		if aPositiveStart > 0 && bPositiveStart > 0 {
			trainLen := 0
			next := bPositiveStart - 1
			for i, j := aPositiveStart-1, bPositiveStart-1; j >= 0; j-- {
				if j < bPositiveStart-1 {
					neg--
				}
				for ; i >= 0 && a[i] >= b[j]; i-- {
					trainLen++
				}
				for next = min(j, next); next >= 0 && b[next] > b[j]-trainLen; next-- {
					if !alreadyFit[next] {
						neg++
						if neg > maxNeg {
							maxNeg = neg
						}
					}
				}
			}
		}
		if debug && 1000-t == 77 {
			fmt.Println(a, b)
		}

		fmt.Println(maxPos + maxNeg)
	}
}

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

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

package main

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

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

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

	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &m)
		a := make([]int, n)
		aPositiveStart := n
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			if a[i] > 0 && aPositiveStart == n {
				aPositiveStart = i
			}
		}
		b := make([]int, m)
		bPositiveStart := m
		for i := 0; i < m; i++ {
			fmt.Fscan(in, &b[i])
			if b[i] > 0 && bPositiveStart == m {
				bPositiveStart = i
			}
		}

		pos, neg := 0, 0
		alreadyFit := make([]bool, m)
		for i, j := 0, 0; j < m; j++ {
			if a[i] < b[j] {
				for ; i < n && a[i] < b[j]; i++ {
				}
				if i == n {
					break
				}
			}
			if a[i] == b[j] {
				if b[j] > 0 {
					pos++
				} else {
					neg++
				}
				alreadyFit[j] = true
			}
		}
		maxPos, maxNeg := pos, neg

		if aPositiveStart < n && bPositiveStart < m {
			trainLen := 0
			next := bPositiveStart
			for i, j := aPositiveStart, bPositiveStart; j < m; j++ {
				if j > bPositiveStart && trainLen > 0 {
					pos--
				}
				for ; i < n && a[i] <= b[j]; i++ {
					trainLen++
				}
				for next = max(j, next); next < m && b[next] < b[j]+trainLen; next++ {
					if !alreadyFit[next] && trainLen > 0 {
						pos++
						if pos > maxPos {
							maxPos = pos
						}
					}
				}
			}
		}
		if aPositiveStart > 0 && bPositiveStart > 0 {
			trainLen := 0
			next := bPositiveStart - 1
			for i, j := aPositiveStart-1, bPositiveStart-1; j >= 0; j-- {
				if j < bPositiveStart-1 && trainLen > 0 {
					neg--
				}
				for ; i >= 0 && a[i] >= b[j]; i-- {
					trainLen++
				}
				for next = min(j, next); next >= 0 && b[next] > b[j]-trainLen; next-- {
					if !alreadyFit[next] && trainLen > 0 {
						neg++
						if neg > maxNeg {
							maxNeg = neg
						}
					}
				}
			}
		}

		fmt.Println(maxPos + maxNeg)
	}
}

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

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

package main

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

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

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

	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &m)
		a := make([]int, n)
		aPosStart := n
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			if a[i] > 0 && aPosStart == n {
				aPosStart = i
			}
		}
		b := make([]int, m)
		bPosStart := m
		for i := 0; i < m; i++ {
			fmt.Fscan(in, &b[i])
			if b[i] > 0 && bPosStart == m {
				bPosStart = i
			}
		}
		alreadyFit := make([]bool, m)
		pos, neg := 0, 0
		for i, j := 0, 0; j < m; j++ {
			if a[i] < b[j] {
				for ; i < n && a[i] < b[j]; i++ {
				}
				if i == n {
					break
				}
			}
			if a[i] == b[j] {
				if b[j] > 0 {
					pos++
				} else {
					neg++
				}
				alreadyFit[j] = true
			}
		}
		maxPos, maxNeg := pos, neg

		if aPosStart < n && bPosStart < m {
			trainLen := 0
			next := bPosStart
			for i, j := aPosStart, bPosStart; j < m; j++ {
				if j > bPosStart && trainLen > 0 {
					pos--
				}
				for ; i < n && a[i] <= b[j]; i++ {
					trainLen++
				}
				for ; next < m && b[next] < b[j]+trainLen; next++ {
					if !alreadyFit[next] && trainLen > 0 {
						pos++
						if pos > maxPos {
							maxPos = pos
						}
					}
				}
			}
		}
		if aPosStart > 0 && bPosStart > 0 {
			trainLen := 0
			next := bPosStart - 1
			for i, j := aPosStart-1, bPosStart-1; j >= 0; j-- {
				if j < bPosStart-1 && trainLen > 0 {
					neg--
				}
				for ; i >= 0 && a[i] >= b[j]; i-- {
					trainLen++
				}
				for ; next >= 0 && b[next] > b[j]-trainLen; next-- {
					if !alreadyFit[next] && trainLen > 0 {
						neg++
						if neg > maxNeg {
							maxNeg = neg
						}
					}
				}
			}
		}

		fmt.Println(maxPos + maxNeg)
	}
}

package main

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

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

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

	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &m)
		a := make([]int, n)
		aPosStart := n
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			if a[i] > 0 && aPosStart == n {
				aPosStart = i
			}
		}
		b := make([]int, m)
		bPosStart := m
		for i := 0; i < m; i++ {
			fmt.Fscan(in, &b[i])
			if b[i] > 0 && bPosStart == m {
				bPosStart = i
			}
		}
		alreadyFit := make([]bool, m)
		pos, neg := 0, 0
		for i, j := 0, 0; j < m; j++ {
			if a[i] < b[j] {
				for ; i < n && a[i] < b[j]; i++ {
				}
				if i == n {
					break
				}
			}
			if a[i] == b[j] {
				if b[j] > 0 {
					pos++
				} else {
					neg++
				}
				alreadyFit[j] = true
			}
		}
		maxPos, maxNeg := pos, neg

		if aPosStart < n && bPosStart < m {
			trainLen := 0
			next := bPosStart
			for i, j := aPosStart, bPosStart; j < m; j++ {
				if j > bPosStart && trainLen > 0 {
					pos--
				}
				for ; i < n && a[i] <= b[j]; i++ {
					trainLen++
				}
				for ; next < m && b[next] < b[j]+trainLen; next++ {
					if !alreadyFit[next] && trainLen > 0 {
						pos++
						if pos > maxPos {
							maxPos = pos
						}
					}
				}
			}
		}
		if aPosStart > 0 && bPosStart > 0 {
			trainLen := 0
			next := bPosStart - 1
			for i, j := aPosStart-1, bPosStart-1; j >= 0; j-- {
				if j < bPosStart-1 && trainLen > 0 {
					neg--
				}
				for ; i >= 0 && a[i] >= b[j]; i-- {
					trainLen++
				}
				for ; next >= 0 && b[next] > b[j]-trainLen; next-- {
					if !alreadyFit[next] {
						neg++
						if neg > maxNeg {
							maxNeg = neg
						}
					}
				}
			}
		}

		fmt.Println(maxPos + maxNeg)
	}
}

package main

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

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

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

	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &m)
		a := make([]int, n)
		aFirstPositive := n
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			if a[i] > 0 && aFirstPositive > i {
				aFirstPositive = i
			}
		}
		b := make([]int, m)
		bFirstPositive := m
		for i := 0; i < m; i++ {
			fmt.Fscan(in, &b[i])
			if b[i] > 0 && bFirstPositive > i {
				bFirstPositive = i
			}
		}
		alreadyFit := make([]bool, m)
		pos, neg := 0, 0
		for i, j := 0, 0; j < m; j++ {
			if a[i] < b[j] {
				for ; i < n && a[i] < b[j]; i++ {
				}
				if i == n {
					break
				}
			}
			if a[i] == b[j] {
				if b[j] > 0 {
					pos++
				} else {
					neg++
				}
				alreadyFit[j] = true
			}
		}
		counted := make([]bool, m)
		maxPos, maxNeg := pos, neg

		if aFirstPositive < n && bFirstPositive < m {
			trainLen := 0
			next := bFirstPositive
			for i, j := aFirstPositive, bFirstPositive; j < m; j++ {
				if j > bFirstPositive && trainLen > 0 {
					pos--
				}
				for ; i < n && a[i] <= b[j]; i++ {
					trainLen++
				}
				for ; next < m && b[next] < b[j]+trainLen; next++ {
					if !alreadyFit[next] && !counted[next] {
						pos++
						if pos > maxPos {
							maxPos = pos
						}
						counted[next] = true
					}
				}
			}
		}
		if aFirstPositive > 0 && bFirstPositive > 0 {
			trainLen := 0
			next := bFirstPositive - 1
			for i, j := aFirstPositive-1, bFirstPositive-1; j >= 0; j-- {
				if j < bFirstPositive-1 && trainLen > 0 {
					neg--
				}
				for ; i >= 0 && a[i] >= b[j]; i-- {
					trainLen++
				}
				for ; next >= 0 && b[next] > b[j]-trainLen; next-- {
					if !alreadyFit[next] && !counted[next] {
						neg++
						if neg > maxNeg {
							maxNeg = neg
						}
						counted[next] = true
					}
				}
				//fmt.Println(i, j, trainLen, counted, neg, next)
			}
		}

		fmt.Println(maxPos + maxNeg)
	}
}

package main

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

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

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

	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &m)
		a := make([]int, n)
		aFirstPositive := n
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			if a[i] > 0 && aFirstPositive > i {
				aFirstPositive = i
			}
		}
		b := make([]int, m)
		bFirstPositive := m
		for i := 0; i < m; i++ {
			fmt.Fscan(in, &b[i])
			if b[i] > 0 && bFirstPositive > i {
				bFirstPositive = i
			}
		}
		alreadyFit := make([]bool, m)
		pos, neg := 0, 0
		for i, j := 0, 0; j < m; j++ {
			if a[i] < b[j] {
				for ; i < n && a[i] < b[j]; i++ {
				}
				if i == n {
					break
				}
			}
			if a[i] == b[j] {
				if b[j] > 0 {
					pos++
				} else {
					neg++
				}
				alreadyFit[j] = true
			}
		}
		counted := make([]bool, m)
		maxPos, maxNeg := pos, neg
		if aFirstPositive < n && bFirstPositive < m {
			trainLen := 0
			for i, j := aFirstPositive, bFirstPositive; j < m; j++ {
				if j > bFirstPositive && trainLen > 0 {
					pos--
				}
				for ; i < n && a[i] <= b[j]; i++ {
					trainLen++
				}
				for k := j; k < m && b[k] < b[j]+trainLen; k++ {
					if !alreadyFit[k] && !counted[k] {
						pos++
						if pos > maxPos {
							maxPos = pos
						}
						counted[k] = true
					}
				}
			}
		}
		if aFirstPositive > 0 && bFirstPositive > 0 {
			trainLen := 0
			for i, j := aFirstPositive-1, bFirstPositive-1; j >= 0; j-- {
				if j < bFirstPositive-1 && trainLen > 0 {
					neg--
				}
				for ; i >= 0 && a[i] >= b[j]; i-- {
					trainLen++
				}
				for k := j; k >= 0 && b[k] > b[j]-trainLen; k-- {
					if !alreadyFit[k] && !counted[k] {
						neg++
						if neg > maxNeg {
							maxNeg = neg
						}
						counted[k] = true
					}
				}
				//fmt.Println(i, j, trainLen, counted, neg)
			}
		}

		fmt.Println(maxPos + maxNeg)
	}
}

package main

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

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

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

	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &m)
		a := make([]int, n)
		aFirstPositive := n
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			if a[i] > 0 && aFirstPositive > i {
				aFirstPositive = i
			}
		}
		b := make([]int, m)
		bFirstPositive := m
		for i := 0; i < m; i++ {
			fmt.Fscan(in, &b[i])
			if b[i] > 0 && bFirstPositive > i {
				bFirstPositive = i
			}
		}
		alreadyFit := make([]bool, m)
		pos, neg := 0, 0
		for i, j := 0, 0; j < m; j++ {
			if a[i] < b[j] {
				for ; i < n && a[i] < b[j]; i++ {
				}
				if i == n {
					break
				}
			}
			if a[i] == b[j] {
				if b[j] > 0 {
					pos++
				} else {
					neg++
				}
				alreadyFit[j] = true
			}
		}
		counted := make([]bool, m)
		maxPos, maxNeg := pos, neg
		if aFirstPositive < n && bFirstPositive < m {
			trainLen := 0
			for i, j := aFirstPositive, bFirstPositive; j < m; j++ {
				if j > bFirstPositive {
					pos--
				}
				for ; i < n && a[i] < b[j]; i++ {
					trainLen++
				}
				for k := j; k < m && b[k] < b[j]+trainLen; k++ {
					if !alreadyFit[k] && !counted[k] {
						pos++
						if pos > maxPos {
							maxPos = pos
						}
						counted[k] = true
					}
				}
			}
		}
		if aFirstPositive > 0 && bFirstPositive > 0 {
			trainLen := 0
			for i, j := aFirstPositive-1, bFirstPositive-1; j >= 0; j-- {
				if j < bFirstPositive-1 {
					neg--
				}
				for ; i >= 0 && a[i] > b[j]; i-- {
					trainLen++
				}
				for k := j; k >= 0 && b[k] > b[j]-trainLen; k-- {
					if !alreadyFit[k] && !counted[k] {
						neg++
						if neg > maxNeg {
							maxNeg = neg
						}
						counted[k] = true
					}
				}
			}
		}

		fmt.Println(maxPos + maxNeg)
	}
}

package main

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

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

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

	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &m)
		a := make([]int, n)
		aFirstPositive := n
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			if a[i] > 0 && aFirstPositive > i {
				aFirstPositive = i
			}
		}
		b := make([]int, m)
		bFirstPositive := m
		for i := 0; i < m; i++ {
			fmt.Fscan(in, &b[i])
			if b[i] > 0 && bFirstPositive > i {
				bFirstPositive = i
			}
		}
		alreadyFit := make([]bool, m)
		pos, neg := 0, 0
		for i, j := 0, 0; j < m; j++ {
			if a[i] < b[j] {
				for ; i < n && a[i] < b[j]; i++ {
				}
				if i == n {
					break
				}
			}
			if a[i] == b[j] {
				if b[j] > 0 {
					pos++
				} else {
					neg++
				}
				alreadyFit[j] = true
			}
		}
		maxPos, maxNeg := pos, neg
		if aFirstPositive < n && bFirstPositive < m {
			trainLen := 0
			for i, j := aFirstPositive, bFirstPositive; j < m; j++ {
				if j > bFirstPositive {
					pos--
				}
				for ; i < n && a[i] < b[j]; i++ {
					trainLen++
				}
				for k := j; k < m && b[k] < b[j]+trainLen; k++ {
					if !alreadyFit[k] {
						pos++
						if pos > maxPos {
							maxPos = pos
						}
					}
				}
			}
		}
		if aFirstPositive > 0 && bFirstPositive > 0 {
			trainLen := 0
			for i, j := aFirstPositive-1, bFirstPositive-1; j >= 0; j-- {
				if j < bFirstPositive-1 {
					neg--
				}
				for ; i >= 0 && a[i] > b[j]; i-- {
					trainLen++
				}
				for k := j; k >= 0 && b[k] > b[j]-trainLen; k-- {
					if !alreadyFit[k] {
						neg++
						if neg > maxNeg {
							maxNeg = neg
						}
					}
				}
			}
		}

		fmt.Println(maxPos + maxNeg)
	}
}


David was given a red checkered rectangle of size $$$n \times m$$$. But he doesn't like it. So David cuts the original or any other rectangle piece obtained during the cutting into two new pieces along the grid lines. He can do this operation as many times as he wants.As a result, he will get a set of rectangles. Rectangles $$$1 \times 1$$$ are forbidden.David also knows how to paint the cells blue. He wants each rectangle from the resulting set of pieces to be colored such that any pair of adjacent cells by side (from the same piece) have different colors.What is the minimum number of cells David will have to paint?

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^3$$$) — the number of test cases. The next lines contain descriptions of test cases. The only line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n, m \leq 3 \cdot 10^4$$$, $$$n \cdot m \geq 2$$$).

For each test case print a single integer — the minimum number of cells David will have to paint blue.

NoteThe following pictures show how the initial rectangle can be split and cells colored blue.In the first test case:  In the second test case:  In the third test case:  In the fourth test case:

package main

import "fmt"

func main() {
  var t int
  fmt.Scanf("%d\n", &t)
  for ; t > 0; t-- {
    var m, n int
    fmt.Scanf("%d %d\n", &m, &n)
    fmt.Printf("%d\n", (m*n+2)/3)
  }
}

package main

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

func main() {
	reader := bReader(os.Stdin)
	writer := bWriter(os.Stdout)

	t := readInt(reader)

	for t > 0 {
		n, m := readTwoInt64(reader)

		fmt.Fprintln(writer, (n*m+2)/3)
		t--
	}

	writer.Flush()
}

func sum(a1 int64, n int64) int64 {
	return (2*a1 + (n-1)*4)*n/2
}

func bReader(stdin *os.File) *bufio.Reader {
	return bufio.NewReaderSize(stdin, 16*1024*1024)
}

func bWriter(stdout *os.File) *bufio.Writer {
	return bufio.NewWriterSize(stdout, 16*1024*1024)
}

func readLine(reader *bufio.Reader) string {
	str, _, err := reader.ReadLine()
	if err == io.EOF {
		return ""
	}

	return strings.TrimRight(string(str), "\r\n")
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

func readInt(reader *bufio.Reader) int {
	return int(readInt64(reader))
}

func readInt64(reader *bufio.Reader) int64 {
	value, err := strconv.ParseInt(readLine(reader), 10, 64)
	checkError(err)

	return value
}

func readString(reader *bufio.Reader) string {
	return readLine(reader)
}

func readArrayString(reader *bufio.Reader) []string {
	return strings.Split(strings.TrimSpace(readLine(reader)), " ")
}

func readArrayInt(reader *bufio.Reader) []int {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := int(arItemTemp)
		ar = append(ar, arItem)
	}

	return ar
}

func readArrayInt64(reader *bufio.Reader) []int64 {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int64

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := arItemTemp
		ar = append(ar, arItem)
	}

	return ar
}

func readTwoInt(reader *bufio.Reader) (int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1]
}

func readTwoInt64(reader *bufio.Reader) (int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1]
}

func readThreeInt(reader *bufio.Reader) (int, int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1], ar[2]
}

func readThreeInt64(reader *bufio.Reader) (int64, int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1], ar[2]
}

package main

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

/*
9
1 2
2 1
3 3
6 6
1 6
2 6
2 7
4 7
5 8
---
1
1
3
12
2
4
5
10
14
*/

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

	bld := strings.Builder{}

	var t int
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		var n, m int
		scanf("%d %d\n", &n, &m)

		cnt := n * m / 3
		if n * m % 3 > 0 {
			cnt++
		}

		fmt.Fprintf(&bld, "%d\n", cnt)
	}

	printf("%s", bld.String())
}

const maxUint = ^uint(0)

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

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

//goland:noinspection GoUnusedType
type pair struct {
	fst int
	snd int
}

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

//goland:noinspection GoUnusedFunction
func printf(f string, a ...interface{}) {
	if _, err := fmt.Fprintf(writer, f, a...); err != nil {
		panic(err)
	}
}

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

//goland:noinspection GoUnusedFunction
func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

//goland:noinspection GoUnusedFunction
func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

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

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

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

	for i := 5; i*i <= n; i += 6 {
		if n%i == 0 || n%(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)
}

//goland:noinspection GoUnusedFunction
func digitAt(num, pos int64) int64 {
	rem := num % int64(math.Pow(10, float64(pos)))
	return rem / int64(math.Pow(10, float64(pos-1)))
}

package main

import (
	"fmt"
)

func F(n, m int) int {
	if n == 0 || m == 0 {
		return 0
	}

	if n == 2 && m == 2 {
		return 2
	}

	if n == 1 {
		return (m-1)/3 + 1
	}

	if m == 1 {
		return (n-1)/3 + 1
	}

	if ((n / 3) * m) > ((m / 3) * n) {
		return (n/3)*m + F(n%3, m)
	} else {
		return (m/3)*n + F(n, m%3)
	}
}

func main() {
	var t int
	fmt.Scanln(&t)

	for i := 1; i <= t; i++ {
		var n, m int
		fmt.Scanln(&n, &m)

		fmt.Println(F(n, m))

	}
}


An army of n droids is lined up in one row. Each droid is described by m integers a1, a2, ..., am, where ai is the number of details of the i-th type in this droid's mechanism. R2-D2 wants to destroy the sequence of consecutive droids of maximum length. He has m weapons, the i-th weapon can affect all the droids in the army by destroying one detail of the i-th type (if the droid doesn't have details of this type, nothing happens to it). A droid is considered to be destroyed when all of its details are destroyed. R2-D2 can make at most k shots. How many shots from the weapon of what type should R2-D2 make to destroy the sequence of consecutive droids of maximum length?

The first line contains three integers n, m, k (1 ≤ n ≤ 105, 1 ≤ m ≤ 5, 0 ≤ k ≤ 109) — the number of droids, the number of detail types and the number of available shots, respectively. Next n lines follow describing the droids. Each line contains m integers a1, a2, ..., am (0 ≤ ai ≤ 108), where ai is the number of details of the i-th type for the respective robot.

Print m space-separated integers, where the i-th number is the number of shots from the weapon of the i-th type that the robot should make to destroy the subsequence of consecutive droids of the maximum length. If there are multiple optimal solutions, print any of them.  It is not necessary to make exactly k shots, the number of shots can be less.

NoteIn the first test the second, third and fourth droids will be destroyed. In the second test the first and second droids will be destroyed.

package main

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

// Maximum heap
type Heap struct {
	s []int
}

func (h *Heap) Empty() bool {
	return len(h.s) == 0
}

func (h *Heap) Push(x int) {
	h.s = append(h.s, x)
	p := len(h.s) - 1
	for p > 0 && x > h.s[(p-1)/2] {
		h.s[p] = h.s[(p-1)/2]
		p = (p-1) / 2
	}
	h.s[p] = x
}

func (h *Heap) Pop() int {
	res := h.s[0]
	h.s[0] = h.s[len(h.s) - 1]
	h.s = h.s[0:len(h.s) - 1]
	p := 0
	for p * 2 + 1 < len(h.s) {
		np := p * 2 + 1
		x := h.s[np]
		if np + 1 < len(h.s) && x < h.s[np + 1] {
			x = h.s[np + 1]
			np++
		}
		if h.s[p] >= x {
			break
		}
		h.s[p], h.s[np] = x, h.s[p]
		p = np
	}
	return res
}

func main() {
	stdin := bufio.NewReader(os.Stdin)
	stdout := bufio.NewWriter(os.Stdout)

	for true {
		var n, m, k int
		if _, err := fmt.Fscan(stdin, &n, &m, &k); err != nil {
			break
		}
		a := make([][]int, n)
		for i := 0; i < n; i++ {
			a[i] = make([]int, m)
		}
		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				fmt.Fscan(stdin, &a[i][j])
			}
		}
		maps := make([]map[int]int, m)
		for i := 0; i < m; i++ {
			maps[i] = make(map[int]int)
		}
		heaps := make([]Heap, m)

		popHeap := func(i int) {
			for true {
				heaps[i].Pop()
				if heaps[i].Empty() {
					break
				}
				//fmt.Printf("i %d, m %d, len(heaps[i].s) = %d\n", i, m, len(heaps[i].s))
				if maps[i][heaps[i].s[0]] != 0 {
					break
				}
			}
		}

		bestLen := 0
		bestShot := make([]int, m)
		shot := make([]int, m)
		start := 0
		end := 0
		for true {
			for end < n {
				sum := 0
				for i := 0; i < m; i++ {
					v := a[end][i]
					if v > shot[i] {
						shot[i] = v
					}
					sum += shot[i]
					maps[i][v]++
					if maps[i][v] == 1 {
						heaps[i].Push(v)
					}
				}
				end++
				if sum > k {
					break
				}
				if sum <= k && end - start + 1 > bestLen {
					bestLen = end - start + 1
					copy(bestShot, shot)
				}
			}
			if end == n {
				break
			}
			for start <= end {
				recalc := false
				for i := 0; i < m; i++ {
					v := a[start][i]
					maps[i][v]--
					if v == shot[i] && maps[i][v] == 0 {
						popHeap(i)
						recalc = true
					}
				}
				start++
				if recalc {
					sum := 0
					for i := 0; i < m; i++ {
						if heaps[i].Empty() {
							shot[i] = 0
						} else {
							shot[i] = heaps[i].s[0]
						}
						sum += shot[i]
					}
					if sum <= k {
						if end - start + 1 > bestLen {
							bestLen = end - start + 1
							copy(bestShot, shot)
						}
						break
					}
				}
			}
			//fmt.Printf("start = %d, end = %d\n", start, end)
		}

		left := k
		for i := 0; i < m; i++ {
			left -= bestShot[i]
		}
		bestShot[0] += left
		for i := 0; i < m; i++ {
			if i > 0 {
				fmt.Fprintf(stdout, " ")
			}
			fmt.Fprintf(stdout, "%d", bestShot[i])
		}
		fmt.Fprintf(stdout, "\n")
	}
	stdout.Flush()
}

package main

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

// Find Range maximum.
type SegTree struct {
	n int
	s []int
}

func Max(a ...int) int {
	res := math.MinInt32
	for _, v := range a {
		if res < v {
			res = v
		}
	}
	return res
}

func NewSegTree(a []int) *SegTree {
	n := len(a)
	t := SegTree{n: n, s: make([]int, 4 * n)}
	var construct func(pos, left, right int)
	construct = func(pos, left, right int) {
		if left == right {
			t.s[pos] = a[left]
			return
		}
		mid := (left + right) / 2
		construct(pos * 2 + 1, left, mid)
		construct(pos * 2 + 2, mid + 1, right)
		t.s[pos] = Max(t.s[pos * 2 + 1], t.s[pos * 2 + 2])
	}
	construct(0, 0, n - 1)
	return &t
}

func (t *SegTree) GetRangeMax(start, end int) int {
	var f func(pos, left, right int) int
	f = func(pos, left, right int) int {
		if start > right || end < left {
			return math.MinInt32
		}
		if left >= start && right <= end {
			return t.s[pos]
		}
		mid := (left + right) / 2
		return Max(f(pos * 2 + 1, left, mid), f(pos * 2 + 2, mid + 1, right))
	}
	return f(0, 0, t.n-1)
}

func main() {
	stdin := bufio.NewReader(os.Stdin)
	stdout := bufio.NewWriter(os.Stdout)

	for true {
		var n, m, k int
		if _, err := fmt.Fscan(stdin, &n, &m, &k); err != nil {
			break
		}
		a := make([][]int, m)
		for i := 0; i < m; i++ {
			a[i] = make([]int, n)
		}
		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				fmt.Fscan(stdin, &a[j][i])
			}
		}
		trees := make([]*SegTree, m)
		for i := 0; i < m; i++ {
			trees[i] = NewSegTree(a[i])
		}
		bestLen := 0
		bestShots := make([]int, m)
		shots := make([]int, m)
		tmpShots := make([]int, m)

		tryRange := func(start, end int) bool {
			count := 0
			for i := 0; i < m; i++ {
				tmpShots[i] = trees[i].GetRangeMax(start, end)
				count += Max(tmpShots[i], shots[i])
			}
			if count > k {
				return false
			}
			for i := 0; i < m; i++ {
				shots[i] = Max(tmpShots[i], shots[i])
			}
			return true
		}

		prevTo := 0
		for i := 0; i < n; i++ {
			if n - i < bestLen {
				break
			}
			low := Max(i, prevTo)
			high := n
			for j := 0; j < m; j++ {
				shots[j] = 0
			}
			for low < high {
				mid := (low + high) / 2
				if tryRange(low, mid) {
					low = mid + 1
				} else {
					high = mid
				}
			}
			prevTo = low - 1
			curLen := low - i
			//fmt.Printf("i = %d, low = %d, curLen %d, shots = %v\n", i, low, curLen, shots)
			if curLen > bestLen {
				bestLen = curLen
				copy(bestShots, shots)
			}
		}
		left := k
		for _, c := range bestShots {
			left -= c
		}
		bestShots[0] += left
		for i, v := range bestShots {
			if i > 0 {
				fmt.Fprintf(stdout, " ")
			}
			fmt.Fprintf(stdout, "%d", v)
		}
		fmt.Fprintf(stdout, "\n")
	}
	stdout.Flush()
}

package main

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

// Find Range maximum.
type SegTree struct {
	n int
	s []int
}

func Max(a ...int) int {
	res := math.MinInt32
	for _, v := range a {
		if res < v {
			res = v
		}
	}
	return res
}

func NewSegTree(a []int) *SegTree {
	n := len(a)
	t := SegTree{n: n, s: make([]int, 4 * n)}
	var construct func(pos, left, right int)
	construct = func(pos, left, right int) {
		if left == right {
			t.s[pos] = a[left]
			return
		}
		mid := (left + right) / 2
		construct(pos * 2 + 1, left, mid)
		construct(pos * 2 + 2, mid + 1, right)
		t.s[pos] = Max(t.s[pos * 2 + 1], t.s[pos * 2 + 2])
	}
	construct(0, 0, n - 1)
	return &t
}

func (t *SegTree) GetRangeMax(start, end int) int {
	var f func(pos, left, right int) int
	f = func(pos, left, right int) int {
		if start > right || end < left {
			return math.MinInt32
		}
		if left >= start && right <= end {
			return t.s[pos]
		}
		mid := (left + right) / 2
		return Max(f(pos * 2 + 1, left, mid), f(pos * 2 + 2, mid + 1, right))
	}
	return f(0, 0, t.n-1)
}

func main() {
	stdin := bufio.NewReader(os.Stdin)
	stdout := bufio.NewWriter(os.Stdout)

	for true {
		var n, m, k int
		if _, err := fmt.Fscan(stdin, &n, &m, &k); err != nil {
			break
		}
		a := make([][]int, m)
		for i := 0; i < m; i++ {
			a[i] = make([]int, n)
		}
		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				fmt.Fscan(stdin, &a[j][i])
			}
		}
		trees := make([]*SegTree, m)
		for i := 0; i < m; i++ {
			trees[i] = NewSegTree(a[i])
		}
		bestLen := 0
		bestShots := make([]int, m)
		shots := make([]int, m)
		tmpShots := make([]int, m)

		tryRange := func(start, end int) bool {
			count := 0
			for i := 0; i < m; i++ {
				tmpShots[i] = trees[i].GetRangeMax(start, end)
				count += Max(tmpShots[i], shots[i])
			}
			if count > k {
				return false
			}
			for i := 0; i < m; i++ {
				shots[i] += tmpShots[i]
			}
			return true
		}

		for i := 0; i < n; i++ {
			if n - i < bestLen {
				break
			}
			low := i
			high := n
			for j := 0; j < m; j++ {
				shots[j] = 0
			}
			for low < high {
				mid := (low + high) / 2
				if tryRange(low, mid) {
					low = mid + 1
				} else {
					high = mid
				}
			}
			curLen := low - i
			if curLen > bestLen {
				bestLen = curLen
				bestShots = shots
			}
		}
		left := k
		for _, c := range bestShots {
			left -= c
		}
		bestShots[0] += left
		for i, v := range bestShots {
			if i > 0 {
				fmt.Fprintf(stdout, " ")
			}
			fmt.Fprintf(stdout, "%d", v)
		}
		fmt.Fprintf(stdout, "\n")
	}
	stdout.Flush()
}