/*
URL:
https://codeforces.com/contest/1443/problem/C
*/

package main

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

var (
	t int

	n int
	A []int64
	B []int64
)

func main() {
	defer stdout.Flush()

	t = readi()
	for tc := 0; tc < t; tc++ {
		n = readi()
		A, B = readlls(n), readlls(n)

		solve()
	}
}

func solve() {
	S := []Store{}
	for i := 0; i < n; i++ {
		a, b := A[i], B[i]
		S = append(S, Store{a: a, b: b})
	}

	sort.Slice(S, func(i, j int) bool {
		if S[i].a < S[j].a {
			return true
		} else if S[i].a > S[j].a {
			return false
		} else {
			return S[i].b > S[j].b
		}
	})

	revB := []int64{}
	for i := n - 1; i >= 0; i-- {
		revB = append(revB, S[i].b)
	}

	bs := make([]int64, n+1)
	for i := 0; i < n; i++ {
		bs[i+1] = bs[i] + revB[i]
	}

	ans := Max(A...)
	ChMin(&ans, bs[n])
	for i := 0; i < n; i++ {
		deli := S[i].a
		walk := bs[n-(i+1)]
		val := Max(deli, walk)

		ChMin(&ans, val)
	}

	printf("%d\n", ans)
}

// ChMin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMin(updatedValue *int64, target int64) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

// Max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Max(integers ...int64) int64 {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

type Store struct {
	a, b int64
}

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

/********** common constants **********/

const (
	MOD = 1000000000 + 7
	// MOD          = 998244353
	ALPH_N  = 26
	INF_I64 = math.MaxInt64
	INF_B60 = 1 << 60
	INF_I32 = math.MaxInt32
	INF_B30 = 1 << 30
	NIL     = -1
	EPS     = 1e-10
)

/********** bufio setting **********/

func init() {
	// bufio.ScanWords <---> bufio.ScanLines
	reads = newReadString(os.Stdin, bufio.ScanWords)
	stdout = bufio.NewWriter(os.Stdout)
}

// mod can calculate a right residual whether value is positive or negative.
func mod(val, m int) int {
	res := val % m
	if res < 0 {
		res += m
	}
	return res
}

// min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func min(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

// chmin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func chmin(updatedValue *int, target int) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

// chmax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func chmax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

// sum returns multiple integers sum.
func sum(integers ...int) int {
	var s int
	s = 0

	for _, i := range integers {
		s += i
	}

	return s
}

// abs is integer version of math.Abs
func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

// pow is integer version of math.Pow
// pow calculate a power by Binary Power (二分累乗法(O(log e))).
func pow(a, e int) int {
	if a < 0 || e < 0 {
		panic(errors.New("[argument error]: PowInt does not accept negative integers"))
	}

	if e == 0 {
		return 1
	}

	if e%2 == 0 {
		halfE := e / 2
		half := pow(a, halfE)
		return half * half
	}

	return a * pow(a, e-1)
}

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := reads()
//i := readi()
//X := readis(n)
//S := readrs()
//a := readf()
//A := readfs(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*********** Input ***********/

var (
	// reads returns a WORD string.
	reads  func() string
	stdout *bufio.Writer
)

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// readi returns an integer.
func readi() int {
	return int(_readInt64())
}
func readi2() (int, int) {
	return int(_readInt64()), int(_readInt64())
}
func readi3() (int, int, int) {
	return int(_readInt64()), int(_readInt64()), int(_readInt64())
}
func readi4() (int, int, int, int) {
	return int(_readInt64()), int(_readInt64()), int(_readInt64()), int(_readInt64())
}

// readll returns as integer as int64.
func readll() int64 {
	return _readInt64()
}
func readll2() (int64, int64) {
	return _readInt64(), _readInt64()
}
func readll3() (int64, int64, int64) {
	return _readInt64(), _readInt64(), _readInt64()
}
func readll4() (int64, int64, int64, int64) {
	return _readInt64(), _readInt64(), _readInt64(), _readInt64()
}

func _readInt64() int64 {
	i, err := strconv.ParseInt(reads(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// readis returns an integer slice that has n integers.
func readis(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = readi()
	}
	return b
}

// readlls returns as int64 slice that has n integers.
func readlls(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = readll()
	}
	return b
}

// readf returns an float64.
func readf() float64 {
	return float64(_readFloat64())
}

func _readFloat64() float64 {
	f, err := strconv.ParseFloat(reads(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func readfs(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = readf()
	}
	return b
}

// readrs returns a rune slice.
func readrs() []rune {
	return []rune(reads())
}

/*********** Output ***********/

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// Printf is function for output strings to buffered os.Stdout.
// You may have to call stdout.Flush() finally.
func printf(format string, a ...interface{}) {
	fmt.Fprintf(stdout, format, a...)
}

/*********** Debugging ***********/

// debugf is wrapper of fmt.Fprintf(os.Stderr, format, a...)
func debugf(format string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, format, a...)
}

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

package main

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

type pair struct {
	a int
	b int
}

func main() {
	//rd, _ := os.Open("input")
	//in := bufio.NewReader(rd)
	in := bufio.NewReader(os.Stdin)

	var t, n int
	var p []pair
	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		fmt.Fscan(in, &n)
		p = make([]pair, n)
		for i := range p {
			fmt.Fscan(in, &p[i].a)
		}
		for i := range p {
			fmt.Fscan(in, &p[i].b)
		}
		sort.Slice(p, func(i, j int) bool {
			return p[i].a > p[j].a || p[i].a == p[j].a && p[i].b > p[j].b
		})
		p = append(p, pair{0, 0})
		waitingTime := p[0].a
		walkingTime := 0
		for i := 0; i < len(p)-1; i++ {
			if walkingTime+p[i].b >= p[i].a {
				break
			}
			walkingTime += p[i].b
			waitingTime = p[i+1].a
		}
		fmt.Println(int(math.Max(float64(waitingTime), float64(walkingTime))))
	}
}

package main

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

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

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

type price struct {
	a, b int64
}

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

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

func main() {
	defer writer.Flush()

	var t int
	scanf("%d\n", &t)
	for {
		if t == 0 {
			break
		}
		t--

		var n int
		scanf("%d\n", &n)

		var c = make([]price, n)

		for i := 0; i < n; i++ {
			var a int64
			scanf("%d", &a)
			c[i].a = a
		}
		scanf("\n")

		for i := 0; i < n; i++ {
			var b int64
			scanf("%d", &b)
			c[i].b = b
		}
		scanf("\n")

		sort.SliceStable(c, func(i, j int) bool {
			return c[i].a <= c[j].a
		})

		var s = make([]int64, len(c))

		for i := 0; i < n; i++ {
			if i == 0 {
				s[0] = c[0].b
				continue
			}
			s[i] = s[i-1] + c[i].b
		}

		var res = min(s[len(s)-1], c[len(c)-1].a)

		for i := 1; i < n; i++ {
			res = min(max(c[i-1].a, s[len(s)-1]-s[i-1]), res)
		}

		printf("%d\n", res)
	}
}

package main

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

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

func solveSingle() {
	n := readInt()
	a := readIntArray(n)
	b := readIntArray(n)
	lo := 0
	hi := 1_000_000_001
	ans := hi
	for lo <= hi {
		mi := (lo + hi) / 2
		if ok(a, b, mi) {
			ans = min(ans, mi)
			hi = mi - 1
		} else {
			lo = mi + 1
		}
	}
	fmt.Println(ans)
}

func ok(a []int, b []int, t int) bool {
	cost := 0
	for i := 0; i < len(a); i++ {
		if a[i] > t {
			cost += b[i]
			if cost > t {
				return false
			}
		}
	}
	return cost <= t
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

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

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

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

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

package main

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

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

func solveSingle() {
	n := readInt()
	a := readInt64Array(n)
	b := readInt64Array(n)
	id := make([]int, n)
	for i := 0; i < n; i++ {
		id[i] = i
	}
	sort.Slice(id, func(i, j int) bool {
		return a[id[i]] < a[id[j]]
	})
	sum := make([]int64, n + 2)
	sum[n] = 0
	for i := n-1; i >= 0; i-- {
		sum[i] = sum[i+1]+b[id[i]]
	}
	ans := sum[0]
	for i := 0; i < n; i++ {
		ans = min(ans, max(sum[i+1], a[id[i]]))
	}
	fmt.Println(ans)
}

//func sol2(ans int, a []int, b []int) {
//	lo := 0
//	hi := 1_000_000_001
//	ans = hi
//	for lo <= hi {
//		mi := (lo + hi) / 2
//		if ok(a, b, mi) {
//			ans = min(ans, mi)
//			hi = mi - 1
//		} else {
//			lo = mi + 1
//		}
//	}
//	fmt.Println(ans)
//}
//
//func ok(a []int, b []int, t int) bool {
//	cost := 0
//	for i := 0; i < len(a); i++ {
//		if a[i] > t {
//			cost += b[i]
//			if cost > t {
//				return false
//			}
//		}
//	}
//	return cost <= t
//}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

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

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

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

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

package main

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

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

func solveSingle() {
	n := readInt()
	a := readIntArray(n)
	b := readIntArray(n)
	id := make([]int, n)
	for i := 0; i < n; i++ {
		id[i] = i
	}
	sort.Slice(id, func(i, j int) bool {
		return a[id[i]] < a[id[j]]
	})
	sum := make([]int, n + 2)
	sum[n] = 0
	for i := n-1; i >= 0; i-- {
		sum[i] = sum[i+1]+b[id[i]]
	}
	ans := sum[0]
	for i := 0; i < n; i++ {
		ans = min(ans, max(sum[i+1], a[id[i]]))
	}
	fmt.Println(ans)
}

func sol2(ans int, a []int, b []int) {
	lo := 0
	hi := 1_000_000_001
	ans = hi
	for lo <= hi {
		mi := (lo + hi) / 2
		if ok(a, b, mi) {
			ans = min(ans, mi)
			hi = mi - 1
		} else {
			lo = mi + 1
		}
	}
	fmt.Println(ans)
}

func ok(a []int, b []int, t int) bool {
	cost := 0
	for i := 0; i < len(a); i++ {
		if a[i] > t {
			cost += b[i]
			if cost > t {
				return false
			}
		}
	}
	return cost <= t
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

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

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

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

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

package main

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

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

func solveSingle() {
	n := readInt()
	a := readInt64Array(n)
	b := readInt64Array(n)
	costY0 := b[0]
	costY1 := int64(0)
	costD0 := int64(0)
	costD1 := a[0]
	for i := 1; i < n; i++ {
		var newCostY0 int64
		var newCostY1 int64
		if max(b[i]+costY0, costY1) < max(b[i]+costD0, costD1) {
			newCostY0 = b[i] + costY0
			newCostY1 = costY1
		} else {
			newCostY0 = b[i] + costD0
			newCostY1 = costD1
		}
		var newCostD0 int64
		var newCostD1 int64
		if max(costY0, max(a[i], costY0)) < max(costD0, max(a[i],costD1)) {
			newCostD0 = costY0
			newCostD1 = max(a[i], costY1)
		} else {
			newCostD0 = costD0
			newCostD1 = max(a[i], costD1)
		}
		costY0 = newCostY0
		costY1 = newCostY1
		costD0 = newCostD0
		costD1 = newCostD1
	}
	fmt.Println(min(max(costY0, costY1), max(costD0, costD1)))
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

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

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

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

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

package main

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

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

func solveSingle() {
	n := readInt()
	a := readInt64Array(n)
	b := readInt64Array(n)
	ab := make([][2]int64, n)
	for i := 0; i < n; i++ {
		ab[i][0] = a[i]
		ab[i][1] = b[i]
	}
	sort.Slice(ab, func(i, j int) bool {
		return ab[i][0] < ab[j][0]
	})
	bestAns := int64(math.MaxInt64)
	sort.Search(len(ab), func(i int) bool {
		ans := int64(math.MaxInt64)
		v := ab[i][0]
		cost := int64(0)
		cost1Max := int64(0)
		for j := 0; j < n; j++ {
			if ab[j][0] > v {
				cost += ab[j][1]
			} else {
				cost1Max = max(cost1Max, ab[j][0])
			}
		}
		ans = min(ans, max(cost, cost1Max))

		cost2 := int64(0)
		cost2Max := int64(0)
		for j := 0; j < n; j++ {
			if ab[j][0] >= v {
				cost2 += ab[j][1]
			} else {
				cost2Max = max(cost2Max, ab[j][0])
			}
		}
		ans = min(ans, max(cost2, cost2Max))

		//fmt.Println(v, ans)
		cont := ans >= bestAns
		bestAns = min(bestAns, ans)
		//fmt.Println("i", i, ans, bestAns, cont)
		return cont
	})
	fmt.Println(bestAns)
}

func solveSingleBak() {
	n := readInt()
	a := readInt64Array(n)
	b := readInt64Array(n)
	costY0 := b[0]
	costY1 := int64(0)
	costD0 := int64(0)
	costD1 := a[0]
	for i := 1; i < n; i++ {
		var newCostY0 int64
		var newCostY1 int64
		if max(b[i]+costY0, costY1) < max(b[i]+costD0, costD1) {
			newCostY0 = b[i] + costY0
			newCostY1 = costY1
		} else {
			newCostY0 = b[i] + costD0
			newCostY1 = costD1
		}
		var newCostD0 int64
		var newCostD1 int64
		if max(costY0, max(a[i], costY0)) < max(costD0, max(a[i], costD1)) {
			newCostD0 = costY0
			newCostD1 = max(a[i], costY1)
		} else {
			newCostD0 = costD0
			newCostD1 = max(a[i], costD1)
		}
		costY0 = newCostY0
		costY1 = newCostY1
		costD0 = newCostD0
		costD1 = newCostD1
	}
	fmt.Println(min(max(costY0, costY1), max(costD0, costD1)))
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

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

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

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

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

package main

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

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

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

type price struct {
	a, b int
}

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

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

func main() {
	defer writer.Flush()

	var t int
	scanf("%d\n", &t)
	for {
		if t == 0 {
			break
		}
		t--

		var n int
		scanf("%d\n", &n)

		var c = make([]price, n)

		for i := 0; i < n; i++ {
			var a int
			scanf("%d", &a)
			c[i].a = a
		}
		scanf("\n")

		for i := 0; i < n; i++ {
			var b int
			scanf("%d", &b)
			c[i].b = b
		}
		scanf("\n")

		sort.SliceStable(c, func(i, j int) bool {
			return c[i].a <= c[j].a
		})

		var s = make([]int, len(c))

		for i := 0; i < n; i++ {
			if i == 0 {
				s[0] = c[0].b
				continue
			}
			s[i] = s[i-1] + c[i].b
		}

		var res = min(s[len(s)-1], c[len(c)-1].a)

		for i := 1; i < n; i++ {
			res = min(max(c[i-1].a, s[len(s)-1]-s[i-1]), res)
		}

		printf("%d\n", res)
	}
}


package main

import (
	"bufio"
	. "fmt"
	"index/suffixarray"
	"os"
	"reflect"
	"unsafe"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	max := func(a, b int) int {
		if a > b {
			return a
		}
		return b
	}
	var s []byte
	Fscan(bufio.NewReader(os.Stdin), &s)
	n := len(s)
	sa := *(*[]int32)(unsafe.Pointer(reflect.ValueOf(suffixarray.New(s)).Elem().FieldByName("sa").Field(0).UnsafeAddr()))
	rank := make([]int, n)
	for i := range rank {
		rank[sa[i]] = i
	}
	height := make([]int, n+1)
	h := 0
	for i, ri := range rank {
		if h > 0 {
			h--
		}
		if ri > 0 {
			for j := int(sa[ri-1]); i+h < n && j+h < n && s[i+h] == s[j+h]; h++ {
			}
		}
		height[ri] = h
	}

	type pair struct{ v, i int }
	pl := make([]int, n+1)
	st := []pair{{-1, 0}}
	for i, v := range height {
		for {
			if top := st[len(st)-1]; top.v < v {
				pl[i] = top.i
				break
			}
			st = st[:len(st)-1]
		}
		st = append(st, pair{v, i})
	}
	pr := make([]int, n+1)
	st = []pair{{-1, n}}
	for i := n - 1; i >= 0; i-- {
		v := height[i]
		for {
			if top := st[len(st)-1]; top.v < v {
				pr[i] = top.i
				break
			}
			st = st[:len(st)-1]
		}
		st = append(st, pair{v, i})
	}

	ans := int64(n) * int64(n+1) / 2
	usedSt := []int{0}
	for i, v := range height {
		for v < usedSt[len(usedSt)-1] {
			usedSt = usedSt[:len(usedSt)-1]
		}
		if v == usedSt[len(usedSt)-1] {
			continue
		}
		usedSt = append(usedSt, v)
		l, r := pl[i], pr[i]
		h, w := int64(v-max(height[l], height[r])), int64(r-l)
		ans += h * w * (w - 1) / 2
	}
	Print(ans)
}

package main

import (
	"bufio"
	. "fmt"
	"index/suffixarray"
	"io"
	"os"
	"reflect"
	"unsafe"
)

// github.com/EndlessCheng/codeforces-go
func CF123D(_r io.Reader, out io.Writer) {
	max := func(a, b int) int {
		if a > b {
			return a
		}
		return b
	}
	var s []byte
	Fscan(bufio.NewReader(_r), &s)
	n := len(s)
	sa := *(*[]int32)(unsafe.Pointer(reflect.ValueOf(suffixarray.New(s)).Elem().FieldByName("sa").Field(0).UnsafeAddr()))
	rank := make([]int, n)
	for i := range rank {
		rank[sa[i]] = i
	}
	height := make([]int, n+1)
	h := 0
	for i, ri := range rank {
		if h > 0 {
			h--
		}
		if ri > 0 {
			for j := int(sa[ri-1]); i+h < n && j+h < n && s[i+h] == s[j+h]; h++ {
			}
		}
		height[ri] = h
	}

	type pair struct{ v, i int }
	pl := make([]int, n+1)
	st := []pair{{-1, 0}}
	for i, v := range height {
		for {
			if top := st[len(st)-1]; top.v < v {
				pl[i] = top.i
				break
			}
			st = st[:len(st)-1]
		}
		st = append(st, pair{v, i})
	}
	pr := make([]int, n+1)
	st = []pair{{-1, n}}
	for i := n - 1; i >= 0; i-- {
		v := height[i]
		for {
			if top := st[len(st)-1]; top.v < v {
				pr[i] = top.i
				break
			}
			st = st[:len(st)-1]
		}
		st = append(st, pair{v, i})
	}

	ans := int64(n) * int64(n+1) / 2
	usedSt := []int{0}
	for i, v := range height {
		for v < usedSt[len(usedSt)-1] {
			usedSt = usedSt[:len(usedSt)-1]
		}
		if v == usedSt[len(usedSt)-1] {
			continue
		}
		usedSt = append(usedSt, v)
		l, r := pl[i], pr[i]
		h, w := int64(v-max(height[l], height[r])), int64(r-l)
		ans += h * w * (w - 1) / 2
	}
	Fprint(out, ans)
}

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


package main

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

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

	var n, m, q, d int
	var x, y string
	Fscan(in, &n, &m, &q)
	fa := make([]int, n)
	dis := make([]int, n)
	for i := range fa {
		fa[i] = i
	}
	var find func(int) int
	find = func(x int) int {
		if fa[x] != x {
			ffx := find(fa[x])
			dis[x] += dis[fa[x]]
			fa[x] = ffx
		}
		return fa[x]
	}
	delta := func(x, y int) int { return ((dis[x]-dis[y])%2 + 2) % 2 }
	merge := func(from, to int, d int) bool {
		if fFrom, fTo := find(from), find(to); fFrom != fTo {
			dis[fFrom] = d + dis[to] - dis[from]
			fa[fFrom] = fTo
			return true
		}
		return delta(from, to) == d
	}
	same := func(x, y int) bool { return find(x) == find(y) }

	id := make(map[string]int, n)
	for ; n > 0; n-- {
		Fscan(in, &x)
		id[x] = len(id)
	}
	for ; m > 0; m-- {
		Fscan(in, &d, &x, &y)
		if merge(id[x], id[y], d-1) {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
	for ; q > 0; q-- {
		Fscan(in, &x, &y)
		if a, b := id[x], id[y]; same(a, b) {
			Fprintln(out, delta(a, b)+1)
		} else {
			Fprintln(out, 3)
		}
	}
}

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

package main

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

var stdReader = bufio.NewReader(os.Stdin)

func readToken() string {
	var char byte
	var err error
	for {
		char, err = stdReader.ReadByte()
		if err == io.EOF {
			return ""
		}
		if err != nil {
			panic(err)
		}
		if !unicode.IsSpace(rune(char)) {
			break
		}
	}
	bytes := []byte{char}
	for {
		char, err := stdReader.ReadByte()
		if err == io.EOF {
			break
		}
		if err != nil {
			panic(err)
		}
		if unicode.IsSpace(rune(char)) {
			break
		}
		bytes = append(bytes, char)
	}
	return string(bytes)
}

func readInt() int {
	i, err := strconv.Atoi(readToken())
	if err != nil {
		panic(err)
	}
	return i
}

var dsu []int

func group(i int) int {
	if dsu[i] == i {
		return i
	}
	dsu[i] = group(dsu[i])
	return dsu[i]
}

var op []int

func opposite(i int) int {
	if op[i] < 0 {
		return -1
	}
	return group(op[i])
}

func relate(ga, gb int) int {
	switch {
	case ga == gb:
		return 1
	case opposite(ga) == gb:
		return 2
	default:
		return 3
	}
}

func unite(ga, gb int) {
	dsu[gb] = ga
}

func main() {
	n, r, q := readInt(), readInt(), readInt()
	en := make(map[string]int)
	for i := 0; i < n; i++ {
		en[readToken()] = i
	}

	dsu = make([]int, n)
	for i := range dsu {
		dsu[i] = i
	}
	op = make([]int, n)
	for i := range op {
		op[i] = -1
	}

	for i := 0; i < r; i++ {
		j := readInt()
		ga, gb := group(en[readToken()]), group(en[readToken()])
		rel := relate(ga, gb)
		if j == 1 {
			if rel == 2 {
				fmt.Println("NO")
			} else {
				if opposite(ga) >= 0 && opposite(gb) >= 0 {
					unite(opposite(ga), opposite(gb))
				} else if opposite(ga) >= 0 {
					op[gb] = opposite(ga)
				} else if opposite(gb) >= 0 {
					op[ga] = opposite(gb)
				}
				unite(ga, gb)
				pos := group(ga)
				if neg := opposite(pos); neg >= 0 {
					op[pos] = neg
					op[neg] = pos
				}
				fmt.Println("YES")
			}
		} else {
			if rel == 1 {
				fmt.Println("NO")
			} else {
				switch {
				case opposite(ga) >= 0 && opposite(gb) >= 0:
					unite(ga, opposite(gb))
					unite(opposite(ga), gb)
				case opposite(ga) >= 0:
					unite(opposite(ga), gb)
				case opposite(gb) >= 0:
					unite(ga, opposite(gb))
				}
				op[group(ga)] = group(gb)
				op[group(gb)] = group(ga)
				fmt.Println("YES")
			}
		}
	}

	for i := 0; i < q; i++ {
		fmt.Println(relate(group(en[readToken()]), group(en[readToken()])))
	}
}


package main

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

var n, m int

func Gcd(x int, y int) int {
	if x == 0 {
		return y
	} else {
		return Gcd(y%x, x)
	}
}

const maxn = 32768

var p [maxn]int
var plen = 0
var bad map[int]bool

func GetPrime() {
	for i := 2; i < maxn; i++ {
		var mk = false
		for j := 0; j < plen && !mk && p[j]*p[j] <= i; j++ {
			if i%p[j] == 0 {
				mk = true
			}
		}
		if !mk {
			p[plen] = i
			plen++
		}
	}
}

func Count(x int) int {
	var ret = 0
	for now := 0; x != 1 && now < plen; now++ {
		if x%p[now] == 0 {
			var cur = 0
			for x%p[now] == 0 {
				cur++
				x /= p[now]
			}
			if bad[p[now]] {
				ret -= cur
			} else {
				ret += cur
			}
		}
	}
	if x != 1 {
		if bad[x] {
			ret--
		} else {
			ret++
		}
	}
	return ret
}

func main() {
	scanner.Split(bufio.ScanWords)
	GetPrime()
	n = scanner.NextInt()
	m = scanner.NextInt()
	a := make([]int, n)
	b := make([]int, n)
	bad = make(map[int]bool)
	for i := 0; i < n; i++ {
		a[i] = scanner.NextInt()
	}
	for i := 0; i < m; i++ {
		bad[scanner.NextInt()] = true
	}
	var ans = 0
	var now = 0
	for i := 0; i < n; i++ {
		now = Gcd(now, a[i])
		ans += Count(a[i])
		b[i] = Count(now)
	}
	var cur = 0
	for i := n - 1; i >= 0; i-- {
		if b[i] < cur {
			ans += (cur - b[i]) * (i + 1)
			cur = b[i]
		}
	}
	fmt.Printf("%d\n", ans)
}

type Scanner struct {
	*bufio.Scanner
}

var scanner = Scanner{bufio.NewScanner(os.Stdin)}

func (self *Scanner) Next() string {
	if !self.Scan() {
		panic(self.Err())
	}
	return self.Text()
}

func (self *Scanner) NextInt() int {
	x, err := strconv.Atoi(self.Next())
	if err != nil {
		panic(err)
	}
	return x
}

package main

import "fmt"

var n, m int

func Gcd(x int, y int) int {
    if x == 0 {
        return y
    } else {
        return Gcd(y%x, x)
    }
}

const maxn = 32768

var p [maxn]int
var plen = 0
var bad map[int]bool

func GetPrime() {
    for i := 2; i < maxn; i++ {
        var mk = false
        for j := 0; j < plen && !mk && p[j]*p[j] <= i; j++ {
            if i%p[j] == 0 {
                mk = true
            }
        }
        if !mk {
            p[plen] = i
            plen++
        }
    }
}

func Count(x int) int {
    var ret = 0
    for now := 0; x != 1 && now < plen; now++ {
        if x%p[now] == 0 {
            var cur = 0
            for x%p[now] == 0 {
                cur++
                x /= p[now]
            }
            if bad[p[now]] {
                ret -= cur
            } else {
                ret += cur
            }
        }
    }
    if x != 1 {
        if bad[x] {
            ret--
        } else {
            ret++
        }
    }
    return ret
}

func main() {
    GetPrime()
    fmt.Scanf("%d %d\n", &n, &m)
    a := make([]int, n)
    b := make([]int, n)
    bad = make(map[int]bool)
    for i := 0; i < n; i++ {
        fmt.Scanf("%d", &a[i])
    }
    var x int
    fmt.Scanf("%d", &x)
    for i := 0; i < m; i++ {
        fmt.Scanf("%d", &x)
        bad[x] = true
    }
    var ans = 0
    var now = 0
    for i := 0; i < n; i++ {
        now = Gcd(now, a[i])
        ans += Count(a[i])
        b[i] = Count(now)
    }
    var cur = 0
    for i := n - 1; i >= 0; i-- {
        if b[i] < cur {
            ans += (cur - b[i]) * (i + 1)
            cur = b[i]
        }
    }
    fmt.Printf("%d\n", ans)
}

package main

import "fmt"

var n, m int

func Gcd(x int, y int) int {
	if x == 0 {
		return y
	} else {
		return Gcd(y%x, x)
	}
}

const maxn = 32768

var p [maxn]int
var plen = 0
var bad map[int]bool

func GetPrime() {
	for i := 2; i < maxn; i++ {
		var mk = false
		for j := 0; j < plen && !mk && p[j]*p[j] <= i; j++ {
			if i%p[j] == 0 {
				mk = true
			}
		}
		if !mk {
			p[plen] = i
			plen++
		}
	}
}

func Count(x int) int {
	var ret = 0
	for now := 0; x != 1 && now < plen; now++ {
		if x%p[now] == 0 {
			var cur = 0
			for x%p[now] == 0 {
				cur++
				x /= p[now]
			}
			if bad[p[now]] {
				ret -= cur
			} else {
				ret += cur
			}
		}
	}
	if x != 1 {
		if bad[x] {
			ret--
		} else {
			ret++
		}
	}
	return ret
}

func main() {
	GetPrime()
	fmt.Scanf("%d %d\n", &n, &m)
	a := make([]int, n)
	b := make([]int, n)
	bad = make(map[int]bool)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
	}
	for i := 0; i < m; i++ {
		var x int
		fmt.Scanf("%d", &x)
		bad[x] = true
	}
	var ans = 0
	var now = 0
	for i := 0; i < n; i++ {
		now = Gcd(now, a[i])
		ans += Count(a[i])
		b[i] = Count(now)
	}
	var cur = 0
	for i := n - 1; i >= 0; i-- {
		if b[i] < cur {
			ans += (cur - b[i]) * (i + 1)
			cur = b[i]
		}
	}
	fmt.Printf("%d\n", ans)
}

package main

import "fmt"

var n, m int

func Gcd(x int, y int) int {
    if x == 0 {
        return y
    } else {
        return Gcd(y%x, x)
    }
}

const maxn = 32768

var p [maxn]int
var plen = 0
var bad map[int]bool

func GetPrime() {
    for i := 2; i < maxn; i++ {
        var mk = false
        for j := 0; j < plen && !mk && p[j]*p[j] <= i; j++ {
            if i%p[j] == 0 {
                mk = true
            }
        }
        if !mk {
            p[plen] = i
            plen++
        }
    }
}

func Count(x int) int {
    var ret = 0
    for now := 0; x != 1 && now < plen; now++ {
        if x%p[now] == 0 {
            var cur = 0
            for x%p[now] == 0 {
                cur++
                x /= p[now]
            }
            if bad[p[now]] {
                ret -= cur
            } else {
                ret += cur
            }
        }
    }
    if x != 1 {
        if bad[x] {
            ret--
        } else {
            ret++
        }
    }
    return ret
}

func main() {
    GetPrime()
    fmt.Scanf("%d %d\n", &n, &m)
    a := make([]int, n)
    b := make([]int, n)
    bad = make(map[int]bool)
    for i := 0; i < n; i++ {
        fmt.Scanf("%d", &a[i])
    }
    var x int
    for i := 0; i < 1; i++ {
        fmt.Scanf("%d", &x)
        bad[x] = true
    }
    var ans = 0
    var now = 0
    for i := 0; i < n; i++ {
        now = Gcd(now, a[i])
        ans += Count(a[i])
        b[i] = Count(now)
    }
    var cur = 0
    for i := n - 1; i >= 0; i-- {
        if b[i] < cur {
            ans += (cur - b[i]) * (i + 1)
            cur = b[i]
        }
    }
    fmt.Printf("%d\n", x)
}

package main

import "fmt"

var n, m int

func Gcd(x int, y int) int {
    if x == 0 {
        return y
    } else {
        return Gcd(y%x, x)
    }
}

const maxn = 32768

var p [maxn]int
var plen = 0
var bad map[int]bool

func GetPrime() {
    for i := 2; i < maxn; i++ {
        var mk = false
        for j := 0; j < plen && !mk && p[j]*p[j] <= i; j++ {
            if i%p[j] == 0 {
                mk = true
            }
        }
        if !mk {
            p[plen] = i
            plen++
        }
    }
}

func Count(x int) int {
    var ret = 0
    for now := 0; x != 1 && now < plen; now++ {
        if x%p[now] == 0 {
            var cur = 0
            for x%p[now] == 0 {
                cur++
                x /= p[now]
            }
            if bad[p[now]] {
                ret -= cur
            } else {
                ret += cur
            }
        }
    }
    if x != 1 {
        if bad[x] {
            ret--
        } else {
            ret++
        }
    }
    return ret
}

func main() {
    GetPrime()
    fmt.Scanf("%d %d\n", &n, &m)
    a := make([]int, n)
    b := make([]int, n)
    bad = make(map[int]bool)
    for i := 0; i < n; i++ {
        fmt.Scanf("%d%c", &a[i])
    }
    for i := 0; i < m; i++ {
        var x int
        fmt.Scanf("%d%c", &x)
        bad[x] = true
    }
    var ans = 0
    var now = 0
    for i := 0; i < n; i++ {
        now = Gcd(now, a[i])
        ans += Count(a[i])
        b[i] = Count(now)
    }
    var cur = 0
    for i := n - 1; i >= 0; i-- {
        if b[i] < cur {
            ans += (cur - b[i]) * (i + 1)
            cur = b[i]
        }
    }
    fmt.Printf("%d\n", ans)
}


package main

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

const cf = true

func solve() {
	var n, d int
	fmt.Fscan(in, &n, &d)
	d++
	powers := make([]int, n)
	for i := range powers {
		fmt.Fscan(in, &powers[i])
	}
	sort.Slice(powers, func(i, j int) bool {
		return powers[j] < powers[i]
	})
	playersLeft := n
	ans := 0
	for i := range powers {
		needed := d/powers[i] + min(1, d%powers[i])
		playersLeft -= needed
		if playersLeft < 0 {
			break
		}
		ans++
	}
	fmt.Fprintln(out, ans)
}

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

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

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

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

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

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

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

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

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

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

var exists = struct{}{}

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

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

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

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

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

type Ordered interface {
	Integer | Float | ~string
}

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

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

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

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

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

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

package main

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

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

	var n, d int
	fmt.Fscan(r, &n, &d)
	p := make([]int, n)

	for i := 0; i < n; i++ {
		fmt.Fscan(r, &p[i])
	}
	sort.Ints(p)

	left := 0
	right := n - 1
	wins := 0

	for right >= 0 {
		// fmt.Fprintf(w, "left: %d, right: %d, d: %d, p[right]: %d, d/p[r]: %d, wins:%d\n", left, right, d, p[right], d/p[right], wins)
		left += d / p[right]
		if right >= left {
			wins++
			right--
		} else {
			break
		}
	}
	fmt.Fprintln(w, wins)
}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	n      int
	d      int64
)

func solve(n int, d int64, p []int64) int {
	var r, prevr, c int
	sort.Slice(p, func(i, j int) bool {
		return p[i] < p[j]
	})
	for i := n - 1; i > -1; i-- {
		r = int(d/p[i] + 1)
		if i < n-1 {
			r += prevr
		}
		prevr = r
		if r <= n {
			c++
		} else {
			break
		}
	}
	return c
}

func main() {
	fmt.Fscan(reader, &n, &d)
	var p = make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &p[i])
	}
	fmt.Println(solve(n, d, p))
}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	n      int
	d      int64
)

func solve(n int, d int64, p []int64) int {
	var r, c = make([]int, n), 0
	sort.Slice(p, func(i, j int) bool {
		return p[i] < p[j]
	})
	for i := n - 1; i > -1; i-- {
		r[i] = int(d/p[i] + 1)
		if i < n-1 {
			r[i] += r[i+1]
		}
		if r[i] <= n {
			c++
		} else {
			break
		}
	}
	return c
}

func main() {
	fmt.Fscan(reader, &n, &d)
	var p = make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &p[i])
	}
	fmt.Println(solve(n, d, p))
}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	n      int
	d      int64
)

func solve(n int, d int64, p []int64) int {
	var r = make([]int, n)
	var c int
	sort.Slice(p, func(i, j int) bool {
		return p[i] < p[j]
	})
	for i := n - 1; i > -1; i-- {
		if i == n-1 {
			r[i] = int((p[i] + d) / p[i])
		} else {
			r[i] = int((p[i]+d)/p[i]) + r[i+1]
		}
		if r[i] <= n {
			c++
		} else {
			break
		}
	}
	return c
}

func main() {
	fmt.Fscan(reader, &n, &d)
	var p = make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &p[i])
	}
	fmt.Println(solve(n, d, p))
}

package main

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

func main() {
	var N, D int
	fmt.Scanln(&N, &D)
	var reader = bufio.NewReader(os.Stdin)
	var p = getIntln(reader)
	sort.Slice(p, func(i, j int) bool {
		return p[i] < p[j]
	})
	var x = sort.Search(N, func(i int) bool {
		return p[i] > D
	})
	var result = N - x
	var first, last = 0, x - 1
	for first < last {
		var x = p[last]
		for x <= D && first < last {
			x += p[last]
			first++
		}
		if x > D {
			result++
		}
		last--
	}
	fmt.Println(result)
}

func getIntln(in *bufio.Reader) []int {
	var x = strings.Split(getString(in), " ")
	var res = make([]int, len(x))
	for k, v := range x {
		res[k], _ = strconv.Atoi(v)
	}
	return res
}

func getString(in *bufio.Reader) string {
	var s, _ = in.ReadString('\n')
	s = strings.TrimSpace(s)
	return s
}

package main

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

// https://codeforces.com/problemset/problem/1725/B
func P1725b(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

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

	var players = make([]int, n)

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

	var res int = 0

	sort.Ints(players)
	for len(players) > 0 {
		var best int = players[len(players)-1]
		var needTotal int = d/best + 1
		if needTotal > len(players) {
			break
		}
		var newPlayers = players[(needTotal - 1):(len(players) - 1)]
		players = newPlayers
		res++
	}
	fmt.Fprintf(out, "%d\n", res)
}

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

package main

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

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

	var n, d int
	fmt.Fscan(r, &n, &d)
	p := make([]int, n)

	for i := 0; i < n; i++ {
		fmt.Fscan(r, &p[i])
	}
	// fmt.Fprintln(w, "******** p ", p)
	sort.Ints(p)
	// fmt.Fprintln(w, "####******** p ", p)

	left := 0
	right := n - 1
	wins := 0

	for {
		// fmt.Fprintf(w, "left: %d, right: %d, d: %d, p[right]: %d, d/p[r]: %d, wins:%d\n", left, right, d, p[right], d/p[right], wins)
		left += d / p[right] + 1
		if right + 1 >= left {
			wins++
			right--
		} else {
			break
		}
	}
	fmt.Fprintln(w, wins)
}

package main

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

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

	var n, d int
	fmt.Fscan(r, &n, &d)
	p := make([]int, n)

	for i := 0; i < n; i++ {
		fmt.Fscan(r, &p[i])
	}
	// fmt.Fprintln(w, "******** p ", p)
	sort.Ints(p)
	// fmt.Fprintln(w, "####******** p ", p)

	left := 0
	right := n - 1
	wins := 0

	for {
		// fmt.Fprintf(w, "left: %d, right: %d, d: %d, p[right]: %d, d/p[r]: %d, wins:%d\n", left, right, d, p[right], d/p[right], wins)
		t := d / p[right]
		if d % p[right] > 0 {
			t++
		}
		left += t
		if right + 1 >= left {
			wins++
			right--
		} else {
			break
		}
	}
	fmt.Fprintln(w, wins)
}

package main

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

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

	var n, d int
	fmt.Fscan(r, &n, &d)
	p := make([]int, n)

	for i := 0; i < n; i++ {
		fmt.Fscan(r, &p[i])
	}
	// fmt.Fprintln(w, "******** p ", p)
	sort.Ints(p)
	// fmt.Fprintln(w, "####******** p ", p)

	left := 0
	right := n - 1
	wins := 0

	for {
		// fmt.Fprintf(w, "left: %d, right: %d, d: %d, p[right]: %d, d/p[r]: %d, wins:%d\n", left, right, d, p[right], d/p[right], wins)
		t := d / p[right]
		if d % p[right] > 0 {
			t++
		}
		left += t
		if right >= left {
			wins++
			right--
		} else {
			break
		}
	}
	fmt.Fprintln(w, wins)
}

package main

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

func main() {
	var N, D int
	fmt.Scanln(&N, &D)
	var reader = bufio.NewReader(os.Stdin)
	var p = getIntln(reader)
	sort.Slice(p, func(i, j int) bool {
		return p[i] < p[j]
	})
	var x = sort.Search(N, func(i int) bool {
		return p[i] > D
	})
	var result = N - x
	var first, last = 0, x - 1
	for first < last {
		var x = p[last]
		for x <= D && first < last {
			x += p[last]
			first++
		}
		result++
		last--
	}
	fmt.Println(result)
}

func getIntln(in *bufio.Reader) []int {
	var x = strings.Split(getString(in), " ")
	var res = make([]int, len(x))
	for k, v := range x {
		res[k], _ = strconv.Atoi(v)
	}
	return res
}

func getString(in *bufio.Reader) string {
	var s, _ = in.ReadString('\n')
	s = strings.TrimSpace(s)
	return s
}

package main

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

// https://codeforces.com/problemset/problem/1725/B
func P1725b(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

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

	var players = make([]int, n)

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

	var res int = 0

	sort.Ints(players)
	for len(players) > 0 {
		var best int = players[len(players)-1]
		var needTotal = int(math.Ceil(float64(d) / float64(best)))
		if needTotal > len(players) {
			break
		}
		var newPlayers = players[(needTotal - 1):(len(players) - 1)]
		players = newPlayers
		res++
	}
	fmt.Fprintf(out, "%d\n", res)
}

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

package main

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

const cf = true

func solve() {
	var n, d int
	fmt.Fscan(in, &n, &d)
	powers := make([]int, n)
	for i := range powers {
		fmt.Fscan(in, &powers[i])
	}
	sort.Slice(powers, func(i, j int) bool {
		return powers[j] < powers[i]
	})
	playersLeft := n
	ans := 0
	for i := range powers {
		needed := d/powers[i] + min(1, d%powers[i])
		playersLeft -= needed
		if playersLeft < 0 {
			break
		}
		ans++
	}
	fmt.Fprintln(out, ans)
}

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

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

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

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

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

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

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

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

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

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

var exists = struct{}{}

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

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

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

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

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

type Ordered interface {
	Integer | Float | ~string
}

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

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

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

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

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

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


// 144B-mic
package main

import (
	"fmt"
	"math"
)

func main() {
	var xa, ya, xb, yb, lx, ly, rx, ry, n, i, j, res int
	var a, b, c []int
	fmt.Scan(&xa, &ya, &xb, &yb)
	lx = int(math.Min(float64(xa), float64(xb)))
	ly = int(math.Min(float64(ya), float64(yb)))
	rx = int(math.Max(float64(xa), float64(xb)))
	ry = int(math.Max(float64(ya), float64(yb)))
	fmt.Scan(&n)
	for i = 0; i < n; i++ {
		var f, s, t int
		fmt.Scan(&f, &s, &t)
		a = append(a, f)
		b = append(b, s)
		c = append(c, t)
	}
	for i = lx; i <= rx; i++ {
		add := 1
		for j = 0; j < n && add == 1; j++ {
			if (a[j]-i)*(a[j]-i)+(b[j]-ly)*(b[j]-ly) <= c[j]*c[j] {
				add = 0
			}
		}
		res += add
	}

	for i = lx; i <= rx; i++ {
		add := 1
		for j = 0; j < n && add == 1; j++ {
			if (a[j]-i)*(a[j]-i)+(b[j]-ry)*(b[j]-ry) <= c[j]*c[j] {
				add = 0
			}
		}
		res += add
	}

	for i = ly + 1; i < ry; i++ {
		add := 1
		for j = 0; j < n && add == 1; j++ {
			if (a[j]-lx)*(a[j]-lx)+(b[j]-i)*(b[j]-i) <= c[j]*c[j] {
				add = 0
			}
		}
		res += add
	}

	for i = ly + 1; i < ry; i++ {
		add := 1
		for j = 0; j < n && add == 1; j++ {
			if (a[j]-rx)*(a[j]-rx)+(b[j]-i)*(b[j]-i) <= c[j]*c[j] {
				add = 0
			}
		}
		res += add
	}
	fmt.Println(res)
}

package main

import (
    "fmt"
    "math"
)

func main() {
    d := func(x1,y1,x2,y2 int) float64 {
        dx, dy := float64(x1-x2), float64(y1-y2)
        return math.Sqrt((dx*dx)+(dy*dy))
    }

    var xa,ya,xb,yb,n int
    fmt.Scan(&xa,&ya,&xb,&yb)
    if xa > xb { xa, xb = xb, xa }
    if ya > yb { ya, yb = yb, ya }
    fmt.Scan(&n)
    x, y, r := make([]int,n), make([]int,n), make([]float64,n)
    for i := 0; i < n; i++ { fmt.Scan(&x[i],&y[i],&r[i]) }
    ans := 0
    for i := xa; i <= xb; i++ {
        a, b := true, true
        for j := 0; j < n; j++ {
            if d(i,ya,x[j],y[j]) <= r[j] { a = false }
            if d(i,yb,x[j],y[j]) <= r[j] { b = false }
        }
        if (a && b) {
            ans += 2
        } else if (a || b) {
            ans += 1
        }
    }
    for i := ya+1; i < yb; i++ {
        a, b := true, true
        for j := 0; j < n; j++ {
            if d(xa,i,x[j],y[j]) <= r[j] { a = false }
            if d(xb,i,x[j],y[j]) <= r[j] { b = false }
        }
        if (a && b) {
            ans += 2
        } else if (a || b) {
            ans += 1
        }
    }
    fmt.Println(ans)
}

package main

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

func main(){
	var a [2]int
	var b [2]int
	re := bufio.NewScanner(os.Stdin)
	re.Split(bufio.ScanWords)
	w := bufio.NewWriter(os.Stdout)
	re.Scan()
	a[0], _ = strconv.Atoi(re.Text())
	re.Scan()
	b[0], _ = strconv.Atoi(re.Text())
	re.Scan()
	a[1], _ = strconv.Atoi(re.Text())
	re.Scan()
	b[1], _ = strconv.Atoi(re.Text())
	var n int
	re.Scan()
	n, _ = strconv.Atoi(re.Text())
	x := make([]int, n)
	y := make([]int, n)
	r := make([]int, n)
	ans := 0
	for i:=0; i<n; i++ {
		re.Scan()
		x[i], _ = strconv.Atoi(re.Text())
		re.Scan()
		y[i], _ = strconv.Atoi(re.Text())
		re.Scan()
		r[i], _ = strconv.Atoi(re.Text())
	}
	if a[0] > a[1] {
		t := a[0]
		a[0] = a[1]
		a[1] = t
	}
	if b[0] > b[1] {
		t := b[0]
		b[0] = b[1]
		b[1] = t
	}
	for i:=a[0]; i<=a[1]; i++ {
		for j:=b[0]; j<=b[1]; j++ {
			if (i == a[0] || i == a[1]) && (j >= b[0] && j <= b[1]) {
				h := 0
				for k:=0; k<n; k++ {
					p, q := 0, 0
					p = i-x[k]
					q = j-y[k]
					if p*p+q*q <= r[k]*r[k] {
						h = 1
					}
				}
				if h == 0 {
					ans += 1
				}
			}
			if (j == b[0] || j == b[1]) && (i > a[0] && i < a[1]) {
				h := 0
				for k:=0; k<n; k++ {
					p, q := 0, 0
					p = i-x[k]
					q = j-y[k]
					if p*p+q*q <= r[k]*r[k] {
						h = 1
					}
				}
				if h == 0 {
					ans += 1
				}
			}
		}
	}
	w.WriteString(strconv.Itoa(ans)+"\n")
	w.Flush()
}

package main

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

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

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

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

  x0, y0, x1, y1 := getI(), getI(), getI(), getI()
  if x0 > x1 {
    x0, x1 = x1, x0
  }
  if y0 > y1 {
    y0, y1 = y1, y0
  }
  chairs := map[[2]int]bool{}
  for x := x0; x <= x1; x++ {
    chairs[[2]int{ x, y0 }] = true
    chairs[[2]int{ x, y1 }] = true
  }
  for y := y0; y <= y1; y++ {
    chairs[[2]int{ x0, y }] = true
    chairs[[2]int{ x1, y }] = true
  }

  numRadiators := getI()
  for i := 0; i < numRadiators; i++ {
    x, y, r := getI(), getI(), getI()
    toRemove := [][2]int{}
    for chair := range(chairs) {
      X, Y := chair[0], chair[1]
      if (X - x) * (X - x) + (Y - y) * (Y - y) <= r * r {
        toRemove = append(toRemove, chair)
      }
    }
    for _, chair := range(toRemove) {
      delete(chairs, chair)
    }
  }

  result := len(chairs)
  writer.WriteString(fmt.Sprintf("%d\n", result))
}


package main

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

func main() {
	for t := ScanInt(); t > 0; t-- {
		n, x := ScanInt(), ScanInt()
		a := ScanIntSlice(n)
		P(solve(n, x, a))
	}

	close(O)
	oWg.Wait()
}

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

	l, r, c := -1, -1, 0

	for i := 0; i < n; i++ {
		if a[i] < x {
			l = i
		}
		if a[i] >= x {
			r = i
		}
	}
	if r == -1 {
		return 0
	}
	if l == -1 {
		return n
	}

	result := l
	for i := l; i >= 0; i-- {
		for c < (x-a[i]) && r > l {
			c += (a[r] - x)
			r--
		}
		if a[i]+c >= x {
			c -= (x - a[i])
			a[i] = x
			result--
		}
	}
	return n - result - 1
}

/*********************** I/O ***********************/

var O chan string
var in *bufio.Scanner
var oWg *sync.WaitGroup
var P func(a ...interface{}) (n int, err error)

func init() {
	P = fmt.Println

	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(1e+9))
	in.Split(bufio.ScanWords)

	O = make(chan string, 16)
	oWg = &sync.WaitGroup{}
	oWg.Add(1)

	writer := bufio.NewWriter(os.Stdout)
	go func() {
		defer oWg.Done()
		defer writer.Flush()

		for line := range O {
			writer.WriteString(line + "\n")
		}
	}()
}

func ScanWord() string { in.Scan(); return in.Text() }
func ScanInt() int     { i, _ := strconv.Atoi(ScanWord()); return i }
func ScanInt64() int64 { i, _ := strconv.ParseInt(ScanWord(), 10, 64); return i }
func ScanStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ScanWord()
	}
	return s
}
func ScanIntSlice(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt()
	}
	return arr
}
func ScanInt64Slice(n int) []int64 {
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt64()
	}
	return arr
}

/*********************** Utils ***********************/

func S(v interface{}) string { return fmt.Sprintf("%v", v) }
func SArr(a []int) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.Itoa(a[i]) + " ")
	}
	return sb.String()
}
func SArr64(a []int64) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.FormatInt(a[i], 10) + " ")
	}
	return sb.String()
}
func MnMx(args ...int64) (int64, int64) {
	minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64)
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn(args ...int64) int64 { mn, _ := MnMx(args...); return mn }
func Mx(args ...int64) int64 { _, mx := MnMx(args...); return mx }
func LCM(a, b int64) int64   { return a / GCD(a, b) * b }
func GCD(a, b int64) int64 {
	if a == 0 {
		return b
	}
	return GCD(b%a, a)
}
func sortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

package main

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

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

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }
func scanIntArr() ([]int, error) {
	str, err := reader.ReadString('\n')
	if err != nil {
		return nil, err
	}
	str = strings.TrimRight(str, "\n\r")
	strArr := strings.Split(str, " ")

	arr := make([]int, len(strArr))
	for i, strA := range strArr {
		arr[i], err = strconv.Atoi(strA)
		if err != nil {
			return nil, err
		}
	}

	return arr, nil
}

func main() {
	defer writer.Flush()

	var t int
	scanf("%d\n", &t)

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

func exec() {
	var n, x int
	scanf("%d %d\n", &n, &x)

	arrA, err := scanIntArr()
	if err != nil {
		fmt.Println(err.Error())
		panic(err)
	}

	richArr := make([]int, 0, len(arrA))
	richSum := uint64(0)
	richNum := uint64(0)
	poorArr := make([]int, 0, len(arrA))
	for _, a := range arrA {
		if a < x {
			poorArr = append(poorArr, a)
			continue
		}
		richSum += uint64(a)
		richNum++
		richArr = append(richArr, a)
	}

	if richNum == 0 {
		printf("0\n")
		return
	}

	sort.Ints(poorArr)
	for i := len(poorArr) - 1; i >= 0; i-- {
		if (uint64(poorArr[i])+richSum)/(richNum+1) >= uint64(x) {
			richSum += uint64(poorArr[i])
			richNum++
			continue
		}
		break
	}
	printf("%d\n", richNum)
}

package main

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

)
func max(a,b int) int{
	if(a>b){
		return a
	}
	return b;
}
func main(){
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var T int;
	fmt.Fscanln(reader, &T)
	for t:=0;t<T;t++ {
		var n int;
		var x int64;
		fmt.Fscanln(reader,&n,&x)
		nums:=make([]int,n)
		var sum int64;
		sum=0
		// count:=0
		for i:=0;i<n;i++{
			if i == n-1 {
				fmt.Fscanf(reader, "%d\n", &nums[i])

			} else {
				fmt.Fscanf(reader, "%d", &nums[i])
			}

		}
		// sum = sum - (count*x)
		sort.Ints(nums);
		// fmt.Println(sum,nums,count)
		// for i:=len(nums)-1-count;i>=0;i--{
		// 	diff:= x-nums[i];
		// 	if(sum-diff>=0){
		// 	sum-=diff;

		// 	count++
		// }
		// }
		prefix:=n;
		for prefix>0 && sum+int64(nums[prefix-1])-x >=0 {
				prefix--
				sum= sum+int64(nums[prefix])-x
		}

		// for i:=len(nums)-count-1;i>=0;i--{
		// 	fmt.Println(nums[i],"here")
		// 	if( sum+nums[i]>=(count+1)*x ){
		// 		count++
		// 		sum+=nums[i]
		// 	}else{
		// 		break;
		// 	}
		// }

		fmt.Fprintln(writer,n-prefix);
	}
}

package main

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

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

	var t, n, x int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &x)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		sort.Ints(a)
		sum := int64(0)
		cnt := int64(0)
		ans := 0
		for i := n - 1; i >= 0; i-- {
			if v := a[i]; v > x {
				sum += int64(v)
				cnt++
				ans = n - i
			} else if v == x {
				ans = n - i
			} else {
				sum += int64(v)
				cnt++
				if int64(x)*cnt <= sum {
					ans = n - i
				} else {
					break
				}
			}
		}
		Fprintln(out, ans)
	}
}

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

package main

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

func main() {
	r := bufio.NewReaderSize(os.Stdin, 50*1024*1024)
	T := readInt(r)
	for t := 1; t <= T; t++ {
		N, X := readTwoInts(r)
		sums := readInts(r, N)
		cnt := solve(sums, float64(X))
		fmt.Print(cnt)
		if t < T {
			fmt.Println()
		}
	}
}

func solve(sums []int, X float64) (result int) {
	sort.Sort(sort.Reverse(sort.IntSlice(sums)))
	curr := float64(0)
	for i := 0; i < len(sums); i++ {
		curr += float64(sums[i])
		avg := curr / float64(i+1)
		if avg < X {
			return i
		}
	}
	return len(sums)
}

/**
* Utils
*/
func readInt(r *bufio.Reader) int {
	return strToInt(readLine(r))
}

func readTwoInts(r *bufio.Reader) (int, int) {
	ints := readInts(r, 2)
	return ints[0], ints[1]
}

func readInts(r *bufio.Reader, n int) []int {
	result := make([]int, n)
	strs := strings.Split(readLine(r), " ")
	for i := 0; i < n; i++ {
		result[i] = strToInt(strs[i])
	}
	return result
}

func readLine(r *bufio.Reader) string {
	str, _, err := r.ReadLine()
	noError(err)
	return string(str)
}

func readLines(r *bufio.Reader, n int) []string {
	result := make([]string, n)
	for i := 0; i < n; i++ {
		result[i] = readLine(r)
	}
	return result
}

func strToInt(s string) int {
	num, err := strconv.Atoi(s)
	noError(err)
	return num
}

func noError(err error) {
	if err != nil {
		panic(err)
	}
}

package main

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

func newScanner() *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	return scanner
}

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

func scanInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = scanInt()
	}
	return a
}

func scanString() string {
	if sc.Scan() {
		return sc.Text()
	}
	panic(sc.Err())
}

var sc = newScanner()

func main() {
	for t := scanInt(); t > 0; t-- {
		n, x := scanInt(), scanInt()
		a := scanInts(n)
		sort.Sort(sort.Reverse(sort.IntSlice(a)))
		sum := int64(0)
		i := 0
		for ; i < n; i++ {
			sum += int64(a[i])
			if sum/int64(i+1) < int64(x) {
				break
			}
		}
		fmt.Println(i)
	}
}

package main

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

type BufferedWriter interface {
	Printf(format string, a ...interface{})
	Flush()
}

type writerImpl struct {
	*bufio.Writer
}

func NewBufferedWriter(writer io.Writer) BufferedWriter {
	return &writerImpl{Writer: bufio.NewWriter(writer)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
	impl.Writer.Flush()
}

type WordScanner interface {
	NextInt() int
	NextInt64() int64
	NextString() string
}

type wordScannerImpl struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) WordScanner {
	s := bufio.NewScanner(reader)
	s.Split(bufio.ScanWords)
	// adjust the following size as needed
	// 2 << 17 is good enough to scan strings of len 2*10^5
	// it could go as high as 2 << 26 before cf judge breaks
	size := 2 << 20
	buf := make([]byte, size)
	s.Buffer(buf, size)
	return &wordScannerImpl{Scanner: s}
}

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

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

func (impl *wordScannerImpl) NextString() string {
	impl.Scan()
	return impl.Text()
}

var reader = NewWordScanner(os.Stdin)
var writer = NewBufferedWriter(os.Stdout)

func main() {
	defer writer.Flush()
	t := reader.NextInt64()
	for ; t > 0; t-- {
		n, x := reader.NextInt64(), reader.NextInt64()
		aa := make([]int64, n)
		for i := 0; i < len(aa); i++ {
			aa[i] = reader.NextInt64()
		}
		writer.Printf("%d\n", sol(aa, x))
	}
}

func sol(aa []int64, x int64) int64 {
	sort.Slice(aa, func(a, b int) bool {
        return aa[a] < aa[b]
	})
    ret := int64(0)
    currTotal := int64(0)
    for i := len(aa)-1; i > -1; i-- {
        if (currTotal + aa[i]) / (ret+1) >= x {
            ret++
            currTotal += aa[i]
            continue
        }
        break
    }
	return ret
}

package main

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

)
func max(a,b int) int{
	if(a>b){
		return a
	}
	return b;
}
func main(){
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var T int;
	fmt.Fscanln(reader, &T)
	for t:=0;t<T;t++ {
		var n,x int;
		fmt.Fscanln(reader,&n,&x)
		nums:=make([]int,n)
		sum:=0
		count:=0
		for i:=0;i<n;i++{
			if i == n-1 {
				fmt.Fscanf(reader, "%d\n", &nums[i])

			} else {
				fmt.Fscanf(reader, "%d", &nums[i])
			}
			if(nums[i]>=x){
			sum+=nums[i]
			count++
			}
		}
		// sum = sum - (count*x)
		sort.Ints(nums);
		for i:=len(nums)-1-count;i>=0;i--{
			if( sum+nums[i]>=(count+1)*x ){
				count++
				sum+=nums[i]
			}
		}
		// fmt.Println(sum,nums,count)
		// for i:=len(nums)-1-count;i>=0;i--{
		// 	diff:= x-nums[i];
		// 	if(sum-diff>=0){
		// 	sum-=diff;

		// 	count++
		// }
		// }
		fmt.Fprintln(writer,count);
	}
}

package main

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

)
func max(a,b int) int{
	if(a>b){
		return a
	}
	return b;
}
func main(){
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var T int;
	fmt.Fscanln(reader, &T)
	for t:=0;t<T;t++ {
		var n,x int;
		fmt.Fscanln(reader,&n,&x)
		nums:=make([]int,n)
		sum:=0
		// count:=0
		for i:=0;i<n;i++{
			if i == n-1 {
				fmt.Fscanf(reader, "%d\n", &nums[i])

			} else {
				fmt.Fscanf(reader, "%d", &nums[i])
			}
			// if(nums[i]>=x){
			// sum+=nums[i]
			// count++
			// }
		}
		// sum = sum - (count*x)
		sort.Ints(nums);
		// fmt.Println(sum,nums,count)
		// for i:=len(nums)-1-count;i>=0;i--{
		// 	diff:= x-nums[i];
		// 	if(sum-diff>=0){
		// 	sum-=diff;

		// 	count++
		// }
		// }
		prefix:=n;
		for prefix>0 && sum+nums[prefix-1]-x>=0 {
				prefix--
				sum= sum+nums[prefix]-x
		}

		// for i:=len(nums)-count-1;i>=0;i--{
		// 	fmt.Println(nums[i],"here")
		// 	if( sum+nums[i]>=(count+1)*x ){
		// 		count++
		// 		sum+=nums[i]
		// 	}else{
		// 		break;
		// 	}
		// }

		fmt.Fprintln(writer,n-prefix);
	}
}

package main

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

)
func max(a,b int) int{
	if(a>b){
		return a
	}
	return b;
}
func main(){
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var T int;
	fmt.Fscanln(reader, &T)
	for t:=0;t<T;t++ {
		var n,x int;
		fmt.Fscanln(reader,&n,&x)
		nums:=make([]int,n)
		sum:=0
		// count:=0
		for i:=0;i<n;i++{
			if i == n-1 {
				fmt.Fscanf(reader, "%d\n", &nums[i])

			} else {
				fmt.Fscanf(reader, "%d", &nums[i])
			}
			// if(nums[i]>=x){
			// sum+=nums[i]
			// count++
			// }
		}
		// sum = sum - (count*x)
		sort.Ints(nums);
		// fmt.Println(sum,nums,count)
		// for i:=len(nums)-1-count;i>=0;i--{
		// 	diff:= x-nums[i];
		// 	if(sum-diff>=0){
		// 	sum-=diff;

		// 	count++
		// }
		// }
		prefix:=n;
		for prefix>0 && sum+nums[prefix-1]-x>=0 {
				prefix--
				sum= sum+prefix-x
		}

		// for i:=len(nums)-count-1;i>=0;i--{
		// 	fmt.Println(nums[i],"here")
		// 	if( sum+nums[i]>=(count+1)*x ){
		// 		count++
		// 		sum+=nums[i]
		// 	}else{
		// 		break;
		// 	}
		// }

		fmt.Fprintln(writer,n-prefix);
	}
}

package main

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

)
func max(a,b int) int{
	if(a>b){
		return a
	}
	return b;
}
func main(){
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var T int;
	fmt.Fscanln(reader, &T)
	for t:=0;t<T;t++ {
		var n,x int;
		fmt.Fscanln(reader,&n,&x)
		nums:=make([]int,n)
		sum:=0
		count:=0
		for i:=0;i<n;i++{
			if i == n-1 {
				fmt.Fscanf(reader, "%d\n", &nums[i])

			} else {
				fmt.Fscanf(reader, "%d", &nums[i])
			}
			if(nums[i]>=x){
			sum+=nums[i]
			count++
			}
		}
		// sum = sum - (count*x)
		sort.Ints(nums);
// 		fmt.Println(sum,"SUM",len(nums)-count-1)
		for i:=len(nums)-count-1;i>=0;i--{
			if( sum+nums[i]>=(count+1)*x ){
				count++
				sum+=nums[i]
			}else{
				break;
			}
		}
		// fmt.Println(sum,nums,count)
		// for i:=len(nums)-1-count;i>=0;i--{
		// 	diff:= x-nums[i];
		// 	if(sum-diff>=0){
		// 	sum-=diff;

		// 	count++
		// }
		// }
		fmt.Fprintln(writer,count);
	}
}

package main

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

)
func max(a,b int) int{
	if(a>b){
		return a
	}
	return b;
}
func main(){
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var T int;
	fmt.Fscanln(reader, &T)
	for t:=0;t<T;t++ {
		var n,x int;
		fmt.Fscanln(reader,&n,&x)
		nums:=make([]int,n)
		sum:=0
		count:=0
		for i:=0;i<n;i++{
			if i == n-1 {
				fmt.Fscanf(reader, "%d\n", &nums[i])

			} else {
				fmt.Fscanf(reader, "%d", &nums[i])
			}
			if(nums[i]>=x){
			sum+=nums[i]
			count++
			}
		}
		sum = sum - (count*x)
		sort.Ints(nums);
		// fmt.Println(sum,nums,count)
		for i:=len(nums)-1-count;i>=0;i--{
			diff:= x-nums[i];
			if(sum-diff>=0){
			sum-=diff;

			count++
		}
		}
		fmt.Fprintln(writer,count);
	}
}

package main

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

)
func max(a,b int) int{
	if(a>b){
		return a
	}
	return b;
}
func main(){
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var T int;
	fmt.Fscanln(reader, &T)
	for t:=0;t<T;t++ {
		var n,x int;
		fmt.Fscanln(reader,&n,&x)
		nums:=make([]int,n)
		sum:=0
		count:=0
		for i:=0;i<n;i++{
			if i == n-1 {
				fmt.Fscanf(reader, "%d\n", &nums[i])

			} else {
				fmt.Fscanf(reader, "%d", &nums[i])
			}
			if(nums[i]>=x){
			sum+=nums[i]
			count++
			}
		}
		// sum = sum - (count*x)
		sort.Ints(nums);
		fmt.Println(sum,"SUM",len(nums)-count-1)
		for i:=len(nums)-count-1;i>=0;i--{
			if( sum+nums[i]>=(count+1)*x ){
				count++
				sum+=nums[i]
			}else{
				break;
			}
		}
		// fmt.Println(sum,nums,count)
		// for i:=len(nums)-1-count;i>=0;i--{
		// 	diff:= x-nums[i];
		// 	if(sum-diff>=0){
		// 	sum-=diff;

		// 	count++
		// }
		// }
		fmt.Fprintln(writer,count);
	}
}

package main

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

func main() {
	r := bufio.NewReaderSize(os.Stdin, 50*1024*1024)
	T := readInt(r)
	for t := 1; t <= T; t++ {
		N, X := readTwoInts(r)
		sums := readInts(r, N)
		cnt := solve(sums, X)
		fmt.Print(cnt)
		if t < T {
			fmt.Println()
		}
	}
}

func solve(sums []int, X int) (result int) {
	sort.Sort(sort.Reverse(sort.IntSlice(sums)))
	curr := 0
	for i := 0; i < len(sums); i++ {
		curr += sums[i]
		avg := curr / (i+1)
		if avg < X {
			return i
		}
	}
	return len(sums)
}

/**
* Utils
*/
func readInt(r *bufio.Reader) int {
	return strToInt(readLine(r))
}

func readTwoInts(r *bufio.Reader) (int, int) {
	ints := readInts(r, 2)
	return ints[0], ints[1]
}

func readInts(r *bufio.Reader, n int) []int {
	result := make([]int, n)
	strs := strings.Split(readLine(r), " ")
	for i := 0; i < n; i++ {
		result[i] = strToInt(strs[i])
	}
	return result
}

func readLine(r *bufio.Reader) string {
	str, _, err := r.ReadLine()
	noError(err)
	return string(str)
}

func readLines(r *bufio.Reader, n int) []string {
	result := make([]string, n)
	for i := 0; i < n; i++ {
		result[i] = readLine(r)
	}
	return result
}

func strToInt(s string) int {
	num, err := strconv.Atoi(s)
	noError(err)
	return num
}

func noError(err error) {
	if err != nil {
		panic(err)
	}
}

package main

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

func main() {
	r := bufio.NewReaderSize(os.Stdin, 100*1024*1024)
	T := readInt(r)
	for t := 1; t <= T; t++ {
		N, X := readTwoInts(r)
		sums := readInts(r, N)
		cnt := solve(sums, X)
		fmt.Print(cnt)
		if t < T {
			fmt.Println()
		}
	}
}

func solve(sums []int, X int) (result int) {
	sort.Sort(sort.Reverse(sort.IntSlice(sums)))
	curr := 0
	for i := 0; i < len(sums); i++ {
		curr += sums[i]
		avg := curr / (i+1)
		if avg < X {
			return i
		}
	}
	return len(sums)
}

/**
* Utils
*/
func readInt(r *bufio.Reader) int {
	return strToInt(readLine(r))
}

func readTwoInts(r *bufio.Reader) (int, int) {
	ints := readInts(r, 2)
	return ints[0], ints[1]
}

func readInts(r *bufio.Reader, n int) []int {
	result := make([]int, n)
	strs := strings.Split(readLine(r), " ")
	for i := 0; i < n; i++ {
		result[i] = strToInt(strs[i])
	}
	return result
}

func readLine(r *bufio.Reader) string {
	str, _, err := r.ReadLine()
	noError(err)
	return string(str)
}

func readLines(r *bufio.Reader, n int) []string {
	result := make([]string, n)
	for i := 0; i < n; i++ {
		result[i] = readLine(r)
	}
	return result
}

func strToInt(s string) int {
	num, err := strconv.Atoi(s)
	noError(err)
	return num
}

func noError(err error) {
	if err != nil {
		panic(err)
	}
}

package main

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

func newScanner() *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	return scanner
}

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

func scanInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = scanInt()
	}
	return a
}

func scanString() string {
	if sc.Scan() {
		return sc.Text()
	}
	panic(sc.Err())
}

var sc = newScanner()

func main() {
	for t := scanInt(); t > 0; t-- {
		n, x := scanInt(), scanInt()
		a := scanInts(n)
		sort.Sort(sort.Reverse(sort.IntSlice(a)))
		sum := 0
		i := 0
		for ; i < n; i++ {
			sum += a[i]
			if sum/(i+1) < x {
				break
			}
		}
		fmt.Println(i)
	}
}


package main

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

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

/*********** DP sub-functions ***********/

// ChMin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMin(updatedValue *int, target int) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

// NthBit returns nth bit value of an argument.
// n starts from 0.
func NthBit(num, nth int) int {
	return num >> uint(nth) & 1
}

// OnBit returns the integer that has nth ON bit.
// If an argument has nth ON bit, OnBit returns the argument.
func OnBit(num, nth int) int {
	return num | (1 << uint(nth))
}

// OffBit returns the integer that has nth OFF bit.
// If an argument has nth OFF bit, OffBit returns the argument.
func OffBit(num, nth int) int {
	return num & ^(1 << uint(nth))
}

// PopCount returns the number of ON bit of an argument.
func PopCount(num int) int {
	res := 0

	for i := 0; i < 70; i++ {
		if ((num >> uint(i)) & 1) == 1 {
			res++
		}
	}

	return res
}

/*********** Arithmetic ***********/

// Max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

// Min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Min(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// Sum returns multiple integers sum.
func Sum(integers ...int) int {
	s := 0

	for _, i := range integers {
		s += i
	}

	return s
}

// PowInt is integer version of math.Pow
// PowInt calculate a power by Binary Power (二分累乗法(O(log e))).
func PowInt(a, e int) int {
	if a < 0 || e < 0 {
		panic(errors.New("[argument error]: PowInt does not accept negative integers"))
	}

	if e == 0 {
		return 1
	}

	if e%2 == 0 {
		halfE := e / 2
		half := PowInt(a, halfE)
		return half * half
	}

	return a * PowInt(a, e-1)
}

// AbsInt is integer version of math.Abs
func AbsInt(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

// Gcd returns the Greatest Common Divisor of two natural numbers.
// Gcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Gcd uses the Euclidean Algorithm.
func Gcd(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}
	if a < b {
		a, b = b, a
	}

	// Euclidean Algorithm
	for b > 0 {
		div := a % b
		a, b = b, div
	}

	return a
}

// Lcm returns the Least Common Multiple of two natural numbers.
// Lcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Lcd uses the Euclidean Algorithm indirectly.
func Lcm(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}

	// a = a'*gcd, b = b'*gcd, a*b = a'*b'*gcd^2
	// a' and b' are relatively prime numbers
	// gcd consists of prime numbers, that are included in a and b
	gcd := Gcd(a, b)

	// not (a * b / gcd), because of reducing a probability of overflow
	return (a / gcd) * b
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		// str := strconv.FormatInt(A[i], 10)  // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

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

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60

var n int
var A []int

func main() {
	n = ReadInt()
	A = ReadIntSlice(n)

	sort.Sort(sort.IntSlice(A))

	l, r := 0, n-1
	hori, verti := int64(0), int64(0)
	for i := 0; i < n; i++ {
		if i%2 == 0 {
			// 後から
			a := A[r]
			r--
			hori += int64(a)
		} else {
			// 前から
			a := A[l]
			l++
			verti += int64(a)
		}
	}

	ans := hori*hori + verti*verti
	fmt.Println(ans)
}

// MODはとったか？
// 遷移だけじゃなくて最後の最後でちゃんと取れよ？

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

package main

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

const (
	MX_INT int64 = math.MaxInt64
	MN_INT int64 = math.MinInt64
)

func main() {
	n := ScanInt64()
	array := ScanInt64Slice(n)
	sort.Slice(array, func(i, j int) bool {
		return array[i] < array[j]
	})
	vertical, horizontal := int64(0), int64(0)
	for i := int64(0); i < n; i++ {
		if i < n/2 {

			horizontal += array[i]
		} else {
			vertical += array[i]
		}
	}

	fmt.Println(vertical * vertical + horizontal * horizontal)

	close(O)
	oWg.Wait()
}

/*********************** I/O ***********************/

var O chan string
var in *bufio.Scanner
var oWg *sync.WaitGroup
var Pl func(a ...interface{}) (n int, err error)

func init() {
	Pl = fmt.Println

	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(1e8))
	in.Split(bufio.ScanWords)

	O = make(chan string, 16)
	oWg = &sync.WaitGroup{}
	oWg.Add(1)

	writer := bufio.NewWriterSize(os.Stdout, int(1e5))
	go func(writer *bufio.Writer) {
		defer oWg.Done()
		defer writer.Flush()

		for line := range O {
			writer.WriteString(line + "\n")
		}
	}(writer)
}

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

func ScanStringSlice(n int64) []string {
	length := int(n)
	s := make([]string, n)
	for i := 0; i < length; i++ {
		s[i] = ScanWord()
	}
	return s
}

func ScanInt64() int64 {
	intStr := ScanWord()
	i, err := strconv.ParseInt(intStr, 10, 64)
	if err != nil {
		panic(err)
	}
	return i
}

func ScanInt() int {
	intStr := ScanWord()
	i, err := strconv.Atoi(intStr)
	if err != nil {
		panic(err)
	}
	return i
}

func ScanInt64Slice(n int64) []int64 {
	length := int(n)
	arr := make([]int64, n)
	for i := 0; i < length; i++ {
		arr[i] = ScanInt64()
	}
	return arr
}

/*********************** Utils ***********************/

func Mx(args ...int64) int64 {
	maxVal := MN_INT
	for _, v := range args {
		if v > maxVal {
			maxVal = v
		}
	}
	return maxVal
}

func Mn(args ...int64) int64 {
	minVal := MX_INT
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
	}
	return minVal
}

func LCM(a, b int64) int64 {
	return a / GCD(a, b) * b
}

func GCD(a, b int64) int64 {
	if a == 0 {
		return b
	}
	return GCD(b%a, a)
}

func S(v interface{}) string {
	return fmt.Sprintf("%v", v)
}

package main

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

func main() {
	var number int
	fmt.Scanf("%d\n", &number)
	// fmt.Println(number)
	reader := bufio.NewReader(os.Stdin)
	line := readLine2(reader)
	arr := strings.Split(string(line), " ")
	sticks := make([]int, 0)
	sum := int64(0)
	for i := 0; i < number; i++ {
		n, _ := strconv.Atoi(arr[i])
		sticks = append(sticks, n)
		sum += int64(n)
	}
	sort.Ints(sticks)
	half1 := int64(0)
	for i:=0;i<len(sticks)/2;i++ {
		half1 += int64(sticks[i])
	}
	half2 := sum - half1
	// fmt.Println(sticks, sum, half1)
	length := half1 * half1 + half2 * half2
	fmt.Println(length)
}

func readLine2(reader *bufio.Reader) []byte {
	result := make([]byte, 0)
	for {
		s, isPrefix, err := reader.ReadLine()
		if err != nil {
			fmt.Printf("error=%+v\n", err)
			break
		}
		result = append(result, s...)
		if !isPrefix {
			// fmt.Println(string(result))
			break
		}
	}
	return result
}

package main

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

var inf int = 0x3f3f3f3f
var reader *bufio.Reader

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

var n int
var a []int

func main() {
	fmt.Fscan(reader, &n)
	a = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i])
	}
	sort.Sort(sort.IntSlice(a))
	var x, y int64
	for i := 0; i < n; i++ {
		if i < n/2 {
			x += int64(a[i])
		} else {
			y += int64(a[i])
		}
	}
	fmt.Println(x*x + y*y)
}

package main

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

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

// IO helper functions

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// IO functions

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

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

// Read next line as []byte
func readbln() []byte {
	b, err := nextLine()
	if err != nil {
		panic(err)
	}
	return b
}

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

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

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

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

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

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

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

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

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

// Simple math functions

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

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

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

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

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

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

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

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

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

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

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

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

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

func main() {
	defer Flush()

	N := readi()
	a := make([]int64, N)

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

	sort.Slice(a, func(i, j int) bool { return a[i] < a[j] })
	var x, y int64
	for i, j := 0, N-1; i <= j; i, j = i+1, j-1 {
		x += a[j]
		if i == j {
			break
		}
		y += a[i]
	}
	println(x*x + y*y)
}

package main

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

const (
	MX_INT int64 = math.MaxInt64
	MN_INT int64 = math.MinInt64
)

func main() {
    n := ScanInt()
    a := ScanInt64Slice(n)
	sort.Slice(a, func(i, j int) bool {return a[i] < a[j]})

    middle := n/2
    h, v := int64(0), int64(0)
    for i := 0; i < n; i++ {
        if i < middle {
        	h += a[i]
		} else {
			v += a[i]
		}
    }

    P(((h*h) + (v*v)))

	close(O)
	oWg.Wait()
}

/*********************** I/O ***********************/

var O chan string
var in *bufio.Scanner
var oWg *sync.WaitGroup
var P func(a ...interface{}) (n int, err error)

func init() {
	P = fmt.Println

	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(1e+9))
	in.Split(bufio.ScanWords)

	O = make(chan string, 16)
	oWg = &sync.WaitGroup{}
	oWg.Add(1)

	writer := bufio.NewWriter(os.Stdout)
	go func() {
		defer oWg.Done()
		defer writer.Flush()

		for line := range O {
			writer.WriteString(line + "\n")
		}
	}()
}

func ScanWord() string {in.Scan(); return in.Text() }
func ScanInt() int {i, _ := strconv.Atoi(ScanWord()); return i}
func ScanInt64() int64 {i, _ := strconv.ParseInt(ScanWord(), 10, 64); return i}
func ScanStringSlice(n int) []string {s := make([]string, n); for i := 0; i < n; i++ {s[i] = ScanWord()}; return s}
func ScanInt64Slice(n int) []int64 {arr := make([]int64, n); for i := 0; i < n; i++ {arr[i] = ScanInt64()}; return arr}

/*********************** Utils ***********************/

func S(v interface{}) string {return fmt.Sprintf("%v", v)}
func SArr(a []int64) string {sb := strings.Builder{}; sb.Grow(len(a) * 9); for i := range a {sb.WriteString(strconv.FormatInt(a[i], 10) + " ")}; return sb.String()}
func MnMx(args ...int64) (int64, int64) {minVal, maxVal := MX_INT, MN_INT; for _, v := range args {if v < minVal {minVal = v}; if v > maxVal {maxVal = v}}; return minVal, maxVal}
func Mn(args ...int64) int64 { mn, _ := MnMx(args...); return mn }
func Mx(args ...int64) int64 { _, mx := MnMx(args...); return mx }
func LCM(a, b int64) int64 {return a / GCD(a, b) * b}
func GCD(a, b int64) int64 {if (a == 0) {return b}; return GCD(b%a, a)}
func sortInt64Slice(a []int64) {sort.Slice(a, func(i, j int) bool { return a[i] < a[j] })}
func abs(x int64) int64 {if x < 0 {return -x}; return x}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscanf(reader, "%d\n", &n)

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

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

	//fmt.Println(a)

	var x, y int64
	for i := 0; i < n/2; i++ {
		x += int64(a[i])
	}
	for i := n / 2; i < n; i++ {
		y += int64(a[i])
	}
	fmt.Printf("%d\n", x*x+y*y)

}

package main

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

/* solution */

func solve() {
	n := readInt()
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = readInt()
	}
	sort.Ints(a)
	var c, d int64
	for i := 0; i < n/2; i++ {
		c += int64(a[i])
		d += int64(a[n-i-1])
	}
	if n%2 > 0 {
		d += int64(a[n/2])
	}
	printInt64(c*c + d*d)
}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOROOT() == "C:\\Soft\\Go" {
		f, _ = os.Open("input.txt")
		defer f.Close()
	} else {
		f = os.Stdin
	}

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

	reader = bufio.NewScanner(f)

	const maxCapacity = 1024 * 1024
	buf := make([]byte, maxCapacity)
	reader.Buffer(buf, maxCapacity)

	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func read() string {
	reader.Scan()
	return reader.Text()
}

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

func print(s string) {
	writer.WriteString(s)
	writer.WriteString("\n")
}

func printInt(i int) {
	print(strconv.Itoa(i))
}

func printInt64(i int64) {
	print(strconv.FormatInt(i, 10))
}

func readInt64() int64 {
	reader.Scan()
	ret, _ := strconv.ParseInt(reader.Text(), 10, 64)
	return ret
}

package main

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

var inf int = 0x3f3f3f3f
var reader *bufio.Reader

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

var n int
var a []int

func main() {
	fmt.Fscan(reader, &n)
	a = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i])
	}
	sort.Sort(sort.IntSlice(a))
	var x, y int
	for i := 0; i < n; i++ {
		if i < n/2 {
			x += a[i]
		} else {
			y += a[i]
		}
	}
	fmt.Println(x*x + y*y)
}