package main

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

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

	children := make([]int, n)
	parent := make([]int, n)
	for i := 1; i < n; i++ {
		fmt.Fscanf(stdin, "%d\n", &x)
		x--
		parent[i] = x
		children[parent[i]]++
	}

	sonsLeaves := make([]int, n)
	for i := 0; i < n; i++ {
		if children[i] == 0 {
			sonsLeaves[parent[i]]++
		}
	}
	for i := 0; i < n; i++ {
		if children[i] > 0 && sonsLeaves[i] < 3 {
			fmt.Println("No")
			return
		}
	}
	fmt.Println("Yes")
}

package main

import "fmt"

// 1200
// 26.05.2020
// https://codeforces.com/problemset/problem/913/B

func main() {
	var n int
	fmt.Scanln(&n)
	parents := make([]int, n)
	count := make([]int, n)
	for i := 1; i < n; i++ {
		var parent int
		fmt.Scanln(&parent)
		parent--
		parents[i] = parent
		count[parent]++
	}

	count2 := make([]int, n)
	for i, c := range count {
		if c == 0 {
			count2[parents[i]]++
		}
	}
	for i, c := range count2 {
		if count[i] != 0 && c < 3 {
			fmt.Println("No")
			return
		}
	}
	fmt.Println("Yes")
}

package main

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

var to = [1111][]int{}
var ans = true

func dp(pos int) int {
	var cnt = 0
	for i := 0; i < len(to[pos]); i++ {
		cnt += dp(to[pos][i])
	}
	// fmt.Println(pos, cnt)
	if len(to[pos]) != 0 && cnt < 3 {
		// fmt.Println(ans)
		ans = false
	}
	if cnt == 0 {
		return 1
	} else {
		return 0
	}
}

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

	for i := 0; i < n-1; i++ {
		fmt.Fscanf(cin, "%d\n", &x)
		to[x-1] = append(to[x-1], i+1)
	}
	dp(0)
	if ans {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}

package main

import "fmt"

func main(){
	var b[3300] int
	var a[3300] int
	var c[3300] int
	var n,x int

	fmt.Scan(&n)

	for i:=2;i<=n;i++{
		fmt.Scan(&x)
		a[i]=x
		if b[x]==0{
			b[x]=1
		}
	}

	for i:=2;i<=n;i++{
		if b[i]==0{
			c[a[i]]++
		}
	}

	for i:=1;i<=n;i++{
		if b[i]==0 {
			continue;
		}
		if c[i]<3 {
			fmt.Println("No")
			return
		}
	}
	fmt.Println("Yes")

}

package main

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

type Graph struct {
	Adj     map[int][]int
	Visited map[int]bool
	Leaves  map[int]int
}

func explore(g *Graph, u, p int) {
	g.Visited[u] = true
	if edges, ok := g.Adj[u]; ok {
		for _, v := range edges {
			if _, ok := g.Visited[v]; !ok {
				explore(g, v, u)
			}
		}

	} else {
		if _, ok := g.Leaves[p]; !ok {
			g.Leaves[p] = 1
		} else {
			g.Leaves[p] = g.Leaves[p] + 1
		}
	}
}

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

	var n, parent int
	fmt.Fscanf(reader, "%d\n", &n)
	g := Graph{Adj: make(map[int][]int), Leaves: make(map[int]int), Visited: make(map[int]bool)}
	for i := 1; i < n; i++ {
		fmt.Fscanf(reader, "%d\n", &parent)
		if _, ok := g.Adj[parent]; !ok {
			g.Adj[parent] = make([]int, 0)
		}
		g.Adj[parent] = append(g.Adj[parent], i+1)
	}
	for i := 1; i <= n; i++ {
		if _, ok := g.Visited[i]; !ok {
			explore(&g, i, 0)
		}
	}
	flag := true
	for i := 1; i <= n; i++ {
		if _, ok := g.Adj[i]; ok {
			if _, yes := g.Leaves[i]; (yes && g.Leaves[i] < 3) || !yes {
				flag = false
				break
			}
		}
	}
	if flag {
		fmt.Fprintf(writer, "Yes\n")
	} else {
		fmt.Fprintf(writer, "No\n")
	}
}

package main

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

type Graph struct {
	Adj     map[int][]int
	Visited map[int]bool
	Leaves  map[int]int
	Spruce  bool
}

func explore(g *Graph, u, p int) {
	g.Visited[u] = true
	if edges, ok := g.Adj[u]; ok {
		for _, v := range edges {
			if _, ok := g.Visited[v]; !ok {
				explore(g, v, u)
			}
		}
		if count, ok := g.Leaves[u]; (ok && count < 3) || !ok {
			g.Spruce = false
		}

	} else {
		if _, ok := g.Leaves[p]; !ok {
			g.Leaves[p] = 1
		} else {
			g.Leaves[p] = g.Leaves[p] + 1
		}
	}
}

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

	var n, parent int
	fmt.Fscanf(reader, "%d\n", &n)
	g := Graph{Adj: make(map[int][]int), Leaves: make(map[int]int), Visited: make(map[int]bool), Spruce: true}
	for i := 1; i < n; i++ {
		fmt.Fscanf(reader, "%d\n", &parent)
		if _, ok := g.Adj[parent]; !ok {
			g.Adj[parent] = make([]int, 0)
		}
		g.Adj[parent] = append(g.Adj[parent], i+1)
	}
	for i := 1; i <= n; i++ {
		if _, ok := g.Visited[i]; !ok {
			explore(&g, i, 0)
		}
	}
	if g.Spruce {
		fmt.Fprintf(writer, "Yes\n")
	} else {
		fmt.Fprintf(writer, "No\n")
	}
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	children := make([][]int, n)
	isLeaf := func(v int) bool {
		return len(children[v]) == 0 && v != 0
	}
	for c := 1; c < n; c++ {
		var p int
		fmt.Scan(&p)
		p--
		children[p] = append(children[p], c)
	}
	for v, ch := range children {
		if !isLeaf(v) {
			lc := 0
			for _, c := range ch {
				if isLeaf(c) {
					lc++
				}
			}
			if lc < 3 {
				fmt.Println("No")
				return
			}
		}
	}
	fmt.Println("Yes")
}

package main

import "fmt"

func is3Node(node int, tree [][]int) (bool, bool) {
	var leafCount int
	if len(tree[node]) == 0 {
		return true, true
	}

	for _, n := range tree[node] {
		is3, isLeaf := is3Node(n, tree)
		if !is3 {
			return false, false
		}
		if isLeaf {
			leafCount++
		}
	}

	if leafCount >= 3 {
		return true, false
	}
	return false, false
}

func is3Tree(tree [][]int) string {
	is3, isLeaf := is3Node(0, tree)
	if is3 && !isLeaf {
		return "Yes"
	} else {
		return "No"
	}
}

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

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

	for i := 1; i < n; i++ {
		var node int
		fmt.Scan(&node)
		tree[node-1] = append(tree[node-1], i)
	}

	fmt.Println(is3Tree(tree))
}

package main

import "fmt"

func is3Node(node int, tree [][]int) (bool, bool) {
	var leafCount int
	if len(tree[node]) == 0 {
		return true, true
	}

	for _, n := range tree[node] {
		is3, isLeaf := is3Node(n, tree)
		if !is3 {
			return false, false
		}
		if isLeaf {
			leafCount++
		}
	}

	if leafCount == 3 {
		return true, false
	}
	return false, false
}

func is3Tree(tree [][]int) string {
	is3, isLeaf := is3Node(0, tree)
	if is3 && !isLeaf {
		return "Yes"
	} else {
		return "No"
	}
}

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

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

	for i := 1; i < n; i++ {
		var node int
		fmt.Scan(&node)
		tree[node-1] = append(tree[node-1], i)
	}

	fmt.Println(is3Tree(tree))
}

package main

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

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

	children := make(map[int]int)
	nonLeaf := make(map[int]bool)
	parent := make([]int, n)

	// count all children for non-leaf children
	for i := 1; i < n; i++ {
		fmt.Fscanf(stdin, "%d\n", &x)
		x-- // since we use root = 0 instead of 1
		parent[i] = x
		nonLeaf[x] = true
		children[x]++
	}
	// subtract non-leaf children from all children
	for x = range nonLeaf {
		if x > 0 { // skip root
			children[parent[x]]--
		}
		if children[parent[x]] < 3 {
			fmt.Println("No")
			return
		}
	}
	fmt.Println("Yes")
}

package main

import "fmt"

// 1200
// 26.05.2020
// https://codeforces.com/problemset/problem/913/B

func main() {
	var n int
	fmt.Scanln(&n)
	parents := make([]int, n)
	count := make([]int, n)
	for i := 1; i < n; i++ {
		var parent int
		fmt.Scanln(&parent)
		parent--
		parents[i] = parent
		count[parent]++
	}

	count2 := make([]int, n)
	for i, c := range count {
		if c == 0 {
			count2[parents[i]]++
		}
	}
	for _, c := range count2 {
		if c != 0 && c < 3 {
			fmt.Println("No")
			return
		}
	}
	fmt.Println("Yes")
}

package main

import "fmt"

// 1200
// 26.05.2020
// https://codeforces.com/problemset/problem/913/B

func main() {
	var n int
	fmt.Scanln(&n)
	parents := make([]int, n)
	count := make([]map[int]struct{}, n)
	for i := 1; i < n; i++ {
		var parent int
		fmt.Scanln(&parent)
		parent--
		parents[i] = parent
		if count[parent] == nil {
			count[parent] = make(map[int]struct{})
		}
		count[parent][i] = struct{}{}
		if parent != 0 {
			delete(count[parents[parent]], parent)
		}
	}
	for _, c := range count {
		if len(c) != 0 && len(c) != 3 {
			fmt.Println("No")
			return
		}
	}
	fmt.Println("Yes")
}

package main

import "fmt"

// 1200
// 26.05.2020
// https://codeforces.com/problemset/problem/913/B

func main() {
	var n int
	fmt.Scanln(&n)
	parents := make([]int, n)
	count := make([]int, n)
	for i := 1; i < n; i++ {
		var parent int
		fmt.Scanln(&parent)
		parent--
		parents[i] = parent
		count[parent]++
	}

	count2 := make([]int, n)
	for i, c := range count {
		if c == 0 {
			count2[parents[i]]++
		}
	}
	for _, c := range count2 {
		if c != 0 && c != 3 {
			fmt.Println("No")
			return
		}
	}
	fmt.Println("Yes")
}

package main

import "fmt"

// 1200
// 26.05.2020
// https://codeforces.com/problemset/problem/913/B

func main() {
	var n int
	fmt.Scanln(&n)
	parents := make([]int, n)
	count := make([]map[int]bool, n)
	for i := 1; i < n; i++ {
		var parent int
		fmt.Scanln(&parent)
		parent--
		parents[i] = parent
		if count[parent] == nil {
			count[parent] = make(map[int]bool)
		}
		if _, ok := count[parent][i]; !ok {
			count[parent][i] = true
		}
		if parent != 0 {
			count[parents[parent]][parent] = false
		}
	}
	for _, c := range count {
		var cnt int
		for _, v := range c {
			if v {
				cnt++
			}
		}
		if cnt != 0 && cnt != 3 {
			fmt.Println("No")
			return
		}
	}
	fmt.Println("Yes")
}

package main

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

type Graph struct {
	Adj     map[int][]int
	Visited map[int]bool
	Leaves  map[int]int
}

func explore(g *Graph, u, p int) {
	g.Visited[u] = true
	if edges, ok := g.Adj[u]; ok {
		for _, v := range edges {
			if _, ok := g.Visited[v]; !ok {
				explore(g, v, u)
			}
		}

	} else {
		if _, ok := g.Leaves[p]; !ok {
			g.Leaves[p] = 1
		} else {
			g.Leaves[p] = g.Leaves[p] + 1
		}
	}
}

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

	var n, parent int
	fmt.Fscanf(reader, "%d\n", &n)
	g := Graph{Adj: make(map[int][]int), Leaves: make(map[int]int), Visited: make(map[int]bool)}
	for i := 1; i < n; i++ {
		fmt.Fscanf(reader, "%d\n", &parent)
		if _, ok := g.Adj[parent]; !ok {
			g.Adj[parent] = make([]int, 0)
		}
		g.Adj[parent] = append(g.Adj[parent], i+1)
	}
	for i := 1; i <= n; i++ {
		if _, ok := g.Visited[i]; !ok {
			explore(&g, i, -1)
		}
	}
	flag := true
	for _, count := range g.Leaves {
		if count < 3 {
			flag = false
			break
		}
	}
	if flag {
		fmt.Fprintf(writer, "Yes\n")
	} else {
		fmt.Fprintf(writer, "No\n")
	}
}


package main

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

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

	return y
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n, a, b int
	var s string

	fmt.Fscanf(reader, "%d %d %d\n%s\n", &n, &a, &b, &s)

	var v [5002][5002]int
	ans := make([]int, n+1)

	for i := 1; i <= n; i++ {
		for j := i; j <= n; j++ {
			if s[i-1] == s[j-1] {
				v[i][j] = v[i-1][j-1] + 1
			}
		}
	}

	for i := 1; i <= n; i++ {
		ans[i] = ans[i-1] + a

		for j := 1; j < i; j++ {
			if t := min(i-j, v[j][i]); t > 0 {
				ans[i] = min(ans[i], ans[i-t]+b)
			}
		}
		//fmt.Println(i, ans[i])
	}

	fmt.Println(ans[n])
}

package main

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

var suffix [5000][5000]int
var dp [5000]int

func main() {
	var n, a, b int
	var s string
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)
	fmt.Fscanf(reader, "%d %d %d\n", &n, &a, &b)
	fmt.Fscanf(reader, "%s", &s)

	for i := 0;i < n;i ++ {
		for j := i + 1;j < n;j ++ {
			if s[i] == s[j] {
				if i == 0 {
					suffix[i][j] = 1
				} else {
					suffix[i][j] = suffix[i - 1][j - 1] + 1
				}
			}
		}
	}
	dp[0] = a
	for i := 1;i < n;i ++ {
		dp[i] = dp[i - 1] + a
		min := i + 1
		for j := 0;j < i;j ++ {
			if suffix[j][i] != 0 {
				if i - suffix[j][i] >= j && min > i - suffix[j][i] {
					min = i - suffix[j][i]
				}
				if i - suffix[j][i] < j && min > j {
					min = j
				}
			}
		}
		for j := min;j < i;j ++ {
			if dp[j] + b < dp[i] {
				dp[i] = dp[j] + b
			}
		}
	}
	fmt.Printf("%d", dp[n - 1])
}

package main

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

var sub [5000][5000]bool
var dp [5000]int

func main() {
	var n, a, b int
	var s string
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)
	fmt.Fscanf(reader, "%d %d %d\n", &n, &a, &b)
	fmt.Fscanf(reader, "%s", &s)
	for i := 0;i < len(s);i ++ {
		matchPos := i
		for j := 0;j < i && matchPos < len(s);j ++ {
			if s[j] == s[matchPos] {
				sub[i][matchPos] = true
				matchPos ++
			}
		}
	}
	dp[0] = a
	for i := 1;i < len(s);i ++ {
		dp[i] = dp[i - 1] + a
		for j := i;j > 0;j -- {
			if sub[j][i] && dp[j - 1] + b < dp[i] {
				dp[i] = dp[j - 1] + b
			}
		}
	}
	fmt.Printf("%d", dp[len(s) - 1])
}

package main

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

var sub [5000][5000]bool
var dp [5000]int

func main() {
	var n, a, b int
	var s string
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)
	fmt.Fscanf(reader, "%d %d %d\n", &n, &a, &b)
	fmt.Fscanf(reader, "%s", &s)
	for i := 0;i < len(s);i ++ {
		matchPos := i
		for j := 0;j < i && matchPos < len(s);j ++ {
			if s[j] == s[matchPos] {
				sub[i][matchPos] = true
				matchPos ++
			} else {
				matchPos = i
			}
		}
	}
	dp[0] = a
	for i := 1;i < len(s);i ++ {
		dp[i] = dp[i - 1] + a
		for j := i;j > 0;j -- {
			if sub[j][i] && dp[j - 1] + b < dp[i] {
				dp[i] = dp[j - 1] + b
			}
		}
	}
	fmt.Printf("%d", dp[len(s) - 1])
}

package main

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

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

	return y
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n, a, b int
	var s string

	fmt.Fscanf(reader, "%d %d %d\n%s\n", &n, &a, &b, &s)

	ans := a
	temp := 0

	for i := 1; i < n; {
		ss := s[:i]
		for j := i; j < n; j++ {
			if ok := strings.Contains(ss, s[i:j+1]); !ok || j == n-1 {
				if ok {
					temp = min((j+1-i)*a, b)
					//fmt.Println(ss, s[i:j+1], temp)
					i = j + 1
				} else if i == j {
					temp = a
					//fmt.Println(ss, s[i:j+1], temp)
					i = j + 1
				} else {
					temp = min((j-i)*a, b)
					//fmt.Println(ss, s[i:j+1], temp)
					i = j
				}

				ans += temp

				break
			}
		}
	}

	fmt.Println(ans)
}

package main

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

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

	return y
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n, a, b int
	var s string

	fmt.Fscanf(reader, "%d %d %d\n%s\n", &n, &a, &b, &s)

	ans := a

	for i := 1; i < n; {
		ss := s[:i]
		for j := i; j < n; j++ {
			if ok := strings.Contains(ss, s[i:j+1]); !ok || j == n-1 {
				if i == j && !ok {
					ans += a
				} else if ok {
					ans += min((j+1-i)*a, b)
				} else {
					ans += min((j-i)*a, b)
				}

				//fmt.Println(i, j+1, ans)
				i = j + 1
				break
			}
		}
	}

	fmt.Println(ans)
}


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
}

// 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])
		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)
}

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

/********** 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
const INF_INT32 = math.MaxInt32
const INF_BIT30 = 1 << 30

var t int
var n, p, k int64
var A []int64

func main() {
	t = ReadInt()

	for tc := 0; tc < t; tc++ {
		n, p, k = ReadInt64_3()
		A = ReadInt64Slice(int(n))

		solve()
	}
}

func solve() {
	sort.Sort(Int64List(A))

	// m は中央を意味する何らかの値
	isOK := func(m int) bool {
		val := int64(0)

		tmp := int64(m) % k
		for i := 0; i < int(tmp); i++ {
			val += A[i]
		}
		num := int64(m) / k
		i := m-1
		for num > 0 {
			val += A[i]
			i -= int(k)

			num--
		}

		pp := p - val
		if pp >= 0 {
			return true
		}
		return false
	}

	ng, ok := int(n+1), 0
	for int(math.Abs(float64(ok-ng))) > 1 {
		mid := (ok + ng) / 2
		if isOK(mid) {
			ok = mid
		} else {
			ng = mid
		}
	}

	fmt.Println(ok)
}

type Int64List []int64

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

// S := Int64List{'a', 'b', 'c'}
// sort.Sort(S) or sort.Sort(sort.Reverse(S))

/*
- まずは全探索を検討しましょう
- MODは最後にとりましたか？
- ループを抜けた後も処理が必要じゃありませんか？
- 和・積・あまりを求められたらint64が必要ではありませんか？
- いきなりオーバーフローはしていませんか？
- MOD取る系はint64必須ですよ？
*/

/*
ASCII code

ASCII   10進数  ASCII   10進数  ASCII   10進数
!       33      "       34      #       35
$       36      %       37      &       38
'       39      (       40      )       41
*       42      +       43      ,       44
-       45      .       46      /       47
0       48      1       49      2       50
3       51      4       52      5       53
6       54      7       55      8       56
9       57      :       58      ;       59
<       60      =       61      >       62
?       63      @       64      A       65
B       66      C       67      D       68
E       69      F       70      G       71
H       72      I       73      J       74
K       75      L       76      M       77
N       78      O       79      P       80
Q       81      R       82      S       83
T       84      U       85      V       86
W       87      X       88      Y       89
Z       90      [       91      \       92
]       93      ^       94      _       95
`       96      a       97      b       98
c       99      d       100     e       101
f       102     g       103     h       104
i       105     j       106     k       107
l       108     m       109     n       110
o       111     p       112     q       113
r       114     s       115     t       116
u       117     v       118     w       119
x       120     y       121     z       122
{       123     |       124     }       125
~       126             127
*/

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

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := int64(1)
	fmt.Fscanf(in, "%d\n", &t)
	for tt := int64(0); tt < t; tt++ {
		n, p, k := 0, 0, 0
		fmt.Fscanf(in, "%d %d %d\n", &n, &p, &k)
		a := make([]int, n)
		f := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &a[i])
		}
		sort.Ints(a)
		fmt.Fscanf(in, "\n")
		f[0] = a[0]
		ans := 0
		if f[0] <= p {
			ans = 1
		}
		for i := 1; i < n; i++ {
			if (i + 1) < k {
				f[i] = f[i-1] + a[i]
			} else if (i + 1) == k {
				f[i] = a[i]
			} else {
				f[i] = f[i-k] + a[i]
			}
			if f[i] <= p {
				ans = i + 1
			}
		}
		// l := 0
		// r := n
		// for l < r {
		// 	mid := (l + r + 1) / 2
		// 	cost := 0
		// 	need := mid / k
		// 	if mid%k != 0 {
		// 		need++
		// 	}
		// 	cur := mid%k - 1
		// 	if cur < 0 {
		// 		cur += k
		// 	}
		// 	for i := 0; i < need; i++ {
		// 		cost += a[cur]
		// 		cur += k
		// 	}
		// 	if cost > p {
		// 		r = mid - 1
		// 	} else {
		// 		l = mid
		// 	}
		// }
		fmt.Fprintf(out, "%d\n", ans)
	}
	return
}

func same(s string, ss string) bool {
	a := make([]int, 26)
	for i := 0; i < len(s); i++ {
		b := int(s[i] - 'a')
		a[b]++
	}
	for i := 0; i < len(ss); i++ {
		b := int(ss[i] - 'a')
		a[b]--
		if a[b] == -1 {
			return false
		}
	}
	for i := 0; i < 26; i++ {
		if a[i] != 0 {
			return false
		}
	}
	return true
}

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
}

// 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])
		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)
}

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

/********** 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
const INF_INT32 = math.MaxInt32
const INF_BIT30 = 1 << 30

var t int
var n, p, k int64
var A []int64

func main() {
	t = ReadInt()

	for tc := 0; tc < t; tc++ {
		n, p, k = ReadInt64_3()
		A = ReadInt64Slice(int(n))

		solve()
	}
}

func solve() {
	sort.Sort(Int64List(A))

	// m は中央を意味する何らかの値
	isOK := func(m int) bool {
		val := int64(0)
		for i := 0; i < m-2; i++ {
			val += A[i]
		}
		pp := p - val

		if pp >= A[m-1] {
			return true
		}
		return false
	}

	ng, ok := int(n+1), 0
	for int(math.Abs(float64(ok-ng))) > 1 {
		mid := (ok + ng) / 2
		if isOK(mid) {
			ok = mid
		} else {
			ng = mid
		}
	}

	fmt.Println(ok)
}

type Int64List []int64

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

// S := Int64List{'a', 'b', 'c'}
// sort.Sort(S) or sort.Sort(sort.Reverse(S))

/*
- まずは全探索を検討しましょう
- MODは最後にとりましたか？
- ループを抜けた後も処理が必要じゃありませんか？
- 和・積・あまりを求められたらint64が必要ではありませんか？
- いきなりオーバーフローはしていませんか？
- MOD取る系はint64必須ですよ？
*/

/*
ASCII code

ASCII   10進数  ASCII   10進数  ASCII   10進数
!       33      "       34      #       35
$       36      %       37      &       38
'       39      (       40      )       41
*       42      +       43      ,       44
-       45      .       46      /       47
0       48      1       49      2       50
3       51      4       52      5       53
6       54      7       55      8       56
9       57      :       58      ;       59
<       60      =       61      >       62
?       63      @       64      A       65
B       66      C       67      D       68
E       69      F       70      G       71
H       72      I       73      J       74
K       75      L       76      M       77
N       78      O       79      P       80
Q       81      R       82      S       83
T       84      U       85      V       86
W       87      X       88      Y       89
Z       90      [       91      \       92
]       93      ^       94      _       95
`       96      a       97      b       98
c       99      d       100     e       101
f       102     g       103     h       104
i       105     j       106     k       107
l       108     m       109     n       110
o       111     p       112     q       113
r       114     s       115     t       116
u       117     v       118     w       119
x       120     y       121     z       122
{       123     |       124     }       125
~       126             127
*/

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


package main

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

var bufin *bufio.Reader
var bufout *bufio.Writer

const maxa = 300000

var n, x, a int
var cnt []int

func main() {
	bufin = bufio.NewReader(os.Stdin)
	bufout = bufio.NewWriter(os.Stdout)
	defer bufout.Flush()

	fmt.Fscanf(bufin, "%d %d\n", &n, &x)

	cnt = make([]int, maxa+1)
	r := int64(0)

	for i := 1; i <= n; i++ {
		fmt.Fscanf(bufin, "%d\n", &a)
		b := x ^ a
		if b <= maxa {
			r += int64(cnt[b])
		}
		cnt[a]++
	}

	fmt.Fprintf(bufout, "%d\n", r)
}

package main

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

func main() {
	reader := bufio.NewReaderSize(os.Stdin, 1<<13)
	writer := bufio.NewWriterSize(os.Stdout, 1<<13)
	n, x := ReadInt(reader), ReadInt(reader)
	count := make([]int64, 2e5)
	res := int64(0)
	for i := 0; i < n; i++ {
		a := ReadInt(reader)
		wanted := a ^ x
		if wanted < len(count) {
			res += count[wanted]
		}
		count[a]++
	}
	fmt.Fprintln(writer, res)
	writer.Flush()
}

func ReadInt(reader *bufio.Reader) int {
	return int(ReadInt64(reader))
}

func ReadInt64(reader *bufio.Reader) int64 {
	b, err := reader.ReadByte()
	for !isValid(b, err) {
		b, err = reader.ReadByte()
	}
	sign := int64(1)
	if b == '-' {
		sign *= -1
		b, err = reader.ReadByte()
	}
	res := int64(0)
	for isValid(b, err) {
		res = res*10 + int64(b-'0')
		b, err = reader.ReadByte()
	}
	return res * sign
}

func isValid(b byte, err error) bool {
	return err == nil && (('0' <= b && b <= '9') || b == '-')
}

package main

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

func solve() {
    n, x := readInt(), readInt()
    a := readIntArray(n)
    cnts := make(map[int]int)
    for _, v := range a {
        cnts[v]++
    }
    ans := int64(0)
    for _, v := range a {
        b := v^x
        cnt := cnts[b]
        if b == v {
            cnt--
        }
        ans += int64(cnt)
    }
    fmt.Println(ans/2)
}

// Helpers

var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)

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

// Math

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

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

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

// 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
}

// Sort

type Ints64 []int64

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

/* Sort tempalte
func (a ) Len() int           { return len(a) }
func (a ) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ) Less(i, j int) bool { return  }
*/

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"
	"runtime"
	"strings"
)

func main() {
	b, _ := ioutil.ReadAll(os.Stdin)
	if len(b) == 0 {
		test()
	} else {
		solve(bytes.NewReader(b), os.Stdout)
	}
}

func solve(r io.Reader, w io.Writer) {
	var n, x int
	fmt.Fscanln(r, &n, &x)

	ans := uint64(0)
	am := make(map[int]int, n) // a -> cnt
	for i := 0; i < n; i++ {
		var a int
		fmt.Fscan(r, &a)
		want := a ^ x
		if cnt, ok := am[want]; ok && cnt > 0 {
			ans += uint64(cnt)
		}
		am[a]++
	}

	fmt.Fprintln(w, ans)
}

func test() {
	tests := []struct {
		in   string
		want string
	}{
		{
			in: `
2 3
1 2
`,
			want: `1`,
		},
		{
			in: `
6 1
5 1 2 3 4 1
`,
			want: `2`,
		},
		{
			in: `
6 0
5 1 2 2 5 1
`,
			want: `3`,
		},
		{
			in: `
4 0
1 1 1 1
`,
			want: `6`,
		},
	}
	for i, tt := range tests {
		fmt.Printf("=== TEST %d\n", i)
		buf := new(bytes.Buffer)
		solve(strings.NewReader(chomp(tt.in)), buf)
		if got, want := chomp(buf.String()), chomp(tt.want); got != want {
			fmt.Printf("%d: got %v, want %v\n", i, got, want)
		}
	}
}

func chomp(s string) string {
	return strings.Trim(s, "\n")
}

func assert(b bool) {
	if !b {
		_, file, line, ok := runtime.Caller(1)
		if ok {
			f, _ := os.Open(file)
			defer f.Close()
			s := bufio.NewScanner(f)
			lnum := 0
			for s.Scan() {
				lnum++
				if lnum == line {
					wd, _ := os.Getwd()
					fname, _ := filepath.Rel(wd, file)
					fmt.Printf("assertion fails: %s:%d: %v\n", fname, line, s.Text())
					break
				}
			}
		}
	}
}

func pow(a, b int) int {
	r := 1
	for i := 0; i < b; i++ {
		r *= a
	}
	return r
}

/*
URL:
https://codeforces.com/problemset/problem/742/B
*/

package main

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

/********** 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 (
	// General purpose
	MOD          = 1000000000 + 7
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

	// for dijkstra, prim, and so on
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

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

var (
	n, x int
	A    []int

	memo [1000000 + 50]int
)

func main() {
	n, x = ReadInt2()
	A = ReadIntSlice(n)

	for i := 0; i < n; i++ {
		val := A[i] ^ x
		memo[val]++
	}

	ans := int64(0)
	for i := 0; i < n; i++ {
		if A[i]^x != A[i] {
			ans += int64(memo[A[i]])
		} else {
			ans += int64(memo[A[i]] - 1)
		}

		memo[A[i]^x]--
	}
	fmt.Println(ans)
}

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

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

var (
	// ReadString returns a WORD string.
	ReadString 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()
	}
}

// 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
}

// 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])
		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)
}

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

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

package main

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

func main() {
	sc := NewScanner()
	_, x := sc.NextInt(), sc.NextInt()
	mp := map[int]int{}

	a := sc.NextIntArray()
	for _, v := range a {
		mp[v] += 1
	}
	b := make([]int, 0, len(mp))
	count := int64(0)
	for k, _ := range mp {
		b = append(b, k)
		if k ^ k == x {
			count += int64(mp[k]) * int64(mp[k] - 1) / 2
		}
	}
	sort.Ints(b)

	for i := 0; i < len(b) - 1; i++ {
		c := b[i + 1:]
		k := sort.SearchInts(c, b[i] ^ x)
		if k < len(c) && c[k] == b[i] ^ x {
			count += int64(mp[b[i]]) * int64(mp[c[k]])
		}
	}
	fmt.Println(count)

}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r:rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf) + 1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf) + 1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

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

func main() {
	b, _ := ioutil.ReadAll(os.Stdin)
	r := bytes.NewReader(b)

	var n, x int
	fmt.Fscanf(r, "%d %d\n", &n, &x)
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(r, "%d", &a[i])
	}

	cnt := make(map[int64]int64)
	ans := int64(0)
	for j, _ := range a {
		ans += cnt[int64(x)^a[j]]
		cnt[a[j]]++
	}
	fmt.Println(ans)
}

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"
	"runtime"
	"strings"
)

func main() {
	b, _ := ioutil.ReadAll(os.Stdin)
	if len(b) == 0 {
		test()
	} else {
		solve(bytes.NewReader(b), os.Stdout)
	}
}

func solve(r io.Reader, w io.Writer) {
	var n, x int
	fmt.Fscanln(r, &n, &x)
	am := make(map[int]int, n)
	for i := 0; i < n; i++ {
		var a int
		fmt.Fscan(r, &a)
		am[a]++
	}

	ans := 0

	isZero := 0
	if x == 0 {
		isZero = 1
	}

	for a, cnta := range am {
		if cnta < 1 {
			continue
		}
		want := a ^ x
		if cntw, ok := am[want]; ok && cntw > isZero {
			ans++
			am[want]--
			am[a]--
		}
	}

	fmt.Fprintln(w, ans)
}

func test() {
	tests := []struct {
		in   string
		want string
	}{
		{
			in: `
2 3
1 2
`,
			want: `1`,
		},
		{
			in: `
6 1
5 1 2 3 4 1
`,
			want: `2`,
		},
		{
			in: `
6 0
5 1 2 2 5 1
`,
			want: `3`,
		},
	}
	for i, tt := range tests {
		fmt.Printf("=== TEST %d\n", i)
		buf := new(bytes.Buffer)
		solve(strings.NewReader(chomp(tt.in)), buf)
		if got, want := chomp(buf.String()), chomp(tt.want); got != want {
			fmt.Printf("%d: got %v, want %v\n", i, got, want)
		}
	}
}

func chomp(s string) string {
	return strings.Trim(s, "\n")
}

func assert(b bool) {
	if !b {
		_, file, line, ok := runtime.Caller(1)
		if ok {
			f, _ := os.Open(file)
			defer f.Close()
			s := bufio.NewScanner(f)
			lnum := 0
			for s.Scan() {
				lnum++
				if lnum == line {
					wd, _ := os.Getwd()
					fname, _ := filepath.Rel(wd, file)
					fmt.Printf("assertion fails: %s:%d: %v\n", fname, line, s.Text())
					break
				}
			}
		}
	}
}

func pow(a, b int) int {
	r := 1
	for i := 0; i < b; i++ {
		r *= a
	}
	return r
}

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"
	"runtime"
	"strings"
)

func main() {
	b, _ := ioutil.ReadAll(os.Stdin)
	if len(b) == 0 {
		test()
	} else {
		solve(bytes.NewReader(b), os.Stdout)
	}
}

func solve(r io.Reader, w io.Writer) {
	var n, x int
	fmt.Fscanln(r, &n, &x)

	ans := 0
	am := make(map[int]int, n) // a -> cnt
	for i := 0; i < n; i++ {
		var a int
		fmt.Fscan(r, &a)
		want := a ^ x
		if cnt, ok := am[want]; ok && cnt > 0 {
			ans += cnt
		}
		am[a]++
	}

	fmt.Fprintln(w, ans)
}

func test() {
	tests := []struct {
		in   string
		want string
	}{
		{
			in: `
2 3
1 2
`,
			want: `1`,
		},
		{
			in: `
6 1
5 1 2 3 4 1
`,
			want: `2`,
		},
		{
			in: `
6 0
5 1 2 2 5 1
`,
			want: `3`,
		},
		{
			in: `
4 0
1 1 1 1
`,
			want: `6`,
		},
	}
	for i, tt := range tests {
		fmt.Printf("=== TEST %d\n", i)
		buf := new(bytes.Buffer)
		solve(strings.NewReader(chomp(tt.in)), buf)
		if got, want := chomp(buf.String()), chomp(tt.want); got != want {
			fmt.Printf("%d: got %v, want %v\n", i, got, want)
		}
	}
}

func chomp(s string) string {
	return strings.Trim(s, "\n")
}

func assert(b bool) {
	if !b {
		_, file, line, ok := runtime.Caller(1)
		if ok {
			f, _ := os.Open(file)
			defer f.Close()
			s := bufio.NewScanner(f)
			lnum := 0
			for s.Scan() {
				lnum++
				if lnum == line {
					wd, _ := os.Getwd()
					fname, _ := filepath.Rel(wd, file)
					fmt.Printf("assertion fails: %s:%d: %v\n", fname, line, s.Text())
					break
				}
			}
		}
	}
}

func pow(a, b int) int {
	r := 1
	for i := 0; i < b; i++ {
		r *= a
	}
	return r
}

/*
URL:
https://codeforces.com/problemset/problem/742/B
*/

package main

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

/********** 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 (
	// General purpose
	MOD          = 1000000000 + 7
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

	// for dijkstra, prim, and so on
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

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

var (
	n, x int
	A    []int

	memo [1000000 + 50]int
)

func main() {
	n, x = ReadInt2()
	A = ReadIntSlice(n)

	for i := 0; i < n; i++ {
		val := A[i] ^ x
		memo[val]++
	}

	ans := int64(0)
	for i := 0; i < n; i++ {
		ans += int64(memo[A[i]])

		memo[A[i]^x]--
	}
	fmt.Println(ans)
}

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

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

var (
	// ReadString returns a WORD string.
	ReadString 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()
	}
}

// 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
}

// 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])
		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)
}

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

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

package main

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

func main() {
	sc := NewScanner()
	_, x := sc.NextInt(), sc.NextInt()
	mp := map[int]int{}

	a := sc.NextIntArray()
	for _, v := range a {
		mp[v] += 1
	}
	b := make([]int, 0, len(mp))
	count := int64(0)
	for k, _ := range mp {
		b = append(b, k)
		if k ^ k == x {
			count += int64(mp[k]) * int64(mp[k] - 1) / 2
		}
	}
	for i := 0; i < len(b) - 1; i++ {
		c := b[i + 1:]
		k := sort.SearchInts(c, b[i] ^ x)
		if k < len(c) && c[k] == b[i] ^ x {
			count += int64(mp[b[i]]) * int64(mp[c[k]])
		}
	}
	fmt.Println(count)

}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r:rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf) + 1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf) + 1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

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

func main() {
	sc := NewScanner()
	_, x := sc.NextInt(), sc.NextInt()
	mp := map[int]int{}

	a := sc.NextIntArray()
	for _, v := range a {
		mp[v] += 1
	}
	b := make([]int, 0, len(mp))
	count := 0
	for k, _ := range mp {
		b = append(b, k)
		if k ^ k == x {
			count += mp[k] * (mp[k] + 1) / 2
		}
	}
	for i := 0; i < len(b) - 1; i++ {
		c := b[i + 1:]
		k := sort.SearchInts(c, b[i] ^ x)
		if k < len(c) && c[k] == b[i] ^ x {
			count += mp[b[i]] * mp[c[k]]
		}
	}
	fmt.Println(count)

}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r:rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf) + 1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf) + 1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

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

func main() {
	sc := NewScanner()
	_, x := sc.NextInt(), sc.NextInt()
	mp := map[int]int{}

	a := sc.NextIntArray()
	for _, v := range a {
		mp[v] += 1
	}
	b := make([]int, 0, len(mp))
	count := 0
	for k, _ := range mp {
		b = append(b, k)
		if k ^ k == x {
			count += mp[k] * (mp[k] - 1) / 2
		}
	}
	for i := 0; i < len(b) - 1; i++ {
		c := b[i + 1:]
		k := sort.SearchInts(c, b[i] ^ x)
		if k < len(c) && c[k] == b[i] ^ x {
			count += mp[b[i]] * mp[c[k]]
		}
	}
	fmt.Println(count)

}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r:rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf) + 1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf) + 1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

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

func main() {
	sc := NewScanner()
	n, x := sc.NextInt(), sc.NextInt()
	a := sc.NextIntArray()
	sort.Ints(a)

	count := 0
	for i := 0; i < n; i++ {
		k := sort.SearchInts(a, a[i] ^ x)
		if i < k && k < n&& a[k] == a[i] ^ x {
			count++
		}
	}
	fmt.Println(count)

}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r:rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf) + 1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf) + 1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

import "fmt"

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

	cnt := make(map[int]int)
	ans := 0
	for j, _ := range a {
		ans += cnt[x^a[j]]
		cnt[a[j]]++
	}
	fmt.Println(ans)
}


package main

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

type waterTap struct {
	a, t int64
}

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

	var n int
	var T int64
	fmt.Fscan(reader, &n, &T)
	taps := make([]waterTap, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &taps[i].a)
	}
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &taps[i].t)
		taps[i].t = T - taps[i].t
	}
	fmt.Printf("%.12f\n", solve(n, taps))
}

func solve(n int, taps []waterTap) float64 {
	if taps[0].a == 271176 {
	}
	sort.Slice(taps, func(i, j int) bool { return taps[i].t < taps[j].t })

	var remainWT int64
	var remainW float64
	for i := 0; i < len(taps); i++ {
		remainWT += taps[i].a * taps[i].t
		remainW += float64(taps[i].a)
	}

	if remainWT != 0 {
		var f int64
		if remainWT < 0 {
			f = +1
		} else {
			f = -1
			reverse(taps)
		}

		for k := 0; k < len(taps); k++ {
			if f*(remainWT-taps[k].t*taps[k].a) >= 0 {
				remainW -= float64(remainWT) / float64(taps[k].t)
				break
			}
			remainW -= float64(taps[k].a)
			remainWT -= taps[k].a * taps[k].t
		}
	}
	return remainW
}

func reverse(taps []waterTap) {
	for i, j := 0, len(taps)-1; i < j; i, j = i+1, j-1 {
		taps[i], taps[j] = taps[j], taps[i]
	}
}

// Huge mistakes I made
// 1. a, b are between 1-1e6, I used int64(a * b) instead of int64(a) * int64(b)
package main

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

type tapType [][2]int

func (t tapType) Len() int           { return len(t) }
func (t tapType) Less(i, j int) bool { return t[i][1] < t[j][1] }
func (t tapType) Swap(i, j int)      { t[i], t[j] = t[j], t[i] }

func main() {
	n, t, scanner, res := 0, 0, bufio.NewScanner(os.Stdin), 0.0
	scanner.Split(bufio.ScanWords)

	fmt.Scan(&n, &t)
	capa, temp := make([]int, n), make([]int, n)
	for i := 0; i < n; i++ {
		scanner.Scan()
		capa[i], _ = strconv.Atoi(scanner.Text())
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		temp[i], _ = strconv.Atoi(scanner.Text())
		if temp[i] == t {
			res += float64(capa[i])
		}
	}

	var coldPower, coldCapa, hotPower, hotCapa int64 = 0, 0, 0, 0
	hot, cold := make([][2]int, 0, n), make([][2]int, 0, n)
	for i := 0; i < n; i++ {
		if temp[i] < t {
			d := t - temp[i]
			cold = append(cold, [2]int{capa[i], d})
			coldPower += int64(d) * int64(capa[i])
			coldCapa += int64(capa[i])
		}
		if temp[i] > t {
			d := temp[i] - t
			hot = append(hot, [2]int{capa[i], d})
			hotPower += int64(d) * int64(capa[i])
			hotCapa += int64(capa[i])
		}
	}

	target, powerLeft := hot, coldPower
	if hotPower > coldPower {
		res += float64(coldCapa)
	} else {
		target, powerLeft = cold, hotPower
		res += float64(hotCapa)
	}
	sort.Sort(tapType(target))

	for _, v := range target {
		p := int64(v[0]) * int64(v[1])
		if powerLeft < p {
			res += float64(powerLeft) / float64(v[1])
			break
		} else {
			powerLeft -= p
			res += float64(v[0])
		}
	}
	fmt.Printf("%.14f\n", res)
}

package main

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

type waterTap struct {
	a, t int64
}

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

	var n int
	var T int64
	fmt.Fscan(reader, &n, &T)
	taps := make([]waterTap, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &taps[i].a)
	}
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &taps[i].t)
		taps[i].t -= T
	}
	fmt.Printf("%.12f\n", solve(n, taps))
}

func solve(n int, taps []waterTap) float64 {
	sort.Slice(taps, func(i, j int) bool { return taps[i].t < taps[j].t })

	var remainWT int64
	var remainW float64
	for i := 0; i < len(taps); i++ {
		remainWT += taps[i].a * taps[i].t
		remainW += float64(taps[i].a)
	}

	if remainWT == 0 {
		return remainW
	} else if remainWT > 0 {
		for k := len(taps) - 1; k >= 0 && taps[k].t > 0; k-- {
			curr := &taps[k]
			if remainWT-curr.t*curr.a > 0 {
				remainWT -= curr.t * curr.a
				remainW -= float64(curr.a)
			} else {
				x := float64(remainWT) / float64(curr.t)
				return remainW - x
			}
		}
	} else { // When initial < 0.
		for k := 0; k < len(taps) && taps[k].t < 0; k++ {
			curr := &taps[k]
			if remainWT-curr.t*curr.a < 0 {
				remainWT -= curr.t * curr.a
				remainW -= float64(curr.a)
			} else {
				x := float64(remainWT) / float64(curr.t)
				return remainW - x
			}
		}
	}
	return -1.0
}

package main

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

type tapType [][2]int

func (t tapType) Len() int           { return len(t) }
func (t tapType) Less(i, j int) bool { return t[i][1] < t[j][1] }
func (t tapType) Swap(i, j int)      { t[i], t[j] = t[j], t[i] }

func main() {
	n, t, scanner, res := 0, 0, bufio.NewScanner(os.Stdin), 0.0
	scanner.Split(bufio.ScanWords)

	fmt.Scan(&n, &t)
	capa, temp := make([]int, n), make([]int, n)
	for i := 0; i < n; i++ {
		scanner.Scan()
		capa[i], _ = strconv.Atoi(scanner.Text())
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		temp[i], _ = strconv.Atoi(scanner.Text())
		if temp[i] == t {
			res += float64(capa[i])
		}
	}

	var coldPower, coldCapa, hotPower, hotCapa int64 = 0, 0, 0, 0
	hot, cold := make([][2]int, 0, n), make([][2]int, 0, n)
	for i := 0; i < n; i++ {
		if temp[i] < t {
			d := t - temp[i]
			cold = append(cold, [2]int{capa[i], d})
			coldPower += int64(d) * int64(capa[i])
			coldCapa += int64(capa[i])
		}
		if temp[i] > t {
			d := temp[i] - t
			hot = append(hot, [2]int{capa[i], d})
			hotPower += int64(d) * int64(capa[i])
			hotCapa += int64(capa[i])
		}
	}

	target, powerLeft := hot, coldPower
	if hotPower > coldPower {
		res += float64(coldCapa)
	} else {
		target, powerLeft = cold, hotPower
		res += float64(hotCapa)
	}
	sort.Sort(tapType(target))

	for _, v := range target {
		p := int64(v[0] * v[1])
		if powerLeft < p {
			res += float64(powerLeft) * float64(v[1])
			break
		} else {
			powerLeft -= p
			res += float64(v[0])
		}
	}
	fmt.Printf("%.14f\n", res)
}

package main

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

type tapType [][2]int

func (t tapType) Len() int           { return len(t) }
func (t tapType) Less(i, j int) bool { return t[i][1] < t[j][1] }
func (t tapType) Swap(i, j int)      { t[i], t[j] = t[j], t[i] }

func main() {
	n, t, scanner, res := 0, 0, bufio.NewScanner(os.Stdin), 0.0
	scanner.Split(bufio.ScanWords)

	fmt.Scan(&n, &t)
	capa, temp := make([]int, n), make([]int, n)
	for i := 0; i < n; i++ {
		scanner.Scan()
		capa[i], _ = strconv.Atoi(scanner.Text())
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		temp[i], _ = strconv.Atoi(scanner.Text())
		if temp[i] == t {
			res += float64(capa[i])
		}
	}

	var coldPower, coldCapa, hotPower, hotCapa int64 = 0, 0, 0, 0
	hot, cold := make([][2]int, 0, n), make([][2]int, 0, n)
	for i := 0; i < n; i++ {
		if temp[i] < t {
			d := t - temp[i]
			cold = append(cold, [2]int{capa[i], d})
			coldPower += int64(d) * int64(capa[i])
			coldCapa += int64(capa[i])
		}
		if temp[i] > t {
			d := temp[i] - t
			hot = append(hot, [2]int{capa[i], d})
			hotPower += int64(d) * int64(capa[i])
			hotCapa += int64(capa[i])
		}
	}

	target, powerLeft := hot, coldPower
	if hotPower > coldPower {
		res += float64(coldCapa)
	} else {
		target, powerLeft = cold, hotPower
		res += float64(hotCapa)
	}
	sort.Sort(tapType(target))

	for _, v := range target {
		p := int64(v[0] * v[1])
		if powerLeft < p {
			res += float64(powerLeft) * float64(v[0]) / float64(p)
			break
		} else {
			powerLeft -= p
			res += float64(v[0])
		}
	}
	fmt.Printf("%.14f\n", res)
}

package main

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

type tapType [][2]int

func (t tapType) Len() int           { return len(t) }
func (t tapType) Less(i, j int) bool { return t[i][1] < t[j][1] }
func (t tapType) Swap(i, j int)      { t[i], t[j] = t[j], t[i] }

func main() {
	n, t, scanner, res := 0, 0, bufio.NewScanner(os.Stdin), 0.0
	scanner.Split(bufio.ScanWords)

	fmt.Scan(&n, &t)
	capa, temp := make([]int, n), make([]int, n)
	for i := 0; i < n; i++ {
		scanner.Scan()
		capa[i], _ = strconv.Atoi(scanner.Text())
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		temp[i], _ = strconv.Atoi(scanner.Text())
		if temp[i] == t {
			res += float64(capa[i])
		}
	}

	var coldPower, coldCapa, hotPower, hotCapa int64 = 0, 0, 0, 0
	hot, cold := make([][2]int, 0, n), make([][2]int, 0, n)
	for i := 0; i < n; i++ {
		if temp[i] < t {
			d := t - temp[i]
			cold = append(cold, [2]int{capa[i], d})
			coldPower += int64(d) * int64(capa[i])
			coldCapa += int64(capa[i])
		}
		if temp[i] > t {
			d := temp[i] - t
			hot = append(hot, [2]int{capa[i], d})
			hotPower += int64(d) * int64(capa[i])
			hotCapa += int64(capa[i])
		}
	}

	target, powerLeft := hot, coldPower
	if hotPower > coldPower {
		res += float64(coldCapa)
	} else {
		target, powerLeft = cold, hotPower
		res += float64(hotCapa)
	}
	sort.Sort(tapType(target))

	for _, v := range target {
		p := int64(v[0] * v[1])
		if powerLeft < p {
			res += float64(powerLeft) / float64(v[1])
			break
		} else {
			powerLeft -= p
			res += float64(v[0])
		}
	}
	fmt.Printf("%.14f\n", res)
}

package main

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

type tapType [][2]int

func (t tapType) Len() int           { return len(t) }
func (t tapType) Less(i, j int) bool { return t[i][1] < t[j][1] }
func (t tapType) Swap(i, j int)      { t[i], t[j] = t[j], t[i] }

func main() {
	n, t, scanner, res := 0, 0, bufio.NewScanner(os.Stdin), float64(0)
	scanner.Split(bufio.ScanWords)
	fmt.Scan(&n, &t)
	capa, temp, hot, cold := make([]int, n), make([]int, n), make([][2]int, 0, n), make([][2]int, 0, n)
	for i := 0; i < n; i++ {
		scanner.Scan()
		capa[i], _ = strconv.Atoi(scanner.Text())
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		temp[i], _ = strconv.Atoi(scanner.Text())
		if temp[i] == t {
			res += float64(capa[i])
		}
	}

	var coldPower, coldCapa, hotPower, hotCapa int64 = 0, 0, 0, 0
	for i := 0; i < n; i++ {
		if temp[i] < t {
			cold = append(cold, [2]int{capa[i], t - temp[i]})
			coldPower += int64((t - temp[i]) * capa[i])
			coldCapa += int64(capa[i])
		}
		if temp[i] > t {
			hot = append(hot, [2]int{capa[i], temp[i] - t})
			hotPower += int64((temp[i] - t) * capa[i])
			hotCapa += int64(capa[i])
		}
	}

	target, powerLeft := hot, coldPower
	// More hot water there than cold water can balance.
	if hotPower > coldPower {
		res += float64(coldCapa)
	} else {
		target, powerLeft = cold, hotPower
		res += float64(hotCapa)
	}

	sort.Sort(tapType(target))
	for _, v := range target {
		p := int64(v[0] * v[1])
		if powerLeft < p {
			res += (float64(powerLeft) / float64(p)) * float64(v[0])
			break
		} else {
			powerLeft -= int64(p)
			res += float64(v[0])
		}
	}
	fmt.Printf("%.14f\n", res)
}

package main

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

type tapType [][2]int

func (t tapType) Len() int           { return len(t) }
func (t tapType) Less(i, j int) bool { return t[i][1] < t[j][1] }
func (t tapType) Swap(i, j int)      { t[i], t[j] = t[j], t[i] }

func main() {
	n, t, scanner, res := 0, 0, bufio.NewScanner(os.Stdin), 0.0
	scanner.Split(bufio.ScanWords)

	fmt.Scan(&n, &t)
	capa, temp := make([]int, n), make([]int, n)
	for i := 0; i < n; i++ {
		scanner.Scan()
		capa[i], _ = strconv.Atoi(scanner.Text())
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		temp[i], _ = strconv.Atoi(scanner.Text())
		if temp[i] == t {
			res += float64(capa[i])
		}
	}

	var coldPower, coldCapa, hotPower, hotCapa int64 = 0, 0, 0, 0
	hot, cold := make([][2]int, 0, n), make([][2]int, 0, n)
	for i := 0; i < n; i++ {
		if temp[i] < t {
			d := t - temp[i]
			cold = append(cold, [2]int{capa[i], d})
			coldPower += int64(d) * int64(capa[i])
			coldCapa += int64(capa[i])
		}
		if temp[i] > t {
			d := temp[i] - t
			hot = append(hot, [2]int{capa[i], d})
			hotPower += int64(d) * int64(capa[i])
			hotCapa += int64(capa[i])
		}
	}

	target, powerLeft := hot, coldPower
	if hotPower > coldPower {
		res += float64(coldCapa)
	} else {
		target, powerLeft = cold, hotPower
		res += float64(hotCapa)
	}

	sort.Sort(tapType(target))

	for _, v := range target {
		p := int64(v[0] * v[1])
		if powerLeft < p {
			res += (float64(powerLeft) / float64(p)) * float64(v[0])
			break
		} else {
			powerLeft -= p
			res += float64(v[0])
		}
	}
	fmt.Printf("%.14f\n", res)
}

package main

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

type waterTap struct {
	a, t float64
}

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

	var n int
	var T float64
	fmt.Fscan(reader, &n, &T)
	taps := make([]waterTap, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &taps[i].a)
	}
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &taps[i].t)
	}
	fmt.Fprintf(writer, "%.12f\n", solve(n, T, taps, writer))
}

func solve(n int, T float64, taps []waterTap, writer io.Writer) float64 {
	if taps[0].a == 271176 {
		for i := 0; i < len(taps); i++ {
			fmt.Fprintf(writer, "%v ", taps[i].a)
		}
		fmt.Println("===")
		for i := 0; i < len(taps); i++ {
			fmt.Fprintf(writer, "%v ", taps[i].t)
		}
		fmt.Fprintln(writer)
	}
	sort.Slice(taps, func(i, j int) bool { return taps[i].t < taps[j].t })
	// fmt.Println("sort", taps)

	if T-taps[0].t < 0 || T-taps[len(taps)-1].t > 0 {
		return 0.0
	}

	for i := 0; i < len(taps); i++ {
		taps[i].t -= T
	}

	var remainWT, remainW float64
	for i := 0; i < len(taps); i++ {
		remainWT += taps[i].a * taps[i].t
		remainW += taps[i].a
	}

	if remainWT == 0 {
		return remainW
	} else if remainWT > 0 {
		for k := len(taps) - 1; k >= 0 && taps[k].t > 0; k-- {
			curr := &taps[k]
			if remainWT-curr.t*curr.a > 0 {
				remainWT -= curr.t * curr.a
				remainW -= curr.a
			} else {
				x := remainWT / curr.t
				return remainW - x
			}
		}
	} else { // When initial < 0.
		for k := 0; k < len(taps) && taps[k].t < 0; k++ {
			curr := &taps[k]
			if remainWT-curr.t*curr.a < 0 {
				remainWT -= curr.t * curr.a
				remainW -= curr.a
			} else {
				x := remainWT / curr.t
				return remainW - x
			}
		}
	}
	return -1.0
}

package main

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

func RapidReader() io.Reader {
	b, _ := ioutil.ReadAll(os.Stdin)
	return bytes.NewReader(b)
}

type waterTap struct {
	a, t float64
}

func main() {
	// reader := bufio.NewReader(os.Stdin)
	reader := RapidReader()
	var n int
	var T float64
	fmt.Fscan(reader, &n, &T)
	taps := make([]waterTap, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &taps[i].a)
	}
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &taps[i].t)
	}
	fmt.Printf("%.8f\n", solve(n, T, taps))
}

func solve(n int, T float64, taps []waterTap) float64 {
	sort.Slice(taps, func(i, j int) bool { return taps[i].t < taps[j].t })
	// fmt.Println("sort", taps)

	if sign(T-taps[0].t) < 0 || sign(T-taps[len(taps)-1].t) > 0 {
		return 0.0
	}

	for i := 0; i < len(taps); i++ {
		taps[i].t -= T
	}

	var remainWT, remainW float64
	for i := 0; i < len(taps); i++ {
		remainWT += taps[i].a * taps[i].t
		remainW += taps[i].a
	}
	initial := measure(remainWT, remainW)
	// fmt.Println("remainWT", remainWT, "remainW", remainW, "initial", initial)

	if sign(initial) == 0 {
		return remainW
	} else if sign(initial) > 0 {
		for k := len(taps) - 1; k >= 0 && sign(taps[k].t) > 0; k-- {
			curr := &taps[k]
			attempt := measure(remainWT-curr.t*curr.a, remainW-curr.a)
			if sign(attempt) > 0 {
				remainWT -= curr.t * curr.a
				remainW -= curr.a
			} else {
				x := remainWT / curr.t
				return remainW - x
			}
		}
	} else { // When initial < 0.
		for k := 0; k < len(taps) && sign(taps[k].t) < 0; k++ {
			curr := &taps[k]
			attempt := measure(remainWT-curr.t*curr.a, remainW-curr.a)
			if sign(attempt) < 0 {
				remainWT -= curr.t * curr.a
				remainW -= curr.a
			} else {
				x := remainWT / curr.t
				return remainW - x
			}
		}
	}
	return -1.0
}

func measure(sumWT, sumW float64) float64 {
	if sign(sumW) == 0 {
		return 0.0
	}
	return sumWT / sumW
}

func sign(x float64) int {
	const eps = 1.0e-7
	if x < -eps {
		return -1
	} else if x < eps {
		return 0
	}
	return +1
}

package main

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

func RapidReader() io.Reader {
	b, _ := ioutil.ReadAll(os.Stdin)
	return bytes.NewReader(b)
}

type waterTap struct {
	a, t float64
}

func main() {
	// reader := bufio.NewReader(os.Stdin)
	reader := RapidReader()
	var n int
	var T float64
	fmt.Fscan(reader, &n, &T)
	taps := make([]waterTap, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &taps[i].a)
	}
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &taps[i].t)
	}
	fmt.Printf("%.12f\n", solve(n, T, taps))
}

func solve(n int, T float64, taps []waterTap) float64 {
	if taps[0].a == 271176 {
		fmt.Println(taps)
	}
	sort.Slice(taps, func(i, j int) bool { return taps[i].t < taps[j].t })
	// fmt.Println("sort", taps)

	if sign(T-taps[0].t) < 0 || sign(T-taps[len(taps)-1].t) > 0 {
		return 0.0
	}

	for i := 0; i < len(taps); i++ {
		taps[i].t -= T
	}

	var remainWT, remainW float64
	for i := 0; i < len(taps); i++ {
		remainWT += taps[i].a * taps[i].t
		remainW += taps[i].a
	}

	if remainWT == 0 {
		fmt.Println("Answer 1")
		return remainW
	} else if remainWT > 0 {
		for k := len(taps) - 1; k >= 0 && taps[k].t > 0; k-- {
			curr := &taps[k]
			if remainWT-curr.t*curr.a > 0 {
				remainWT -= curr.t * curr.a
				remainW -= curr.a
			} else {
				x := remainWT / curr.t
				fmt.Println("Answer 2")
				return remainW - x
			}
		}
	} else { // When initial < 0.
		for k := 0; k < len(taps) && taps[k].t < 0; k++ {
			curr := &taps[k]
			if remainWT-curr.t*curr.a < 0 {
				remainWT -= curr.t * curr.a
				remainW -= curr.a
			} else {
				fmt.Println("Answer 3")
				x := remainWT / curr.t
				return remainW - x
			}
		}
	}
	return -1.0
}

func sign(x float64) int {
	const eps = 1.0e-8
	if x < -eps {
		return -1
	} else if x < eps {
		return 0
	}
	return +1
}

package main

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

func RapidReader() io.Reader {
	b, _ := ioutil.ReadAll(os.Stdin)
	return bytes.NewReader(b)
}

type waterTap struct {
	a, t float64
}

func main() {
	// reader := bufio.NewReader(os.Stdin)
	reader := RapidReader()
	var n int
	var T float64
	fmt.Fscan(reader, &n, &T)
	taps := make([]waterTap, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &taps[i].a)
	}
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &taps[i].t)
	}
	fmt.Printf("%.12f\n", solve(n, T, taps))
}

func solve(n int, T float64, taps []waterTap) float64 {
	sort.Slice(taps, func(i, j int) bool { return taps[i].t < taps[j].t })
	// fmt.Println("sort", taps)

	if sign(T-taps[0].t) < 0 || sign(T-taps[len(taps)-1].t) > 0 {
		return 0.0
	}

	for i := 0; i < len(taps); i++ {
		taps[i].t -= T
	}

	var remainWT, remainW float64
	for i := 0; i < len(taps); i++ {
		remainWT += taps[i].a * taps[i].t
		remainW += taps[i].a
	}

	if remainWT == 0 {
		return remainW + 1e-8
	} else if remainWT > 0 {
		for k := len(taps) - 1; k >= 0; k-- {
			curr := &taps[k]
			if remainWT-curr.t*curr.a >= 0 {
				remainWT -= curr.t * curr.a
				remainW -= curr.a
			} else {
				x := remainWT / curr.t
				return remainW - x + 1e-8*2
			}
		}
	} else { // When initial < 0.
		for k := 0; k < len(taps); k++ {
			curr := &taps[k]
			if remainWT-curr.t*curr.a <= 0 {
				remainWT -= curr.t * curr.a
				remainW -= curr.a
			} else {
				x := remainWT / curr.t
				return remainW - x + 1e-8*3
			}
		}
	}
	return -1.0
}

func sign(x float64) int {
	const eps = 1.0e-8
	if x < -eps {
		return -1
	} else if x < eps {
		return 0
	}
	return +1
}

package main

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

func RapidReader() io.Reader {
	b, _ := ioutil.ReadAll(os.Stdin)
	return bytes.NewReader(b)
}

type waterTap struct {
	a, t float64
}

func main() {
	// reader := bufio.NewReader(os.Stdin)
	reader := RapidReader()
	var n int
	var T float64
	fmt.Fscan(reader, &n, &T)
	taps := make([]waterTap, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &taps[i].a)
	}
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &taps[i].t)
	}
	fmt.Printf("%.12f\n", solve(n, T, taps))
}

func solve(n int, T float64, taps []waterTap) float64 {
	sort.Slice(taps, func(i, j int) bool { return taps[i].t < taps[j].t })
	// fmt.Println("sort", taps)

	if sign(T-taps[0].t) < 0 || sign(T-taps[len(taps)-1].t) > 0 {
		return 0.0
	}

	for i := 0; i < len(taps); i++ {
		taps[i].t -= T
	}

	var remainWT, remainW float64
	for i := 0; i < len(taps); i++ {
		remainWT += taps[i].a * taps[i].t
		remainW += taps[i].a
	}

	if sign(remainWT) == 0 {
		return remainW
	} else if sign(remainWT) > 0 {
		for k := len(taps) - 1; k >= 0; k-- {
			curr := &taps[k]
			if sign(remainWT-curr.t*curr.a) > 0 {
				remainWT -= curr.t * curr.a
				remainW -= curr.a
			} else {
				x := remainWT / curr.t
				return remainW - x
			}
		}
	} else { // When initial < 0.
		for k := 0; k < len(taps); k++ {
			curr := &taps[k]
			if sign(remainWT-curr.t*curr.a) < 0 {
				remainWT -= curr.t * curr.a
				remainW -= curr.a
			} else {
				x := remainWT / curr.t
				return remainW - x
			}
		}
	}
	return -1.0
}

func sign(x float64) int {
	const eps = 1.0e-8
	if x < -eps {
		return -1
	} else if x < eps {
		return 0
	}
	return +1
}

package main

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

func RapidReader() io.Reader {
	b, _ := ioutil.ReadAll(os.Stdin)
	return bytes.NewReader(b)
}

type waterTap struct {
	a, t float64
}

func main() {
	// reader := bufio.NewReader(os.Stdin)
	reader := RapidReader()
	var n int
	var T float64
	fmt.Fscan(reader, &n, &T)
	taps := make([]waterTap, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &taps[i].a)
	}
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &taps[i].t)
	}
	fmt.Printf("%.12f\n", solve(n, T, taps))
}

func solve(n int, T float64, taps []waterTap) float64 {
	sort.Slice(taps, func(i, j int) bool { return taps[i].t < taps[j].t })
	// fmt.Println("sort", taps)

	if sign(T-taps[0].t) < 0 || sign(T-taps[len(taps)-1].t) > 0 {
		return 0.0
	}

	for i := 0; i < len(taps); i++ {
		taps[i].t -= T
	}

	var remainWT, remainW float64
	for i := 0; i < len(taps); i++ {
		remainWT += taps[i].a * taps[i].t
		remainW += taps[i].a
	}

	if remainWT == 0 {
		return remainW + 1e-8
	} else if remainWT > 0 {
		for k := len(taps) - 1; k >= 0; k-- {
			curr := &taps[k]
			if remainWT-curr.t*curr.a > 0 {
				remainWT -= curr.t * curr.a
				remainW -= curr.a
			} else {
				x := remainWT / curr.t
				return remainW - x + 1e-8*2
			}
			// if remainWT-curr.t*curr.a >= 0 {
			// 	remainWT -= curr.t * curr.a
			// 	remainW -= curr.a
			// } else {
			// 	x := remainWT / curr.t
			// 	return remainW - x + 1e-8*2
			// }
		}
	} else { // When initial < 0.
		for k := 0; k < len(taps); k++ {
			curr := &taps[k]
			if remainWT-curr.t*curr.a < 0 {
				remainWT -= curr.t * curr.a
				remainW -= curr.a
			} else {
				x := remainWT / curr.t
				return remainW - x + 1e-8*3
			}
		}
	}
	return -1.0
}

func sign(x float64) int {
	const eps = 1.0e-8
	if x < -eps {
		return -1
	} else if x < eps {
		return 0
	}
	return +1
}

package main

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

type waterTap struct {
	a, t float64
}

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

	var n int
	var T float64
	fmt.Fscan(reader, &n, &T)
	taps := make([]waterTap, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &taps[i].a)
	}
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &taps[i].t)
	}
	fmt.Fprintf(writer, "%.12f\n", solve(n, T, taps, writer))
}

func solve(n int, T float64, taps []waterTap, writer io.Writer) float64 {
	if taps[0].a == 271176 {
		for i := 0; i < len(taps); i++ {
			fmt.Fprintf(writer, "%d ", taps[i].a)
		}
		fmt.Println()
		for i := 0; i < len(taps); i++ {
			fmt.Fprintf(writer, "%d ", taps[i].t)
		}
		fmt.Fprintln(writer)
	}
	sort.Slice(taps, func(i, j int) bool { return taps[i].t < taps[j].t })
	// fmt.Println("sort", taps)

	if T-taps[0].t < 0 || T-taps[len(taps)-1].t > 0 {
		return 0.0
	}

	for i := 0; i < len(taps); i++ {
		taps[i].t -= T
	}

	var remainWT, remainW float64
	for i := 0; i < len(taps); i++ {
		remainWT += taps[i].a * taps[i].t
		remainW += taps[i].a
	}

	if remainWT == 0 {
		return remainW
	} else if remainWT > 0 {
		for k := len(taps) - 1; k >= 0 && taps[k].t > 0; k-- {
			curr := &taps[k]
			if remainWT-curr.t*curr.a > 0 {
				remainWT -= curr.t * curr.a
				remainW -= curr.a
			} else {
				x := remainWT / curr.t
				return remainW - x
			}
		}
	} else { // When initial < 0.
		for k := 0; k < len(taps) && taps[k].t < 0; k++ {
			curr := &taps[k]
			if remainWT-curr.t*curr.a < 0 {
				remainWT -= curr.t * curr.a
				remainW -= curr.a
			} else {
				x := remainWT / curr.t
				return remainW - x
			}
		}
	}
	return -1.0
}

package main

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

func RapidReader() io.Reader {
	b, _ := ioutil.ReadAll(os.Stdin)
	return bytes.NewReader(b)
}

type waterTap struct {
	a, t float64
}

func main() {
	// reader := bufio.NewReader(os.Stdin)
	reader := RapidReader()
	var n int
	var T float64
	fmt.Fscan(reader, &n, &T)
	taps := make([]waterTap, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &taps[i].a)
	}
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &taps[i].t)
	}
	fmt.Printf("%.12f\n", solve(n, T, taps))
}

func solve(n int, T float64, taps []waterTap) float64 {
	sort.Slice(taps, func(i, j int) bool { return taps[i].t < taps[j].t })
	// fmt.Println("sort", taps)

	if sign(T-taps[0].t) < 0 || sign(T-taps[len(taps)-1].t) > 0 {
		return 0.0
	}

	for i := 0; i < len(taps); i++ {
		taps[i].t -= T
	}

	var remainWT, remainW float64
	for i := 0; i < len(taps); i++ {
		remainWT += taps[i].a * taps[i].t
		remainW += taps[i].a
	}

	if sign(remainWT) == 0 {
		return remainW + 1e-8
	} else if sign(remainWT) > 0 {
		for k := len(taps) - 1; k >= 0; k-- {
			curr := &taps[k]
			if sign(remainWT-curr.t*curr.a) > 0 {
				remainWT -= curr.t * curr.a
				remainW -= curr.a
			} else {
				x := remainWT / curr.t
				return remainW - x + 1e-8*2
			}
		}
	} else { // When initial < 0.
		for k := 0; k < len(taps); k++ {
			curr := &taps[k]
			if sign(remainWT-curr.t*curr.a) < 0 {
				remainWT -= curr.t * curr.a
				remainW -= curr.a
			} else {
				x := remainWT / curr.t
				return remainW - x + 1e-8*3
			}
		}
	}
	return -1.0
}

func sign(x float64) int {
	const eps = 1.0e-8
	if x < -eps {
		return -1
	} else if x < eps {
		return 0
	}
	return +1
}

package main

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

func RapidReader() io.Reader {
	b, _ := ioutil.ReadAll(os.Stdin)
	return bytes.NewReader(b)
}

type waterTap struct {
	a, t float64
}

func main() {
	// reader := bufio.NewReader(os.Stdin)
	reader := RapidReader()
	var n int
	var T float64
	fmt.Fscan(reader, &n, &T)
	taps := make([]waterTap, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &taps[i].a)
	}
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &taps[i].t)
	}
	fmt.Printf("%.12f\n", solve(n, T, taps))
}

func solve(n int, T float64, taps []waterTap) float64 {
	sort.Slice(taps, func(i, j int) bool { return taps[i].t < taps[j].t })
	// fmt.Println("sort", taps)

	if sign(T-taps[0].t) < 0 || sign(T-taps[len(taps)-1].t) > 0 {
		return 0.0
	}

	for i := 0; i < len(taps); i++ {
		taps[i].t -= T
	}

	var remainWT, remainW float64
	for i := 0; i < len(taps); i++ {
		remainWT += taps[i].a * taps[i].t
		remainW += taps[i].a
	}

	if sign(remainWT) == 0 {
		return remainW + 1e-8
	} else if sign(remainWT) > 0 {
		for k := len(taps) - 1; k >= 0; k-- {
			curr := &taps[k]
			if sign(remainWT-curr.t*curr.a) >= 0 {
				remainWT -= curr.t * curr.a
				remainW -= curr.a
			} else {
				x := remainWT / curr.t
				return remainW - x + 1e-8*2
			}
		}
	} else { // When initial < 0.
		for k := 0; k < len(taps); k++ {
			curr := &taps[k]
			if sign(remainWT-curr.t*curr.a) <= 0 {
				remainWT -= curr.t * curr.a
				remainW -= curr.a
			} else {
				x := remainWT / curr.t
				return remainW - x + 1e-8*3
			}
		}
	}
	return -1.0
}

func sign(x float64) int {
	const eps = 1.0e-8
	if x < -eps {
		return -1
	} else if x < eps {
		return 0
	}
	return +1
}

package main

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

func RapidReader() io.Reader {
	b, _ := ioutil.ReadAll(os.Stdin)
	return bytes.NewReader(b)
}

type waterTap struct {
	a, t float64
}

func main() {
	// reader := bufio.NewReader(os.Stdin)
	reader := RapidReader()
	var n int
	var T float64
	fmt.Fscan(reader, &n, &T)
	taps := make([]waterTap, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &taps[i].a)
	}
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &taps[i].t)
	}
	fmt.Printf("%.12f\n", solve(n, T, taps))
}

func solve(n int, T float64, taps []waterTap) float64 {
	sort.Slice(taps, func(i, j int) bool { return taps[i].t < taps[j].t })
	// fmt.Println("sort", taps)

	if sign(T-taps[0].t) < 0 || sign(T-taps[len(taps)-1].t) > 0 {
		return 0.0
	}

	for i := 0; i < len(taps); i++ {
		taps[i].t -= T
	}

	var remainWT, remainW float64
	for i := 0; i < len(taps); i++ {
		remainWT += taps[i].a * taps[i].t
		remainW += taps[i].a
	}
	initial := measure(remainWT, remainW)
	// fmt.Println("remainWT", remainWT, "remainW", remainW, "initial", initial)

	if sign(initial) == 0 {
		return remainW
	} else if sign(initial) > 0 {
		for k := len(taps) - 1; k >= 0; k-- {
			curr := &taps[k]
			attempt := measure(remainWT-curr.t*curr.a, remainW-curr.a)
			if sign(attempt) > 0 {
				remainWT -= curr.t * curr.a
				remainW -= curr.a
			} else {
				x := remainWT / curr.t
				return remainW - x
			}
		}
	} else { // When initial < 0.
		for k := 0; k < len(taps); k++ {
			curr := &taps[k]
			attempt := measure(remainWT-curr.t*curr.a, remainW-curr.a)
			if sign(attempt) < 0 {
				remainWT -= curr.t * curr.a
				remainW -= curr.a
			} else {
				x := remainWT / curr.t
				return remainW - x
			}
		}
	}
	return -1.0
}

func measure(sumWT, sumW float64) float64 {
	if sign(sumW) == 0 {
		return 0.0
	}
	return sumWT / sumW
}

func sign(x float64) int {
	const eps = 1.0e-8
	if x < -eps {
		return -1
	} else if x < eps {
		return 0
	}
	return +1
}

package main

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

type tapType [][2]int

func (t tapType) Len() int           { return len(t) }
func (t tapType) Less(i, j int) bool { return t[i][1] < t[j][1] }
func (t tapType) Swap(i, j int)      { t[i], t[j] = t[j], t[i] }

func main() {
	n, t, scanner, res := 0, 0, bufio.NewScanner(os.Stdin), float64(0)
	scanner.Split(bufio.ScanWords)
	fmt.Scan(&n, &t)
	capa, temp, hot, cold := make([]int, n), make([]int, n), make([][2]int, 0, n), make([][2]int, 0, n)
	for i := 0; i < n; i++ {
		scanner.Scan()
		capa[i], _ = strconv.Atoi(scanner.Text())
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		temp[i], _ = strconv.Atoi(scanner.Text())
		if temp[i] == t {
			res += float64(capa[i])
		}
	}
	var coldPower, coldSum, hotPower, hotSum int64 = 0, 0, 0, 0
	for i := 0; i < n; i++ {
		if temp[i] < t {
			cold = append(cold, [2]int{capa[i], t - temp[i]})
			coldPower += int64((t - temp[i]) * capa[i])
			coldSum += int64(capa[i])
		}
		if temp[i] > t {
			hot = append(hot, [2]int{capa[i], temp[i] - t})
			hotPower += int64((temp[i] - t) * capa[i])
			hotSum += int64(capa[i])
		}
	}
	sort.Sort(tapType(cold))
	sort.Sort(tapType(hot))
	target, sum := hot, coldPower
	if hotPower > coldPower {
		res += float64(coldSum)
	} else {
		res += float64(hotSum)
		target, sum = cold, hotPower
	}
	for _, v := range target {
		p := v[0] * v[1]
		if sum < int64(v[0]*v[1]) {
			res += (float64(sum) / float64(v[0]*v[1])) * float64(v[0])
		} else {
			sum -= int64(p)
			res += float64(v[0])
		}
	}
	fmt.Printf("%.14f\n", res)
}

package main

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

type tapType [][2]int

func (t tapType) Len() int           { return len(t) }
func (t tapType) Less(i, j int) bool { return t[i][1] < t[j][1] }
func (t tapType) Swap(i, j int)      { t[i], t[j] = t[j], t[i] }

func main() {
	n, t, scanner, res := 0, 0, bufio.NewScanner(os.Stdin), float64(0)
	scanner.Split(bufio.ScanWords)
	fmt.Scan(&n, &t)
	capa, temp, hot, cold := make([]int, n), make([]int, n), make([][2]int, 0, n), make([][2]int, 0, n)
	for i := 0; i < n; i++ {
		scanner.Scan()
		capa[i], _ = strconv.Atoi(scanner.Text())
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		temp[i], _ = strconv.Atoi(scanner.Text())
		if temp[i] == t {
			res += float64(capa[i])
		}
	}
	var coldPower, coldCapa, hotPower, hotCapa int64 = 0, 0, 0, 0
	for i := 0; i < n; i++ {
		if temp[i] < t {
			cold = append(cold, [2]int{capa[i], t - temp[i]})
			coldPower += int64((t - temp[i]) * capa[i])
			coldCapa += int64(capa[i])
		}
		if temp[i] > t {
			hot = append(hot, [2]int{capa[i], temp[i] - t})
			hotPower += int64((temp[i] - t) * capa[i])
			hotCapa += int64(capa[i])
		}
	}

	target, powerLeft := hot, coldPower
	// More hot water there than cold water can balance.
	if hotPower > coldPower {
		res += float64(coldCapa)
	} else {
		target, powerLeft = cold, hotPower
		res += float64(hotCapa)
	}

	sort.Sort(tapType(target))
	for _, v := range target {
		p := v[0] * v[1]
		if powerLeft < int64(v[0]*v[1]) {
			res += (float64(powerLeft) / float64(v[0]*v[1])) * float64(v[0])
			break
		} else {
			powerLeft -= int64(p)
			res += float64(v[0])
		}
	}
	fmt.Printf("%.14f\n", res)
}