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

// 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"
	"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
}


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


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

// 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"
	"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]+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++
				}
			}
			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"
	"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"
)

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


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

	}
}

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"
    "sort"
    )

func main() {
    var n, m int

    fmt.Scan(&n)
    boys := make([]int, n, n)
    for i := 0; i < n; i++ {
        fmt.Scan(&boys[i])
    }
    sort.Sort(sort.IntSlice(boys))

    fmt.Scan(&m)
    girls := make([]int, m, m)
    for i := 0; i < m; i++ {
        fmt.Scan(&girls[i])
    }
    sort.Sort(sort.IntSlice(girls))

    i := 0
    j := 0
    result := 0
    for i < n && j < m {
        if girls[j] - boys[i] <= 1 && girls[j] - boys[i] >= -1 {
            result++
            i++
            j++
        } else if girls[j] > boys[i] {
            i++
        } else {
            j++
        }
    }

    fmt.Println(result)
}

package main

import (
	"fmt"
	"sort"
)

const paired = 10e3 // > problem upper bound value

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

func main() {
	n, m := 0, 0
	ans := 0
	fmt.Scan(&n)
	as := make([]int, n)
	for i := range as {
		fmt.Scan(&as[i])
	}
	fmt.Scan(&m)
	bs := make([]int, m)
	for i := range bs {
		fmt.Scan(&bs[i])
	}
	sort.Ints(as)
	sort.Ints(bs)
	for _, a := range as {
		for j, b := range bs {
			// skill gap
			if abs(a-b) <= 1 {
				bs[j] = paired
				ans++
				// pair formed -> next
				break
			}
		}
	}
	fmt.Println(ans)
}

package main

import (
  "fmt"
  "sort"
  "math"
  )

func main(){
  var m,d,p int
  fmt.Scan(&m)
  mt := make([]int,m)
  for i:=0;i<m;i++{
    fmt.Scan(&mt[i])
    }
  sort.Ints(mt)
fmt.Scan(&d)
  dt := make([]int,d)
  for i:=0;i<d;i++{
    fmt.Scan(&dt[i])
    }
  sort.Ints(dt)
  for a,b := 0,0; a<m && b<d; {
    if math.Abs(float64(mt[a]-dt[b])) <=1 {
      p++
      a++
      b++
    } else if mt[a]>dt[b] {
      b++
     } else {
      a++
      }
  }
  fmt.Println(p)
  }

package main

import "fmt"
import "sort"

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

func main() {
	var n, m, x, ans int
	var a1 []int
	var a2 []int
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		fmt.Scan(&x)
		a1 = append(a1, x)
	}
	sort.Ints(a1)
	fmt.Scan(&m)
	for i := 0; i < m; i++ {
		fmt.Scan(&x)
		a2 = append(a2, x)
	}
	sort.Ints(a2)
	id1 := 0
	id2 := 0
	for id1 < n && id2 < m {
		if abs(a1[id1], a2[id2]) <= 1 {
			id1++
			id2++
			ans++
			continue
		}
		if a1[id1] < a2[id2] {
			id1++
			continue
		}
		if a2[id2] < a1[id1] {
			id2++
			continue
		}
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
	"math"
	"sort"
)

func main() {
	var nb, ng int

	fmt.Scan(&nb)
	var bs = make([]int, nb)
	for i := 0; i < nb; i++ {
		fmt.Scan(&bs[i])
	}

	fmt.Scan(&ng)
	var gs = make([]int, ng)
	for i := 0; i < ng; i++ {
		fmt.Scan(&gs[i])
	}

	sort.Ints(bs)
	sort.Ints(gs)

	s := 0
	ans := 0
	for i := 0; i < nb; i++ {
		for j := s; j < ng; j++ {
			if math.Abs(float64(bs[i])-float64(gs[j])) < 2 {
				ans++
				s = j + 1
				break
			}
		}
	}

	fmt.Println(ans)
}

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

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

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

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

	return b
}

func main () {
	var (
		n,m int
		size int = 105
	)

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

	u := make([]int, size)
	v := make([]int, size)

	fmt.Fscan(input, &n)
	for i := 0; i < n; i++ {
		var tmp int
		fmt.Fscan(input, &tmp)
		u[tmp]++
	}

	fmt.Fscan(input, &m)
	for i := 0; i < m; i++ {
		var tmp int
		fmt.Fscan(input, &tmp)
		v[tmp]++
	}

	// Solve
	ans := 0
	for i := 0; i < size; i++ {
		d := min(u[i], v[i])

		u[i] -= d
		v[i] -= d
		ans += d

		if u[i] > 0 {
			d = min(u[i], v[i + 1])
			u[i] -= d
			v[i + 1] -= d
			ans += d
		}

		if v[i] > 0 {
			d = min(v[i], u[i + 1])
			v[i] -= d
			u[i + 1] -= d
			ans += d
		}
	}

	fmt.Println(ans)
}

package main;

import (
	"fmt"
	// "math"
)

var G [][]int;
var arr1, arr2 []int;
var n, m int;

var Rmatch []int;
var vis []bool;

func match(u int) bool {
	if (vis[u]) { return false; }
	vis[u] = true;
	for _, v := range G[u] {
		if (Rmatch[v] == -1 || match(Rmatch[v])) {
			Rmatch[v] = u;
			return true;
		}
	}
	return false;
}

func solve() int {
	var ret int = 0;
	vis = make([]bool, n)
	Rmatch = make([]int, m)
	for i := range Rmatch { Rmatch[i] = -1; }
	for i := 0; i < n; i++ {
		for j := range vis { vis[j] = false; }
		if match(i) { ret++; }
	}
	return ret;
}

func main() {
	fmt.Scanf("%d\n",&n);
	arr1 = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d",&arr1[i])
	}
	fmt.Scanf("\n%d\n",&m);
	arr2 = make([]int, m)
	for i := 0; i < m; i++ {
		fmt.Scanf("%d",&arr2[i])
	}
	G = make([][]int, n);
	for i := range arr1 {
		for j := range arr2 {
			if arr1[i] >= arr2[j] - 1 && arr1[i] <= arr2[j] + 1 {
				G[i] = append(G[i], j);
			}
		}
	}
	var matching int = solve()
	fmt.Println(matching)
}

package main

import (
            "fmt"
            "sort"
        )

func main() {
    var n, m int
    fmt.Scanln(&n)
    var a []int = make([]int, n)
    for i := 0; i < n; i++ {
        fmt.Scanf("%d", &a[i])
    }

    fmt.Scanln()

    fmt.Scanln(&m)
    var b []int = make([]int, m)
    for i := 0; i < m; i++ {
        fmt.Scanf("%d", &b[i])
    }

    sort.Ints(a)
    sort.Ints(b)

    var answer, free int

    for i := 0; i < n; i++ {
        for j := free; j < m; j++ {
            var diff int = b[j] - a[i]
            // For a[i] we dont have pair
            if diff > 1 {
                break
            }

            if diff == 1 || diff == 0 || diff == -1 {
                free = j + 1
                answer++
                break
            }
        }
    }

    fmt.Println(answer)
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n, m int
	fmt.Scan(&n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		var ai int
		fmt.Scan(&ai)
		a[i] = ai
	}

	fmt.Scan(&m)
	b := make([]int, m)
	for i := 0; i < m; i++ {
		var bi int
		fmt.Scan(&bi)
		b[i] = bi
	}
	sort.Sort(sort.IntSlice(a))
	sort.Sort(sort.IntSlice(b))

	c := 0
	for i, j := 0, 0; i < n && j < m; {
		if a[i] < b[j]-1 {
			i++
			continue
		}
		if b[j] < a[i]-1 {
			j++
			continue
		}
		if a[i] == b[j] || a[i] == b[j]+1 || a[i] == b[j]-1 {
			i++
			j++
			c++
			continue
		}
	}
	fmt.Println(c)
}

package main

import (
	"fmt"
	"sort"
)

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

	fmt.Scan(&n)

	a := make([]int, n)

	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}

	fmt.Scan(&m)

	b := make([]int, m)

	for i := 0; i < m; i++ {
		fmt.Scan(&b[i])
	}

	sort.Ints(a)
	sort.Ints(b)

	p := -1

	for i := 0; i < len(a); i++ {
		for j := p + 1; j < len(b); j++ {
			if a[i] == b[j] || a[i]+1 == b[j] || a[i]-1 == b[j] {
				p = j
				res++
				break
			}
		}
	}

	fmt.Println(res)
}

package main

import (
	"fmt"
	"math"
	"sort"
)

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

	fmt.Scan(&n)

	a := make([]int, n)

	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}

	fmt.Scan(&m)

	b := make([]int, m)

	for i := 0; i < m; i++ {
		fmt.Scan(&b[i])
	}

	sort.Ints(a)
	sort.Ints(b)

	p := -1

	for i := 0; i < len(a); i++ {
		for j := p + 1; j < len(b); j++ {
			if math.Abs(float64(a[i])-float64(b[j])) <= 1 {
				p = j
				res++
				break
			}
		}
	}

	fmt.Println(res)
}

package main

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

const MAXN = 100 + 10
var used ,math ,A,B  [MAXN]int
var G [MAXN][MAXN]int
var n ,m int

func abs(x int) int {

    var ret int
    if x >= 0 {

        ret = x
    }else {
        ret = -x
    }
    return ret
}

func  Find(x int)   bool {

    for i := 1;i <= m; i ++ {

        if 1 == G[x][i] && 0 == ( used[i]) {

            used[i] = 1
            if -1 == math[i] || Find(math[i]){
                math[i] = x
                return true

            }
        }
    }

    return false
}
func build() int {

    for i := 0 ;i <= n; i ++ {
        for j := 0 ;j <= m; j ++ {
            G[i][j] = 0
            math[j] = -1
            used[j] = 0
        }
    }
    for i := 1;i <= n; i ++ {

        for j := 1;j <= m; j ++ {

            if abs(A[i] - B[j]) <= 1 {
                G[i][j] = 1
            }
        }
    }
    ret := 0
    for i := 1;i <= n; i ++ {
        for j := 1;j <= m;j ++ {
            used[j] = 0
        }
        if Find(i) {
            ret ++
        }
    }
    return ret
}

func main(){

    reader := bufio.NewReader(os.Stdin)
    fmt.Fscan(reader,&n)
    for i := 1; i<= n;i ++ {
        fmt.Fscan(reader,&A[i])
    }
    fmt.Fscan(reader,&m)
    for i := 1;i <= m; i ++ {
        fmt.Fscan(reader,&B[i])
    }
    ans := build()
    fmt.Printf("%d\n",ans)

}

package main

import (
	"fmt"
	"math"
	"sort"
)

type mySlice []int

func (a mySlice) Len() int           { return len(a) }
func (a mySlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a mySlice) Less(i, j int) bool { return a[i] < a[j] }

func main() {
	var nBoys, nGirls int
	fmt.Scan(&nBoys)
	boys := make(mySlice, nBoys)
	for i := 0; i < nBoys; i++ {
		fmt.Scan(&boys[i])
	}
	fmt.Scan(&nGirls)
	girls := make(mySlice, nGirls)
	for i := 0; i < nGirls; i++ {
		fmt.Scan(&girls[i])
	}

	cont := 0
	sort.Sort(boys)
	sort.Sort(girls)

	for _, boy := range boys {
		for j, girl := range girls {
			if math.Abs(float64(boy)-float64(girl)) < 2 {
				cont++
				girls[j] = -1000
				break
			}
		}
	}

	fmt.Print(cont)
}

package main

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

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

type IntSorter struct {
	data []int
}
func (s *IntSorter) Len() int { return len(s.data) }
func (s *IntSorter) Swap(i, j int) { s.data[i], s.data[j] = s.data[j], s.data[i] }
func (s *IntSorter) Less(i, j int) bool { return s.data[i] < s.data[j] }

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

	N := ReadInt32()
	boys := make([]int, N)
	for i := 0; i < N; i += 1 {
		boys[i] = ReadInt32()
	}

	M := ReadInt32()
	girls := make([]int, M)
	for i := 0; i < M; i += 1 {
		girls[i] = ReadInt32()
	}

	bSorter := &IntSorter{ boys }
	gSorter := &IntSorter{ girls }
	sort.Sort(bSorter)
	sort.Sort(gSorter)
	boys, girls = bSorter.data, gSorter.data

	bi, gi := 0, 0
	matches := 0
	for bi < N && gi < M {
		if boys[bi] == girls[gi] || boys[bi] - girls[gi] == -1 || boys[bi] - girls[gi] == 1 {
			matches++
			bi++
			gi++
			continue
		}
		if boys[bi] > girls[gi] {
			gi++
		} else {
			bi++
		}
	}
	PrintInts(matches)
}

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

func PrintInts(ints ...int) {
	for _, value := range ints {
		writer.WriteString(strconv.Itoa(value))
		writer.WriteByte(' ')
	}
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n, m int
	fmt.Scan(&n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}
	fmt.Scan(&m)
	b := make([]int, m)
	for i := 0; i < m; i++ {
		fmt.Scan(&b[i])
	}
	sort.Ints(a)
	sort.Ints(b)
	res := 0
	for i, j := 0, 0; i < n && j < m; {
		switch {
		case a[i]+1 < b[j]:
			i++
		case b[j]+1 < a[i]:
			j++
		default:
			res++
			i++
			j++
		}
	}
	fmt.Println(res)
}

package main

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

var reader *bufio.Reader

func init() {
	stdin := os.Stdin
	// stdin, _ = os.Open("1.in")
	reader = bufio.NewReaderSize(stdin, 1<<20)
}

var n, m int
var a, b []int

func main() {
	fmt.Fscan(reader, &n)
	a = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i])
	}
	fmt.Fscan(reader, &m)
	b = make([]int, m)
	for i := 0; i < m; i++ {
		fmt.Fscan(reader, &b[i])
	}
	var bipartite Bipartite
	bipartite.Init(n + m)
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if math.Abs(float64(a[i]-b[j])) <= 1 {
				bipartite.AddEdge(i, n+j)
			}
		}
	}
	fmt.Println(bipartite.BipartiteMatching())
}

// Bipartite 二分图
type Bipartite struct {
	V     int
	G     [][]int
	Match []int
	Used  []bool
}

// Init 初始化数组
func (r *Bipartite) Init(n int) {
	r.V = n
	r.G = make([][]int, n)
	r.Match = make([]int, n)
	r.Used = make([]bool, n)
}

// AddEdge 向图中添加一条连接u，v的边
func (r *Bipartite) AddEdge(u, v int) {
	r.G[u] = append(r.G[u], v)
	r.G[v] = append(r.G[v], u)
}

// DFS 通过DFS寻找增广路
func (r *Bipartite) DFS(v int) bool {
	r.Used[v] = true
	for i := 0; i < len(r.G[v]); i++ {
		u := r.G[v][i]
		w := r.Match[u]
		if w < 0 || !r.Used[w] && r.DFS(w) {
			r.Match[v] = u
			r.Match[u] = v
			return true
		}
	}
	return false
}

// BipartiteMatching 求解二分图的最大匹配
func (r *Bipartite) BipartiteMatching() (res int) {
	for i := 0; i < len(r.Match); i++ {
		r.Match[i] = -1
	}
	for v := 0; v < r.V; v++ {
		if r.Match[v] < 0 {
			for i := 0; i < len(r.Used); i++ {
				r.Used[i] = false
			}
			if r.DFS(v) {
				res++
			}
		}
	}
	return
}

package main

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

func Readln(r *bufio.Reader) (string, error) {
	var (
		isPrefix bool  = true
		err      error = nil
		line, ln []byte
	)
	for isPrefix && err == nil {
		line, isPrefix, err = r.ReadLine()
		ln = append(ln, line...)
	}
	return string(ln), err
}

func readIntArray(line string) []int {

	numStrings := strings.Split(strings.TrimSpace(string(line)), " ")

	numbers := make([]int, 0)

	for _, v := range numStrings {
		var converted int

		fmt.Sscanf(v, "%d", &converted)
		numbers = append(numbers, converted)
	}

	return numbers
}

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

	Readln(reader)
	line, _ := Readln(reader)
	boys := readIntArray(line)

	Readln(reader)
	line, _ = Readln(reader)
	girls := readIntArray(line)

	count := 0
	sort.Ints(boys)
	sort.Ints(girls)
	for i := 0; i < len(boys); i++ {
		for j := 0; j < len(girls); j++ {
			if int(math.Abs(float64(boys[i] - girls[j]))) <= 1 {
				girls[j] = math.MaxInt32
				count++
				break
			}
		}
	}

	fmt.Println(count)
}

package main

import (
	"fmt"
	"sort"
)

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

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

	b := make([]int, m)
	for i := 0; i < m; i++ {
		fmt.Scan(&b[i])
	}

	sort.Ints(a)
	sort.Ints(b)

	p := -1

	for i := 0; i < len(a); i++ {
		for j := p + 1; j < len(b); j++ {
			difference := Abs(a[i] - b[j])
			if difference <= 1 {
				p = j
				res++
				break
			}
		}
	}
	fmt.Print(res)
}

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

package main

import (
	"fmt"
	"sort"
)

const paired = 10e3 // > problem upper bound value

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

func main() {
	n, m := 0, 0
	ans := 0
	fmt.Scanf("%d\n", &n)
	as := make([]int, n)
	for i := range as {
		fmt.Scanf("%d", &as[i])
	}
	fmt.Scanf("\n%d\n", &m)
	bs := make([]int, m)
	for i := range bs {
		fmt.Scanf("%d", &bs[i])
	}
	sort.Ints(as)
	sort.Ints(bs)
	for _, a := range as {
		for j, b := range bs {
			// skill gap
			if abs(a-b) <= 1 {
				bs[j] = paired
				ans++
				// pair formed -> next
				break
			}
		}
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
	"sort"
)

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

	fmt.Scan(&n)

	a := make([]int, n)

	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}

	fmt.Scan(&m)

	b := make([]int, m)

	for i := 0; i < m; i++ {
		fmt.Scan(&b[i])
	}

	sort.Ints(a)
	sort.Ints(b)

	var p int

	for i := 0; i < len(a); i++ {
		for j := p + 1; j < len(b); j++ {
			if a[i] == b[j] || a[i]+1 == b[j] || a[i]-1 == b[j] {
				p = j
				res++
				break
			}
		}
	}

	fmt.Println(res)
}

package main

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

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

type IntSorter struct {
	data []int
}
func (s *IntSorter) Len() int { return len(s.data) }
func (s *IntSorter) Swap(i, j int) { s.data[i], s.data[j] = s.data[j], s.data[i] }
func (s *IntSorter) Less(i, j int) bool { return s.data[i] < s.data[j] }

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

	N := ReadInt32()
	boys := make([]int, N)
	for i := 0; i < N; i += 1 {
		boys[i] = ReadInt32()
	}

	M := ReadInt32()
	girls := make([]int, M)
	for i := 0; i < M; i += 1 {
		girls[i] = ReadInt32()
	}

	bSorter := &IntSorter{ boys }
	gSorter := &IntSorter{ girls }
	sort.Sort(bSorter)
	sort.Sort(gSorter)
	boys, girls = bSorter.data, gSorter.data

	bi, gi := 0, 0
	matches := 0
	for bi < N && gi < M {
		if boys[bi] == girls[gi] || boys[bi] - girls[gi] == -1 || boys[bi] - girls[gi] == -1 {
			matches++
			bi++
			gi++
			continue
		}
		if boys[bi] > girls[gi] {
			gi++
		} else {
			bi++
		}
	}
	PrintInts(matches)
}

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

func PrintInts(ints ...int) {
	for _, value := range ints {
		writer.WriteString(strconv.Itoa(value))
		writer.WriteByte(' ')
	}
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n, m int
	fmt.Scanf("%d\n", &n)
	a := intArray(n)
	sort.Ints(a[:])
	fmt.Scanf("%d\n", &m)
	b := intArray(m)
	sort.Ints(b[:])
	c := 0
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			difference := Abs(a[i] - b[j])
			if difference <= 1 {
				b[j] = 1000
				c++
				break
			}
		}
	}
	fmt.Println(c)
}

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

func intArray(n int) []int {
	b := make([]int, n)
	for iter := 0; iter < n; iter++ {
		fmt.Scanf("%d", &b[iter])
	}
	return b
}

package main

import (
	"fmt"
	"sort"
)

const paired = 10e3 // > problem upper bound value

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

func main() {
	n, m := 0, 0
	ans := 0
	fmt.Scan(&n)
	as := make([]int, n)
	for i := range as {
		fmt.Scanf("%d", &as[i])
	}
	fmt.Scan(&m)
	bs := make([]int, m)
	for i := range bs {
		fmt.Scanf("%d", &bs[i])
	}
	sort.Ints(as)
	sort.Ints(bs)
	for _, a := range as {
		for j, b := range bs {
			// skill gap
			if abs(a-b) <= 1 {
				bs[j] = paired
				ans++
				// pair formed -> next
				break
			}
		}
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
	"sort"
)

const paired = 10e3 // > problem upper bound value

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

func main() {
	n, m := 0, 0
	ans := 0
	fmt.Scanf("%d", &n)
	as := make([]int, n)
	for i := range as {
		fmt.Scanf("%d", &as[i])
	}
	fmt.Scanf("%d", &m)
	bs := make([]int, m)
	for i := range bs {
		fmt.Scanf("%d", &bs[i])
	}
	sort.Ints(as)
	sort.Ints(bs)
	for _, a := range as {
		for j, b := range bs {
			// skill gap
			if abs(a-b) <= 1 {
				bs[j] = paired
				ans++
				// pair formed -> next
				break
			}
		}
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
)

const paired = 10e3 // > problem upper bound value

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

func main() {
	n, m := 0, 0
	ans := 0
	fmt.Scanf("%d\n", &n)
	as := make([]int, n)
	for i := range as {
		fmt.Scanf("%d", &as[i])
	}
	fmt.Scanf("\n%d\n", &m)
	bs := make([]int, m)
	for i := range bs {
		fmt.Scanf("%d", &bs[i])
	}
	fmt.Println(n, m, as, bs)
	for _, a := range as {
		for j, b := range bs {
			// skill gap
			if abs(a-b) <= 1 {
				bs[j] = paired
				ans++
				// pair formed -> next
				break
			}
		}
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
	"sort"
)

const paired = 10e3 // > problem upper bound value

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

func main() {
	n, m := 0, 0
	ans := 0
	fmt.Scan(&n)
	as := make([]int, n)
	for i := range as {
		fmt.Scanf("%d", &as[i])
	}
	fmt.Scan(&m)
	bs := make([]int, m)
	for i := range bs {
		fmt.Scanf("%d", &bs[i])
	}
	fmt.Println(n, m, as, bs)
	sort.Ints(as)
	sort.Ints(bs)
	for _, a := range as {
		for j, b := range bs {
			// skill gap
			if abs(a-b) <= 1 {
				bs[j] = paired
				ans++
				// pair formed -> next
				break
			}
		}
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
	"sort"
)

const paired = 10e3 // > problem upper bound value

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

func main() {
	n, m := 0, 0
	ans := 0
	fmt.Scanf("%d\n", &n)
	as := make([]int, n)
	for i := range as {
		fmt.Scanf("%d", &as[i])
	}
	fmt.Scanf("%d\n", &m)
	bs := make([]int, m)
	for i := range bs {
		fmt.Scanf("%d", &bs[i])
	}
	sort.Ints(as)
	sort.Ints(bs)
	for _, a := range as {
		for j, b := range bs {
			// skill gap
			if abs(a-b) <= 1 {
				bs[j] = paired
				ans++
				// pair formed -> next
				break
			}
		}
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
)

const paired = 10e3 // > problem upper bound value

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

func main() {
	n, m := 0, 0
	ans := 0
	fmt.Scanf("%d\n", &n)
	as := make([]int, n)
	for i := range as {
		fmt.Scanf("%d\n", &as[i])
	}
	fmt.Scanf("%d\n", &m)
	bs := make([]int, m)
	for i := range bs {
		fmt.Scanf("%d\n", &bs[i])
	}
	for _, a := range as {
		for j, b := range bs {
			// skill gap
			if abs(a-b) <= 1 {
				bs[j] = paired
				ans++
				// pair formed -> next
				break
			}
		}
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
)

const paired = 10e3 // > problem upper bound value

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

func main() {
	n, m := 0, 0
	ans := 0
	fmt.Scanf("%d\n", &n)
	as := make([]int, n)
	for i := range as {
		fmt.Scanf("%d", &as[i])
	}
	fmt.Scanf("%d\n", &m)
	bs := make([]int, m)
	for i := range bs {
		fmt.Scanf("%d", &bs[i])
	}
	for _, a := range as {
		for j, b := range bs {
			// skill gap
			if abs(a-b) <= 1 {
				bs[j] = paired
				// pair formed -> next
				ans++
				break
			}
		}
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
)

const paired = 10e3 // > problem upper bound value

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

func main() {
	n, m := 0, 0
	ans := 0
	fmt.Scanf("%d\n", &n)
	as := make([]int, n)
	for i := range as {
		fmt.Scanf("%d", &as[i])
	}
	fmt.Scanf("\n%d\n", &m)
	bs := make([]int, m)
	for i := range bs {
		fmt.Scanf("%d", &bs[i])
	}
	for _, a := range as {
		for j, b := range bs {
			// skill gap
			if abs(a-b) <= 1 {
				bs[j] = paired
				ans++
				// pair formed -> next
				break
			}
		}
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
)

const paired = 10e3 // > problem upper bound value

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

func main() {
	n, m := 0, 0
	ans := 0
	fmt.Scanf("%d\n", &n)
	as := make([]int, n)
	for i := range as {
		fmt.Scanf("%d", &as[i])
	}
	fmt.Scanf("%d\n", &m)
	bs := make([]int, m)
	for i := range bs {
		fmt.Scanf("%d", &bs[i])
	}
	for _, a := range as {
		for j, b := range bs {
			// skill gap
			if abs(a-b) <= 1 {
				bs[j] = paired
				ans++
				// pair formed -> next
				break
			}
		}
	}
	fmt.Println(ans)
}


package main

import "fmt"

func main() {
	var r, s, p int
	fmt.Scanf("%d %d %d", &r, &s, &p)

	var prob = make([]float64, (r+1) * (s+1) * (p+1))

	var set = func (rx int, sx int, px int, val float64) {
		prob[rx * (s+1) * (p+1) + sx * (p+1) + px] = val
	}
	var get = func (rx int, sx int, px int) float64 {
		return prob[rx * (s+1) * (p+1) + sx * (p+1) + px]
	}

	for i := r; i >= 0; i-- {
		for j := s; j >= 0; j-- {
			for k := p; k >= 0; k-- {
				if i == r && j == s && k == p {
					// Initial condition - the probability is 1.
					set(i, j, k, 1.0)
				} else {
					// The probability of getting into state (i, j, k) is the sum of
					// 1) P(i+1, j, k) * (P(an "i" loses)
					//    where
					//      P(an "i" loses) is (i+1)/(i+1+j+k))
					// 2) and 3) symmetrically for j and k.
					pp := 0.0
					if i != r && (j != 0 || k != 0) {
						pp += get(i+1, j, k) * float64((i+1) * k) / float64((i+1) * j + j * k + k * (i+1))
					}
					if j != s && (i != 0 || k != 0) {
						pp += get(i, j+1, k) * float64(i * (j+1)) / float64(i * (j+1) + (j+1) * k + k * i)
					}
					if k != p && (i != 0 || j != 0) {
						pp += get(i, j, k+1) * float64(j * (k+1)) / float64(i * j + j * (k+1) + (k+1) * i)
					}
					set(i, j, k, pp)
				}
	//			fmt.Printf("%02d %02d %02d %12.5f\n", i, j, k, get(i, j, k))
			}
		}
	}
	var ans_r = 0.0
	for i := r; i >= 1; i-- {
		ans_r += get(i, 0, 0)
	}
	var ans_s = 0.0
	for i := s; i >= 1; i-- {
		ans_s += get(0, i, 0)
	}
	var ans_p = 0.0
	for i := p; i >= 1; i-- {
		ans_p += get(0, 0, i)
	}
	fmt.Printf("%14.12g %14.12g %14.12g\n", ans_r, ans_s, ans_p)
}

package main

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

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

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

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

  r, s, p := getI(), getI(), getI()
  g := make([][][]float64, r+1)
  m := make([][][]bool, r+1)
  for i := 0; i <= r; i++ {
    g[i] = make([][]float64, s+1)
    m[i] = make([][]bool, s+1)
    for j := 0; j <= s; j++ {
      g[i][j] = make([]float64, p+1)
      m[i][j] = make([]bool, p+1)
    }
  }
  g[r][s][p] = 1
  queue := [][]int{ []int{ r, s, p } }
  tail := 0
  for tail != len(queue) {
    r, s, p := queue[tail][0], queue[tail][1], queue[tail][2]
    tail++
    curr := g[r][s][p]
    space := float64(r*s + s*p + p*r)
    if space == 0.0 {
      continue
    }
    if r > 0 {
      g[r-1][s][p] += curr * float64(p*r)/space
      if !m[r-1][s][p] {
        m[r-1][s][p] = true
        queue = append(queue, []int{ r-1, s, p })
      }
    }
    if s > 0 {
      g[r][s-1][p] += curr * float64(r*s)/space
      if !m[r][s-1][p] {
        m[r][s-1][p] = true
        queue = append(queue, []int{ r, s-1, p })
      }
    }
    if p > 0 {
      g[r][s][p-1] += curr * float64(s*p)/space
      if !m[r][s][p-1] {
        m[r][s][p-1] = true
        queue = append(queue, []int{ r, s, p-1 })
      }
    }
  }
  R, S, P := 0.0, 0.0, 0.0
  for i := 1; i <= r; i++ {
    R += g[i][0][0]
  }
  for i := 1; i <= s; i++ {
    S += g[0][i][0]
  }
  for i := 1; i <= p; i++ {
    P += g[0][0][i]
  }
  writer.WriteString(fmt.Sprintf("%.12f %.12f %.12f\n", R, S, P))
}

package main

import "fmt"

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

	dp := make([][][]float64, n+1)
	for i, _ := range dp {
		dp[i] = make([][]float64, m+1)
		for j, _ := range dp[i] {
			dp[i][j] = make([]float64, k+1)
		}
	}

	dp[n][m][k] = 1

	for r := n; r >= 0; r-- {
		for p := m; p >= 0; p-- {
			for s := k; s >= 0; s-- {
				deno := float64(r*p + p*s + r*s)

				if deno == 0 {
					continue
				}

				if r > 0 {
					dp[r-1][p][s] += dp[r][p][s] * float64(r*p) / deno
				}
				if p > 0 {
					dp[r][p-1][s] += dp[r][p][s] * float64(s*p) / deno
				}
				if s > 0 {
					dp[r][p][s-1] += dp[r][p][s] * float64(r*s) / deno
				}
			}
		}
	}

	var proba1, proba2, proba3 float64
	for i := 0; i <= n; i++ {
		proba1 += dp[i][0][0]
	}

	for i := 0; i <= m; i++ {
		proba2 += dp[0][i][0]
	}

	for i := 0; i <= k; i++ {
		proba3 += dp[0][0][i]
	}

	fmt.Printf("%.15f %.15f %.15f", proba1, proba3, proba2)
}

package main

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

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

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

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

  r, s, p := getI(), getI(), getI()
  g := make([][][]float64, r+1)
  m := make([][][]bool, r+1)
  for i := 0; i <= r; i++ {
    g[i] = make([][]float64, s+1)
    m[i] = make([][]bool, s+1)
    for j := 0; j <= s; j++ {
      g[i][j] = make([]float64, p+1)
      m[i][j] = make([]bool, p+1)
    }
  }
  g[r][s][p] = 1
  queue := [][]int{ []int{ r, s, p } }
  tail := 0
  for tail != len(queue) {
    r, s, p := queue[tail][0], queue[tail][1], queue[tail][2]
    tail++
    curr := g[r][s][p]
    space := float64(r*s + s*p + p*r)
    if space == 0.0 {
      continue
    }
    if r > 0 {
      g[r-1][s][p] += curr * float64(p*r)/space
      if !m[r-1][s][p] {
        m[r-1][s][p] = true
        queue = append(queue, []int{ r-1, s, p })
      }
    }
    if s > 0 {
      g[r][s-1][p] += curr * float64(r*s)/space
      if !m[r][s-1][p] {
        m[r][s-1][p] = true
        queue = append(queue, []int{ r, s-1, p })
      }
    }
    if p > 0 {
      g[r][s][p-1] += curr * float64(s*p)/space
      if !m[r][s][p-1] {
        m[r][s][p-1] = true
        queue = append(queue, []int{ r, s, p-1 })
      }
    }
  }
  R, S, P := 0.0, 0.0, 0.0
  for i := 1; i <= r; i++ {
    R += g[i][0][0]
  }
  for i := 1; i <= s; i++ {
    S += g[0][i][0]
  }
  for i := 1; i <= p; i++ {
    P += g[0][0][i]
  }
  writer.WriteString(fmt.Sprintf("%f %f %f\n", R, S, P))
}

package main

import "fmt"

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

	dp := make([][][]float32, n+1)
	for i, _ := range dp {
		dp[i] = make([][]float32, m+1)
		for j, _ := range dp {
			dp[i][j] = make([]float32, k+1)
		}
	}

	dp[n][m][k] = 1

	for r := n; r >= 0; r-- {
		for p := m; p >= 0; p-- {
			for s := k; s >= 0; s-- {
				deno := float32(r*p + p*s + r*s)

				if deno == 0 {
					continue
				}

				if r > 0 {
					dp[r-1][p][s] += dp[r][p][s] * float32(r*p) / deno
				}
				if p > 0 {
					dp[r][p-1][s] += dp[r][p][s] * float32(s*p) / deno
				}
				if s > 0 {
					dp[r][p][s-1] += dp[r][p][s] * float32(r*s) / deno
				}
			}
		}
	}

	var proba1, proba2, proba3 float32
	for i := 0; i <= n; i++ {
		proba1 += dp[i][0][0]
	}

	for i := 0; i <= m; i++ {
		proba2 += dp[0][i][0]
	}

	for i := 0; i <= k; i++ {
		proba3 += dp[0][0][i]
	}

	fmt.Printf("%.9f %.9f %.9f", proba1, proba3, proba2)
}


package main

import (
	"bufio"
	"fmt"
	"io"
	"math/rand"
	"os"
)

var rw = NewFastIO(os.Stdin, os.Stdout)

func main() {
	defer rw.Flush()

	n := rw.NextInt()
	a := rw.NextInts(n)
	var nums [3][]uint64
	for j := 0; j < n; j++ {
		nums[0] = append(nums[0], rand.Uint64())
		nums[1] = append(nums[1], rand.Uint64())
		nums[2] = append(nums[2], nums[0][j]^nums[1][j])
	}
	cnts := map[uint64]int{0: 1}
	pref := make([]uint64, n+1)
	ctr := make([]int, n)
	frq := make([]int, n)
	var ans int64
	for i, j := 0, 0; i < n; i++ {
		a[i]--
		pref[i+1] = pref[i] ^ nums[ctr[a[i]]%3][a[i]]
		ctr[a[i]]++
		frq[a[i]]++
		for frq[a[i]] > 3 {
			frq[a[j]]--
			cnts[pref[j]]--
			j++
		}
		ans += int64(cnts[pref[i+1]])
		cnts[pref[i+1]]++
	}
	rw.Println(ans)
}

// @meooow template uwu
type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, c)
	}
	return string(s)
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}
func (f *FastIO) NextInt64() int64 {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := int64(0)
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int64(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}
func (f *FastIO) NextInt64s(n int) []int64 {
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt64()
	}
	return a
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f, a...)
}
func (f *FastIO) PrintInts(a ...int) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}
func (f *FastIO) PrintInt64s(a ...int64) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInt64s(a ...int64) {
	f.PrintInt64s(a...)
	f.Println()
}

package main

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

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

	n := readNum(reader)

	A := readNNums(reader, n)

	fmt.Println(solve(n, A))
}

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
}

var r *rand.Rand

func init() {
	r = rand.New(rand.NewSource(99))
}

func solve(n int, A []int) int64 {
	hashes := make([]uint64, n+1)
	indices := make([][]int, n+1)
	freq := make([]uint64, n+1)
	hashValues := make([]uint64, n+1)
	hashFreq := make(map[uint64]int)
	for i := 1; i <= n; i++ {
		hashValues[i] = r.Uint64()
		indices[i] = make([]int, 0, 4)
	}
	hashFreq[0]++
	var start int
	var ans int64
	for i := 0; i < n; i++ {
		cur := A[i]
		if len(indices[cur]) >= 3 {
			remove := indices[cur][0]
			indices[cur] = indices[cur][1:]
			for start <= remove {
				hashFreq[hashes[start]]--
				start++
			}
		}
		before := freq[cur]
		freq[cur] = (before + 1) % 3
		hashes[i+1] = hashes[i] + (freq[cur]-before)*hashValues[cur]
		ans += int64(hashFreq[hashes[i+1]])
		hashFreq[hashes[i+1]]++
		indices[cur] = append(indices[cur], i)
	}

	return ans
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"math/rand"
	"os"
)

var rw = NewFastIO(os.Stdin, os.Stdout)

const M = 1

func main() {
	defer rw.Flush()

	n := rw.NextInt()
	a := rw.NextInts(n)
	var cnts [M]map[uint64]int
	var nums [M][3][]uint64
	for i := range cnts {
		cnts[i] = make(map[uint64]int)
		cnts[i][0] = 1
		for j := 0; j < n; j++ {
			nums[i][0] = append(nums[i][0], rand.Uint64())
			nums[i][1] = append(nums[i][1], rand.Uint64())
			nums[i][2] = append(nums[i][2], nums[i][0][j]^nums[i][1][j])
		}
	}
	pref := make([]uint64, n+1)
	ctr := make([]int, n)
	frq := make([]int, n)
	var ans int64
	for i, j := 0, 0; i < n; i++ {
		a[i]--
		pref[i+1] = pref[i] ^ nums[0][ctr[a[i]]%3][a[i]]
		ctr[a[i]]++
		frq[a[i]]++
		for frq[a[i]] > 3 {
			frq[a[j]]--
			cnts[0][pref[j]]--
			j++
		}
		ans += int64(cnts[0][pref[i+1]])
		cnts[0][pref[i+1]]++
	}
	rw.Println(ans)
}

// @meooow template uwu
type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, c)
	}
	return string(s)
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}
func (f *FastIO) NextInt64() int64 {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := int64(0)
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int64(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}
func (f *FastIO) NextInt64s(n int) []int64 {
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt64()
	}
	return a
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f, a...)
}
func (f *FastIO) PrintInts(a ...int) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}
func (f *FastIO) PrintInt64s(a ...int64) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInt64s(a ...int64) {
	f.PrintInt64s(a...)
	f.Println()
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"math/rand"
	"os"
)

var rw = NewFastIO(os.Stdin, os.Stdout)

func main() {
	defer rw.Flush()
	n := rw.NextInt()
	a := rw.NextInts(n)
	var nums [3][]uint32
	for j := 0; j < n; j++ {
		nums[0] = append(nums[0], rand.Uint32())
		nums[1] = append(nums[1], rand.Uint32())
		nums[2] = append(nums[2], nums[0][j]^nums[1][j])
	}
	cnts := map[uint32]int{0: 1}
	pref := make([]uint32, n+1)
	ctr := make([]int, n)
	frq := make([]int, n)
	var ans int64
	for i, j := 0, 0; i < n; i++ {
		a[i]--
		pref[i+1] = pref[i] ^ nums[ctr[a[i]]%3][a[i]]
		ctr[a[i]]++
		frq[a[i]]++
		for frq[a[i]] > 3 {
			frq[a[j]]--
			cnts[pref[j]]--
			j++
		}
		ans += int64(cnts[pref[i+1]])
		cnts[pref[i+1]]++
	}
	rw.Println(ans)
}

// @meooow template uwu
type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, c)
	}
	return string(s)
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}
func (f *FastIO) NextInt64() int64 {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := int64(0)
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int64(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}
func (f *FastIO) NextInt64s(n int) []int64 {
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt64()
	}
	return a
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f, a...)
}
func (f *FastIO) PrintInts(a ...int) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}
func (f *FastIO) PrintInt64s(a ...int64) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInt64s(a ...int64) {
	f.PrintInt64s(a...)
	f.Println()
}


package main

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

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

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

func Dprint(format string, a ...interface{}) {
	if Debug {
		fmt.Fprintf(os.Stderr, format+"\n", a...)
	}
}

var (
	n, k int
	a    []int
	adj  [][]int

	mem [][]int64
)

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

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

func dp(vert int, dep int, prevVert int) (fRet int64) {
	if m := mem[vert][dep]; m != -1 {
		return m
	}
	defer func() {
		mem[vert][dep] = fRet
	}()

	var ret int64

	var chi []int
	for _, ch := range adj[vert] {
		if ch != prevVert {
			chi = append(chi, ch)
		}
	}

	if dep == 0 {
		ret = int64(a[vert])
		for _, ch := range chi {
			ret += dp(ch, k-1, vert)
		}
	} else {
		// We are calculating max(dp(ch0, b) + dp(ch1, a) + dp(ch2, a) + dp(ch3, a)) for all ch0, ch1, ...; ch0 != ch1 != ch2 ...
		var cSum int64
		for _, ch := range chi {
			cSum += dp(ch, maxI(dep-1, k-dep-1), vert)
		}

		for _, ch := range chi {
			ret = maxI64(ret,
				cSum-
					dp(ch, maxI(dep-1, k-dep-1), vert)+
					dp(ch, dep-1, vert),
			)
		}
	}

	for nDep := dep + 1; nDep <= k; nDep++ {
		ret = maxI64(ret, dp(vert, nDep, prevVert))
	}
	return ret
}

func main() {
	defer writer.Flush()

	Scanf("%d %d\n", &n, &k)
	k++

	a = make([]int, n)
	for i := 0; i < n; i++ {
		Scanf("%d", &a[i])
	}
	Scanf("\n")

	adj = make([][]int, n)
	for i := 0; i < n-1; i++ {
		var from, to int
		Scanf("%d %d\n", &from, &to)
		from--
		to--
		adj[from] = append(adj[from], to)
		adj[to] = append(adj[to], from)
	}

	mem = make([][]int64, n)
	for i := 0; i < n; i++ {
		mem[i] = make([]int64, k+1)
		for j := 0; j <= k; j++ {
			mem[i][j] = -1
		}
	}

	Printf("%d\n", dp(0, 0, 0))
}

package main

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

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

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

func Dprint(format string, a ...interface{}) {
	if Debug {
		fmt.Fprintf(os.Stderr, format+"\n", a...)
	}
}

var (
	n, k int
	a    []int
	adj  [][]int

	mem [][]int64
)

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

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

func dp(vert int, dep int, prevVert int) (fRet int64) {
	if m := mem[vert][dep]; m != -1 {
		return m
	}
	defer func() {
		mem[vert][dep] = fRet
	}()

	var ret int64

	var chi []int
	for _, ch := range adj[vert] {
		if ch != prevVert {
			chi = append(chi, ch)
		}
	}

	if dep == 0 {
		ret = int64(a[vert])
		for _, ch := range chi {
			ret += dp(ch, k-1, vert)
		}
	} else {
		for _, ch1 := range chi {
			iRet := int64(dp(ch1, dep-1, vert))
			for _, ch2 := range chi {
				if ch2 == ch1 {
					continue
				}
				iRet += dp(ch2, maxI(dep-1, k-dep-1), vert)
			}
			ret = maxI64(ret, iRet)
		}
	}

	for nDep := dep + 1; nDep <= k; nDep++ {
		ret = maxI64(ret, dp(vert, nDep, prevVert))
	}
	return ret
}

func main() {
	defer writer.Flush()

	Scanf("%d %d\n", &n, &k)
	k++

	a = make([]int, n)
	for i := 0; i < n; i++ {
		Scanf("%d", &a[i])
	}
	Scanf("\n")

	adj = make([][]int, n)
	for i := 0; i < n-1; i++ {
		var from, to int
		Scanf("%d %d\n", &from, &to)
		from--
		to--
		adj[from] = append(adj[from], to)
		adj[to] = append(adj[to], from)
	}

	Dprint("adj=%v", adj)

	mem = make([][]int64, n)
	for i := 0; i < n; i++ {
		mem[i] = make([]int64, k+1)
		for j := 0; j <= k; j++ {
			mem[i][j] = -1
		}
	}

	Printf("%d\n", dp(0, 0, 0))
	Dprint("mem=%v", mem)
}

package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
)

func min(x, y int) int {
	if x < y {
		return x
	}

	return y
}

func max(x, y int) int {
	if x > y {
		return x
	}

	return y
}

func dfs(c, p, k int) {
	if g[c] == nil {
		return
	}

	for e := g[c].Front(); e != nil; e = e.Next() {
		next := e.Value.(int)
		if next == p {
			continue
		}

		dfs(next, c, k)

		//fmt.Println(c, next, "before", dp[c][:k+1])

		for i := 0; i <= k; i++ {
			temp[i] = dp[c][i]
		}

		for i := 0; i <= k; i++ {
			for j := 0; j <= k; j++ {
				if i+j+1 > k {
					m := min(i, j+1)
					temp[m] = max(temp[m], dp[c][i]+dp[next][j])
				}
			}
		}

		for i := 0; i <= k; i++ {
			dp[c][i] = temp[i]
		}

		//fmt.Println(c, next, "after", dp[c][:k+1])
	}
}

var g [201]*list.List
var dp [201][201]int
var temp [201]int

func main() {

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

	fmt.Fscanf(reader, "%d %d\n", &n, &k)

	for i := 1; i <= n; i++ {
		if i != n {
			fmt.Fscanf(reader, "%d", &dp[i][0])
		} else {
			fmt.Fscanf(reader, "%d\n", &dp[i][0])
		}
	}

	x, y := 0, 0
	for i := 1; i < n; i++ {
		fmt.Fscanf(reader, "%d %d\n", &x, &y)

		if g[x] == nil {
			g[x] = list.New()
		}

		if g[y] == nil {
			g[y] = list.New()
		}

		g[x].PushBack(y)
		g[y].PushBack(x)
	}

	dfs(1, 0, k)

	for i := 1; i <= k; i++ {
		if dp[1][i] > dp[1][0] {
			dp[1][0] = dp[1][i]
		}
	}

	fmt.Println(dp[1][0])
}