We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value  should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist.Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and".

The first line contains two integers n, m (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits. Each of the next m lines contains three integers li, ri, qi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit.

If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them. If the interesting array doesn't exist, print "NO" (without the quotes) in the single line.

package main

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

var segTree [300000]int
var l, r, q [100000]int

func treeUpdate(index, l, r, left, right, mask int) {
	mid := (l + r) / 2
	if l == left && r == right {
		segTree[index] |= mask
	} else if mid >= right {
		segTree[index*2+1] |= segTree[index]
		segTree[index*2+2] |= segTree[index]
		treeUpdate(index*2+1, l, mid, left, right, mask)
		segTree[index] = segTree[index*2+1] & segTree[index*2+2]
	} else if mid < left {
		segTree[index*2+1] |= segTree[index]
		segTree[index*2+2] |= segTree[index]
		treeUpdate(index*2+2, mid+1, r, left, right, mask)
		segTree[index] = segTree[index*2+1] & segTree[index*2+2]
	} else {
		segTree[index*2+1] |= segTree[index]
		segTree[index*2+2] |= segTree[index]
		treeUpdate(index*2+1, l, mid, left, mid, mask)
		treeUpdate(index*2+2, mid+1, r, mid+1, right, mask)
		segTree[index] = segTree[index*2+1] & segTree[index*2+2]
	}
}

func treeQuery(index, l, r, left, right int) int {
	mid := (l + r) / 2
	if l == left && r == right {
		return segTree[index]
	} else if mid >= right {
		segTree[index*2+1] |= segTree[index]
		segTree[index*2+2] |= segTree[index]
		return treeQuery(index*2+1, l, mid, left, right)
	} else if mid < left {
		segTree[index*2+1] |= segTree[index]
		segTree[index*2+2] |= segTree[index]
		return treeQuery(index*2+2, mid+1, r, left, right)
	} else {
		segTree[index*2+1] |= segTree[index]
		segTree[index*2+2] |= segTree[index]
		return treeQuery(index*2+1, l, mid, left, mid) & treeQuery(index*2+2, mid+1, r, mid+1, right)
	}
}

func treeTraverse(index, l, r, mask int) {
	if l > r {
		return
	} else if l == r {
		fmt.Printf("%d ", mask|segTree[index])
	} else {
		mid := (l + r) / 2
		treeTraverse(index*2+1, l, mid, mask|segTree[index])
		treeTraverse(index*2+2, mid+1, r, mask|segTree[index])
	}
}

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)
	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	for i := 0; i < m; i++ {
		fmt.Fscanf(reader, "%d %d %d\n", &l[i], &r[i], &q[i])
		treeUpdate(0, 0, n-1, l[i]-1, r[i]-1, q[i])
	}
	for i := 0; i < m; i++ {
		if treeQuery(0, 0, n-1, l[i]-1, r[i]-1) != q[i] {
			fmt.Printf("NO")
			return
		}
	}
	fmt.Printf("YES\n")
	treeTraverse(0, 0, n-1, 0)
}

package main

import (
	"bufio"
	"bytes"
	"errors"
	"io"
	"math/rand"
	"os"
	"strconv"
	"time"
	"unicode/utf8"
)

// ******************************** CODE STARTS ********************************

const (
	height int = 30
)

var (
	length int
	tree [height][]int
	seg_left []int
	seg_right []int
	seg_value []int
)

func tree_init(n int) {
	length = 1
	for length < n {
		length *= 2
	}
	for i := 0; i < height; i++ {
		tree[i] = make([]int, length * 2)
	}
}

func tree_update(bit, pos, value int) {
	pos += length
	tree[bit][pos] = value
	pos /= 2
	for pos > 0 {
		tree[bit][pos] = minInt(tree[bit][pos * 2], tree[bit][pos * 2 + 1])
		pos /= 2
	}
}

func tree_min(bit, left, right int) int {
	left += length
	right += length

	ans := tree[bit][left]
	for left <= right {
		ans = minInt(ans, tree[bit][left])
		ans = minInt(ans, tree[bit][right])

		left = (left + 1) / 2
		right = (right - 1) / 2
	}

	return ans
}

func solve() {
	n := readInt()
	m := readInt()

	var add [height][]int
	var sub [height][]int
	for i := 0; i < height; i++ {
		add[i] = make([]int, n)
		sub[i] = make([]int, n)
	}

	seg_left = make([]int, m)
	seg_right = make([]int, m)
	seg_value = make([]int, m)

	for i := 0; i < m; i++ {
		seg_left[i] = readInt() - 1
		seg_right[i] = readInt() - 1
		seg_value[i] = readInt()
		for bit := 0; bit < height; bit++ {
			if ((seg_value[i] >> uint(bit)) & 1) == 1 {
				add[bit][seg_left[i]]++
				sub[bit][seg_right[i]]++
			}
		}
	}

	// set 1
	tree_init(n)
  count := 0
	for bit := 0; bit < height; bit++ {
		for i := 0; i < n; i++ {
			count += add[bit][i]
			if count > 0 {
				tree_update(bit, i, 1)
			}
			count -= sub[bit][i]
		}
	}

	// check 0
	for i := 0; i < m; i++ {
		for bit := 0; bit < height; bit++ {
			if ((seg_value[i] >> uint(bit)) & 1) == 0 {
				value := tree_min(bit, seg_left[i], seg_right[i])
				if value == 1 {
					printString("NO")
					printLine()
					return
				}
			}
		}
	}

	// OK, output
	printString("YES")
	printLine()
	for i := 0; i < n; i++ {
		value := 0
		for bit := 0; bit < height; bit++ {
			if tree_min(bit, i, i) == 1 {
				value ^= 1 << uint(bit)
			}
		}
		printInt(value)
		printSpace()
	}
}

// ********************************* CODE ENDS *********************************

// IO.

var (
	scanner *Scanner
	writer  *bufio.Writer
)

func main() {
	input := os.Stdin
	output := os.Stdout
	//	input, _ := os.Open("")
	//	output, _ := os.Create("")

	defer input.Close()
	defer output.Close()

	scanner = NewScanner(input)
	scanner.Split(bufio.ScanWords)

	writer = bufio.NewWriter(output)
	defer writer.Flush()

	rand.Seed(int64(time.Now().Nanosecond()))
	solve()
}

// Read.

func readString() string {
	scanner.Scan()
	ans := scanner.Text()
	return ans
}

func readLine() string {
	scanner.Split(bufio.ScanLines)
	defer scanner.Split(bufio.ScanWords)
	return readString()
}

func readInt() int {
	ans, _ := strconv.ParseInt(readString(), 10, 0)
	return int(ans)
}

func readInt64() int64 {
	ans, _ := strconv.ParseInt(readString(), 10, 64)
	return ans
}

func readFloat64() float64 {
	ans, _ := strconv.ParseFloat(readString(), 64)
	return ans
}

// Write.

func printString(value string) {
	writer.WriteString(value)
}

func printLine() {
	printString("\n")
}

func printSpace() {
	printString(" ")
}

func printInt(value int) {
	printString(strconv.FormatInt(int64(value), 10))
}

func printInt64(value int64) {
	printString(strconv.FormatInt(value, 10))
}

func printFloat64(value float64) {
	printString(strconv.FormatFloat(value, 'f', 20, 64))
}

// Tuple

type Tuple struct {
	a, b int
}

func (p Tuple) Equals(key Key) bool {
	return p.a == key.(Tuple).a
}

func (p Tuple) Less(key Key) bool {
	return p.a < key.(Tuple).a
}

// Sorting

type TupleSlice []Tuple

func (p TupleSlice) Len() int {
	return len(p)
}

func (p TupleSlice) Less(j, i int) bool {
	if p[j].a == p[i].a {
		return p[j].b < p[i].b
	} else {
		return p[j].a < p[i].a
	}
}

func (p TupleSlice) Swap(j, i int) {
	p[j], p[i] = p[i], p[j]
}

// Min, max, abs.

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

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

func absInt(a int) int {
	if a > 0 {
		return a
	} else {
		return -a
	}
}

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

func minInt64(a, b int64) int64 {
	if a < b {
		return a
	} else {
		return b
	}
}

func absInt64(a int64) int64 {
	if a > 0 {
		return a
	} else {
		return -a
	}
}

func maxFloat64(a, b float64) float64 {
	if a > b {
		return a
	} else {
		return b
	}
}

func minFloat64(a, b float64) float64 {
	if a < b {
		return a
	} else {
		return b
	}
}

func absFloat64(a float64) float64 {
	if a > 0 {
		return a
	} else {
		return -a
	}
}

// Set Key interface implementations.

type Int int

func (p Int) Equals(key Key) bool {
	return int(p) == int(key.(Int))
}

func (p Int) Less(key Key) bool {
	return int(p) < int(key.(Int))
}

// Set.

type Key interface {
	Equals(key Key) bool
	Less(key Key) bool
}

type node struct {
	key   Key
	left  *node
	right *node
}

func newNode(key Key) *node {
	return &node{key, nil, nil}
}

type SplayTree struct {
	root *node
	tmp  *node
	len  int
}

func NewSplayTree() *SplayTree {
	return &SplayTree{nil, newNode(nil), 0}
}

func rotateLeft(x, p *node) {
	p.right = x.left
	x.left = p
}

func rotateRight(x, p *node) {
	p.left = x.right
	x.right = p
}

func (p *SplayTree) splay(x *node, key Key) *node {
	if x == nil {
		return nil
	}

	left := p.tmp
	right := p.tmp

	for {
		if key.Less(x.key) {
			y := x.left
			if y == nil {
				break
			}
			if key.Less(y.key) { // zig-zig
				rotateRight(y, x)
				x = y
				if x.left == nil {
					break
				}
			}
			// link right
			right.left = x
			right = x
			// move left
			x = x.left
		} else if x.key.Less(key) {
			y := x.right
			if y == nil {
				break
			}
			if y.key.Less(key) { // zig-zig
				rotateLeft(y, x)
				x = y
				if x.right == nil {
					break
				}
			}
			// link left
			left.right = x
			left = x
			// move right
			x = x.right
		} else {
			break
		}
	}

	left.right = x.left
	right.left = x.right
	x.left = p.tmp.right
	x.right = p.tmp.left

	return x
}

// left <= key < right
func (p *SplayTree) split(key Key) (left, right *node) {
	p.root = p.splay(p.root, key)
	if p.root.key.Equals(key) || p.root.key.Less(key) {
		right := p.root.right
		p.root.right = nil
		return p.root, right
	} else {
		left := p.root.left
		p.root.left = nil
		return left, p.root
	}
}

// keys from left tree must be less then keys from right tree
func (p *SplayTree) join(left, right *node) *node {
	if left == nil {
		return right
	} else if right == nil {
		return left
	}
	left = p.splay(left, right.key)
	left.right = right
	return left
}

type Set interface {
	Len() int
	Insert(key Key) error
	Find(key Key) bool
	Remove(key Key) error
}

func NewSet() Set {
	return Set(NewSplayTree())
}

func (p *SplayTree) Len() int {
	return p.len
}

func (p *SplayTree) Insert(key Key) error {
	if p.root == nil {
		p.root = newNode(key)
		p.len++
	} else {
		p.root = p.splay(p.root, key)
		if p.root.key.Equals(key) {
			return errors.New("Such key already exists")
		} else {
			left, right := p.split(key)
			p.root = newNode(key)
			p.root.left = left
			p.root.right = right
			p.len++
		}
	}
	return nil
}

func (p *SplayTree) Find(key Key) bool {
	if p.root == nil {
		return false
	}
	p.root = p.splay(p.root, key)
	return p.root.key.Equals(key)
}

func (p *SplayTree) Remove(key Key) error {
	p.root = p.splay(p.root, key)
	if p.root == nil || !p.root.key.Equals(key) {
		return errors.New("Such key doesn't exist")
	}
	p.root = p.join(p.split(key))
	return nil
}

// Copied scan.go
// Scanner provides a convenient interface for reading data such as
// a file of newline-delimited lines of text. Successive calls to
// the Scan method will step through the 'tokens' of a file, skipping
// the bytes between the tokens. The specification of a token is
// defined by a split function of type SplitFunc; the default split
// function breaks the input into lines with line termination stripped. Split
// functions are defined in this package for scanning a file into
// lines, bytes, UTF-8-encoded runes, and space-delimited words. The
// client may instead provide a custom split function.
//
// Scanning stops unrecoverably at EOF, the first I/O error, or a token too
// large to fit in the buffer. When a scan stops, the reader may have
// advanced arbitrarily far past the last token. Programs that need more
// control over error handling or large tokens, or must run sequential scans
// on a reader, should use bufio.Reader instead.
//
type Scanner struct {
	r            io.Reader // The reader provided by the client.
	split        SplitFunc // The function to split the tokens.
	maxTokenSize int       // Maximum size of a token; modified by tests.
	token        []byte    // Last token returned by split.
	buf          []byte    // Buffer used as argument to split.
	start        int       // First non-processed byte in buf.
	end          int       // End of data in buf.
	err          error     // Sticky error.
}

// SplitFunc is the signature of the split function used to tokenize the
// input. The arguments are an initial substring of the remaining unprocessed
// data and a flag, atEOF, that reports whether the Reader has no more data
// to give. The return values are the number of bytes to advance the input
// and the next token to return to the user, plus an error, if any. If the
// data does not yet hold a complete token, for instance if it has no newline
// while scanning lines, SplitFunc can return (0, nil, nil) to signal the
// Scanner to read more data into the slice and try again with a longer slice
// starting at the same point in the input.
//
// If the returned error is non-nil, scanning stops and the error
// is returned to the client.
//
// The function is never called with an empty data slice unless atEOF
// is true. If atEOF is true, however, data may be non-empty and,
// as always, holds unprocessed text.
type SplitFunc func(data []byte, atEOF bool) (advance int, token []byte, err error)

// Errors returned by Scanner.
var (
	ErrTooLong         = errors.New("bufio.Scanner: token too long")
	ErrNegativeAdvance = errors.New("bufio.Scanner: SplitFunc returns negative advance count")
	ErrAdvanceTooFar   = errors.New("bufio.Scanner: SplitFunc returns advance count beyond input")
)

const (
	// Maximum size used to buffer a token. The actual maximum token size
	// may be smaller as the buffer may need to include, for instance, a newline.
	MaxScanTokenSize = 1 << 20
)

// NewScanner returns a new Scanner to read from r.
// The split function defaults to ScanLines.
func NewScanner(r io.Reader) *Scanner {
	return &Scanner{
		r:            r,
		split:        ScanLines,
		maxTokenSize: MaxScanTokenSize,
		buf:          make([]byte, 4096), // Plausible starting size; needn't be large.
	}
}

// Err returns the first non-EOF error that was encountered by the Scanner.
func (s *Scanner) Err() error {
	if s.err == io.EOF {
		return nil
	}
	return s.err
}

// Bytes returns the most recent token generated by a call to Scan.
// The underlying array may point to data that will be overwritten
// by a subsequent call to Scan. It does no allocation.
func (s *Scanner) Bytes() []byte {
	return s.token
}

// Text returns the most recent token generated by a call to Scan
// as a newly allocated string holding its bytes.
func (s *Scanner) Text() string {
	return string(s.token)
}

// Scan advances the Scanner to the next token, which will then be
// available through the Bytes or Text method. It returns false when the
// scan stops, either by reaching the end of the input or an error.
// After Scan returns false, the Err method will return any error that
// occurred during scanning, except that if it was io.EOF, Err
// will return nil.
func (s *Scanner) Scan() bool {
	// Loop until we have a token.
	for {
		// See if we can get a token with what we already have.
		if s.end > s.start {
			advance, token, err := s.split(s.buf[s.start:s.end], s.err != nil)
			if err != nil {
				s.setErr(err)
				return false
			}
			if !s.advance(advance) {
				return false
			}
			s.token = token
			if token != nil {
				return true
			}
		}
		// We cannot generate a token with what we are holding.
		// If we've already hit EOF or an I/O error, we are done.
		if s.err != nil {
			// Shut it down.
			s.start = 0
			s.end = 0
			return false
		}
		// Must read more data.
		// First, shift data to beginning of buffer if there's lots of empty space
		// or space is neded.
		if s.start > 0 && (s.end == len(s.buf) || s.start > len(s.buf)/2) {
			copy(s.buf, s.buf[s.start:s.end])
			s.end -= s.start
			s.start = 0
		}
		// Is the buffer full? If so, resize.
		if s.end == len(s.buf) {
			if len(s.buf) >= s.maxTokenSize {
				s.setErr(ErrTooLong)
				return false
			}
			newSize := len(s.buf) * 2
			if newSize > s.maxTokenSize {
				newSize = s.maxTokenSize
			}
			newBuf := make([]byte, newSize)
			copy(newBuf, s.buf[s.start:s.end])
			s.buf = newBuf
			s.end -= s.start
			s.start = 0
			continue
		}
		// Finally we can read some input. Make sure we don't get stuck with
		// a misbehaving Reader. Officially we don't need to do this, but let's
		// be extra careful: Scanner is for safe, simple jobs.
		for loop := 0; ; {
			n, err := s.r.Read(s.buf[s.end:len(s.buf)])
			s.end += n
			if err != nil {
				s.setErr(err)
				break
			}
			if n > 0 {
				break
			}
			loop++
			if loop > 100 {
				s.setErr(io.ErrNoProgress)
				break
			}
		}
	}
}

// advance consumes n bytes of the buffer. It reports whether the advance was legal.
func (s *Scanner) advance(n int) bool {
	if n < 0 {
		s.setErr(ErrNegativeAdvance)
		return false
	}
	if n > s.end-s.start {
		s.setErr(ErrAdvanceTooFar)
		return false
	}
	s.start += n
	return true
}

// setErr records the first error encountered.
func (s *Scanner) setErr(err error) {
	if s.err == nil || s.err == io.EOF {
		s.err = err
	}
}

// Split sets the split function for the Scanner. If called, it must be
// called before Scan. The default split function is ScanLines.
func (s *Scanner) Split(split SplitFunc) {
	s.split = split
}

// Split functions

// ScanBytes is a split function for a Scanner that returns each byte as a token.
func ScanBytes(data []byte, atEOF bool) (advance int, token []byte, err error) {
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}
	return 1, data[0:1], nil
}

var errorRune = []byte(string(utf8.RuneError))

// ScanRunes is a split function for a Scanner that returns each
// UTF-8-encoded rune as a token. The sequence of runes returned is
// equivalent to that from a range loop over the input as a string, which
// means that erroneous UTF-8 encodings translate to U+FFFD = "\xef\xbf\xbd".
// Because of the Scan interface, this makes it impossible for the client to
// distinguish correctly encoded replacement runes from encoding errors.
func ScanRunes(data []byte, atEOF bool) (advance int, token []byte, err error) {
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}

	// Fast path 1: ASCII.
	if data[0] < utf8.RuneSelf {
		return 1, data[0:1], nil
	}

	// Fast path 2: Correct UTF-8 decode without error.
	_, width := utf8.DecodeRune(data)
	if width > 1 {
		// It's a valid encoding. Width cannot be one for a correctly encoded
		// non-ASCII rune.
		return width, data[0:width], nil
	}

	// We know it's an error: we have width==1 and implicitly r==utf8.RuneError.
	// Is the error because there wasn't a full rune to be decoded?
	// FullRune distinguishes correctly between erroneous and incomplete encodings.
	if !atEOF && !utf8.FullRune(data) {
		// Incomplete; get more bytes.
		return 0, nil, nil
	}

	// We have a real UTF-8 encoding error. Return a properly encoded error rune
	// but advance only one byte. This matches the behavior of a range loop over
	// an incorrectly encoded string.
	return 1, errorRune, nil
}

// dropCR drops a terminal \r from the data.
func dropCR(data []byte) []byte {
	if len(data) > 0 && data[len(data)-1] == '\r' {
		return data[0 : len(data)-1]
	}
	return data
}

// ScanLines is a split function for a Scanner that returns each line of
// text, stripped of any trailing end-of-line marker. The returned line may
// be empty. The end-of-line marker is one optional carriage return followed
// by one mandatory newline. In regular expression notation, it is `\r?\n`.
// The last non-empty line of input will be returned even if it has no
// newline.
func ScanLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}
	if i := bytes.IndexByte(data, '\n'); i >= 0 {
		// We have a full newline-terminated line.
		return i + 1, dropCR(data[0:i]), nil
	}
	// If we're at EOF, we have a final, non-terminated line. Return it.
	if atEOF {
		return len(data), dropCR(data), nil
	}
	// Request more data.
	return 0, nil, nil
}

// isSpace reports whether the character is a Unicode white space character.
// We avoid dependency on the unicode package, but check validity of the implementation
// in the tests.
func isSpace(r rune) bool {
	if r <= '\u00FF' {
		// Obvious ASCII ones: \t through \r plus space. Plus two Latin-1 oddballs.
		switch r {
		case ' ', '\t', '\n', '\v', '\f', '\r':
			return true
		case '\u0085', '\u00A0':
			return true
		}
		return false
	}
	// High-valued ones.
	if '\u2000' <= r && r <= '\u200a' {
		return true
	}
	switch r {
	case '\u1680', '\u180e', '\u2028', '\u2029', '\u202f', '\u205f', '\u3000':
		return true
	}
	return false
}

// ScanWords is a split function for a Scanner that returns each
// space-separated word of text, with surrounding spaces deleted. It will
// never return an empty string. The definition of space is set by
// unicode.IsSpace.
func ScanWords(data []byte, atEOF bool) (advance int, token []byte, err error) {
	// Skip leading spaces.
	start := 0
	for width := 0; start < len(data); start += width {
		var r rune
		r, width = utf8.DecodeRune(data[start:])
		if !isSpace(r) {
			break
		}
	}
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}
	// Scan until space, marking end of word.
	for width, i := 0, start; i < len(data); i += width {
		var r rune
		r, width = utf8.DecodeRune(data[i:])
		if isSpace(r) {
			return i + width, data[start:i], nil
		}
	}
	// If we're at EOF, we have a final, non-empty, non-terminated word. Return it.
	if atEOF && len(data) > start {
		return len(data), data[start:], nil
	}
	// Request more data.
	return 0, nil, nil
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF482B(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type pair struct{ l, r int }

	var n, q, l, r, v int
	Fscan(in, &n, &q)
	fas := [30][]int{}
	for i := range fas {
		fas[i] = make([]int, n+1)
		for j := range fas[i] {
			fas[i][j] = j
		}
	}
	var find func([]int, int) int
	find = func(fa []int, x int) int {
		if fa[x] != x {
			fa[x] = find(fa, fa[x])
		}
		return fa[x]
	}
	same := func(fa []int, x, y int) bool { return find(fa, x) == find(fa, y) }
	mergeRange := func(fa []int, l, r int) {
		for i := find(fa, l); i < r; i = find(fa, i+1) {
			fa[i] = r
		}
	}
	check0 := [30][]pair{}
	for ; q > 0; q-- {
		Fscan(in, &l, &r, &v)
		l--
		for i, fa := range fas {
			if v>>i&1 == 0 {
				check0[i] = append(check0[i], pair{l, r})
			} else {
				mergeRange(fa, l, r)
			}
		}
	}
	for i, qs := range check0 {
		fa := fas[i]
		for _, q := range qs {
			if same(fa, q.l, q.r) {
				Fprintln(out, "NO")
				return
			}
		}
	}
	ans := make([]int, n)
	for j, fa := range fas {
		for i, f := range fa {
			if f != i {
				ans[i] |= 1 << j
			}
		}
	}
	Fprintln(out, "YES")
	for _, v := range ans {
		Fprint(out, v, " ")
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF482B(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type pair struct{ l, r int }

	var n, q, l, r, v int
	Fscan(in, &n, &q)
	fas := [30][]int{}
	for i := range fas {
		fas[i] = make([]int, n+1)
		for j := range fas[i] {
			fas[i][j] = j
		}
	}
	var find func([]int, int) int
	find = func(fa []int, x int) int {
		if fa[x] != x {
			fa[x] = find(fa, fa[x])
		}
		return fa[x]
	}
	same := func(fa []int, x, y int) bool { return find(fa, x) == find(fa, y) }
	mergeRange := func(fa []int, l, r int) {
		for i := find(fa, l); i < r; i = find(fa, i+1) {
			fa[i] = r
		}
	}
	check0 := [30][]pair{}
	for ; q > 0; q-- {
		Fscan(in, &l, &r, &v)
		l--
		for i, fa := range fas {
			if w := v >> i & 1; w == 0 {
				check0[i] = append(check0[i], pair{l, r})
			} else {
				mergeRange(fa, l, r)
			}
		}
	}
	for i, qs := range check0 {
		fa := fas[i]
		for _, q := range qs {
			l, r := q.l, q.r
			if same(fa, l, r) {
				Fprintln(out, "NO")
				return
			}
		}
	}
	ans := make([]int, n)
	for j, fa := range fas {
		for i, f := range fa {
			if f != i {
				ans[i] |= 1 << j
			}
		}
	}
	Fprintln(out, "YES")
	for _, v := range ans {
		Fprint(out, v, " ")
	}
}

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

package main

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

var segTree [300000]int
var l, r, q [100000]int

func treeUpdate(index, l, r, left, right, mask int) {
	mid := (l + r) / 2
	if l == left && r == right {
		segTree[index] |= mask
	} else if mid >= right {
		treeUpdate(index*2+1, l, mid, left, right, mask)
		segTree[index] = segTree[index*2+1] & segTree[index*2+2]
	} else if mid < left {
		treeUpdate(index*2+2, mid+1, r, left, right, mask)
		segTree[index] = segTree[index*2+1] & segTree[index*2+2]
	} else {
		treeUpdate(index*2+1, l, mid, left, mid, mask)
		treeUpdate(index*2+2, mid+1, r, mid+1, right, mask)
		segTree[index] = segTree[index*2+1] & segTree[index*2+2]
	}
}

func treeQuery(index, l, r, left, right int) int {
	mid := (l + r) / 2
	if l == left && r == right {
		return segTree[index]
	} else if mid >= right {
		return treeQuery(index*2+1, l, mid, left, right)
	} else if mid < left {
		return treeQuery(index*2+2, mid+1, r, left, right)
	} else {
		return treeQuery(index*2+1, l, mid, left, mid) & treeQuery(index*2+2, mid+1, r, mid+1, right)
	}
}

func treeTraverse(index, l, r, mask int) {
	if l > r {
		return
	} else if l == r {
		fmt.Printf("%d ", mask|segTree[index])
	} else {
		mid := (l + r) / 2
		treeTraverse(index*2+1, l, mid, mask|segTree[index])
		treeTraverse(index*2+2, mid+1, r, mask|segTree[index])
	}
}

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)
	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	for i := 0; i < m; i++ {
		fmt.Fscanf(reader, "%d %d %d\n", &l[i], &r[i], &q[i])
		treeUpdate(0, 0, n-1, l[i]-1, r[i]-1, q[i])
	}
	for i := 0; i < m; i++ {
		if treeQuery(0, 0, n-1, l[i]-1, r[i]-1) != q[i] {
			fmt.Printf("NO")
			return
		}
	}
	fmt.Printf("YES\n")
	treeTraverse(0, 0, n-1, 0)
}

package main

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

var segTree [300000]int
var l, r, q [100000]int

func treeUpdate(index, l, r, left, right, mask int) {
	mid := (l + r) / 2
	if l == left && r == right {
		segTree[index] |= mask
	} else if mid >= right {
		segTree[index*2+1] |= segTree[index]
		segTree[index*2+2] |= segTree[index]
		treeUpdate(index*2+1, l, mid, left, right, mask)
		segTree[index] = segTree[index*2+1] & segTree[index*2+2]
	} else if mid < left {
		segTree[index*2+1] |= segTree[index]
		segTree[index*2+2] |= segTree[index]
		treeUpdate(index*2+2, mid+1, r, left, right, mask)
		segTree[index] = segTree[index*2+1] & segTree[index*2+2]
	} else {
		segTree[index*2+1] |= segTree[index]
		segTree[index*2+2] |= segTree[index]
		treeUpdate(index*2+1, l, mid, left, mid, mask)
		treeUpdate(index*2+2, mid+1, r, mid+1, right, mask)
		segTree[index] = segTree[index*2+1] & segTree[index*2+2]
	}
}

func treeQuery(index, l, r, left, right int) int {
	mid := (l + r) / 2
	if l == left && r == right {
		return segTree[index]
	} else if mid >= right {
		segTree[index*2+1] |= segTree[index]
		segTree[index*2+2] |= segTree[index]
		return treeQuery(index*2+1, l, mid, left, right)
	} else if mid < left {
		segTree[index*2+1] |= segTree[index]
		segTree[index*2+2] |= segTree[index]
		return treeQuery(index*2+2, mid+1, r, left, right)
	} else {
		segTree[index*2+1] |= segTree[index]
		segTree[index*2+2] |= segTree[index]
		return treeQuery(index*2+1, l, mid, left, mid) & treeQuery(index*2+2, mid+1, r, mid+1, right)
	}
}

func treeTraverse(index, l, r, mask int) {
	if l > r {
		return
	} else if l == r {
		fmt.Printf("%d ", mask|segTree[index])
	} else {
		mid := (l + r) / 2
		treeTraverse(index*2+1, l, mid, mask|segTree[index])
		treeTraverse(index*2+2, mid+1, r, mask|segTree[index])
	}
}

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)
	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	for i := 0; i < m; i++ {
		fmt.Fscanf(reader, "%d %d %d\n", &l[i], &r[i], &q[i])
		treeUpdate(0, 0, n-1, l[i]-1, r[i]-1, q[i])
	}
	treeTraverse(0, 0, n-1, 0)
	fmt.Printf("\n")
	for i := 0; i < m; i++ {
		if treeQuery(0, 0, n-1, l[i]-1, r[i]-1) != q[i] {
			fmt.Printf("NO #%d ans = %d", i, treeQuery(0, 0, n-1, l[i]-1, r[i]-1))
			return
		}
	}
	fmt.Printf("YES\n")
}

package main

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

var segTree [300000]int
var l, r, q [100000]int

func treeUpdate(index, l, r, left, right, mask int) {
	mid := (l + r) / 2
	if l == left && r == right {
		segTree[index] |= mask
	} else if mid >= right {
		treeUpdate(index*2+1, l, mid, left, right, mask)
		segTree[index] = segTree[index*2+1] & segTree[index*2+2]
	} else if mid < left {
		treeUpdate(index*2+2, mid+1, r, left, right, mask)
		segTree[index] = segTree[index*2+1] & segTree[index*2+2]
	} else {
		treeUpdate(index*2+1, l, mid, left, mid, mask)
		treeUpdate(index*2+2, mid+1, r, mid+1, right, mask)
		segTree[index] = segTree[index*2+1] & segTree[index*2+2]
	}
}

func treeQuery(index, l, r, left, right int) int {
	mid := (l + r) / 2
	if l == left && r == right {
		return segTree[index]
	} else if mid >= right {
		segTree[index*2+1] |= segTree[index]
		segTree[index*2+2] |= segTree[index]
		return treeQuery(index*2+1, l, mid, left, right)
	} else if mid < left {
		segTree[index*2+1] |= segTree[index]
		segTree[index*2+2] |= segTree[index]
		return treeQuery(index*2+2, mid+1, r, left, right)
	} else {
		segTree[index*2+1] |= segTree[index]
		segTree[index*2+2] |= segTree[index]
		return treeQuery(index*2+1, l, mid, left, mid) & treeQuery(index*2+2, mid+1, r, mid+1, right)
	}
}

func treeTraverse(index, l, r, mask int) {
	if l > r {
		return
	} else if l == r {
		fmt.Printf("%d ", mask|segTree[index])
	} else {
		mid := (l + r) / 2
		treeTraverse(index*2+1, l, mid, mask|segTree[index])
		treeTraverse(index*2+2, mid+1, r, mask|segTree[index])
	}
}

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)
	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	for i := 0; i < m; i++ {
		fmt.Fscanf(reader, "%d %d %d\n", &l[i], &r[i], &q[i])
		treeUpdate(0, 0, n-1, l[i]-1, r[i]-1, q[i])
	}
	for i := 0; i < m; i++ {
		if treeQuery(0, 0, n-1, l[i]-1, r[i]-1) != q[i] {
			fmt.Printf("NO")
			return
		}
	}
	fmt.Printf("YES\n")
	treeTraverse(0, 0, n-1, 0)
}


Recently Luba bought a very interesting book. She knows that it will take t seconds to read the book. Luba wants to finish reading as fast as she can.But she has some work to do in each of n next days. The number of seconds that Luba has to spend working during i-th day is ai. If some free time remains, she can spend it on reading.Help Luba to determine the minimum number of day when she finishes reading.It is guaranteed that the answer doesn't exceed n.Remember that there are 86400 seconds in a day.

The first line contains two integers n and t (1 ≤ n ≤ 100, 1 ≤ t ≤ 106) — the number of days and the time required to read the book. The second line contains n integers ai (0 ≤ ai ≤ 86400) — the time Luba has to spend on her work during i-th day.

Print the minimum day Luba can finish reading the book. It is guaranteed that answer doesn't exceed n.

package main

import "fmt"

func main() {
	var n, t int
	fmt.Scanf("%d %d\n", &n, &t)

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

	for i := 0; i < n; i++ {
		t = t - (86400 - a[i])
		if t <= 0 {
			fmt.Println(i + 1)
			break
		}
	}
}

package main

import "fmt"

func main() {
	var (
		n, t int
	)
	fmt.Scanf("%d%d\n", &n, &t)
	for i := 1; i <= n; i++ {
		var a int
		fmt.Scanf("%d", &a)
		t -= 86400 - a
		if t <= 0 {
			fmt.Println(i)
			break
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var n,t int
	fmt.Scanf("%d%d",&n,&t)
	//fmt.Println(n,t)
	tme := make([]int,n)
	for i:= 0;i < n;i++{
		fmt.Scan(&tme[i])
		t -= (86400 - tme[i])
		//fmt.Println(tme[i],t)
		if t <= 0 {
			fmt.Println(i + 1)
			return
		}
	}
}

package main

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

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

// IO helper functions

// Returns next token from input.  It must be initialized by SetInput()
// before the first call.
var nextToken func() ([]byte, error)
var nextLine func() ([]byte, error)

// Holds a buffer for output.  It must be initialized by SetOutput().
// All IO fucntions (read*() and [e]print*()) should write to OutputWriter
// instead of this.
var OutputBuffer *bufio.Writer

// Holds an io.Writer.  It must be initialized by SetOutput()
var OutputWriter io.Writer

// Set IO functions for interactive input/output.
func SetInteractive(w io.Writer, r io.Reader) {
	SetUnbefferedInput(r)
	OutputBuffer = nil
	OutputWriter = w
}

// Setup OutputBuffer and OutputWriter.
func SetOutput(w io.Writer) {
	OutputBuffer = bufio.NewWriter(w)
	OutputWriter = OutputBuffer
}

// Flushes OutputBuffer
func Flush() {
	if OutputBuffer != nil {
		OutputBuffer.Flush()
	}
}

// Returns true if c is a white space
func IsSpace(c byte) bool {
	switch c {
	case '\t', '\n', '\v', '\f', '\r', ' ':
		return true
	}
	return false
}

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

// Setup nextToken with input buffer.
func SetInput(r io.Reader) {
	buf := new(bytes.Buffer)
	var b []byte

	var i int
	rest := func() ([]byte, error) {
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		return b[j:i], nil
	}
	initial := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextToken = rest
		return rest()
	}
	nextToken = initial

	restLn := func() ([]byte, error) {
		for i < len(b) && IsNewLine(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsNewLine(b[i]) {
			i++
		}
		return b[j:i], nil
	}

	initialLn := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextLine = restLn
		return restLn()
	}
	nextLine = initialLn
}

// Setup nextToken without input buffer.
func SetUnbefferedInput(r io.Reader) {
	buf := bufio.NewReader(r)
	var b []byte

	var i int
	nextToken = func() ([]byte, error) {
		var err error
		if i == len(b) {
			b, err = buf.ReadBytes('\n')
			if err != nil {
				return nil, err
			}
			i = 0
			j := len(b) - 1
			for 0 <= j && IsSpace(b[j]) {
				j--
			}
			b = b[0 : j+1]
		}
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		if i == j {
			return nil, io.ErrUnexpectedEOF
		}
		return b[j:i], nil
	}
}

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

// IO functions

// Reads next token and return it as []byte
func readb() []byte {
	b, err := nextToken()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

// Reads next token and return it as string
func reads() string {
	return string(readb())
}

// Read next line as []byte.  Trailing '\n' will not be included.
// See also comments on readb()
func readbln() []byte {
	b, err := nextLine()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

// Read next line as string
func readsln() string {
	return string(readbln())
}

// Reads next token and return it as int64
func readll() int64 {
	i, err := strconv.ParseInt(reads(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// Reads next token and return it as int
func readi() int {
	return int(readll())
}

// Reads next token and return it as float64
func readf() float64 {
	f, err := strconv.ParseFloat(reads(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// Write args to OutputWriter with the format f
func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(OutputWriter, f, args...)
}

// Write args to OutputWriter without format
func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(OutputWriter, args...)
}

// Write args to stderr with the format f
func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

// Write args to stderr without format
func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

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

// Utilities

func sumSlice(a []int) int {
	var res int
	for _, v := range a {
		res += v
	}
	return res
}

func sumSlicell(a []int64) int64 {
	var res int64
	for _, v := range a {
		res += v
	}
	return res
}

func readInts(N int) (int, []int) {
	if N == 0 {
		N = readi()
	}
	a := make([]int, N)
	for i := range a {
		a[i] = readi()
	}
	return N, a
}

func readIntsll(N int) (int, []int64) {
	if N == 0 {
		N = readi()
	}
	a := make([]int64, N)
	for i := range a {
		a[i] = readll()
	}
	return N, a
}

// reverse slice in place
func reverse(a []int) {
	for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
		a[i], a[j] = a[j], a[i]
	}
}

// areverse: allocating reverse
func areverse(a []int) []int {
	b := make([]int, len(a))
	for i := range a {
		b[i] = a[len(a)-i-1]
	}
	return b
}

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

// Simple math functions

const (
	// big prime
	INF  = 1000000007
	INF2 = 1000000009
	INF3 = 998244353
)

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

func minll(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

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

func maxll(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

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

func absll(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

// egcd(a, b) returns d, x, y:
//   d is gcd(a,b)
//   x, y are  integers that satisfy ax + by = d
func egcd(a, b int) (int, int, int) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcd(b, a%b)
	return d, y, x - a/b*y
}

func egcdll(a, b int64) (int64, int64, int64) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcdll(b, a%b)
	return d, y, x - a/b*y
}

func gcd(a, b int) int {
	d, _, _ := egcd(a, b)
	return d
}

func gcdll(a, b int64) int64 {
	d, _, _ := egcdll(a, b)
	return d
}

// set up IO functions
func init() {
	// for non-interactive
	SetInput(os.Stdin)
	SetOutput(os.Stdout)

	// Enable below when interactive.  Its ok to leave above intact.
	// SetInteractive(os.Stdout, os.Stdin)
}

func main() {
	defer Flush()

	n := readi()
	t := readi()
	_, a := readInts(n)

	var i int
	for ; i < n && 0 < t; i++ {
		t -= (86400 - a[i])
	}
	println(i)

}

package main

import (
	"fmt"
)

func main() {
	var n,t int
	fmt.Scanf("%d%d",&n,&t)
	tme := make([]int,n)
	for i:= 0;i < n;i++{
		fmt.Scanf("%d",&tme[i])
		t -= 86400 - tme[i]
		if t <= 0 {
			fmt.Println(i + 1)
			break
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var n,t int
	fmt.Scanf("%d%d",&n,&t)
	fmt.Println(n,t)
	tme := make([]int,n)
	for i:= 0;i < n;i++{
		fmt.Scanf("%d",&tme[i])
		t -= (86400 - tme[i])
		if t <= 0 {
			fmt.Println(i + 1)
			break
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var n,t int
	fmt.Scanf("%d%d",&n,&t)
	fmt.Println(n,t)
	tme := make([]int,n)
	for i:= 0;i < n;i++{
		fmt.Scanf("%d",&tme[i])
		t -= (86400 - tme[i])
		fmt.Println(tme[i],t)
		if t <= 0 {
			fmt.Println(i + 1)
			break
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var n,t int
	fmt.Scanf("%d%d",&n,&t)
	tme := make([]int,n)
	for i:= 0;i < n;i++{
		fmt.Scanf("%d",&tme[i])
		t -= (86400 - tme[i])
		if t <= 0 {
			fmt.Println(i + 1)
			break
		}
	}
	//fmt.Println(n,t,tme[0],tme[1])
}

package main

import (
	"fmt"
	)

func main() {
	var n,t int
	fmt.Scanf("%d%d",&n,&t)
	tme := make([]int,n)
	for i:= 0;i < n;i++{
		fmt.Scanf("%d",&tme[i])
		t -= 86400 - tme[i]
		if t <= 0 {
			fmt.Println(i + 1)
			return
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var n,t int
	fmt.Scanf("%d%d",&n,&t)
	tme := make([]int,n)
	for i:= 0;i < n;i++{
		fmt.Scanf("%d",&tme[i])
		t -= 86400 - tme[i]
		if t <= 0 {
			fmt.Println(i + 1)
			break
		}
	}
	fmt.Println(n,t,tme[0],tme[1])
}

package main

import "fmt"

func main() {
	var n, t int
	fmt.Scanf("%d%d", &n, &t)

	ans := 0
	for i := 1; t > 0 && i <= n; i++ {
		var x int
		fmt.Scanf("%d", &x)
		t -= 86400 - x
		ans++
	}
	fmt.Println(ans)
}

package main

import "fmt"

func main() {
	var (
		n int
		t int
	)
	fmt.Scanf("%d%d", &n, &t)

	ans := 0
	for i := 1; t > 0 && i <= n; i++ {
		var x int
		fmt.Scanf("%d", &x)
		t -= 86400 - x
		ans++
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
)

func main() {

	var n, t int

	fmt.Scanf("%d %d", &n, &t)

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

	var ok bool

	for i := 0; i < n; i++ {
		if t <= 0 {
			fmt.Println(i)
			ok = true
			break
		}

		t -= (86400 - a[i])

	}
	if !ok {
		fmt.Println(n)
	}
}

package main

import (
	"fmt"
)

func main() {

	var n, t int

	fmt.Scanf("%d %d", &n, &t)

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

	var ok bool

	for i := 0; i < n; i++ {
		if t == 0 {
			fmt.Println(i)
			ok = true
			break
		}

		t -= (86400 - a[i])

	}
	if !ok {
		fmt.Println(n)
	}
}

package main

import (
	"fmt"
)

func main() {

	var n, t int

	fmt.Scanf("%d %d", &n, &t)

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

	var ok bool

	for i := 0; i < n; i++ {
		if t <= 0 {
			//fmt.Println(t)
			fmt.Println(i)
			ok = true
			break
		}

		t -= (86400 - a[i])

	}
	if !ok {
		fmt.Println(n)
	}
}


Let $$$a$$$ and $$$b$$$ be two arrays of lengths $$$n$$$ and $$$m$$$, respectively, with no elements in common. We can define a new array $$$\mathrm{merge}(a,b)$$$ of length $$$n+m$$$ recursively as follows:  If one of the arrays is empty, the result is the other array. That is, $$$\mathrm{merge}(\emptyset,b)=b$$$ and $$$\mathrm{merge}(a,\emptyset)=a$$$. In particular, $$$\mathrm{merge}(\emptyset,\emptyset)=\emptyset$$$.  If both arrays are non-empty, and $$$a_1<b_1$$$, then $$$\mathrm{merge}(a,b)=[a_1]+\mathrm{merge}([a_2,\ldots,a_n],b)$$$. That is, we delete the first element $$$a_1$$$ of $$$a$$$, merge the remaining arrays, then add $$$a_1$$$ to the beginning of the result.  If both arrays are non-empty, and $$$a_1>b_1$$$, then $$$\mathrm{merge}(a,b)=[b_1]+\mathrm{merge}(a,[b_2,\ldots,b_m])$$$. That is, we delete the first element $$$b_1$$$ of $$$b$$$, merge the remaining arrays, then add $$$b_1$$$ to the beginning of the result. This algorithm has the nice property that if $$$a$$$ and $$$b$$$ are sorted, then $$$\mathrm{merge}(a,b)$$$ will also be sorted. For example, it is used as a subroutine in merge-sort. For this problem, however, we will consider the same procedure acting on non-sorted arrays as well. For example, if $$$a=[3,1]$$$ and $$$b=[2,4]$$$, then $$$\mathrm{merge}(a,b)=[2,3,1,4]$$$.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).There is a permutation $$$p$$$ of length $$$2n$$$. Determine if there exist two arrays $$$a$$$ and $$$b$$$, each of length $$$n$$$ and with no elements in common, so that $$$p=\mathrm{merge}(a,b)$$$.

The first line contains a single integer $$$t$$$ ($$$1\le t\le 1000$$$)  — the number of test cases. Next $$$2t$$$ lines contain descriptions of test cases.  The first line of each test case contains a single integer $$$n$$$ ($$$1\le n\le 2000$$$). The second line of each test case contains $$$2n$$$ integers $$$p_1,\ldots,p_{2n}$$$ ($$$1\le p_i\le 2n$$$). It is guaranteed that $$$p$$$ is a permutation. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$2000$$$.

For each test case, output "YES" if there exist arrays $$$a$$$, $$$b$$$, each of length $$$n$$$ and with no common elements, so that $$$p=\mathrm{merge}(a,b)$$$. Otherwise, output "NO".

NoteIn the first test case, $$$[2,3,1,4]=\mathrm{merge}([3,1],[2,4])$$$.In the second test case, we can show that $$$[3,1,2,4]$$$ is not the merge of two arrays of length $$$2$$$.In the third test case, $$$[3,2,6,1,5,7,8,4]=\mathrm{merge}([3,2,8,4],[6,1,5,7])$$$.In the fourth test case, $$$[1,2,3,4,5,6]=\mathrm{merge}([1,3,6],[2,4,5])$$$, for example.

package main

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

const (
	SCANNER_BUF_SIZE = 2 << 20

	INF = int64(math.MaxInt64)
	MOD = 1e9 + 7
)

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

func init() {
	writer = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, SCANNER_BUF_SIZE), SCANNER_BUF_SIZE)
}

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

func sint64() int64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sstring() string {
	scanner.Scan()
	return scanner.Text()
}

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

func lcm(a, b int64) int64 {
	return a * b / gcd(a, b)
}

func abs(a int64) int64 {
	if a < 0 {
		return a * -1
	}
	return a
}

func min(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r < a {
			a = r
		}
	}
	return a
}

func max(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r > a {
			a = r
		}
	}
	return a
}

func main() {
	defer writer.Flush()
	t := sint64()
	for ; t > 0; t-- {
		n := sint64()
		p := make([]int64, n*2)
		for i := range p {
			p[i] = sint64()
		}
		printf("%s\n", sol(p))
	}
}

func sol(p []int64) string {
	s := make([]bool, len(p))
	target := int64(len(p))
	lastIdx := int64(len(p))
	groupLens := []int64{}
	for i := int64(len(p) - 1); i >= 0; i-- {
		val := p[i]
		s[val-1] = true
		if val == target {
			groupLens = append(groupLens, lastIdx-i)
			lastIdx = i
			for target > 0 && s[target-1] {
				target--
			}
			// if target == 0 {
			// }
		}
	}
	// printf("%v\n", groupLens)
	dp := make([]bool, len(p)/2+1)
	dp[0] = true
	for _, v := range groupLens {
		tmp := make([]bool, len(dp))
		for j := range tmp {
			if dp[j] {
				tmp[j] = true
			}
			if v == int64(j) {
				tmp[j] = true
			}
			if int64(j) >= v && dp[int64(j)-v] {
				tmp[j] = true
			}
		}
		dp = tmp
		// printf("after: v=%d %v\n", v, dp)
	}
	// printf("%v\n", dp)
	if dp[len(dp)-1] {
		return "YES"
	}
	return "NO"
}

package main

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

/*
6
OEYCDK
EYOKCD
KDCEOY
CKHOYE
YOEDKC
DCKYEO
3
IWL
GIW
WLI
*/

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

func printf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(out, x, a...)
}

func scanf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fscanf(in, x, a...)
}

type priorityQueue [][2]int

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

func (pq priorityQueue) Less(i, j int) bool {
	return pq[i][1] > pq[j][1]
}

func (pq priorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
}

func (pq *priorityQueue) Push(x interface{}) {
	*pq = append(*pq, x.([2]int))
}

func (pq *priorityQueue) Pop() (res interface{}) {
	res = (*pq)[len(*pq)-1]
	*pq = (*pq)[:len(*pq)-1]
	return res
}

var err error

func nextInt() (n int) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextLong() (n int64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextDouble() (n float64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func next() (n string) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return ""
		}
	}
}

func Fill(a []int, v int) {
	for i := range a {
		a[i] = v
	}
}

func FillB(a []bool, v bool) {
	for i := range a {
		a[i] = v
	}
}

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

var (
	names map[string]int
	words []string
	withs [][]string
	res   []string
	has   []bool
)

func dfs(ind int) {
	if has[ind] {
		return
	}
	has[ind] = true
	for i := len(withs[ind]) - 1; i >= 0; i-- {
		dfs(names[withs[ind][i]])
	}
	res = append(res, words[ind])
}

func main() {
	in = bufio.NewReaderSize(os.Stdin, 8192)
	out = bufio.NewWriterSize(os.Stdout, 8192)
	if false {
		name := "in"
		file1, err := os.Open(name + ".in")
		if err != nil {
			log.Fatal(err)
		}
		// file2, err := os.Create(name + ".out")
		// if err != nil {
		// 	log.Fatal(err)
		// }
		in = bufio.NewReaderSize(file1, 8192)
		// out = bufio.NewWriterSize(file2, 8192)
	}
	defer out.Flush()

	for cases := nextInt(); cases > 0; cases-- {
		n := nextInt()
		arr := make([]int, 2*n)
		for i := range arr {
			arr[i] = nextInt()
		}
		inds := []int{0}
		for i := 1; i < 2*n; i++ {
			if arr[i] > arr[inds[len(inds)-1]] {
				inds = append(inds, i)
			}
		}
		dp := make([][]bool, len(inds)+1)
		for i := range dp {
			dp[i] = make([]bool, 2*n+1)
		}
		dp[len(inds)][n] = true
		inds = append(inds, 2*n)
		for i := len(inds) - 2; i >= 0; i-- {
			num := inds[i+1] - inds[i]
			for diff := range dp[i] {
				if diff-num >= 0 && dp[i+1][diff-num] {
					dp[i][diff] = true
				}
				if diff+num <= 2*n && dp[i+1][diff+num] {
					dp[i][diff] = true
				}
			}
		}
		if dp[0][n] {
			printf("YES\n")
		} else {
			printf("NO\n")
		}
	}
}

package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	for t := io.nextInt(); t > 0; t-- {
		n := io.nextInt()
		p := io.nextInts(2 * n)

		p = append(p, 2*n+1)
		dp := big.NewInt(1)
		last, cnt := 0, uint(0)
		for _, x := range p {
			if x > last {
				dp.Or(dp, new(big.Int).Lsh(dp, cnt))
				last, cnt = x, 0
			}
			cnt++
		}

		if dp.Bit(n) == 1 {
			io.println("YES")
		} else {
			io.println("NO")
		}
	}
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for !('0' <= f.p[0] && f.p[0] <= '9') {
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	return n
}

func (f *fastIO) nextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.nextInt()
	}
	return a
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *fastIO) printInts(a ...int) {
	if len(a) > 0 {
		f.print(a[0])
		for _, x := range a[1:] {
			f.print(" ", x)
		}
	}
}

func (f *fastIO) printlnInts(a ...int) {
	f.printInts(a...)
	f.println()
}

package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	for t := io.nextInt(); t > 0; t-- {
		n := io.nextInt()
		p := io.nextInts(2 * n)

		mx := make([]int, 2*n+1)
		for i := 0; i < 2*n; i++ {
			mx[i+1] = mx[i]
			if p[i] > p[mx[i]] {
				mx[i+1] = i
			}
		}

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

		var solve func(i, j int) int
		solve = func(i, j int) int {
			if i < 0 || j < 0 {
				return -1
			}
			if i == 0 && j == 0 {
				return 1
			}
			if dp[i][j] == 0 {
				k := i + j
				d := k - mx[k]
				dp[i][j] = -1
				if solve(i-d, j) == 1 || solve(i, j-d) == 1 {
					dp[i][j] = 1
				}
			}
			return dp[i][j]
		}

		if solve(n, n) == 1 {
			io.println("YES")
		} else {
			io.println("NO")
		}
	}
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for !('0' <= f.p[0] && f.p[0] <= '9') {
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	return n
}

func (f *fastIO) nextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.nextInt()
	}
	return a
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *fastIO) printInts(a ...int) {
	if len(a) > 0 {
		f.print(a[0])
		for _, x := range a[1:] {
			f.print(" ", x)
		}
	}
}

func (f *fastIO) printlnInts(a ...int) {
	f.printInts(a...)
	f.println()
}

package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	for t := io.nextInt(); t > 0; t-- {
		n := io.nextInt()
		p := io.nextInts(2 * n)

		p = append(p, 2*n+1)
		dp := make([]bool, 2*n+1)
		dp[0] = true
		last, cnt := 0, 0
		for _, x := range p {
			if x > last {
				for i := 2 * n; i >= cnt; i-- {
					dp[i] = dp[i] || dp[i-cnt]
				}
				last, cnt = x, 0
			}
			cnt++
		}

		if dp[n] {
			io.println("YES")
		} else {
			io.println("NO")
		}
	}
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for !('0' <= f.p[0] && f.p[0] <= '9') {
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	return n
}

func (f *fastIO) nextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.nextInt()
	}
	return a
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *fastIO) printInts(a ...int) {
	if len(a) > 0 {
		f.print(a[0])
		for _, x := range a[1:] {
			f.print(" ", x)
		}
	}
}

func (f *fastIO) printlnInts(a ...int) {
	f.printInts(a...)
	f.println()
}

package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	bf := make([]byte, 1024*1024)
	sc.Buffer(bf, 1024*1024*10)
	sc.Split(bufio.ScanWords)

	in := func() int {
		sc.Scan()
		i, _ := strconv.Atoi(sc.Text())
		return i
	}

	for t := in(); t > 0; t-- {
		n := in()
		a := make([]int, 2*n)

		b := make([]int, 0, n)

		for i := range a {
			a[i] = in()
		}

		for i := 1; i < 2*n; i++ {
			if a[i] < a[i-1] {
				a[i] = a[i-1]
			}
		}

		c := 0
		p := a[0]

		for i := range a {
			if a[i] == p {
				c++
			} else {
				b = append(b, c)
				c = 1
				p = a[i]
			}
		}

		dp := make([][]bool, len(b)+1)

		for i := range dp {
			dp[i] = make([]bool, n+1)
		}

		m := len(b)

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

		for i := 1; i <= n; i++ {
			dp[0][i] = false
		}

		for i := 1; i <= m; i++ {
			for j := 1; j <= n; j++ {
				if j < b[i-1] {
					dp[i][j] = dp[i-1][j]
				} else {
					dp[i][j] = dp[i-1][j] || dp[i-1][j-b[i-1]]
				}
			}
		}

		if dp[m][n] {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

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

	var T, n int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]int, 2*n)
		pos := []int{0}
		for i := range a {
			Fscan(in, &a[i])
			if a[i] > a[pos[len(pos)-1]] {
				pos = append(pos, i)
			}
		}
		pos = append(pos, 2*n)
		a = nil
		for i := 1; i < len(pos); i++ {
			a = append(a, pos[i]-pos[i-1])
		}
		dp := make([]bool, n+1)
		dp[0] = true
		for _, v := range a {
			for s := n; s >= v; s-- {
				dp[s] = dp[s] || dp[s-v]
			}
		}
		if dp[n] {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

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


Polycarp was gifted an array $$$a$$$ of length $$$n$$$. Polycarp considers an array beautiful if there exists a number $$$C$$$, such that each number in the array occurs either zero or $$$C$$$ times. Polycarp wants to remove some elements from the array $$$a$$$ to make it beautiful.For example, if $$$n=6$$$ and $$$a = [1, 3, 2, 1, 4, 2]$$$, then the following options are possible to make the array $$$a$$$ array beautiful:   Polycarp removes elements at positions $$$2$$$ and $$$5$$$, array $$$a$$$ becomes equal to $$$[1, 2, 1, 2]$$$;  Polycarp removes elements at positions $$$1$$$ and $$$6$$$, array $$$a$$$ becomes equal to $$$[3, 2, 1, 4]$$$;  Polycarp removes elements at positions $$$1, 2$$$ and $$$6$$$, array $$$a$$$ becomes equal to $$$[2, 1, 4]$$$; Help Polycarp determine the minimum number of elements to remove from the array $$$a$$$ to make it beautiful.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case consists of one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, output one integer — the minimum number of elements that Polycarp has to remove from the array $$$a$$$ to make it beautiful.

package main

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

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}
func solve(r io.Reader) int {
	var n int
	fmt.Fscan(r, &n)
	cnt := map[int]int{}
	set := make(map[int]int)
	for i := 0; i < n; i++ {
		var x int
		fmt.Fscan(r, &x)
		cnt[x]++
	}
	for _, value := range cnt {
		set[value] = value
	}

	result := n
	for _, value := range set {
		curr := 0
		for _, v := range cnt {
			if v < value {
				curr += v
			} else {
				curr += (v - value)
			}
		}
		result = min(result, curr)
	}
	return result
}
func x(r io.Reader) {
	buf := bufio.NewReader(r)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var t int
	fmt.Fscan(buf, &t)
	for i := 0; i < t; i++ {
		re := solve(buf)
		writer.WriteString(strconv.Itoa(re) + "\n")
	}
}

func main() { x(os.Stdin) }

package main

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

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

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

func LCM(x, y int) int {
	if x >= y {
		initial := x
		for i := 1; i < y+2; i++ {
			if initial%y == 0 {
				break
			} else {
				initial = initial + x
			}

		}
		return initial
	} else {
		initial := y
		for i := 1; i < x+2; i++ {
			if initial%x == 0 {
				break
			} else {
				initial = initial + y
			}

		}
		return initial
	}
}

func IsItPrime(x int) bool {
	if x < 10 {
		if x == 2 || x == 3 || x == 5 || x == 7 {
			return true
		} else {
			return false
		}
	} else {
		j := 0
		k := math.Sqrt(float64(x))
		for m := 3; m <= int(k); {
			if x%m == 0 {
				j = 1
				break
			}
			m = m + 2
		}
		if j == 1 {
			return false
		} else {
			return true
		}
	}

}

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

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &t)
	for o := 0; o < t; o++ {
		var n int
		fmt.Fscan(input, &n)

		ARRAY := make([]int64, n+1)
		BARRAY := make([]int, 0)

		for i := 1; i < n+1; i++ {
			fmt.Fscan(input, &ARRAY[i])
		}

		sort.Slice(ARRAY, func(i, j int) bool {
			return ARRAY[i] > ARRAY[j]
		})
		countt := 1

		for i := 0; i < n; i++ {
			if ARRAY[i] == ARRAY[i+1] {
				countt = countt + 1
			} else {
				BARRAY = append(BARRAY, countt)
				countt = 1
			}
		}

		sort.Ints(BARRAY)
		k := int64(len(BARRAY))

		var maxx int64
		for i := 0; int64(i) < k; i++ {
			if (k-int64(i))*int64(BARRAY[i]) > maxx {
				maxx = (k - int64(i)) * int64(BARRAY[i])
			}
		}
		fmt.Println(int64(n) - maxx)
	}
}

package main

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

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

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

func LCM(x, y int) int {
	if x >= y {
		initial := x
		for i := 1; i < y+2; i++ {
			if initial%y == 0 {
				break
			} else {
				initial = initial + x
			}

		}
		return initial
	} else {
		initial := y
		for i := 1; i < x+2; i++ {
			if initial%x == 0 {
				break
			} else {
				initial = initial + y
			}

		}
		return initial
	}
}

func IsItPrime(x int) bool {
	if x < 10 {
		if x == 2 || x == 3 || x == 5 || x == 7 {
			return true
		} else {
			return false
		}
	} else {
		j := 0
		k := math.Sqrt(float64(x))
		for m := 3; m <= int(k); {
			if x%m == 0 {
				j = 1
				break
			}
			m = m + 2
		}
		if j == 1 {
			return false
		} else {
			return true
		}
	}

}

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

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &t)
	for o := 0; o < t; o++ {
		var n int
		fmt.Fscan(input, &n)

		ARRAY := make([]int64, n+1)
		BARRAY := make([]int, 0)

		for i := 1; i < n+1; i++ {
			fmt.Fscan(input, &ARRAY[i])
		}

		sort.Slice(ARRAY, func(i, j int) bool {
			return ARRAY[i] > ARRAY[j]
		})
		count := 1

		for i := 0; i < n; i++ {
			if ARRAY[i] == ARRAY[i+1] {
				count = count + 1
			} else {
				BARRAY = append(BARRAY, count)
				count = 1
			}
		}

		sort.Ints(BARRAY)
		k := int64(len(BARRAY))

		var max int64
		for i := 0; int64(i) < k; i++ {
			if (k-int64(i))*int64(BARRAY[i]) > max {
				max = (k - int64(i)) * int64(BARRAY[i])
			}
		}
		fmt.Println(int64(n) - max)
	}
}

package main

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

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

	var T, n, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		cnt := map[int]int{}
		for i := 0; i < n; i++ {
			Fscan(in, &v)
			cnt[v]++
		}
		cc := make([]int, n+1)
		for _, c := range cnt {
			cc[c]++
		}
		save, s := 0, 0
		for i, c := range cc {
			if i*(len(cnt)-s) > save {
				save = i * (len(cnt) - s)
			}
			s += c
		}
		Fprintln(out, n-save)
	}
}
func main() { CF1490F(os.Stdin, os.Stdout) }

package main

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

var in *bufio.Reader

func solve() {
	var n int
	fmt.Fscan(in, &n)

	cnt := make(map[int]int)
	for i := 0; i < n; i++ {
		var ai int
		fmt.Fscan(in, &ai)

		cnt[ai]++
	}

	freqs := make([]int, 0)
	for _, v := range cnt {
		freqs = append(freqs, v)
	}

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

	last := freqs[0]
	minCost, curCost := n, n
	for i, v := range freqs {
		curCost -= v
		curCost += (last - v) * i
		if minCost > curCost {
			minCost = curCost
		}
		last = v
	}
	fmt.Println(minCost)
}

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

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

	for i := 0; i < t; i++ {
		solve()
	}
}

package main

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

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

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

	// single test case

	var tt int
	Fscan(in, &tt)

	for tt > 0 {
		tt--

		var n int
		Fscan(in, &n)

		t := 0
		occur := map[int]int{}

		for i := 0; i < n; i++ {
			Fscan(in, &t)
			occur[t]++
		}

		cntOccur := make([]int, n+1)

		for _, v := range occur {
			cntOccur[v]++
		}

		save, s := 0, 0
		for i, c := range cntOccur {
			if i*(len(occur)-s) > save {
				save = i * (len(occur) - s)
			}
			s += c
		}
		Fprint(out, n-save, "\n")
	}

	// 입력 에러 방지
	_leftData, _ := ioutil.ReadAll(in)
	if _s := strings.TrimSpace(string(_leftData)); _s != "" {
		panic("읽지않은 데이터 발견：\n" + _s)
	}
}

// LETYLETYLETY
func main() { CF702f(os.Stdin, os.Stdout) }

// YTELYTELYTEL

package main

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

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

func main(){
	var t, n, a int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &t)
	for (t > 0){
		t--
		fmt.Fscan(in, &n)
		m := make(map[int]int)
		cnt := make([]int, n+1)
		d := make([]int, n+1)

		for i :=0; i<n; i++{
			fmt.Fscan(in, &a)
			m[a] = m[a]+1
		}

		for k := range m {
			cnt[m[k]]++
		}

		d[n] = cnt[n]
		for i := n-1; i>=1; i--{
			d[i] = d[i+1] + cnt[i]
		}

		result := n
		for i := 1; i<=n; i++{
			result = min(result, n-i*d[i])
		}

		fmt.Println(result)
	}
}

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 i := 0; i < t; i++ {
		var n int
		fmt.Fscan(in, &n)
		hash := make(map[int64]int64, 0)
		for j := 1; j <= n; j++ {
			var x int64
			fmt.Fscan(in, &x)
			hash[x]++
		}
		var arr []int64
		for _, val := range hash {
			arr = append(arr, val)
		}
		sort.Slice(arr, func(i, j int) bool {
			return arr[i] <= arr[j]
		})
		k := len(arr)
		// convert to hash array of counts
		suffixSum := make([]int64, k+1)
		suffixSum[k] = 0
		suffixSum[k-1] = arr[k-1]
		for i := k - 2; i >= 0; i-- {
			suffixSum[i] = suffixSum[i+1] + arr[i]
		}
		preSum := int64(0)
		ans := int64(n + 5)
		for i := 0; i < k; i++ {
			curAns := preSum + suffixSum[i+1] - int64(k-i-1)*arr[i]
			if curAns < ans {
				ans = curAns
			}
			preSum += arr[i]
		}
		fmt.Fprintln(out, ans)
	}
}

package main

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

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}
func solve(r io.Reader) int {
	var n int
	fmt.Fscan(r, &n)
	cnt := map[int]int{}
	for i := 0; i < n; i++ {
		var x int
		fmt.Fscan(r, &x)
		cnt[x]++
	}
	result := n
	for _, value := range cnt {
		curr := 0
		for _, v := range cnt {
			if v < value {
				curr += v
			} else {
				curr += (v - value)
			}
		}
		result = min(result, curr)
	}
	return result
}
func x(r io.Reader) {
	buf := bufio.NewReader(r)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var t int
	fmt.Fscan(buf, &t)
	for i := 0; i < t; i++ {
		re := solve(buf)
		writer.WriteString(strconv.Itoa(re))
	}
}

func main() { x(os.Stdin) }


A new agent called Killjoy invented a virus COVID-2069 that infects accounts on Codeforces. Each account has a rating, described by an integer (it can possibly be negative or very large).Killjoy's account is already infected and has a rating equal to $$$x$$$. Its rating is constant. There are $$$n$$$ accounts except hers, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th account's initial rating is $$$a_i$$$. Any infected account (initially the only infected account is Killjoy's) instantly infects any uninfected account if their ratings are equal. This can happen at the beginning (before any rating changes) and after each contest. If an account is infected, it can not be healed.Contests are regularly held on Codeforces. In each contest, any of these $$$n$$$ accounts (including infected ones) can participate. Killjoy can't participate. After each contest ratings are changed this way: each participant's rating is changed by an integer, but the sum of all changes must be equal to zero. New ratings can be any integer.Find out the minimal number of contests needed to infect all accounts. You can choose which accounts will participate in each contest and how the ratings will change.It can be proven that all accounts can be infected in some finite number of contests.

The first line contains a single integer $$$t$$$ $$$(1 \le t \le 100)$$$ — the number of test cases. The next $$$2t$$$ lines contain the descriptions of all test cases. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$2 \le n \le 10^3$$$, $$$-4000 \le x \le 4000$$$) — the number of accounts on Codeforces and the rating of Killjoy's account. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(-4000 \le a_i \le 4000)$$$ — the ratings of other accounts.

For each test case output the minimal number of contests needed to infect all accounts.

NoteIn the first test case it's possible to make all ratings equal to $$$69$$$. First account's rating will increase by $$$1$$$, and second account's rating will decrease by $$$1$$$, so the sum of all changes will be equal to zero.In the second test case all accounts will be instantly infected, because all ratings (including Killjoy's account's rating) are equal to $$$4$$$.

package main

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

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

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

func solve(n int, x int, A []int) int {
	sort.Ints(A)

	if A[0] == x && A[n-1] == x {
		return 0
	}

	i := sort.SearchInts(A, x)
	if i < n && A[i] == x {
		return 1
	}
	var sum int64

	for i := 0; i < n; i++ {
		sum += int64(A[i]) - int64(x)
	}

	if sum == 0 {
		return 1
	}

	return 2
}

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
}

package main

import (
	"io"
	"io/ioutil"
	"os"
)

const _B = 1024 * 1024

// Buffer is the IO middleware
type Buffer struct {
	i   int
	b   []byte
	in  io.Reader
	out io.Writer
}

// NewBuffer provides the io utils references for Solution
func NewBuffer(in io.Reader, out io.Writer) *Buffer {
	return &Buffer{
		b:   make([]byte, _B),
		in:  in,
		out: out,
	}
}

type test struct {
	n, x    int
	ratings []int
}

func readInt(b []byte, i, n int) (int, int) {
	for ; b[i] != '-' && !(b[i] >= '0' && b[i] <= '9'); i++ {
	}

	neg := b[i] == '-'
	if neg {
		i++
	}

	var x int
	for ; i < n && b[i] >= '0' && b[i] <= '9'; i++ {
		x = (x << 3) + (x << 1) + int(b[i]&15)
	}

	if neg {
		x = -x
	}

	return x, i
}

func (o *Buffer) processInput() (int, []test) {
	b, _ := ioutil.ReadAll(o.in)
	n := len(b)

	var t int
	var i int

	t, i = readInt(b, i, n)
	tests := make([]test, t)

	for j := 0; j < t; j++ {
		test := &tests[j]

		test.n, i = readInt(b, i, n)
		test.x, i = readInt(b, i, n)

		test.ratings = make([]int, test.n)
		for k := 0; k < test.n; k++ {
			test.ratings[k], i = readInt(b, i, n)
		}
	}

	return t, tests
}

func (o *Buffer) println(x int) {
	o.b[o.i] = byte(x) + '0'
	o.i++
	o.b[o.i] = '\n'
	o.i++

	if o.i >= len(o.b)-(1<<4) {
		o.flush()
	}
}

func (o *Buffer) flush() {
	if o.i > 0 {
		o.out.Write(o.b[:o.i])
		o.i = 0
	}
}

// Solution solves cf 1419 C
func Solution(_io *Buffer) {

	t, tests := _io.processInput()
	// fmt.Println(t, tests)

	for i := 0; i < t; i++ {
		test := &tests[i]

		// 2 <= n <= 1e3
		// -4e3 <= x <= 4e3

		someEqual := false
		allEqual := true

		for j := 0; j < test.n; j++ {
			if test.ratings[j] == test.x {
				someEqual = true
			} else {
				allEqual = false
			}
		}

		if allEqual {
			_io.println(0)
			continue
		}

		if someEqual {
			_io.println(1)
			continue
		}

		sum := 0
		for j := 0; j < test.n; j++ {
			sum += test.x - test.ratings[j]
		}

		if sum == 0 {
			_io.println(1)
		} else {
			_io.println(2)
		}
	}

	_io.flush()
}

func main() {
	_io := NewBuffer(os.Stdin, os.Stdout)
	Solution(_io)
}

package main

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

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

func main() {
	defer rw.Flush()
	for tt := rw.NextInt(); tt > 0; tt-- {
		n, x := rw.NextInt(), rw.NextInt()
		a := rw.NextInts(n)
		var sum int
		all := true
		any := false
		for _, e := range a {
			all = all && e == x
			any = any || e == x
			sum += e
		}
		if n*x == sum {
			if all {
				rw.Println(0)
			} else {
				rw.Println(1)
			}
		} else {
			if any {
				rw.Println(1)
			} else {
				rw.Println(2)
			}
		}
	}
}

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

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

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

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

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

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

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

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

package main

import (
	"io"
	"io/ioutil"
	"os"
)

const _B = 1024 * 1024

// Buffer is the IO middleware
type Buffer struct {
	i   int
	b   []byte
	in  io.Reader
	out io.Writer
}

// NewBuffer provides the io utils references for Solution
func NewBuffer(in io.Reader, out io.Writer) *Buffer {
	return &Buffer{
		b:   make([]byte, _B),
		in:  in,
		out: out,
	}
}

type test struct {
	n, x    int
	ratings []int
}

func readInt(b []byte, i, n int) (int, int) {
	for ; b[i] != '-' && !(b[i] >= '0' && b[i] <= '9'); i++ {
	}

	neg := b[i] == '-'
	if neg {
		i++
	}

	var x int
	for ; i < n && b[i] >= '0' && b[i] <= '9'; i++ {
		x = (x << 3) + (x << 1) + int(b[i]&15)
	}

	if neg {
		x = -x
	}

	return x, i
}

func (o *Buffer) processInput() (int, []test) {
	b, _ := ioutil.ReadAll(o.in)
	n := len(b)

	var t int
	var i int

	t, i = readInt(b, i, n)
	tests := make([]test, t)

	for j := 0; j < t; j++ {
		test := &tests[j]

		test.n, i = readInt(b, i, n)
		test.x, i = readInt(b, i, n)

		test.ratings = make([]int, test.n)
		for k := 0; k < test.n; k++ {
			test.ratings[k], i = readInt(b, i, n)
		}
	}

	return t, tests
}

func (o *Buffer) println(x int) {
	o.b[o.i] = byte(x) + '0'
	o.i++
	o.b[o.i] = '\n'
	o.i++

	if o.i >= len(o.b)-(1<<4) {
		o.flush()
	}
}

func (o *Buffer) flush() {
	if o.i > 0 {
		o.out.Write(o.b[:o.i])
		o.i = 0
	}
}

// Solution solves cf 1419 C
func Solution(_io *Buffer) {

	t, tests := _io.processInput()
	// fmt.Println(t, tests)

	for i := 0; i < t; i++ {
		test := &tests[i]

		// 2 <= n <= 1e3
		// -4e3 <= x <= 4e3

		allEqual := true
		for j := 0; j < test.n; j++ {
			if test.ratings[j] != test.x {
				allEqual = false
				break
			}
		}

		if allEqual {
			_io.println(0)
			continue
		}

		sum := 0
		for j := 0; j < test.n; j++ {
			sum += test.x - test.ratings[j]
		}

		if sum == 0 {
			_io.println(1)
		} else {
			_io.println(2)
		}
	}

	_io.flush()
}

func main() {
	_io := NewBuffer(os.Stdin, os.Stdout)
	Solution(_io)
}

package main

import (
	"io"
	"io/ioutil"
	"os"
)

const _B = 1024 * 1024

// Buffer is the IO middleware
type Buffer struct {
	i   int
	b   []byte
	in  io.Reader
	out io.Writer
}

// NewBuffer provides the io utils references for Solution
func NewBuffer(in io.Reader, out io.Writer) *Buffer {
	return &Buffer{
		b:   make([]byte, _B),
		in:  in,
		out: out,
	}
}

type test struct {
	n, x    int
	ratings []int
}

func readInt(b []byte, i, n int) (int, int) {
	for ; b[i] != '-' && !(b[i] >= '0' && b[i] <= '9'); i++ {
	}

	neg := b[i] == '-'
	if neg {
		i++
	}

	var x int
	for ; i < n && b[i] >= '0' && b[i] <= '9'; i++ {
		x = (x << 3) + (x << 1) + int(b[i]&15)
	}

	if neg {
		x = -x
	}

	return x, i
}

func (o *Buffer) processInput() (int, []test) {
	b, _ := ioutil.ReadAll(o.in)
	n := len(b)

	var t int
	var i int

	t, i = readInt(b, i, n)
	tests := make([]test, t)

	for j := 0; j < t; j++ {
		test := &tests[j]

		test.n, i = readInt(b, i, n)
		test.x, i = readInt(b, i, n)

		test.ratings = make([]int, test.n)
		for k := 0; k < test.n; k++ {
			test.ratings[k], i = readInt(b, i, n)
		}
	}

	return t, tests
}

func (o *Buffer) println(x int) {
	o.b[o.i] = byte(x) + '0'
	o.i++
	o.b[o.i] = '\n'
	o.i++

	if o.i >= len(o.b)-(1<<4) {
		o.flush()
	}
}

func (o *Buffer) flush() {
	if o.i > 0 {
		o.out.Write(o.b[:o.i])
		o.i = 0
	}
}

// Solution solves cf 1419 C
func Solution(_io *Buffer) {

	t, tests := _io.processInput()
	// fmt.Println(t, tests)

	for i := 0; i < t; i++ {
		test := &tests[i]

		// 2 <= n <= 1e3
		// -4e3 <= x <= 4e3

		ans := 0
		sum := 0

		for j := 0; j < test.n-1; j++ {
			sum += test.x - test.ratings[j]
		}

		if sum != 0 {
			ans++
		}

		if test.n > 0 {
			if test.x-test.ratings[test.n-1] != -sum {
				ans++
			}
		}

		_io.println(ans)
	}

	_io.flush()
}

func main() {
	_io := NewBuffer(os.Stdin, os.Stdout)
	Solution(_io)
}

package main

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

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

func main() {
	defer rw.Flush()
	for tt := rw.NextInt(); tt > 0; tt-- {
		n, x := rw.NextInt(), rw.NextInt()
		a := rw.NextInts(n)
		var sum int
		all := true
		for _, e := range a {
			all = all && e == x
			sum += e
		}
		if n*x == sum {
			if all {
				rw.Println(0)
			} else {
				rw.Println(1)
			}
		} else {
			rw.Println(2)
		}
	}
}

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

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

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

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

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

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

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

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