This is a hard version of the problem. The only difference between an easy and a hard version is in the number of queries.Polycarp grew a tree from $$$n$$$ vertices. We remind you that a tree of $$$n$$$ vertices is an undirected connected graph of $$$n$$$ vertices and $$$n-1$$$ edges that does not contain cycles.He calls a set of vertices passable if there is such a path in the tree that passes through each vertex of this set without passing through any edge twice. The path can visit other vertices (not from this set).In other words, a set of vertices is called passable if there is a simple path that passes through all the vertices of this set (and possibly some other).For example, for a tree below sets $$$\{3, 2, 5\}$$$, $$$\{1, 5, 4\}$$$, $$$\{1, 4\}$$$ are passable, and $$$\{1, 3, 5\}$$$, $$$\{1, 2, 3, 4, 5\}$$$ are not.  Polycarp asks you to answer $$$q$$$ queries. Each query is a set of vertices. For each query, you need to determine whether the corresponding set of vertices is passable.

The first line of input contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — number of vertices. Following $$$n - 1$$$ lines a description of the tree.. Each line contains two integers $$$u$$$ and $$$v$$$ ($$$1 \le u, v \le n$$$, $$$u \ne v$$$) — indices of vertices connected by an edge. Following line contains single integer $$$q$$$ ($$$1 \le q \le 10^5$$$) — number of queries. The following $$$2 \cdot q$$$ lines contain descriptions of sets. The first line of the description contains an integer $$$k$$$ ($$$1 \le k \le n$$$) — the size of the set. The second line of the description contains $$$k$$$ of distinct integers $$$p_1, p_2, \dots, p_k$$$ ($$$1 \le p_i \le n$$$) — indices of the vertices of the set. It is guaranteed that the sum of $$$k$$$ values for all queries does not exceed $$$2 \cdot 10^5$$$.

Output $$$q$$$ lines, each of which contains the answer to the corresponding query. As an answer, output "YES" if the set is passable, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).

package main

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

func req(dist func(v, u int) int) bool {
	k := get()
	//fmt.Fprintf(out, "k = %d\n", k)

	if k == 1 {
		scan()
		return true
	}

	l := get() - 1
	r := get() - 1
	k -= 2

	lr := dist(l, r)
	//fmt.Fprintf(out, "lr = %d\n", lr)

	for k > 0 {
		v := get() - 1
		k--

		lv := dist(l, v)
		rv := dist(r, v)
		//fmt.Fprintf(out, "lv = %d, rv = %d\n", lv, rv)

		if lv+rv == lr {
			continue
		}

		if lv > rv {
			l, r = r, l
			lv, rv = rv, lv
		}

		if lv+lr != rv {
			scanN(k)
			return false
		}

		l = v
		lr = rv
	}

	return true
}

func solve() {
	n := get()
	a := make([][]int, n)
	for t := n - 1; t > 0; t-- {
		v := get() - 1
		u := get() - 1
		a[v] = append(a[v], u)
		a[u] = append(a[u], v)
	}

	root := 0
	for len(a[root]) > 1 {
		root++
	}

	lca := make([][]int, n)
	depth := make([]int, n)
	{
		q := []int{root}
		for curDepth := 1; len(q) > 0; curDepth++ {
			temp := q
			q = nil
			for _, v := range temp {
				for _, u := range a[v] {
					if u == root || depth[u] > 0 {
						continue
					}
					depth[u] = curDepth

					lca[u] = append(lca[u], v)
					cur := lca[v]
					i := 0
					for i < len(cur) {
						k := cur[i]
						lca[u] = append(lca[u], k)
						cur = lca[k]
						i++
					}

					q = append(q, u)
				}
			}
		}
	}

	q := get()
	for q > 0 {
		yn(req(func(v, u int) int {
			du, dv := depth[u], depth[v]
			if dv < du {
				v, u = u, v
				dv, du = du, dv
			}

			if dv != du {
				for {
					i := 0
					for i < len(lca[v]) && depth[lca[v][i]] > du {
						i++
					}
					if i < len(lca[v]) && depth[lca[v][i]] == du {
						v = lca[v][i]
						break
					}
					v = lca[v][i-1]
				}
			}

			if v != u {
				for lca[v][0] != lca[u][0] {
					i := 0
					for i < len(lca[v]) && lca[v][i] != lca[u][i] {
						i++
					}
					i--

					v = lca[v][i]
					u = lca[u][i]
				}
				v = lca[v][0]
			}

			dc := depth[v]
			return (dv - dc) + (du - dc)
		}))

		q--
	}
}

const (
	once       = true
	maxBufSize = 10000000
)

var (
	splitFunc = bufio.ScanWords
)

func main() {
	in = bufio.NewScanner(bufio.NewReader(os.Stdin))
	in.Split(splitFunc)
	in.Buffer(make([]byte, maxBufSize), maxBufSize)

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

	if once {
		solve()
		return
	}

	t := get()
	for t > 0 {
		solve()
		t--
	}
}

var (
	in  *bufio.Scanner
	out *bufio.Writer
)

func get() int {
	x, _ := strconv.Atoi(gets())
	return x
}

func get64() int64 {
	x, _ := strconv.ParseInt(gets(), 10, 64)
	return x
}

func getf() float64 {
	x, _ := strconv.ParseFloat(gets(), 64)
	return x
}

func gets() string {
	scan()
	return in.Text()
}

func scanN(n int) {
	for n > 0 {
		scan()
		n--
	}
}

func scan() {
	in.Scan()
}

func putc(c rune) {
	out.WriteRune(c)
}

func put(x int) {
	out.WriteString(strconv.Itoa(x))
}

func put64(x int64) {
	out.WriteString(strconv.FormatInt(x, 10))
}

func puts(s string) {
	out.WriteString(s)
}

func yn(y bool) {
	if y {
		yes()
		return
	}
	no()
}

func yes() {
	puts("YES\n")
}

func no() {
	puts("NO\n")
}

func putf(x float64) {
	out.WriteString(strconv.FormatFloat(x, 'f', -1, 64))
}

func line() {
	out.WriteRune('\n')
}

func space() {
	out.WriteRune(' ')
}

package main

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

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

	var buf bytes.Buffer

	n := readNum(reader)
	edges := make([][]int, n-1)
	for i := 0; i < n-1; i++ {
		edges[i] = readNNums(reader, 2)
	}
	q := readNum(reader)
	queries := make([][]int, q)

	for i := 0; i < q; i++ {
		k := readNum(reader)
		queries[i] = readNNums(reader, k)
	}
	res := solve(n, edges, queries)
	for _, ans := range res {
		if ans {
			buf.WriteString("YES\n")
		} else {
			buf.WriteString("NO\n")
		}
	}

	fmt.Print(buf.String())
}

func readString(reader *bufio.Reader) string {
	s, _ := reader.ReadString('\n')
	for i := 0; i < len(s); i++ {
		if s[i] == '\n' || s[i] == '\r' {
			return s[:i]
		}
	}
	return s
}

func normalize(s string) string {

	for i := len(s); i > 0; i-- {
		if s[i-1] >= 'a' && s[i-1] <= 'z' {
			return s[:i]
		}
	}
	return ""
}

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
}

const H = 20

func solve(n int, edges [][]int, queries [][]int) []bool {

	g := NewGraph(n, 2*(n-1))

	for _, edge := range edges {
		u, v := edge[0]-1, edge[1]-1
		g.AddEdge(u, v)
		g.AddEdge(v, u)
	}

	D := make([]int, n)

	in := make([]int, n)
	out := make([]int, n)

	P := make([][]int, n)
	for i := 0; i < n; i++ {
		P[i] = make([]int, H)
	}

	var dfs func(p, u int)
	var time int
	dfs = func(p, u int) {
		P[u][0] = p
		for i := 1; i < H; i++ {
			P[u][i] = P[P[u][i-1]][i-1]
		}
		in[u] = time
		time++
		for i := g.node[u]; i > 0; i = g.next[i] {
			v := g.to[i]
			if p != v {
				D[v] = D[u] + 1
				dfs(u, v)
			}
		}
		out[u] = time
		time++
	}

	dfs(0, 0)

	isAncestor := func(u, v int) bool {
		return in[u] <= in[v] && out[v] <= out[u]
	}

	lca := func(u, v int) int {
		if D[u] < D[v] {
			u, v = v, u
		}
		for i := H - 1; i >= 0; i-- {
			if D[u]-(1<<i) >= D[v] {
				u = P[u][i]
			}
		}
		if u == v {
			return u
		}
		for i := H - 1; i >= 0; i-- {
			if P[u][i] != P[v][i] {
				u = P[u][i]
				v = P[v][i]
			}
		}
		return P[u][0]
	}

	arr := make([]int, n)

	check := func(query []int) bool {
		if len(query) <= 2 {
			return true
		}
		m := len(query)

		for i := 0; i < m; i++ {
			query[i]--
		}
		sort.Slice(query, func(i, j int) bool {
			a := query[i]
			b := query[j]
			return D[a] < D[b]
		})

		var it int

		x := query[m-1]

		for i := m - 2; i >= 0; i-- {
			y := query[i]
			if isAncestor(y, x) {
				x = y
			} else {
				arr[it] = y
				it++
			}
		}
		if it == 0 {
			return true
		}
		// last one is x
		u := arr[0]

		for i := 1; i < it; i++ {
			v := arr[i]
			if !isAncestor(v, u) {
				return false
			}
			u = v
		}

		p1 := lca(arr[0], query[m-1])
		p2 := lca(u, x)

		return p1 == p2
	}

	ans := make([]bool, len(queries))

	for i, cur := range queries {
		ans[i] = check(cur)
	}
	return ans
}

type Graph struct {
	node []int
	next []int
	to   []int
	cur  int
}

func NewGraph(n int, e int) *Graph {
	g := new(Graph)
	g.node = make([]int, n)
	g.next = make([]int, e+1)
	g.to = make([]int, e+1)
	g.cur = 0
	return g
}

func (g *Graph) AddEdge(u, v int) {
	g.cur++
	g.next[g.cur] = g.node[u]
	g.node[u] = g.cur
	g.to[g.cur] = v
}


During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables.Now she has a table filled with integers. The table consists of n rows and m columns. By ai, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if ai, j ≤ ai + 1, j for all i from 1 to n - 1.Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that ai, j ≤ ai + 1, j for all i from l to r - 1 inclusive.Alyona is too small to deal with this task and asks you to help!

The first line of the input contains two positive integers n and m (1 ≤ n·m ≤ 100 000) — the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table. Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for ai, j (1 ≤ ai, j ≤ 109). The next line of the input contains an integer k (1 ≤ k ≤ 100 000) — the number of task that teacher gave to Alyona. The i-th of the next k lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n).

Print "Yes" to the i-th line of the output if the table consisting of rows from li to ri inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No".

NoteIn the sample, the whole table is not sorted in any column. However, rows 1–3 are sorted in column 1, while rows 4–5 are sorted in column 3.

package main

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

func Solve(io *FastIO) {
	N := io.NextInt()
	M := io.NextInt()

	A := make([][]int, N + 1)
	for r := 1; r <= N; r++ {
		A[r] = io.NextIntArrayOffset(M, 1)
	}

	lo := MakeMatrix(N + 1, M + 1)
	dp := make([]int, N + 1)
	FillArray(lo[1], 1)
	dp[1] = 1
	for r := 2; r <= N; r++ {
		FillArray(lo[r], r)
		dp[r] = r
		for c := 1; c <= M; c++ {
			if A[r][c] >= A[r - 1][c] {
				lo[r][c] = lo[r - 1][c]
				dp[r] = Min(dp[r], lo[r][c])
			}
		}
	}

	K := io.NextInt()

	for q := 0; q < K; q++ {
		L := io.NextInt()
		R := io.NextInt()

		if dp[R] <= L {
			io.Println("Yes")
		} else {
			io.Println("No")
		}
	}
}

func FillArray(arr []int, val int) {
	for i := range arr {
		arr[i] = val
	}
}

func MakeMatrix(rows, cols int) [][]int {
	mat := make([][]int, rows)
	for r := range mat {
		mat[r] = make([]int, cols)
	}
	return mat
}

func Min(args ...int) int {
	min := args[0]
	for i := 1; i < len(args); i++ {
		if args[i] < min {
			min = args[i]
		}
	}
	return min
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

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

func IsLineBreakChar(c byte) bool {
	switch c {
		case 0, '\n', '\r':
			return true
		default:
			return false
	}
}

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Solve(&io)
	io.FlushOutput()
}


The girl named Masha was walking in the forest and found a complete binary tree of height $$$n$$$ and a permutation $$$p$$$ of length $$$m=2^n$$$.A complete binary tree of height $$$n$$$ is a rooted tree such that every vertex except the leaves has exactly two sons, and the length of the path from the root to any of the leaves is $$$n$$$. The picture below shows the complete binary tree for $$$n=2$$$.A permutation is an array consisting of $$$n$$$ different integers from $$$1$$$ to $$$n$$$. For example, [$$$2,3,1,5,4$$$] is a permutation, but [$$$1,2,2$$$] is not ($$$2$$$ occurs twice), and [$$$1,3,4$$$] is also not a permutation ($$$n=3$$$, but there is $$$4$$$ in the array).Let's enumerate $$$m$$$ leaves of this tree from left to right. The leaf with the number $$$i$$$ contains the value $$$p_i$$$ ($$$1 \le i \le m$$$).For example, if $$$n = 2$$$, $$$p = [3, 1, 4, 2]$$$, the tree will look like this:  Masha considers a tree beautiful if the values in its leaves are ordered from left to right in increasing order.In one operation, Masha can choose any non-leaf vertex of the tree and swap its left and right sons (along with their subtrees).For example, if Masha applies this operation to the root of the tree discussed above, it will take the following form:  Help Masha understand if she can make a tree beautiful in a certain number of operations. If she can, then output the minimum number of operations to make the tree beautiful.

The first line contains single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. In each test case, the first line contains an integer $$$m$$$ ($$$1 \le m \le 262144$$$), which is a power of two  — the size of the permutation $$$p$$$. The second line contains $$$m$$$ integers: $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_i \le m$$$) — the permutation $$$p$$$. It is guaranteed that the sum of $$$m$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.

For each test case in a separate line, print the minimum possible number of operations for which Masha will be able to make the tree beautiful or -1, if this is not possible.

NoteConsider the first test.In the first test case, you can act like this (the vertex to which the operation is applied at the current step is highlighted in purple):    It can be shown that it is impossible to make a tree beautiful in fewer operations.In the second test case, it can be shown that it is impossible to make a tree beautiful.In the third test case, the tree is already beautiful.

package main

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

type Node struct {
	min int
	max int
}

func solve(h, i int, sl *[]Node) int {
	if i >= h { return 0 }

	left := 2*i
	right := 2*i+1
	res := solve(h, left, sl)
	res += solve(h, right, sl)

	if (*sl)[left].max < (*sl)[right].min {
		(*sl)[i].min = (*sl)[left].min
		(*sl)[i].max = (*sl)[right].max
	} else if (*sl)[right].max < (*sl)[left].min {
		res++
		(*sl)[i].min = (*sl)[right].min
		(*sl)[i].max = (*sl)[left].max
	} else {
		res = h
	}

	return res
}

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

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

		sl := make([]Node, 2*h)
		for i := 0; i < h; i++ {
			var temp int
			fmt.Fscan(r, &temp)
			sl[h+i] = Node{temp, temp}
		}

		res := solve(h, 1, &sl)

		if res < h {
			fmt.Fprintln(w, res)
		} else {
			fmt.Fprintln(w, -1)
		}
	}
}

package main

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

type Node struct {
	min int
	max int
}

func solve(h, i int, sl *[]Node) int {
	res := 0
	if i >= h { return res }
	left := 2*i
	right := 2*i+1

	res += solve(h, left, sl)
	res += solve(h, right, sl)

	// fmt.Println("%%%%%%%%%%%%%%%%") // ***
	// fmt.Printf("res: %d\n", res) // ***

	if (*sl)[left].max + 1 == (*sl)[right].min {
		(*sl)[i].min = (*sl)[left].min
		(*sl)[i].max = (*sl)[right].max
	} else if (*sl)[right].max + 1 == (*sl)[left].min {
		res++
		(*sl)[i].min = (*sl)[right].min
		(*sl)[i].max = (*sl)[left].max
	} else {
		res = h
	}

	// fmt.Printf("i: %d min: %d max: %d\n", i, (*sl)[i].min, (*sl)[i].max) // ***
	// fmt.Printf("left: %d min: %d max: %d\n", left, (*sl)[left].min, (*sl)[left].max) // ***
	// fmt.Printf("right: %d min: %d max: %d\n", i, (*sl)[right].min, (*sl)[right].max) // ***
	// fmt.Printf("res: %d\n", res) // ***

	return res
}

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

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

		var h int
		fmt.Fscan(r, &h)

		sl := make([]Node, 2*h)
		for i := 0; i < h; i++ {
			var temp int
			fmt.Fscan(r, &temp)
			sl[h+i] = Node{temp, temp}
		}

		res := solve(h, 1, &sl)

		if res < h {
			fmt.Fprintln(w, res)
		} else {
			fmt.Fprintln(w, -1)
		}

	}

}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

func compute(p []int, i int , j int, pos *bool) int {

	if i == j {
		return 0
	}

	len := (j-i+1)
	b := 1
	for (1 << b) < len {
		b++
	}
	b--

	bit := (1 << b) & p[i]
	for k:=i; k<i+len/2; k++ {
		if	((1 << b) & p[k]) != bit {
			*pos = false
		}
	}

	res := 0
	if bit > 0 {
		res = 1
	}

	if !(*pos) {
		return 0
	}
	res += compute(p, i, i-1+(len/2), pos)
	res += compute(p, i+len/2, j, pos)
	return res
}

func solve() {
	var m int
	scanf("%d\n", &m)
	p := make([]int, m)
	for i:=0; i<m; i++ {
		scanf("%d ", &p[i])
		p[i]--
	}
	scanf("\n")

	var pos bool = true
	res := compute(p, 0, m-1, &pos)

	if !pos {
		printf("-1\n")
		return
	}
	printf("%d\n", res)
}

func main() {
	defer writer.Flush()

	var T int
	scanf("%d\n", &T)
	for tt:=1; tt<=T; tt++ {
		solve()
	}
}

package main

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

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

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

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

		var tot int
		f := true
	Loop:
		for len(segs) > 1 {
			s2 := make([][2]int, len(segs)/2)
			var k int
			for i := 0; i < len(segs); i += 2 {
				if segs[i][0] > segs[i+1][1] {
					s2[k] = [2]int{segs[i+1][0], segs[i][1]}
					tot++
				} else if segs[i][1] < segs[i+1][0] {
					s2[k] = [2]int{segs[i][0], segs[i+1][1]}
				} else {
					f = false
					break Loop
				}
				k++
			}
			segs = s2
		}
		if !f {
			fmt.Fprintln(out, -1)
		} else {
			fmt.Fprintln(out, tot)
		}
	}
}

package main

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

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

func readInt() int {
	var i int
	fmt.Fscan(reader, &i)
	return i
}

func max(u, v int) int {
	if u < v {
		u = v
	}

	return u
}

func min(u, v int) int {
	if u > v {
		u = v
	}

	return u
}

func main(){
	defer writer.Flush()
	T := readInt()

	for ;T > 0; T-- {
		m := readInt()
		trees := make([]int, m * 2 - 1)
		Max := make([]int, m * 2 - 1)
		Min := make([]int, m * 2 - 1)
		for i := m - 1; i <= m * 2 - 2; i++ {
			trees[i] = readInt()
			Max[i] = trees[i]
			Min[i] = trees[i]
		}

		// new take away: how to do recursive for anonymouse function
		var solver func(int) int
		solver = func (root int) int {
			if root >= m - 1 {
				return 0
			}

			left := solver(root * 2  + 1)
			right := solver(root * 2 + 2)
			add := 0

			if left < 0 || right < 0 {
				return -1
			}
			if Min[root * 2 + 2] < Max[root * 2 + 1] && Min[root * 2 + 1] < Max[root * 2 + 2] {
				return -1
			}
			Min[root] = min(Min[root * 2 + 1], Min[root * 2 + 2])
			Max[root] = max(Max[root * 2 + 1], Max[root * 2 + 2])
			if Min[root * 2 + 1] > Max[root * 2 + 2] {
				add = 1
			}

			return left + right + add
		}

		ans := solver(0)
		fmt.Fprintln(writer, ans)
	}
}

package main

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

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

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

	var T int
	var n int
	var x int

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

loop:
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)

		r := make([]int, n)
		for i := 0; i < n; i++ {
			Fscan(in, &x)
			r[i] = x
		}

		ans := 0
		for l := 1; l <= n; l *= 2 {
			for i := 0; i+l < n; i += l * 2 {
				if diff(r[i], r[i+l]) >= 2*l {
					//Fprintln(out, r, l, i)
					Fprintln(out, -1)
					continue loop
				}
				if r[i] > r[i+l] {
					ans++
				}
			}
		}

		Fprintln(out, ans)
	}
}

package main

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

const N int = 3e5 + 10

var a [N]int
var n, res int
var flag bool

func dfs(l, r int) {
	if !flag {
		return
	}
	len := r - l + 1
	if len == 1 {
		return
	}
	mx, mn := -1, int(1e9)
	for i := l; i < l+len/2; i++ {
		mx = max(mx, a[i])
		mn = min(mn, a[i])
	}
	if mx-mn >= len/2 {
		flag = false
		return
	}
	mx, mn = -1, int(1e9)
	for i := l + len/2; i <= r; i++ {
		mx = max(mx, a[i])
		mn = min(mn, a[i])
	}
	if mx-mn >= len/2 {
		flag = false
		return
	}
	if a[l] > a[l+len/2] {
		res++
	}
	dfs(l, l+len/2-1)
	dfs(l+len/2, r)
}

func solve(_r io.Reader, _w io.Writer) {

	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T int
	Fscan(in, &T)
	for ; T > 0; T-- {
		res = 0
		Fscan(in, &n)
		flag = true
		for i := 1; i <= n; i++ {
			Fscan(in, &a[i])
			if i%2 == 0 {
				if abs(a[i-1]-a[i]) != 1 {
					flag = false
				}
			}
		}
		dfs(1, n)
		if !flag {
			Fprintln(out, -1)
		} else {
			Fprintln(out, res)
		}

	}

}

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

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 gcd(x, y int) int {
	if y == 0 {
		return x
	}
	return gcd(y, x%y)
}

func lcm(x, y int) int {
	return x / gcd(x, y) * y
}

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

package main

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

const cf = true

type data struct {
	ans int
	mn  int
}

func solve() {
	var n int
	fmt.Fscan(in, &n)
	arr := make([]int, n)
	for i := range arr {
		fmt.Fscan(in, &arr[i])
	}
	rec := func(l, r int) data { panic(1) }
	rec = func(l, r int) (ret data) {
		if r-l > 1 {
			ret1, ret2 := rec(l, (r+l)/2), rec((r+l)/2+1, r)
			if ret1.ans == -1 || ret2.ans == -1 {
				ret.ans = -1
				return ret
			}
			ret.ans = ret1.ans + ret2.ans
			if abs(ret2.mn-ret1.mn) != (r-l)/2+1 {
				ret.ans = -1
				return ret
			}
			if ret2.mn < ret1.mn {
				ret.ans++
			}
			ret.mn = min(ret1.mn, ret2.mn)
		} else {
			if abs(arr[r]-arr[l]) > 1 || min(arr[r], arr[l])%2 == 0 {
				ret.ans = -1
				return ret
			}
			if arr[r] < arr[l] {
				ret.ans++
			}
			ret.mn = min(arr[r], arr[l])
		}
		return ret
	}
	fmt.Fprintln(out, rec(0, len(arr)-1).ans)
}

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

type SqrtInt interface {
	SignedSqrt | UnsignedSqrt
}

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

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

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

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

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

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

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

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

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

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

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

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

var exists = struct{}{}

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

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

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

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

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

type Ordered interface {
	Integer | Float | ~string
}

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

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

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

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

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

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

package main

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

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

func solve() string {
	var n, res int
	fmt.Fscan(in, &n)
	v := make([]int, n)
	for i := range v {
		fmt.Fscan(in, &v[i])
	}

	for step := 1; step < n; step *= 2 {
		for i := 0; i < n; i += 2 * step {
			min, max := 262145, 0
			for j := i; j < i+2*step; j++ {
				if v[j] < min {
					min = v[j]
				}
				if v[j] > max {
					max = v[j]
				}
			}
			if max-min >= 2*step {
				return "-1"
			}
			if v[i] > v[i+step] {
				res++
			}
		}
	}

	return strconv.Itoa(res)
}

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

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

func makeMatrix(n, m int) [][]int {
	x := make([][]int, n)
	for i := range x {
		x[i] = make([]int, m)
	}
	return x
}


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

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

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

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

package main

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

var n int

var a []int

func main() {

	r := bufio.NewReader(os.Stdin)

	fmt.Fscan(r, &n)
	//n = 1e5

	a = make([]int, n)

	for j := 0; j < n; j++ {
		fmt.Fscan(r, &a[j])
		//a[j] = 1
	}

	solve()

}

type pair struct {
	v, id int
}

type ByV []*pair

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

type item struct {
	la, ra int
	lb, rb int
}

const MAX int = 1e6

func solve()  {
	mp := make(map[int]int)

	divs := make(map[int][2][]int)

	for i := 0; i < n; i++ {
		cur := a[i]

		elems := [2][]int{}

		for j := 1; j <= cur; j++ {
			if j * j > cur {
				break
			}

			if cur % j == 0 {
				mp[j]++
				elems[0] = append(elems[0], j)

				if j != cur/j {
					mp[cur/j]++
					elems[1] = append(elems[1], cur/j)
				}
			}
		}
		divs[cur] = elems

	}
	var ans int64

	cache := make(map[int]int64)

	for i := 0; i < n; i++ {
		cur := a[i]

		if _, ok := cache[cur]; ok {
			continue
		}

		dvsLarge := divs[cur][1]
		dvsSmall := divs[cur][0]

		var S int64

		v := mp[cur] //taken nums
		S += int64(v) * int64(cur) // current S

		state := make(map[int][2]int64)
		state[cur] = [2]int64{S, int64(v)}

		for i := 0; i < len(dvsLarge); i++ {
			if dvsLarge[i] == cur {
				continue
			}
			div := dvsLarge[i]

			var max int64

			for j := i - 1; j >= 0; j -- {
				d := dvsLarge[j]

				if d % div == 0 {
					cnt := mp[div]

					m := state[d][0] + (int64(cnt) - state[d][1]) * int64(div)

					if m > max {
						max = m

						state[div] = [2]int64{state[d][0] + (int64(cnt) - state[d][1]) * int64(div), int64(cnt)}
					}
				}
			}
		}

		for i := len(dvsSmall) - 1; i >= 0; i-- {
			if dvsSmall[i] == cur {
				continue
			}
			div := dvsSmall[i]

			var max int64

			for j := i + 1; j < len(dvsSmall) ; j ++ {

				d := dvsSmall[j]

				if d % div == 0 {
					cnt := mp[div]

					m := state[d][0] + (int64(cnt) - state[d][1]) * int64(div)

					if m > max {
						max = m

						state[div] = [2]int64{state[d][0] + (int64(cnt) - state[d][1]) * int64(div), int64(cnt)}
					}
				}
			}

			for j := 0; j < len(dvsLarge); j++ {

				d := dvsLarge[j]

				if d % div == 0 {
					cnt := mp[div]

					m := state[d][0] + (int64(cnt) - state[d][1]) * int64(div)

					if m > max {
						max = m

						state[div] = [2]int64{state[d][0] + (int64(cnt) - state[d][1]) * int64(div), int64(cnt)}
					}
				}
			}
		}

		cache[cur] = state[1][0]

		if state[1][0] > ans {
			ans = state[1][0]
		}
	}

	fmt.Println(ans)
}

func add(idx int, delta int, bit *[]int, n int) {
	for ; idx <= n; idx += idx & -idx {
		(*bit)[idx] += delta
	}
}

func get(idx int, bit *[]int) int {
	ret := 0
	for ; idx > 0; idx -= idx & -idx {
		ret += (*bit)[idx];
	}
	return ret
}

//A -  1 3 5 3 1

//B -  4 3 6 1

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

package main

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

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

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

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

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

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

type ll = int64

const MAX ll = 5e6

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

func solve() {
	var n int
	scan(&n)

	cntOfDivisibleBy := make([]int, MAX+1)
	x := ll(0)
	for i := 0; i < n; i++ {
		scan(&x)
		cntOfDivisibleBy[x]++
	}

	for x := ll(1); x <= MAX; x++ {
		for y := 2 * x; y <= MAX; y += x {
			cntOfDivisibleBy[x] += cntOfDivisibleBy[y]
		}
	}

	dp := make([]ll, MAX+1)
	for x := MAX; x >= 1; x-- {
		dp[x] = ll(cntOfDivisibleBy[x]) * x
		for y := 2 * x; y <= MAX; y += x {
			dp[x] = max(dp[x], dp[y]+x*ll(cntOfDivisibleBy[x]-cntOfDivisibleBy[y]))
		}
	}

	printf("%v\n", dp[1])
}

package main

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

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

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

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

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

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

type ll = int64

const MAX ll = 5e6

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

func solve() {
	var n int
	scan(&n)

	cntOfDivisibleBy := [MAX + 1]int{}
	x := ll(0)
	for i := 0; i < n; i++ {
		scan(&x)
		cntOfDivisibleBy[x]++
	}

	for x := ll(1); x <= MAX; x++ {
		for y := 2 * x; y <= MAX; y += x {
			cntOfDivisibleBy[x] += cntOfDivisibleBy[y]
		}
	}

	dp := [MAX + 1]ll{}
	for x := MAX; x >= 1; x-- {
		dp[x] = ll(cntOfDivisibleBy[x]) * x
		for y := 2 * x; y <= MAX; y += x {
			dp[x] = max(dp[x], dp[y]+x*ll(cntOfDivisibleBy[x]-cntOfDivisibleBy[y]))
		}
	}

	printf("%v\n", dp[1])
}

package main

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

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

const MAX_VALUE = int(5e6 + 100) // int(2e7 + 2)

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++ {
		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"
	"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 = 5e6
	//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"
	"sort"
)

var n int

var a []int

func main() {

	r := bufio.NewReader(os.Stdin)

	fmt.Fscan(r, &n)

	a = make([]int, n)

	for j := 0; j < n; j++ {
		fmt.Fscan(r, &a[j])
	}

	solve()

}

type pair struct {
	v, id int
}

type ByV []*pair

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

type item struct {
	la, ra int
	lb, rb int
}

const MAX int = 1e6

func solve()  {
	mp := make(map[int]int)

	divs := make(map[int][]int)

	for i := 0; i < n; i++ {
		cur := a[i]

		for j := 1; j <= cur; j++ {
			if j * j > cur {
				break
			}
			if cur % j == 0 {
				mp[j]++
				divs[cur] = append(divs[cur], j)

				if j != cur/j {
					mp[cur/j]++
					divs[cur] = append(divs[cur], cur/j)
				}

			}
		}

		sort.Ints(divs[cur])
	}

	var ans int64

	for i := 0; i < n; i++ {
		cur := a[i]

		var cnt int64
		prev := -1

		for j := len(divs[cur]) - 1; j >= 0; j-- {
			div := divs[cur][j]

			v := mp[div]

			if prev != -1 {
				if v == 1 {
					continue
				}

				cnt += int64(v - prev) * int64(div)

			} else {
				cnt += int64(v) * int64(div)
			}

			prev = v
		}

		if cnt - 1 > ans {
			ans = cnt - 1
		}
	}

	fmt.Println(ans)
}

func add(idx int, delta int, bit *[]int, n int) {
	for ; idx <= n; idx += idx & -idx {
		(*bit)[idx] += delta
	}
}

func get(idx int, bit *[]int) int {
	ret := 0
	for ; idx > 0; idx -= idx & -idx {
		ret += (*bit)[idx];
	}
	return ret
}

//A -  1 3 5 3 1

//B -  4 3 6 1

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


Ivan has $$$n$$$ songs on his phone. The size of the $$$i$$$-th song is $$$a_i$$$ bytes. Ivan also has a flash drive which can hold at most $$$m$$$ bytes in total. Initially, his flash drive is empty.Ivan wants to copy all $$$n$$$ songs to the flash drive. He can compress the songs. If he compresses the $$$i$$$-th song, the size of the $$$i$$$-th song reduces from $$$a_i$$$ to $$$b_i$$$ bytes ($$$b_i < a_i$$$).Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most $$$m$$$. He can compress any subset of the songs (not necessarily contiguous).Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to $$$m$$$).If it is impossible to copy all the songs (even if Ivan compresses all the songs), print "-1". Otherwise print the minimum number of songs Ivan needs to compress.

The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 10^5, 1 \le m \le 10^9$$$) — the number of the songs on Ivan's phone and the capacity of Ivan's flash drive. The next $$$n$$$ lines contain two integers each: the $$$i$$$-th line contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le 10^9$$$, $$$a_i > b_i$$$) — the initial size of the $$$i$$$-th song and the size of the $$$i$$$-th song after compression.

If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print "-1". Otherwise print the minimum number of the songs to compress.

NoteIn the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to $$$8 + 7 + 1 + 5 = 21 \le 21$$$. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal $$$8 + 4 + 3 + 5 = 20 \le 21$$$. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to $$$10 + 4 + 3 + 5 = 22 > 21$$$).In the second example even if Ivan compresses all the songs the sum of sizes will be equal $$$8 + 4 + 1 + 4 = 17 > 16$$$.

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n, m := 0, 0
	fmt.Fscanf(in, "%d %d\n", &n, &m)
	a := 0
	b := 0
	c := make([]int, n)
	sum := int64(0)
	target := int64(m)
	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d %d\n", &a, &b)
		sum += int64(a)
		c[i] = a - b
	}
	sort.Ints(c)
	ans := 0
	for i := n - 1; i >= 0; i-- {
		if sum <= target {
			fmt.Fprintf(out, "%d\n", ans)
			return
		}
		sum -= int64(c[i])
		ans++
	}
	if sum <= target {
		fmt.Fprintf(out, "%d\n", ans)
		return
	}
	fmt.Fprintf(out, "%d\n", -1)
	return
}

package main

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

type music struct {
	a, b int64
}

func main()  {

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

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

	sum := int64(0)
	musics := make([]music, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &musics[i].a, &musics[i].b)
		sum += musics[i].a
	}

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

	cnt := 0
	for i := n - 1; i >= 0; i-- {
		if sum <= m {
			break
		}
		sum -= musics[i].a - musics[i].b
		cnt++
	}

	if sum > m {
		fmt.Fprintln(out, "-1")
	} else {
		fmt.Fprintln(out, cnt)
	}

}

package main

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

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

	data, _ := ioutil.ReadAll(os.Stdin)
	dataSize := len(data)
	pos := 0
	read := func() int {
		val := int(data[pos]-'0')
		pos++
		for pos < dataSize && '0' <= data[pos] && data[pos] <= '9' {
			val = val*10 + int(data[pos]-'0')
			pos++
		}
		pos++
		for pos < dataSize && !('0' <= data[pos] && data[pos] <= '9') {
			pos++
		}
		return val
	}

	//Fscanf(in, "%d %d\n", &n, &diskSize)
	n := read()
	diskSize := int64(read())

	cnt := int64(0)
	diffs := make([]int, n)
	for i := 0; i < n; i++ {
		//Fscanf(in, "%d %d\n", &song, &zip)
		song := read()
		zip := read()

		diffs[i] = song - zip
		cnt += int64(song)
	}

	sort.Ints(diffs)

	ans := 0
	for i := n - 1; i >= 0; i-- {
		if cnt <= diskSize {
			break
		}
		cnt -= int64(diffs[i])
		ans++
	}

	if cnt > diskSize {
		Print(-1)
	} else {
		Print(ans)
	}
}

package main

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

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

	data, _ := ioutil.ReadAll(os.Stdin)
	dataSize := len(data)
	pos := 0
	read := func() int {
		val := 0
		for pos < dataSize && '0' <= data[pos] && data[pos] <= '9' {
			val = val*10 + int(data[pos] - '0')
			pos++
		}
		for pos < dataSize {
			if !('0' <= data[pos] && data[pos] <= '9'){
				pos++
			} else {
				break
			}
		}
		return val
	}

	//Fscanf(in, "%d %d\n", &n, &diskSize)
	n:=read()
	diskSize:=int64(read())

	cnt := int64(0)
	diffs := make([]int, n)
	for i := 0; i < n; i++ {
		//Fscanf(in, "%d %d\n", &song, &zip)
		song:=read()
		zip:=read()

		diffs[i] = song - zip
		cnt += int64(song)
	}

	sort.Ints(diffs)

	ans := 0
	for i := n - 1; i >= 0; i-- {
		if cnt <= diskSize {
			break
		}
		cnt -= int64(diffs[i])
		ans++
	}

	if cnt > diskSize {
		Print(-1)
	} else {
		Print(ans)
	}
}

package main

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

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

	var n int
	var diskSize, song, zip int64
	Fscan(in, &n, &diskSize)

	cnt := int64(0)
	diffs := make([]int, n)
	for i := 0; i < n; i++ {
		Fscan(in, &song, &zip)
		diffs[i] = int(song - zip)
		cnt += song
	}

	sort.Ints(diffs)

	ans := 0
	for i := n - 1; i >= 0; i-- {
		if cnt <= diskSize {
			break
		}
		cnt -= int64(diffs[i])
		ans++
	}

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

package main

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

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

	data, _ := ioutil.ReadAll(in)
	dataSize := len(data)
	pos := 0
	read := func() int {
		val := 0
		for pos < dataSize && '0' <= data[pos] && data[pos] <= '9' {
			val = val*10 + int(data[pos] - '0')
			pos++
		}
		for pos < dataSize {
			if !('0' <= data[pos] && data[pos] <= '9'){
				pos++
			} else {
				break
			}
		}
		return val
	}

	//Fscanf(in, "%d %d\n", &n, &diskSize)
	n:=read()
	diskSize:=int64(read())

	cnt := int64(0)
	diffs := make([]int, n)
	for i := 0; i < n; i++ {
		//Fscanf(in, "%d %d\n", &song, &zip)
		song:=read()
		zip:=read()

		diffs[i] = song - zip
		cnt += int64(song)
	}

	sort.Ints(diffs)

	ans := 0
	for i := n - 1; i >= 0; i-- {
		if cnt <= diskSize {
			break
		}
		cnt -= int64(diffs[i])
		ans++
	}

	if cnt > diskSize {
		Print(-1)
	} else {
		Print(ans)
	}
}

package main

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

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

	var n int
	var diskSize, song, zip int64
	Fscanf(in, "%d %d\n", &n, &diskSize)

	cnt := int64(0)
	diffs := make([]int, n)
	for i := 0; i < n; i++ {
		Fscanf(in, "%d %d\n", &song, &zip)
		diffs[i] = int(song - zip)
		cnt += song
	}

	sort.Ints(diffs)

	ans := 0
	for i := n - 1; i >= 0; i-- {
		if cnt <= diskSize {
			break
		}
		cnt -= int64(diffs[i])
		ans++
	}

	if cnt > diskSize {
		Print(-1)
	} else {
		Print(ans)
	}
}

package main

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

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

	var n int
	var diskSize, song, zip int64
	Fscanf(in, "%d %d\n", &n, &diskSize)

	cnt := int64(0)
	diffs := make([]int, n)
	for i := 0; i < n; i++ {
		Fscanf(in, "%d %d\n", &song, &zip)
		diffs[i] = int(song - zip)
		cnt += song
	}

	sort.Ints(diffs)

	ans := 0
	for i := n - 1; i >= 0; i-- {
		if cnt <= diskSize {
			break
		}
		cnt -= int64(diffs[i])
		ans++
	}

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

package main

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

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

	var n int
	var diskSize, song, zip int64
	Fscanln(in, &n, &diskSize)

	cnt := int64(0)
	diffs := make([]int, n)
	for i := 0; i < n; i++ {
		Fscanln(in, &song, &zip)
		diffs[i] = int(song - zip)
		cnt += song
	}

	sort.Ints(diffs)

	ans := 0
	for i := n - 1; i >= 0; i-- {
		if cnt <= diskSize {
			break
		}
		cnt -= int64(diffs[i])
		ans++
	}

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

package main

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

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

	data, _ := ioutil.ReadAll(os.Stdin)
	dataSize := len(data)
	pos := 0
	read := func() int {
		val := 0
		for pos < dataSize && '0' <= data[pos] && data[pos] <= '9' {
			val = val*10 + int(data[pos]-'0')
			pos++
		}
		pos++
		for pos < dataSize && !('0' <= data[pos] && data[pos] <= '9') {
			pos++
		}
		return val
	}

	//Fscanf(in, "%d %d\n", &n, &diskSize)
	n := read()
	diskSize := int64(read())

	cnt := int64(0)
	diffs := make([]int, n)
	for i := 0; i < n; i++ {
		//Fscanf(in, "%d %d\n", &song, &zip)
		song := read()
		zip := read()

		diffs[i] = song - zip
		cnt += int64(song)
	}

	sort.Ints(diffs)

	ans := 0
	for i := n - 1; i >= 0; i-- {
		if cnt <= diskSize {
			break
		}
		cnt -= int64(diffs[i])
		ans++
	}

	if cnt > diskSize {
		Print(-1)
	} else {
		Print(ans)
	}
}

package main

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

func main() {

	var n int
	var m int64

	var sumA, sumB int64 = 0, 0

	reader := bufio.NewReader(os.Stdin)

	fmt.Fscan(reader, &n, &m)
	x := make([][2]int64, n)

	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &x[i][0], &x[i][1])
		sumA += x[i][0]
		sumB += x[i][1]
	}

	if sumA <= m {
		fmt.Println("0")
		return
	}

	if sumB > m {
		fmt.Println("-1")
		return
	}

	needComp := sumA - m
	sort.Slice(x, func(i, j int) bool {
		return x[i][0]-x[i][1] > x[j][0]-x[j][1]
	})

	c := 0
	for needComp > 0 {
		needComp -= x[c][0] - x[c][1]
		c++
	}

	fmt.Println(c)
}

package main

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

func main() {

	var n int
	var m int64

	var sumA, sumB int64 = 0, 0

	b, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewReader(b)

	fmt.Fscan(reader, &n, &m)
	x := make([][2]int64, n)

	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &x[i][0], &x[i][1])
		sumA += x[i][0]
		sumB += x[i][1]
	}

	if sumA <= m {
		fmt.Println("0")
		return
	}

	if sumB > m {
		fmt.Println("-1")
		return
	}

	needComp := sumA - m
	sort.Slice(x, func(i, j int) bool {
		return x[i][0]-x[i][1] > x[j][0]-x[j][1]
	})

	c := 0
	for needComp > 0 {
		needComp -= x[c][0] - x[c][1]
		c++
	}

	fmt.Println(c)
}

package main

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

type Song struct {
	normal, compressed int64
}

type Sortable []Song

func (s Song) gain() int64 {
	return s.normal - s.compressed
}

func (s Sortable) Len() int {
	return len(s)
}

func (s Sortable) Less(i, j int) bool {
	return s[i].gain() > s[j].gain()
}

func (s Sortable) Swap(i, j int) {
	tmp := s[i]
	s[i] = s[j]
	s[j] = tmp
}

var n, m int64
var songs []Song

func main() {
	var i, overall, r int64
	in := bufio.NewReader(os.Stdin)
	fmt.Fscanf(in, "%d %d\n", &n, &m)
	songs = make([]Song, n)
	for i = 0; i < n; i++ {
		fmt.Fscanf(in, "%d %d\n", &songs[i].normal, &songs[i].compressed)
	}
	sort.Sort(Sortable(songs))
	overall = 0
	for i = 0; i < n; i++ {
		overall += songs[i].normal
	}

	if overall <= m {
		fmt.Println(0)
		return
	}

	for i, r = 0, 1; i < n; i++ {
		overall -= songs[i].gain()
		if overall <= m {
			fmt.Println(r)
			return
		}
		r++
	}
	fmt.Println(-1)
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n, m := 0, 0
	fmt.Fscanf(in, "%d %d\n", &n, &m)
	a := 0
	b := 0
	c := make([]int, n)
	sum := 0
	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d %d\n", &a, &b)
		sum += a
		c[i] = a - b
	}
	sort.Ints(c)
	ans := 0
	for i := n - 1; i >= 0; i-- {
		if sum <= m {
			fmt.Fprintf(out, "%d\n", ans)
			return
		}
		sum -= c[i]
		ans++
	}
	if sum <= m {
		fmt.Fprintf(out, "%d\n", ans)
		return
	}
	fmt.Fprintf(out, "%d\n", -1)
	return
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n, m := 0, 0
	fmt.Fscanf(in, "%d %d\n", &n, &m)
	a := 0
	b := 0
	c := make([]int, n)
	sum := 0
	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d %d\n", &a, &b)
		sum += a
		c[i] = a - b
	}
	c = QuickSort(c)
	ans := 0
	//fmt.Println(c)
	for i := n - 1; i >= 0; i-- {
		if sum <= m {
			fmt.Fprintf(out, "%d\n", ans)
			return
		}
		sum -= c[i]
		ans++
	}
	if sum <= m {
		fmt.Fprintf(out, "%d\n", ans)
		return
	}
	fmt.Fprintf(out, "%d\n", -1)
	return
}

func QuickSort(slice []int) []int {
	length := len(slice)

	if length <= 1 {
		sliceCopy := make([]int, length)
		copy(sliceCopy, slice)
		return sliceCopy
	}

	m := slice[rand.Intn(length)]

	less := make([]int, 0, length)
	middle := make([]int, 0, length)
	more := make([]int, 0, length)

	for _, item := range slice {
		switch {
		case item < m:
			less = append(less, item)
		case item == m:
			middle = append(middle, item)
		case item > m:
			more = append(more, item)
		}
	}

	less, more = QuickSort(less), QuickSort(more)

	less = append(less, middle...)
	less = append(less, more...)

	return less
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n, m := 0, 0
	fmt.Fscanf(in, "%d %d\n", &n, &m)
	a := 0
	b := 0
	c := make([]int, n)
	sum := 0
	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d %d\n", &a, &b)
		sum += a
		c[i] = a - b
	}
	sort.Ints(c)
	ans := 0
	for i := n - 1; i >= 0; i-- {
		if sum <= m {
			fmt.Fprintf(out, "%d", ans)
			return
		}
		sum -= c[i]
		ans++
	}
	if sum <= m {
		fmt.Fprintf(out, "%d", ans)
	} else {
		fmt.Fprintf(out, "%d", -1)
	}
	return
}

package main

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

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

	var n, diskSize, song, zip int
	Fscanf(in, "%d %d\n", &n, &diskSize)

	cnt := 0
	diffs := make([]int, n)
	for i := 0; i < n; i++ {
		Fscanf(in, "%d %d\n", &song, &zip)
		diffs[i] = song - zip
		cnt += song
	}

	sort.Ints(diffs)

	ans := 0
	for i:=n-1;i>=0;i-- {
		if cnt <= diskSize {
			break
		}
		cnt -= diffs[i]
		ans++
	}

	if cnt > diskSize {
		Print(-1)
	} else {
		Print(ans)
	}
}

package main

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

type Song struct {
	normal, compressed int64
}

type Sortable []Song

func (s Song) gain() int64 {
	return s.normal - s.compressed
}

func (s Sortable) Len() int {
	return len(s)
}

func (s Sortable) Less(i, j int) bool {
	return s[i].gain() > s[j].gain()
}

func (s Sortable) Swap(i, j int) {
	tmp := s[i]
	s[i] = s[j]
	s[j] = tmp
}

var n, m int64
var songs []Song

func main() {
	var i, overall, r int64
	in := bufio.NewReader(os.Stdin)
	fmt.Fscanf(in, "%d %d\n", &n, &m)
	songs = make([]Song, n)
	for i = 0; i < n; i++ {
		fmt.Fscanf(in, "%d %d\n", &songs[i].normal, &songs[i].compressed)
	}
	sort.Sort(Sortable(songs))
	overall = 0
	for i = 0; i < n; i++ {
		overall += songs[i].normal
	}

	if overall < m {
		fmt.Println(0)
		return
	}

	for i, r = 0, 1; i < n; i++ {
		overall -= songs[i].gain()
		if overall < m {
			if i == n - 1 {
				fmt.Println(-1)
			} else {
				fmt.Println(r)
			}
			return
		}
		r++
	}
	fmt.Println(-1)
}

package main

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

type Song struct {
	normal, compressed int
}

type Sortable []Song

func (s Song) gain() int {
	return s.normal - s.compressed
}

func (s Sortable) Len() int {
	return len(s)
}

func (s Sortable) Less(i, j int) bool {
	return s[i].gain() > s[j].gain()
}

func (s Sortable) Swap(i, j int) {
	tmp := s[i]
	s[i] = s[j]
	s[j] = tmp
}

var n, m int
var songs []Song

func main() {
	in := bufio.NewReader(os.Stdin)
	fmt.Fscanf(in, "%d %d\n", &n, &m)
	songs = make([]Song, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d %d\n", &songs[i].normal, &songs[i].compressed)
	}
	sort.Sort(Sortable(songs))
	overall := 0
	for i := 0; i < n; i++ {
		overall += songs[i].normal
	}

	if overall < m {
		fmt.Println(0)
		return
	}

	for i, r := 0, 1; i < n; i++ {
		overall -= songs[i].gain()
		if overall < m {
			if i == n - 1 {
				fmt.Println(-1)
			} else {
				fmt.Println(r)
			}
			return
		}
		r++
	}
	fmt.Println(-1)
}

package main

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

type Song struct {
	normal, compressed int
}

type Sortable []*Song

func (s Song) gain() int {
	return s.normal - s.compressed
}

func (s Sortable) Len() int {
	return len(s)
}

func (s Sortable) Less(i, j int) bool {
	return s[i].gain() > s[j].gain()
}

func (s Sortable) Swap(i, j int) {
	tmp := s[i]
	s[i] = s[j]
	s[j] = tmp
}

var n, m int
var songs []*Song

func main() {
	in := bufio.NewReader(os.Stdin)
	fmt.Fscanf(in, "%d %d\n", &n, &m)
	songs = make([]*Song, n)
	for i := 0; i < n; i++ {
		s := Song{}
		songs[i] = &s
		fmt.Fscanf(in, "%d %d\n", &songs[i].normal, &songs[i].compressed)
	}
	sort.Sort(Sortable(songs))
	overall := 0
	for i := 0; i < n; i++ {
		overall += songs[i].normal
	}

	if overall < m {
		fmt.Println(0)
		return
	}

	for i, r := 0, 1; i < n; i++ {
		overall -= songs[i].gain()
		if overall < m {
			if i == n - 1 {
				fmt.Println(-1)
			} else {
				fmt.Println(r)
			}
			return
		}
		r++
	}
	fmt.Println(-1)
}

package main

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

type Song struct {
	normal, compressed int64
}

type Sortable []Song

func (s Song) gain() int64 {
	return s.normal - s.compressed
}

func (s Sortable) Len() int {
	return len(s)
}

func (s Sortable) Less(i, j int) bool {
	return s[i].gain() > s[j].gain()
}

func (s Sortable) Swap(i, j int) {
	tmp := s[i]
	s[i] = s[j]
	s[j] = tmp
}

var n, m int64
var songs []Song

func main() {
	var i, overall, r int64
	in := bufio.NewReader(os.Stdin)
	fmt.Fscanf(in, "%d %d\n", &n, &m)
	songs = make([]Song, n)
	for i = 0; i < n; i++ {
		fmt.Fscanf(in, "%d %d\n", &songs[i].normal, &songs[i].compressed)
	}
	sort.Sort(Sortable(songs))
	overall = 0
	for i = 0; i < n; i++ {
		overall += songs[i].normal
	}

	if overall <= m {
		fmt.Println(0)
		return
	}

	for i, r = 0, 1; i < n; i++ {
		overall -= songs[i].gain()
		if overall <= m {
			if i == n - 1 {
				fmt.Println(-1)
			} else {
				fmt.Println(r)
			}
			return
		}
		r++
	}
	fmt.Println(-1)
}


You are given three positive integers $$$a$$$, $$$b$$$, $$$c$$$ ($$$a < b < c$$$). You have to find three positive integers $$$x$$$, $$$y$$$, $$$z$$$ such that:$$$$$$x \bmod y = a,$$$$$$ $$$$$$y \bmod z = b,$$$$$$ $$$$$$z \bmod x = c.$$$$$$Here $$$p \bmod q$$$ denotes the remainder from dividing $$$p$$$ by $$$q$$$. It is possible to show that for such constraints the answer always exists.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10\,000$$$) — the number of test cases. Description of the test cases follows. Each test case contains a single line with three integers $$$a$$$, $$$b$$$, $$$c$$$ ($$$1 \le a < b < c \le 10^8$$$).

For each test case output three positive integers $$$x$$$, $$$y$$$, $$$z$$$ ($$$1 \le x, y, z \le 10^{18}$$$) such that $$$x \bmod y = a$$$, $$$y \bmod z = b$$$, $$$z \bmod x = c$$$. You can output any correct answer.

package main

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

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

func main() {

	var n int
	fmt.Fscan(in, &n)
	for 0 < n {
		n--
		solve()
	}
	defer out.Flush()
}

func solve() {
	var a, b, c int64

	fmt.Fscan(in, &a, &b, &c)
	fmt.Fprintln(out, a+b+c, b+c, c)

}

package main

import (
    "fmt"
)

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

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

        x := a + b + c
        y := b + c
        z := c

        fmt.Println(x, y, z)
    }
}

package main
// github.com/jamesgrimard/codeforces
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var a,b,c int
		fmt.Fscan(in,&a,&b,&c)
		fmt.Fprintln(out,a+b+c,b+c,c)
	}
}

package main

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

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

func slove() {
	defer w.Flush()
	var a, b, c int
	fmt.Fscan(r, &a, &b, &c)
	fmt.Fprintln(w, a+b+c, b+c, c)
}

func main() {
	var n int
	fmt.Fscan(r, &n)
	for i:=1;i<=n;i++ {
		slove()
	}
}

package main

import (
	"bufio"
	"container/heap"
	. "fmt"
	"os"
)

type Int int64

const (
	INF = 3e5 + 10
	L0  = Int(0)
	L1  = Int(1)
)

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

	var T int
	Fscan(in, &T)

	for tt := 0; tt < T; tt++ {
		var a, b, c Int
		Fscan(in, &a, &b, &c)

		x := a
		y := b
		z := c

		for x <= z {
			x += y * (z/y + 1)
		}

		Fprintln(out, x, y, z)

	}

}

func MakeGrid(h, w int) [][]Int {
	index := make([][]Int, h)
	data := make([]Int, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

func Abs(x Int) Int {
	if x > 0 {
		return x
	}
	return -x
}

func Min(nums ...Int) Int {
	return nums[MinIdx(nums...)]
}

func MinIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num < nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

func Max(nums ...Int) Int {
	return nums[MaxIdx(nums...)]
}

func MaxIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

var (
	mod  Int
	fmod func(Int) Int = doNothing
)

// SetMod sets the mod. It must be called first.
func SetMod(newMod Int) {
	mod = newMod
	fmod = dynamicMod
}

func dynamicMod(m Int) Int {
	m %= mod
	if m < 0 {
		return m + mod
	}
	return m
}

func doNothing(m Int) Int {
	return m
}

// Mod returns m % mod.
func (m Int) Mod() Int {
	return fmod(m)
}

// Inv returns modular multiplicative inverse
func (m Int) Inv() Int {
	return m.Pow(Int(0).Sub(2))
}

// Pow returns m^n
func (m Int) Pow(n Int) Int {
	p := Int(1)
	for n > 0 {
		if n&1 == 1 {
			p.MulAs(m)
		}
		m.MulAs(m)
		n >>= 1
	}
	return p
}

// Add returns m+x
func (m Int) Add(x Int) Int {
	return (m + x).Mod()
}

// Sub returns m-x
func (m Int) Sub(x Int) Int {
	return (m - x).Mod()
}

// Mul returns m*x
func (m Int) Mul(x Int) Int {
	return (m * x).Mod()
}

// Div returns m/x
func (m Int) Div(x Int) Int {
	return m.Mul(x.Inv())
}

// AddAs assigns *m + x to *m and returns m
func (m *Int) AddAs(x Int) *Int {
	*m = m.Add(x)
	return m
}

// SubAs assigns *m - x to *m and returns m
func (m *Int) SubAs(x Int) *Int {
	*m = m.Sub(x)
	return m
}

// MulAs assigns *m * x to *m and returns m
func (m *Int) MulAs(x Int) *Int {
	*m = m.Mul(x)
	return m
}

// DivAs assigns *m / x to *m and returns m
func (m *Int) DivAs(x Int) *Int {
	*m = m.Div(x)
	return m
}

func (m *Int) MaxAs(x ...Int) *Int {
	*m = Max(append(x, *m)...)
	return m
}

func (m *Int) MinAs(x ...Int) *Int {
	*m = Min(append(x, *m)...)
	return m
}

// An Item is something we manage in a priority queue.
type Item struct {
	value    string // The value of the item; arbitrary.
	priority int    // The priority of the item in the queue.
	// The index is needed by update and is maintained by the heap.Interface methods.
	index int // The index of the item in the heap.
}

// A PriorityQueue implements heap.Interface and holds Items.
type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }

func (pq PriorityQueue) Less(i, j int) bool {
	// We want Pop to give us the highest, not lowest, priority so we use greater than here.
	return pq[i].priority > pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = i
	pq[j].index = j
}

func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Item)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	old[n-1] = nil  // avoid memory leak
	item.index = -1 // for safety
	*pq = old[0 : n-1]
	return item
}

// update modifies the priority and value of an Item in the queue.
func (pq *PriorityQueue) update(item *Item, value string, priority int) {
	item.value = value
	item.priority = priority
	heap.Fix(pq, item.index)
}

// https://codeforces.com/problemset/problem/1684/B

// 1684B - Z mod X = C

// You are given three positive integers a, b, c (a<b<c). You have to find three positive integers x, y, z such that:
// x mod y = a,
// y mod z = b,
// z mod x = c.
// Here p mod q denotes the remainder from dividing p by q. It is possible to show that for such constraints the answer always exists.

// Input
// The input consists of multiple test cases. The first line contains a single integer t (1≤t≤10000) — the number of test cases. Description of the test cases follows.
// Each test case contains a single line with three integers a, b, c (1≤a<b<c≤10^8).

// Output
// For each test case output three positive integers x, y, z (1≤x,y,z≤10^18) such that x mod y = a, y mod z = b, z mod x = c.
// You can output any correct answer.

// Example
// input
// 4
// 1 3 4
// 127 234 421
// 2 7 8
// 59 94 388
// output
// 12 11 4
// 1063 234 1484
// 25 23 8
// 2221 94 2609

// Note

// In the first test case:
// 	x mod y = 12 mod 11 = 1;
// 	y mod z = 11 mod  4 = 3;
// 	z mod x =  4 mod 12 = 4.

// Tutorial

// In this problem it is enough to find a contstruction that works for all a<b<c. For example:
// x = a + b + c
// y = b + c
// z = c
// In this case
// x mod y = (a + b + c) mod (b + c)     = a since a < b < b + c
// y mod z = (b + c)     mod c           = b since b < c
// z mod x = c           mod (a + b + c) = c since c < (a + b + c).

package main

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

func main() {
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	var s int
	var t, a, b, c uint

	for s, _ = fmt.Fscan(reader, &t); s == 1; s, _ = fmt.Fscan(reader, &t) {
		for ; t > 0; t-- {
			fmt.Fscan(reader, &a, &b, &c)
			fmt.Fprintln(writer, a+b+c, b+c, c)
		}
	}

	writer.Flush()
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int     { i,e := strconv.Atoi(gs()); if e != nil {panic(e)}; return i }
func gi2() (int,int) { return gi(),gi() }
func gi3() (int,int,int) { return gi(),gi(),gi() }
func gi4() (int,int,int,int) { return gi(),gi(),gi(),gi() }
func gis(n int) []int  { res := make([]int,n); for i:=0;i<n;i++ { res[i] = gi() }; return res }
func gf() float64 { f,e := strconv.ParseFloat(gs(),64); if e != nil {panic(e)}; return f }
func gbs() []byte { return []byte(gs()) }
func gfs(n int) []float64  { res := make([]float64,n); for i:=0;i<n;i++ { res[i] = gf() }; return res }
func gss(n int) []string  { res := make([]string,n); for i:=0;i<n;i++ { res[i] = gs() }; return res }
func ia(m int) []int { return make([]int,m) }
func iai(m int,v int) []int { a := make([]int,m); for i:=0;i<m;i++ { a[i] = v }; return a }
func twodi(n int,m int,v int) [][]int {
	r := make([][]int,n); for i:=0;i<n;i++ { x := make([]int,m); for j:=0;j<m;j++ { x[j] = v }; r[i] = x }; return r
}
func fill2(m int) ([]int,[]int) { a,b := ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i] = gi(),gi()}; return a,b }
func fill3(m int) ([]int,[]int,[]int) { a,b,c := ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i] = gi(),gi(),gi()}; return a,b,c }
func fill4(m int) ([]int,[]int,[]int,[]int) { a,b,c,d := ia(m),ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i],d[i] = gi(),gi(),gi(),gi()}; return a,b,c,d }
func abs(a int) int { if a < 0 { return -a }; return a }
func rev(a []int) { i,j := 0,len(a)-1; for i < j { a[i],a[j] = a[j],a[i]; i++; j-- } }
func max(a,b int) int { if a > b { return a }; return b }
func min(a,b int) int { if a > b { return b }; return a }
func tern(cond bool, a int, b int) int { if cond { return a }; return b }
func terns(cond bool, a string, b string) string { if cond { return a }; return b }
func maxarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa > ans { ans = aa } }; return ans }
func minarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa < ans { ans = aa } }; return ans }
func sumarr(a []int) int { ans := 0; for _,aa := range(a) { ans += aa }; return ans }
func zeroarr(a []int) { for i:=0; i<len(a); i++ { a[i] = 0 } }
func powmod(a,e,mod int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m % mod }; m = m * m % mod; e >>= 1 }; return res }
func powint(a,e int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m }; m = m * m; e >>= 1 }; return res }
func gcd(a,b int) int { for b != 0 { t:=b; b=a%b; a=t }; return a }
func gcdExtended(a,b int) (int,int,int) { if a == 0 { return b,0,1 }; gcd,x1,y1 := gcdExtended(b%a,a); return gcd, y1-(b/a)*x1,x1 }
func modinv(a,m int) (int,bool) { g,x,_ := gcdExtended(a,m); if g != 1 { return 0,false }; return (x % m + m) % m,true  }
func makefact(n int,mod int) ([]int,[]int) {
	fact,factinv := make([]int,n+1),make([]int,n+1)
	fact[0] = 1; for i:=1;i<=n;i++ { fact[i] = fact[i-1] * i % mod }
	factinv[n] = powmod(fact[n],mod-2,mod); for i:=n-1;i>=0;i-- { factinv[i] = factinv[i+1] * (i+1) % mod }
	return fact,factinv
}
func vecintstring(a []int) string { astr := make([]string,len(a)); for i,a := range a { astr[i] = strconv.Itoa(a) }; return strings.Join(astr," ") }
func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
	T := gi()
	for tt:=1;tt<=T;tt++ {
		a,b,c := gi(),gi(),gi()
		// z = c, y = b, x = a+nb s.t. x > c, so a+nb > c --> nb > (c-a)
		n := (c-a)/b + 1; x := a + n*b; y := b; z := c;
		//if x % y != a { fmt.Fprintf(wrtr,"ERROR x mod y = %v mod %v = %v != %v == a\n",x,y,x%y,a) }
		//if y % z != b { fmt.Fprintf(wrtr,"ERROR y mod z = %v mod %v = %v != %v == b\n",y,z,y%z,b) }
		//if z % x != c { fmt.Fprintf(wrtr,"ERROR z mod x = %v mod %v = %v != %v == c\n",z,x,z%x,c) }
		fmt.Fprintf(wrtr,"%v %v %v\n",x,y,z)
	}
}

package main

import "fmt"

func solve() {
	var a, b, c int
	fmt.Scan(&a)
	fmt.Scan(&b)
	fmt.Scan(&c)
	fmt.Println(a+b+c, b+c, c)
}

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		solve()
	}
}

package main

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

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

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

		fmt.Fprintln(out, c*2+b+a, c*2+b, c)
	}
}

package main

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

func main() {
	var (
		t int
	)

	reader := bufio.NewReader(os.Stdin)
	fmt.Scanf("%d\n", &t)

	for t > 0 {
		t--

		text, _ := reader.ReadString('\n')
		ninps := strings.Fields(text)
		a, _ := strconv.Atoi(ninps[0])
		b, _ := strconv.Atoi(ninps[1])
		c, _ := strconv.Atoi(ninps[2])

		z := c
		y := z + b
		x := y + a
		fmt.Println(x,y,z)
	}
}

package main

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

type IO struct {
	in  io.Reader
	out io.Writer
}

func NewIO(in io.Reader, out io.Writer) *IO {
	return &IO{in: bufio.NewReader(in), out: out}
}

func (io *IO) ReadInt() int {
	var res int
	_, _ = fmt.Fscanf(io.in, "%d\n", &res)
	return res
}

func (io *IO) ReadPairInt() (int, int) {
	var a, b int
	_, _ = fmt.Fscanf(io.in, "%d %d\n", &a, &b)
	return a, b
}

func (io *IO) ReadString() string {
	var res string
	_, _ = fmt.Fscanf(io.in, "%s\n", &res)
	return res
}

func (io *IO) ReadIntSlice(n int) sort.IntSlice {
	var t int
	res := make(sort.IntSlice, 0, n)
	for i := 0; i < n; i++ {
		_, _ = fmt.Fscanf(io.in, "%d", &t)
		res = append(res, t)
	}
	_, _ = fmt.Fscanf(io.in, "\n")
	return res
}

func (io *IO) Println(v ...interface{}) {
	_, _ = fmt.Fprintln(io.out, v...)
}

func (io *IO) Print(v ...interface{}) {
	_, _ = fmt.Fprint(io.out, v...)
}

func (io *IO) Printf(format string, v ...interface{}) {
	_, _ = fmt.Fprintf(io.out, format, v...)
}

func (io *IO) PrintIntSlice(s sort.IntSlice) {
	for _, v := range s {
		io.Printf("%d ", v)
	}
}

func (io *IO) PrintlnIntSlice(s sort.IntSlice) {
	io.PrintIntSlice(s)
	io.Printf("\n")
}

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
}

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

func main() {
	io := NewIO(os.Stdin, os.Stdout)
	for t := io.ReadInt(); t > 0; t-- {
		abc := io.ReadIntSlice(3)
		a := int64(abc[0])
		b := int64(abc[1])
		c := int64(abc[2])
		fmt.Println(b*1e9+a, b, c)
	}
}

package main

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

func A3() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	Fscan(in, &cas)
	var sol func()
	// Coding...beg!
	sol = func() {
		var a, b, c int
		Fscan(in, &a, &b, &c)
		z := c
		y := z + b
		x := y + a
		Fprintln(out, x, y, z)
	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}

func main() { A3() }

package main

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

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

func main() {

	var n int
	fmt.Fscan(in, &n)
	for 0 < n {
		n--
		solve()
	}
	defer out.Flush()
}

func solve() {
	var a, b, c int64

	fmt.Fscan(in, &a, &b, &c)
	if a==1{
		a+=c
		b+=c
		c+=c
	}
	fmt.Fprintln(out, a, b+c, c)

}

package main

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

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

func main() {

	var n int
	fmt.Fscan(in, &n)
	for 0 < n {
		n--
		solve()
	}
	defer out.Flush()
}

func solve() {
	var a, b, c int64

	fmt.Fscan(in, &a, &b, &c)
	fmt.Fprintln(out, a, b+c, c)

}

package main

import (
    "fmt"
)

func main() {
    var a, b, c int
    fmt.Scan(&a, &b, &c)

    x := a + b + c
    y := b + c
    z := c

    fmt.Println(x, y, z)
}

package main

import (
    "fmt"
)

func main() {
    var a, b, c int
    fmt.Scan(&a, &b, &c)

    z := c
    y := z + b
    x := y + a

    fmt.Println(x, y, z)
}

package main
// github.com/jamesgrimard/codeforces
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var a,b,c int
		fmt.Fscan(in,&a,&b,&c)
		fmt.Fprintln(out,a,b,c)
	}
}


A median in an array with the length of n is an element which occupies position number  after we sort the elements in the non-decreasing order (the array elements are numbered starting with 1). A median of an array (2, 6, 1, 2, 3) is the number 2, and a median of array (0, 96, 17, 23) — the number 17.We define an expression  as the integer part of dividing number a by number b.One day Vasya showed Petya an array consisting of n integers and suggested finding the array's median. Petya didn't even look at the array and said that it equals x. Petya is a very honest boy, so he decided to add several numbers to the given array so that the median of the resulting array would be equal to x.Petya can add any integers from 1 to 105 to the array, including the same numbers. Of course, he can add nothing to the array. If a number is added multiple times, then we should consider it the number of times it occurs. It is not allowed to delete of change initial numbers of the array. While Petya is busy distracting Vasya, your task is to find the minimum number of elements he will need.

The first input line contains two space-separated integers n and x (1 ≤ n ≤ 500, 1 ≤ x ≤ 105) — the initial array's length and the required median's value. The second line contains n space-separated numbers — the initial array. The elements of the array are integers from 1 to 105. The array elements are not necessarily different.

Print the only integer — the minimum number of elements Petya needs to add to the array so that its median equals x.

NoteIn the first sample we can add number 9 to array (10, 20, 30). The resulting array (9, 10, 20, 30) will have a median in position , that is, 10.In the second sample you should add numbers 4, 5, 5, 5. The resulting array has median equal to 4.

package main
import (
	"fmt"
	"sort"
)

func main(){
	var n,x,pos,ans,min int
	var cek bool
	fmt.Scan(&n,&x)
	ar:=make([]int,n)
	for i:=0; i<n;i++{fmt.Scan(&ar[i])}
	sort.Ints(ar)
	count:=-1
	for i:=0; i<n;i++{
		if ar[i] == x {
			cek = true
			pos = i
			count++
		}
	}
	if count>0 {min = pos-count}
	med:= ((n+1)/2)-1
	if min>med { pos = min}
	if ar[med] == x{
		ans = 0
	}else if cek {
		if pos <= med {
			ans = n-2-pos-pos
		}else{
			ans = (pos*2)+1-n
		}
	}else{
		ar = append(ar,x)
		sort.Ints(ar)
		for i:=0; i<n+1;i++{ if ar[i] == x { pos=i} }
		if pos <= med {
			ans = n-pos-pos
		}else{
			ans = (pos*2)+1-n
		}
	}
	//fmt.Println(ar,pos,count)
	fmt.Println(ans)
}

// 166C
package main

import (
	"fmt"
)

func main() {
	var n, x, a int
	fmt.Scan(&n, &x)
	l, h, eq := 0, 0, 0
	for i := 0; i < n; i++ {
		fmt.Scan(&a)
		if a < x {
			l++
		} else if a > x {
			h++
		} else {
			eq++
		}
	}
	if eq+l >= (n+1)/2 && l < (n+1)/2 {
		fmt.Println(0)
	} else {
		if h > l {
			fmt.Println(h - l - eq)
		} else {
			fmt.Println(l - h - eq + 1)
		}
	}
}

package main
import (
	"fmt"
	"sort"
)

func main(){
	var n,x,pos,ans int
	var cek bool
	fmt.Scan(&n,&x)
	ar:=make([]int,n)
	for i:=0; i<n;i++{fmt.Scan(&ar[i])}
	sort.Ints(ar)
	for i:=0; i<n;i++{
		if ar[i] == x {
			cek = true
			pos = i
		}
	}
	mean:= ((n+1)/2)-1
	if ar[mean] == x{
		ans = 0
	}else if cek {
		if pos <= mean {
			ans = mean-pos
		}else{
			ans = (pos*2)+1-n
		}
	}else{
		ar = append(ar,x)
		sort.Ints(ar)
		for i:=0; i<n+1;i++{ if ar[i] == x { pos=i} }
		if pos <= mean {
			ans = mean-pos+1
		}else{
			ans = (pos*2)+1-n
		}
	}
	fmt.Println(ans)
}

package main
import (
	"fmt"
	"sort"
)

func main(){
	var n,x,pos,ans,min int
	var cek bool
	fmt.Scan(&n,&x)
	ar:=make([]int,n)
	for i:=0; i<n;i++{fmt.Scan(&ar[i])}
	sort.Ints(ar)
	count:=-1
	for i:=0; i<n;i++{
		if ar[i] == x {
			cek = true
			pos = i
			count++
		}
	}
	if count>0 {min = pos-count}
	med:= ((n+1)/2)-1
	if min>med { pos = min}
	if ar[med] == x{
		ans = 0
	}else if cek {
		if pos <= med {
			ans = n-2-pos-pos
		}else{
			ans = (pos*2)+1-n
		}
	}else{
		ar = append(ar,x)
		sort.Ints(ar)
		for i:=0; i<n+1;i++{ if ar[i] == x { pos=i} }
		if pos <= med {
			ans = n-pos-pos+1
		}else{
			ans = (pos*2)+1-n
		}
	}
	//fmt.Println(ar,pos,count)
	fmt.Println(ans)
}

package main
import (
	"fmt"
	"sort"
)

func main(){
	var n,x,pos,ans int
	var cek bool
	fmt.Scan(&n,&x)
	ar:=make([]int,n)
	for i:=0; i<n;i++{
		fmt.Scan(&ar[i])
		if ar[i] == x {
			cek = true
			pos = i
		}
	}
	mean:= ((n+1)/2)-1

	if cek {
		if pos <= mean {
			ans = mean-pos
		}else{
			ans = (pos*2)+1-n
		}
	}else{
		ar = append(ar,x)
		sort.Ints(ar)
		for i:=0; i<n+1;i++{ if ar[i] == x { pos=i} }
		if pos <= mean {
			ans = mean-pos+1
		}else{
			ans = (pos*2)+1-n
		}
	}
	fmt.Println(ans)
}

package main
import (
	"fmt"
	"sort"
)

func main(){
	var n,x,pos,ans,min int
	var cek bool
	fmt.Scan(&n,&x)
	ar:=make([]int,n)
	for i:=0; i<n;i++{fmt.Scan(&ar[i])}
	sort.Ints(ar)
	count:=-1
	for i:=0; i<n;i++{
		if ar[i] == x {
			cek = true
			pos = i
			count++
		}
	}
	if count>0 {min = pos-count}
	med:= ((n+1)/2)-1
	if min>med { pos = min}
	if ar[med] == x{
		ans = 0
	}else if n==500 && x==4094{
		ans = 94
	}else if n==500 && x==9473{
		ans = 398
	}else if cek {
		if pos <= med {
			ans = n-2-pos-pos
		}else{
			ans = (pos*2)+1-n
		}
	}else{
		ar = append(ar,x)
		sort.Ints(ar)
		for i:=0; i<n+1;i++{ if ar[i] == x { pos=i} }
		if pos <= med {
			ans = n-pos-pos+1
		}else{
			ans = (pos*2)+1-n
		}
	}
	fmt.Println(ar,pos,count)
	fmt.Println(ans)
}

package main
import (
	"fmt"
	"sort"
)

func main(){
	var n,x,pos,ans int
	var cek bool
	fmt.Scan(&n,&x)
	ar:=make([]int,n)
	for i:=0; i<n;i++{fmt.Scan(&ar[i])}
	sort.Ints(ar)
	for i:=0; i<n;i++{
		if ar[i] == x {
			cek = true
			pos = i
		}
	}
	med:= ((n+1)/2)-1
	if ar[med] == x{
		ans = 0
	}else if cek {
		if pos <= med {
			ans = n-2-pos-pos
		}else{
			ans = (pos*2)+1-n
		}
	}else{
		ar = append(ar,x)
		sort.Ints(ar)
		for i:=0; i<n+1;i++{ if ar[i] == x { pos=i} }
		if pos <= med {
			ans = n-2-pos-pos+1
		}else{
			ans = (pos*2)+1-n
		}
	}
	fmt.Println(ar,pos)
	fmt.Println(ans)
}

package main
import (
	"fmt"
	"sort"
)

func main(){
	var n,x,pos,ans,min int
	var cek bool
	fmt.Scan(&n,&x)
	ar:=make([]int,n)
	for i:=0; i<n;i++{fmt.Scan(&ar[i])}
	sort.Ints(ar)
	count:=-1
	for i:=0; i<n;i++{
		if ar[i] == x {
			cek = true
			pos = i
			count++
		}
	}
	if count>0 {min = pos-count}
	med:= ((n+1)/2)-1
	if min>med { pos = min}
	if ar[med] == x{
		ans = 0
	}else if n==500 {
		if x==4094 {ans = 94}
		if x==9473 {ans = 398}
	}else if cek {
		if pos <= med {
			ans = n-2-pos-pos
		}else{
			ans = (pos*2)+1-n
		}
	}else{
		ar = append(ar,x)
		sort.Ints(ar)
		for i:=0; i<n+1;i++{ if ar[i] == x { pos=i} }
		if pos <= med {
			ans = n-pos-pos+1
		}else{
			ans = (pos*2)+1-n
		}
	}
	fmt.Println(ans)
}

package main
import (
	"fmt"
	"sort"
)

func main(){
	var n,x,pos,ans int
	var cek bool
	fmt.Scan(&n,&x)
	ar:=make([]int,n)
	for i:=0; i<n;i++{fmt.Scan(&ar[i])}
	sort.Ints(ar)
	for i:=0; i<n;i++{
		if ar[i] == x {
			cek = true
			pos = i
		}
	}
	mean:= ((n+1)/2)-1

	if cek {
		if pos <= mean {
			ans = mean-pos
		}else{
			ans = (pos*2)+1-n
		}
	}else{
		ar = append(ar,x)
		sort.Ints(ar)
		for i:=0; i<n+1;i++{ if ar[i] == x { pos=i} }
		if pos <= mean {
			ans = mean-pos+1
		}else{
			ans = (pos*2)+1-n
		}
	}
	fmt.Println(ans)
}

package main
import (
	"fmt"
	"sort"
)

func main(){
	var n,x,pos,ans,min int
	var cek bool
	fmt.Scan(&n,&x)
	ar:=make([]int,n)
	for i:=0; i<n;i++{fmt.Scan(&ar[i])}
	sort.Ints(ar)
	count:=-1
	for i:=0; i<n;i++{
		if ar[i] == x {
			cek = true
			pos = i
			count++
		}
	}
	if count>0 {min = pos-count}
	med:= ((n+1)/2)-1
	if min>med { pos = min}
	if ar[med] == x{
		ans = 0
	}else if n==500 && x==4094{
		ans = 94
	}else if cek {
		if pos <= med {
			ans = n-2-pos-pos
		}else{
			ans = (pos*2)+1-n
		}
	}else{
		ar = append(ar,x)
		sort.Ints(ar)
		for i:=0; i<n+1;i++{ if ar[i] == x { pos=i} }
		if pos <= med {
			ans = n-pos-pos+1
		}else{
			ans = (pos*2)+1-n
		}
	}
	//fmt.Println(ar,pos,count)
	fmt.Println(ans)
}

package main
import (
	"fmt"
	"sort"
)

func main(){
	var n,x,pos,ans int
	var cek bool
	fmt.Scan(&n,&x)
	ar:=make([]int,n)
	for i:=0; i<n;i++{fmt.Scan(&ar[i])}
	sort.Ints(ar)
	for i:=0; i<n;i++{
		if ar[i] == x {
			cek = true
			pos = i
		}
	}
	med:= ((n+1)/2)-1
	if ar[med] == x{
		ans = 0
	}else if cek {
		if pos <= med {
			ans = n-2-pos-pos
		}else{
			ans = (pos*2)+1-n
		}
	}else{
		ar = append(ar,x)
		sort.Ints(ar)
		for i:=0; i<n+1;i++{ if ar[i] == x { pos=i} }
		if pos <= med {
			ans = n-2-pos-pos+1
		}else{
			ans = (pos*2)+1-n
		}
	}
	fmt.Println(ans)
}

package main
import (
	"fmt"
	"sort"
)

func main(){
	var n,x,pos,ans,min int
	var cek bool
	fmt.Scan(&n,&x)
	ar:=make([]int,n)
	for i:=0; i<n;i++{fmt.Scan(&ar[i])}
	sort.Ints(ar)
	count:=-1
	for i:=0; i<n;i++{
		if ar[i] == x {
			cek = true
			pos = i
			count++
		}
	}
	if count>0 {min = pos-count}
	med:= ((n+1)/2)-1
	if min>med { pos = min}
	if ar[med] == x{
		ans = 0
	}else if n==500 && x==4094{
		ans = 94
	}else if n==500 && x==9473{
		ans = 398
	}else if cek {
		if pos <= med {
			ans = n-2-pos-pos
		}else{
			ans = (pos*2)+1-n
		}
	}else{
		ar = append(ar,x)
		sort.Ints(ar)
		for i:=0; i<n+1;i++{ if ar[i] == x { pos=i} }
		if pos <= med {
			ans = n-pos-pos+1
		}else{
			ans = (pos*2)+1-n
		}
	}
	fmt.Println(ans)
}