Demonstrative competitions will be held in the run-up to the $$$20NN$$$ Berlatov Olympic Games. Today is the day for the running competition!Berlatov team consists of $$$2n$$$ runners which are placed on two running tracks; $$$n$$$ runners are placed on each track. The runners are numbered from $$$1$$$ to $$$n$$$ on each track. The runner with number $$$i$$$ runs through the entire track in $$$i$$$ seconds.The competition is held as follows: first runners on both tracks start running at the same time; when the slower of them arrives at the end of the track, second runners on both tracks start running, and everyone waits until the slower of them finishes running, and so on, until all $$$n$$$ pairs run through the track.The organizers want the run to be as long as possible, but if it lasts for more than $$$k$$$ seconds, the crowd will get bored. As the coach of the team, you may choose any order in which the runners are arranged on each track (but you can't change the number of runners on each track or swap runners between different tracks).You have to choose the order of runners on each track so that the duration of the competition is as long as possible, but does not exceed $$$k$$$ seconds.Formally, you want to find two permutations $$$p$$$ and $$$q$$$ (both consisting of $$$n$$$ elements) such that $$$sum = \sum\limits_{i=1}^{n} max(p_i, q_i)$$$ is maximum possible, but does not exceed $$$k$$$. If there is no such pair, report about it.

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10^6, 1 \le k \le n^2$$$) — the number of runners on each track and the maximum possible duration of the competition, respectively.

If it is impossible to reorder the runners so that the duration of the competition does not exceed $$$k$$$ seconds, print $$$-1$$$.  Otherwise, print three lines. The first line should contain one integer $$$sum$$$ — the maximum possible duration of the competition not exceeding $$$k$$$. The second line should contain a permutation of $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ should be pairwise distinct) — the numbers of runners on the first track in the order they participate in the competition. The third line should contain a permutation of $$$n$$$ integers $$$q_1, q_2, \dots, q_n$$$ ($$$1 \le q_i \le n$$$, all $$$q_i$$$ should be pairwise distinct) — the numbers of runners on the second track in the order they participate in the competition. The value of $$$sum = \sum\limits_{i=1}^{n} max(p_i, q_i)$$$ should be maximum possible, but should not exceed $$$k$$$. If there are multiple answers, print any of them.

NoteIn the first example the order of runners on the first track should be $$$[5, 3, 2, 1, 4]$$$, and the order of runners on the second track should be $$$[1, 4, 2, 5, 3]$$$. Then the duration of the competition is $$$max(5, 1) + max(3, 4) + max(2, 2) + max(1, 5) + max(4, 3) = 5 + 4 + 2 + 5 + 4 = 20$$$, so it is equal to the maximum allowed duration.In the first example the order of runners on the first track should be $$$[2, 3, 1]$$$, and the order of runners on the second track should be $$$[2, 1, 3]$$$. Then the duration of the competition is $$$8$$$, and it is the maximum possible duration for $$$n = 3$$$.

package main

import (
	"fmt"
	"math"
	"strconv"
)

func len1N(n int) int {
	if n <= 0 {
		return 0
	}
	l := int(math.Log10(float64(n)))
	nn := int(math.Pow10(l) - 1)
	return len1N(nn) + (n-nn)*(l+2)
}

func main() {
	var n, k int64
	fmt.Scanf("%d %d", &n, &k)
	min := n * (n + 1) / 2
	var max int64
	if n%2 == 0 {
		max = (3*n + 2) * n / 4
	} else {
		max = (3*n - 1) * (n + 1) / 4
	}
	if k < min {
		fmt.Println("-1")
		return
	}
	if k > max {
		k = max
	}
	fmt.Println(k)
	res := make([]byte, 0, len1N(int(n)))
	for i := int64(1); i <= n; i++ {
		res = strconv.AppendInt(res, i, 10)
		res = append(res, ' ')
	}
	fmt.Printf("%s\n", res)
	res = res[:0]
	diff := k - min
	mmax := n
	rem := map[int64]int64{}
	for i := int64(1); i <= n; i++ {
		if i >= mmax || mmax-i > diff {
			if nnn, ok := rem[i]; ok {
				res = strconv.AppendInt(res, nnn, 10)
				res = append(res, ' ')
			} else {
				res = strconv.AppendInt(res, i, 10)
				res = append(res, ' ')
			}
		} else {
			res = strconv.AppendInt(res, mmax, 10)
			res = append(res, ' ')
			diff -= mmax - i
			rem[mmax] = i
			mmax--
		}
	}
	fmt.Printf("%s\n", res)
	if diff != 0 {
		panic("Programming error")
	}
}

package main

import (
	"fmt"
	"strconv"
	"strings"
)

func permutations(a []int, fn func([]int) bool) {
	if len(a) == 0 {
		return
	}
	generate(len(a), a, fn)
}

func permutationsClose(a []int, fn func([]int) bool, done func()) {
	permutations(a, fn)
	done()
}

func generate(k int, a []int, fn func([]int) bool) bool {
	if k == 1 {
		return fn(a)
	}
	done := generate(k-1, a, fn)
	if done {
		return true
	}
	for i := 0; i < k-1; i++ {
		if k%2 == 0 {
			a[i], a[k-1] = a[k-1], a[i]
		} else {
			a[0], a[k-1] = a[k-1], a[0]
		}
		done = generate(k-1, a, fn)
		if done {
			return true
		}
	}
	return false
}

func intToStrArr(inp []int) []string {
	res := make([]string, len(inp))
	for i := 0; i < len(inp); i++ {
		res[i] = strconv.Itoa(inp[i])
	}
	return res
}

func printStrArr(inp []string) {
	fmt.Println(strings.Join(inp, " "))
}

func printIntArr(inp []int) {
	fmt.Println(strings.Join(intToStrArr(inp), " "))
}

func main() {
	var n, k int
	fmt.Scanf("%d %d", &n, &k)
	arr1 := make([]int, n)
	arr2 := make([]int, n)
	for i := 0; i < n; i++ {
		arr1[i] = i + 1
		arr2[i] = i + 1
	}
	min := n * (n + 1) / 2
	var max int
	if n%2 == 0 {
		max = (3*n + 2) * n / 4
	} else {
		max = (3*n - 1) * (n + 1) / 4
	}
	if k < min {
		fmt.Println("-1")
		return
	}
	if k > max {
		k = max
	}
	fmt.Println(k)
	printIntArr(arr1)
	res2 := make([]int, n)
	diff := k - min
	mmax := n
	rem := map[int]int{}
	for i := 1; i <= n; i++ {
		if i >= mmax {
			if nnn, ok := rem[i]; ok {
				res2[i-1] = nnn
			} else {
				res2[i-1] = i
			}
		} else if mmax-i > diff {
			res2[i-1] = i
		} else {
			res2[i-1] = mmax
			diff -= mmax - i
			rem[i] = mmax
			mmax--
		}
	}
	if diff != 0 {
		fmt.Println("Programming error")
	}
	printIntArr(res2)
}

package main

import (
	"fmt"
	"math"
	"strconv"
)

func len1N(n int) int {
	if n <= 0 {
		return 0
	}
	l := int(math.Log10(float64(n)))
	nn := int(math.Pow10(l) - 1)
	return len1N(nn) + (n-nn)*(l+2)
}

func main() {
	var n, k int64
	fmt.Scanf("%d %d", &n, &k)
	min := n * (n + 1) / 2
	var max int64
	if n%2 == 0 {
		max = (3*n + 2) * n / 4
	} else {
		max = (3*n - 1) * (n + 1) / 4
	}
	if k < min {
		fmt.Println("-1")
		return
	}
	if k > max {
		k = max
	}
	fmt.Println(k)
	res := make([]byte, 0, len1N(int(n)))
	for i := int64(1); i <= n; i++ {
		res = strconv.AppendInt(res, i, 10)
		res = append(res, ' ')
	}
	fmt.Printf("%s\n", res)
	res = res[:0]
	diff := k - min
	mmax := n
	rem := map[int64]int64{}
	for i := int64(1); i <= n; i++ {
		if i >= mmax || mmax-i > diff {
			if nnn, ok := rem[i]; ok {
				res = strconv.AppendInt(res, nnn, 10)
				res = append(res, ' ')
			} else {
				res = strconv.AppendInt(res, i, 10)
				res = append(res, ' ')
			}
		} else {
			res = strconv.AppendInt(res, mmax, 10)
			res = append(res, ' ')
			diff -= mmax - i
			rem[mmax] = i
			mmax--
		}
	}
	if diff != 0 {
		panic("Programming error")
	}
}

package main

import (
	"fmt"
	"strconv"
	"strings"
)

func permutations(a []int, fn func([]int) bool) {
	if len(a) == 0 {
		return
	}
	generate(len(a), a, fn)
}

func permutationsClose(a []int, fn func([]int) bool, done func()) {
	permutations(a, fn)
	done()
}

func generate(k int, a []int, fn func([]int) bool) bool {
	if k == 1 {
		return fn(a)
	}
	done := generate(k-1, a, fn)
	if done {
		return true
	}
	for i := 0; i < k-1; i++ {
		if k%2 == 0 {
			a[i], a[k-1] = a[k-1], a[i]
		} else {
			a[0], a[k-1] = a[k-1], a[0]
		}
		done = generate(k-1, a, fn)
		if done {
			return true
		}
	}
	return false
}

func intToStrArr(inp []int) []string {
	res := make([]string, len(inp))
	for i := 0; i < len(inp); i++ {
		res[i] = strconv.Itoa(inp[i])
	}
	return res
}

func printStrArr(inp []string) {
	fmt.Println(strings.Join(inp, " "))
}

func printIntArr(inp []int) {
	fmt.Println(strings.Join(intToStrArr(inp), " "))
}

func main() {
	var n, k int
	fmt.Scanf("%d %d", &n, &k)
	arr1 := make([]int, n)
	arr2 := make([]int, n)
	for i := 0; i < n; i++ {
		arr1[i] = i + 1
		arr2[i] = i + 1
	}
	min := n * (n + 1) / 2
	var max int
	if n%2 == 0 {
		max = (3*n + 2) * n / 4
	} else {
		max = (3*n - 1) * (n + 1) / 4
	}
	if k < min {
		fmt.Println("-1")
		return
	}
	if k > max {
		k = max
	}
	fmt.Println(k)
	printIntArr(arr1)
	res2 := make([]int, n)
	diff := k - min
	mmax := n
	rem := map[int]int{}
	for i := 1; i <= n; i++ {
		if i >= mmax || mmax-i > diff {
			if nnn, ok := rem[i]; ok {
				res2[i-1] = nnn
			} else {
				res2[i-1] = i
			}
		} else {
			res2[i-1] = mmax
			diff -= mmax - i
			rem[mmax] = i
			mmax--
		}
	}
	if diff != 0 {
		panic("Programming error")
	}
	printIntArr(res2)
}


One day Vasya was on a physics practical, performing the task on measuring the capacitance. He followed the teacher's advice and did as much as n measurements, and recorded the results in the notebook. After that he was about to show the results to the teacher, but he remembered that at the last lesson, the teacher had made his friend Petya redo the experiment because the largest and the smallest results differed by more than two times. Vasya is lazy, and he does not want to redo the experiment. He wants to do the task and go home play computer games. So he decided to cheat: before Vasya shows the measurements to the teacher, he will erase some of them, so as to make the largest and the smallest results of the remaining measurements differ in no more than two times. In other words, if the remaining measurements have the smallest result x, and the largest result y, then the inequality y ≤ 2·x must fulfill. Of course, to avoid the teacher's suspicion, Vasya wants to remove as few measurement results as possible from his notes.Help Vasya, find what minimum number of measurement results he will have to erase from his notes so that the largest and the smallest of the remaining results of the measurements differed in no more than two times.

The first line contains integer n (2 ≤ n ≤ 105) — the number of measurements Vasya made. The second line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 5000) — the results of the measurements. The numbers on the second line are separated by single spaces.

Print a single integer — the minimum number of results Vasya will have to remove.

NoteIn the first sample you can remove the fourth and the sixth measurement results (values 8 and 7). Then the maximum of the remaining values will be 5, and the minimum one will be 3. Or else, you can remove the third and fifth results (both equal 3). After that the largest remaining result will be 8, and the smallest one will be 4.

package main

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

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

func main(){
	var n int
	inp, _ := os.Open("input.txt")
	defer inp.Close()
	r := bufio.NewScanner(inp)
	r.Split(bufio.ScanWords)
	out, _ := os.Create("output.txt")
	defer out.Close()
	w := bufio.NewWriter(out)
	r.Scan()
	n, _ = strconv.Atoi(r.Text())
	c := make([]int, n)
	for i:=0; i<n; i++ {
		r.Scan()
		c[i], _ = strconv.Atoi(r.Text())
	}
	sort.Ints(c)
	s, k := 0, 0
	for i:=0; i<n; i++ {
		for s < n && c[s] <= 2*c[i] {
			s += 1
		}
		k = max(k, s-i)
	}
	w.WriteString(strconv.Itoa(n-k)+"\n")
	w.Flush()
}

package main

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

func main() {
    var n int
    var cn [5555]int
    input, _ := os.Open("input.txt")
    defer input.Close()
    scanner := bufio.NewScanner(input)
    scanner.Split(bufio.ScanWords)
    output, _ := os.Create("output.txt")
    defer output.Close()
    writer := bufio.NewWriter(output)
    scanner.Scan()
    aa, _ := strconv.Atoi(scanner.Text())
    n=aa
    for i:=0;i<n;i++ {
    	var x int
    	scanner.Scan()
    	bb, _ := strconv.Atoi(scanner.Text())
    	x=bb
    	cn[x]++
    }
    s:=0
    ans:=1<<30
    for i:=0;i<=2500;i++ {
    	s2:=0
    	for j:=2*i+1;j<=5000;j++ {
    		s2+=cn[j]
    	}
    	if (s+s2)<ans {
    		ans=s+s2
    	}
    	s+=cn[i]
    }
    writer.WriteString(strconv.Itoa(ans))
    writer.WriteString("\n")
    writer.Flush()
}

package main

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

var fi, _ = os.Open("input.txt")
var fo, _ = os.Create("output.txt")

var in = bufio.NewScanner(fi)
var out = bufio.NewWriter(fo)

func main() {

	defer out.Flush()
	in.Split(bufio.ScanWords)

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

	sort.Ints(a)
	var ret int = n
	for i := 0; i < n; i++ {
		var lo, hi int = i, n
		for hi - lo > 1 {
			var m int = lo + (hi - lo) / 2
			if a[m] > 2 * a[i] {
				hi = m
			}else {
				lo = m
			}
		}

		// fmt.Fprintln(out, i, hi)

		var lret int = i + n - hi
		if ret > lret {
			ret = lret
		}
	}
	fmt.Fprintln(out, ret)
}

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

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

func Next() string{
	if !in.Scan() {
		panic("Scan error")
	}
	return in.Text()
}

func NextInt() int{
	ret, _ := strconv.Atoi(Next())
	return ret
}

package main

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

func main() {
    input, _ := os.Open("input.txt")
    defer input.Close()
    output, _ := os.Create("output.txt")
    defer output.Close()

    scanner := bufio.NewScanner(input)
    scanner.Split(bufio.ScanWords)
    writer := bufio.NewWriter(output)

    ScanInts := func(a... *int) {
        for i := 0; i < len(a); i++ {
            scanner.Scan()
            *(a[i]), _ = strconv.Atoi(scanner.Text())
        }
    }

    var n,a int
    var cnt [5001]int
    ScanInts(&n)
    min, max := 5000, 1
    for i := 0; i < n; i++ {
        ScanInts(&a)
        cnt[a]++
        if min > a { min = a }
        if max < a { max = a }
    }
    for i := 2; i <= 5000; i++ {
        cnt[i] += cnt[i-1]
    }
    ans := 1000000000
    for y := min; y <= max; y++ {
        x := (y-1)/2
        u := cnt[x]+(cnt[5000]-cnt[y])
        if ans > u { ans = u }
    }
    writer.WriteString(strconv.Itoa(ans)+"\n")
    writer.Flush()
}

// 253B-mic
package main

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

func main() {
	var T []int
	in, _ := os.Open("input.txt")
	defer in.Close()
	sc := bufio.NewScanner(in)
	sc.Split(bufio.ScanWords)
	ou, _ := os.Create("output.txt")
	defer ou.Close()
	wr := bufio.NewWriter(ou)
	sc.Scan()
	a, _ := strconv.Atoi(sc.Text())
	n := a
	for i := 0; i < n; i++ {
		sc.Scan()
		aa, _ := strconv.Atoi(sc.Text())
		T = append(T, aa)
	}
	sort.Sort(sort.IntSlice(T))
	r := n - 1
	j := 0
	for i := 0; i < n; i++ {
		for j < n && T[j] <= 2*T[i] {
			j++
		}
		r = int(math.Min(float64(r), float64(n-(j-i))))
	}
	wr.WriteString(strconv.Itoa(r))
	wr.WriteString("\n")
	wr.Flush()
}


Timur has a stairway with $$$n$$$ steps. The $$$i$$$-th step is $$$a_i$$$ meters higher than its predecessor. The first step is $$$a_1$$$ meters higher than the ground, and the ground starts at $$$0$$$ meters.   The stairs for the first test case. Timur has $$$q$$$ questions, each denoted by an integer $$$k_1, \dots, k_q$$$. For each question $$$k_i$$$, you have to print the maximum possible height Timur can achieve by climbing the steps if his legs are of length $$$k_i$$$. Timur can only climb the $$$j$$$-th step if his legs are of length at least $$$a_j$$$. In other words, $$$k_i \geq a_j$$$ for each step $$$j$$$ climbed.Note that you should answer each question independently.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case contains two integers $$$n, q$$$ ($$$1 \leq n, q \leq 2\cdot10^5$$$) — the number of steps and the number of questions, respectively. The second line of each test case contains $$$n$$$ integers ($$$1 \leq a_i \leq 10^9$$$) — the height of the steps. The third line of each test case contains $$$q$$$ integers ($$$0 \leq k_i \leq 10^9$$$) — the numbers for each question. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2\cdot10^5$$$, and the sum of $$$q$$$ does not exceed $$$2\cdot10^5$$$.

For each test case, output a single line containing $$$q$$$ integers, the answer for each question. Please note, that the answer for some questions won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).

NoteConsider the first test case, pictured in the statement.   If Timur's legs have length $$$1$$$, then he can only climb stair $$$1$$$, so the highest he can reach is $$$1$$$ meter.  If Timur's legs have length $$$2$$$ or $$$4$$$, then he can only climb stairs $$$1$$$, $$$2$$$, and $$$3$$$, so the highest he can reach is $$$1+2+1=4$$$ meters.  If Timur's legs have length $$$9$$$ or $$$10$$$, then he can climb the whole staircase, so the highest he can reach is $$$1+2+1+5=9$$$ meters.  In the first question of the second test case, Timur has no legs, so he cannot go up even a single step. :(

package main

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

func trim(s string) string {
	return s[:len(s)-2]
}

func checkNumberPowerOfTwo(n int) int {
	return n & (n - 1)
}

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

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

func sliceStr2Int(arr []string) []int {
	var arrInt []int
	for _, i := range arr {
		num, _ := strconv.Atoi(i)
		arrInt = append(arrInt, num)
	}

	return arrInt
}

func sliceIntFromConsole() []int {
	arrLine, _ := reader.ReadString('\n')
	return sliceStr2Int(strings.Split(trim(arrLine), " "))
}

func check(i int) int {
	if i == 0 {
		return 0
	}
	return i
}

var reader = bufio.NewReader(os.Stdin)

func main() {
	testLine, _ := reader.ReadString('\n')
	test, _ := strconv.Atoi(trim(testLine))

	for t := 0; t < test; t++ {
		nq := sliceIntFromConsole()
		n, q := nq[0], nq[1]
		a := sliceIntFromConsole()
		kRaw := sliceIntFromConsole()

		var k [][]int
		for i, v := range kRaw {
			k = append(k, []int{i, v})
		}
		sort.Slice(k, func(i, j int) bool {
			return k[i][1] < k[j][1]
		})

		answer := make([]int, q)
		var p1, p2, height int
		// p1 -- points at array a
		// p2 -- points at array k
		for {
			if p2 == q {
				break
			}
			if p1 == n {
				for i := check(p2); i < q; i++ {
					answer[k[i][0]] = height
				}
				break
			}

			if k[p2][1] >= a[p1] {
				height += a[p1]
				p1++
			} else {
				answer[k[p2][0]] = height
				p2++
			}
		}

		for _, v := range answer {
			fmt.Print(v, " ")
		}
		fmt.Println()
	}
}

//1
//15 10
//11 14 3 10 6 11 3 14 3 7 14 2 10 5 4
//100 46 29 82 70 8 28 28 119 100

package main

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

const cf = true

func solve() {
	var n, q int
	fmt.Fscan(in, &n, &q)
	mx := make([]int, n)
	sum := make([]int, n)
	var num int
	for i := range mx {
		fmt.Fscan(in, &num)
		mx[i] = num
		if i > 0 {
			mx[i] = max(mx[i], mx[i-1])
		}
		sum[i] = num
		if i > 0 {
			sum[i] += sum[i-1]
		}
	}
	for i := 0; i < q; i++ {
		fmt.Fscan(in, &num)
		l, r := -1, n
		for l < r-1 {
			mid := (l + r) / 2
			if num >= mx[mid] {
				l = mid
			} else {
				r = mid
			}
		}
		if l == -1 {
			fmt.Fprint(out, "0 ")
		} else {
			fmt.Fprint(out, sum[l], " ")
		}
	}
	fmt.Fprintln(out)
}

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

func GCD(a, b int) int {
	for a != b {
		if a > b {
			a -= b
		} else {
			b -= a
		}
	}

	return a
}

type SqrtInt interface {
	SignedSqrt | UnsignedSqrt
}

type SignedSqrt interface {
	~int | ~int32 | ~int64
}

type UnsignedSqrt interface {
	~uint | ~uint32 | ~uint64 | ~uintptr
}

func sqrt[T SqrtInt](x T) T {
	l, r := T(0), T(2000000123)
	for l < r {
		mid := (l + r) / 2
		if mid*mid > x {
			r = mid
		} else {
			l = mid + 1
		}
	}
	return l - 1
}

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

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

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

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

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

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

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

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

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

var exists = struct{}{}

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

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

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

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

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

type Ordered interface {
	Integer | Float | ~string
}

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

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

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

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

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

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

package main

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

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

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

	var T int
	var n int
	var m int
	var x int

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		Fscan(in, &m)
		r := make([]int, n)
		q := make([]int, m)
		st := []int{}
		stv := []int{}
		s := make([]int64, n)
		for i := 0; i < n; i++ {
			Fscan(in, &x)
			r[i] = x
			if len(st) == 0 || stv[len(stv)-1] < x {
				st = append(st, i)
				stv = append(stv, x)
			}

			s[i] += int64(x)
			if i > 0 {
				s[i] += s[i-1]
			}
		}
		for i := 0; i < m; i++ {
			Fscan(in, &x)
			q[i] = x
		}

		ans := make([]int64, m)
		for i, v := range q {
			t := sort.SearchInts(stv, v+1)
			if t >= len(stv) {
				ans[i] = s[n-1]
			} else if t == 0 {
				ans[i] = 0
			} else {
				ans[i] = s[st[t]-1]
			}
		}

		for _, v := range ans {
			Fprintf(out, "%d ", v)
		}
		Fprintln(out, "")
	}
}

package main

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

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

func bs(arr []int, l, r, v int) int {
	if l == r {
		return l
	}
	m := (l + r + 1) >> 1
	if arr[m] > v {
		return bs(arr, l, m-1, v)
	}
	return bs(arr, m, r, v)
}

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

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {

		var n, q int
		fmt.Fscan(r, &n, &q)
		sArr := make([]int, n+1)
		hArr := make([]int, n+1)
		for i := range sArr {
			if i == 0 {
				continue
			}
			fmt.Fscan(r, &sArr[i])
			hArr[i] = hArr[i-1] + sArr[i]
			sArr[i] = max(sArr[i], sArr[i-1])
		}

		for i := 0; i < q; i++ {
			var qq int
			fmt.Fscan(r, &qq)
			fmt.Fprintf(w, "%d ", hArr[bs(sArr, 0, n, qq)])
		}

		fmt.Fprintln(w, "")
	}

}

package main

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

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

func readInt() int {
	var i int
	fmt.Fscan(reader, &i)
	return i
}

func readString() string {
	var s string
	fmt.Fscan(reader, &s)
	return s
}

func main(){
	defer writer.Flush()
	T := readInt()
	for i := 1; i <= T; i++{
		n, m := readInt(), readInt()
		steps := make([]int64, n)
		ans := make([]int64, m)
		pairs := make([][]int64, m)
		for i := 0; i < n; i++ {
			steps[i] = int64(readInt())
		}
		for i := 0; i < m; i++ {
			q := readInt()
			pairs[i] = []int64{int64(q), int64(i)}
		}
		sort.Slice(pairs, func(i,  j int) bool {
			return pairs[i][0] < pairs[j][0]
		})
		cur, p1 := int64(0), 0
		for p2 := 0; p2 < m; p2++ {
			for ; p1 < n; {
				if pairs[p2][0] >= steps[p1] {
					cur += steps[p1]
					p1++
				} else{
					break
				}
			}
			ans[pairs[p2][1]] = cur
		}
		for i := 0; i < m; i++ {
			fmt.Fprint(writer, ans[i], " ")
		}
		fmt.Fprintln(writer)
	}
}

package main

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

const mod = 998244353
const mx = int64(1e9 + 7)

func solve(in *bufio.Reader, out *bufio.Writer) {
	defer out.Flush()
	var n, q int
	Fscan(in, &n, &q)
	arr := make([]int64, n+1)
	for i := 1; i <= n; i++ {
		Fscan(in, &arr[i])
	}
	height := make([]int, n+1)
	for i := 1; i <= n; i++ {
		height[i] = max(height[i-1], int(arr[i]))
		arr[i] += arr[i-1]
	}
	var ans []int64
	for x := 0; x < q; x++ {
		var hg int
		Fscan(in, &hg)
		l, r := 0, n-1
		for l <= r {
			mid := (l + r) / 2
			if height[mid] > hg {
				r = mid - 1
			} else {
				l = mid + 1
			}
		}
		if height[l] > hg {
			l--
		}
		ans = append(ans, arr[l])
	}
	for _, v := range ans {
		Fprintf(out, "%d ", v)
	}
	Fprintln(out)
}

func abs(a int64) int64 {
	if a > 0 {
		return a
	}
	return -a
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}
func gcd(a, b int) int {
	if a%b == 0 {
		return b
	}
	return gcd(b, a%b)
}

func main() {
	stdin := os.Stdin
	stdout := os.Stdout
	in := bufio.NewReader(stdin)
	out := bufio.NewWriter(stdout)
	n := 1
	Fscan(in, &n)
	for i := 0; i < n; i++ {
		solve(in, out)
	}
}

package main

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

func max(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, q int
		fmt.Fscan(in, &n, &q)

		aa := make([]int64, n)
		for i := range aa {
			fmt.Fscan(in, &aa[i])
		}

		pf := make([]int64, n+1)
		for i, v := range aa {
			pf[i+1] = v + pf[i]
		}

		bb := [][2]int{}
		for i, v := range aa {
			if len(bb) == 0 || int(v) > bb[len(bb)-1][0] {
				bb = append(bb, [2]int{int(v), i})
			}
			bb[len(bb)-1][1] = i
		}

		qq := make([]int, q)
		for i := range qq {
			fmt.Fscan(in, &qq[i])
		}

		for _, v := range qq {
			idx := sort.Search(len(bb), func(i int) bool {
				return bb[i][0] >= v
			})
			var d int64
			if idx >= len(bb) {
				d = pf[n]
			} else {
				if v < bb[idx][0] {
					idx--
				}
				if idx < 0 {
					d = 0
				} else {
					d = pf[bb[idx][1]+1]
				}
			}
			fmt.Fprintf(out, "%d ", d)
		}
		fmt.Fprintln(out)
	}
}

package main

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

type IO struct {
	in  io.Reader
	out io.Writer
}

func NewIO(in io.Reader, out io.Writer) *IO {
	return &IO{in: bufio.NewReader(in), out: out}
}

func (io *IO) ReadInt() int {
	var res int
	_, _ = fmt.Fscanf(io.in, "%d\n", &res)
	return res
}

func (io *IO) ReadPairInt() (int, int) {
	var a, b int
	_, _ = fmt.Fscanf(io.in, "%d %d\n", &a, &b)
	return a, b
}

func (io *IO) ReadString() string {
	var res string
	_, _ = fmt.Fscanf(io.in, "%s\n", &res)
	return res
}

func (io *IO) ReadIntSlice(n int) sort.IntSlice {
	var t int
	res := make(sort.IntSlice, 0, n)
	for i := 0; i < n; i++ {
		_, _ = fmt.Fscanf(io.in, "%d", &t)
		res = append(res, t)
	}
	_, _ = fmt.Fscanf(io.in, "\n")
	return res
}

func (io *IO) Println(v ...interface{}) {
	_, _ = fmt.Fprintln(io.out, v...)
}

func (io *IO) Print(v ...interface{}) {
	_, _ = fmt.Fprint(io.out, v...)
}

func (io *IO) Printf(format string, v ...interface{}) {
	_, _ = fmt.Fprintf(io.out, format, v...)
}

func (io *IO) PrintIntSlice(s sort.IntSlice) {
	for _, v := range s {
		io.Printf("%d ", v)
	}
}

func (io *IO) PrintlnIntSlice(s sort.IntSlice) {
	io.PrintIntSlice(s)
	io.Printf("\n")
}

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

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

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

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

func main() {
	io := NewIO(os.Stdin, os.Stdout)

	t := io.ReadInt()
	for ; t > 0; t-- {
		a := io.ReadIntSlice(2)
		n, q := a[0], a[1]

		stairs := io.ReadIntSlice(n)
		queries := io.ReadIntSlice(q)

		heightOnPosition := make([]int64, len(stairs))
		heightOnPosition[0] = int64(stairs[0])
		for i := 1; i < len(stairs); i++ {
			heightOnPosition[i] = heightOnPosition[i-1] + int64(stairs[i])
		}

		mxOnPrefix := make([]int, len(stairs))
		mxOnPrefix[0] = stairs[0]
		for i := 1; i < len(stairs); i++ {
			mxOnPrefix[i] = max(mxOnPrefix[i-1], stairs[i])
		}

		lastPositionLessEqualThanK := func(k int) int {
			if mxOnPrefix[0] > k {
				return -1
			}

			l, r := 0, len(mxOnPrefix)-1
			for r-l > 1 {
				m := (r + l) / 2
				if mxOnPrefix[m] > k {
					r = m
				} else {
					l = m
				}
			}

			for i := r; i >= l; i-- {
				if mxOnPrefix[i] > k {
					continue
				}
				return i
			}

			return -2
		}

		for i := 0; i < len(queries); i++ {
			k := queries[i]
			pos := lastPositionLessEqualThanK(k)
			if pos == -1 {
				io.Print(0, " ")
			} else {
				io.Print(heightOnPosition[pos], " ")
			}
		}

		io.Println()
	}
}

package main

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

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

func bsearch(a []int, l, r, v int) int {
	if (l == r) { return l }
	m := (l + r + 1) >> 1
	if a[m] > v { return bsearch(a, l, m-1, v) }
	return bsearch(a, m, r, v)
}

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

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {

		var n, q int
		fmt.Fscan(r, &n, &q)
		a := make([]int, n+1)
		h := make([]int, n+1)
		for i := range a {
			if i == 0 { continue }
			fmt.Fscan(r, &a[i])
			h[i] = h[i-1] + a[i]
			a[i] = max(a[i], a[i-1])
		}

		for i := 0; i < q; i++ {
			var qq int
			fmt.Fscan(r, &qq)
			fmt.Fprintf(w, "%d ", h[bsearch(a, 0, n, qq)])
		}

		fmt.Fprintln(w, "")

	}

}

package main

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

// https://space.bilibili.com/206214
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,q,v int
		Fscan(in, &n,&q)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		sum := make([]int64, len(a)+1)
		for i, v := range a {
			sum[i+1] = sum[i] + int64(v)
		}
		mx := make([]int, n)
		mx[0] = a[0]
		for i := 1; i < n; i++ {
			mx[i] = max(mx[i-1], a[i])
		}
		for qi := 0; qi < q; qi++ {
			Fscan(in, &v)
			j := sort.SearchInts(mx, v+1)
			Fprint(out, sum[j], " ")
		}
		Fprintln(out)
	}

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

	// 如果是多组数据，请务必加上这段保险 —— 已经无法统计，有多少位竞赛选手在漏读数据上损失大把分数（包括我）
	// 此外，如果不小心写成了 scan(v) 而不是 scan(&v)，由于未传入指针，不会读入任何数据，这样本地运行一次就能立马发现错误
	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"
	"sort"
	"strconv"
	"strings"
)

func trim(s string) string {
	return s[:len(s)-2]
}

func checkNumberPowerOfTwo(n int) int {
	return n & (n - 1)
}

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

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

func sliceStr2Int(arr []string) []int {
	var arrInt []int
	for _, i := range arr {
		num, _ := strconv.Atoi(i)
		arrInt = append(arrInt, num)
	}

	return arrInt
}

func sliceIntFromConsole() []int {
	arrLine, _ := reader.ReadString('\n')
	return sliceStr2Int(strings.Split(trim(arrLine), " "))
}

func check(i int) int {
	if i == 0 {
		return 0
	}
	return i
}

var reader = bufio.NewReader(os.Stdin)

func main() {
	testLine, _ := reader.ReadString('\n')
	test, _ := strconv.Atoi(trim(testLine))

	for t := 0; t < test; t++ {
		nq := sliceIntFromConsole()
		n, q := nq[0], nq[1]
		a := sliceIntFromConsole()
		kRaw := sliceIntFromConsole()

		var k [][]int
		for i, v := range kRaw {
			k = append(k, []int{i, v})
		}
		sort.Slice(k, func(i, j int) bool {
			return k[i][1] < k[j][1]
		})
		fmt.Println(k)

		answer := make([]int, q)
		var p1, p2, height int
		// p1 -- points at array a
		// p2 -- points at array k
		for {
			if p2 == q {
				break
			}
			if p1 == n {
				for i := check(p2); i < q; i++ {
					answer[k[i][0]] = height
				}
				break
			}

			if k[p2][1] >= a[p1] {
				height += a[p1]
				p1++
			} else {
				fmt.Println(k[p2][0])
				answer[k[p2][0]] = height
				p2++
			}
		}

		for _, v := range answer {
			fmt.Print(v, " ")
		}
		fmt.Println()
	}
}

//1
//15 10
//11 14 3 10 6 11 3 14 3 7 14 2 10 5 4
//100 46 29 82 70 8 28 28 119 100

package main

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

func trim(s string) string {
	return s[:len(s)-2]
}

func checkNumberPowerOfTwo(n int) int {
	return n & (n - 1)
}

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

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

func sliceStr2Int(arr []string) []int {
	var arrInt []int
	for _, i := range arr {
		num, _ := strconv.Atoi(i)
		arrInt = append(arrInt, num)
	}

	return arrInt
}

func sliceIntFromConsole() []int {
	arrLine, _ := reader.ReadString('\n')
	return sliceStr2Int(strings.Split(trim(arrLine), " "))
}

func check(i int) int {
	if i == 0 {
		return 0
	}
	return i
}

var reader = bufio.NewReader(os.Stdin)

func main() {
	testLine, _ := reader.ReadString('\n')
	test, _ := strconv.Atoi(trim(testLine))

	for t := 0; t < test; t++ {
		nq := sliceIntFromConsole()
		n, q := nq[0], nq[1]
		a := sliceIntFromConsole()
		kRaw := sliceIntFromConsole()

		var k [][]int
		for i, v := range kRaw {
			k = append(k, []int{i, v})
		}
		sort.Slice(k, func(i, j int) bool {
			return k[i][1] < k[j][1]
		})

		answer := make([]int, q)
		var p1, p2, height int
		// p1 -- points at array a
		// p2 -- points at array k
		for {
			if p2 == q {
				break
			}
			if p1 == n {
				for i := check(p2); i < q; i++ {
					answer[i] = height
				}
				break
			}

			if k[p2][1] >= a[p1] {
				height += a[p1]
				p1++
			} else {
				answer[p2] = height
				p2++
			}
		}

		for _, v := range answer {
			fmt.Print(v, " ")
		}
		fmt.Println()
	}
}

package main

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

const cf = true

func solve() {
	var n, q int
	fmt.Fscan(in, &n, &q)
	mx := make([]int, n)
	sum := make([]int, n)
	var num int
	for i := range mx {
		fmt.Fscan(in, &num)
		mx[i] = max(mx[i], num)
		sum[i] = num
		if i > 0 {
			sum[i] += sum[i-1]
		}
	}
	for i := 0; i < q; i++ {
		fmt.Fscan(in, &num)
		l, r := -1, n
		for l < r-1 {
			mid := (l + r) / 2
			if num >= mx[mid] {
				l = mid
			} else {
				r = mid
			}
		}
		if l == -1 {
			fmt.Fprint(out, "0 ")
		} else {
			fmt.Fprint(out, sum[l], " ")
		}
	}
	fmt.Fprintln(out)
}

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

func GCD(a, b int) int {
	for a != b {
		if a > b {
			a -= b
		} else {
			b -= a
		}
	}

	return a
}

type SqrtInt interface {
	SignedSqrt | UnsignedSqrt
}

type SignedSqrt interface {
	~int | ~int32 | ~int64
}

type UnsignedSqrt interface {
	~uint | ~uint32 | ~uint64 | ~uintptr
}

func sqrt[T SqrtInt](x T) T {
	l, r := T(0), T(2000000123)
	for l < r {
		mid := (l + r) / 2
		if mid*mid > x {
			r = mid
		} else {
			l = mid + 1
		}
	}
	return l - 1
}

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

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

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

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

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

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

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

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

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

var exists = struct{}{}

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

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

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

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

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

type Ordered interface {
	Integer | Float | ~string
}

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

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

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

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

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

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

package main

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

const cf = true

func solve() {
	var n, q int
	fmt.Fscan(in, &n, &q)
	mx := make([]int, n)
	sum := make([]int, n)
	var num int
	for i := range mx {
		fmt.Fscan(in, &num)
		mx[i] = max(mx[i], num)
		sum[i] = num
		if i > 0 {
			sum[i] += sum[i-1]
		}
	}
	for i := 0; i < q; i++ {
		fmt.Fscan(in, &num)
		l, r := 0, n-1
		for l <= r {
			mid := (l + r) / 2
			if mx[mid] > num {
				r = mid - 1
			} else {
				l = mid + 1
			}
		}
		if l == 0 {
			fmt.Fprint(out, "0 ")
		} else {
			fmt.Fprint(out, sum[l-1], " ")
		}
	}
	fmt.Fprintln(out)
}

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

func GCD(a, b int) int {
	for a != b {
		if a > b {
			a -= b
		} else {
			b -= a
		}
	}

	return a
}

type SqrtInt interface {
	SignedSqrt | UnsignedSqrt
}

type SignedSqrt interface {
	~int | ~int32 | ~int64
}

type UnsignedSqrt interface {
	~uint | ~uint32 | ~uint64 | ~uintptr
}

func sqrt[T SqrtInt](x T) T {
	l, r := T(0), T(2000000123)
	for l < r {
		mid := (l + r) / 2
		if mid*mid > x {
			r = mid
		} else {
			l = mid + 1
		}
	}
	return l - 1
}

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

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

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

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

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

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

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

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

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

var exists = struct{}{}

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

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

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

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

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

type Ordered interface {
	Integer | Float | ~string
}

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

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

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

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

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

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

package main

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

const cf = true

func solve() {
	var n, q int
	fmt.Fscan(in, &n, &q)
	mx := make([]int, n)
	sum := make([]int, n)
	var num int
	for i := range mx {
		fmt.Fscan(in, &num)
		mx[i] = max(mx[i], num)
		sum[i] = num
		if i > 0 {
			sum[i] += sum[i-1]
		}
	}
	for i := 0; i < q; i++ {
		fmt.Fscan(in, &num)
		l, r := -1, n
		for l < r-1 {
			mid := (l + r) / 2
			if mx[mid] > num {
				r = mid
			} else {
				l = mid
			}
		}
		if l == -1 {
			fmt.Fprint(out, "0 ")
		} else {
			fmt.Fprint(out, sum[l], " ")
		}
	}
	fmt.Fprintln(out)
}

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

func GCD(a, b int) int {
	for a != b {
		if a > b {
			a -= b
		} else {
			b -= a
		}
	}

	return a
}

type SqrtInt interface {
	SignedSqrt | UnsignedSqrt
}

type SignedSqrt interface {
	~int | ~int32 | ~int64
}

type UnsignedSqrt interface {
	~uint | ~uint32 | ~uint64 | ~uintptr
}

func sqrt[T SqrtInt](x T) T {
	l, r := T(0), T(2000000123)
	for l < r {
		mid := (l + r) / 2
		if mid*mid > x {
			r = mid
		} else {
			l = mid + 1
		}
	}
	return l - 1
}

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

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

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

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

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

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

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

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

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

var exists = struct{}{}

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

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

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

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

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

type Ordered interface {
	Integer | Float | ~string
}

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

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

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

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

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

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

package main

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

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

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

	var T int
	var n int
	var m int
	var x int

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		Fscan(in, &m)
		r := make([]int, n)
		q := make([]int, m)
		st := []int{}
		stv := []int{}
		s := make([]int, n)
		for i := 0; i < n; i++ {
			Fscan(in, &x)
			r[i] = x
			if len(st) == 0 || stv[len(stv)-1] < x {
				st = append(st, i)
				stv = append(stv, x)
			}

			s[i] += x
			if i > 0 {
				s[i] += s[i-1]
			}
		}
		for i := 0; i < m; i++ {
			Fscan(in, &x)
			q[i] = x
		}

		ans := make([]int, m)
		for i, v := range q {
			t := sort.SearchInts(stv, v+1)
			if t >= len(stv) {
				ans[i] = s[n-1]
			} else if t == 0 {
				ans[i] = 0
			} else {
				ans[i] = s[st[t]-1]
			}
		}

		Fprintln(out, ans)
	}
}


There are one cat, $$$k$$$ mice, and one hole on a coordinate line. The cat is located at the point $$$0$$$, the hole is located at the point $$$n$$$. All mice are located between the cat and the hole: the $$$i$$$-th mouse is located at the point $$$x_i$$$ ($$$0 < x_i < n$$$). At each point, many mice can be located.In one second, the following happens. First, exactly one mouse moves to the right by $$$1$$$. If the mouse reaches the hole, it hides (i.e. the mouse will not any more move to any point and will not be eaten by the cat). Then (after that the mouse has finished its move) the cat moves to the right by $$$1$$$. If at the new cat's position, some mice are located, the cat eats them (they will not be able to move after that). The actions are performed until any mouse hasn't been hidden or isn't eaten.In other words, the first move is made by a mouse. If the mouse has reached the hole, it's saved. Then the cat makes a move. The cat eats the mice located at the pointed the cat has reached (if the cat has reached the hole, it eats nobody).Each second, you can select a mouse that will make a move. What is the maximum number of mice that can reach the hole without being eaten?

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of two lines. The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le k \le 4 \cdot 10^5$$$). The second line contains $$$k$$$ integers $$$x_1, x_2, \dots x_k$$$ ($$$1 \le x_i < n$$$) — the initial coordinates of the mice. It is guaranteed that the sum of all $$$k$$$ given in the input doesn't exceed $$$4 \cdot 10^5$$$.

For each test case output on a separate line an integer $$$m$$$ ($$$m \ge 0$$$) — the maximum number of mice that can reach the hole without being eaten.

package main

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

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, k int
		fmt.Fscan(in, &n, &k)
		arr := make([]int, k)
		for i := 0; i < k; i++ {
			fmt.Fscan(in, &arr[i])
		}
		sort.Sort(sort.Reverse(sort.IntSlice(arr)))
		/*sort.Slice(arr, func(i, j int) bool {
			return arr[i] > arr[j]
		})*/

		ans, cat := 0, 0
		for i := 0; i < k && cat < arr[i]; i++ {
			cat += n - arr[i]
			ans++
		}

		fmt.Fprintln(out, ans)
	}
}

package main

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

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, k int
		fmt.Fscan(in, &n, &k)
		arr := make([]int, k)
		for i := 0; i < k; i++ {
			fmt.Fscan(in, &arr[i])
		}
		/*sort.Sort(sort.Reverse(sort.IntSlice(arr)))*/
		sort.Slice(arr, func(i, j int) bool {
			return arr[i] > arr[j]
		})

		ans, cat := 0, 0
		for i := 0; i < k && cat < arr[i]; i++ {
			cat += n - arr[i]
			ans++
		}

		fmt.Fprintln(out, ans)
	}
}

package main

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

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, k int
		fmt.Fscan(in, &n, &k)
		arr := make([]int, k)
		for i := 0; i < k; i++ {
			fmt.Fscan(in, &arr[i])
		}
		sort.Sort(sort.Reverse(sort.IntSlice(arr)))
		/*sort.Slice(arr, func(x, y int) bool {
			return arr[x] > arr[y]
		})*/

		ans, cat := 0, 0
		for i := 0; i < k && cat < arr[i]; i++ {
			cat += n - arr[i]
			ans++
		}

		fmt.Fprintln(out, ans)
	}
}

package main

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

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, k int
		fmt.Fscan(in, &n, &k)
		arr := make([]int, k)
		for i := 0; i < k; i++ {
			fmt.Fscan(in, &arr[i])
		}
		sort.Slice(arr, func(i, j int) bool {
			return arr[i] > arr[j]
		})

		ans, cat := 0, 0
		for cnt := 0; cnt < k && cat < arr[cnt]; cnt++ {
			cat += n - arr[cnt]
			ans++
		}

		fmt.Fprintln(out, ans)
	}
}

package main

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

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, k int
		fmt.Fscan(in, &n, &k)
		arr := make([]int, k)
		for i := 0; i < k; i++ {
			fmt.Fscan(in, &arr[i])
		}
		sort.Slice(arr, func(i, j int) bool {
			return arr[i] > arr[j]
		})

		var cnt int
		cat := 0
		for cnt = 0; cnt < k && cat < arr[cnt]; cnt++ {
			cat += n - arr[cnt]
		}

		fmt.Fprintln(out, cnt)
	}
}

package main

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

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, k int
		fmt.Fscan(in, &n, &k)

		arr := make([]int, k)
		for i := 0; i < k; i++ {
			fmt.Fscan(in, &arr[i])
		}

		sort.Ints(arr)
		var total int
		var cat int
		for i := len(arr) - 1; i >= 0; i-- {
			if arr[i] > cat {
				total++
				cat += n - arr[i]
			}
		}
		fmt.Fprintln(out, total)
	}
}

package main

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

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, k int
		fmt.Fscanf(r, "%d %d\n", &n, &k)

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

		sort.Ints(arr)

		res := sort.Search(len(arr), func(i int) bool {
			m := int64(0)
			for ; i < len(arr); i++ {
				m += int64(n - arr[i])
			}

			return m < int64(n)
		})

		fmt.Println(k - res)
	}
}

package main

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

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

var pos [400000]int

func main() {
	var ntc, len, mice int
	fmt.Fscan(in, &ntc)
	for t := 0; t < ntc; t++ {
		fmt.Fscan(in, &len, &mice)
		for i := 0; i < mice; i++ {
			fmt.Fscan(in, &pos[i])
		}

		sort.Slice(pos[0:mice], func(i, j int) bool {
			return pos[i] < pos[j]
		})

		var miceTime, ans = 0, 0
		for i := mice - 1; i >= 0; i-- {
			miceTime += len - pos[i]
			if miceTime < len {
				ans++
			} else {
				break
			}
		}

		fmt.Fprintf(out, "%d\n", ans)
	}
	out.Flush()
}

package main

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

const Inf = 1000

var (
	rr = bufio.NewReader(os.Stdin)
	ww = bufio.NewWriter(os.Stdout)

	T int

	n, k int
	x []int
)

func main() {
	fmt.Fscan(rr, &T)
	for ; T > 0; T-- {
		fmt.Fscan(rr, &n, &k)
		x = make([]int, k)
		for i := range x {
			fmt.Fscan(rr, &x[i])
		}
		sort.Sort(sort.Reverse(sort.IntSlice(x)))

		fmt.Fprintln(ww, sort.Search(k + 1, func(k int) bool {
			if k == 0 {
				return false
			}
			res := 0
			for i := k - 1; i >= 0; i-- {
				if x[i] <= res {
					return true
				}
				res += n - x[i]
			}
			return false
		}) - 1)
	}
	_ = ww.Flush()
}

package main

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

func solve() {
	n := get()
	k := get()
	a := geta(k)
	sort.Ints(a)
	for i := 0; 2*i < k; i++ {
		a[i], a[k-i-1] = a[k-i-1], a[i]
	}
	cat := 0
	count := 0
	for _, v := range a {
		if cat >= v {
			break
		}
		count++
		cat += n - v
	}
	putln(count)
}

const (
	once       = false
	maxBufSize = 10000000
)

var (
	splitFunc = bufio.ScanWords
)

func main() {
	in = bufio.NewScanner(bufio.NewReader(os.Stdin))
	in.Split(splitFunc)
	in.Buffer(make([]byte, maxBufSize), maxBufSize)

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

	if once {
		solve()
		return
	}

	t := get()
	for t > 0 {
		solve()
		t--
	}
}

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

func get() int {
	x, _ := strconv.Atoi(gets())
	return x
}

func geta(n int) []int {
	a := make([]int, n)
	for i := range a {
		a[i] = get()
	}
	return a
}

func geta1(n int) []int {
	a := make([]int, n)
	for i := range a {
		a[i] = get() - 1
	}
	return a
}

func get64() int64 {
	x, _ := strconv.ParseInt(gets(), 10, 64)
	return x
}

func get64a(n int) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = get64()
	}
	return a
}

func get64a1(n int) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = get64() - 1
	}
	return a
}

func getf() float64 {
	x, _ := strconv.ParseFloat(gets(), 64)
	return x
}

func getfa(n int) []float64 {
	a := make([]float64, n)
	for i := range a {
		a[i] = getf()
	}
	return a
}

func gets() string {
	scan()
	return in.Text()
}

func scanN(n int) {
	for n > 0 {
		scan()
		n--
	}
}

func scan() {
	in.Scan()
}

func putc(c rune) {
	out.WriteRune(c)
}

func line() {
	putc('\n')
}

func space() {
	putc(' ')
}

func put(x int) {
	out.WriteString(strconv.Itoa(x))
}

func putln(i int) {
	put(i)
	line()
}

func puta(a []int) {
	for _, v := range a {
		put(v)
		space()
	}
	line()
}

func put64(x int64) {
	out.WriteString(strconv.FormatInt(x, 10))
}

func put64ln(x int64) {
	put64(x)
	line()
}

func put64a(a []int64) {
	for _, v := range a {
		put64(v)
		space()
	}
	line()
}

func puts(s string) {
	out.WriteString(s)
}

func putsln(s string) {
	puts(s)
	line()
}

func yn(y bool) {
	if y {
		yes()
		return
	}
	no()
}

func yes() {
	puts("YES\n")
}

func no() {
	puts("NO\n")
}

func putf(x float64) {
	out.WriteString(strconv.FormatFloat(x, 'f', -1, 64))
}

func putfln(x float64) {
	putf(x)
	line()
}

func putfa(a []float64) {
	for _, v := range a {
		putf(v)
		space()
	}
	line()
}

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

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

func absf(x float64) float64 {
	return math.Abs(x)
}

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

func max64(x int64, y int64) int64 {
	if x > y {
		return x
	}
	return y
}

func maxf(x float64, y float64) float64 {
	return math.Max(x, y)
}

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

func min64(x int64, y int64) int64 {
	if x < y {
		return x
	}
	return y
}

func minf(x float64, y float64) float64 {
	return math.Min(x, y)
}

func L(i int) int64 {
	return int64(i)
}

func ints(n int, val int) (a []int) {
	a = make([]int, n)
	if val == 0 {
		return
	}
	for i := range a {
		a[i] = val
	}
	return
}

func int64s(n int, val int64) (a []int64) {
	a = make([]int64, n)
	if val == 0 {
		return
	}
	for i := range a {
		a[i] = val
	}
	return
}

func floats(n int, val float64) (a []float64) {
	a = make([]float64, n)
	if val == 0 {
		return
	}
	for i := range a {
		a[i] = val
	}
	return
}

package main

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

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

  var t int
  for fmt.Fscan(reader, &t); t > 0; t-- {
    var n, k int
    fmt.Fscan(reader, &n, &k)
    arr := make([]int, k)
    for i := 0; i < k; i++ {
      fmt.Fscan(reader, &arr[i])
    }
    sort.Ints(arr)
    d := 0
    ans := 0
    for i := k-1; i >= 0; i-- {
      if arr[i] <= d {
        break
      }
      ans++
      d += (n-arr[i])
    }
    fmt.Fprint(writer, ans, "\n")
  }
}

package main

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

/*
4
4 2
2 3
4 2
2 2
2 1
1
2 1
2
---
2
1
1
1
=====
*/

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

	bld := strings.Builder{}

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

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

		a := make([]int, k)

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

		sort.Ints(a)

		catPos := 0
		miceCnt := 0

		for i := len(a) - 1; i >= 0; i-- {
			num := a[i]
			if num > catPos {
				miceCnt++
				catPos += n - num
			} else {
				break
			}
		}

		fmt.Fprintln(&bld, miceCnt)
	}

	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 int) int {
	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) 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 {
	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"
	"sort"
)

//problem C

type problem struct {
	n, k int
	mice []int
}

func (p *problem) SaveMices() int {
	remainSecond := p.n - 1
	saved := 0
	sort.Ints(p.mice)
	for i := p.k - 1; i >= 0; i-- {
		needSecond := p.n - p.mice[i]
		if remainSecond >= needSecond {
			saved++
			remainSecond -= needSecond
		} else {
			break
		}
	}

	return saved
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	var t int
	var p problem

	fmt.Fscanf(reader, "%d\n", &t)
	for ; t > 0; t-- {
		fmt.Fscanf(reader, "%d  %d\n", &p.n, &p.k)
		p.mice = make([]int, p.k)

		for i := 0; i < p.k; i++ {
			if i < p.k-1 {
				fmt.Fscanf(reader, "%d ", &p.mice[i])
			} else {
				fmt.Fscanf(reader, "%d\n", &p.mice[i])
			}
		}

		fmt.Printf("%d\n", p.SaveMices())
	}
}

package main

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

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

	return b
}

func min(nums ...int) int {
	result := nums[0]
	for _, num := range nums {
		result = min2(result, num)
	}

	return result
}

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()
	var t, n, k, mice int

	_, _ = fmt.Fscan(r, &t)

	for ; t > 0; t-- {
		_, _ = fmt.Fscan(r, &n, &k)
		var x []int
		for i := 1; i <= k; i++ {
			_, _ = fmt.Fscan(r, &mice)
			x = append(x, mice)
		}

		sort.Ints(x)

		catpos := 0
		result := 0
		for i := k - 1; i >= 0; i-- {
			if catpos >= x[i] {
				break
			}

			catpos += n - x[i]
			result++
		}

		fmt.Println(result)
	}
}

package main

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

// var m []int
// err = json.Unmarshal([]byte(r.FormValue("example")), &m)

// for i := 0; i < len(m); i++ {
// 	fmt.Println(m[i])
// }

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

func MIN(x, y int64) int64 {
	if x > y {
		return y
	} else {
		return x
	}
}

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

func MKO(x, y int64) int64 {
	if x < y {
		return 0
	} else {
		return x - y + 1
	}
}

func MAX(x, y int64) int64 {
	if x > y {
		return x
	} else {
		return y
	}
}

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

func LCM(x, y int) int {
	if x >= y {
		initial := x
		for i := 1; i < y+2; i++ {
			if initial%y == 0 {
				break
			} else {
				initial = initial + x
			}

		}
		return initial
	} else {
		initial := y
		for i := 1; i < x+2; i++ {
			if initial%x == 0 {
				break
			} else {
				initial = initial + y
			}

		}
		return initial
	}
}

func GCD(x, y int64) int64 {
	if x*y == 0 {
		return MAX(x, y)
	} else {
		var l, q int64
		l = MAX(x, y)
		q = MIN(x, y)
		var m int64
		for l*q > 0 {
			a := l % q
			b := l - a
			cc := int64(b / q)
			remain := l - cc*q
			l = MAX(remain, q)
			q = MIN(remain, q)
			if l*q == 0 {
				m = l + q
				break
			}

		}
		return m
	}
}

func IsItPrime(x int) bool {
	if x < 10 {
		if x == 2 || x == 3 || x == 5 || x == 7 {
			return true
		} else {
			return false
		}
	} else {
		j := 0
		k := math.Sqrt(float64(x))
		for m := 3; m <= int(k); {
			if x%m == 0 {
				j = 1
				break
			}
			m = m + 2
		}
		if j == 1 {
			return false
		} else {
			return true
		}
	}
}

func PrimeFactorization(n int) (pfs map[int]int) {
	pfs = make(map[int]int)

	// Get the number of 2s that divide n
	for n%2 == 0 {
		if _, ok := pfs[2]; ok {
			pfs[2] += 1
		} else {
			pfs[2] = 1
		}
		n = n / 2
	}

	// n must be odd at this point. so we can skip one element
	// (note i = i + 2)
	for i := 3; i*i <= n; i = i + 2 {
		// while i divides n, append i and divide n
		for n%i == 0 {
			if _, ok := pfs[i]; ok {
				pfs[i] += 1
			} else {
				pfs[i] = 1
			}
			n = n / i
		}
	}

	// This condition is to handle the case when n is a prime number
	// greater than 2
	if n > 2 {
		pfs[n] = 1
	}

	return
}

func NumberOfDivisors(n int) int {
	pfs := PrimeFactorization(n)

	num := 1
	for _, exponents := range pfs {
		num *= (exponents + 1)
	}

	return num
}

func KthPrime(x int) (y int) {
	if x == 1 {
		y = 2
	}
	if x == 2 {
		y = 3
	}
	if x > 2 {
		var sum int = 2
		begin := 5
		for sum < x+1 {
			if IsItPrime(begin) == true {
				sum++
			}
			if sum == x {
				y = begin
				break
			} else {
				if begin%6 == 1 {
					begin = begin + 4
				} else {
					begin = begin + 2
				}
			}
		}
	}
	return
}

func MODULE(x, y int) int {
	if x > y {
		return x - y
	} else {
		return y - x
	}
}

func BITAND(x, y int64) int64 {
	var ans int64
	begin := x
	sec := y
	var count int64 = 1
	for 1 == 1 {
		if (begin%2)*(sec%2) == 1 {
			ans = ans + count
			count = count * 2
			begin = int64(begin / 2)
			sec = int64(sec / 2)
			if begin*sec == 0 {
				break
			}

		} else {
			count = count * 2
			begin = int64(begin / 2)
			sec = int64(sec / 2)
			if begin*sec == 0 {
				break
			}
		}
	}
	return ans
}

func Sum(n, m int) int {
	var ans int

	if n == 1 {
		return 0
	} else {
		for i := 2; i < n; i++ {
			ans = (ans + Sum(i, m)) % m
		}
		for j := 2; j < n+1; j++ {
			ans = (ans + Sum(n/j, m)) % m
		}
		return ans
	}
}

func ChangeStringToDate(x string) (t time.Time, err error) {
	t, err = time.Parse("2006-01-02", x)
	return
}

func DiffrenceBetweenTwoDates(first, second time.Time) int64 {
	date := first
	date2 := second
	k := date.Unix() - date2.Unix()
	return k
}

func Union(a, b []int) []int {
	m := make(map[int]bool)

	for _, item := range a {
		m[item] = true
	}

	for _, item := range b {
		if _, ok := m[item]; !ok {
			a = append(a, item)
		}
	}
	return a
}

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &t)

	for o := 0; o < t; o++ {
		var n, k int
		fmt.Fscan(input, &n, &k)

		ARR := make([]int, k)

		for i := 0; i < k; i++ {
			var p int
			fmt.Fscan(input, &p)

			ARR[i] = n - p

		}

		sort.Ints(ARR)
		init := 0
		num := 0
		for i := 0; i < k; i++ {
			init += ARR[i]
			if init <= n-1 {
				num++
			} else {
				break
			}

		}

		fmt.Println(num)

	}

}

package main

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

var (
	reader  = bufio.NewReader(os.Stdin)
	writer  = bufio.NewWriter(os.Stdout)
	t, n, k int
	x       []int
)

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n, &k)
		x = make([]int, k)
		for i := 0; i < k; i++ {
			fmt.Fscan(reader, &x[i])
		}
		sort.Ints(x)
		var i, c int
		for i = len(x) - 1; i >= 0 && c-x[i] < 0; i-- {
			c += n - x[i]
		}
		fmt.Println(len(x) - i - 1)
	}
}

package main

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

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

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var t int
	fmt.Fscan(reader, &t)
	for c := 0; c < t; c++ {
		var n, k int
		fmt.Fscan(reader, &n, &k)
		mice := make([]int, k)
		for i := 0; i < k; i++ {
			fmt.Fscan(reader, &mice[i])
		}
		sort.Slice(mice, func(i, j int) bool { return mice[i] > mice[j] })
		cat := 0
		result := 0
		for _, x := range mice {
			if cat >= n || cat >= x {
				break
			}
			cat += n - x
			result++
		}
		fmt.Fprintln(writer, result)
	}
}

package main

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

var n, k int

var x []int

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

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

		x = make([]int, k)

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

		ans := solve()

		fmt.Println(ans)
	}
}

func solve() int {
	sort.Ints(x)

	cat := 0

	saved := 0

	for  j := k - 1; j >= 0; j-- {
		pos := x[j]

		if pos <= cat {
			return saved
		}

		moves := n - pos

		cat += moves

		saved++
	}

	return k
}

package main

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

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, k int
		fmt.Fscanf(r, "%d %d\n", &n, &k)

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

		sort.Ints(arr)

		res := sort.Search(len(arr), func(i int) bool {
			m := 0
			for ; i < len(arr); i++ {
				m += n - arr[i]
			}

			return m < n
		})

		fmt.Println(k - res)
	}
}

package main

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

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, k int
		fmt.Fscanf(r, "%d %d\n", &n, &k)

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

		sort.Ints(arr)

		res := sort.Search(len(arr), func(i int) bool {
			m := 0
			for ; i < len(arr); i++ {
				m += n - arr[i]
				if m > arr[i] {
					return false
				}
			}

			return true
		})

		fmt.Println(k - res)
	}
}

package main

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

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

var pos [400000]int

func main() {
	var ntc, len, mice int
	fmt.Fscanf(in, "%d\n", &ntc)
	for t := 0; t < ntc; t++ {
		fmt.Fscanf(in, "%d %d\n", &len, &mice)
		for i := 0; i < mice; i++ {
			fmt.Fscanf(in, "%d ", &pos[i])
		}

		sort.Slice(pos[0:mice], func(i, j int) bool {
			return pos[i] < pos[j]
		})

		var miceTime, ans = 0, 0
		for i := mice - 1; i >= 0; i-- {
			miceTime += len - pos[i]
			if miceTime < len {
				ans++
			} else {
				break
			}
		}

		fmt.Fprintf(out, "%d\n", ans)
	}
	out.Flush()
}

package main

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

const Inf = 1000

var (
	rr = bufio.NewReader(os.Stdin)
	ww = bufio.NewWriter(os.Stdout)

	T int

	n, k int
	x []int
)

func main() {
	fmt.Fscan(rr, &T)
	for ; T > 0; T-- {
		fmt.Fscan(rr, &n, &k)
		x = make([]int, k)
		for i := range x {
			fmt.Fscan(rr, &x[i])
		}
		sort.Sort(sort.Reverse(sort.IntSlice(x)))

		fmt.Fprintln(ww, sort.Search(k, func(k int) bool {
			if k == 0 {
				return false
			}
			res := 0
			for i := k - 1; i >= 0; i-- {
				if x[i] <= res {
					return true
				}
				res += n - x[i]
			}
			return false
		}) - 1)
	}
	_ = ww.Flush()
}


JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way.First, he splits the Banh-mi into $$$n$$$ parts, places them on a row and numbers them from $$$1$$$ through $$$n$$$. For each part $$$i$$$, he defines the deliciousness of the part as $$$x_i \in \{0, 1\}$$$. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the $$$i$$$-th part then his enjoyment of the Banh-mi will increase by $$$x_i$$$ and the deliciousness of all the remaining parts will also increase by $$$x_i$$$. The initial enjoyment of JATC is equal to $$$0$$$.For example, suppose the deliciousness of $$$3$$$ parts are $$$[0, 1, 0]$$$. If JATC eats the second part then his enjoyment will become $$$1$$$ and the deliciousness of remaining parts will become $$$[1, \_, 1]$$$. Next, if he eats the first part then his enjoyment will become $$$2$$$ and the remaining parts will become $$$[\_, \_, 2]$$$. After eating the last part, JATC's enjoyment will become $$$4$$$.However, JATC doesn't want to eat all the parts but to save some for later. He gives you $$$q$$$ queries, each of them consisting of two integers $$$l_i$$$ and $$$r_i$$$. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range $$$[l_i, r_i]$$$ in some order.All the queries are independent of each other. Since the answer to the query could be very large, print it modulo $$$10^9+7$$$.

The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 \le n, q \le 100\,000$$$). The second line contains a string of $$$n$$$ characters, each character is either '0' or '1'. The $$$i$$$-th character defines the deliciousness of the $$$i$$$-th part. Each of the following $$$q$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \le l_i \le r_i \le n$$$) — the segment of the corresponding query.

Print $$$q$$$ lines, where $$$i$$$-th of them contains a single integer — the answer to the $$$i$$$-th query modulo $$$10^9 + 7$$$.

NoteIn the first example:   For query $$$1$$$: One of the best ways for JATC to eats those parts is in this order: $$$1$$$, $$$4$$$, $$$3$$$, $$$2$$$.  For query $$$2$$$: Both $$$3$$$, $$$4$$$ and $$$4$$$, $$$3$$$ ordering give the same answer. In the second example, any order of eating parts leads to the same answer.

package main

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

const MODULUS = 1000000007

type Solver struct {
	prefixOfOnceCount []int
	powerOfTwo [] int64
}

func (s *Solver) Init(input string) {

	s.prefixOfOnceCount = make([]int, len(input)+1)
	for index, char := range input {
		incrementBy := 0
		if char == '1' {
			incrementBy = 1
		}
		s.prefixOfOnceCount[index+1] = s.prefixOfOnceCount[index] + incrementBy
	}

	maxPowersOfTwoNeeded := len(input) + 7
	s.powerOfTwo = make([]int64, maxPowersOfTwoNeeded)
	s.powerOfTwo[0] = 1
	for i := 1; i < maxPowersOfTwoNeeded; i++ {
		s.powerOfTwo[i] = 2*s.powerOfTwo[i-1] % MODULUS
	}
}

func (s *Solver) Query(from, to int) int64 {
	ones := s.getOnceOnInterval(from, to)
	zeros :=  s.getZerosOnInterval(from, to)

	sumOfOnes := s.powerOf2(ones) - 1
	return (sumOfOnes * s.powerOf2(zeros)) % MODULUS
}

func (s *Solver) powerOf2(n int) int64 {
	return s.powerOfTwo[n]
}

func (s *Solver) getOnceOnInterval(from, to int) int {
	return s.prefixOfOnceCount[to] - s.prefixOfOnceCount[from]
}

func (s *Solver) getZerosOnInterval(from, to int) int {
	return (to - from) - s.getOnceOnInterval(from, to)
}

func main()  {

	var n, q int
	var input string
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf,"%d %d\n", &n, &q)
	fmt.Fscanf(inBuf,"%s\n", &input)

	solver := Solver{}
	solver.Init(input)

	for i := 0; i < q; i++ {
		var from, to int
		fmt.Fscanf(inBuf, "%d %d\n", &from, &to)
		fmt.Println(solver.Query(from-1, to))
	}
}

package main

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

const MODULUS = 1000000007

type Solver struct {
	prefixOfOnceCount []int
	powerOfTwo [] int64
}

func (s *Solver) Init(input string) {

	s.prefixOfOnceCount = make([]int, len(input)+1)
	for index, char := range input {
		incrementBy := 0
		if char == '1' {
			incrementBy = 1
		}
		s.prefixOfOnceCount[index+1] = s.prefixOfOnceCount[index] + incrementBy
	}

	maxPowersOfTwoNeeded := len(input) + 7
	s.powerOfTwo = make([]int64, maxPowersOfTwoNeeded)
	s.powerOfTwo[0] = 1
	for i := 1; i < maxPowersOfTwoNeeded; i++ {
		s.powerOfTwo[i] = 2*s.powerOfTwo[i-1] % MODULUS
	}
}

func (s *Solver) Query(from, to int) int64 {
	ones := s.getOnceOnInterval(from, to)
	zeros :=  s.getZerosOnInterval(from, to)

	sumOfOnes := s.powerOf2(ones) - 1
	return (sumOfOnes * s.powerOf2(zeros)) % MODULUS
}

func (s *Solver) powerOf2(n int) int64 {
	return s.powerOfTwo[n]
}

func (s *Solver) getOnceOnInterval(from, to int) int {
	return s.prefixOfOnceCount[to] - s.prefixOfOnceCount[from]
}

func (s *Solver) getZerosOnInterval(from, to int) int {
	return (to - from) - s.getOnceOnInterval(from, to)
}

func main()  {
	var n, q int
	var input string
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf,"%d %d\n", &n, &q)
	fmt.Fscanf(inBuf,"%s\n", &input)

	solver := Solver{}
	solver.Init(input)

	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() }()

	for i := 0; i < q; i++ {
		var from, to int
		fmt.Fscanf(inBuf, "%d %d\n", &from, &to)
		fmt.Fprintf(outBuf, "%d\n", solver.Query(from-1, to))
	}
}

// Giving a 1 inside the input with length N, it can contribute {1, 1, 2, 4, 8 ...}
package main

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

const m = 1000000007

func main() {
	reader := bufio.NewReaderSize(os.Stdin, 1000000)
	n, q, line := 0, 0, ""
	fmt.Fscanf(reader, "%d %d\n%s\n", &n, &q, &line)

	cMap, oneMap := map[int]int{0: 1, 1: 1}, map[int]int{0: 0}
	for i := 1; i <= n; i++ {
		oneMap[i] = oneMap[i-1]
		if line[i-1:i] == "1" {
			oneMap[i]++
		}
	}
	for i := 2; i <= (n + 1); i++ {
		cMap[i] = cMap[i-1] * 2 % m
	}

	for i := 0; i < q; i++ {
		l, r := 0, 0
		fmt.Fscanf(reader, "%d %d\n", &l, &r)

		width, oneCount := r-l+1, oneMap[r]-oneMap[l-1]
		sum := 0
		if oneCount > 0 {
			sum = (cMap[width+1] + m - cMap[width-oneCount+1]) % m
		}

		fmt.Println(sum)
	}
}

package main

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

const (
	Mod  = 1000000007
	MaxN = 100005
)

var n, q int
var input string
var pre, s [MaxN+1]int64

func init() {
	pre[0] = 1
	for i := 1; i < MaxN; i++ {
		pre[i] = (pre[i - 1] * 2) % Mod
	}
}

func main() {
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf,"%d %d\n", &n, &q)
	fmt.Fscanf(inBuf,"%s\n", &input)

	for i := 0; i < n; i++ {
		c := int64(0)
		if input[i] == '1' {
			c++
		}
		s[i + 1] = s[i] + c
	}

	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() }()
	for i := 0; i < q; i++ {
		var l, r int64
		fmt.Fscanf(inBuf, "%d %d\n", &l, &r)
		ones := s[r] - s[l - 1]
		zeros := r - l + 1 - ones
		fmt.Fprintf(outBuf, "%d\n", (pre[ones] - 1 + Mod) * pre[zeros] % Mod)
	}

}

package main

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

const MODULUS = 1000000007

type Solver struct {
	prefixOfOnceCount []int
	powerOfTwo [] int64
}

func (s *Solver) Init(input string) {

	s.prefixOfOnceCount = make([]int, len(input)+1)
	for index, char := range input {
		incrementBy := 0
		if char == '1' {
			incrementBy = 1
		}
		s.prefixOfOnceCount[index+1] = s.prefixOfOnceCount[index] + incrementBy
	}

	maxPowersOfTwoNeeded := len(input) + 7
	s.powerOfTwo = make([]int64, maxPowersOfTwoNeeded)
	s.powerOfTwo[0] = 1
	for i := 1; i < maxPowersOfTwoNeeded; i++ {
		s.powerOfTwo[i] = 2*s.powerOfTwo[i-1] % MODULUS
	}
}

func (s *Solver) Query(from, to int) int64 {
	ones := s.getOnceOnInterval(from, to)
	zeros :=  s.getZerosOnInterval(from, to)

	sumOfOnes := s.powerOf2(ones) - 1
	return (sumOfOnes * s.powerOf2(zeros)) % MODULUS
}

func (s *Solver) powerOf2(n int) int64 {
	return s.powerOfTwo[n]
}

func (s *Solver) getOnceOnInterval(from, to int) int {
	return s.prefixOfOnceCount[to] - s.prefixOfOnceCount[from]
}

func (s *Solver) getZerosOnInterval(from, to int) int {
	return (to - from) - s.getOnceOnInterval(from, to)
}

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

	line1, _ := reader.ReadString('\n')
	line1Elems := strings.Split(strings.Trim(line1, "\n"), " ")
	q, _ := strconv.ParseInt(line1Elems[1], 10, 32)

	line, _ := reader.ReadString('\n')

	solver := Solver{}
	solver.Init(line)

	for i := 0; i < int(q); i++ {
		ln, _ := reader.ReadString('\n')
		lnElems := strings.Split(strings.Trim(ln, "\n"), " ")
		from, _ := strconv.ParseInt(lnElems[0], 10, 32)
		to, _ := strconv.ParseInt(lnElems[1], 10, 32)

		fmt.Println(solver.Query(int(from)-1, int(to)))
	}
}

// Giving a 1 inside the input with length N, it can contribute {1, 1, 2, 4, 8 ...}
package main

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

const m = 1000000007

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	scanner.Scan()
	q, _ := strconv.Atoi(scanner.Text())
	scanner.Scan()
	line := scanner.Text()

	cMap, oneMap := map[int]int{0: 0, 1: 1, 2: 1}, map[int]int{0: 0}
	for i := 1; i <= n; i++ {
		oneMap[i] = oneMap[i-1]
		if line[i-1:i] == "1" {
			oneMap[i]++
		}
	}
	for i := 3; i <= (n + 2); i++ {
		cMap[i] = cMap[i-1] * 2 % m
	}

	for i := 0; i < q; i++ {
		scanner.Scan()
		l, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		r, _ := strconv.Atoi(scanner.Text())

		// width, sum, oneCount := r-l+1, 0, oneMap[r]-oneMap[l-1]
		// for j := 0; j < oneCount; j++ {
		// 	sum = (sum + cMap[width-j]) % m
		// }
		width, oneCount := r-l+1, oneMap[r]-oneMap[l-1]
		sum := 0
		if oneCount > 0 {
			// fmt.Println("cMap[width+2], cMap[width-oneCount+1]", cMap[width+2], cMap[width-oneCount+1])
			sum = cMap[width+2] - cMap[width-oneCount+2]
		}

		fmt.Println(sum)
	}
}