Easy and hard versions are actually different problems, so we advise you to read both statements carefully.You are given a weighted rooted tree, vertex $$$1$$$ is the root of this tree. Also, each edge has its own cost.A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a vertex $$$v$$$ is the last different from $$$v$$$ vertex on the path from the root to the vertex $$$v$$$. Children of vertex $$$v$$$ are all vertices for which $$$v$$$ is the parent. A vertex is a leaf if it has no children. The weighted tree is such a tree that each edge of this tree has some weight.The weight of the path is the sum of edges weights on this path. The weight of the path from the vertex to itself is $$$0$$$.You can make a sequence of zero or more moves. On each move, you select an edge and divide its weight by $$$2$$$ rounding down. More formally, during one move, you choose some edge $$$i$$$ and divide its weight by $$$2$$$ rounding down ($$$w_i := \left\lfloor\frac{w_i}{2}\right\rfloor$$$).Each edge $$$i$$$ has an associated cost $$$c_i$$$ which is either $$$1$$$ or $$$2$$$ coins. Each move with edge $$$i$$$ costs $$$c_i$$$ coins.Your task is to find the minimum total cost to make the sum of weights of paths from the root to each leaf at most $$$S$$$. In other words, if $$$w(i, j)$$$ is the weight of the path from the vertex $$$i$$$ to the vertex $$$j$$$, then you have to make $$$\sum\limits_{v \in leaves} w(root, v) \le S$$$, where $$$leaves$$$ is the list of all leaves.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$S$$$ ($$$2 \le n \le 10^5; 1 \le S \le 10^{16}$$$) — the number of vertices in the tree and the maximum possible sum of weights you have to obtain. The next $$$n-1$$$ lines describe edges of the tree. The edge $$$i$$$ is described as four integers $$$v_i$$$, $$$u_i$$$, $$$w_i$$$ and $$$c_i$$$ ($$$1 \le v_i, u_i \le n; 1 \le w_i \le 10^6; 1 \le c_i \le 2$$$), where $$$v_i$$$ and $$$u_i$$$ are vertices the edge $$$i$$$ connects, $$$w_i$$$ is the weight of this edge and $$$c_i$$$ is the cost of this edge. It is guaranteed that the sum of $$$n$$$ does not exceed $$$10^5$$$ ($$$\sum n \le 10^5$$$).

For each test case, print the answer: the minimum total cost required to make the sum of weights paths from the root to each leaf at most $$$S$$$.

package main

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

type pair struct {
	to int
	val int64
	c int
}

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

	T:= 0
	for Fscan(in, &T); T > 0; T-- {
		var n int
		var s int64
		Fscan(in,&n,&s)
		s=-s
		e:=make([][]pair,n+1)
		for i:=0;i<n-1;i++{
			var u,v,c int
			var val int64
			Fscan(in,&u,&v,&val,&c)
			// Fprintln(out,u,v,n)
			e[u]=append(e[u],pair{v,val,c})
			e[v]=append(e[v],pair{u,val,c})
		}

		e[0]=append(e[0],pair{1,0,1})
		e[1]=append(e[1],pair{0,0,1})

		ans:=make([][]int64,2)
		add:=func(pe int64,cur int64,c int){
			if cur==int64(0){
				return
			}
			// Fprintln(out,pe,cur)
			c--
			for pe>0{
				ans[c]=append(ans[c],((pe+1)/2)*cur)
				pe/=2
			}
		}
		var dfs func(int,int,int64,int64,int) int64
		dfs=func(u int,p int,cst int64,pe int64, c int) int64 {
			if len(e[u])==1{
				s+=cst
				// Fprintln(out,1,pe)
				add(pe,int64(1),c)
				return int64(1)
			}
			cur:=int64(0)
			for _,ed := range(e[u]){
				if ed.to==p{
					continue
				}
				cur+=dfs(ed.to,u,cst+ed.val,ed.val,ed.c)
			}
			// Fprintln(out,cur,pe)
			add(pe,cur,c)
			return cur
		}

		dfs(1,0,int64(0),int64(0),0)
		// Fprintln(out,s,ans)
		cnt:=100000000

		for kk:=0;kk<2;kk++{
			ans[kk]=append(ans[kk],int64(1e16))

			sort.SliceStable(ans[kk], func(i, j int) bool {
				return ans[kk][i] > ans[kk][j]
			})

			ans[kk][0]=0
			for i:=1;i<len(ans[kk]);i++{
				ans[kk][i]+=ans[kk][i-1]
			}

		}

		bs:=func(s int64,a []int64) int {
			l,r:=-1,len(a)
			if a[r-1]<s{
				return 1000000000
			}
			for r-l>1{
				m:=(l+r)/2
				if a[m]<s{
					l=m
				} else{
					r=m
				}
			}
			return r
		}

		for i,x:=range(ans[0]){
			val:=i+2*bs(s-x,ans[1])
			if val<cnt{
				cnt=val
			}
		}

		Fprintln(out,cnt)
	}
}

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

package main

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

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

	tc := readNum(reader)
	var buf bytes.Buffer
	for tc > 0 {
		tc--
		var n int
		var S uint64
		s, _ := reader.ReadBytes('\n')
		pos := readInt(s, 0, &n)
		readUint64(s, pos+1, &S)
		E := make([][]int, n-1)
		for i := 0; i < n-1; i++ {
			E[i] = readNNums(reader, 4)
		}
		res := solve(n, int64(S), E)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}
	fmt.Print(buf.String())
}

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

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

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

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

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

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

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

	return i
}

const H = 17

func solve(n int, S int64, E [][]int) int {
	adj := make([][]int, n)
	for i := 0; i < n; i++ {
		adj[i] = make([]int, 0, 3)
	}

	for i := 0; i < len(E); i++ {
		u, v := E[i][0], E[i][1]
		u--
		v--
		adj[u] = append(adj[u], i)
		adj[v] = append(adj[v], i)
	}

	pq1 := make(PriorityQueue, 0, n)
	pq2 := make(PriorityQueue, 0, n)
	var tot1 int64
	var tot2 int64
	var dfs func(p, u int) int

	dfs = func(p, u int) int {
		if len(adj[u]) == 1 && p >= 0 {
			return 1
		}

		var cnt int
		for _, i := range adj[u] {
			e := E[i]
			v, w, x := e[0]^e[1]^(u+1), e[2], e[3]
			v--
			if p == v {
				continue
			}
			c := dfs(u, v)
			cnt += c
			edge := NewEdge(w, c)

			if x == 1 {
				heap.Push(&pq1, edge)
				tot1 += edge.priority()
			} else {
				heap.Push(&pq2, edge)
				tot2 += edge.priority()
			}

		}
		return cnt
	}

	dfs(-1, 0)

	v1 := make([]int64, 0, H*n)
	v2 := make([]int64, 0, H*n)

	v1 = move(&pq1, tot1, v1)
	v2 = move(&pq2, tot2, v2)

	ans := len(v1) + 2*len(v2)

	for i, j := 0, len(v2)-1; i < len(v1); i++ {
		// remove i cost 1 edges
		for j > 0 && v1[i]+v2[j-1] <= S {
			j--
		}
		if v1[i]+v2[j] <= S && i+2*j < ans {
			ans = i + 2*j
		}
	}

	return ans
}

func move(pq *PriorityQueue, tot int64, v []int64) []int64 {
	for tot > 0 {
		v = append(v, tot)
		cur := heap.Pop(pq).(*Edge)
		tot -= cur.diff()
		cur.w /= 2
		if cur.w > 0 {
			heap.Push(pq, cur)
		}
	}
	// remove all edges
	v = append(v, 0)
	return v
}

type Edge struct {
	w     int
	c     int
	index int
}

func NewEdge(w, c int) *Edge {
	edge := new(Edge)
	edge.w = w
	edge.c = c
	return edge
}

func (edge Edge) priority() int64 {
	return int64(edge.w) * int64(edge.c)
}

func (edge Edge) diff() int64 {
	tot := edge.priority()
	return tot - int64(edge.w/2)*int64(edge.c)
}

// A PriorityQueue implements heap.Interface and holds Items.
type PriorityQueue []*Edge

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

func (pq PriorityQueue) Less(i, j int) bool {
	// We want Pop to give us the highest, not lowest, priority so we use greater than here.
	return pq[i].diff() > pq[j].diff()
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = i
	pq[j].index = j
}

func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Edge)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	old[n-1] = nil  // avoid memory leak
	item.index = -1 // for safety
	*pq = old[0 : n-1]
	return item
}

package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"io"
	"math"
	"os"
)

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

	type Edge struct {
		v, w, c int
	}

	for t := io.NextInt(); t > 0; t-- {
		n := io.NextInt()
		var s int64
		fmt.Fscan(io, &s)
		g := make([][]Edge, n+1)
		for i := 0; i < n-1; i++ {
			u, v, w, c := io.NextInt(), io.NextInt(), io.NextInt(), io.NextInt()
			g[u] = append(g[u], Edge{v, w, c})
			g[v] = append(g[v], Edge{u, w, c})
		}

		var total int64
		pq1, pq2 := &EHeap{E{}}, &EHeap{E{}}
		var dfs func(u, p int) int
		dfs = func(u, p int) (s int) {
			if u != p && len(g[u]) == 1 {
				return 1
			}
			for _, e := range g[u] {
				if e.v != p {
					cnt := dfs(e.v, u)
					ee := E{e.w, cnt}
					if e.c == 1 {
						heap.Push(pq1, ee)
					} else {
						heap.Push(pq2, ee)
					}
					total += ee.cost()
					s += cnt
				}
			}
			return
		}
		dfs(1, 1)

		req := total - s
		get := func(pq *EHeap) ([]int64, int64) {
			var s int64
			var v []int64
			for s < req && (*pq)[0].w > 0 {
				e := heap.Pop(pq).(E)
				v = append(v, e.remove())
				s += e.remove()
				e.w /= 2
				heap.Push(pq, e)
			}
			return v, s
		}
		v1, _ := get(pq1)
		v2, jsum := get(pq2)
		j := len(v2)
		ans, cur := math.MaxInt32, 2*j
		if jsum >= req {
			ans = cur
		}
		var isum int64
		for _, x := range v1 {
			isum += x
			cur++
			for j > 0 && isum+jsum >= req {
				if cur < ans {
					ans = cur
				}
				j--
				jsum -= v2[j]
				cur -= 2
			}
			if isum+jsum >= req && cur < ans {
				ans = cur
			}
		}

		io.Println(ans)
	}
}

type E struct {
	w, mul int
}

func (e E) cost() int64   { return int64(e.w) * int64(e.mul) }
func (e E) remove() int64 { return int64(e.w+1) / 2 * int64(e.mul) }

type EHeap []E

func (h EHeap) Len() int           { return len(h) }
func (h EHeap) Less(i, j int) bool { return h[i].remove() > h[j].remove() }
func (h EHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *EHeap) Push(x interface{}) {
	*h = append(*h, x.(E))
}

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

type FastIO struct {
	*bufio.Reader
	*bufio.Writer
	c [1]byte
}

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

func (f *FastIO) readC() byte {
	f.Read(f.c[:])
	return f.c[0]
}

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

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

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

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

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

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

func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}

package main

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

const (
	SCANNER_BUF_SIZE = 2 << 20

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

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

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

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

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

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

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

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

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

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

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

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

type Edge struct {
	w int64
	c int64 // 1 or 2
	l int64
}

func (e Edge) Ded() int64 {
	return (e.w - e.w/2) * e.l
}

func (e Edge) Ded2() int64 {
	return Edge{w: e.w / 2, l: e.l}.Ded()
}

type Heap []Edge

func (h Heap) Len() int {
	return len(h)
}

func (h Heap) Less(a, b int) bool {
	return h[a].Ded() > h[b].Ded() // max heap
}

func (h Heap) Swap(a, b int) {
	h[a], h[b] = h[b], h[a]
}

func (h *Heap) Push(x interface{}) {
	*h = append(*h, x.(Edge))
}

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

func main() {
	defer writer.Flush()
	t := sint64()
	for ; t > 0; t-- {
		n := sint64()
		S := sint64()
		edges := map[int64]map[int64]Edge{}
		for i := int64(1); i < n; i++ {
			v := sint64()
			u := sint64()
			w := sint64()
			c := sint64()
			if edges[v] == nil {
				edges[v] = map[int64]Edge{}
			}
			if edges[u] == nil {
				edges[u] = map[int64]Edge{}
			}
			edges[v][u] = Edge{w: w, c: c}
			edges[u][v] = Edge{w: w, c: c}
		}
		printf("%d\n", sol(n, S, edges))
	}
}

func sol(n, S int64, edges map[int64]map[int64]Edge) int64 {
	sum := int64(0)
	c1 := &Heap{}
	c2 := &Heap{}
	explored := map[int64]bool{}
	var dfs func(int64) int64
	dfs = func(i int64) int64 {
		explored[i] = true
		ret := int64(0)
		isLeaf := true
		for nxt := range edges[i] {
			if explored[nxt] {
				continue
			}
			isLeaf = false
			nxtLeaves := dfs(nxt)
			toPush := Edge{
				w: edges[i][nxt].w,
				l: nxtLeaves,
			}
			sum += toPush.w * toPush.l
			if edges[i][nxt].c == 1 {
				heap.Push(c1, toPush)
			} else if edges[i][nxt].c == 2 {
				heap.Push(c2, toPush)
			}
			ret += nxtLeaves
		}
		if isLeaf {
			return 1
		}
		return ret
	}
	dfs(1)
	cost := int64(0)
	for sum > S {
		if c1.Len() == 0 {
			c2a := heap.Pop(c2).(Edge)
			sum -= c2a.Ded()
			cost+=2
			heap.Push(c2, Edge{w: c2a.w/2, l: c2a.l})
			continue
		}
		if c2.Len() == 0 {
			c1a := heap.Pop(c1).(Edge)
			sum -= c1a.Ded()
			cost++
			heap.Push(c1, Edge{w: c1a.w/2, l: c1a.l})
			continue
		}
		c1a := heap.Pop(c1).(Edge)
		if sum - c1a.Ded() <= S {
			return cost+1
		}
		if c1.Len() == 0 {
			c2a := heap.Pop(c2).(Edge)
			if c1a.Ded() + c1a.Ded2() > c2a.Ded() {
				sum -= c1a.Ded()
				cost++
				heap.Push(c2, c2a)
				heap.Push(c1, Edge{w: c1a.w/2, l: c1a.l})
			} else {
				sum -= c2a.Ded()
				cost+=2
				heap.Push(c1, c1a)
				heap.Push(c2, Edge{w: c2a.w/2, l: c2a.l})
			}
			continue
		}
		c1b := heap.Pop(c1).(Edge)
		c2a := heap.Pop(c2).(Edge)
		if c1a.Ded() + c1a.Ded2() > c2a.Ded() || c1a.Ded() + c1b.Ded() > c2a.Ded() {
			sum -= c1a.Ded()
			cost++
			heap.Push(c2, c2a)
			heap.Push(c1, Edge{w: c1a.w/2, l: c1a.l})
			heap.Push(c1, c1b)
		} else {
			sum -= c2a.Ded()
			cost+=2
			heap.Push(c1, c1a)
			heap.Push(c1, c1b)
			heap.Push(c2, Edge{w: c2a.w/2, l: c2a.l})
		}
	}
	return cost
}

package main

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

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

	tc := readNum(reader)
	var buf bytes.Buffer
	for tc > 0 {
		tc--
		var n int
		var S uint64
		s, _ := reader.ReadBytes('\n')
		pos := readInt(s, 0, &n)
		readUint64(s, pos+1, &S)
		E := make([][]int, n-1)
		for i := 0; i < n-1; i++ {
			E[i] = readNNums(reader, 4)
		}
		res := solve(n, int64(S), E)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}
	fmt.Print(buf.String())
}

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

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

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

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

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

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

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

	return i
}

const H = 17

func solve(n int, S int64, E [][]int) int {
	adj := make([][]int, n)
	for i := 0; i < n; i++ {
		adj[i] = make([]int, 0, 3)
	}

	for i := 0; i < len(E); i++ {
		u, v := E[i][0], E[i][1]
		u--
		v--
		adj[u] = append(adj[u], i)
		adj[v] = append(adj[v], i)
	}

	pq1 := make(PriorityQueue, 0, n)
	pq2 := make(PriorityQueue, 0, n)
	var tot1 int64
	var tot2 int64
	var dfs func(p, u int) int

	dfs = func(p, u int) int {
		if len(adj[u]) == 1 && p >= 0 {
			return 1
		}

		var cnt int
		for _, i := range adj[u] {
			e := E[i]
			v, w, x := e[0]^e[1]^(u+1), e[2], e[3]
			v--
			if p == v {
				continue
			}
			c := dfs(u, v)
			cnt += c
			edge := NewEdge(w, c)

			if x == 1 {
				heap.Push(&pq1, edge)
				tot1 += edge.priority()
			} else {
				heap.Push(&pq2, edge)
				tot2 += edge.priority()
			}

		}
		return cnt
	}

	dfs(-1, 0)

	v1 := make([]int64, 0, H*n)
	v2 := make([]int64, 0, H*n)

	v1 = move(&pq1, tot1, v1)
	v2 = move(&pq2, tot2, v2)

	ans := len(v1) + len(v2)

	for i, j := 0, len(v2)-1; i < len(v1); i++ {
		// remove i cost 1 edges
		for j > 0 && v1[i]+v2[j-1] <= S {
			j--
		}
		if v1[i]+v2[j] <= S && i+2*j < ans {
			ans = i + 2*j
		}
	}

	return ans
}

func move(pq *PriorityQueue, tot int64, v []int64) []int64 {
	for tot > 0 {
		v = append(v, tot)
		cur := heap.Pop(pq).(*Edge)
		tot -= cur.diff()
		cur.w /= 2
		if cur.w > 0 {
			heap.Push(pq, cur)
		}
	}
	// remove all edges
	v = append(v, 0)
	return v
}

type Edge struct {
	w     int
	c     int
	index int
}

func NewEdge(w, c int) *Edge {
	edge := new(Edge)
	edge.w = w
	edge.c = c
	return edge
}

func (edge Edge) priority() int64 {
	return int64(edge.w) * int64(edge.c)
}

func (edge Edge) diff() int64 {
	tot := edge.priority()
	return tot - int64(edge.w/2)*int64(edge.c)
}

// A PriorityQueue implements heap.Interface and holds Items.
type PriorityQueue []*Edge

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

func (pq PriorityQueue) Less(i, j int) bool {
	// We want Pop to give us the highest, not lowest, priority so we use greater than here.
	return pq[i].diff() > pq[j].diff()
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = i
	pq[j].index = j
}

func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Edge)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	old[n-1] = nil  // avoid memory leak
	item.index = -1 // for safety
	*pq = old[0 : n-1]
	return item
}

package main

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

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

	type Edge struct {
		v, w, c int
	}

	for t := io.NextInt(); t > 0; t-- {
		n := io.NextInt()
		var s int64
		fmt.Fscan(io, &s)
		g := make([][]Edge, n+1)
		for i := 0; i < n-1; i++ {
			u, v, w, c := io.NextInt(), io.NextInt(), io.NextInt(), io.NextInt()
			g[u] = append(g[u], Edge{v, w, c})
			g[v] = append(g[v], Edge{u, w, c})
		}

		var total int64
		pq1, pq2 := new(EHeap), new(EHeap)
		var dfs func(u, p int) int
		dfs = func(u, p int) (s int) {
			if u != p && len(g[u]) == 1 {
				return 1
			}
			for _, e := range g[u] {
				if e.v != p {
					cnt := dfs(e.v, u)
					ee := E{e.w, cnt}
					if e.c == 1 {
						heap.Push(pq1, ee)
					} else {
						heap.Push(pq2, ee)
					}
					total += ee.cost()
					s += cnt
				}
			}
			return
		}
		dfs(1, 1)

		heap.Push(pq1, E{0, 0})
		heap.Push(pq1, E{0, 0})
		heap.Push(pq2, E{0, 0})
		req := total - s
		ans := 0
		last2 := s
		for req > 0 {
			e1, e2 := heap.Pop(pq1).(E), heap.Pop(pq2).(E)
			if e2.remove() < e1.remove()+(*pq1)[0].remove() {
				ans++
				req -= e1.remove()
				e1.w /= 2
			} else {
				ans += 2
				req -= e2.remove()
				last2 = e2.remove()
				e2.w /= 2
			}
			heap.Push(pq1, e1)
			heap.Push(pq2, e2)
		}

		if req+last2-(*pq1)[0].remove() <= 0 {
			ans--
		}

		io.Println(ans)
	}
}

type E struct {
	w, mul int
}

func (e E) cost() int64 {
	return int64(e.w) * int64(e.mul)
}

func (e E) remove() int64 {
	return int64(e.w+1) / 2 * int64(e.mul)
}

type EHeap []E

func (h EHeap) Len() int           { return len(h) }
func (h EHeap) Less(i, j int) bool { return h[i].remove() > h[j].remove() }
func (h EHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *EHeap) Push(x interface{}) {
	*h = append(*h, x.(E))
}

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

type FastIO struct {
	*bufio.Reader
	*bufio.Writer
	c [1]byte
}

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

func (f *FastIO) readC() byte {
	f.Read(f.c[:])
	return f.c[0]
}

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

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

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

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

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

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

func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}

package main

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

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

	type Edge struct {
		v, w, c int
	}

	for t := io.NextInt(); t > 0; t-- {
		n := io.NextInt()
		var s int64
		fmt.Fscan(io, &s)
		g := make([][]Edge, n+1)
		for i := 0; i < n-1; i++ {
			u, v, w, c := io.NextInt(), io.NextInt(), io.NextInt(), io.NextInt()
			g[u] = append(g[u], Edge{v, w, c})
			g[v] = append(g[v], Edge{u, w, c})
		}

		var total int64
		pq1, pq2 := new(EHeap), new(EHeap)
		var dfs func(u, p int) int
		dfs = func(u, p int) (s int) {
			if u != p && len(g[u]) == 1 {
				return 1
			}
			for _, e := range g[u] {
				if e.v != p {
					cnt := dfs(e.v, u)
					ee := E{e.w, cnt}
					if e.c == 1 {
						heap.Push(pq1, ee)
					} else {
						heap.Push(pq2, ee)
					}
					total += ee.cost()
					s += cnt
				}
			}
			return
		}
		dfs(1, 1)

		heap.Push(pq1, E{0, 0})
		heap.Push(pq1, E{0, 0})
		heap.Push(pq2, E{0, 0})
		req := total - s
		ans := 0
		for req > 0 {
			e1, e2 := heap.Pop(pq1).(E), heap.Pop(pq2).(E)
			if e1.remove() >= req || e2.remove() < e1.remove()+(*pq1)[0].remove() {
				ans++
				req -= e1.remove()
				e1.w /= 2
			} else {
				ans += 2
				req -= e2.remove()
				e2.w /= 2
			}
			heap.Push(pq1, e1)
			heap.Push(pq2, e2)
		}

		io.Println(ans)
	}
}

type E struct {
	w, mul int
}

func (e E) cost() int64 {
	return int64(e.w) * int64(e.mul)
}

func (e E) remove() int64 {
	return int64(e.w+1) / 2 * int64(e.mul)
}

type EHeap []E

func (h EHeap) Len() int           { return len(h) }
func (h EHeap) Less(i, j int) bool { return h[i].remove() > h[j].remove() }
func (h EHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *EHeap) Push(x interface{}) {
	*h = append(*h, x.(E))
}

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

type FastIO struct {
	*bufio.Reader
	*bufio.Writer
	c [1]byte
}

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

func (f *FastIO) readC() byte {
	f.Read(f.c[:])
	return f.c[0]
}

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

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

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

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

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

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

func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}

package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"io"
	"math"
	"os"
)

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

	type Edge struct {
		v, w, c int
	}

	for t := io.NextInt(); t > 0; t-- {
		n := io.NextInt()
		var s int64
		fmt.Fscan(io, &s)
		g := make([][]Edge, n+1)
		for i := 0; i < n-1; i++ {
			u, v, w, c := io.NextInt(), io.NextInt(), io.NextInt(), io.NextInt()
			g[u] = append(g[u], Edge{v, w, c})
			g[v] = append(g[v], Edge{u, w, c})
		}

		var total int64
		pq1, pq2 := &EHeap{E{}}, &EHeap{E{}}
		var dfs func(u, p int) int
		dfs = func(u, p int) (s int) {
			if u != p && len(g[u]) == 1 {
				return 1
			}
			for _, e := range g[u] {
				if e.v != p {
					cnt := dfs(e.v, u)
					ee := E{e.w, cnt}
					if e.c == 1 {
						heap.Push(pq1, ee)
					} else {
						heap.Push(pq2, ee)
					}
					total += ee.cost()
					s += cnt
				}
			}
			return
		}
		dfs(1, 1)

		req := total - s
		get := func(pq *EHeap) []int64 {
			r := req
			var v []int64
			for r > 0 && (*pq)[0].w > 0 {
				e := heap.Pop(pq).(E)
				v = append(v, e.remove())
				r -= e.remove()
				e.w /= 2
				heap.Push(pq, e)
			}
			return v
		}
		v1, v2 := get(pq1), get(pq2)
		var jsum, isum int64
		j := len(v2)
		for _, x := range v2 {
			jsum += x
		}
		ans, cur := math.MaxInt32, 2*j
		if jsum >= req {
			ans = cur
		}
		for _, x := range v1 {
			isum += x
			cur++
			for j > 0 && isum+jsum >= req {
				if cur < ans {
					ans = cur
				}
				j--
				jsum -= v2[j]
				cur -= 2
			}
		}

		io.Println(ans)
	}
}

type E struct {
	w, mul int
}

func (e E) cost() int64   { return int64(e.w) * int64(e.mul) }
func (e E) remove() int64 { return int64(e.w+1) / 2 * int64(e.mul) }

type EHeap []E

func (h EHeap) Len() int           { return len(h) }
func (h EHeap) Less(i, j int) bool { return h[i].remove() > h[j].remove() }
func (h EHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *EHeap) Push(x interface{}) {
	*h = append(*h, x.(E))
}

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

type FastIO struct {
	*bufio.Reader
	*bufio.Writer
	c [1]byte
}

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

func (f *FastIO) readC() byte {
	f.Read(f.c[:])
	return f.c[0]
}

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

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

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

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

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

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

func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}

package main

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

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

	type Edge struct {
		v, w, c int
	}

	for t := io.NextInt(); t > 0; t-- {
		n := io.NextInt()
		var s int64
		fmt.Fscan(io, &s)
		g := make([][]Edge, n+1)
		for i := 0; i < n-1; i++ {
			u, v, w, c := io.NextInt(), io.NextInt(), io.NextInt(), io.NextInt()
			g[u] = append(g[u], Edge{v, w, c})
			g[v] = append(g[v], Edge{u, w, c})
		}

		var total int64
		pq1, pq2 := new(EHeap), new(EHeap)
		var dfs func(u, p int) int
		dfs = func(u, p int) (s int) {
			if u != p && len(g[u]) == 1 {
				return 1
			}
			for _, e := range g[u] {
				if e.v != p {
					cnt := dfs(e.v, u)
					ee := E{e.w, cnt}
					if e.c == 1 {
						heap.Push(pq1, ee)
					} else {
						heap.Push(pq2, ee)
					}
					total += ee.cost()
					s += cnt
				}
			}
			return
		}
		dfs(1, 1)

		heap.Push(pq1, E{0, 0})
		heap.Push(pq1, E{0, 0})
		heap.Push(pq2, E{0, 0})
		req := total - s
		ans := 0
		last2 := int64(1e16)
		for req > 0 {
			e1, e2 := heap.Pop(pq1).(E), heap.Pop(pq2).(E)
			if e2.remove() < e1.remove()+(*pq1)[0].remove() {
				ans++
				req -= e1.remove()
				e1.w /= 2
			} else {
				ans += 2
				req -= e2.remove()
				last2 = e2.remove()
				e2.w /= 2
			}
			heap.Push(pq1, e1)
			heap.Push(pq2, e2)
		}

		if req+last2-(*pq1)[0].remove() <= 0 {
			ans--
		}

		io.Println(ans)
	}
}

type E struct {
	w, mul int
}

func (e E) cost() int64 {
	return int64(e.w) * int64(e.mul)
}

func (e E) remove() int64 {
	return int64(e.w+1) / 2 * int64(e.mul)
}

type EHeap []E

func (h EHeap) Len() int           { return len(h) }
func (h EHeap) Less(i, j int) bool { return h[i].remove() > h[j].remove() }
func (h EHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *EHeap) Push(x interface{}) {
	*h = append(*h, x.(E))
}

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

type FastIO struct {
	*bufio.Reader
	*bufio.Writer
	c [1]byte
}

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

func (f *FastIO) readC() byte {
	f.Read(f.c[:])
	return f.c[0]
}

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

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

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

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

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

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

func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}

package main

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

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

	type Edge struct {
		v, w, c int
	}

	for t := io.NextInt(); t > 0; t-- {
		n := io.NextInt()
		var s int64
		fmt.Fscan(io, &s)
		g := make([][]Edge, n+1)
		for i := 0; i < n-1; i++ {
			u, v, w, c := io.NextInt(), io.NextInt(), io.NextInt(), io.NextInt()
			g[u] = append(g[u], Edge{v, w, c})
			g[v] = append(g[v], Edge{u, w, c})
		}

		var total int64
		pq1, pq2 := new(EHeap), new(EHeap)
		var dfs func(u, p int) int
		dfs = func(u, p int) (s int) {
			if u != p && len(g[u]) == 1 {
				return 1
			}
			for _, e := range g[u] {
				if e.v != p {
					cnt := dfs(e.v, u)
					ee := E{e.w, cnt}
					if e.c == 1 {
						heap.Push(pq1, ee)
					} else {
						heap.Push(pq2, ee)
					}
					total += ee.cost()
					s += cnt
				}
			}
			return
		}
		dfs(1, 1)

		heap.Push(pq1, E{0, 0})
		heap.Push(pq1, E{0, 0})
		heap.Push(pq2, E{0, 0})
		ans := 0
		for total > s {
			e1, e2 := heap.Pop(pq1).(E), heap.Pop(pq2).(E)
			e11 := heap.Pop(pq1).(E)
			if e2.remove() < e1.remove()+e11.remove() || total-e1.remove() <= s {
				ans++
				total -= e1.remove()
				e1.w /= 2
			} else {
				ans += 2
				total -= e2.remove()
				e2.w /= 2
			}
			heap.Push(pq1, e1)
			heap.Push(pq1, e11)
			heap.Push(pq2, e2)
		}

		io.Println(ans)
	}
}

type E struct {
	w, mul int
}

func (e E) cost() int64 {
	return int64(e.w) * int64(e.mul)
}

func (e E) remove() int64 {
	return int64(e.w+1) / 2 * int64(e.mul)
}

type EHeap []E

func (h EHeap) Len() int           { return len(h) }
func (h EHeap) Less(i, j int) bool { return h[i].remove() > h[j].remove() }
func (h EHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *EHeap) Push(x interface{}) {
	*h = append(*h, x.(E))
}

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

type FastIO struct {
	*bufio.Reader
	*bufio.Writer
	c [1]byte
}

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

func (f *FastIO) readC() byte {
	f.Read(f.c[:])
	return f.c[0]
}

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

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

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

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

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

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

func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}

package main

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

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

	type Edge struct {
		v, w, c int
	}

	for t := io.NextInt(); t > 0; t-- {
		n := io.NextInt()
		var s int64
		fmt.Fscan(io, &s)
		g := make([][]Edge, n+1)
		for i := 0; i < n-1; i++ {
			u, v, w, c := io.NextInt(), io.NextInt(), io.NextInt(), io.NextInt()
			g[u] = append(g[u], Edge{v, w, c})
			g[v] = append(g[v], Edge{u, w, c})
		}

		var total int64
		pq := new(EHeap)
		var dfs func(u, p int) int
		dfs = func(u, p int) (s int) {
			if u != p && len(g[u]) == 1 {
				return 1
			}
			for _, e := range g[u] {
				if e.v != p {
					cnt := dfs(e.v, u)
					e := E{e.w, e.c, cnt}
					heap.Push(pq, e)
					total += e.cost()
					s += cnt
				}
			}
			return
		}
		dfs(1, 1)

		ans := 0
		for total > s {
			e := heap.Pop(pq).(E)
			ans += e.c
			total -= e.remove()
			e.w /= 2
			heap.Push(pq, e)
		}

		io.Println(ans)
	}
}

type E struct {
	w, c, mul int
}

func (e E) cost() int64 {
	return int64(e.w) * int64(e.mul)
}

func (e E) remove() int64 {
	return int64(e.w+1) / 2 * int64(e.mul)
}

func (e E) removeCost() int64 {
	return e.remove() * int64(3-e.c)
}

type EHeap []E

func (h EHeap) Len() int           { return len(h) }
func (h EHeap) Less(i, j int) bool { return h[i].removeCost() > h[j].removeCost() }
func (h EHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *EHeap) Push(x interface{}) {
	*h = append(*h, x.(E))
}

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

type FastIO struct {
	*bufio.Reader
	*bufio.Writer
	c [1]byte
}

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

func (f *FastIO) readC() byte {
	f.Read(f.c[:])
	return f.c[0]
}

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

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

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

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

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

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

func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}

package main

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

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

	type Edge struct {
		v, w, c int
	}

	for t := io.NextInt(); t > 0; t-- {
		n := io.NextInt()
		var s int64
		fmt.Fscan(io, &s)
		g := make([][]Edge, n+1)
		for i := 0; i < n-1; i++ {
			u, v, w, c := io.NextInt(), io.NextInt(), io.NextInt(), io.NextInt()
			g[u] = append(g[u], Edge{v, w, c})
			g[v] = append(g[v], Edge{u, w, c})
		}

		var total int64
		pq1, pq2 := new(EHeap), new(EHeap)
		var dfs func(u, p int) int
		dfs = func(u, p int) (s int) {
			if u != p && len(g[u]) == 1 {
				return 1
			}
			for _, e := range g[u] {
				if e.v != p {
					cnt := dfs(e.v, u)
					ee := E{e.w, cnt}
					if e.c == 1 {
						heap.Push(pq1, ee)
					} else {
						heap.Push(pq2, ee)
					}
					total += ee.cost()
					s += cnt
				}
			}
			return
		}
		dfs(1, 1)

		heap.Push(pq1, E{0, 0})
		heap.Push(pq1, E{0, 0})
		heap.Push(pq2, E{0, 0})
		ans := 0
		for total > s {
			e1, e2 := heap.Pop(pq1).(E), heap.Pop(pq2).(E)
			e11 := heap.Pop(pq1).(E)
			if e2.remove() > e1.remove()+e11.remove() {
				ans += 2
				total -= e2.remove()
				e2.w /= 2
			} else {
				ans++
				total -= e1.remove()
				e1.w /= 2
			}
			heap.Push(pq1, e1)
			heap.Push(pq1, e11)
			heap.Push(pq2, e2)
		}

		io.Println(ans)
	}
}

type E struct {
	w, mul int
}

func (e E) cost() int64 {
	return int64(e.w) * int64(e.mul)
}

func (e E) remove() int64 {
	return int64(e.w+1) / 2 * int64(e.mul)
}

type EHeap []E

func (h EHeap) Len() int           { return len(h) }
func (h EHeap) Less(i, j int) bool { return h[i].remove() > h[j].remove() }
func (h EHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *EHeap) Push(x interface{}) {
	*h = append(*h, x.(E))
}

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

type FastIO struct {
	*bufio.Reader
	*bufio.Writer
	c [1]byte
}

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

func (f *FastIO) readC() byte {
	f.Read(f.c[:])
	return f.c[0]
}

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

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

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

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

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

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

func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}

package main

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

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

	type Edge struct {
		v, w, c int
	}

	for t := io.NextInt(); t > 0; t-- {
		n := io.NextInt()
		var s int64
		fmt.Fscan(io, &s)
		g := make([][]Edge, n+1)
		for i := 0; i < n-1; i++ {
			u, v, w, c := io.NextInt(), io.NextInt(), io.NextInt(), io.NextInt()
			g[u] = append(g[u], Edge{v, w, c})
			g[v] = append(g[v], Edge{u, w, c})
		}

		var total int64
		pq1, pq2 := new(EHeap), new(EHeap)
		var dfs func(u, p int) int
		dfs = func(u, p int) (s int) {
			if u != p && len(g[u]) == 1 {
				return 1
			}
			for _, e := range g[u] {
				if e.v != p {
					cnt := dfs(e.v, u)
					ee := E{e.w, cnt}
					if e.c == 1 {
						heap.Push(pq1, ee)
					} else {
						heap.Push(pq2, ee)
					}
					total += ee.cost()
					s += cnt
				}
			}
			return
		}
		dfs(1, 1)

		heap.Push(pq1, E{0, 0})
		heap.Push(pq1, E{0, 0})
		heap.Push(pq2, E{0, 0})
		req := total - s
		ans := 0
		last1, last2 := int64(1e16), int64(1e16)
		for req > 0 {
			e1, e2 := heap.Pop(pq1).(E), heap.Pop(pq2).(E)
			if e2.remove() < e1.remove()+(*pq1)[0].remove() {
				ans++
				req -= e1.remove()
				last1 = e1.remove()
				e1.w /= 2
			} else {
				ans += 2
				req -= e2.remove()
				last2 = e2.remove()
				e2.w /= 2
			}
			heap.Push(pq1, e1)
			heap.Push(pq2, e2)
		}

		if req+last2-(*pq1)[0].remove() <= 0 || req+last1 <= 0 {
			ans--
		}

		io.Println(ans)
	}
}

type E struct {
	w, mul int
}

func (e E) cost() int64 {
	return int64(e.w) * int64(e.mul)
}

func (e E) remove() int64 {
	return int64(e.w+1) / 2 * int64(e.mul)
}

type EHeap []E

func (h EHeap) Len() int           { return len(h) }
func (h EHeap) Less(i, j int) bool { return h[i].remove() > h[j].remove() }
func (h EHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *EHeap) Push(x interface{}) {
	*h = append(*h, x.(E))
}

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

type FastIO struct {
	*bufio.Reader
	*bufio.Writer
	c [1]byte
}

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

func (f *FastIO) readC() byte {
	f.Read(f.c[:])
	return f.c[0]
}

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

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

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

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

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

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

func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}

package main

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

const (
	SCANNER_BUF_SIZE = 2 << 20

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

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

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

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

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

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

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

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

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

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

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

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

type Edge struct {
	w int64
	c int64 // 1 or 2
	l int64
}

func (e Edge) Ded() int64 {
	return (e.w - e.w/2) * e.l
}

func (e Edge) Ded2() int64 {
	return Edge{w: e.w / 2, l: e.l}.Ded()
}

type Heap []Edge

func (h Heap) Len() int {
	return len(h)
}

func (h Heap) Less(a, b int) bool {
	return h[a].Ded() > h[b].Ded() // max heap
}

func (h Heap) Swap(a, b int) {
	h[a], h[b] = h[b], h[a]
}

func (h *Heap) Push(x interface{}) {
	*h = append(*h, x.(Edge))
}

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

func main() {
	defer writer.Flush()
	t := sint64()
	for ; t > 0; t-- {
		n := sint64()
		S := sint64()
		edges := map[int64]map[int64]Edge{}
		for i := int64(1); i < n; i++ {
			v := sint64()
			u := sint64()
			w := sint64()
			c := sint64()
			if edges[v] == nil {
				edges[v] = map[int64]Edge{}
			}
			if edges[u] == nil {
				edges[u] = map[int64]Edge{}
			}
			edges[v][u] = Edge{w: w, c: c}
			edges[u][v] = Edge{w: w, c: c}
		}
		printf("%d\n", sol(n, S, edges))
	}
}

func sol(n, S int64, edges map[int64]map[int64]Edge) int64 {
	sum := int64(0)
	c1 := &Heap{}
	c2 := &Heap{}
	explored := map[int64]bool{}
	var dfs func(int64) int64
	dfs = func(i int64) int64 {
		explored[i] = true
		ret := int64(0)
		isLeaf := true
		for nxt := range edges[i] {
			if explored[nxt] {
				continue
			}
			isLeaf = false
			nxtLeaves := dfs(nxt)
			toPush := Edge{
				w: edges[i][nxt].w,
				l: nxtLeaves,
			}
			sum += toPush.w * toPush.l
			if edges[i][nxt].c == 1 {
				heap.Push(c1, toPush)
			} else if edges[i][nxt].c == 2 {
				heap.Push(c2, toPush)
			}
			ret += nxtLeaves
		}
		if isLeaf {
			return 1
		}
		return ret
	}
	dfs(1)
	cost := int64(0)
	for sum > S {
		if c1.Len() == 0 {
			c2a := heap.Pop(c2).(Edge)
			sum -= c2a.Ded()
			cost+=2
			heap.Push(c2, Edge{w: c2a.w/2, l: c2a.l})
			continue
		}
		if c2.Len() == 0 {
			c1a := heap.Pop(c1).(Edge)
			sum -= c1a.Ded()
			cost++
			heap.Push(c2, Edge{w: c1a.w/2, l: c1a.l})
			continue
		}
		c1a := heap.Pop(c1).(Edge)
		if sum - c1a.Ded() <= S {
			return cost+1
		}
		c1b := heap.Pop(c1).(Edge)
		c2a := heap.Pop(c2).(Edge)
		if c1a.Ded() + c1a.Ded2() > c2a.Ded() || c1a.Ded() + c1b.Ded() > c2a.Ded() {
			sum -= c1a.Ded()
			cost++
			heap.Push(c2, c2a)
			heap.Push(c1, Edge{w: c1a.w/2, l: c1a.l})
			heap.Push(c1, c1b)
		} else {
			sum -= c2a.Ded()
			cost+=2
			heap.Push(c1, c1a)
			heap.Push(c1, c1b)
			heap.Push(c2, Edge{w: c2a.w/2, l: c2a.l})
		}
	}
	return cost
}


Vanya smashes potato in a vertical food processor. At each moment of time the height of the potato in the processor doesn't exceed h and the processor smashes k centimeters of potato each second. If there are less than k centimeters remaining, than during this second processor smashes all the remaining potato.Vanya has n pieces of potato, the height of the i-th piece is equal to ai. He puts them in the food processor one by one starting from the piece number 1 and finishing with piece number n. Formally, each second the following happens:  If there is at least one piece of potato remaining, Vanya puts them in the processor one by one, until there is not enough space for the next piece.  Processor smashes k centimeters of potato (or just everything that is inside). Provided the information about the parameter of the food processor and the size of each potato in a row, compute how long will it take for all the potato to become smashed.

The first line of the input contains integers n, h and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ h ≤ 109) — the number of pieces of potato, the height of the food processor and the amount of potato being smashed each second, respectively. The second line contains n integers ai (1 ≤ ai ≤ h) — the heights of the pieces.

Print a single integer — the number of seconds required to smash all the potatoes following the process described in the problem statement.

NoteConsider the first sample.   First Vanya puts the piece of potato of height 5 into processor. At the end of the second there is only amount of height 2 remaining inside.  Now Vanya puts the piece of potato of height 4. At the end of the second there is amount of height 3 remaining.  Vanya puts the piece of height 3 inside and again there are only 3 centimeters remaining at the end of this second.  Vanya finally puts the pieces of height 2 and 1 inside. At the end of the second the height of potato in the processor is equal to 3.  During this second processor finally smashes all the remaining potato and the process finishes. In the second sample, Vanya puts the piece of height 5 inside and waits for 2 seconds while it is completely smashed. Then he repeats the same process for 4 other pieces. The total time is equal to 2·5 = 10 seconds.In the third sample, Vanya simply puts all the potato inside the processor and waits 2 seconds.

package main

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

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

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

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

  n, h, k := getI(), int64(getI()), int64(getI())
  heights := make([]int64, n)
  for i := range heights {
    heights[i] = int64(getI())
  }
  pos, current, t := 0, int64(0), int64(0)
  for pos < n {
    if current > h - heights[pos] {
      dt := (current - h + heights[pos] + k - 1) / k
      t += dt
      current -= dt * k
      if current < 0 {
        current = 0
      }
    }
    //fmt.Printf("pos %d, current %d, t %d\n", pos, current, t)
    for pos < n && current + heights[pos] <= h {
      current += heights[pos]
      pos++
    }
    t++
    current -= k
    if current < 0 {
      current = 0
    }
  }
  //fmt.Printf("pos %d, current %d, t %d\n", pos, current, t)
  t += (current + k - 1) / k
  writer.WriteString(fmt.Sprintf("%d\n", t))
}

package main

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

func main() {
	fsc := NewFastScanner()
	N, H, K := fsc.NextInt(), fsc.NextInt64(), fsc.NextInt64()
	A := make([]int, N)
	for i := 0; i < N; i++ {
		A[i] = fsc.NextInt()
	}
	var sum int64
	sum = 0
	var ans int64
	ans = 0

	var head int
	head = 0
	for head < N {
		for head < N && sum+int64(A[head]) <= H {
			sum += int64(A[head])
			head++
		}
		if head < N && sum+int64(A[head]) > H {
			var rest int64
			rest = sum + int64(A[head]) - H
			var time int64
			time = rest / K
			if rest%K != 0 {
				time++
			}
			sum -= time * K
			if sum < 0 {
				sum = 0
			}
			ans += time
		}
	}

	ans += sum / K
	if sum%K != 0 {
		ans++
	}
	fmt.Println(ans)
}

//template
type FastScanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewFastScanner() *FastScanner {
	rdr := bufio.NewReaderSize(os.Stdin, 2028)
	return &FastScanner{r: rdr}
}

func (s *FastScanner) 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 *FastScanner) NextInt() int {
	val, err := strconv.Atoi(s.Next())
	if err != nil {
		panic(err)
	}
	return val
}
func (s *FastScanner) NextInt64() int64 {
	val, err := strconv.ParseInt(s.Next(), 10, 64)
	if err != nil {
		panic(err)
	}
	return val
}
func (s *FastScanner) Pre() {
	if s.p >= len(s.buf) {
		s.ReadLine()
		s.p = 0
	}
}

func (s *FastScanner) ReadLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, err := s.r.ReadLine()
		if err != nil {
			panic(err.Error())
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

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

func main() {
	n := getInt()
	h := getInt()
	k := getInt()
	var curr int
	var ans int64
	for i := 0; i < n; i++ {
		a := getInt()
		//~ fmt.Fprintln(os.Stderr,"get", a)
	retry:
		if a+curr <= h {
			curr += a
			//~ fmt.Fprintln(os.Stderr,"put", a, "now_curr", curr)
			continue
		}

		cut := max(1, (curr+a-h)/k)
		ans += int64(cut)
		curr -= cut * k
		if curr < 0 {
			curr = 0
		}
		//~ fmt.Fprintln(os.Stderr,"cut", cut, "now_curr", curr)
		//~ if cut == 0 {
		//~ fmt.Fprintln(os.Stderr,"break", cut)
		//~ break
		//~ }
		goto retry
	}
	ans += int64(curr / k)
	if curr%k != 0 {
		ans++
	}
	fmt.Println(ans)
}

type Input struct {
	*bufio.Scanner
}

var globalInput = wrapStdin()

func wrapStdin() Input {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	return Input{Scanner: sc}
}
func getInt() int {
	globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x
}
func getIntOk() (int, bool) {
	ok := globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x, ok
}
func getNint(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		v[i] = getInt()
	}
	return v
}
func getVint(v []int) bool {
	var ok bool
	for i := 0; i < len(v); i++ {
		v[i], ok = getIntOk()
	}
	return ok
}
func getWord() string {
	globalInput.Scan()
	return globalInput.Text()
}
func getWordOk() (string, bool) {
	ok := globalInput.Scan()
	return globalInput.Text(), ok
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}
func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}
func minStr(a, b string) string {
	if a < b {
		return a
	}
	return b
}
func maxStr(a, b string) string {
	if a < b {
		return b
	}
	return a
}
func remin(a *int, b int) bool {
	if b < *a {
		*a = b
		return true
	}
	return false
}
func remax(a *int, b int) bool {
	if b > *a {
		*a = b
		return true
	}
	return false
}
func remin64(a *int64, b int64) bool {
	if b < *a {
		*a = b
		return true
	}
	return false
}
func remax64(a *int64, b int64) bool {
	if b > *a {
		*a = b
		return true
	}
	return false
}
func reminStr(a *string, b string) bool {
	if b < *a {
		*a = b
		return true
	}
	return false
}
func remaxStr(a *string, b string) bool {
	if b > *a {
		*a = b
		return true
	}
	return false
}

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

type (
	FLess    func(i, j int) bool
	FSwap    func(i, j int)
	wrapSort struct {
		Length int
		FLess
		FSwap
	}
)

func (w *wrapSort) Len() int           { return w.Length }
func (w *wrapSort) Less(i, j int) bool { return w.FLess(i, j) }
func (w *wrapSort) Swap(i, j int)      { w.FSwap(i, j) }
func wsort(length int, fless func(i, j int) bool, fswap func(i, j int)) {
	sort.Sort(&wrapSort{length, fless, fswap})
}

package main

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

func main() {
	n := getInt()
	h := getInt()
	k := getInt()
	var curr, ans int
	for i := 0; i < n; i++ {
		a := getInt()
		//~ fmt.Fprintln(os.Stderr,"get", a)
	retry:
		if a+curr <= h {
			curr += a
			//~ fmt.Fprintln(os.Stderr,"put", a, "now_curr", curr)
			continue
		}

		cut := max(1, (curr + a - h) / k)
		ans += cut
		curr -= cut * k
		if curr < 0 {
			curr = 0
		}
		//~ fmt.Fprintln(os.Stderr,"cut", cut, "now_curr", curr)
		//~ if cut == 0 {
			//~ fmt.Fprintln(os.Stderr,"break", cut)
			//~ break
		//~ }
		goto retry
	}
	ans += curr / k
	if curr%k != 0 {
		ans++
	}
	fmt.Println(ans)
}

type Input struct {
	*bufio.Scanner
}

var globalInput = wrapStdin()

func wrapStdin() Input {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	return Input{Scanner: sc}
}
func getInt() int {
	globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x
}
func getIntOk() (int, bool) {
	ok := globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x, ok
}
func getNint(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		v[i] = getInt()
	}
	return v
}
func getVint(v []int) bool {
	var ok bool
	for i := 0; i < len(v); i++ {
		v[i], ok = getIntOk()
	}
	return ok
}
func getWord() string {
	globalInput.Scan()
	return globalInput.Text()
}
func getWordOk() (string, bool) {
	ok := globalInput.Scan()
	return globalInput.Text(), ok
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}
func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}
func minStr(a, b string) string {
	if a < b {
		return a
	}
	return b
}
func maxStr(a, b string) string {
	if a < b {
		return b
	}
	return a
}
func remin(a *int, b int) bool {
	if b < *a {
		*a = b
		return true
	}
	return false
}
func remax(a *int, b int) bool {
	if b > *a {
		*a = b
		return true
	}
	return false
}
func remin64(a *int64, b int64) bool {
	if b < *a {
		*a = b
		return true
	}
	return false
}
func remax64(a *int64, b int64) bool {
	if b > *a {
		*a = b
		return true
	}
	return false
}
func reminStr(a *string, b string) bool {
	if b < *a {
		*a = b
		return true
	}
	return false
}
func remaxStr(a *string, b string) bool {
	if b > *a {
		*a = b
		return true
	}
	return false
}

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

type (
	FLess    func(i, j int) bool
	FSwap    func(i, j int)
	wrapSort struct {
		Length int
		FLess
		FSwap
	}
)

func (w *wrapSort) Len() int           { return w.Length }
func (w *wrapSort) Less(i, j int) bool { return w.FLess(i, j) }
func (w *wrapSort) Swap(i, j int)      { w.FSwap(i, j) }
func wsort(length int, fless func(i, j int) bool, fswap func(i, j int)) {
	sort.Sort(&wrapSort{length, fless, fswap})
}


Sereja is a coder and he likes to take part in Codesorfes rounds. However, Uzhland doesn't have good internet connection, so Sereja sometimes skips rounds.Codesorfes has rounds of two types: Div1 (for advanced coders) and Div2 (for beginner coders). Two rounds, Div1 and Div2, can go simultaneously, (Div1 round cannot be held without Div2) in all other cases the rounds don't overlap in time. Each round has a unique identifier — a positive integer. The rounds are sequentially (without gaps) numbered with identifiers by the starting time of the round. The identifiers of rounds that are run simultaneously are different by one, also the identifier of the Div1 round is always greater.Sereja is a beginner coder, so he can take part only in rounds of Div2 type. At the moment he is taking part in a Div2 round, its identifier equals to x. Sereja remembers very well that he has taken part in exactly k rounds before this round. Also, he remembers all identifiers of the rounds he has taken part in and all identifiers of the rounds that went simultaneously with them. Sereja doesn't remember anything about the rounds he missed.Sereja is wondering: what minimum and what maximum number of Div2 rounds could he have missed? Help him find these two numbers.

The first line contains two integers: x (1 ≤ x ≤ 4000) — the round Sereja is taking part in today, and k (0 ≤ k < 4000) — the number of rounds he took part in. Next k lines contain the descriptions of the rounds that Sereja took part in before. If Sereja took part in one of two simultaneous rounds, the corresponding line looks like: "1 num2 num1" (where num2 is the identifier of this Div2 round, num1 is the identifier of the Div1 round). It is guaranteed that num1 - num2 = 1. If Sereja took part in a usual Div2 round, then the corresponding line looks like: "2 num" (where num is the identifier of this Div2 round). It is guaranteed that the identifiers of all given rounds are less than x.

Print in a single line two integers — the minimum and the maximum number of rounds that Sereja could have missed.

NoteIn the second sample we have unused identifiers of rounds 1, 6, 7. The minimum number of rounds Sereja could have missed equals to 2. In this case, the round with the identifier 1 will be a usual Div2 round and the round with identifier 6 will be synchronous with the Div1 round. The maximum number of rounds equals 3. In this case all unused identifiers belong to usual Div2 rounds.

package main

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

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

//Wrong
func main() {
	// defer func(start time.Time) {
	// 	fmt.Println(time.Since(start))
	// }(time.Now())

	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	x, k := nextInt(), nextInt()

	takenRounds := make([]bool, x)
	round := 0
	for i:=0; i<k; i++ {
		round = nextInt()
		takenRounds[nextInt()] = true
		if round==1 {
			takenRounds[nextInt()] = true
		}
	}

	// fmt.Println(takenRounds)
	min, max := 0, 0

	for i:=1; i<x; i++ {
		if takenRounds[i] == false {
			max++
			min++
			if i+1<x && takenRounds[i+1]==false {
				max++
				i++
			}
		}
	}

	println(min, max)
}

func next() string {
	if !scanner.Scan() {
		panic("Scan error")
	}
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(next())
	if err != nil {
		panic(err)
	}
	return n
}

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

func print(a ...interface{}) {
	fmt.Fprint(writer, a...)
}

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

func main() {
	var x,k,c,t,max,min int
	a:=make([]bool,4040)
	fmt.Scan(&x,&k)
	for i:=0;i<k;i++ {
		fmt.Scan(&c)
		c=(c%2)+1
		for j:=0;j<c;j++ {
			fmt.Scan(&t)
			a[t] = true
		}
	}
	for i:=1;i<x;i++ {
		if a[i]==false {
			max++
		}
	}
	for i:=1;i<x;i++ {
		if a[i]==false {
			if a[i+1]==false {
				i++
				min++
			} else {
				min++
			}
		}
	}
	fmt.Println(min,max)
}

package main

import "fmt"

func main() {
    var x,k,t,num1,num2 int
    fmt.Scan(&x,&k)
    f := make([]bool,x)
    for i := 0; i < k; i++ {
        fmt.Scan(&t,&num2)
        if t == 1 { fmt.Scan(&num1); f[num1] = true }
        f[num2] = true
    }
    min, max := 0, 0
    for i := 1; i < x; i++ {
        if !f[i] { max++ }
    }
    for i := 1; i < x; i++ {
        if !f[i] {
            min++
            if ((i+1) < x) && !f[i+1] { i++ }
        }
    }
    fmt.Println(min,max)
}

/*********************************************************************************
*     File Name           :     B.go
*     Created By          :     YIMMON, yimmon.zhuang@gmail.com
*     Creation Date       :     [2014-03-10 23:38]
*     Last Modified       :     [2014-03-10 23:58]
*     Description         :
**********************************************************************************/

package main

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

func main() {
    scanner.Split(bufio.ScanWords)

    x, k := scanner.NextInt(), scanner.NextInt()
    var vst = make([]bool, x)
    for i := 0; i < k; i++ {
        t := scanner.NextInt()
        vst[scanner.NextInt()] = true
        if t == 1 {
            vst[scanner.NextInt()] = true
        }
    }
    var beg = 1
    for beg < x && vst[beg] {
        beg++
    }
    var m1, m2 int = 0, 0
    for i := beg; i < x; i++ {
        if vst[i] {
            m1 += (i - beg + 1) / 2
            beg = i + 1
            for beg < x && vst[beg] {
                beg++
            }
            if beg > i+1 {
                i = beg - 1
            }
        } else {
            m2++
        }
    }
    if beg < x {
        m1 += (x - beg + 1) / 2
    }
    fmt.Println(m1, m2)
}

type Scanner struct {
    *bufio.Scanner
}

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

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

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

// 401B-mic
package main

import (
    "fmt"
)

func main() {
    var n, k, l, m, j, cou, cou1 int
    var a [100001]int
    fmt.Scan(&n, &k)
    for i := k; i > 0; i-- {
        fmt.Scan(&j)
        if j == 1 {
            fmt.Scan(&l, &m)
            a[l] = 1
            a[m] = 1
            cou += 2
        } else {
            fmt.Scan(&l)
            a[l] = 1
            cou++
        }
    }

    max1 := n - 1 - cou
    i := 1
    for i <= n-1 {
        if a[i] == 0 && a[i+1] == 0 {
            cou1++
            i = i + 2
        } else if a[i] == 0 {
            cou1++
            i++
        } else {
            i++
        }
    }
    fmt.Print(cou1)
    fmt.Print(" ")
    fmt.Print(max1)
    fmt.Println()
}

package main

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

func main() {
    scanner.Split(bufio.ScanWords)
    x := scanner.NextInt()
    fill := map[int]bool{}
    for k := scanner.NextInt(); k > 0; k-- {
        num := scanner.NextInt()
        x1 := scanner.NextInt()
        fill[x1] = true
        if num == 1 {
            x1 = scanner.NextInt()
            fill[x1] = true
        }
    }
    max := 0
    min := 0
    for x--; x > 0; x-- {
        if fill[x] {
            continue
        }
        max++
        min++
        if x > 1 && !fill[x-1] {
            max++
            fill[x-1] = true
        }
    }
    fmt.Printf("%d %d\n", min, max)
}

type Scanner struct {
    *bufio.Scanner
}

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

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

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

func (self *Scanner) NextDouble() float64 {
    x, err := strconv.ParseFloat(self.Next(), 64)
    if err != nil {
        panic(err)
    }
    return x
}

/*********************************************************************************
*     File Name           :     B.go
*     Created By          :     YIMMON, yimmon.zhuang@gmail.com
*     Creation Date       :     [2014-03-10 23:38]
*     Last Modified       :     [2014-03-10 23:54]
*     Description         :
**********************************************************************************/

package main

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

func main() {
    scanner.Split(bufio.ScanWords)

    x, k := scanner.NextInt(), scanner.NextInt()
    var vst = make([]bool, x)
    for i := 0; i < k; i++ {
        t := scanner.NextInt()
        vst[scanner.NextInt()] = true
        if t == 1 {
            vst[scanner.NextInt()] = true
        }
    }
    var beg = 1
    for beg < x && vst[beg] {
        beg++
    }
    var m1, m2 int = 0, 0
    for i := beg; i < x; i++ {
        if vst[i] {
            m1 += (i - beg + 1) / 2
            beg = i + 1
            for beg < x && vst[beg] {
                beg++
            }
            if beg > i+1 {
                i = beg - 1
            }
        } else {
            m2++
        }
    }
    fmt.Println(m1, m2)
}

type Scanner struct {
    *bufio.Scanner
}

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

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

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


The Little Elephant enjoys recursive functions.This time he enjoys the sorting function. Let a is a permutation of an integers from 1 to n, inclusive, and ai denotes the i-th element of the permutation. The Little Elephant's recursive function f(x), that sorts the first x permutation's elements, works as follows:  If x = 1, exit the function.  Otherwise, call f(x - 1), and then make swap(ax - 1, ax) (swap the x-th and (x - 1)-th elements of a). The Little Elephant's teacher believes that this function does not work correctly. But that-be do not get an F, the Little Elephant wants to show the performance of its function. Help him, find a permutation of numbers from 1 to n, such that after performing the Little Elephant's function (that is call f(n)), the permutation will be sorted in ascending order.

A single line contains integer n (1 ≤ n ≤ 1000) — the size of permutation.

In a single line print n distinct integers from 1 to n — the required permutation. Numbers in a line should be separated by spaces. It is guaranteed that the answer exists.

package main

import "fmt"

func main() {
    var n int
    fmt.Scan(&n)
    fmt.Print(n)
    for i := 1; i < n; i++ {
        fmt.Print(" ", i)
    }

}

package main

import (
	"fmt"
)

func main() {
	var a int
	fmt.Scan(&a)
	fmt.Print(a," ")
	for i:=1;i<a;i++ {
		fmt.Print(i," ")
	}
}

package main

import "fmt"

func main() {
    var n int
    fmt.Scan(&n)
    fmt.Print(n)
    for i := 1; i < n; i++ { fmt.Print(" ",i) }
    fmt.Println()
}

package main

import "fmt"

func main() {
    var n int		// membuat variabel n dengan jenis int
    fmt.Scan(&n)    // mengambil input
    fmt.Print(n)    // mencetak n
    for i := 1; i < n; i++ { fmt.Print(" ",i) } // looping kemudian cetak spasi dan i
    fmt.Println()   // mencetak enter
}

package main

import "fmt"

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

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

	for i := 0; i < n; i++ {
		fmt.Print(an[i], " ")
	}
	fmt.Println()
}

// 221A-mic
package main

import (
    "fmt"
)

func main() {
    var n int
    fmt.Scan(&n)
    fmt.Print(n)
    fmt.Printf(" ")
    for i:=1;i<n;i++ {
    fmt.Print(i)
    fmt.Print(" ")
    }
}

package main

import "fmt"

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

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

	for i := 0; i < n; i++ {
		fmt.Print(an[i], " ")
	}
	fmt.Println()
}


This is the easy version of the problem. The only difference is that in this version $$$n \leq 2000$$$. You can make hacks only if both versions of the problem are solved.There are $$$n$$$ potions in a line, with potion $$$1$$$ on the far left and potion $$$n$$$ on the far right. Each potion will increase your health by $$$a_i$$$ when drunk. $$$a_i$$$ can be negative, meaning that potion will decrease will health.You start with $$$0$$$ health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative.What is the largest number of potions you can drink?

The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2000$$$) — the number of potions.  The next line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ... ,$$$a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) which represent the change in health after drinking that potion.

Output a single integer, the maximum number of potions you can drink without your health becoming negative.

NoteFor the sample, you can drink $$$5$$$ potions by taking potions $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$ and $$$6$$$. It is not possible to drink all $$$6$$$ potions because your health will go negative at some point

package main

import (
	"bufio"
	"container/heap"
	. "fmt"
	"io"
	"os"
	"sort"
)

// github.com/EndlessCheng/codeforces-go
type hp struct{ sort.IntSlice }

func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
func (h *hp) Pop() interface{}   { a := h.IntSlice; v := a[len(a)-1]; h.IntSlice = a[:len(a)-1]; return v }
func (h *hp) push(v int)         { heap.Push(h, v) }
func (h hp) empty() bool         { return len(h.IntSlice) == 0 }
func (h hp) top() int            { return h.IntSlice[0] }

func CF1526C2(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, cntP int
	Fscan(in, &n)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
		if a[i] >= 0 {
			cntP++
		}
	}

	s := int64(0)
	h := hp{}
	for _, v := range a {
		if v >= 0 {
			s += int64(v)
		} else {
			if s+int64(v) >= 0 {
				h.push(v)
				s += int64(v)
			} else if h.Len() > 0 && v > h.top() {
				s -= int64(h.top())
				s += int64(v)
				h.IntSlice[0] = v
				heap.Fix(&h, 0)
			}
		}
	}
	Fprint(out, cntP+h.Len())
}

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

package main

import (
	"bufio"
	"container/heap"
	. "fmt"
	"io"
	"os"
	"sort"
)

// github.com/EndlessCheng/codeforces-go
type hp struct{ sort.IntSlice }

func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
func (h *hp) Pop() interface{}   { a := h.IntSlice; v := a[len(a)-1]; h.IntSlice = a[:len(a)-1]; return v }
func (h *hp) push(v int)         { heap.Push(h, v) }
func (h *hp) pop() int           { return heap.Pop(h).(int) }
func (h hp) empty() bool         { return len(h.IntSlice) == 0 }
func (h hp) top() int            { return h.IntSlice[0] }

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

	var n, cntP int
	Fscan(in, &n)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
		if a[i] >= 0 {
			cntP++
		}
	}

	s := int64(0)
	h := hp{}
	for _, v := range a {
		if v >= 0 {
			s += int64(v)
		} else {
			if s+int64(v) >= 0 {
				h.push(v)
				s += int64(v)
			} else if !h.empty() {
				if v > h.top() {
					s -= int64(h.top())
					s += int64(v)
					h.IntSlice[0] = v
					heap.Fix(&h, 0)
				}
			}
		}
	}
	Fprintln(out, cntP+h.Len())
}

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

package main

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

const (
	MOD int64 = 1e9+7
	NL byte = byte('\n')
	SP byte = byte(' ')
)

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

	n int
	a [200000]int
	res int
)

func main() {
	initBuffer()
	var t int = 1
	// t = getInt()
	for i := 0; i < t; i++ {
		solve()
	}
	// test()
	out.Flush()
}

func solve() {
	initialize()
	input()
	count()
	output()
}

func initialize() {

}

func input() {
	n = getInt()
	for i := 0; i < n; i++ {
		a[i] = getInt()
	}
}

type IntHeap []int
type any = interface{}

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

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

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

func count() {
	var cur int64
	h := &IntHeap{}
	heap.Init(h)
	for i := 0; i < n; i++ {
		res++
		cur += int64(a[i])
		heap.Push(h, a[i])
		if cur < 0 {
			res--
			cur -= int64((*h)[0])
			heap.Pop(h)
		}
	}
}

func output() {
	out.Write(intToBytes(res))
}

func test() {
	fmt.Print()
}

func initBuffer() {
	in = bufio.NewScanner(os.Stdin)
	in.Buffer([]byte{}, 4 * 1024 * 1024)
	in.Split(scanWords)
	out = bufio.NewWriter(os.Stdout)
}

func scanWords(data []byte, atEOF bool) (advance int, token []byte, err error) {
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}
	i := bytes.IndexByte(data, '\n')
	j := bytes.IndexByte(data, ' ')
	ind := min(i, j)
	if ind < 0 {
		ind = max(i, j)
	}
	if ind >= 0 {
		return ind + 1, dropCR(data[0:ind]), nil
	}
	if atEOF {
		return len(data), dropCR(data), nil
	}
	return 0, nil, nil
}

func dropCR(data []byte) []byte {
	if len(data) > 0 && data[len(data)-1] == '\r' {
		return data[:len(data)-1]
	}
	return data
}

func getFloat() float64 {
	in.Scan()
	b := in.Bytes()
	var isNeg bool
	if b[0] == '-' {
		isNeg = true
		b = b[1:]
	}
	var res1 int64
	for i := 0; i < len(b); i++ {
		if b[i] == '.' {
			b = b[i+1:]
			break
		}
		res1 *= 10
		res1 += int64(b[i]) - 48
		if i == len(b) - 1 {
			b = b[:0]
		}
	}
	var res2 float64
	for i := len(b); i > 0; i-- {
		res2 += float64(b[i-1]) - 48
		res2 /= 10
	}
	if isNeg {
		res1 *= -1
		res2 *= -1
	}
	return float64(res1) + res2
}

func getInt() int {
	in.Scan()
	b := in.Bytes()
	var isNeg bool
	if b[0] == '-' {
		isNeg = true
		b = b[1:]
	}
	var res int
	for i := 0; i < len(b); i++ {
		res *= 10
		res += int(b[i]) - 48
	}
	if isNeg {
		res *= -1
	}
	return res
}

func getInt64() int64 {
	in.Scan()
	b := in.Bytes()
	var isNeg bool
	if b[0] == '-' {
		isNeg = true
		b = b[1:]
	}
	var res int64
	for i := 0; i < len(b); i++ {
		res *= 10
		res += int64(b[i]) - 48
	}
	if isNeg {
		res *= -1
	}
	return res
}

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

func intToBytes(n int) []byte {
	var res [33]byte
	var isNeg bool
	if n < 0 {
		isNeg = true
		n = -n
	}
	var l int = 33
	for n > 0 {
		l--
		res[l] = byte(n % 10 + 48)
		n /= 10
	}
	if l == 33 {
		l--
		res[l] = byte(48)
	}
	if isNeg {
		l--
		res[l] = byte(45)
	}
	return res[l:]
}

func int64ToBytes(n int64) []byte {
	var res [65]byte
	var isNeg bool
	if n < 0 {
		isNeg = true
		n = -n
	}
	var l int = 65
	for n > 0 {
		l--
		res[l] = byte(n % 10 + 48)
		n /= 10
	}
	if l == 65 {
		l--
		res[l] = byte(48)
	}
	if isNeg {
		l--
		res[l] = byte(45)
	}
	return res[l:]
}

func bsLower(arr []int, n int) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] < n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] >= n {
		return minN
	}
	return maxN
}

func bsLower64(arr []int64, n int64) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] < n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] >= n {
		return minN
	}
	return maxN
}

func bsUpper(arr []int, n int) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] <= n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] > n {
		return minN
	}
	return maxN
}

func bsUpper64(arr []int64, n int64) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] <= n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] > n {
		return minN
	}
	return maxN
}

func max(n1, n2 int) int {
	if n1 > n2 {
		return n1
	}
	return n2
}

func max64(n1, n2 int64) int64 {
	if n1 > n2 {
		return n1
	}
	return n2
}

func min(n1, n2 int) int {
	if n1 < n2 {
		return n1
	}
	return n2
}

func min64(n1, n2 int64) int64 {
	if n1 < n2 {
		return n1
	}
	return n2
}

func yes(res, isUpperCase bool) bool {
	var arr [4]string = [4]string{"No", "Yes", "NO", "YES"}
	var ind int
	if res == true {
		ind = 1
	}
	if isUpperCase == true {
		ind += 2
	}
	out.WriteString(arr[ind])
	out.WriteByte(NL)
	return res
}