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"
	"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"
	. "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"
	"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"
	"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
}


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"
	"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 (
  "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"
	"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})
}


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
}

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 (
	"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)
}

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


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

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()
}


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
}