Vladik often travels by trains. He remembered some of his trips especially well and I would like to tell you about one of these trips:Vladik is at initial train station, and now n people (including Vladik) want to get on the train. They are already lined up in some order, and for each of them the city code ai is known (the code of the city in which they are going to).Train chief selects some number of disjoint segments of the original sequence of people (covering entire sequence by segments is not necessary). People who are in the same segment will be in the same train carriage. The segments are selected in such way that if at least one person travels to the city x, then all people who are going to city x should be in the same railway carriage. This means that they can’t belong to different segments. Note, that all people who travel to the city x, either go to it and in the same railway carriage, or do not go anywhere at all.Comfort of a train trip with people on segment from position l to position r is equal to XOR of all distinct codes of cities for people on the segment from position l to position r. XOR operation also known as exclusive OR.Total comfort of a train trip is equal to sum of comfort for each segment.Help Vladik to know maximal possible total comfort.

First line contains single integer n (1 ≤ n ≤ 5000) — number of people. Second line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 5000), where ai denotes code of the city to which i-th person is going.

The output should contain a single integer — maximal possible total comfort.

NoteIn the first test case best partition into segments is: [4, 4] [2, 5, 2] [3], answer is calculated as follows: 4 + (2 xor 5) + 3 = 4 + 7 + 3 = 14In the second test case best partition into segments is: 5 1 [3] 1 5 [2, 4, 2] 5, answer calculated as follows: 3 + (2 xor 4) = 3 + 6 = 9.

package main

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

const (
  N_MAX = 5001
  INF = 123456789
)

func Init() {
}

func Solve(io *FastIO) {
	N := io.NextInt()
	A := io.NextIntArrayOffset(N, 1)

	L := make([]int, N_MAX)
	R := make([]int, N_MAX)

	for i := 1; i <= N; i++ {
	  R[A[i]] = i
	}
	for i := N; i > 0; i-- {
	  L[A[i]] = i
	}

	cost := make([]int, N + 1)
	end := make([]int, N + 1)

	for i := 1; i <= N; i++ {
	  if L[A[i]] != i {
	    cost[i] = -INF
	    continue
	  }
	  far := R[A[i]]
	  idx := i
	  xor := A[i]
	  seen := make(map[int]bool)
	  seen[A[i]] = true
	  for idx < far {
	    if L[A[idx]] < i {
	      xor = -INF
	      break
	    }
	    if !seen[A[idx]] {
	      seen[A[idx]] = true
	      xor ^= A[idx]
	      far = Max(far, R[A[idx]])
	    }
	    idx++
	  }
	  cost[i] = xor
	  end[i] = far
	}

// 	fmt.Println(end)
// 	fmt.Println(cost)

	best := make([]int, N + 1)
	bestSoFar := 0
	for i := 1; i <= N; i++ {
	  best[end[i]] = Max(best[end[i]], bestSoFar, bestSoFar + cost[i])
	  bestSoFar = Max(bestSoFar, best[i])
	}
// 	fmt.Println(best)
	io.Println(bestSoFar)
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

package main

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

const (
    N_MAX = 5000
)

func Init() {
}

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

    all := NewFreqMap()
    for _, v := range A {
        all.Increment(v)
    }

    best := make([]int, N)
    prev := 0
    for i := range A {
        best[i] = Max(best[i], prev)
        out := all.Clone()
        has := NewFreqMap()
        for j := i; j < N; j++ {
            out.Decrement(A[j])
            has.Increment(A[j])
            if out.Size + has.Size == all.Size {
                best[j] = Max(best[j], prev + has.XorScore)
            }
        }
        prev = best[i]
    }
    fmt.Println(best[N - 1])
}

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

type FreqMap struct {
    Freq [N_MAX + 1]int
    Size int
    XorScore int
}

func NewFreqMap() *FreqMap {
    return &FreqMap{}
}

func (this *FreqMap) Increment(k int) {
    if (this.Freq[k] == 0) {
        this.XorScore ^= k
        this.Size++
    }
    this.Freq[k]++
}

func (this *FreqMap) Decrement(k int) {
    if this.Freq[k] == 1 {
        this.XorScore ^= k
        this.Size--
    }
    this.Freq[k]--
}

func (this FreqMap) Clone() *FreqMap {
    freq := NewFreqMap()
    for k, v := range this.Freq {
        freq.Freq[k] = v
    }
    freq.Size = this.Size
    freq.XorScore = this.XorScore
    return freq
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


She is skilled in all kinds of magics, and is keen on inventing new one.—Perfect Memento in Strict SensePatchouli is making a magical talisman. She initially has $$$n$$$ magical tokens. Their magical power can be represented with positive integers $$$a_1, a_2, \ldots, a_n$$$. Patchouli may perform the following two operations on the tokens.  Fusion: Patchouli chooses two tokens, removes them, and creates a new token with magical power equal to the sum of the two chosen tokens.  Reduction: Patchouli chooses a token with an even value of magical power $$$x$$$, removes it and creates a new token with magical power equal to $$$\frac{x}{2}$$$. Tokens are more effective when their magical powers are odd values. Please help Patchouli to find the minimum number of operations she needs to make magical powers of all tokens odd values.

Each test contains multiple test cases.  The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^3$$$) — the number of test cases. The description of the test cases follows. For each test case, the first line contains one integer $$$n$$$ ($$$1 \leq n\leq 2\cdot 10^5$$$) — the initial number of tokens. The second line contains $$$n$$$ intergers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the initial magical power of the $$$n$$$ tokens. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, print a single integer — the minimum number of operations Patchouli needs to make all tokens have an odd value of magical power. It can be shown that under such restrictions the required sequence of operations exists.

NoteTest case 1:$$$a$$$ consists solely of odd numbers initially.Test case 2:Choose the tokens with magical power of $$$1$$$ and $$$2$$$ and perform Fusion. Now $$$a=[1,3]$$$, both are odd numbers.Test case 3:Choose the tokens with magical power of $$$2$$$ and $$$8$$$ and perform Fusion. Now $$$a=[4,10]$$$.Choose the token with magical power of $$$10$$$ and perform Reduction. Now $$$a=[4,5]$$$.Choose the tokens with magical power of $$$4$$$ and $$$5$$$ and perform Fusion. Now $$$a=[9]$$$, and $$$9$$$ is an odd number.It can be shown that you can not make all the magical powers odd numbers in less than $$$3$$$ moves, so the answer is $$$3$$$.

package main

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

type CF1688B struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1688B) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1688B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1688B) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1688B) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1688B) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1688B
Date: 9/1/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1688/B
Problem: CF1688B
**/
func (in *CF1688B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		arr := make([]int, n)
		odd := 0
		even := 0
		evenIndex := -1
		for i := 0; i < n; i++ {
			arr[i] = in.NextInt()
			if arr[i]%2 == 0 {
				if evenIndex == -1 {
					evenIndex = i
				}
				if arr[evenIndex] > arr[i] {
					evenIndex = i
				}
				even++
			} else {
				odd++
			}
		}

		ans := 0
		if odd > 0 {
			ans = even
		} else {
			count := -1
			for i := 0; i < n; i++ {
				if arr[i]%2 == 0 {
					aux := 0
					for arr[i]%2 == 0 {
						arr[i] /= 2
						aux++
					}
					if count == -1 {
						count = aux
					}
					if aux < count {
						count = aux
					}
				}

			}
			ans = count + n - 1
		}
		fmt.Println(ans)
	}
}

func NewCF1688B(r *bufio.Reader) *CF1688B {
	return &CF1688B{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1688B(bufio.NewReader(os.Stdin)).Run()
}

package main

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

func main() {
	var t int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &t)
	for e := 0; e < t; e++ {
		var n int
		fmt.Fscan(in, &n)
		a := make([]int, n)
		f1, f2 := false, true
		kn := 0
		pos := 0
		km := 0
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			if a[i]%2 != 0 {
				f1 = true
			}

			if a[i]%2 == 0 {
				p := a[i]
				kl := 0
				for u := 0; ; u++ {
					kl++
					p /= 2
					if p%2 != 0 {
						break
					}
				}
				if f2 || (kl < km) {
					pos = i
					km = kl
				}
				f2 = false
				kn++
			}
			//fmt.Println(km, "////*")
		}
		if f1 && f2 {
			fmt.Println(0)
		} else {
			if f1 {
				fmt.Println(kn)
			} else {
				k := 0
				for {
					a[pos] /= 2
					k++
					//fmt.Println(pos, "//")
					if a[pos]%2 != 0 {
						fmt.Println(k + kn - 1)
						break
					}
				}
			}
		}

	}
}

package main

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

/*
6
1
2
1
3
2
4 6
3
8 12 13
4
12 8 4 10
4
10 20 30 4
---
1
0
2
2
4
4

1
5
16 16 16 16 1
---
4

*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int
	scanf("%d\n", &n)

	a := make([]int, n)

	oddCnt := 0

	minRed := maxInt

	for i := range a {
		scanf("%d", &a[i])

		if a[i]%2 != 0 {
			oddCnt++
			minRed = 1
		} else {
			num := a[i]

			red := 0

			for num%2 == 0 {
				num /= 2
				red++
			}

			minRed = min(minRed, red)
		}
	}
	scanf("\n")

	ans := 0

	evenCnt := n - oddCnt

	if evenCnt > 0 {
		ans = evenCnt - 1 + minRed
	}

	fmt.Fprintln(writer, ans)
}

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

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

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

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

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

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

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int) int {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func sum(nums ...int) (sum int) {
	for _, num := range nums {
		sum += num
	}
	return
}

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

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

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

	for i := int64(5); i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

//goland:noinspection GoUnusedFunction
func factorize(num int64) []int64 {
	factors := make([]int64, 0)

	for ftr := int64(2); ftr*ftr <= num; ftr++ {
		for num%ftr == 0 {
			num /= ftr
			factors = append(factors, ftr)
		}
	}

	if num > 1 {
		factors = append(factors, num)
	}

	return factors
}

package main

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

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

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

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

		if e != n {
			fmt.Fprintln(out, e)
		} else {
			g := math.MaxInt32
			for _, v := range arr {
				x := bits.TrailingZeros(uint(v))
				if x < g {
					g = x
				}
			}
			fmt.Fprintln(out, g+n-1)
		}
	}
}

package main

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

func main() {
	scanner := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var ntests int
	fmt.Fscan(scanner, &ntests)
	for iter := 0; iter < ntests; iter++ {
		var n int
		fmt.Fscan(scanner, &n)
		free := 0
		odd := 10000
		for i := 0; i < n; i++ {
			var tmp uint
			fmt.Fscan(scanner, &tmp)
			co := 0
			if tmp%2 == 0 {
				for tmp%2 == 0 {
					co++
					tmp >>= 1
				}
				if co < odd {
					odd = co
				}
			} else {
				odd = 0
				free++
			}
		}
		if free != 0 {
			fmt.Fprintln(writer, n-free)
		} else {
			fmt.Fprintln(writer, n-1+odd)
		}
	}
}

package main

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

package main

import (
	"bufio"
	"fmt"
	"os"
	"runtime/debug"
)

/* begin of MyIO */
const eof = 0

type any = interface{}

type MyIO struct {
	in  *os.File
	out *bufio.Writer
	_i  int
	_n  int
	buf []byte
}

func NewIO(in *os.File, out *os.File) *MyIO {
	return &MyIO{
		in,
		bufio.NewWriter(out),
		0, 0, make([]byte, 1<<12),
	}
}

func (myio *MyIO) rc() byte {
	if myio._i == myio._n {
		myio._n, _ = myio.in.Read(myio.buf)
		if myio._n == 0 { // EOF
			return eof
		}
		myio._i = 0
	}
	b := myio.buf[myio._i]
	myio._i++
	return b
}

func (myio *MyIO) ReadInt() (x int) {
	rc := myio.rc
	b := rc()
	neg := false
	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
		if b == '-' {
			neg = true
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = (x << 1) + (x << 3) + int(b&15)
	}
	if neg {
		return -x
	}
	return
}

func (myio *MyIO) ReadInt64() (x int64) {
	rc := myio.rc
	b := rc()
	neg := false

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
		if b == '-' {
			neg = true
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = (x << 1) + (x << 3) + int64(b&15)
	}
	if neg {
		return -x
	}
	return x
}

func (myio *MyIO) ReadBytes() (s []byte) {
	rc := myio.rc
	b := rc()
	for ; b <= ' '; b = rc() {
	}
	for ; b > ' '; b = rc() {
		s = append(s, b)
	}
	return
}

func (myio *MyIO) ReadStr() (s string) {
	return string(myio.ReadBytes())
}

func (myio *MyIO) Flush() {
	myio.out.Flush()
}

func (myio *MyIO) Printf(format string, a ...any) (n int, err error) {
	return fmt.Fprintf(myio.out, format, a...)
}

func (myio *MyIO) Println(a ...any) (n int, err error) {
	return fmt.Fprintln(myio.out, a...)
}

func (myio *MyIO) Print(a ...any) (n int, err error) {
	return fmt.Fprint(myio.out, a...)
}

var myio *MyIO

func ri() int {
	return myio.ReadInt()
}

func ri64() int64 {
	return myio.ReadInt64()
}

func ria(_n int) []int {
	a := make([]int, _n)
	for i := range a {
		a[i] = ri()
	}
	return a
}

/* End of MyIO */

func sol() {
	n := ri()
	var ev, mb int = 0, 1024
	for i := 0; i < n; i++ {
		z := ri()
		if (z & 1) == 0 {
			ev++
			zz := z & -z
			mt := 0
			for ; zz != 1; zz >>= 1 {
				mt++
			}
			if mb > mt {
				mb = mt
			}
		}
	}
	if ev < n {
		myio.Println(ev)
	} else if ev > 0 {
		myio.Println(mb + ev - 1)
	} else {
		myio.Println(0)
	}
}

func main() {
	debug.SetGCPercent(-1)
	in, out := os.Stdin, os.Stdout
	myio = NewIO(in, out)
	defer myio.Flush()

	var t = 1
	t = ri()
	for ; t > 0; t-- {
		sol()
	}
}

package main

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

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

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

	var T int
	var n int
	var x int

	loop:
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]int, n)
		oc := 0
		for i := 0; i < n; i++ {
			Fscan(in, &x)
			a[i] = x
			if x%2 == 0 {
				oc++
			}
		}
		if oc == 0 {
			Fprintln(out, 0)
			continue loop
		}
		if oc < n {
			Fprintln(out, oc)
			continue loop
		}

		sort.Ints(a)
		mi := 32
		for _, v := range a {
			t := 0
			for v%2 == 0 {
				v /= 2
				t++
				if t > mi {
					break
				}
			}
			if t < mi {
				mi = t
			}
		}

		Fprintln(out, oc + mi-1)
	}
}

package main

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

// This example demonstrates an integer heap built using the heap interface.

// An IntHeap is a min-heap of ints.
/* type IntHeap []int64

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

func (h *IntHeap) Push(x interface{}) {
	// Push and Pop use pointer receivers because they modify the slice's length,
	// not just its contents.
	*h = append(*h, x.(int64))
}

func (h *IntHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}
*/

// // This example inserts several ints into an Int, checks the minimum,
// // and removes them in order of priority.
// func main() {
// 	h := &IntHeap{2, 1, 5}
// 	heap.Init(h)
// 	heap.Push(h, 3)
// 	fmt.Printf("minimum: %d\n", (*h)[0])
// 	for h.Len() > 0 {
// 		fmt.Printf("%d ", heap.Pop(h))
// 	}
// }

func A2() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	Fscan(in, &cas)
	var sol func()
	// Coding...beg!
	sol = func() {
		var n int
		Fscan(in, &n)
		a := make([]int, n)
		ans := int(1e9 + 7)
		var e int
		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
		}
		for i := 0; i < n; i++ {
			if a[i]%2 == 0 {
				e++
				var cnt int
				for a[i]%2 == 0 {
					cnt++
					a[i] /= 2
				}
				if cnt < ans {
					ans = cnt
				}
			} else {

			}
		}
		if e != n {
			Fprintln(out, e)
		} else {
			Fprintln(out, ans+(e-1))
		}
	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}

func main() { A2() }

package main

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

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

func slove() {
	var n int
	fmt.Fscan(r, &n)
	nums := make([]int, n)
	cnt := 0
	for i:=0;i<n;i++ {
		fmt.Fscan(r, &nums[i])
		if nums[i] % 2 == 0 {
			cnt++
		}
	}
	if cnt != n {
		fmt.Fprintln(w, cnt)
	} else {
		m := 32
		for i := range nums {
			for j:=0;j<32;j++ {
				if nums[i] & (1 << j) != 0 {
					m = min(m, j)
					break
				}
			}
		}
		fmt.Fprintln(w, m + n - 1)
	}

}

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

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

package main

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

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

func main() {
	defer out.Flush()

	var tc int
	for fmt.Fscan(in, &tc); tc > 0; tc-- {
		var n int
		fmt.Fscan(in, &n)
		var evencnt, minpow int
		minpow = 999
		for i := 0; i < n; i++ {
			var a int
			fmt.Fscan(in, &a)
			if a%2 == 0 {
				evencnt++
			}
			p := bits.TrailingZeros(uint(a))
			if p < minpow {
				minpow = p
			}
		}
		if minpow == 0 {
			fmt.Fprintln(out, evencnt)
		} else {
			fmt.Fprintln(out, minpow+evencnt-1)
		}
	}
}

package main

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

type CF1688B struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1688B) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1688B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1688B) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1688B) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1688B) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1688B
Date: 9/1/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1688/B
Problem: CF1688B
**/
func (in *CF1688B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		arr := make([]int, n)
		odd := 0
		even := 0
		evenIndex := -1
		count := 20000000000
		for i := 0; i < n; i++ {
			arr[i] = in.NextInt()
			if arr[i] > count {
				count = arr[i] + 1
			}
			if arr[i]%2 == 0 {
				if evenIndex == -1 {
					evenIndex = i
				}
				if arr[evenIndex] > arr[i] {
					evenIndex = i
				}
				even++
			} else {
				odd++
			}
		}

		ans := 0
		if odd > 0 {
			ans = n - odd
		} else {

			for i := evenIndex; i < n; i++ {
				if arr[i]%2 == 0 {
					aux := 0
					for arr[i]%2 == 0 {
						arr[i] /= 2
						aux++
					}
					if aux < count {
						count = aux
					}
				}

			}
			ans += count + n - 1
		}
		fmt.Println(ans)
	}
}

func NewCF1688B(r *bufio.Reader) *CF1688B {
	return &CF1688B{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1688B(bufio.NewReader(os.Stdin)).Run()
}

package main

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

type CF1688B struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1688B) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1688B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1688B) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1688B) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1688B) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1688B
Date: 9/1/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1688/B
Problem: CF1688B
**/
func (in *CF1688B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		arr := make([]int, n)
		odd := 0
		even := 0
		evenIndex := -1
		for i := 0; i < n; i++ {
			arr[i] = in.NextInt()
			if arr[i]%2 == 0 {
				if evenIndex == -1 {
					evenIndex = i
				}
				if arr[evenIndex] > arr[i] {
					evenIndex = i
				}
				even++
			} else {
				odd++
			}
		}

		ans := 0
		if odd > 0 {
			ans = n - odd
		} else {
			for arr[evenIndex]%2 == 0 {
				arr[evenIndex] /= 2
				ans++
			}
			ans += n - 1
		}
		fmt.Println(ans)
	}
}

func NewCF1688B(r *bufio.Reader) *CF1688B {
	return &CF1688B{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1688B(bufio.NewReader(os.Stdin)).Run()
}

package main

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

func main() {
	var t int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &t)
	for e := 0; e < t; e++ {
		var n int
		fmt.Fscan(in, &n)
		a := make([]int, n)
		f1, f2 := false, true
		kn := 0
		pos := 0
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			if a[i]%2 != 0 {
				f1 = true
			}
			if a[i]%2 == 0 {
				if f2 || a[i] < a[pos] {
					pos = i
				}
				f2 = false
				kn++
			}
		}
		if f1 && f2 {
			fmt.Println(0)
		} else {
			if f1 {
				fmt.Println(kn)
			} else {
				k := 0
				for {
					a[pos] /= 2
					k++
					if a[pos]%2 != 0 {
						fmt.Println(k + kn - 1)
						break
					}
				}
			}
		}

	}
}

package main

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

/*
6
1
2
1
3
2
4 6
3
8 12 13
4
12 8 4 10
4
10 20 30 4
---
1
0
2
3
4
4

4 6 -> 10 -> 5
8 12 13 -> 20 -> 10 -> 5

8 -> 4 -> 2 -> 1

12 8 4 10 -> 34 -> 17

8 16 32 64 -> 100 -> 50 -> 25

2 2 2 2 2 -> 10 -> 5 == 5ops
4 4 4 4 4 -> 20 -> 10 -> 5 == 6ops

10 20 30 4 -3> 64 -> 32 -> 16 -> 8 -> 4 -> 2 -> 1 == 9ops
5  10 15 2
    5    1

*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int
	scanf("%d\n", &n)

	a := make([]int, n)

	oddCnt := 0

	minRed := maxInt

	for i := range a {
		scanf("%d", &a[i])

		if a[i]%2 != 0 {
			oddCnt++
		} else {
			num := a[i]

			red := 0

			for num%2 == 0 {
				num /= 2
				red++
			}

			minRed = min(minRed, red)
		}
	}
	scanf("\n")

	ans := 0

	evenCnt := n - oddCnt

	if evenCnt > 0 {
		ans = evenCnt - 1 + minRed
	}

	fmt.Fprintln(writer, ans)
}

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

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

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

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

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

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

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int) int {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func sum(nums ...int) (sum int) {
	for _, num := range nums {
		sum += num
	}
	return
}

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

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

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

	for i := int64(5); i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

//goland:noinspection GoUnusedFunction
func factorize(num int64) []int64 {
	factors := make([]int64, 0)

	for ftr := int64(2); ftr*ftr <= num; ftr++ {
		for num%ftr == 0 {
			num /= ftr
			factors = append(factors, ftr)
		}
	}

	if num > 1 {
		factors = append(factors, num)
	}

	return factors
}

package main

import (
	"bufio"
	"fmt"
	"math/bits"
	"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 int
		fmt.Fscan(in, &n)

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

		if e != n {
			fmt.Fprintln(out, e)
		} else {
			sort.Ints(arr)
			g := bits.TrailingZeros(uint(arr[0]))
			fmt.Fprintln(out, g+n-1)
		}
	}
}


Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is ti and its pages' width is equal to wi. The thickness of each book is either 1 or 2. All books have the same page heights.  Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure.  Help Shaass to find the minimum total thickness of the vertical books that we can achieve.

The first line of the input contains an integer n, (1 ≤ n ≤ 100). Each of the next n lines contains two integers ti and wi denoting the thickness and width of the i-th book correspondingly, (1 ≤ ti ≤ 2, 1 ≤ wi ≤ 100).

On the only line of the output print the minimum total thickness of the vertical books that we can achieve.

package main

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

// github.com/EndlessCheng/codeforces-go
func CF294B(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	b := [3][]int{}
	var n, t, w, all, s int
	Fscan(in, &n)
	for i := 0; i < n; i++ {
		Fscan(in, &t, &w)
		b[t] = append(b[t], w)
		all += w
	}
	for i := 1; i < 3; i++ {
		sort.Sort(sort.Reverse(sort.IntSlice(b[i])))
		b[i] = append(b[i], 0)
	}
	ans := int(1e9)
	for i, v := range b[1] {
		ss := s
		for j, w := range b[2] {
			b := i + j*2
			top := all - ss
			if top <= b && b < ans {
				ans = b
			}
			ss += w
		}
		s += v
	}
	Fprint(out, ans)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF294B(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	b := [3][]int{}
	var n, t, w, all, s int
	Fscan(in, &n)
	for i := 0; i < n; i++ {
		Fscan(in, &t, &w)
		b[t] = append(b[t], w)
		all += w
	}
	for i := 1; i < 3; i++ {
		sort.Sort(sort.Reverse(sort.IntSlice(b[i])))
		b[i] = append(b[i], 0)
	}
	ans := int(1e9)
	for i, v := range b[1] {
		ss := s
		for j, w := range b[2] {
			if t := i + j*2; t >= all-ss && t < ans {
				ans = t
			}
			ss += w
		}
		s += v
	}
	Fprint(out, ans)
}

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


Mark is asked to take a group photo of $$$2n$$$ people. The $$$i$$$-th person has height $$$h_i$$$ units.To do so, he ordered these people into two rows, the front row and the back row, each consisting of $$$n$$$ people. However, to ensure that everyone is seen properly, the $$$j$$$-th person of the back row must be at least $$$x$$$ units taller than the $$$j$$$-th person of the front row for each $$$j$$$ between $$$1$$$ and $$$n$$$, inclusive.Help Mark determine if this is possible.

The first line contains one integer $$$t$$$ ($$$1\leq t\leq 100$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two positive integers $$$n$$$ and $$$x$$$ ($$$1\leq n\leq 100$$$, $$$1\leq x\leq 10^3$$$) — the number of people in each row and the minimum difference Mark wants. The second line of each test case contains $$$2n$$$ positive integers $$$h_1,h_2,\ldots,h_{2n}$$$ ($$$1\leq h_i\leq 10^3$$$) — the height of each person in units. Note that the sum of $$$n$$$ over all test cases is not bounded.

For each test case, print a single line containing "YES" if Mark could arrange people satisfying his condition and "NO" otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answers).

NoteIn the first test case, one possible order is to have the third, fifth, and sixth person on the back row and the second, first, and fourth on the front row. The heights of the people will look like this. Back$$$9$$$$$$12$$$$$$16$$$Front$$$3$$$$$$1$$$$$$10$$$ It works because   $$$h_3-h_2 = 9-3 \geq 6$$$,    $$$h_5-h_1 = 12-1\geq 6$$$, and    $$$h_6-h_4 = 16-10\geq 6$$$. In the second test case, it can be shown there is no way to order people in a way that satisfies the condition.In the third test case, the only way to arrange people to satisfy the condition is to have the first person on the back row and the second person on the front row.

package main

import (
	"fmt"
	"sort"
)

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

	for tt := 0; tt < T; tt++ {
		var N, X int
		fmt.Scanf("%d %d\n", &N, &X)

		var arr []int
		var lim int = N << 1

		for i := 0; i < lim; i++ {
			var c int

			if i != lim - 1 {
				fmt.Scanf("%d", &c)
			} else {
				fmt.Scanf("%d\n", &c)
			}

			arr = append(arr, c)
		}

		sort.Ints(arr)

        var done bool = false

		for i := 0; i < N; i++ {
			if arr[i + N] - arr[i] < X {
				fmt.Println("NO")
                done = true
                break
			}
		}

        if done {
            continue
        }

		fmt.Println("YES")
	}
}
//

package main

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

func solve() {
	n := get()
	x := get()
	var arr []int
	for i := 0; i < 2*n; i++ {
		arr = append(arr, get())
	}
	sort.Ints(arr)
	for i := 0; i < n; i++ {
		if arr[i+n]-arr[i] < x {
			puts("NO")
			line()
			return
		}
	}
	puts("YES")
	line()
}

const (
	maxBufSize = 10000000
)

func main() {
	in = bufio.NewScanner(bufio.NewReader(os.Stdin))
	in.Split(bufio.ScanWords)
	in.Buffer(make([]byte, maxBufSize), maxBufSize)
	out = bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := get()
	for i := 0; i < t; i++ {
		solve()
	}
}

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

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

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

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

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

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

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

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

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

func yes() {
	puts("YES")
	line()
}

func no() {
	puts("NO")
	line()
}

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

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

package main

import (
	"fmt"
	"sort"
)

func solve() {
	var n int
	var x int
	fmt.Scan(&n)
	fmt.Scan(&x)
	var arr []int
	for i := 0; i < 2*n; i++ {
		var a int
		fmt.Scan(&a)
		arr = append(arr, a)
	}
	sort.Ints(arr)
	for i := 0; i < n; i++ {
		if arr[i+n]-arr[i] < x {
			fmt.Println("NO")
			return
		}
	}
	fmt.Println("YES")
}

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

package main

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

const N = 1000

var in = bufio.NewReader(os.Stdin)
var out = bufio.NewWriter(os.Stdout)
var t, n, x int
var check bool

func main() {
	defer out.Flush()
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n, &x)
		arr := make([]int, n*2)
		check = false
		for j := 0; j < 2*n; j++ {
			fmt.Fscan(in, &arr[j])
		}
		sort.Ints(arr)

		for j := 0; j < n; j++ {
			if arr[j+n]-arr[j] < x {
				check = true
				break
			}
		}
		if check {
			fmt.Fprintln(out, "NO")
		} else {
			fmt.Fprintln(out, "YES")
		}

	}
}

package main

import (
	"fmt"
	"sort"
)

const N = 1000

var t, n, x int
var check bool

func main() {
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		fmt.Scan(&n)
		fmt.Scan(&x)
		arr := make([]int, n*2)
		check = false
		for j := 0; j < 2*n; j++ {
			fmt.Scan(&arr[j])
		}
		sort.Ints(arr)

		for j := 0; j < n; j++ {
			if arr[j+n]-arr[j] < x {
				check = true
				break
			}
		}
		if check {
			fmt.Println("NO")
		} else {
			fmt.Println("YES")
		}

	}
}

package main

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

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

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

	var T int64
	fmt.Fscan(r, &T)
	for ; T > 0; T-- {
		var n, diff int64
		fmt.Fscan(r, &n, &diff)
		arr := make([]int, n*2)
		for i := int64(0); i < 2*n; i++ {
			fmt.Fscan(r, &arr[i])
		}
		sort.Ints(arr)
		can := true
		for i := int64(0); i < n; i++ {
			if int64(arr[i+n]-arr[i]) < diff {
				can = false
				break
			}
		}
		if can {
			fmt.Fprintln(w, "YES")
		} else {
			fmt.Fprintln(w, "NO")
		}

	}

}

package main
import "fmt"
import "sort"

func main() {

    var testCase int
    fmt.Scan(&testCase)
    for testCase > 0 {

        var n, x int
        var nums []int

        fmt.Scan(&n)
        fmt.Scan(&x)
        for i:=0; i<n*2; i++ {
            var v int
            fmt.Scan(&v)
            nums = append(nums, v)
        }
        sort.Ints(nums)
        res := "YES"
        for i:=0; i<n; i++ {
            if nums[i+n] - nums[i] < x {
                res = "NO"
                break
            }
        }

        fmt.Println(res)

        testCase--
    }
}

// 1705A. Mark the Photographer
package main

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

func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n, x int
		fmt.Fscan(reader, &n, &x)
		h := make([]int, 2*n)
		for i := 0; i < 2*n; i++ {
			fmt.Fscan(reader, &h[i])
		}
		sort.Ints(h)
		possible := true
		for i := 0; i < n; i++ {
			if h[n+i]-h[i] < x {
				possible = false
				break
			}
		}
		if possible {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

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, x int
		fmt.Fscan(in, &n, &x)
		aa := make([]int, n*2)
		for i := 0; i < n*2; i++ {
			fmt.Fscan(in, &aa[i])
		}

		sort.Ints(aa)
		f := true
		for i := 0; i < n; i++ {
			if aa[i+n]-aa[i] < x {
				f = false
				break
			}
		}
		if f {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main
import(
    "fmt"
    "bufio"
    "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,c int
		fmt.Fscan(in,&n,&c)
		list:= make([]int,n*2)
		for j:=0;j<n*2;j++{
			fmt.Fscan(in,&list[j])
		}
		sort.Ints(list)
		for j:=0;j<n;j++{
			if list[j+n]-list[j] < c {
				fmt.Fprintln(out,"NO")
				goto out
			}
		}
		fmt.Fprintln(out,"YES")
		out:
	}
}

package main

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

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

	var buf bytes.Buffer

	tc := readNum(reader)

	for tc > 0 {
		tc--
		n, x := readTwoNums(reader)
		heights := readNNums(reader, 2*n)
		res := solve(heights, x)
		if res {
			buf.WriteString("YES\n")
		} else {
			buf.WriteString("NO\n")
		}
	}

	fmt.Print(buf.String())
}

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

func normalize(s string) string {

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

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

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

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

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

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

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

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

	return i
}

func solve(heights []int, x int) bool {
	sort.Ints(heights)
	n := len(heights) / 2
	for i := 0; i < n; i++ {
		if heights[i+n]-heights[i] < x {
			return false
		}
	}
	return true
}

package main

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

const MOD int64 = 1000000007

var n, x int

var h[]int

// ByAge implements sort.Interface based on the Age field.
type ByV []*pair

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

func main() {

	read := bufio.NewReader(os.Stdin)

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

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

		fmt.Fscan(read, &n, &x)

		h = make([]int, 2 * n)

		for j := 0; j < 2 * n; j++ {
			fmt.Fscan(read, &h[j])
		}

		fmt.Println(solve())
	}
}

type pair struct {
	v int
	idx []int
}

func solve() string {
	sort.Ints(h)

	for i := 0; i < n; i++ {
		if h[i] + x > h[i + n] {
			return "NO"
		}
	}
	return "YES"
}

//IQ = 87

//........105....99...87..87..87..87.87.......87....

//            86

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

package main

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

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

func main() {
	defer out.Flush()

	var test int
	fmt.Fscan(in, &test)
	for i := 0; i < test; i++ {
		if solve() {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

func solve() bool {
	var n, x int
	fmt.Fscan(in, &n, &x)
	a := make([]int, n*2)
	for i := 0; i < n*2; i++ {
		fmt.Fscan(in, &a[i])
	}
	sort.Ints(a)
	for i := 0; i < n; i++ {
		if a[n+i] - a[i] < x {
			return false
		}
	}
	return true
}

package main

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

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

func main() {
	defer out.Flush()
	var t, n, x int
	for fmt.Fscan(in, &t); t > 0; t-- {
		fmt.Fscan(in, &n, &x)
		a := make([]int, 2*n)
		for i := 0; i < 2*n; i++ {
			fmt.Fscan(in, &a[i])
		}
		sort.Ints(a)
		flag := true
		for i := 0; i < n; i++ {
			if a[i]+x > a[i+n] {
				flag = false
				break
			}
		}
		if flag {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

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

var (
	reader  = bufio.NewReader(os.Stdin)
	t, n, x int
	h       []int
)

func solve(n, x int, h []int) string {
	sort.Slice(h, func(i, j int) bool {
		return h[i] < h[j]
	})
	for i := 0; i < n; i++ {
		if h[i+n]-h[i] < x {
			return "NO"
		}
	}
	return "YES"
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n, &x)
		h = make([]int, 2*n)
		for i := 0; i < 2*n; i++ {
			fmt.Fscan(reader, &h[i])
		}
		fmt.Println(solve(n, x, h))
	}
}

package main

import (
	"fmt"
	"sort"
)

func solve(arr []int, x int) bool {
	sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })
	n := len(arr) / 2
	for i := 0; i < n; i++ {
		if arr[i+n]-arr[i] < x {
			return false
		}
	}
	return true
}

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

	for i := 0; i < k; i++ {
		var n, x int
		fmt.Scan(&n, &x)
		arr := make([]int, n*2)
		for i := range arr {
			fmt.Scan(&arr[i])
		}
		if solve(arr, x) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

type CF1705A struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1705A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1705A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1705A) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1705A) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1705A) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1705A
Date: 20/07/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1705/A
Problem: CF1705A
**/
func (in *CF1705A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n, x := in.NextInt(), in.NextInt()
		arr := make([]int, 2*n)
		for i := 0; i < len(arr); i++ {
			arr[i] = in.NextInt()
		}

		sort.Ints(arr)
		ans := true
		for i := 0; i < n; i++ {
			if arr[i+n]-arr[i] < x {
				ans = false
				break
			}
		}

		if ans {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

func NewCF1705A(r *bufio.Reader) *CF1705A {
	return &CF1705A{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1705A(bufio.NewReader(os.Stdin)).Run()
}

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

const NL = '\n'

func main() {
	defer writer.Flush()
	var t, n, x, j int
	var isOkay uint8
	var g string
	scanf("%d\n", &t)
	j = 0
	for j < t {
		isOkay = 0
		scanf("%d %d\n", &n, &x)
		g, _ = reader.ReadString(NL)
		a := StrToIntSlice(g)
		sort.Ints(a)
		for i := 0; i < n; i++ {
			if distOk(a[i], a[n+i], x) {
				isOkay = 1 - isOkay
				break
			}
		}
		printf("%s\n", yesNoMap(isOkay))
		j++
	}
}
func split(str string) []string {
	return strings.Fields(str)
}

func Atoi(str string) int {
	out, _ := strconv.ParseInt(str, 10, 64)
	return int(out)
}

func StrToIntSlice(str string) []int {
	sarr := split(str)
	iarr := make([]int, len(sarr))
	for i := range sarr {
		iarr[i] = Atoi(sarr[i])
	}
	return iarr
}

func distOk(f, b, minDist int) bool {
	return (b - f) < minDist
}

func yesNoMap(b uint8) string {
	return map[uint8]string{0: "YES", 1: "NO"}[b]
}

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

package main

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

var reader *bufio.Reader

// 1705A
func main() {
	var (
		t int
		n int
		x int
	)

	reader = bufio.NewReader(os.Stdin)

	_, _ = fmt.Fscanln(reader, &t)

	for i := 0; i < t; i++ {
		_, _ = fmt.Fscanln(reader, &n, &x)
		n = n * 2

		heights := make([]int, n)
		for i := 0; i < n; i++ {
			_, _ = fmt.Fscan(reader, &heights[i])
		}
		_, _ = fmt.Fscanln(reader)

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

		var failed = false
		for i := 0; i < n/2; i++ {
			a := heights[n/2+i]
			b := heights[i]
			if a-b < x {
				fmt.Println("NO")
				failed = true
				break
			}
		}

		if failed {
			continue
		}

		fmt.Println("YES")
	}
}

package main

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

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)
	n := v[0]
	x := v[1]
	arr := readArrInt(in)

	sort.Ints(arr)

	for i := 0; i < n; i++ {
		if arr[i+n]-arr[i] < x {
			out.WriteString("NO\n")
			return
		}
	}

	out.WriteString("YES\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
}

package main

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

func solve() {
	n := get()
	x := get()
	a := make([]int, 2*n)
	for i := range a {
		a[i] = get()
	}
	sort.Ints(a)
	for i := 0; i < n; i++ {
		if a[i]+x > a[i+n] {
			no()
			return
		}
	}
	yes()
}

const (
	once       = false
	maxBufSize = 10000000
)

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

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

	if once {
		solve()
		return
	}

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

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

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

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

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

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

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

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

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

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

func yes() {
	puts("YES")
	line()
}

func no() {
	puts("NO")
	line()
}

//func putf(x float64) {
//	out.WriteString(strconv.FormatFloat(x))
//}

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

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

package main

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

func A0() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	Fscan(in, &cas)
	var sol func()
	// Coding...beg!
	sol = func() {
		var n int
		var x int
		Fscan(in, &n, &x)
		a := make([]int, 2*n)
		for i := 0; i < 2*n; i++ {
			Fscan(in, &a[i])
		}
		sort.Ints(a)
		for i := n; i < 2*n; i++ {
			if a[i]-a[i-n] < x {
				Fprintln(out, "No")
				return
			}
		}

		Fprintln(out, "Yes")
	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}
func main() { A0() }

package main

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

var _, _, _ = main, rw, do

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

func rw(r io.Reader, w io.Writer) {
	var t int
	fmt.Fscan(r, &t)
	for ti := 0; ti < t; ti++ {
		var n, k int
		fmt.Fscan(r, &n, &k)
		b := make([]int, 2*n)
		for i := range b {
			fmt.Fscan(r, &b[i])
		}
		w1 := do(n, k, b)
		fmt.Fprintln(w, w1)
	}
}

func do(n, k int, b []int) string {
	sort.Ints(b)
	for i := 0; i < n; i++ {
		if b[i+n]-b[i] < k {
			return "NO"
		}
	}
	return "YES"
}

package main

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

var rr = bufio.NewReader(os.Stdin)
var ww = bufio.NewWriter(os.Stdout)

func slove() {
	var n, x int
	fmt.Fscan(rr, &n, &x)
	nums := make([]int, 2*n)
	for i:=0;i<len(nums);i++ {
		fmt.Fscan(rr, &nums[i])
	}
	sort.Ints(nums)
	for i:=0;i<n;i++ {
		if nums[i] + x > nums[i+n] {
			fmt.Fprintln(ww, "NO")
			return
		}
	}
	fmt.Fprintln(ww, "YES")
}

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

package main

import (
	"fmt"
	"sort"
)

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

	for tt := 0; tt < T; tt++ {
		var N, X int
		fmt.Scanf("%d %d\n", &N, &X)

		var arr []int
		var lim int = N << 1

		for i := 0; i < lim; i++ {
			var c int

			if i != lim - 1 {
				fmt.Scanf("%d ", &c)
			} else {
				fmt.Scanf("%d\n", &c)
			}

			arr = append(arr, c)
		}

		sort.Ints(arr)

        var done bool = false

		for i := 0; i < N; i++ {
			if arr[i + N] - arr[i] < X {
				fmt.Println("NO")
                done = true
                break
			}
		}

        if done {
            continue
        }

		fmt.Println("YES")
	}
}

// 2
package main

import (
	"fmt"
	"sort"
)

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

	outer: for tt := 0; tt < T; tt++ {
		var N, X int
		fmt.Scanf("%d %d\n", &N, &X)

		var arr []int
		var lim int = N << 1

		for i := 0; i < lim; i++ {
			var c int

			if i != lim - 1 {
				fmt.Scanf("%d ", &c)
			} else {
				fmt.Scanf("%d\n", &c)
			}

			arr = append(arr, c)
		}

		sort.Ints(arr)

		for i := 0; i < N; i++ {
			if arr[i + N] - arr[i] < X {
				fmt.Println("NO")
				goto outer
			}
		}

		fmt.Println("YES")
	}
}

package main

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

func solve() {
	n := get()
	x := get()
	var arr []int
	for i := 0; i < 2*n; i++ {
		arr = append(arr, get())
	}
	sort.Ints(arr)
	for i := 0; i < n; i++ {
		if arr[i+n]-arr[i] < x {
			puts("NO")
			line()
			return
		}
	}
	puts("YES")
	line()
}

const (
	maxBufSize = 10000000
)

func main() {
	in = bufio.NewScanner(bufio.NewReader(os.Stdin))
	in.Split(bufio.ScanWords)
	in.Buffer(make([]byte, maxBufSize), maxBufSize)
	out = bufio.NewWriter(os.Stdout)
	// defer out.Flush()
	t := get()
	for i := 0; i < t; i++ {
		solve()
	}
}

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

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

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

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

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

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

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

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

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

func yes() {
	puts("YES")
	line()
}

func no() {
	puts("NO")
	line()
}

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

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

/******************************************************************************

                            Online Go Lang Compiler.
                Code, Compile, Run and Debug Go Lang program online.
Write your code in this editor and press "Run" button to execute it.

*******************************************************************************/

package main
import "fmt"
import "sort"

func main() {

    var testCase int
    fmt.Scan(&testCase)
    for testCase > 0 {

        var n, x int
        var nums []int

        fmt.Scan(&n)
        fmt.Scan(&x)
        for i:=0; i<n*2; i++ {
            var v int
            fmt.Scan(&v)
            nums = append(nums, v)
        }
        sort.Ints(nums)
        if nums[n]-nums[0] < x {
            fmt.Println("NO")
        } else {
            fmt.Println("YES")
        }

        testCase--
    }
}

// 1705A. Mark the Photographer
package main

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

func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n, x int
		fmt.Fscan(reader, &n, &x)
		h := make([]int, 2*n)
		for i := 0; i < 2*n; i++ {
			fmt.Fscan(reader, &h[i])
		}
		sort.Ints(h)
		if h[n]-h[0] >= x {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

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, j int
		fmt.Fscan(in, &n, &j)
		aa := make([]int, n*2)
		for i := 0; i < n*2; i++ {
			fmt.Fscan(in, &aa[i])
		}

		sort.Ints(aa)
		f := true
		for i := 0; i < n/2; i++ {
			if aa[i+n]-aa[i] < j {
				f = false
				break
			}
		}
		if f {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}


A social network for dogs called DH (DogHouse) has k special servers to recompress uploaded videos of cute cats. After each video is uploaded, it should be recompressed on one (any) of the servers, and only after that it can be saved in the social network.We know that each server takes one second to recompress a one minute fragment. Thus, any server takes m seconds to recompress a m minute video.We know the time when each of the n videos were uploaded to the network (in seconds starting from the moment all servers started working). All videos appear at different moments of time and they are recompressed in the order they appear. If some video appeared at time s, then its recompressing can start at that very moment, immediately. Some videos can await recompressing when all the servers are busy. In this case, as soon as a server is available, it immediately starts recompressing another video. The videos that await recompressing go in a queue. If by the moment the videos started being recompressed some servers are available, then any of them starts recompressing the video.For each video find the moment it stops being recompressed.

The first line of the input contains integers n and k (1 ≤ n, k ≤ 5·105) — the number of videos and servers, respectively. Next n lines contain the descriptions of the videos as pairs of integers si, mi (1 ≤ si, mi ≤ 109), where si is the time in seconds when the i-th video appeared and mi is its duration in minutes. It is guaranteed that all the si's are distinct and the videos are given in the chronological order of upload, that is in the order of increasing si.

Print n numbers e1, e2, ..., en, where ei is the time in seconds after the servers start working, when the i-th video will be recompressed.

package main

import (
    "container/heap"

    "bufio"
    "os"
    "strconv"
)

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

func ReadInt64() int64 {
    scanner.Scan()
    ans, _ := strconv.ParseInt(scanner.Text(), 0, 64)
    return ans
}

func Print64Ints(ints ...int64) {
    for _, value := range ints {
        writer.WriteString(strconv.FormatInt(value, 10))
        writer.WriteByte(' ')
    }
}

type IntHeap []int64

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

func (h *IntHeap) Push(x interface{}) {
    *h = append(*h, x.(int64))
}

func (h *IntHeap) Pop() interface{} {
    old := *h
    n := len(old)
    x := old[n-1]
    *h = old[0 : n-1]
    return x
}

func main() {
    defer writer.Flush()
    scanner.Split(bufio.ScanWords)
    videos, servers := ReadInt64(), ReadInt64()

    h := make(IntHeap, servers)
    heap.Init(&h)

    for videos > 0 {
        videos--
        start, length := ReadInt64(), ReadInt64()
        serv := heap.Pop(&h).(int64)
        if serv > start {
            serv = serv + length
        } else {
            serv = start + length
        }
        Print64Ints(serv)
        writer.WriteByte('\n')
        heap.Push(&h, serv)
    }
}

package main

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

func main() {
	var (
		n, k int
		s, l int64
	)

	if _, err := fmt.Fscanf(os.Stdin, "%d %d\n", &n, &k); err != nil {
		log.Fatal(err)
	}

	var (
		pq  = IntHeap{}
		in  = bufio.NewScanner(os.Stdin)
		out = bufio.NewWriter(os.Stdout)
	)

	heap.Init(&pq)

	for i := 0; i < n; i++ {
		in.Scan()
		if err := in.Err(); err != nil {
			log.Fatal("reading standard input:", err)
		}
		if _, err := fmt.Sscanf(in.Text(), "%d %d\n", &s, &l); err != nil {
			log.Fatal(err)
		}
		if pq.Len() == k {
			if pq[0] > s {
				s = pq[0]
			}
			pq[0] = s + l
			heap.Fix(&pq, 0)
		} else {
			heap.Push(&pq, s+l)
		}
		fmt.Fprintln(out, s+l)
	}

	out.Flush()
}

// An IntHeap is a min-heap of ints.
type IntHeap []int64

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

func (h *IntHeap) Push(x interface{}) {
	// Push and Pop use pointer receivers because they modify the slice's length,
	// not just its contents.
	*h = append(*h, x.(int64))
}

func (h *IntHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

package main

import (
    "container/heap"

    "bufio"
    "os"
    "strconv"
)

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

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

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

type IntHeap []int

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

func (h *IntHeap) Push(x interface{}) {
    *h = append(*h, x.(int))
}

func (h *IntHeap) Pop() interface{} {
    old := *h
    n := len(old)
    x := old[n-1]
    *h = old[0 : n-1]
    return x
}

func main() {
    defer writer.Flush()
    scanner.Split(bufio.ScanWords)
    videos, servers := ReadInt32(), ReadInt32()

    h := make(IntHeap, servers)
    heap.Init(&h)
    //heap.Push(&h, 3)
    //PrintInts((h)[0])

    for videos > 0 {
        videos--
        start, length := ReadInt32(), ReadInt32()
        serv := heap.Pop(&h).(int)
        if serv > start {
            serv = serv + length
        } else {
            serv = start + length
        }
        PrintInts(serv)
        writer.WriteByte('\n')
        heap.Push(&h, serv)
    }
}

package main

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

func main() {
	var (
		n, k int
		s, l int
	)

	if _, err := fmt.Fscanf(os.Stdin, "%d %d\n", &n, &k); err != nil {
		log.Fatal(err)
	}

	var (
		pq  = &IntHeap{}
		out = bufio.NewWriter(os.Stdout)
	)

	heap.Init(pq)

	for i := 0; i < n; i++ {
		if _, err := fmt.Fscanf(os.Stdin, "%d %d\n", &s, &l); err != nil {
			log.Fatal(err)
		}
		if pq.Len() == k {
			if ns := heap.Pop(pq).(int); ns > s {
				s = ns
			}
		}
		heap.Push(pq, s+l)
		fmt.Fprintln(out, s+l)
	}

	out.Flush()
}

// An IntHeap is a min-heap of ints.
type IntHeap []int

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

func (h *IntHeap) Push(x interface{}) {
	// Push and Pop use pointer receivers because they modify the slice's length,
	// not just its contents.
	*h = append(*h, x.(int))
}

func (h *IntHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}


Imagine that there is a group of three friends: A, B and С. A owes B 20 rubles and B owes C 20 rubles. The total sum of the debts is 40 rubles. You can see that the debts are not organized in a very optimal manner. Let's rearrange them like that: assume that A owes C 20 rubles and B doesn't owe anything to anybody. The debts still mean the same but the total sum of the debts now equals 20 rubles.This task is a generalisation of a described example. Imagine that your group of friends has n people and you know the debts between the people. Optimize the given debts without changing their meaning. In other words, finally for each friend the difference between the total money he should give and the total money he should take must be the same. Print the minimum sum of all debts in the optimal rearrangement of the debts. See the notes to the test samples to better understand the problem.

The first line contains two integers n and m (1 ≤ n ≤ 100; 0 ≤ m ≤ 104). The next m lines contain the debts. The i-th line contains three integers ai, bi, ci (1 ≤ ai, bi ≤ n; ai ≠ bi; 1 ≤ ci ≤ 100), which mean that person ai owes person bi ci rubles. Assume that the people are numbered by integers from 1 to n. It is guaranteed that the same pair of people occurs at most once in the input. The input doesn't simultaneously contain pair of people (x, y) and pair of people (y, x).

Print a single integer — the minimum sum of debts in the optimal rearrangement.

NoteIn the first sample, you can assume that person number 1 owes 8 rubles to person number 2, 1 ruble to person number 3 and 1 ruble to person number 4. He doesn't owe anybody else anything. In the end, the total debt equals 10.In the second sample, there are no debts.In the third sample, you can annul all the debts.

package main

import "os"
import "bufio"
import "strconv"

func main(){
	var n, m int
	var a, b, c int
	var arr [200][2]int
	r := bufio.NewScanner(os.Stdin)
	r.Split(bufio.ScanWords)
	w := bufio.NewWriter(os.Stdout)
	r.Scan()
	n, _ = strconv.Atoi(r.Text())
	r.Scan()
	m, _ = strconv.Atoi(r.Text())
	min := 0
	for i:=0; i<m; i++ {
		r.Scan()
		a, _ = strconv.Atoi(r.Text())
		r.Scan()
		b, _ = strconv.Atoi(r.Text())
		r.Scan()
		c, _ = strconv.Atoi(r.Text())
		arr[a][0] += c
		arr[b][1] += c
	}
	for i:=1; i<=n; i++ {
		if arr[i][1] - arr[i][0] >= 0 {
			min += arr[i][1]-arr[i][0]
		} else {
			min += 0
		}
	}
	w.WriteString(strconv.Itoa(min)+"\n")
	w.Flush()
}

package main
import (
	"fmt"
	"bufio"
	"os"
	"strconv"
	//"sort"
)

func main() {
	var x,y,z,n,m,ans int
	var a [1100]int
	input := bufio.NewScanner(os.Stdin)
	input.Split(bufio.ScanWords)
	fmt.Scan(&n,&m)
	for i:=0;i<m;i++ {
		input.Scan()
		aa,_:=strconv.Atoi(input.Text())
		x=aa
		input.Scan()
		bb,_:=strconv.Atoi(input.Text())
		y=bb
		input.Scan()
		cc,_:=strconv.Atoi(input.Text())
		z=cc
		a[x]+=z
		a[y]-=z
	}
	for i:=1;i<=n;i++ {
		if a[i]>0 {
			ans+=a[i]
		}
	}
	fmt.Println(ans)
}

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanWords)

    ScanInts := func(a... *int) {
        for i := 0; i < len(a); i++ {
            scanner.Scan()
            *(a[i]), _ = strconv.Atoi(scanner.Text())
        }
    }

    var n,m int
    ScanInts(&n,&m)
    ans, d, a, b, c := 0, make([]int,n+1), make([]int,m), make([]int,m), make([]int,m)
    for i := 0; i < m; i++ {
        ScanInts(&a[i],&b[i],&c[i])
        d[a[i]] += c[i]
        d[b[i]] -= c[i]
    }
    for i := 1; i <= n; i++ {
        if d[i] > 0 {
            ans += d[i]
        }
    }
    fmt.Println(ans)
}

// 376B
package main

import (
	"fmt"
)

func main() {
	var arr [10100]int
	var a, b, c, d, e int
	ans := 0
	fmt.Scan(&a, &b)
	for i := 0; i < b; i++ {
		fmt.Scan(&c, &d, &e)
		arr[c] -= e
		arr[d] += e
	}
	for i := 0; i <= a; i++ {
		if arr[i] > 0 {
			ans += arr[i]
		}
	}
	fmt.Print(ans)
}

// 376B-mic
package main

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

func main() {
	z := bufio.NewReader(os.Stdin)
	var n, m, i, a, b, c, sum int
	var p [105]int
	fmt.Fscan(z, &n, &m)
	for i = 1; i <= m; i++ {
		fmt.Fscan(z, &a, &b, &c)
		p[a] += c
		p[b] -= c
	}

	for i = 1; i <= n; i++ {
		if p[i] > 0 {
			sum += p[i]
		}
	}
	fmt.Println(sum)
}


You're given an array $$$a$$$ of $$$n$$$ integers, such that $$$a_1 + a_2 + \cdots + a_n = 0$$$.In one operation, you can choose two different indices $$$i$$$ and $$$j$$$ ($$$1 \le i, j \le n$$$), decrement $$$a_i$$$ by one and increment $$$a_j$$$ by one. If $$$i < j$$$ this operation is free, otherwise it costs one coin.How many coins do you have to spend in order to make all elements equal to $$$0$$$?

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 5000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$)  — the number of elements. The next line contains $$$n$$$ integers $$$a_1, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$). It is given that $$$\sum_{i=1}^n a_i = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case, print the minimum number of coins we have to spend in order to make all elements equal to $$$0$$$.

NotePossible strategy for the first test case:   Do $$$(i=2, j=3)$$$ three times (free), $$$a = [-3, 2, 0, 1]$$$.  Do $$$(i=2, j=1)$$$ two times (pay two coins), $$$a = [-1, 0, 0, 1]$$$.  Do $$$(i=4, j=1)$$$ one time (pay one coin), $$$a = [0, 0, 0, 0]$$$.

package main

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

const N = 1e5+5

var a [N]int64

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

var scanner *bufio.Scanner
func init() {
    scanner = bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]byte, 1 << 20), 3 << 20)
	scanner.Split(bufio.ScanWords)
}

func read(p interface{}) {
    scanner.Scan()
    fmt.Sscan(scanner.Text(), p)
}

func main() {
    var t int
    for read(&t); t > 0; t-- {
        var n int
        read(&n)
        for i := 0; i < n; i++ {
            read(&a[i])
        }
        // fmt.Println("case #", t, a[:n])
        var result int64
        var more int64
        for i := 0; i < n; i++ {
            if a[i] < 0 {
                delta := min(-a[i], more)
                a[i] += delta
                more -= delta
                result += -a[i]
            } else {
                more += a[i]
                a[i] = 0
            }
        }
        fmt.Println(result)
    }
}

package main

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

const N = 1e5+5

var a [N]int64

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
	buffer := make([]byte, 1024*1024)
	scanner.Buffer(buffer, 3*1024*1024)
	scanner.Split(bufio.ScanWords)

	in := func() int64 {
		scanner.Scan()
		i, _ := strconv.ParseInt(scanner.Text(), 10, 64)
		return i
    }
    for t := in(); t > 0; t-- {
        n := in()
        for i := 0; i < int(n); i++ {
            a[i] = in()
        }
        // fmt.Println("case #", ca, a[:n])
        var result int64
        var more int64
        for i := 0; i < int(n); i++ {
            if a[i] < 0 {
                delta := min(-a[i], more)
                a[i] += delta
                more -= delta
                result += -a[i]
            } else {
                more += a[i]
                a[i] = 0
            }
        }
        fmt.Println(result)
    }
}

package main

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

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

func max(a, b int) int {
	if a == min(a, b) {
		return b
	}
	return a
}
func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

var out chan string
var in *bufio.Scanner
var outWg *sync.WaitGroup

func GetNum(p string) int64 {
	var ans int64 = 0
	flag := true
	i := 0
	n := len(p)
	if p[i] == '-' {
		flag = false
		i++
	}
	for ; i < n; i++ {
		ans = (ans << 1) + (ans << 3) + int64(p[i]-'0')
	}
	if !flag {
		ans = -ans
	}
	return ans
}
func ReadString() string {
	in.Scan()
	return in.Text()
}
func ReadStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ReadString()
	}
	return s
}
func ReadInt() int {
	intStr := ReadString()
	return int(GetNum(intStr))
}
func ReadIntSlice(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = ReadInt()
	}
	return arr
}
func ReadInt64() int64 {
	int64Str := ReadString()
	return GetNum(int64Str)
}
func ReadInt64Slice(n int) []int64 {
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		arr[i] = ReadInt64()
	}
	return arr
}

func init() {

	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1<<10), int(2e+5))
	in.Split(bufio.ScanWords)

	out = make(chan string, 1<<4)
	outWg = &sync.WaitGroup{}
	outWg.Add(1)
	writer := bufio.NewWriterSize(os.Stdout, int(2e+5))
	go func(write *bufio.Writer) {
		defer outWg.Done()
		defer write.Flush()
		for line := range out {
			write.WriteString(line + "\n")
		}
	}(writer)
}

func work() {
	n := ReadInt()
	a := ReadIntSlice(n)
	var delt,ans int64 = 0,0
	for i := 0; i < n; i++ {
		delt += int64(a[i])
		if a[i] < 0 && delt < 0 {
			ans -= delt
			delt = 0
		}
	}
	fmt.Println(ans)
}

func main() {
	for t := ReadInt(); t > 0; t-- {
		work()
	}
	close(out)
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	buffer := make([]byte, 1024*1024)
	scanner.Buffer(buffer, 3*1024*1024)
	scanner.Split(bufio.ScanWords)

	in := func() int64 {
		scanner.Scan()
		i, _ := strconv.ParseInt(scanner.Text(), 10, 64)
		return i
	}

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

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

		for i := range a {
			if a[i] > 0 && i != int(n-1) {
				a[i+1] += a[i]
				a[i] = 0
			}
		}

		var s int64 = 0

		for i := range a {
			if a[i] > 0 {
				s += a[i]
			}
		}

		fmt.Println(s)
	}
}

package main

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

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

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

	var sum, x int64
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n)
		sum = 0
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &x)
			sum += x
			if sum < 0 {
				sum = 0
			}
		}

		fmt.Fprintln(out, sum)
	}
	defer out.Flush()
}

package main

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

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

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

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

	var sum, x int64
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n)
		sum = 0
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &x)
			sum += x
			if sum < 0 {
				sum = 0
			}
		}

		fmt.Fprintln(out, sum)
	}
	defer out.Flush()
}

// dsfdsf

package main

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

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

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

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

	arr := [100000]int64{}
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n)
		var sum, now int64
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &arr[j])
		}

		for j := n - 1; j >= 0; j-- {
			now += arr[j]
			sum = max(sum, now)
		}

		fmt.Fprintln(out, sum)
		defer out.Flush()
	}
}

package main

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

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

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

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

	arr := [100000]int64{}
	var sum, now int64
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n)
		sum, now = 0, 0
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &arr[j])
		}

		for j := n - 1; j >= 0; j-- {
			now += arr[j]
			sum = max(sum, now)
		}

		fmt.Fprintln(out, sum)
	}
	defer out.Flush()
}

// dsfdsf

package main

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

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

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

	var sum, x int64
	for t > 0 {
		fmt.Fscan(in, &n)
		sum = 0
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &x)
			sum += x
			if sum < 0 {
				sum = 0
			}
		}

		fmt.Fprintln(out, sum)
		defer out.Flush()
		t--
	}

} //

package main

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

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

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

	var sum, x int64
	for t > 0 {
		fmt.Fscan(in, &n)
		sum = 0
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &x)
			sum += x
			if sum < 0 {
				sum = 0
			}
		}

		fmt.Fprintln(out, sum)
		t--
	}
	defer out.Flush()
}

package main

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

/*
-3 5 -3 1
*/

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

	var T, N, S int64
	for Fscan(in, &T); T > 0; T-- {
		var result []int64
		for Fscan(in, &N); N > 0; N-- {
			Fscan(in, &S)
			result = append(result, S)
		}

		var total int64
		for i := len(result) - 1; i >= 0; i-- {
			if result[i] < 0 {
				total -= result[i]
			} else if result[i] > 0 {
				if total >= result[i] {
					total -= result[i]
				} else {
					total = 0
				}
			}
		}

		Fprintln(out, total)
	}
}

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

	var T, N, S int64
	for Fscan(in, &T); T > 0; T-- {
		var result []int64
		for Fscan(in, &N); N > 0; N-- {
			Fscan(in, &S)
			result = append(result, S)
		}

		var sum int64
		for i := 0; i < len(result); i++ {
			if result[i] > 0 {
				for j := i + 1; j < len(result); j++ {
					switch {
					case result[j] < 0:
						if result[i]+result[j] >= 0 {
							result[i] += result[j]
							result[j] = 0
						} else {
							result[j] += result[i]
							result[i] = 0
						}
					}

					if result[i] == 0 {
						break
					}
				}
				sum += result[i]
			}
		}

		Fprintln(out, sum)
	}
}

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

package main

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

func main() {
	var scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	scanInt := func(n *int) {
		scanner.Scan()
		num, _ := strconv.ParseInt(scanner.Text(), 10, 32)
		*n = int(num)
	}
	scanInt64 := func(n *int64) {
		scanner.Scan()
		num, _ := strconv.ParseInt(scanner.Text(), 10, 64)
		*n = num
	}

	var t, n int
	var a int64
	scanInt(&t) // number of cases

	for x := 0; x < t; x++ {
		scanInt(&n)
		arrA := make([]int64, n)

		for y := 0; y < n; y++ {
			scanInt64(&a)
			arrA[y] = a
		}

		j := n - 1
		i := n - 1
		// free
		for ; i >= 0 && j >= 0; {
			if arrA[i] <= 0 && arrA[j] >= 0 {
				i--
				j--
				continue
			}

			if arrA[i] <= 0{
				i--
				continue
			}

			if arrA[j] >= 0 {
				j--
				continue
			}

			if i >= j {
				i--
				continue
			}

			if arrA[i] + arrA[j] >= 0 {
				arrA[i] += arrA[j]
				arrA[j] = 0
			} else {
				arrA[j] += arrA[i]
				arrA[i] = 0
			}
		}

		m := int64(0)
		j = n - 1
		i = n - 1
		// paid
		for ; i >= 0 && j >= 0; {
			if arrA[i] <= 0 && arrA[j] >= 0 {
				i--
				j--
				continue
			}
			if arrA[i] <= 0{
				i--
				continue
			}

			if arrA[j] >= 0 {
				j--
				continue
			}

			if arrA[i] + arrA[j] >= 0 {
				m += int64(math.Abs(float64(arrA[j])))
				arrA[i] += arrA[j]
				arrA[j] = 0
			} else {
				m += int64(math.Abs(float64(arrA[i])))
				arrA[j] += arrA[i]
				arrA[i] = 0
			}
		}

		fmt.Println(m)
	}
}

package main

import "fmt"

const N = 1e5+5

var a [N]int64

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

func main() {
    var t int
    fmt.Scanln(&t)
    for ca := 0; ca < t; ca++ {
        var n int
        fmt.Scanln(&n)
        for i := 0; i < n; i++ {
            fmt.Scan(&a[i])
        }
        fmt.Scanln()
        var result int64
        var more int64
        for i := 0; i < n; i++ {
            if a[i] < 0 {
                delta := min(-a[i], more)
                a[i] += delta
                delta -= delta
                result += -a[i]
            } else {
                more += a[i]
                a[i] = 0
            }
        }
        fmt.Println(result)
    }
}

package main

import "fmt"

const N = 1e5+5

var a [N]int64

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

func main() {
    var t int
    fmt.Scanln(&t)
    for ca := 0; ca < t; ca++ {
        var n int
        fmt.Scanln(&n)
        for i := 0; i < n; i++ {
            fmt.Scanf("%d%c", &a[i])
        }
        // fmt.Println("case #", ca, a[:n])
        var result int64
        var more int64
        for i := 0; i < n; i++ {
            if a[i] < 0 {
                delta := min(-a[i], more)
                a[i] += delta
                more -= delta
                result += -a[i]
            } else {
                more += a[i]
                a[i] = 0
            }
        }
        fmt.Println(result)
    }
}

package main

import "fmt"

const N = 1e5+5

var a [N]int64

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

func main() {
    var t int
    fmt.Scanln(&t)
    for ca := 0; ca < t; ca++ {
        var n int
        fmt.Scanln(&n)
        for i := 0; i < n; i++ {
            fmt.Scan(&a[i])
        }
        var result int64
        var more int64
        for i := 0; i < n; i++ {
            if a[i] < 0 {
                delta := min(-a[i], more)
                a[i] += delta
                delta -= delta
                result += -a[i]
            } else {
                more += a[i]
                a[i] = 0
            }
        }
        fmt.Println(result)
    }
}

package main

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

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

func max(a, b int) int {
	if a == min(a, b) {
		return b
	}
	return a
}
func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

var out chan string
var in *bufio.Scanner
var outWg *sync.WaitGroup

func GetNum(p string) int64 {
	var ans int64 = 0
	flag := true
	i := 0
	n := len(p)
	if p[i] == '-' {
		flag = false
		i++
	}
	for ; i < n; i++ {
		ans = (ans << 1) + (ans << 3) + int64(p[i]-'0')
	}
	if !flag {
		ans = -ans
	}
	return ans
}
func ReadString() string {
	in.Scan()
	return in.Text()
}
func ReadStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ReadString()
	}
	return s
}
func ReadInt() int {
	intStr := ReadString()
	return int(GetNum(intStr))
}
func ReadIntSlice(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = ReadInt()
	}
	return arr
}
func ReadInt64() int64 {
	int64Str := ReadString()
	return GetNum(int64Str)
}
func ReadInt64Slice(n int) []int64 {
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		arr[i] = ReadInt64()
	}
	return arr
}

func init() {

	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1<<10), int(2e+5))
	in.Split(bufio.ScanWords)

	out = make(chan string, 1<<4)
	outWg = &sync.WaitGroup{}
	outWg.Add(1)
	writer := bufio.NewWriterSize(os.Stdout, int(2e+5))
	go func(write *bufio.Writer) {
		defer outWg.Done()
		defer write.Flush()
		for line := range out {
			write.WriteString(line + "\n")
		}
	}(writer)
}

func work() {
	n := ReadInt()
	a := ReadIntSlice(n)
	delt := 0
	ans := 0
	for i := 0; i < n; i++ {
		delt += a[i]
		if a[i] < 0 && delt < 0 {
			ans -= delt
			delt = 0
		}
	}
	fmt.Println(ans)
}

func main() {
	for t := ReadInt(); t > 0; t-- {
		work()
	}
	close(out)
}

package main

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

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

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

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

	arr := [100000]int{}
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n)
		sum, now := 0, 0
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &arr[j])
		}

		for j := n - 1; j >= 0; j-- {
			now += arr[j]
			sum = max(sum, now)
		}

		fmt.Fprint(out, sum)
		defer out.Flush()
	}
}

package main

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

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

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

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

	arr := [100000]int{}
	sum, now := 0, 0
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n)
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &arr[j])
		}

		for j := n - 1; j >= 0; j-- {
			now += arr[j]
			sum = max(sum, now)
		}
	}

	fmt.Fprint(out, sum)
	defer out.Flush()
}

package main

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

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

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

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

	arr := [100000]int{}
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n)
		sum, now := 0, 0
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &arr[j])
		}

		for j := n - 1; j >= 0; j-- {
			now += arr[j]
			sum = max(sum, now)
		}

		fmt.Fprintln(out, sum)
		defer out.Flush()
	}
}

package main

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

/*
-3 5 -3 1
*/

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

	var T, N, S int64
	for Fscan(in, &T); T > 0; T-- {
		var result []int64
		for Fscan(in, &N); N > 0; N-- {
			Fscan(in, &S)
			result = append(result, S)
		}

		for i := 0; i < len(result); i++ {
			if result[i] > 0 {
				for j := i + 1; j < len(result); j++ {
					switch {
					case result[j] < 0:
						if result[i]+result[j] >= 0 {
							result[i] += result[j]
							result[j] = 0
						} else {
							result[i] = 0
							result[j] += result[i]
						}
					}

					if result[i] == 0 {
						break
					}
				}
			}
		}

		var sum int64
		for i := 0; i < len(result); i++ {
			if result[i] > 0 {
				sum += result[i]
			}
		}

		Fprintln(out, sum)
	}
}

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

package main

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

/*
-3 5 -3 1
*/

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

	var T, N, S int
	for Fscan(in, &T); T > 0; T-- {
		var result []int
		for Fscan(in, &N); N > 0; N-- {
			Fscan(in, &S)
			result = append(result, S)
		}

		for i := 0; i < len(result); i++ {
			if result[i] > 0 {
				for j := i + 1; j < len(result); j++ {
					switch {
					case result[j] < 0:
						if result[i]+result[j] >= 0 {
							result[i] += result[j]
							result[j] = 0
						} else {
							result[i] = 0
							result[j] += result[i]
						}
					}

					if result[i] == 0 {
						break
					}
				}
			}
		}

		sum := 0
		for i := 0; i < len(result); i++ {
			if result[i] > 0 {
				sum += result[i]
			}
		}

		Fprintln(out, sum)
	}
}

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