Timur has $$$n$$$ candies. The $$$i$$$-th candy has a quantity of sugar equal to $$$a_i$$$. So, by eating the $$$i$$$-th candy, Timur consumes a quantity of sugar equal to $$$a_i$$$.Timur will ask you $$$q$$$ queries regarding his candies. For the $$$j$$$-th query you have to answer what is the minimum number of candies he needs to eat in order to reach a quantity of sugar greater than or equal to $$$x_j$$$ or print -1 if it's not possible to obtain such a quantity. In other words, you should print the minimum possible $$$k$$$ such that after eating $$$k$$$ candies, Timur consumes a quantity of sugar of at least $$$x_j$$$ or say that no possible $$$k$$$ exists.Note that he can't eat the same candy twice and queries are independent of each other (Timur can use the same candy in different queries).

The first line of input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. The description of test cases follows. The first line contains $$$2$$$ integers $$$n$$$ and $$$q$$$ ($$$1 \leq n, q \leq 1.5\cdot10^5$$$) — the number of candies Timur has and the number of queries you have to print an answer for respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^4$$$) — the quantity of sugar in each of the candies respectively. Then $$$q$$$ lines follow.  Each of the next $$$q$$$ lines contains a single integer $$$x_j$$$ ($$$1 \leq x_j \leq 2 \cdot 10^9$$$) – the quantity Timur wants to reach for the given query. It is guaranteed that the sum of $$$n$$$ and the sum of $$$q$$$ over all test cases do not exceed $$$1.5 \cdot 10^5$$$.

For each test case output $$$q$$$ lines. For the $$$j$$$-th line output the number of candies Timur needs to eat in order to reach a quantity of sugar greater than or equal to $$$x_j$$$ or print -1 if it's not possible to obtain such a quantity.

NoteFor the first test case:For the first query, Timur can eat any candy, and he will reach the desired quantity.For the second query, Timur can reach a quantity of at least $$$10$$$ by eating the $$$7$$$-th and the $$$8$$$-th candies, thus consuming a quantity of sugar equal to $$$14$$$.For the third query, there is no possible answer.For the fourth query, Timur can reach a quantity of at least $$$14$$$ by eating the $$$7$$$-th and the $$$8$$$-th candies, thus consuming a quantity of sugar equal to $$$14$$$.For the second test case:For the only query of the second test case, we can choose the third candy from which Timur receives exactly $$$3$$$ sugar. It's also possible to obtain the same answer by choosing the fourth candy.

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	buf := make([]byte, 0, 13*200000)
	scanner.Buffer(buf, 13*200000)

	var line string
	var strList []string
	if scanner.Scan() {
		line = scanner.Text()
	}

	t, _ := strconv.Atoi(line)

	for test := 0; test < t; test++ {
		if scanner.Scan() {
			line = scanner.Text()
		}

		strList = strings.Split(line, " ")
		n, _ := strconv.Atoi(strList[0])
		q, _ := strconv.Atoi(strList[1])

		if scanner.Scan() {
			line = scanner.Text()
		}

		strList = strings.Split(line, " ")

		list := make([]int64, n)
		for i := 0; i < n; i++ {
			list[i], _ = strconv.ParseInt(strList[i], 10, 64)
		}

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

		sum := make([]int64, n)
		for i := 0; i < n; i++ {
			var prev int
			if i == 0 {
				prev = 0
			} else {
				prev = i - 1
			}

			sum[i] = sum[prev] + list[i]
		}

		for i := 0; i < q; i++ {
			var quantityString string
			var quantity int64
			if scanner.Scan() {
				quantityString = scanner.Text()
				quantity, _ = strconv.ParseInt(quantityString, 10, 64)
			}

			candies := -1
			left := 1
			right := n
			for left <= right {
				mid := (left + right) / 2
				if sum[mid-1] >= quantity {
					candies = mid
					right = mid - 1
				} else {
					left = mid + 1
				}
			}

			fmt.Println(candies)
		}
	}
}

package main

import(
	"fmt"
	"sort"
	"bufio"
	"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 main(){
	defer writer.Flush()
	var t int
	scanf("%d\n",&t)
	for i := 0; i < t; i++ {
		var n, q int
		scanf("%d %d\n", &n, &q)
		v := make([]int, n)
		pref := make([]int, n+1)
		for i :=0; i < n; i++ {
			scanf("%d", &v[i])
		}
		scanf("\n")

		sort.Slice(v, func(i, j int) bool {
			return v[i] > v[j]
		})
		for i := 1; i <= n; i++ {
			pref[i] = pref[i-1] + v[i-1]
		}
		for i :=0; i < q; i++ {
			var x int
			scanf("%d\n", &x)
			l := -1
			r := n+1
			for l+1 < r {
				m := (l+r)/2
				if(pref[m] >= x) {
					r=m
				} else {
					l=m
				}
			}
			if(r != n+1) {
				printf("%d \n", r)
			} else {
				printf("%d \n", -1)
			}

		}

	}

}

package main

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

func binary_search(val int, x []int) int {
	l := 0
	h := len(x) - 1
	//m := m = (l + h) / 2
	for l <= h {
		m := (l + h) / 2
		if val > x[m] {
			l = m + 1
		} else {
			h = m - 1
		}
	}
	return h
}

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)
		n, _ := strconv.Atoi(ninps[0])
		q, _ := strconv.Atoi(ninps[1])

		x := make([]int, n)

		text, _ = reader.ReadString('\n')
		ninps = strings.Fields(text)
		for i, val := range ninps {
			x[i], _ = strconv.Atoi(val)
		}

		sort.Sort(sort.Reverse(sort.IntSlice(x)))

		sum := 0
		for i := 0; i < n; i++ {
			sum += x[i]
			x[i] = sum
		}

		for i := 0; i < q; i++ {
			text, _ = reader.ReadString('\n')
			ninps = strings.Fields(text)
			query, _ := strconv.Atoi(ninps[0])
			if query <= sum {
				ans := binary_search(query, x) + 2
				fmt.Println(ans)
			} else {
                                fmt.Println(-1)
			}
		}
	}
}

package main

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

func main() {
	r := readerStdin()
	t := readInt(r)

	for t != 0 {
		t--

		dataCandies, candies := readSliceInt(r), readSliceInt(r)

		n, q := dataCandies[0], dataCandies[1]

		sort.Ints(candies)

		prefixSum := make([]int, len(candies))
		prefixSum[0] = candies[n-1]
		for i := 1; i < n; i++ {
			prefixSum[i] = prefixSum[i-1] + candies[n-i-1]
		}

		for q != 0 {
			q--

			sugar := readInt(r)

			quantity := sort.SearchInts(prefixSum, sugar)
			if quantity == n {
				fmt.Println(-1)
			} else {
				fmt.Println(quantity + 1)
			}
		}
	}
}

func readerStdin() *bufio.Reader {
	return bufio.NewReader(os.Stdin)
}

func readString(r *bufio.Reader) (s string) {
	s, _ = r.ReadString('\n')
	s = strings.ReplaceAll(s, "\n", "")
	s = strings.ReplaceAll(s, "\r", "")
	return
}

func readInt(r *bufio.Reader) (i int) {
	str := readString(r)
	i, _ = strconv.Atoi(str)
	return
}

func readSliceInt(r *bufio.Reader) []int {
	str := readString(r)
	slice := strings.Split(str, " ")

	var result = make([]int, len(slice))
	for i, v := range slice {
		x, _ := strconv.Atoi(v)
		result[i] = x
	}

	return result
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var T,N,M,A,max int

	//num := make(map[int]int)

	fmt.Fscan(in, &T)
	for i := 0;i < T;i++{
		fmt.Fscan(in, &N,&M)
		list := make([]int,N)
		for j := 0;j < N;j++{
			fmt.Fscan(in, &A)
			list[j] = A
			max += A
		}
		sort.Sort(sort.Reverse(sort.IntSlice(list)))

		for j := 1;j < N;j++{
			list[j] += list[j-1]
		}
		for j := 0;j < M;j++{
			fmt.Fscan(in, &A)
			if max < A{
				fmt.Println(-1)
			}else{
				fmt.Println(sort.SearchInts(list,A)+1)
			}
		}
		max = 0
	}
}

package main

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

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

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

	t := 0
	fmt.Fscan(in, &t)
	for k := 0; k < t; k++ {
		n, q := 0, 0
		fmt.Fscan(in, &n, &q)
		a := make([]int, n+1)
		for i := 1; i <= n; i++ {
			fmt.Fscan(in, &a[i])
		}
		sort.Slice(a, func(i, j int) bool {
			return a[i] > a[j]
		})
		presum := make([]int, n+1)
		for i := 1; i <= n; i++ {
			presum[i] = presum[i-1] + a[i-1]
		}
		for i := 0; i < q; i++ {
			qq := 0
			fmt.Fscan(in, &qq)
			x := sort.Search(n+1, func(j int) bool {
				return presum[j] >= qq
			})
			if x == n+1 {
				fmt.Fprintln(out, -1)
			} else {
				fmt.Fprintln(out, x)
			}
		}
	}
}

// Code generated by golang.org/x/tools/cmd/bundle. DO NOT EDIT.
//go:generate bundle -o output.go -prefix  .

package main

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

// Signed is a constraint that permits any signed integer type.
// If future releases of Go add new predeclared signed integer types,
// this constraint will be modified to include them.
type constraints_Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

// Unsigned is a constraint that permits any unsigned integer type.
// If future releases of Go add new predeclared unsigned integer types,
// this constraint will be modified to include them.
type constraints_Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

// Integer is a constraint that permits any integer type.
// If future releases of Go add new predeclared integer types,
// this constraint will be modified to include them.
type constraints_Integer interface {
	constraints_Signed | constraints_Unsigned
}

// Float is a constraint that permits any floating-point type.
// If future releases of Go add new predeclared floating-point types,
// this constraint will be modified to include them.
type constraints_Float interface {
	~float32 | ~float64
}

// Complex is a constraint that permits any complex numeric type.
// If future releases of Go add new predeclared complex numeric types,
// this constraint will be modified to include them.
type constraints_Complex interface {
	~complex64 | ~complex128
}

// Ordered is a constraint that permits any ordered type: any type
// that supports the operators < <= >= >.
// If future releases of Go add new ordered types,
// this constraint will be modified to include them.
type constraints_Ordered interface {
	constraints_Integer | constraints_Float | ~string
}

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

	n := readInt(in)
	for i := 0; i < n; i++ {
		solution(in, out)
	}
}

func solution(in *bufio.Reader, out *bufio.Writer) {
	v := readArrInt(in)
	q := v[1]

	arr := readArrInt(in)
	sort.Sort(sort.Reverse(sort.IntSlice(arr)))
	tm := treemap_New[int, int]()
	sum := 0
	for i, v := range arr {
		sum += v
		tm.Set(sum, i)
	}

	for i := 0; i < q; i++ {
		// find first higher then
		val := readInt(in)
		if val > sum {
			out.WriteString("-1\n")
			continue
		}
		ans := tm.LowerBound(val).Value() + 1
		out.WriteString(strconv.Itoa(ans) + "\n")
	}
}

func Max(sum int, sum2 int) int {
	if sum > sum2 {
		return sum
	}
	return sum2

}

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

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLine(in *bufio.Reader) string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	return line
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

func readArrInt64(in *bufio.Reader) []int64 {
	numbs := readLineNumbs(in)
	arr := make([]int64, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.ParseInt(n, 10, 64)
		arr[i] = val
	}
	return arr
}

// TreeMap is the generic red-black tree based map
type treemap_TreeMap[Key, Value any] struct {
	endNode    *treemap_node[Key, Value]
	beginNode  *treemap_node[Key, Value]
	count      int
	keyCompare func(a Key, b Key) bool
}

type treemap_node[Key, Value any] struct {
	right   *treemap_node[Key, Value]
	left    *treemap_node[Key, Value]
	parent  *treemap_node[Key, Value]
	isBlack bool
	key     Key
	value   Value
}

// New creates and returns new TreeMap.
func treemap_New[Key constraints_Ordered, Value any]() *treemap_TreeMap[Key, Value] {
	endNode := &treemap_node[Key, Value]{isBlack: true}
	return &treemap_TreeMap[Key, Value]{beginNode: endNode, endNode: endNode, keyCompare: treemap_defaultKeyCompare[Key]}
}

// NewWithKeyCompare creates and returns new TreeMap with the specified key compare function.
// Parameter keyCompare is a function returning a < b.
func treemap_NewWithKeyCompare[Key, Value any](
	keyCompare func(a, b Key) bool,
) *treemap_TreeMap[Key, Value] {
	endNode := &treemap_node[Key, Value]{isBlack: true}
	return &treemap_TreeMap[Key, Value]{beginNode: endNode, endNode: endNode, keyCompare: keyCompare}
}

// Len returns total count of elements in a map.
// Complexity: O(1).
func (t *treemap_TreeMap[Key, Value]) Len() int { return t.count }

// Set sets the value and silently overrides previous value if it exists.
// Complexity: O(log N).
func (t *treemap_TreeMap[Key, Value]) Set(key Key, value Value) {
	parent := t.endNode
	current := parent.left
	less := true
	for current != nil {
		parent = current
		switch {
		case t.keyCompare(key, current.key):
			current = current.left
			less = true
		case t.keyCompare(current.key, key):
			current = current.right
			less = false
		default:
			current.value = value
			return
		}
	}
	x := &treemap_node[Key, Value]{parent: parent, value: value, key: key}
	if less {
		parent.left = x
	} else {
		parent.right = x
	}
	if t.beginNode.left != nil {
		t.beginNode = t.beginNode.left
	}
	t.insertFixup(x)
	t.count++
}

// Del deletes the value.
// Complexity: O(log N).
func (t *treemap_TreeMap[Key, Value]) Del(key Key) {
	z := t.findNode(key)
	if z == nil {
		return
	}
	if t.beginNode == z {
		if z.right != nil {
			t.beginNode = z.right
		} else {
			t.beginNode = z.parent
		}
	}
	t.count--
	treemap_removeNode(t.endNode.left, z)
}

// Clear clears the map.
// Complexity: O(1).
func (t *treemap_TreeMap[Key, Value]) Clear() {
	t.count = 0
	t.beginNode = t.endNode
	t.endNode.left = nil
}

// Get retrieves a value from a map for specified key and reports if it exists.
// Complexity: O(log N).
func (t *treemap_TreeMap[Key, Value]) Get(id Key) (Value, bool) {
	node := t.findNode(id)
	if node == nil {
		node = t.endNode
	}
	return node.value, node != t.endNode
}

// Contains checks if key exists in a map.
// Complexity: O(log N)
func (t *treemap_TreeMap[Key, Value]) Contains(id Key) bool { return t.findNode(id) != nil }

// Range returns a pair of iterators that you can use to go through all the keys in the range [from, to].
// More specifically it returns iterators pointing to lower bound and upper bound.
// Complexity: O(log N).
func (t *treemap_TreeMap[Key, Value]) Range(from, to Key) (treemap_ForwardIterator[Key, Value], treemap_ForwardIterator[Key, Value]) {
	return t.LowerBound(from), t.UpperBound(to)
}

// LowerBound returns an iterator pointing to the first element that is not less than the given key.
// Complexity: O(log N).
func (t *treemap_TreeMap[Key, Value]) LowerBound(key Key) treemap_ForwardIterator[Key, Value] {
	result := t.endNode
	node := t.endNode.left
	if node == nil {
		return treemap_ForwardIterator[Key, Value]{tree: t, node: t.endNode}
	}
	for {
		if t.keyCompare(node.key, key) {
			if node.right != nil {
				node = node.right
			} else {
				return treemap_ForwardIterator[Key, Value]{tree: t, node: result}
			}
		} else {
			result = node
			if node.left != nil {
				node = node.left
			} else {
				return treemap_ForwardIterator[Key, Value]{tree: t, node: result}
			}
		}
	}
}

// UpperBound returns an iterator pointing to the first element that is greater than the given key.
// Complexity: O(log N).
func (t *treemap_TreeMap[Key, Value]) UpperBound(key Key) treemap_ForwardIterator[Key, Value] {
	result := t.endNode
	node := t.endNode.left
	if node == nil {
		return treemap_ForwardIterator[Key, Value]{tree: t, node: t.endNode}
	}
	for {
		if !t.keyCompare(key, node.key) {
			if node.right != nil {
				node = node.right
			} else {
				return treemap_ForwardIterator[Key, Value]{tree: t, node: result}
			}
		} else {
			result = node
			if node.left != nil {
				node = node.left
			} else {
				return treemap_ForwardIterator[Key, Value]{tree: t, node: result}
			}
		}
	}
}

// Iterator returns an iterator for tree map.
// It starts at the first element and goes to the one-past-the-end position.
// You can iterate a map at O(N) complexity.
// Method complexity: O(1)
func (t *treemap_TreeMap[Key, Value]) Iterator() treemap_ForwardIterator[Key, Value] {
	return treemap_ForwardIterator[Key, Value]{tree: t, node: t.beginNode}
}

// Reverse returns a reverse iterator for tree map.
// It starts at the last element and goes to the one-before-the-start position.
// You can iterate a map at O(N) complexity.
// Method complexity: O(log N)
func (t *treemap_TreeMap[Key, Value]) Reverse() treemap_ReverseIterator[Key, Value] {
	node := t.endNode.left
	if node != nil {
		node = treemap_mostRight(node)
	}
	return treemap_ReverseIterator[Key, Value]{tree: t, node: node}
}

func treemap_defaultKeyCompare[Key constraints_Ordered](
	a, b Key,
) bool {
	return a < b
}

func (t *treemap_TreeMap[Key, Value]) findNode(id Key) *treemap_node[Key, Value] {
	current := t.endNode.left
	for current != nil {
		switch {
		case t.keyCompare(id, current.key):
			current = current.left
		case t.keyCompare(current.key, id):
			current = current.right
		default:
			return current
		}
	}
	return nil
}

func treemap_mostLeft[Key, Value any](
	x *treemap_node[Key, Value],
) *treemap_node[Key, Value] {
	for x.left != nil {
		x = x.left
	}
	return x
}

func treemap_mostRight[Key, Value any](
	x *treemap_node[Key, Value],
) *treemap_node[Key, Value] {
	for x.right != nil {
		x = x.right
	}
	return x
}

func treemap_successor[Key, Value any](
	x *treemap_node[Key, Value],
) *treemap_node[Key, Value] {
	if x.right != nil {
		return treemap_mostLeft(x.right)
	}
	for x != x.parent.left {
		x = x.parent
	}
	return x.parent
}

func treemap_predecessor[Key, Value any](
	x *treemap_node[Key, Value],
) *treemap_node[Key, Value] {
	if x.left != nil {
		return treemap_mostRight(x.left)
	}
	for x.parent != nil && x != x.parent.right {
		x = x.parent
	}
	return x.parent
}

func treemap_rotateLeft[Key, Value any](
	x *treemap_node[Key, Value],
) {
	y := x.right
	x.right = y.left
	if x.right != nil {
		x.right.parent = x
	}
	y.parent = x.parent
	if x == x.parent.left {
		x.parent.left = y
	} else {
		x.parent.right = y
	}
	y.left = x
	x.parent = y
}

func treemap_rotateRight[Key, Value any](
	x *treemap_node[Key, Value],
) {
	y := x.left
	x.left = y.right
	if x.left != nil {
		x.left.parent = x
	}
	y.parent = x.parent
	if x == x.parent.left {
		x.parent.left = y
	} else {
		x.parent.right = y
	}
	y.right = x
	x.parent = y
}

func (t *treemap_TreeMap[Key, Value]) insertFixup(x *treemap_node[Key, Value]) {
	root := t.endNode.left
	x.isBlack = x == root
	for x != root && !x.parent.isBlack {
		if x.parent == x.parent.parent.left {
			y := x.parent.parent.right
			if y != nil && !y.isBlack {
				x = x.parent
				x.isBlack = true
				x = x.parent
				x.isBlack = x == root
				y.isBlack = true
			} else {
				if x != x.parent.left {
					x = x.parent
					treemap_rotateLeft(x)
				}
				x = x.parent
				x.isBlack = true
				x = x.parent
				x.isBlack = false
				treemap_rotateRight(x)
				break
			}
		} else {
			y := x.parent.parent.left
			if y != nil && !y.isBlack {
				x = x.parent
				x.isBlack = true
				x = x.parent
				x.isBlack = x == root
				y.isBlack = true
			} else {
				if x == x.parent.left {
					x = x.parent
					treemap_rotateRight(x)
				}
				x = x.parent
				x.isBlack = true
				x = x.parent
				x.isBlack = false
				treemap_rotateLeft(x)
				break
			}
		}
	}
}

//nolint:gocyclo
//noinspection GoNilness
func treemap_removeNode[Key, Value any](
	root, z *treemap_node[Key, Value],
) {
	var y *treemap_node[Key, Value]
	if z.left == nil || z.right == nil {
		y = z
	} else {
		y = treemap_successor(z)
	}
	var x *treemap_node[Key, Value]
	if y.left != nil {
		x = y.left
	} else {
		x = y.right
	}
	var w *treemap_node[Key, Value]
	if x != nil {
		x.parent = y.parent
	}
	if y == y.parent.left {
		y.parent.left = x
		if y != root {
			w = y.parent.right
		} else {
			root = x // w == nil
		}
	} else {
		y.parent.right = x
		w = y.parent.left
	}
	removedBlack := y.isBlack
	if y != z {
		y.parent = z.parent
		if z == z.parent.left {
			y.parent.left = y
		} else {
			y.parent.right = y
		}
		y.left = z.left
		y.left.parent = y
		y.right = z.right
		if y.right != nil {
			y.right.parent = y
		}
		y.isBlack = z.isBlack
		if root == z {
			root = y
		}
	}
	if removedBlack && root != nil {
		if x != nil {
			x.isBlack = true
		} else {
			for {
				if w != w.parent.left {
					if !w.isBlack {
						w.isBlack = true
						w.parent.isBlack = false
						treemap_rotateLeft(w.parent)
						if root == w.left {
							root = w
						}
						w = w.left.right
					}
					if (w.left == nil || w.left.isBlack) && (w.right == nil || w.right.isBlack) {
						w.isBlack = false
						x = w.parent
						if x == root || !x.isBlack {
							x.isBlack = true
							break
						}
						if x == x.parent.left {
							w = x.parent.right
						} else {
							w = x.parent.left
						}
					} else {
						if w.right == nil || w.right.isBlack {
							w.left.isBlack = true
							w.isBlack = false
							treemap_rotateRight(w)
							w = w.parent
						}
						w.isBlack = w.parent.isBlack
						w.parent.isBlack = true
						w.right.isBlack = true
						treemap_rotateLeft(w.parent)
						break
					}
				} else {
					if !w.isBlack {
						w.isBlack = true
						w.parent.isBlack = false
						treemap_rotateRight(w.parent)
						if root == w.right {
							root = w
						}
						w = w.right.left
					}
					if (w.left == nil || w.left.isBlack) && (w.right == nil || w.right.isBlack) {
						w.isBlack = false
						x = w.parent
						if !x.isBlack || x == root {
							x.isBlack = true
							break
						}
						if x == x.parent.left {
							w = x.parent.right
						} else {
							w = x.parent.left
						}
					} else {
						if w.left == nil || w.left.isBlack {
							w.right.isBlack = true
							w.isBlack = false
							treemap_rotateLeft(w)
							w = w.parent
						}
						w.isBlack = w.parent.isBlack
						w.parent.isBlack = true
						w.left.isBlack = true
						treemap_rotateRight(w.parent)
						break
					}
				}
			}
		}
	}
}

// ForwardIterator represents a position in a tree map.
// It is designed to iterate a map in a forward order.
// It can point to any position from the first element to the one-past-the-end element.
type treemap_ForwardIterator[Key, Value any] struct {
	tree *treemap_TreeMap[Key, Value]
	node *treemap_node[Key, Value]
}

// Valid reports if the iterator position is valid.
// In other words it returns true if an iterator is not at the one-past-the-end position.
func (i treemap_ForwardIterator[Key, Value]) Valid() bool { return i.node != i.tree.endNode }

// Next moves an iterator to the next element.
// It panics if it goes out of bounds.
func (i *treemap_ForwardIterator[Key, Value]) Next() {
	if i.node == i.tree.endNode {
		panic("out of bound iteration")
	}
	i.node = treemap_successor(i.node)
}

// Prev moves an iterator to the previous element.
// It panics if it goes out of bounds.
func (i *treemap_ForwardIterator[Key, Value]) Prev() {
	i.node = treemap_predecessor(i.node)
	if i.node == nil {
		panic("out of bound iteration")
	}
}

// Key returns a key at the iterator position
func (i treemap_ForwardIterator[Key, Value]) Key() Key { return i.node.key }

// Value returns a value at the iterator position
func (i treemap_ForwardIterator[Key, Value]) Value() Value { return i.node.value }

// ReverseIterator represents a position in a tree map.
// It is designed to iterate a map in a reverse order.
// It can point to any position from the one-before-the-start element to the last element.
type treemap_ReverseIterator[Key, Value any] struct {
	tree *treemap_TreeMap[Key, Value]
	node *treemap_node[Key, Value]
}

// Valid reports if the iterator position is valid.
// In other words it returns true if an iterator is not at the one-before-the-start position.
func (i treemap_ReverseIterator[Key, Value]) Valid() bool { return i.node != nil }

// Next moves an iterator to the next element in reverse order.
// It panics if it goes out of bounds.
func (i *treemap_ReverseIterator[Key, Value]) Next() {
	if i.node == nil {
		panic("out of bound iteration")
	}
	i.node = treemap_predecessor(i.node)
}

// Prev moves an iterator to the previous element in reverse order.
// It panics if it goes out of bounds.
func (i *treemap_ReverseIterator[Key, Value]) Prev() {
	if i.node != nil {
		i.node = treemap_successor(i.node)
	} else {
		i.node = i.tree.beginNode
	}
	if i.node == i.tree.endNode {
		panic("out of bound iteration")
	}
}

// Key returns a key at the iterator position
func (i treemap_ReverseIterator[Key, Value]) Key() Key { return i.node.key }

// Value returns a value at the iterator position
func (i treemap_ReverseIterator[Key, Value]) Value() Value { return i.node.value }

package main

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

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

	var T int
	for fmt.Fscan(in, &T); T > 0; T-- {
		var n, q int
		fmt.Fscan(in, &n, &q)
		a := make([]int, n)
		for i := 0;i < n; i++ {
			fmt.Fscan(in, &a[i])
		}
		sort.Slice(a, func(i, j int) bool {
			return a[i] > a[j]
		})
		for i := 1;i < n; i++ {
			a[i] += a[i - 1]
		}

		for i := 0;i < q; i++ {
			var x int
			fmt.Fscan(in, &x)
			index := sort.Search(len(a), func(i int) bool {
				return a[i] >= x
			})
			if index == n {
				index = -2
			}
			fmt.Fprintln(out, index + 1)
		}
	}
}

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

package main

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

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

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

	var T int
	var n int
	var m int
	var x int

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

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

		a := make([]int, n)
		sum :=0
		for i := n - 1; i >= 0; i-- {
			sum += r[i]
			a[n-1-i] = sum
		}

		for j := 0; j < m; j++ {
			Fscan(in, &x)
			ans := sort.SearchInts(a, x)
			if ans == n {
				Fprintln(out, -1)
			} else {
				Fprintln(out, ans+1)
			}
		}
	}
}

package main

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

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

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

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

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

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

		for i := 0; i < q; i++ {
			var v int
			fmt.Fscan(in, &v)
			x := sort.Search(n, func(i int) bool { return int(g[i]) >= v })
			x++
			if x > n {
				x = -1
			}
			fmt.Fprintln(out, x)
		}
	}
}

package main

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

//func max(a, b int) int {if a > b {return a} else {return b}}
var r = bufio.NewReader(os.Stdin)
var w = bufio.NewWriter(os.Stdout)

func slove() {
	var n, q int
	fmt.Fscan(r, &n, &q)
	nums := make([]int, n)
	for i:=0;i<n;i++ {
		fmt.Fscan(r, &nums[i])
	}
	sort.Ints(nums)
	prefix := make([]int, n+1)
	for i := n-1; i >=0 ; i-- {
		prefix[n-i] = prefix[n-i-1] + nums[i]
	}
	for i := 0; i < q; i++ {
		var tar int
		fmt.Fscan(r, &tar)
		j := sort.Search(n, func(j int) bool {
			return prefix[j+1] >= tar
		})
		if j >= n {
			fmt.Fprintln(w, -1)
		} else {
			fmt.Fprintln(w, j+1)
		}
	}
}

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

package main

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

//goland:noinspection GoUnusedFunction
func min(a int, nums ...int) int {
	min := a

	for _, num := range nums {
		if num < min {
			min = num
		}
	}
	return min
}

//goland:noinspection GoUnusedFunction
func max(a int, nums ...int) int {
	max := a

	for _, num := range nums {
		if num > max {
			max = num
		}
	}
	return max
}

//goland:noinspection GoUnusedFunction
func maxFloat(a float64, nums ...float64) float64 {
	max := a

	for _, num := range nums {
		if num > max {
			max = num
		}
	}
	return max
}

//goland:noinspection GoUnusedFunction
func fprintlnArr(w io.Writer, arr []int) {
	for _, val := range arr {
		fmt.Fprint(w, val, " ")
	}
	fmt.Fprintln(w)
}

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

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

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

		cnt := make([]int, n)
		sum := 0
		for i := 0; i < n; i++ {
			sum += arr[i]
			cnt[i] = sum
		}

		for i := 0; i < m; i++ {
			var cur int
			fmt.Fscan(in, &cur)
			res := sort.SearchInts(cnt, cur) + 1
			if res > n {
				fmt.Fprintln(out, -1)
			} else {
				fmt.Fprintln(out, res)
			}
		}
	}
}

package main

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

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

	solve := func(curCase int) {
		var n,q int
		Fscan(in, &n,&q)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		sort.Sort(sort.Reverse(sort.IntSlice(a)))
		sum := make([]int, n+1)
		for i, v := range a {
			sum[i+1] = sum[i] + v
		}

		for qi := 0; qi < q; qi++ {
			var x int
			Fscan(in, &x)
			i := sort.SearchInts(sum, x)
			if i == n+1 {
				i = -1
			}
			Fprintln(out, i)
		}

	}

	cases := 1
	Fscan(in, &cases)
	for curCase := 1; curCase <= cases; curCase++ {
		solve(curCase)
	}

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

}

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

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	buf := make([]byte, 0, 13*200000)
	scanner.Buffer(buf, 13*200000)

	var line string
	var strList []string
	if scanner.Scan() {
		line = scanner.Text()
	}

	t, _ := strconv.Atoi(line)

	for test := 0; test < t; test++ {
		if scanner.Scan() {
			line = scanner.Text()
		}

		strList = strings.Split(line, " ")
		n, _ := strconv.Atoi(strList[0])
		q, _ := strconv.Atoi(strList[1])

		if scanner.Scan() {
			line = scanner.Text()
		}

		strList = strings.Split(line, " ")

		list := make([]int64, n)
		var sum int64
		sum = 0
		for i := 0; i < n; i++ {
			list[i], _ = strconv.ParseInt(strList[i], 10, 64)
			sum += list[i]
		}

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

		for i := 0; i < q; i++ {
			var quantityString string
			var quantity int64
			if scanner.Scan() {
				quantityString = scanner.Text()
				quantity, _ = strconv.ParseInt(quantityString, 10, 64)
			}

			if quantity > sum {
				fmt.Println("-1")
				continue
			}

			if quantity == sum {
				fmt.Println(n)
				continue
			}

			var current int64
			current = 0
			diff := sum - quantity
			if n < 10 || diff >= sum/2 {
				for j := 0; j < n; j++ {
					current += list[j]
					if current >= quantity {
						fmt.Println(j + 1)
						break
					}
				}
			} else {
				for j := n - 1; j >= 0; j-- {
					current += list[j]
					if current >= diff {
						fmt.Println(j)
						break
					}
				}
			}
		}
	}
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	buf := make([]byte, 0, 13*200000)
	scanner.Buffer(buf, 13*200000)

	var line string
	var strList []string
	if scanner.Scan() {
		line = scanner.Text()
	}

	t, _ := strconv.Atoi(line)

	for test := 0; test < t; test++ {
		if scanner.Scan() {
			line = scanner.Text()
		}

		strList = strings.Split(line, " ")
		n, _ := strconv.Atoi(strList[0])
		q, _ := strconv.Atoi(strList[1])

		if scanner.Scan() {
			line = scanner.Text()
		}

		strList = strings.Split(line, " ")

		list := make([]int64, n)
		var sum int64
		sum = 0
		for i := 0; i < n; i++ {
			list[i], _ = strconv.ParseInt(strList[i], 10, 64)
			sum += list[i]
		}

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

		for i := 0; i < q; i++ {
			var quantityString string
			var quantity int64
			if scanner.Scan() {
				quantityString = scanner.Text()
				quantity, _ = strconv.ParseInt(quantityString, 10, 64)
			}

			if quantity > sum {
				fmt.Println("-1")
				continue
			}

			if quantity == sum {
				fmt.Println(n)
				continue
			}

			var current int64
			current = 0
			diff := sum - quantity
			if diff >= sum/2 {
				for j := 0; j < n; j++ {
					current += list[j]
					if current >= quantity {
						fmt.Println(j + 1)
						break
					}
				}
			} else {
				for j := n - 1; j >= 0; j-- {
					current += list[j]
					if current >= diff {
						fmt.Println(j)
						break
					}
				}
			}
		}
	}
}

package main

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

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

	for test := 0; test < t; test++ {

		var n, q int
		fmt.Scanf("%d %d\n", &n, &q)

		var line string
		scanner := bufio.NewScanner(os.Stdin)
		buf := make([]byte, 0, 13*200000)
		scanner.Buffer(buf, 13*200000)

		if scanner.Scan() {
			line = scanner.Text()
		}

		strList := strings.Split(line, " ")

		list := make([]int64, n)
		var sum int64
		sum = 0
		for i := 0; i < n; i++ {
			list[i], _ = strconv.ParseInt(strList[i], 10, 64)
			sum += list[i]
		}

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

		for i := 0; i < q; i++ {
			var quantityString string
			var quantity int64
			if scanner.Scan() {
				quantityString = scanner.Text()
				quantity, _ = strconv.ParseInt(quantityString, 10, 64)
			}

			if quantity > sum {
				fmt.Println("-1")
				continue
			}

			var current int64
			current = 0
			for j := 0; j < n; j++ {
				current += list[j]
				if current >= quantity {
					fmt.Println(j + 1)
					break
				}
			}
		}
	}
}

package main

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

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

	for test := 0; test < t; test++ {

		var n, q int
		fmt.Scanf("%d %d\n", &n, &q)

		var line string
		scanner := bufio.NewScanner(os.Stdin)
		buf := make([]byte, 0, 13*200000)
		scanner.Buffer(buf, 13*200000)

		if scanner.Scan() {
			line = scanner.Text()
		}

		strList := strings.Split(line, " ")

		list := make([]int64, n)
		var sum int64
		sum = 0
		for i := 0; i < n; i++ {
			list[i], _ = strconv.ParseInt(strList[i], 10, 64)
			sum += list[i]
		}

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

		for i := 0; i < q; i++ {
			var quantity int64
			fmt.Scanln(&quantity)

			if quantity > sum {
				fmt.Println("-1")
				continue
			}

			var current int64
			current = 0
			for j := 0; j < n; j++ {
				current += list[j]
				fmt.Printf("%d %d %d", list[1], current, quantity)
				if current >= quantity {
					fmt.Println(j + 1)
					break
				}
			}
		}
	}
}

package main

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

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

	for test := 0; test < t; test++ {

		var n, q int
		fmt.Scanf("%d %d\n", &n, &q)

		var line string
		scanner := bufio.NewScanner(os.Stdin)
		buf := make([]byte, 0, 13*200000)
		scanner.Buffer(buf, 13*200000)

		if scanner.Scan() {
			line = scanner.Text()
		}

		strList := strings.Split(line, " ")

		list := make([]int64, n)
		var sum int64
		sum = 0
		for i := 0; i < n; i++ {
			list[i], _ = strconv.ParseInt(strList[i], 10, 64)
			sum += list[i]
		}

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

		for i := 0; i < q; i++ {
			var quantity int64
			fmt.Scanln(&quantity)

			if quantity > sum {
				fmt.Println("-1")
				continue
			}

			var current int64
			current = 0
			for j := 0; j < n; j++ {
				current += list[j]
				if current >= quantity {
					fmt.Println(j + 1)
					break
				}
			}
		}
	}
}

package main

import(
	"fmt"
	"sort"
)

func main(){
	var t int
	fmt.Scanf("%d",&t)
	for i := 0; i < t; i++ {
		var n, q int
		fmt.Scanf("%d %d", &n, &q)
		v := make([]int, n)
		pref := make([]int, n+1)
		for i :=0; i < n; i++ {
			fmt.Scanf("%d", &v[i])
		}
		sort.Slice(v, func(i, j int) bool {
			return v[i] > v[j]
		})
		for i := 1; i <= n; i++ {
			pref[i] = pref[i-1] + v[i-1]
		}

		for i :=0; i < q; i++ {
			var x int
			fmt.Scanf("%d", &x)
			l := -1
			r := n+1
			for l+1 < r {
				m := (l+r)/2
				if(pref[m] >= x) {
					r=m
				} else {
					l=m
				}
			}
			if(r != n+1) {
				fmt.Printf("%d \n", r)
			} else {
				fmt.Printf("%d \n", -1)
			}

		}

	}

}

package main

import(
	"fmt"
	"sort"
)

func main(){
	var t int
	fmt.Scanf("%d",&t)
	for i := 0; i < t; i++ {
		var n, q int
		fmt.Scanf("%d %d", &n, &q)
		v := make([]int, n)
		pref := make([]int, n+1)
		for i :=0; i < n; i++ {
			fmt.Scanf("%d", &v[i])
		}
		sort.Slice(v, func(i, j int) bool {
			return v[i] > v[j]
		})
		for i := 1; i <= n; i++ {
			pref[i] = pref[i-1] + v[i-1]
		}
		fmt.Println(v)
		fmt.Println(pref)

		for i :=0; i < q; i++ {
			var x int
			fmt.Scanf("%d", &x)
			l := -1
			r := n+1
			for l+1 < r {
				m := (l+r)/2
				if(pref[m] >= x) {
					r=m
				} else {
					l=m
				}
			}
			if(r != n+1) {
				fmt.Printf("%d \n", r)
			} else {
				fmt.Printf("%d \n", -1)
			}

		}

	}

}

package main

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

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

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

	t := 0
	fmt.Fscan(in, &t)
	for k := 0; k < t; k++ {
		n, q := 0, 0
		fmt.Fscan(in, &n, &q)
		fmt.Println(n, q)
		a := make([]int, n+1)
		for i := 1; i <= n; i++ {
			fmt.Fscan(in, &a[i])
		}
		sort.Slice(a, func(i, j int) bool {
			return a[i] > a[j]
		})
		presum := make([]int, n+1)
		for i := 1; i <= n; i++ {
			presum[i] = presum[i-1] + a[i-1]
		}
		for i := 0; i < q; i++ {
			qq := 0
			fmt.Fscan(in, &qq)
			x := sort.Search(n+1, func(j int) bool {
				return presum[j] >= qq
			})
			if x == n+1 {
				fmt.Fprintln(out, -1)
			} else {
				fmt.Fprintln(out, x)
			}
		}
	}
}


Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...$$$n$$$ friends live in a city which can be represented as a number line. The $$$i$$$-th friend lives in a house with an integer coordinate $$$x_i$$$. The $$$i$$$-th friend can come celebrate the New Year to the house with coordinate $$$x_i-1$$$, $$$x_i+1$$$ or stay at $$$x_i$$$. Each friend is allowed to move no more than once.For all friends $$$1 \le x_i \le n$$$ holds, however, they can come to houses with coordinates $$$0$$$ and $$$n+1$$$ (if their houses are at $$$1$$$ or $$$n$$$, respectively).For example, let the initial positions be $$$x = [1, 2, 4, 4]$$$. The final ones then can be $$$[1, 3, 3, 4]$$$, $$$[0, 2, 3, 3]$$$, $$$[2, 2, 5, 5]$$$, $$$[2, 1, 3, 5]$$$ and so on. The number of occupied houses is the number of distinct positions among the final ones.So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of friends. The second line contains $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le n$$$) — the coordinates of the houses of the friends.

Print two integers — the minimum and the maximum possible number of occupied houses after all moves are performed.

NoteIn the first example friends can go to $$$[2, 2, 3, 3]$$$. So friend $$$1$$$ goes to $$$x_1+1$$$, friend $$$2$$$ stays at his house $$$x_2$$$, friend $$$3$$$ goes to $$$x_3-1$$$ and friend $$$4$$$ goes to $$$x_4-1$$$. $$$[1, 1, 3, 3]$$$, $$$[2, 2, 3, 3]$$$ or $$$[2, 2, 4, 4]$$$ are also all valid options to obtain $$$2$$$ occupied houses.For the maximum number of occupied houses friends can go to $$$[1, 2, 3, 4]$$$ or to $$$[0, 2, 4, 5]$$$, for example.

package main

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

const N = 200005
var n int
var a[N] int
var b[N] int
var minMark[N] bool
var maxMark[N] bool

func main() {
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	fmt.Fscanf(reader, "%d\n", &n)
	for i := 1; i <= n; i++ {
		var x int
		fmt.Fscanf(reader, "%d", &x)
		a[x]++
	}

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

		if minMark[i] {
			continue
		}

		if a[i] > 0 && minMark[i - 1] {
			continue
		}

		if a[i] > 0 {
			minMark[i+1] = true
		}
	}

	for i := 0; i <= n + 1; i++ {
		if maxMark[i] {
			if a[i] > 0 {
				maxMark[i + 1] = true
			}
			continue
		}

		if a[i] > 0 {
			maxMark[i] = true
			if a[i] > 1 {
				maxMark[i + 1] = true
			}
			continue
		}

		if a[i + 1] > 0 {
			a[i + 1]--
			maxMark[i] = true
		}
	}

	var minHall = 0
	var maxHall = 0

	for i := 0; i <= n + 1; i++ {
		if minMark[i] {
			minHall++
		}
		if maxMark[i] {
			maxHall++
		}
	}

	fmt.Fprintf(writer, "%d %d", minHall, maxHall)
}

package main

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

var writer *bufio.Writer
var reader *bufio.Reader

func get_min(x []int) int {
    var ans int
    for ptr_l := 1; ptr_l < len(x); ans++ {
        var ptr_r = ptr_l
        for ; ptr_r + 1 < len(x) && x[ptr_r + 1] - x[ptr_l] <= 2; ptr_r++ {
        }
        ptr_l = ptr_r + 1
    }
    return ans
}

func get_max(x []int) int {
    var ans int
    var last = -1
    for i := 1; i < len(x); i++ {
        if x[i] - 1 > last {
            last = x[i] - 1
            ans++
        } else if x[i] > last {
            last = x[i]
            ans++
        } else if x[i] + 1 > last {
            last = x[i] + 1
            ans++
        }
    }
    return ans
}

func solve() {
    var n int
    fmt.Fscanf(reader, "%d\n", &n)
    x := make([]int, n + 1, n + 1)
    for i := 1; i <= n; i++ {
        fmt.Fscanf(reader, "%d ", &x[i])
    }
    sort.Ints(x)
    fmt.Fprintf(writer, "%d %d\n", get_min(x), get_max(x))
}

func main() {
    reader = bufio.NewReader(os.Stdin)
    writer = bufio.NewWriter(os.Stdout)
    defer writer.Flush()
    solve()
}


You are given a tree consisting of $$$n$$$ vertices, and $$$q$$$ triples $$$(x_i, y_i, s_i)$$$, where $$$x_i$$$ and $$$y_i$$$ are integers from $$$1$$$ to $$$n$$$, and $$$s_i$$$ is a string with length equal to the number of vertices on the simple path from $$$x_i$$$ to $$$y_i$$$.You want to write a lowercase Latin letter on each vertex in such a way that, for each of $$$q$$$ given triples, at least one of the following conditions holds:  if you write out the letters on the vertices on the simple path from $$$x_i$$$ to $$$y_i$$$ in the order they appear on this path, you get the string $$$s_i$$$;  if you write out the letters on the vertices on the simple path from $$$y_i$$$ to $$$x_i$$$ in the order they appear on this path, you get the string $$$s_i$$$. Find any possible way to write a letter on each vertex to meet these constraints, or report that it is impossible.

The first line contains two integers $$$n$$$ and $$$q$$$ ($$$2 \le n \le 4 \cdot 10^5$$$; $$$1 \le q \le 4 \cdot 10^5$$$) — the number of vertices in the tree and the number of triples, respectively. Then $$$n - 1$$$ lines follow; the $$$i$$$-th of them contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$; $$$u_i \ne v_i$$$) — the endpoints of the $$$i$$$-th edge. These edges form a tree. Then $$$q$$$ lines follow; the $$$j$$$-th of them contains two integers $$$x_j$$$ and $$$y_j$$$, and a string $$$s_j$$$ consisting of lowercase Latin letters. The length of $$$s_j$$$ is equal to the number of vertices on the simple path between $$$x_j$$$ and $$$y_j$$$. Additional constraint on the input: $$$\sum \limits_{j=1}^{q} |s_j| \le 4 \cdot 10^5$$$.

If there is no way to meet the conditions on all triples, print NO. Otherwise, print YES in the first line, and a string of $$$n$$$ lowercase Latin letters in the second line; the $$$i$$$-th character of the string should be the letter you write on the $$$i$$$-th vertex. If there are multiple answers, print any of them.

package main

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

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

	n, m := readTwoNums(reader)
	E := make([][]int, n-1)
	for i := 0; i < n-1; i++ {
		E[i] = readNNums(reader, 2)
	}
	X := make([]int, m)
	Y := make([]int, m)
	S := make([]string, m)
	for i := 0; i < m; i++ {
		s, _ := reader.ReadBytes('\n')
		pos := readInt(s, 0, &X[i]) + 1
		pos = readInt(s, pos, &Y[i]) + 1
		S[i] = string(s[pos:])
	}
	res := solve(n, E, X, Y, S)

	if len(res) == 0 {
		fmt.Println("NO")
	} else {
		fmt.Println("YES")
		fmt.Println(res)
	}
}

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
}

func readNInt64s(reader *bufio.Reader, n int) []int64 {
	res := make([]int64, n)
	s, _ := reader.ReadBytes('\n')

	var pos int

	for i := 0; i < n; i++ {
		pos = readInt64(s, pos, &res[i]) + 1
	}

	return res
}

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

func solve(n int, E [][]int, X []int, Y []int, S []string) string {

	g := NewGraph(n, len(E)*2)

	for _, e := range E {
		u, v := e[0]-1, e[1]-1
		g.AddEdge(u, v)
		g.AddEdge(v, u)
	}
	P := make([]int, n)
	H := make([]int, n)
	var dfs1 func(p, u int)

	dfs1 = func(p, u int) {
		P[u] = p
		for i := g.node[u]; i > 0; i = g.next[i] {
			v := g.to[i]
			if p != v {
				H[v] = H[u] + 1
				dfs1(u, v)
			}
		}
	}

	dfs1(0, 0)

	left := make([]int, n)
	right := make([]int, n)

	get_path := func(u, v int) []int {
		var l, r int

		for u != v {
			if H[u] > H[v] {
				left[l] = u
				l++
				u = P[u]
			} else {
				right[r] = v
				r++
				v = P[v]
			}
		}
		left[l] = u
		l++
		for r > 0 {
			left[l] = right[r-1]
			r--
			l++
		}
		res := make([]int, l)
		copy(res, left)
		return res
	}

	paths := make([][]int, len(X))
	opts := make([]Assign, n)

	for i := 0; i < n; i++ {
		opts[i] = Assign{'a', 'a', false}
	}

	m := len(X)

	for i := 0; i < m; i++ {
		paths[i] = get_path(X[i]-1, Y[i]-1)
		k := len(paths[i])
		for j := 0; j < len(paths[i]); j++ {
			opts[paths[i][j]] = Assign{S[i][j], S[i][k-1-j], true}
		}
	}
	var edges int
	for i := 0; i < m; i++ {
		k := len(paths[i])

		for j := 0; j < k; j++ {
			u := paths[i][j]
			c := S[i][j]
			rc := S[i][k-1-j]
			d := opts[u].first
			rd := opts[u].second
			if d != c {
				edges++
			}
			if d != rc {
				edges++
			}
			if rd != c {
				edges++
			}
			if rd != rc {
				edges++
			}
		}
	}

	nm := (n + m) * 2

	ga := NewGraph(nm, edges*2)
	gb := NewGraph(nm, edges*2)

	add_edge := func(u int, vx int, v int, vy int) {
		// (val[u] == vx) -> (val[v] == vy)
		// a -> b and !b -> !a
		ga.AddEdge(u*2+vx, v*2+vy)
		gb.AddEdge(v*2+vy, u*2+vx)
		ga.AddEdge(v*2+1-vy, u*2+1-vx)
		gb.AddEdge(u*2+1-vx, v*2+1-vy)
	}

	for i := 0; i < m; i++ {
		k := len(paths[i])

		for j := 0; j < k; j++ {
			u := paths[i][j]
			c := S[i][j]
			rc := S[i][k-1-j]
			d := opts[u].first
			rd := opts[u].second
			if d != c {
				add_edge(u, 0, n+i, 1)
			}
			if d != rc {
				add_edge(u, 0, n+i, 0)
			}
			if rd != c {
				add_edge(u, 1, n+i, 1)
			}
			if rd != rc {
				add_edge(u, 1, n+i, 0)
			}
		}
	}

	used := make([]bool, nm)
	ord := make([]int, 0, nm)

	var dfs2 func(u int)

	dfs2 = func(u int) {
		used[u] = true
		for i := ga.node[u]; i > 0; i = ga.next[i] {
			v := ga.to[i]
			if !used[v] {
				dfs2(v)
			}
		}
		ord = append(ord, u)
	}

	for u := 0; u < nm; u++ {
		if !used[u] {
			dfs2(u)
		}
	}

	comp := make([]int, nm)
	for i := 0; i < nm; i++ {
		comp[i] = -1
	}

	var dfs3 func(u int, c int)

	dfs3 = func(u int, c int) {
		comp[u] = c
		for i := gb.node[u]; i > 0; i = gb.next[i] {
			v := gb.to[i]
			if comp[v] < 0 {
				dfs3(v, c)
			}
		}
	}
	var ctr int
	for i := nm - 1; i >= 0; i-- {
		if comp[ord[i]] < 0 {
			dfs3(ord[i], ctr)
			ctr++
		}
	}

	for i := 0; i < nm; i++ {
		if comp[i] == comp[i^1] {
			return ""
		}
	}

	buf := make([]byte, n)

	for i := 0; i < 2*n; i += 2 {
		if !opts[i/2].assign {
			buf[i/2] = 'a'
		} else if comp[i] > comp[i^1] {
			buf[i/2] = opts[i/2].first
		} else {
			buf[i/2] = opts[i/2].second
		}
	}
	return string(buf)
}

type Assign struct {
	first  byte
	second byte
	assign bool
}

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
}