You are a usual chat user on the most famous streaming platform. Of course, there are some moments when you just want to chill and spam something.More precisely, you want to spam the emote triangle of size $$$k$$$. It consists of $$$2k-1$$$ messages. The first message consists of one emote, the second one — of two emotes, ..., the $$$k$$$-th one — of $$$k$$$ emotes, the $$$k+1$$$-th one — of $$$k-1$$$ emotes, ..., and the last one — of one emote.For example, the emote triangle for $$$k=3$$$ consists of $$$5$$$ messages:  Of course, most of the channels have auto moderation. Auto moderator of the current chat will ban you right after you spam at least $$$x$$$ emotes in succession (you can assume you are the only user in the chat). Now you are interested — how many messages will you write before getting banned? Or maybe you will not get banned at all (i.e. will write all $$$2k-1$$$ messages and complete your emote triangle successfully)? Note that if you get banned as a result of writing a message, this message is also counted.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The next $$$t$$$ lines describe test cases. The only line of the test case contains integers $$$k$$$ and $$$x$$$ ($$$1 \le k \le 10^9; 1 \le x \le 10^{18}$$$).

For each test case, print the number of messages you will write before getting banned for the corresponding values $$$k$$$ and $$$x$$$.

NoteLet's analyze the test cases of the example.  In the first test case, you write three messages containing $$$1$$$, $$$2$$$ and $$$3$$$ emotes respectively, and since $$$1 + 2 + 3 \ge 6$$$, you get banned after that.  In the second test case, you write four messages containing $$$1$$$, $$$2$$$, $$$3$$$ and $$$4$$$ emotes respectively, and since $$$1 + 2 + 3 + 4 \ge 7$$$, you get banned after that.  In the third test case, you write one message containing exactly $$$1$$$ emote. It doesn't get you banned, since $$$1 < 2$$$, but you have already finished posting your emote triangle. So you wrote one message successfully.  In the fourth test case, you write four messages containing $$$1$$$, $$$2$$$, $$$3$$$ and $$$2$$$ emotes respectively, and since $$$1 + 2 + 3 + 2 \ge 7$$$, you get banned after that.  In the fifth test case, you write three messages containing $$$1$$$, $$$2$$$ and $$$1$$$ emote respectively. It doesn't get you banned, since $$$1 + 2 + 1 < 5$$$, but you have already finished posting your emote triangle. So you wrote three messages successfully.  In the sixth test case, since $$$x = 1$$$, you get banned as soon as you send your first message.  The seventh test case is too large to analyze, so we'll skip it.

package main

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

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

	var T int
	fmt.Fscan(in, &T)

	for t := 0; t < T; t++ {
		var k, x int64
		fmt.Fscan(in, &k, &x)

		search := func(n int64) bool {
			var tot int64

			if n > k {
				tot = k * (k + 1) / 2
				n -= k
				c := k - n
				tot += k*(k-1)/2 - (c * (c - 1) / 2)
			} else {
				tot = n * (n + 1) / 2
			}
			return tot < x
		}

		l, r := int64(0), 2*k-1
		for r-l > 1 {
			m := (r + l) / 2
			if search(m) {
				l = m
			} else {
				r = m
			}
		}

		fmt.Fprintln(out, r)
	}
}

package main

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

var k, x int64

func main() {

	r := bufio.NewReader(os.Stdin)

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

	for i := 0; i < t; i++ {
		fmt.Fscan(r, &k, &x)

		a := solve()

		fmt.Println(a)
	}

}

type pair struct {
	v, id int
}

type ByV []*pair

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

type item struct {
	la, ra int
	lb, rb int
}

const MAX int = 1e6

func solve() int64 {
	s := k * (k + 1)/2

	if s >= x {

		var lo, hi int64 = 1, k

		for ; hi - lo > 1; {

			m := (hi + lo)/2

			ss := m * (m + 1)/2

			if ss > x {
				hi = m

			} else if ss < x {

				lo = m

			} else {
				return m
			}
		}

		s1 := lo * (lo + 1)/2

		if s1 >= x {
			return lo
		}

		return hi

	} else {
		x -= s

		tot := (k - 1) * (k) / 2

		if tot <= x {
			return k + (k - 1)
		}

		var lo, hi int64 = 1, k

		for ;hi - lo > 1; {

			m := (lo + hi)/2

			ss := tot - m * (m + 1)/2

			if x > ss {
				hi = m

			} else if x < ss {
				lo = m

			} else {
				return k + (k - 1) - m
			}
		}

		s1 := tot - hi * (hi + 1)/2

		if s1 >= x {
			return (k - 1) - hi + k
		}

		return (k - 1) - lo + k

	}
}

//1000 000 001 000 000 000

func add(idx int, delta int, bit *[]int, n int) {
	for ; idx <= n; idx += idx & -idx {
		(*bit)[idx] += delta
	}
}

func get(idx int, bit *[]int) int {
	ret := 0
	for ; idx > 0; idx -= idx & -idx {
		ret += (*bit)[idx];
	}
	return ret
}

//A -  1 3 5 3 1

//B -  4 3 6 1

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

package main

import (
	"fmt"
)

func main() {
	getSum := func(l int64, r int64) int64 {
		return (r*(r+1))/2 - (l*(l-1))/2
	}
	var t int32
	fmt.Scan(&t)
	for tc := int32(0); tc < t; tc++ {
		var k, x, ans int64
		fmt.Scan(&k, &x)
		l := int64(1)
		r := 2*k - 1
		ans = 0
		check := func(mid int64) bool {
			if mid > k {
				return x > 2*getSum(1, k-1)+k-getSum(1, 2*k-1-mid)
			} else {
				return x > getSum(1, mid)
			}
		}
		for l <= r {
			mid := (l + r) / 2
			if check(mid) {
				ans = mid
				l = mid + 1
			} else {
				r = mid - 1
			}
		}
		if ans < 2*k-1 {
			ans++
		}
		fmt.Println(ans)
	}
}

package main

import (
	"fmt"
)

func main() {
	getSum := func(l int64, r int64) int64 {
		if l > r {
			return int64(0)
		}
		return (r*(r+1))/2 - (l*(l-1))/2
	}
	var t int32
	fmt.Scan(&t)
	for tc := int32(0); tc < t; tc++ {
		var k, x, ans int64
		fmt.Scan(&k, &x)
		l := int64(1)
		r := 2*k - 1
		ans = 0
		check := func(mid int64) bool {
			if mid > k {
				return x > 2*getSum(1, k-1)+k-getSum(1, 2*k-1-mid)
			} else {
				return x > getSum(1, mid)
			}
		}
		for l <= r {
			mid := (l + r) / 2
			if check(mid) {
				ans = mid
				l = mid + 1
			} else {
				r = mid - 1
			}
		}
		if ans < 2*k-1 {
			ans++
		}
		fmt.Println(ans)
	}
}

package main

import "fmt"

func main() {
	var t, k, x int64
	fmt.Scanln(&t)
	for i := int64(0); i < t; i++ {
		fmt.Scanln(&k, &x)
		fmt.Println(binarySearch(k*2-1, x))
	}
}

func binarySearch(msgCount, banCount int64) int64 {
	left := int64(0)
	right := msgCount
	for {
		mid := (left + right) / 2
		if sumOfNMembers(mid, msgCount) > banCount {
			right = mid
		} else {
			left = mid
		}
		if right-left <= 1 {
			if sumOfNMembers(mid-1, msgCount) >= banCount {
				return mid - 1
			} else if sumOfNMembers(mid, msgCount) >= banCount {
				return mid
			} else {
				return mid + 1
			}
		}
	}
}

func nthMember(n, total int64) int64 {
	if n*2-1 <= total {
		return 1 + (n - 1)
	}
	return total/2 + 1 - (n - (total/2 + 1))
}

func sumOfNMembers(n, total int64) int64 {
	if n*2-1 <= total {
		return ((1 + nthMember(n, total)) * n) / 2
	}
	return ((1+nthMember(total/2+1, total))*(total/2+1))/2 + (nthMember(n, total)+total/2)*(n-(total/2+1))/2
}

package main

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

func countEmojis(k, n int64) int64 {
  if n <= k {
    return (1 + n) * n / 2
  }
  return k * (k - 1) / 2 - (k - n - 1) * (3*k - n) / 2
}

func solve(sc *bufio.Scanner, wr *bufio.Writer) {
  sc.Scan()
  k, _ := strconv.ParseInt(sc.Text(), 10, 64)

  sc.Scan()
  x, _ := strconv.ParseInt(sc.Text(), 10, 64)

  l, r := int64(0), 2 * k
  for r - l > 1 {
    m := (l + r) / 2
    if countEmojis(k, m) < x {
      l = m
    } else {
      r = m
    }
  }

  if r > 2*k - 1 {
    r = 2*k - 1
  }

  wr.WriteString(strconv.FormatInt(r, 10))
  wr.WriteRune('\n')
}

const once = false

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

  wr := bufio.NewWriter(os.Stdout)
  defer wr.Flush()

  if once {
    solve(sc, wr)
  } else {
    sc.Scan()
    times, _ := strconv.Atoi(sc.Text())
    for times > 0 {
      solve(sc, wr)
      times--
    }
  }
}

package main

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

/*
9
3 1
3 2
3 3
3 4
3 5
3 6
3 7
3 8
3 9
---
1
2
2
3
3
3
4
4
5
======
1
1 2
---
1
*/

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

	bld := strings.Builder{}

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

	for ; t > 0; t-- {
		var k, x int64
		scanf("%d %d\n", &k, &x)

		lftIdx := int64(1)
		rgtIdx := 2*k - 1

		ans := 2*k - 1

	loop:
		for lftIdx <= rgtIdx {
			midIdx := (lftIdx + rgtIdx) / 2

			midStart := int64(0)
			midEnd := int64(0)

			if midIdx <= k {
				midEnd = midIdx * (midIdx + 1) / 2
				midStart = midEnd - midIdx + 1
			} else {
				midEnd = k*(k+1)/2 + ((midIdx-k)*(abs(midIdx-2*k)+k-1))/2
				midStart = midEnd - abs(midIdx-2*k) + 1
			}

			switch {
			case x >= midStart && x <= midEnd:
				ans = midIdx
				break loop
			case x < midStart:
				rgtIdx = midIdx - 1
			case x > midEnd:
				lftIdx = midIdx + 1
			}
		}

		fmt.Fprintln(&bld, ans)
	}

	printf("%s", bld.String())
}

const maxUint = ^uint(0)

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

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

//goland:noinspection GoUnusedType
type pair struct {
	x int64
	y int64
}

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

//goland:noinspection GoUnusedFunction
func printf(f string, a ...interface{}) {
	if _, err := fmt.Fprintf(writer, f, a...); err != nil {
		panic(err)
	}
}

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

//goland:noinspection GoUnusedFunction
func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

//goland:noinspection GoUnusedFunction
func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

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

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

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

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

	return true
}

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

//goland:noinspection GoUnusedFunction
//func lcm(a, b int) int64 {
//	return abs(a) / gcd(a, b) * abs(b)
//}
//
//// Examples:
////	digitAt(1234, -1) -> panic!
////	digitAt(1234, 0) -> 4
////	digitAt(1234, 1) -> 3
////	digitAt(1234, 2) -> 2
////	digitAt(1234, 3) -> 1
////	digitAt(1234, 4) -> panic!
////goland:noinspection GoUnusedFunction
//func digitAt(num, pos int) int {
//	num = abs(num)
//
//	lenNum := int(math.Log10(float64(num))) + 1
//	if pos < 0 || pos >= lenNum {
//		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
//		panic(err)
//	}
//
//	rem := num % int(math.Pow10(pos+1))
//	return rem / int(math.Pow10(pos))
//}

package main

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

var sc = bufio.NewScanner(os.Stdin)

func scan() string {
	for sc.Scan() {
		break
	}
	return sc.Text()
}

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

func scanInt8() int8 {
	i, err := strconv.ParseInt(scan(), 10, 8)
	if err != nil {
		panic(err)
	}
	return int8(i)
}

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

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

func min(ints ...int) int {
	a := ints[0]
	n := len(ints)
	for i := 1; i < n; i++ {
		if ints[i] < a {
			a = ints[i]
		}
	}
	return a
}

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

type Noop struct{}

var noop = Noop{}

func count(k int64, m int64) int64 {
	if m <= k {
		return (1 + m) * m / 2
	}
	return (1+k)*k/2 + ((k-1)+(k-(m-k)))*(m-k)/2
}

func proc() {
	k, x := scanInt64(), scanInt64()
	a := int64(1)
	b := k + k - 1
	for a < b {
		m := (a + b) / 2
		c := count(k, m)
		// fmt.Printf("a: %d b: %d m: %d c: %d\n", a, b, m, c)
		if c < x {
			a = m + 1
		} else {
			b = m
		}
	}
	fmt.Println(a)
}

func main() {
	sc.Split(bufio.ScanWords)
	t := scanInt()
	for i := 0; i < t; i++ {
		proc()
	}
}

package main

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

var r = bufio.NewReader(os.Stdin)

func check(mid, x, k int64) bool {
	var ans = mid * (mid - 1) / 2

	if mid <= k {
		return ans < x
	} else {
		ans = k * (k + 1) / 2
		dif := mid - k
		an := k - 1
		a2 := k - dif + 1
		sum := (a2 + an) * (dif-1) / 2
		return ans + sum < x
	}
}

/*
999 1000 999 998 997 996
*/

func solve(){
	var k, x int64
	fmt.Fscan(r, &k, &x)

	var l, r int64 = 0, 2*k

	for l + 1 < r {
		mid := (l + r) / 2
		if check(mid, x, k) {
			l = mid
		} else {
			r = mid
		}
	}

	fmt.Println(l)
}

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

package main

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

var (
	input = bufio.NewReader(os.Stdin)
	output = bufio.NewWriter(os.Stdout)
)

func assert(test bool) {
    if !test {panic("assert!")}
}

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
// TODO***
// - read the statement carefully
// - get solution before coding
// - check the boundaries (int/int64)

func main() {
    defer output.Flush()
    //solve()
    //return
	var t int64
	fmt.Fscan(input, &t)
    for t > 0 {
        t--
        solve()
    }
}

func solve() {
    var k, x int64
    fmt.Fscan(input, &k, &x)

    ok := func(v int64) bool {
        var tot int64

        if v > k {
            tot = k * (k + 1) / 2
            v -= k
            c := k - v
            tot += k * (k - 1) / 2 - (c * (c - 1) / 2)
        } else {
            tot = v * (v + 1) / 2
        }

        return tot < x
    }

    l, r := int64(0), 2*k - 1

    for r - l > 1 {
        m := (l + r) / 2
        if ok(m) {
            l = m
        } else {
            r = m
        }
    }
    fmt.Fprintln(output, r)
}

package main

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

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

	var T int
	fmt.Fscan(in, &T)

	for t := 0; t < T; t++ {
		var k, x int64
		fmt.Fscan(in, &k, &x)

		sum := k * (k + 1) / 2
		search := func(p, n int64) int64 {
			if p*(p+1)/2 <= n {
				return p
			}

			l, r := int64(1), int64(p)
			for r-l >= 1 {
				z := (l + r) / 2
				if z*(z+1)/2 < n {
					l = z + 1
				} else {
					r = z
				}
			}
			return r
		}

		var t int64
		if sum >= x {
			t = search(k, x)
		} else {
			t += k
			k = k - 1
			x -= sum
			for k*(k+1)/2 >= x {
				k--
			}
			t += k
		}

		if t > 1608737403 {
			fmt.Fprintln(out, 1608737403)
		} else {
			fmt.Fprintln(out, t)
		}
	}
}

package main

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

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

	var T int
	fmt.Fscan(in, &T)

	for t := 0; t < T; t++ {
		var k, x int64
		fmt.Fscan(in, &k, &x)

		sum := k * (k + 1) / 2
		search := func(p, n int64) int64 {
			if p*(p+1)/2 <= n {
				return p
			}

			l, r := int64(1), int64(p)
			for r-l >= 1 {
				z := (l + r) / 2
				if z*(z+1)/2 < n {
					l = z + 1
				} else {
					r = z
				}
			}
			return r
		}

		var t int64
		if sum >= x {
			t = search(k, x)
		} else {
			t = k + search(k-1, x-sum)
		}

		if t > 1608737403 {
			fmt.Fprintln(out, 1608737403)
		} else {
			fmt.Fprintln(out, t)
		}
	}
}

package main

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

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

	var T int
	fmt.Fscan(in, &T)

	for t := 0; t < T; t++ {
		var k, x int64
		fmt.Fscan(in, &k, &x)

		sum := k * (k + 1) / 2
		search := func(p, n int64) int64 {
			if p*(p+1)/2 <= n {
				return p
			}

			l, r := int64(1), int64(p)
			for r-l >= 1 {
				z := (l + r) / 2
				if z*(z+1)/2 < n {
					l = z + 1
				} else {
					r = z
				}
			}
			return r
		}

		var t int64
		if sum > x {
			t = search(k, x)
		} else {
			t = k + search(k-1, x-sum)
		}

		if t > 1608737403 {
			fmt.Fprintln(out, 1608737403)
		} else {
			fmt.Fprintln(out, t)
		}
	}
}

package main

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

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

	var T int
	fmt.Fscan(in, &T)

	for t := 0; t < T; t++ {
		var k, x int64
		fmt.Fscan(in, &k, &x)

		sum := k * (k + 1) / 2
		search := func(p, n int64) int64 {
			if p*(p+1)/2 <= n {
				return p
			}

			l, r := int64(1), int64(p)
			for r-l >= 1 {
				z := (l + r) / 2
				if z*(z+1)/2 < n {
					l = z + 1
				} else {
					r = z
				}
			}
			return r
		}

		var t int64
		if sum > x {
			t = search(k, x)
		} else {
			t = k + search(k-1, x-sum)
		}

		fmt.Fprintln(out, t)
	}
}

package main

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

var k, x int64

func main() {

	r := bufio.NewReader(os.Stdin)

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

	for i := 0; i < t; i++ {
		fmt.Fscan(r, &k, &x)

		a := solve()

		fmt.Println(a)
	}

}

type pair struct {
	v, id int
}

type ByV []*pair

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

type item struct {
	la, ra int
	lb, rb int
}

const MAX int = 1e6

func solve() int64 {
	s := k * (k + 1)/2

	if s >= x {

		var lo, hi int64 = 1, k

		for ; hi - lo > 1; {

			m := (hi + lo)/2

			ss := m * (m + 1)/2

			if ss > x {
				hi = m

			} else if ss < x {

				lo = m

			} else {
				return m
			}
		}

		s1 := lo * (lo + 1)/2

		if s1 >= x {
			return lo
		}

		return hi

	} else {
		x -= s

		tot := (k - 1) * (k) / 2

		if tot < x {
			return k + (k - 1)
		}

		var lo, hi int64 = 1, k

		for ;hi - lo > 1; {

			m := (lo + hi)/2

			ss := tot - m * (m + 1)/2

			if x > ss {
				hi = m

			} else if x < ss {
				lo = m

			} else {
				return k + (k - 1) - m
			}
		}

		s1 := tot - hi * (hi + 1)/2

		if s1 >= x {
			return (k - 1) - hi + k
		}

		return (k - 1) - lo + k

	}
}

//1000 000 001 000 000 000

func add(idx int, delta int, bit *[]int, n int) {
	for ; idx <= n; idx += idx & -idx {
		(*bit)[idx] += delta
	}
}

func get(idx int, bit *[]int) int {
	ret := 0
	for ; idx > 0; idx -= idx & -idx {
		ret += (*bit)[idx];
	}
	return ret
}

//A -  1 3 5 3 1

//B -  4 3 6 1

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

package main

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

var k, x int64

func main() {

	r := bufio.NewReader(os.Stdin)

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

	for i := 0; i < t; i++ {
		fmt.Fscan(r, &k, &x)

		a := solve()

		fmt.Println(a)
	}

}

type pair struct {
	v, id int
}

type ByV []*pair

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

type item struct {
	la, ra int
	lb, rb int
}

const MAX int = 1e6

func solve() int64 {
	s := k * (k + 1)/2

	if s > x {

		var lo, hi int64 = 1, k

		for ; hi - lo > 1; {

			m := (hi + lo)/2

			ss := m * (m + 1)/2

			if ss > x {
				hi = m

			} else if ss < x {

				lo = m

			} else {
				return m
			}
		}

		s1 := lo * (lo + 1)/2

		if s1 >= x {
			return lo
		}

		return hi

	} else {
		x -= s

		tot := (k - 1) * (k) / 2

		if tot < x {
			return k + (k - 1)
		}

		var lo, hi int64 = 1, k

		for ;hi - lo > 1; {

			m := (lo + hi)/2

			ss := tot - m * (m + 1)/2

			if x > ss {
				hi = m

			} else if x < ss {
				lo = m

			} else {
				return k + (k - 1) - m
			}
		}

		s1 := tot - hi * (hi + 1)/2

		if s1 >= x {
			return (k - 1) - hi + k
		}

		return (k - 1) - lo + k

	}
}

//1000 000 001 000 000 000

func add(idx int, delta int, bit *[]int, n int) {
	for ; idx <= n; idx += idx & -idx {
		(*bit)[idx] += delta
	}
}

func get(idx int, bit *[]int) int {
	ret := 0
	for ; idx > 0; idx -= idx & -idx {
		ret += (*bit)[idx];
	}
	return ret
}

//A -  1 3 5 3 1

//B -  4 3 6 1

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

package main

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

var k, x int64

func main() {

	r := bufio.NewReader(os.Stdin)

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

	for i := 0; i < t; i++ {
		fmt.Fscan(r, &k, &x)

		a := solve()

		fmt.Println(a)
	}

}

type pair struct {
	v, id int
}

type ByV []*pair

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

type item struct {
	la, ra int
	lb, rb int
}

const MAX int = 1e6

func solve() int64 {
	s := k * (k + 1)/2

	if s > x {

		var lo, hi int64 = 1, k

		for ; hi - lo > 1; {

			m := (hi + lo)/2

			ss := m * (m + 1)/2

			if ss > x {
				hi = m

			} else if ss < x {

				lo = m

			} else {
				return m
			}
		}

		s1 := lo * (lo + 1)/2

		if s1 >= x {
			return lo
		}

		return hi

	} else {
		x -= s

		tot := (k - 1) * (k) / 2

		if tot < x {
			return k + (k - 1)
		}

		var lo, hi int64 = 1, k

		for ;hi - lo > 1; {

			m := (lo + hi)/2

			ss := tot - m * (m + 1)/2

			if x > ss {
				hi = m

			} else if x < ss {
				lo = m

			} else {
				return k + m
			}
		}

		s1 := tot - hi * (hi + 1)/2

		if s1 >= x {
			return (k - 1) - hi + k
		}

		return (k - 1) - lo + k

	}
}

//1000 000 001 000 000 000

func add(idx int, delta int, bit *[]int, n int) {
	for ; idx <= n; idx += idx & -idx {
		(*bit)[idx] += delta
	}
}

func get(idx int, bit *[]int) int {
	ret := 0
	for ; idx > 0; idx -= idx & -idx {
		ret += (*bit)[idx];
	}
	return ret
}

//A -  1 3 5 3 1

//B -  4 3 6 1

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

package main

import "fmt"

func main() {
	var t, k, x int64
	fmt.Scanln(&t)
	for i := int64(0); i < t; i++ {
		fmt.Scanln(&k, &x)
		println(binarySearch(k*2-1, x))
	}
}

func binarySearch(msgCount, banCount int64) int64 {
	left := int64(0)
	right := msgCount
	for {
		mid := (left + right) / 2
		if sumOfNMembers(mid, msgCount) > banCount {
			right = mid
		} else {
			left = mid
		}
		if right-left <= 1 {
			if sumOfNMembers(mid-1, msgCount) >= banCount {
				return mid - 1
			} else if sumOfNMembers(mid, msgCount) >= banCount {
				return mid
			} else {
				return mid + 1
			}
		}
	}
}

func nthMember(n, total int64) int64 {
	if n*2-1 <= total {
		return 1 + (n - 1)
	}
	return total/2 + 1 - (n - (total/2 + 1))
}

func sumOfNMembers(n, total int64) int64 {
	if n*2-1 <= total {
		return ((1 + nthMember(n, total)) * n) / 2
	}
	return ((1+nthMember(total/2+1, total))*(total/2+1))/2 + (nthMember(n, total)+total/2)*(n-(total/2+1))/2
}


Little Artem has invented a time machine! He could go anywhere in time, but all his thoughts of course are with computer science. He wants to apply this time machine to a well-known data structure: multiset.Artem wants to create a basic multiset of integers. He wants these structure to support operations of three types:  Add integer to the multiset. Note that the difference between set and multiset is that multiset may store several instances of one integer.  Remove integer from the multiset. Only one instance of this integer is removed. Artem doesn't want to handle any exceptions, so he assumes that every time remove operation is called, that integer is presented in the multiset.  Count the number of instances of the given integer that are stored in the multiset. But what about time machine? Artem doesn't simply apply operations to the multiset one by one, he now travels to different moments of time and apply his operation there. Consider the following example.  First Artem adds integer 5 to the multiset at the 1-st moment of time.  Then Artem adds integer 3 to the multiset at the moment 5.  Then Artem asks how many 5 are there in the multiset at moment 6. The answer is 1.  Then Artem returns back in time and asks how many integers 3 are there in the set at moment 4. Since 3 was added only at moment 5, the number of integers 3 at moment 4 equals to 0.  Then Artem goes back in time again and removes 5 from the multiset at moment 3.  Finally Artyom asks at moment 7 how many integers 5 are there in the set. The result is 0, since we have removed 5 at the moment 3. Note that Artem dislikes exceptions so much that he assures that after each change he makes all delete operations are applied only to element that is present in the multiset. The answer to the query of the third type is computed at the moment Artem makes the corresponding query and are not affected in any way by future changes he makes.Help Artem implement time travellers multiset.

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of Artem's queries. Then follow n lines with queries descriptions. Each of them contains three integers ai, ti and xi (1 ≤ ai ≤ 3, 1 ≤ ti, xi ≤ 109) — type of the query, moment of time Artem travels to in order to execute this query and the value of the query itself, respectively. It's guaranteed that all moments of time are distinct and that after each operation is applied all operations of the first and second types are consistent.

For each ask operation output the number of instances of integer being queried at the given moment of time.

package main

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

func main() {
	defer out.Flush()
	Q := in.NextInt()
	qs := make([]Query, Q)
	xTimes := make(map[int][]int)
	for i := range qs {
		qs[i].a = in.NextInt()
		qs[i].t = in.NextInt()
		qs[i].x = in.NextInt()
		xTimes[qs[i].x] = append(xTimes[qs[i].x], qs[i].t)
	}
	timeToXTime := make(map[int]int)
	fs := make(map[int]*Fenwick)
	for x, times := range xTimes {
		sort.Ints(times)
		for i, t := range times {
			timeToXTime[t] = i
		}
		fs[x] = NewFenwick(len(times))
	}
	for i := range qs {
		q := qs[i]
		f := fs[q.x]
		xt := timeToXTime[q.t]
		switch q.a {
		case 1:
			f.Set(xt, 1)
		case 2:
			f.Set(xt, -1)
		case 3:
			fmt.Fprintln(out, f.Sum(0, xt))
		}
	}
}

type Query struct {
	a, t, x int
}

type Fenwick struct {
	size int
	sum  []int
}

func NewFenwick(size int) *Fenwick {
	return &Fenwick{
		size,
		make([]int, size+1),
	}
}

func step(x int) int {
	return x & -x
}

func (f *Fenwick) Add(pos int, value int) {
	for i := pos + 1; i <= f.size; i += step(i) {
		f.sum[i] += value
	}
}

func (f *Fenwick) Set(pos int, value int) {
	f.Add(pos, value-f.Get(pos))
}

func (f *Fenwick) prefixSum(pos int) int {
	var result int
	for i := pos + 1; i > 0; i -= step(i) {
		result += f.sum[i]
	}
	return result
}

func (f *Fenwick) Sum(a, b int) int {
	return f.prefixSum(b) - f.prefixSum(a-1)
}

func (f *Fenwick) Get(a int) int {
	return f.Sum(a, a)
}

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

type FastReader struct {
	r   *bufio.Reader
	buf string
}

func NewFastReader(r io.Reader) *FastReader {
	return &FastReader{
		r: bufio.NewReader(r),
	}
}

func (r *FastReader) advance() {
	for len(r.buf) != 0 && r.buf[0] == ' ' {
		r.buf = r.buf[1:]
	}
	var err error
	for len(r.buf) == 0 && err != io.EOF {
		buf := new(bytes.Buffer)
		for {
			var chunk []byte
			var more bool
			chunk, more, err = r.r.ReadLine()
			buf.Write(chunk)
			if !more {
				break
			}
		}
		r.buf = buf.String()
	}
}

func (r *FastReader) NextLine() string {
	r.advance()
	line := r.buf
	r.buf = ""
	return line
}

func (r *FastReader) NextWord() string {
	r.advance()
	var word string
	wordStart := 0
	for i := 0; i < len(r.buf); i++ {
		b := r.buf[i]
		if b == ' ' {
			if i != wordStart {
				word = r.buf[wordStart:i]
				r.buf = r.buf[i:]
				break
			}
			wordStart = i + 1
		}
	}
	if len(word) == 0 {
		word = r.buf
		r.buf = ""
	}
	return word
}

func (r *FastReader) NextInt() int {
	return parseInt(r.NextWord())
}

func parseInt(word string) int {
	sign := 1
	if word[0] == '-' {
		sign = -1
		word = word[1:]
	}
	result := 0
	for i := 0; i < len(word); i++ {
		result = result*10 + int(word[i]) - '0'
	}
	result *= sign
	return result
}

package main

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

var in = bufio.NewScanner(os.Stdin)

func readInt() int {
	if !in.Scan() {
		panic(in.Err())
	}
	res := 0
	for _, b := range in.Bytes() {
		res = res*10 + int(b-'0')
	}
	return res
}

var ts = 0

type fenwick [][2]int

func (f fenwick) get(x int) int {
	if f[x][0] < ts {
		f[x][0] = ts
		f[x][1] = 0
	}
	res := f[x][1]
	for x &= x - 1; x > 0; x &= x - 1 {
		if f[x][0] < ts {
			f[x][0] = ts
			f[x][1] = 0
		}
		res += f[x][1]
	}
	return res
}

func (f fenwick) add(x, n int) {
	for x < len(f) {
		if f[x][0] < ts {
			f[x][0] = ts
			f[x][1] = 0
		}
		f[x][1] += n
		x += x & (-x)
	}
}

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

	n := readInt()
	ops := make(map[int][][3]int, n)
	var times []int
	nRes := 0
	for i := 0; i < n; i++ {
		a := readInt()
		t := readInt()
		x := readInt()
		val := [3]int{a, t, 0}
		if a == 3 {
			val[2] = nRes
			nRes++
		}
		ops[x] = append(ops[x], val)
		times = append(times, t)
	}

	sort.Ints(times)
	tm := make(map[int]int)
	for i, t := range times {
		tm[t] = i+1
	}

	res := make([]int, nRes)
	fen := make(fenwick, n+3)
	for _, ops := range ops {
		for i := range ops {
			t := tm[ops[i][1]]
			switch ops[i][0] {
			case 1:
				fen.add(t, 1)
			case 2:
				fen.add(t, -1)
			case 3:
				res[ops[i][2]] = fen.get(t)
			default:
				panic("switch")
			}
		}
		ts++
	}
	for i := range res {
		fmt.Fprintln(out, res[i])
	}
}


Slavic has an array of length $$$n$$$ consisting only of zeroes and ones. In one operation, he removes either the first or the last element of the array. What is the minimum number of operations Slavic has to perform such that the total sum of the array is equal to $$$s$$$ after performing all the operations? In case the sum $$$s$$$ can't be obtained after any amount of operations, you should output -1.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \leq n, s \leq 2 \cdot 10^5$$$) — the length of the array and the needed sum of elements. The second line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$0 \leq a_i \leq 1$$$) — the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.

For each test case, output a single integer — the minimum amount of operations required to have the total sum of the array equal to $$$s$$$, or -1 if obtaining an array with sum $$$s$$$ isn't possible.

NoteIn the first test case, the sum of the whole array is $$$1$$$ from the beginning, so we don't have to make any operations.In the second test case, the sum of the array is $$$2$$$ and we want it to be equal to $$$1$$$, so we should remove the first element. The array turns into $$$[1, 0]$$$, which has a sum equal to $$$1$$$.In the third test case, the sum of the array is $$$5$$$ and we need it to be $$$3$$$. We can obtain such a sum by removing the first two elements and the last element, doing a total of three operations. The array turns into $$$[0, 1, 1, 1, 0, 0]$$$, which has a sum equal to $$$3$$$.

package main

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

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

	var testCases int
	Fscan(in, &testCases)

	for t := 0; t < testCases; t++ {
		var n, s int
		Fscan(in, &n, &s)
		nums := make([]int, n)

		sum := 0
		for i := 0; i < n; i++ {
			Fscan(in, &nums[i])
			sum += nums[i]
		}

		if sum < s {
			Fprintln(out, -1)
			continue
		}

		window := -1
		i := 0
		j := 0
		cur := 0
		for j < n {
			cur += nums[j]
			if cur < s {
				j++
				continue
			}
			if cur == s {
				window = max(window, j-i+1)
				j++
				continue
			}
			if cur > s {
				cur -= nums[j]
				cur -= nums[i]
				i++
			}

		}

		Fprintln(out, n-window)
	}

}

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

package main

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

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

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

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

		a := make([]int, n+1)
		var pos int
		for i := 0; i < n; i++ {
			var v int
			fmt.Fscan(in, &v)

			if v == 1 {
				a[pos] = i + 1
				pos++
			}
		}

		if x > n || a[x-1] == 0 {
			fmt.Fprintln(out, -1)
			continue
		}

		if a[x] == 0 && a[x-1] > 0 {
			fmt.Fprintln(out, 0)
			continue
		}
		ans := math.MaxInt32
		for i := 0; i < n; i++ {
			if a[i] == 0 || i+x+1 > n || a[i+x-1] == 0 {
				break
			}

			var tot int
			if i > 0 {
				tot += a[i-1]
			}

			if a[i+x] > 0 {
				tot += n - a[i+x] + 1
			}
			ans = min(ans, tot)
		}
		fmt.Fprintln(out, ans)
	}
}

package main

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

//goland:noinspection GoUnusedFunction
func min(a int, nums ...int) int {
	min := a

	for _, num := range nums {
		if num < min {
			min = num
		}
	}
	return min
}

//goland:noinspection GoUnusedFunction
func max(a int, nums ...int) int {
	max := a

	for _, num := range nums {
		if num > max {
			max = num
		}
	}
	return max
}

//goland:noinspection GoUnusedFunction
func maxFloat(a float64, nums ...float64) float64 {
	max := a

	for _, num := range nums {
		if num > max {
			max = num
		}
	}
	return max
}

//goland:noinspection GoUnusedFunction
func fprintlnArr(w io.Writer, arr []int) {
	for _, val := range arr {
		fmt.Fprint(w, val, " ")
	}
	fmt.Fprintln(w)
}

func query(l int, r int, p []int) int {
	ans := p[r]
	if l > 0 {
		ans -= p[l-1]
	}
	return ans
}

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

	var t int
	for fmt.Fscan(in, &t); t > 0; t-- {
		var n, s int
		fmt.Fscan(in, &n, &s)

		arr := make([]int, n)
		sum := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			sum[i] = arr[i]
			if i > 0 {
				sum[i] += sum[i-1]
			}
		}

		ans := math.MaxInt32
		ansFound := false
		for i := 0; i < n; i++ {
			l := i
			r := n - 1
			pos := -1
			for l <= r {
				mid := l + (r-l)/2

				if query(i, mid, sum) <= s {
					pos = mid
					l = mid + 1
				} else {
					r = mid - 1
				}
			}
			if pos == -1 || query(i, pos, sum) != s {
				continue
			}
			ansFound = true
			ans = min(ans, n-(pos-i+1))
		}
		if ansFound {
			fmt.Fprintln(out, ans)
		} else {
			fmt.Fprintln(out, -1)
		}
	}
}

package main

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

//goland:noinspection GoUnusedFunction
func min(a int, nums ...int) int {
	min := a

	for _, num := range nums {
		if num < min {
			min = num
		}
	}
	return min
}

//goland:noinspection GoUnusedFunction
func max(a int, nums ...int) int {
	max := a

	for _, num := range nums {
		if num > max {
			max = num
		}
	}
	return max
}

//goland:noinspection GoUnusedFunction
func maxFloat(a float64, nums ...float64) float64 {
	max := a

	for _, num := range nums {
		if num > max {
			max = num
		}
	}
	return max
}

//goland:noinspection GoUnusedFunction
func fprintlnArr(w io.Writer, arr []int) {
	for _, val := range arr {
		fmt.Fprint(w, val, " ")
	}
	fmt.Fprintln(w)
}

func query(l int, r int, p []int) int {
	ans := p[r]
	if l > 0 {
		ans -= p[l-1]
	}
	return ans
}

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

	var t int
	for fmt.Fscan(in, &t); t > 0; t-- {
		var n, s int
		fmt.Fscan(in, &n, &s)

		arr := make([]int, n)
		sum := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			sum[i] = arr[i]
			if i > 0 {
				sum[i] += sum[i-1]
			}
		}

		ans := math.MaxInt32
		for i := 0; i < n; i++ {
			l := i
			r := n - 1
			pos := -1
			for l <= r {
				mid := l + (r-l)/2

				if query(i, mid, sum) <= s {
					pos = mid
					l = mid + 1
				} else {
					r = mid - 1
				}
			}
			if pos == -1 || query(i, pos, sum) != s {
				continue
			}
			ans = min(ans, n-(pos-i+1))
		}
		if ans == math.MaxInt32 {
			ans = -1
		}
		fmt.Fprintln(out, ans)
	}
}

package main

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

/* begin of MyIO */
const eof = 0

type any = interface{}

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

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

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

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

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

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

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

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

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

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

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

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

var myio *MyIO

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

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

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

/* End of MyIO */

func sol() {
	n, s := ri(), ri()
	a := ria(n)
	d := make([]int, 0)
	d = append(d, -1)
	for i := 0; i < n; i++ {
		if a[i] == 1 {
			d = append(d, i)
		}
	}
	d = append(d, n)

	// myio.Println(d, len(d), s)

	if len(d)-2 < s {
		myio.Println(-1)
		return
	}
	l := 0
	for i := 0; i+s+1 < len(d); i++ {
		l1 := d[i+s+1] - d[i] - 1
		// myio.Println(d[i]+1, d[i+s+1]-1)
		if l1 > l {
			l = l1
		}
	}
	myio.Println(n - l)
}

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

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

package main

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

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

func main() {
	defer writer.Flush()

	var Q int
	fmt.Fscan(reader, &Q)

	const INF int = 1000000000

	for ; Q > 0; Q-- {
		var n, s int
		fmt.Fscan(reader, &n, &s)

		a := make([]int, n+1)
		m := 0

		for i := 1; i <= n; i++ {
			fmt.Fscan(reader, &a[i])
			m += a[i]
			a[i] += a[i-1]
		}

		last := make([]int, m+1)
		for i := 1; i <= m; i++ {
			last[i] = -1
		}

		ans := INF
		for r := 1; r <= n; r++ {
			if a[r]-s >= 0 && last[a[r]-s] != -1 {
				if ans > last[a[r]-s]+n-r {
					ans = last[a[r]-s] + n - r
				}
			}
			if last[a[r]] == -1 {
				last[a[r]] = r
			}
		}

		if ans == INF {
			fmt.Fprintf(writer, "%d\n", -1)
		} else {
			fmt.Fprintf(writer, "%d\n", ans)
		}
	}
}

package main

import (
    "fmt"
    _"math"
)

func UNUSED(x ...interface{}) {}

func main() {

	var a [200007]int
	var t, n, s int
	fmt.Scan(&t)
	for(t > 0) {
		t --
		var sum, ind, res int
		res = 1000007

		fmt.Scan(&n, &s)
		for i := 0; i < n; i ++ {
			fmt.Scan(&a[i])
			sum += a[i]
		}
		if sum < s {
			fmt.Println(-1)
			continue
		}
		var b []int = a[:n]
		var fromtop [200007]int

		fromtop[ind] = 0
		ind ++

		for i, x := range b {
			if x == 1 {
				fromtop[ind] = i + 1
				ind ++
			}
		}
		fromtop[ind] = len(b) + 1
		ind ++

		sum -= s

		for i := 0; i <= sum; i ++ {
			res = min(res, fromtop[i] + len(b) - fromtop[ind - sum - 1 + i] + 1)
		}

		fmt.Println(res)
	}
}

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

package main

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

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

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

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

func do(a []int, n, s int) int {
	j := 0
	t := 0
	ans := -1
	// log.Print(a)
	for i := 0; i < n; i++ {
		t += a[i]
		// log.Print(i, t, s, j, ans)
		if t > s {
			for a[j] != 1 {
				j++
			}
			j++
			t--
		}
		if t == s {
			if ans == -1 {
				ans = n
			}
			if ans > j+n-i-1 {
				ans = j + n - i - 1
			}
		}
	}
	return ans
}

package main

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

/*
2
11 4
1 0 1 0 1 0 0 1 1 0 1
11 3
1 0 1 0 1 0 0 1 1 0 1
---
2
4

10101001101
11223334556

========
1
5 1
0 0 1 1 0
*/

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

	a := make([]int, n)
	psa := make([]int, n)
	sumA := 0

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

		psa[i] = a[i]
		if i != 0 {
			psa[i] += psa[i-1]
		}

		sumA += a[i]
	}
	scanf("\n")

	if s > sumA {
		fmt.Fprintln(writer, -1)
		return
	} else if s == sumA {
		fmt.Fprintln(writer, 0)
		return
	}

	removeCnt := sumA - s

	i := 0
	for ; i < n; i++ {
		if psa[i] == removeCnt {
			break
		}
	}

	bestOpCnt := i + 1

	k := n - 1
	for j := i - 1; j >= -1; j-- {
		leftSum := 0
		if j >= 0 {
			leftSum = psa[j]
		}

		for k >= 0 && leftSum+(psa[n-1]-psa[k]+a[k]) != removeCnt {
			k--
		}

		opCnt := (j + 1) + n - k
		bestOpCnt = min(bestOpCnt, opCnt)
	}

	fmt.Fprintln(writer, bestOpCnt)
}

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

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

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

const maxUint = ^uint(0)

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

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

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

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

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

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

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

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

	return minNumIdx
}

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

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

	return maxNumIdx
}

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

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

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

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

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

	return true
}

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

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

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

	num = abs(num)

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

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

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

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

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

	return factors
}

package main

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

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

func slove() {
	var n, s int
	fmt.Fscan(r, &n, &s)
	nums := make([]int, n)
	for i:=0;i<n;i++ {
		fmt.Fscan(r, &nums[i])
	}
	l, r := 0, -1
	cnt := 0
	ans := n
	for r < len(nums) - 1 {
		r++
		if nums[r] == 1 {
			cnt++
		}
		for cnt > s {
			if nums[l] == 1 {
				cnt--
			}
			l++
		}
		if cnt == s {
			ans = min(ans, n - (r-l+1))
		}
	}
	if ans == n {
			fmt.Fprintln(w, -1)
	} else {
		fmt.Fprintln(w, ans)
	}
}

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

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

package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"os"
	"strings"
)

// https://space.bilibili.com/206214/dynamic
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	solve := func(curCase int) {
		var n , tar int
		Fscan(in, &n,&tar)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}

		l := 0
		s := 0
		ans := -1
		for r, v := range a {
			s+=v
			for l<n && s > tar {
				s-=a[l]
				l++
			}
			if s == tar {
				ans = max(ans, r-l+1)
			}
		}
		if ans == -1 {
			Fprintln(out, -1)
		} else {
			Fprintln(out, n-ans)
		}
	}

	cases := 1
	Fscan(in, &cases)
	for curCase := 1; curCase <= cases; curCase++ {
		solve(curCase)
	}

	leftData, _ := ioutil.ReadAll(in)
	if s := strings.TrimSpace(string(leftData)); s != "" {
		panic("有未读入的数据：\n" + s)
	}

}

func main() { run(os.Stdin, os.Stdout) }
func max(a, b int) int {
	if b > a {
		return b
	}
	return a
}

package main

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

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

	var testCases int
	Fscan(in, &testCases)

	for t := 0; t < testCases; t++ {
		var n, s int
		Fscan(in, &n, &s)
		nums := make([]int, n)
		distanceOfOneFromFront := make([]int, n)
		distanceOfOneFromBack := make([]int, n)

		cnt := 5 * n
		sum := 0
		for i := 0; i < n; i++ {
			Fscan(in, &nums[i])
			sum += nums[i]
			if nums[i] == 1 {
				cnt = 0
			} else {
				cnt++
			}
			distanceOfOneFromBack[i] = cnt
		}

		if sum < s {
			Fprintln(out, -1)
			continue
		}

		cnt = 5 * n
		for i := n - 1; i >= 0; i-- {
			if nums[i] == 1 {
				cnt = 0
			} else {
				cnt++
			}
			distanceOfOneFromFront[i] = cnt
		}

		l := 0
		r := n - 1
		cnt = 0
		for sum > s {
			if distanceOfOneFromFront[l] > distanceOfOneFromBack[r] {
				sum -= nums[r]
				r--
			} else {
				sum -= nums[l]
				l++
			}
			cnt++
		}

		Fprintln(out, cnt)
	}

}

package main

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

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

	var testCases int
	Fscan(in, &testCases)

	for t := 0; t < testCases; t++ {
		var n, s int
		Fscan(in, &n, &s)
		nums := make([]int, n)
		distanceOfOneFromFront := make([]int, n)
		distanceOfOneFromBack := make([]int, n)

		cnt := 5 * n
		sum := 0
		for i := 0; i < n; i++ {
			Fscan(in, &nums[i])
			sum += nums[i]
			if nums[i] == 1 {
				cnt = 0
			} else {
				cnt++
			}
			distanceOfOneFromBack[i] = cnt
		}

		if sum < s {
			Fprintln(out, -1)
			continue
		}

		cnt = 5 * n
		for i := n - 1; i >= 0; i-- {
			if nums[i] == 1 {
				cnt = 0
			} else {
				cnt++
			}
			distanceOfOneFromFront[i] = cnt
		}

		fmt.Println(distanceOfOneFromFront)
		fmt.Println(distanceOfOneFromBack)
		l := 0
		r := n - 1
		cnt = 0
		for sum > s {
			if distanceOfOneFromFront[l] > distanceOfOneFromBack[r] {
				sum -= nums[r]
				r--
			} else {
				sum -= nums[l]
				l++
			}
			cnt++
		}

		Fprintln(out, cnt)
	}

}

package main

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

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

	var testCases int
	Fscan(in, &testCases)

	for t := 0; t < testCases; t++ {
		var n, s int
		Fscan(in, &n, &s)
		nums := make([]int, n)
		distanceOfOneFromFront := make([]int, n)
		distanceOfOneFromBack := make([]int, n)

		cnt := n + 5
		sum := 0
		for i := 0; i < n; i++ {
			Fscan(in, &nums[i])
			sum += nums[i]
			if nums[i] == 1 {
				cnt = 0
			} else {
				cnt++
			}
			distanceOfOneFromBack[i] = cnt
		}

		if sum < s {
			Fprintln(out, -1)
			continue
		}

		cnt = n + 5
		for i := n - 1; i >= 0; i-- {
			if nums[i] == 1 {
				cnt = 0
			} else {
				cnt++
			}
			distanceOfOneFromFront[i] = cnt
		}

		l := 0
		r := n - 1
		cnt = 0
		for sum > s {
			if distanceOfOneFromFront[l] > distanceOfOneFromBack[r] {
				sum -= nums[r]
				r--
			} else {
				sum -= nums[l]
				l++
			}
			cnt++
		}

		Fprintln(out, cnt)
	}

}

package main

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

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

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

		a := make([]int, n)
		var c int
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			if a[i] == 1 {
				c++
			}
		}

		if x > c {
			fmt.Fprintln(out, -1)
			continue
		}
		var tot int
		l, r := 0, n-1
		for c > x {
			l2 := l
			for a[l2] == 0 {
				l2++
			}
			r2 := r
			for a[r2] == 0 {
				r2--
			}

			if l2-l <= r-r2 {
				tot += l2 - l + 1
				l = l2 + 1
				// fmt.Println("Selecting left: ", tot, l2-l+1)
			} else {
				tot += r - r2 + 1
				r = r2 - 1
				// fmt.Println("Selecting right: ", tot, r-r2+1)
			}
			c--
		}
		fmt.Fprintln(out, tot)
	}
}

package main

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

/* begin of MyIO */
const eof = 0

type any = interface{}

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

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

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

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

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

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

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

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

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

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

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

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

var myio *MyIO

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

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

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

/* End of MyIO */

func sol() {
	n, s := ri(), ri()
	a := ria(n)
	d := make([]int, 0)
	for i := 0; i < n; i++ {
		if a[i] == 1 {
			d = append(d, i)
		}
	}
	if len(d) < s {
		fmt.Println(-1)
		return
	}
	if len(d) == s {
		fmt.Println(0)
		return
	}
	if len(d) == s+1 {
		a := d[0] + 1
		b := n - d[s]
		if a < b {
			fmt.Println(a)
		} else {
			fmt.Println(b)
		}
		return
	}
	l := d[s] - 1
	// fmt.Println("l=", l)
	for i := 0; i+s+1 < len(d); i++ {
		l1 := d[i+s+1] - d[i] - 1
		// myio.Println(d[i]+1, d[i+s+1]-1)
		if l1 > l {
			l = l1
		}
	}
	fmt.Println(n - l)
}

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

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


There are $$$n$$$ monsters standing in a row numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th monster has $$$h_i$$$ health points (hp). You have your attack power equal to $$$a$$$ hp and your opponent has his attack power equal to $$$b$$$ hp.You and your opponent are fighting these monsters. Firstly, you and your opponent go to the first monster and fight it till his death, then you and your opponent go the second monster and fight it till his death, and so on. A monster is considered dead if its hp is less than or equal to $$$0$$$.The fight with a monster happens in turns.   You hit the monster by $$$a$$$ hp. If it is dead after your hit, you gain one point and you both proceed to the next monster.  Your opponent hits the monster by $$$b$$$ hp. If it is dead after his hit, nobody gains a point and you both proceed to the next monster. You have some secret technique to force your opponent to skip his turn. You can use this technique at most $$$k$$$ times in total (for example, if there are two monsters and $$$k=4$$$, then you can use the technique $$$2$$$ times on the first monster and $$$1$$$ time on the second monster, but not $$$2$$$ times on the first monster and $$$3$$$ times on the second monster).Your task is to determine the maximum number of points you can gain if you use the secret technique optimally.

The first line of the input contains four integers $$$n, a, b$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5, 1 \le a, b, k \le 10^9$$$) — the number of monsters, your attack power, the opponent's attack power and the number of times you can use the secret technique. The second line of the input contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 10^9$$$), where $$$h_i$$$ is the health points of the $$$i$$$-th monster.

Print one integer — the maximum number of points you can gain if you use the secret technique optimally.

package main

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

var sc, wr = bufio.NewScanner(os.Stdin), bufio.NewWriter(os.Stdout)

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

func scanInt64() int64 {
	sc.Scan(); a,_ := strconv.ParseInt(sc.Text(),10,64); return a
}

func scanFloat64() float64 {
	sc.Scan(); a,_ := strconv.ParseFloat(sc.Text(),64); return a
}

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

func scanString() string {
	sc.Scan(); return sc.Text()
}

func printInts(a ...int) {
	for i, e := range a {
		fmt.Fprint(wr, e); if i != len(a)-1 { fmt.Fprint(wr, " ") }
	}
	fmt.Fprintln(wr); wr.Flush()
}

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

//•*¨*•.¸¸♪Main•*¨*•.¸¸♪(　-ω-)ノ　(　・ω・)

func main() {
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 10000), 1000000)

	n := scanInt()
	a := scanInt()
	b := scanInt()
	k := scanInt()
	hs := scanInts(n)

	for i := 0; i < n; i++ {
		hs[i] %= a+b
		if hs[i] == 0 {
			hs[i] = a+b
		}
		hs[i] = (hs[i]+a-1)/a - 1
	}

	sort.Ints(hs)

	ans := 0

	for i := 0; i < n; i++ {
		if hs[i] <=k {
			k -= hs[i]
			ans++
		}
	}

	fmt.Println(ans)
}

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    capacity := 5000 * 1024
    scanner.Buffer(make([]byte, capacity), capacity)
    scanner.Scan()
    line := scanner.Text()
    parts := strings.Split(line, " ")
    n, _ := strconv.Atoi(parts[0])
    a, _ := strconv.Atoi(parts[1])
    b, _ := strconv.Atoi(parts[2])
    k, _ := strconv.Atoi(parts[3])

    scanner.Scan()
    line = scanner.Text()
    parts = strings.Split(line, " ")
    h := make([]int, n)
    for i := 0; i < n; i++ {
        h[i], _ = strconv.Atoi(parts[i])
    }
    //~ fmt.Println(n, a, b, k, h)

    skip_counts := make([]int, n)
    for i := 0; i < n; i++ {
        skip_counts[i] = skips_needed(a, b, h[i])
    }

    sort.Ints(skip_counts)
    //~ fmt.Println(skip_counts)

    wins := 0
    sum_skips := 0
    for i := 0; i < n; i++ {
        if sum_skips + skip_counts[i] <= k {
            wins++
            sum_skips += skip_counts[i]
        } else {
            break
        }
    }

    fmt.Println(wins)
}

func skips_needed(a, b, h int) int {
    m := h % (a + b)
    if m == 0 {
        m = a + b
    }
    skips := m / a
    if m % a == 0 {
        skips--
    }
    return skips
}

package main

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

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

	var n, a, b, k int
	fmt.Fscanf(reader, "%d %d %d %d\n", &n, &a, &b, &k)
	hs := make([]int, n)
	for j := 0; j < n; j++ {
		fmt.Fscanf(reader, "%d", &hs[j])
	}
	fmt.Fscanf(reader, "\n")
	fmt.Fprintf(writer, "%d\n", findMaxToBeat(hs, a, b, k))
}

func findMaxToBeat(hs []int, a, b, k int) int {
	n := len(hs)

	costs := make([]int, 0, n)
	for _, h := range hs {
		x := h / (a + b)
		if x*(a+b) == h {
			x--
		}
		y := (h - x*(a+b) + a - 1) / a

		costs = append(costs, y-1)
	}

	sort.Slice(costs, func(e1, e2 int) bool {
		return costs[e1] < costs[e2]
	})

	ans := 0
	for _, c := range costs {
		if k >= c {
			ans++
			k -= c
		}
	}

	return ans
}

/*
https://codeforces.com/contest/1296/problem/D
*/

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	line1, _ := reader.ReadString('\n')
	line1_arr := strings.Fields(line1)
	//fmt.Println(line1_arr)
	line2, _ := reader.ReadString('\n')
	line2_arr := strings.Fields(line2)
	//fmt.Println(line2_arr)

	N, _ := strconv.Atoi(line1_arr[0])
	//fmt.Println("N: ", N)
	a, _ := strconv.Atoi(line1_arr[1])
	//fmt.Println("a: ", a)
	b, _ := strconv.Atoi(line1_arr[2])
	//fmt.Println("b: ", b)
	k, _ := strconv.Atoi(line1_arr[3])
	//fmt.Println("k: ", k)

	arr := make([]int, N)

	for i := 0; i < N; i++ {
		h, _ := strconv.Atoi(line2_arr[i])
		//fmt.Println("h: ", h)
		d := a + b
		rem := h % d
		if rem == 0 {
			//fmt.Println("rem: ", rem)
			rem = d
		}
		num_skip := rem/a - 1
		if rem%a != 0 {
			num_skip++
		}

		arr[i] = num_skip
	}

	//fmt.Println("before sorting arr: ", arr)
	sort.Ints(arr)
	//fmt.Println("arr: ", arr)
	ret := 0
	for i := 0; i < N; i++ {
		if k >= arr[i] {
			ret++
			k -= arr[i]
		}
	}
	fmt.Println(ret)
}

package main

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

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

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

func main() {
	var n, a, b, k int
	scanf("%d %d %d %d\n", &n, &a, &b, &k)
	h := make([]int, n)
	for i := range h {
		scanf("%d", &h[i])
	}
	move := make([]int, n)
	for i, v := range h {
		v = v % (a + b)
		if v == 0 {
			v = a + b
		}
		num := v / a
		if v % a > 0 {
			num++
		}
		move[i] = num - 1
	}
	cnt := 0
	ans := 0
	sort.Ints(move)
	for _, v := range move {
		// printf("%d ", v)
		cnt += v
		if cnt > k {
			break
		}
		ans++
	}
	printf("%d\n", ans)
	writer.Flush()
}

package main

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

func main () {
	in := bufio.NewReader(os.Stdin)
	var n, a, b, k int;
	fmt.Fscan(in, &n, &a, &b, &k);
	h := make([]int, n);
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &h[i]);
		h[i] = h[i] % (a + b);
		if (h[i] == 0) {
			h[i] = a + b;
		}
	}

	sort.Ints(h);

	ans := 0;
	for _, x := range h {
		if (x <= a) {
			ans++;
			continue;
		}
		k -= (x + a - 1) / a - 1;
		if (k < 0) {
			break;
		}
		ans++;
	}
	fmt.Println(ans);
}

package main

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

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

	var n, a, b, k int
	fmt.Fscanf(reader, "%d %d %d %d\n", &n, &a, &b, &k)
	hs := make([]int, n)
	for j := 0; j < n; j++ {
		fmt.Fscanf(reader, "%d", &hs[j])
	}
	fmt.Fscanf(reader, "\n")
	fmt.Fprintf(writer, "%d\n", findMaxToBeat(hs, a, b, k))
}

func findMaxToBeat(hs []int, a, b, k int) int {
	n := len(hs)

	costs := make([]int, 0, n)
	for _, h := range hs {
		x := h / (a + b)
		if x*(a+b) == h {
			x--
		}
		y := (h - x*(a+b) + a - 1) / a

		costs = append(costs, y-1)
	}

	sort.Slice(costs, func(e1, e2 int) bool {
		return e1 < e2
	})

	ans := 0
	for _, c := range costs {
		if k >= c {
			ans++
			k -= c
		}
	}

	return ans
}

package main

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

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

	var n, a, b, k int64
	fmt.Fscanf(reader, "%d %d %d %d\n", &n, &a, &b, &k)
	hs := make([]int64, n)
	for j := 0; j < int(n); j++ {
		fmt.Fscanf(reader, "%d", &hs[j])
	}
	fmt.Fscanf(reader, "\n")
	fmt.Fprintf(writer, "%d\n", findMaxToBeat(hs, a, b, k))
}

func findMaxToBeat(hs []int64, a, b, k int64) int {
	n := len(hs)

	costs := make([]int64, 0, n)
	for _, h := range hs {
		x := h / (a + b)
		if x*(a+b) == h {
			x--
		}
		y := (h - x*(a+b) + a - 1) / a

		costs = append(costs, y-1)
	}

	sort.Slice(costs, func(e1, e2 int) bool {
		return e1 < e2
	})

	ans := 0
	for _, c := range costs {
		if k >= c {
			ans++
			k -= c
		}
	}

	return ans
}


Recently, Petya learned about a new game "Slay the Dragon". As the name suggests, the player will have to fight with dragons. To defeat a dragon, you have to kill it and defend your castle. To do this, the player has a squad of $$$n$$$ heroes, the strength of the $$$i$$$-th hero is equal to $$$a_i$$$.According to the rules of the game, exactly one hero should go kill the dragon, all the others will defend the castle. If the dragon's defense is equal to $$$x$$$, then you have to send a hero with a strength of at least $$$x$$$ to kill it. If the dragon's attack power is $$$y$$$, then the total strength of the heroes defending the castle should be at least $$$y$$$.The player can increase the strength of any hero by $$$1$$$ for one gold coin. This operation can be done any number of times.There are $$$m$$$ dragons in the game, the $$$i$$$-th of them has defense equal to $$$x_i$$$ and attack power equal to $$$y_i$$$. Petya was wondering what is the minimum number of coins he needs to spend to defeat the $$$i$$$-th dragon.Note that the task is solved independently for each dragon (improvements are not saved).

The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — number of heroes. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^{12}$$$), where $$$a_i$$$ is the strength of the $$$i$$$-th hero. The third line contains a single integer $$$m$$$ ($$$1 \le m \le 2 \cdot 10^5$$$) — the number of dragons. The next $$$m$$$ lines contain two integers each, $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i \le 10^{12}; 1 \le y_i \le 10^{18}$$$) — defense and attack power of the $$$i$$$-th dragon.

Print $$$m$$$ lines, $$$i$$$-th of which contains a single integer — the minimum number of coins that should be spent to defeat the $$$i$$$-th dragon.

NoteTo defeat the first dragon, you can increase the strength of the third hero by $$$1$$$, then the strength of the heroes will be equal to $$$[3, 6, 3, 3]$$$. To kill the dragon, you can choose the first hero.To defeat the second dragon, you can increase the forces of the second and third heroes by $$$1$$$, then the strength of the heroes will be equal to $$$[3, 7, 3, 3]$$$. To kill the dragon, you can choose a second hero.To defeat the third dragon, you can increase the strength of all the heroes by $$$1$$$, then the strength of the heroes will be equal to $$$[4, 7, 3, 4]$$$. To kill the dragon, you can choose a fourth hero.To defeat the fourth dragon, you don't need to improve the heroes and choose a third hero to kill the dragon.To defeat the fifth dragon, you can increase the strength of the second hero by $$$2$$$, then the strength of the heroes will be equal to $$$[3, 8, 2, 3]$$$. To kill the dragon, you can choose a second hero.

package main

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

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
}

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

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

	var n int
	fmt.Fscan(in, &n)
	h := make([]int64, n)
	var z int64
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &h[i])
		z += h[i]
	}

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

	var m int
	fmt.Fscan(in, &m)
	for i := 0; i < m; i++ {
		var d, a int64
		fmt.Fscan(in, &d, &a)

		idx := sort.Search(n, func(i int) bool { return h[i] >= d })
		if idx >= n {
			idx--
		}

		c := max(0, d-h[idx]) + max(0, a-(z-h[idx]))
		if idx > 0 {
			c = min(c, max(0, d-h[idx-1]+max(0, a-(z-h[idx-1]))))
		}

		// if idx < n-1 {
		// 	c = min(c, max(0, d-h[idx+1]+max(0, a-(z-h[idx+1]))))
		// }

		fmt.Fprintln(out, c)
	}
}

package main

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

var a []int64
var b []*drag
var n,m int

type LL []int64

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

type drag struct {
	x, y int64
}

func main() {

	r := bufio.NewReader(os.Stdin)

	fmt.Fscan(r, &n)

	a = make([]int64, n)

	for j := 0; j < n; j++ {
		fmt.Fscan(r, &a[j])
	}

	fmt.Fscan(r, &m)

	b = make([]*drag, m)

	for j := 0; j < m; j++ {
		var x, y int64
		fmt.Fscan(r, &x, &y)

		d := &drag{x, y}
		b[j] = d
	}

	solve()

}

func solve() {
	sort.Sort(LL(a))

	var sum int64

	for i := 0; i < n; i++ {
		sum += a[i]
	}

	for i := 0; i < m; i++ {
		dr := b[i]

		attack := dr.y
		def := dr.x

		idx := sort.Search(n, func(i int) bool {
			return a[i] >= def
		})

		var cost int64

		if idx == n {
			largest := a[n - 1]

			cost += def - largest
			s := sum - largest

			if attack > s {
				cost += attack - s
			}

			//15 20   30 10
			//15 10
			//80 85

		} else {

			guy := a[idx]

			s := sum - guy

			if attack > s {
				cost += attack - s
			}

			if idx > 0 {
				prev := a[idx - 1]

				var a int64

				if attack > (sum - prev) {
					a = attack - (sum - prev)
				}

				temp := def - prev + a

				cost = min(cost, temp)
			}

		}
		// ..... 93 94  100   95 200

		fmt.Println(cost)
	}
}

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

package main

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

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

	var n, m int
	var tot, def, atk int64
	Fscan(in, &n)
	a := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
		tot += a[i]
	}
	sort.Slice(a, func(i, j int) bool { return a[i] < a[j] })
	for Fscan(in, &m); m > 0; m-- {
		Fscan(in, &def, &atk)
		i := sort.Search(n, func(i int) bool { return a[i] >= def })
		res := int64(2e18)
		if i < n {
			res = atk - (tot - a[i])
		}
		if i > 0 {
			if cost := def - a[i-1] + max(atk-(tot-a[i-1]), 0); cost < res {
				res = cost
			}
		}
		Fprintln(out, max(res, 0))
	}
}

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

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

package main

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

func val(x int64) int64 {
	if x < 0 {
		return -x
	}
	return 0
}

func main() {
	r := bufio.NewReaderSize(os.Stdin, 1024*1024)

	//var T int
	//fmt.Fscanf(r, "%d\n", &T)

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

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

	var d int
	fmt.Fscanf(r, "%d\n", &d)

	for i := 0; i < d; i++ {
		var x, y int64
		fmt.Fscanf(r, "%d %d\n", &x, &y)

		j := sort.Search(n, func(j int) bool {
			return arr[j] >= x
		})

		res := int64(-1)
		for k := j - 1; k <= j+1 && k < n; k++ {
			if k >= 0 {
				z := val(arr[k]-x) + val(sum-y-arr[k])

				if res == -1 || res > z {
					res = z
				}
			}
		}

		fmt.Println(res)
	}

	//}
}

package main

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

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
}

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

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

	var n int
	fmt.Fscan(in, &n)
	h := make([]int64, n)
	var z int64
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &h[i])
		z += h[i]
	}

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

	var m int
	fmt.Fscan(in, &m)
	for i := 0; i < m; i++ {
		var d, a int64
		fmt.Fscan(in, &d, &a)

		idx := sort.Search(n, func(i int) bool { return h[i] >= d })
		if idx >= n {
			idx--
		}

		c := max(0, d-h[idx]) + max(0, a-(z-h[idx]))
		if idx > 0 {
			c = min(c, max(0, d-h[idx-1]+max(0, a-(z-h[idx-1]))))
		}

		if idx < n-1 {
			c = min(c, max(0, d-h[idx+1]+max(9, a-(z-h[idx+1]))))
		}

		fmt.Fprintln(out, c)
	}
}

package main

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

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

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

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

	var n int
	fmt.Fscan(in, &n)
	h := make([]int64, n)
	var z int64
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &h[i])
		z += h[i]
	}

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

	var m int
	fmt.Fscan(in, &m)
	for i := 0; i < m; i++ {
		var d, a int64
		fmt.Fscan(in, &d, &a)

		var c int64
		idx := sort.Search(n, func(i int) bool { return h[i] >= d })
		if idx >= n {
			idx--
		}

		if idx > 0 && abs(h[idx-1]-d) < abs(h[idx]-d) {
			idx--
		}

		x := h[idx]
		c += max(0, d-x)
		c += max(0, a-(z-x))
		fmt.Fprintln(out, c)
	}
}

package main

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

var a []int64
var b []*drag
var n,m int

type LL []int64

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

type drag struct {
	x, y int64
}

func main() {

	r := bufio.NewReader(os.Stdin)

	fmt.Fscan(r, &n)

	a = make([]int64, n)

	for j := 0; j < n; j++ {
		fmt.Fscan(r, &a[j])
	}

	fmt.Fscan(r, &m)

	b = make([]*drag, m)

	for j := 0; j < m; j++ {
		var x, y int64
		fmt.Fscan(r, &x, &y)

		d := &drag{x, y}
		b[j] = d
	}

	solve()

}

func solve() {
	sort.Sort(LL(a))

	var sum int64

	for i := 0; i < n; i++ {
		sum += a[i]
	}

	for i := 0; i < m; i++ {
		dr := b[i]

		attack := dr.y
		def := dr.x

		idx := sort.Search(n, func(i int) bool {
			return a[i] >= def
		})

		var cost int64

		if idx == n {
			largest := a[n - 1]

			cost += def - largest
			s := sum - largest

			if attack > s {
				cost += attack - s
			}

		} else {

			guy := a[idx]

			s := sum - guy

			if attack > s {
				cost += attack - s
			}

			if idx > 0 {
				prev := a[idx - 1]

				temp := def - prev + attack - (sum - prev)

				cost = min(cost, temp)
			}

		}

		fmt.Println(cost)
	}
}

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