Recall that string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly zero or all) characters. For example, for the string $$$a$$$="wowwo", the following strings are subsequences: "wowwo", "wowo", "oo", "wow", "", and others, but the following are not subsequences: "owoo", "owwwo", "ooo".The wow factor of a string is the number of its subsequences equal to the word "wow". Bob wants to write a string that has a large wow factor. However, the "w" key on his keyboard is broken, so he types two "v"s instead. Little did he realise that he may have introduced more "w"s than he thought. Consider for instance the string "ww". Bob would type it as "vvvv", but this string actually contains three occurrences of "w":   "vvvv"  "vvvv"  "vvvv" For example, the wow factor of the word "vvvovvv" equals to four because there are four wows:  "vvvovvv"  "vvvovvv"  "vvvovvv"  "vvvovvv" Note that the subsequence "vvvovvv" does not count towards the wow factor, as the "v"s have to be consecutive.For a given string $$$s$$$, compute and output its wow factor. Note that it is not guaranteed that it is possible to get $$$s$$$ from another string replacing "w" with "vv". For example, $$$s$$$ can be equal to "vov".

The input contains a single non-empty string $$$s$$$, consisting only of characters "v" and "o". The length of $$$s$$$ is at most $$$10^6$$$.

Output a single integer, the wow factor of $$$s$$$.

NoteThe first example is explained in the legend.

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"reflect"
	"strconv"
	"unsafe"
)

var DEBUG = true

func main() {
	defer Flush()

	s := readb()

	var t []byte
	var i, j int
	for i < len(s) {
		if s[i] == 'v' {
			for j = i; j < len(s) && s[j] == 'v'; j++ {
			}
			for d := 0; d < (j - i - 1); d++ {
				t = append(t, 'w')
			}
			i = j
		} else if s[i] == 'o' {
			t = append(t, 'o')
			i++
		}
	}
	a := f(t)
	Reverse(t)
	b := f(t)
	Reverse(b)
	Reverse(t)

	var ans int64
	for i := 1; i < len(t)-1; i++ {
		if t[i] != 'o' {
			continue
		}
		ans += int64(a[i]) * int64(b[i])
	}
	println(ans)
}

func f(t []byte) []int {
	a := make([]int, len(t)+1)
	for i := range t {
		a[i+1] = a[i]
		if t[i] == 'w' {
			a[i+1]++
		}
	}
	return a
}

// =============================================================================

// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// these functions are derived from sort in Go source.
// name of functions are changed

// The function panics if the provided interface is not a slice.
func Reverse(slice interface{}) {
	rv := reflect.ValueOf(slice)
	swap := _reverse_Swapper(slice)
	length := rv.Len()
	for i, j := 0, length-1; i < j; i, j = i+1, j-1 {
		swap(i, j)
	}
}

func _reverse_Swapper(slice interface{}) func(i, j int) {
	v := reflect.ValueOf(slice)
	vdata := v.Pointer()
	vlen := v.Len()
	vcap := v.Cap()

	sz := int(v.Type().Elem().Size())

	// for typical small elements
	switch sz {
	case 24: // for slice
		var s [][3]int64
		h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
		h.Data = vdata
		h.Len = vlen
		h.Cap = vcap
		return func(i, j int) { s[i], s[j] = s[j], s[i] }
	case 16: // for string
		var s [][2]int64
		h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
		h.Data = vdata
		h.Len = vlen
		h.Cap = vcap
		return func(i, j int) { s[i], s[j] = s[j], s[i] }
	case 8:
		var s []int64
		h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
		h.Data = vdata
		h.Len = vlen
		h.Cap = vcap
		return func(i, j int) { s[i], s[j] = s[j], s[i] }
	case 4:
		var s []int32
		h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
		h.Data = vdata
		h.Len = vlen
		h.Cap = vcap
		return func(i, j int) { s[i], s[j] = s[j], s[i] }
	case 2:
		var s []int16
		h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
		h.Data = vdata
		h.Len = vlen
		h.Cap = vcap
		return func(i, j int) { s[i], s[j] = s[j], s[i] }
	case 1:
		var s []int8
		h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
		h.Data = vdata
		h.Len = vlen
		h.Cap = vcap
		return func(i, j int) { s[i], s[j] = s[j], s[i] }
	}

	// for large elements
	if sz%16 == 0 {
		m := sz / 16
		var s [][2]int64
		h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
		h.Data = vdata
		h.Len = vlen * m
		h.Cap = vcap * m
		return func(i, j int) {
			for k := 0; k < m; k++ {
				ri := i*m + k
				rj := j*m + k
				s[ri], s[rj] = s[rj], s[ri]
			}
		}
	}
	if sz%8 == 0 {
		m := sz / 8
		var s []int64
		h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
		h.Data = vdata
		h.Len = vlen * m
		h.Cap = vcap * m
		return func(i, j int) {
			for k := 0; k < m; k++ {
				ri := i*m + k
				rj := j*m + k
				s[ri], s[rj] = s[rj], s[ri]
			}
		}
	}
	if sz%4 == 0 {
		m := sz / 4
		var s []int32
		h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
		h.Data = vdata
		h.Len = vlen * m
		h.Cap = vcap * m
		return func(i, j int) {
			for k := 0; k < m; k++ {
				ri := i*m + k
				rj := j*m + k
				s[ri], s[rj] = s[rj], s[ri]
			}
		}
	}
	if sz%2 == 0 {
		m := sz / 2
		var s []int16
		h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
		h.Data = vdata
		h.Len = vlen * m
		h.Cap = vcap * m
		return func(i, j int) {
			for k := 0; k < m; k++ {
				ri := i*m + k
				rj := j*m + k
				s[ri], s[rj] = s[rj], s[ri]
			}
		}
	} else {
		m := sz
		var s []int8
		h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
		h.Data = vdata
		h.Len = vlen * m
		h.Cap = vcap * m
		return func(i, j int) {
			for k := 0; k < m; k++ {
				ri := i*m + k
				rj := j*m + k
				s[ri], s[rj] = s[rj], s[ri]
			}
		}
	}
}

// =============================================================================

// -----------------------------------------------------------------------------

// IO helper functions

// Returns next token from input.  It must be initialized by SetInput()
// before the first call.
var nextToken func() ([]byte, error)
var nextLine func() ([]byte, error)

// Holds a buffer for output.  It must be initialized by SetOutput().
// All IO fucntions (read*() and [e]print*()) should write to OutputWriter
// instead of this.
var OutputBuffer *bufio.Writer

// Holds an io.Writer.  It must be initialized by SetOutput()
var OutputWriter io.Writer

// Set IO functions for interactive input/output.
func SetInteractive(w io.Writer, r io.Reader) {
	SetUnbefferedInput(r)
	OutputBuffer = nil
	OutputWriter = w
}

// Setup OutputBuffer and OutputWriter.
func SetOutput(w io.Writer) {
	OutputBuffer = bufio.NewWriter(w)
	OutputWriter = OutputBuffer
}

// Flushes OutputBuffer
func Flush() {
	if OutputBuffer != nil {
		OutputBuffer.Flush()
	}
}

// Returns true if c is a white space
func IsSpace(c byte) bool {
	switch c {
	case '\t', '\n', '\v', '\f', '\r', ' ':
		return true
	}
	return false
}

func IsNewLine(c byte) bool {
	switch c {
	case '\n', '\r':
		return true
	}
	return false
}

// Setup nextToken with input buffer.
func SetInput(r io.Reader) {
	buf := new(bytes.Buffer)
	var b []byte

	var i int
	rest := func() ([]byte, error) {
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		return b[j:i], nil
	}
	initial := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextToken = rest
		return rest()
	}
	nextToken = initial

	restLn := func() ([]byte, error) {
		for i < len(b) && IsNewLine(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsNewLine(b[i]) {
			i++
		}
		return b[j:i], nil
	}

	initialLn := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextLine = restLn
		return restLn()
	}
	nextLine = initialLn
}

// Setup nextToken without input buffer.
func SetUnbefferedInput(r io.Reader) {
	buf := bufio.NewReader(r)
	var b []byte

	var i int
	nextToken = func() ([]byte, error) {
		var err error
		if i == len(b) {
			b, err = buf.ReadBytes('\n')
			if err != nil {
				return nil, err
			}
			i = 0
			j := len(b) - 1
			for 0 <= j && IsSpace(b[j]) {
				j--
			}
			b = b[0 : j+1]
		}
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		if i == j {
			return nil, io.ErrUnexpectedEOF
		}
		return b[j:i], nil
	}
}

// -----------------------------------------------------------------------------

// IO functions

// Reads next token and return it as []byte
func readb() []byte {
	b, err := nextToken()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

// Reads next token and return it as string
func reads() string {
	return string(readb())
}

// Read next line as []byte.  Trailing '\n' will not be included.
// See also comments on readb()
func readbln() []byte {
	b, err := nextLine()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

// Read next line as string
func readsln() string {
	return string(readbln())
}

// Reads next token and return it as int64
func readll() int64 {
	i, err := strconv.ParseInt(reads(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// Reads next token and return it as int
func readi() int {
	return int(readll())
}

// Reads next token and return it as float64
func readf() float64 {
	f, err := strconv.ParseFloat(reads(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// Write args to OutputWriter with the format f
func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(OutputWriter, f, args...)
}

// Write args to OutputWriter without format
func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(OutputWriter, args...)
}

// Write args to stderr with the format f
func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

// Write args to stderr without format
func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func dbgf(f string, args ...interface{}) {
	if !DEBUG {
		return
	}
	fmt.Fprintf(os.Stderr, f, args...)
}

func dbg(args ...interface{}) {
	if !DEBUG {
		return
	}
	fmt.Fprintln(os.Stderr, args...)
}

// -----------------------------------------------------------------------------

// Utilities

func sumSlice(a []int) int {
	var res int
	for _, v := range a {
		res += v
	}
	return res
}

func sumSlicell(a []int64) int64 {
	var res int64
	for _, v := range a {
		res += v
	}
	return res
}

func readInts(N int) (int, []int) {
	if N == 0 {
		N = readi()
	}
	a := make([]int, N)
	for i := range a {
		a[i] = readi()
	}
	return N, a
}

func readIntsll(N int) (int, []int64) {
	if N == 0 {
		N = readi()
	}
	a := make([]int64, N)
	for i := range a {
		a[i] = readll()
	}
	return N, a
}

// -----------------------------------------------------------------------------

// Simple math functions

const (
	// big prime
	INF  = 1000000007
	INF2 = 1000000009
	INF3 = 998244353
)

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

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

func minf(a, b float64) float64 {
	if a < b {
		return a
	}
	return b
}

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

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

func maxf(a, b float64) float64 {
	if a < b {
		return b
	}
	return a
}

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

func absll(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func absf(a float64) float64 {
	if a < 0 {
		return -a
	}
	return a
}

// egcd(a, b) returns d, x, y:
//   d is gcd(a,b)
//   x, y are  integers that satisfy ax + by = d
func egcd(a, b int) (int, int, int) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcd(b, a%b)
	return d, y, x - a/b*y
}

func egcdll(a, b int64) (int64, int64, int64) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcdll(b, a%b)
	return d, y, x - a/b*y
}

func gcd(a, b int) int {
	d, _, _ := egcd(a, b)
	return d
}

func gcdll(a, b int64) int64 {
	d, _, _ := egcdll(a, b)
	return d
}

// set up IO functions
func init() {
	// for non-interactive
	SetInput(os.Stdin)
	SetOutput(os.Stdout)

	// Enable below when interactive.  Its ok to leave above intact.
	// SetInteractive(os.Stdout, os.Stdin)
}

package main

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

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

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

func main() {
	defer writer.Flush()
	var s string
	scanf("%s", &s)
	n := len(s)
	larr := make([]int, n+3)
	rarr := make([]int, n+3)

	cur := 0

	for i := 0; i < n; i++ {
		if s[i] == 'v' {
			cur++
			larr[i+1] = larr[i]
		} else {
			if cur > 0 {
				larr[i+1] = cur - 1 + larr[i]
			} else {
				larr[i+1] = larr[i]
			}
			cur = 0
		}
	}

	cur = 0

	for i := n - 1; i >= 0; i-- {
		if s[i] == 'v' {
			cur++
			rarr[i+1] = rarr[i+2]
		} else {
			if cur > 0 {
				rarr[i+1] = cur - 1 + rarr[i+2]
			} else {
				rarr[i+1] = rarr[i+2]
			}
			cur = 0
		}
	}
	ans := int64(0)

	//fmt.Println(larr, rarr)

	for i := 0; i < n; i++ {
		if s[i] == 'o' {
			ans += int64(larr[i+1]) * int64(rarr[i+1])
		}
	}

	printf("%d", ans)
}

package main

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

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

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

func main() {
	defer writer.Flush()
	var s string
	scanf("%s", &s)
	n := len(s)
	larr := make([]int, n+3)
	rarr := make([]int, n+3)

	cur := 0

	for i := 0; i < n; i++ {
		if s[i] == 'v' {
			cur++
			larr[i+1] = larr[i]
		} else {
			if cur > 0 {
				larr[i+1] = cur - 1 + larr[i]
			} else {
				larr[i+1] = larr[i]
			}
			cur = 0
		}
	}

	cur = 0

	for i := n - 1; i >= 0; i-- {
		if s[i] == 'v' {
			cur++
			rarr[i+1] = rarr[i+2]
		} else {
			if cur > 0 {
				rarr[i+1] = cur - 1 + rarr[i+2]
			} else {
				rarr[i+1] = rarr[i+2]
			}
			cur = 0
		}
	}
	ans := int64(0)

	//fmt.Println(larr, rarr)

	for i := 0; i < n; i++ {
		if s[i] == 'o' {
			ans += int64(larr[i+1] * rarr[i+1])
		}
	}

	printf("%d", ans)
}


You have a long fence which consists of $$$n$$$ sections. Unfortunately, it is not painted, so you decided to hire $$$q$$$ painters to paint it. $$$i$$$-th painter will paint all sections $$$x$$$ such that $$$l_i \le x \le r_i$$$.Unfortunately, you are on a tight budget, so you may hire only $$$q - 2$$$ painters. Obviously, only painters you hire will do their work.You want to maximize the number of painted sections if you choose $$$q - 2$$$ painters optimally. A section is considered painted if at least one painter paints it.

The first line contains two integers $$$n$$$ and $$$q$$$ ($$$3 \le n, q \le 5000$$$) — the number of sections and the number of painters availible for hire, respectively. Then $$$q$$$ lines follow, each describing one of the painters: $$$i$$$-th line contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \le l_i \le r_i \le n$$$).

Print one integer — maximum number of painted sections if you hire $$$q - 2$$$ painters.

package main

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

func main() {
	read := bufio.NewReader(os.Stdin)
	write := bufio.NewWriter(os.Stdout)
	var n, q int
	fmt.Fscan(read, &n, &q)
	mlr := make([][2]int, q)
	zbr := make([]int, n)
	zbrtest := make([]int, n)
	nall := 0
	for i := 0; i < q; i++ {
		var l, r int
		fmt.Fscan(read, &l, &r)
		l = l - 1
		r = r - 1
		m := [2]int{l, r}
		mlr[i] = m
		for j := l; j <= r; j++ {
			if zbr[j] == 0 {
				nall = nall + 1
			}
			zbr[j] = zbr[j] + 1

		}
	}

	nmin := n + 1
	for i := 0; i < q-1; i++ {
		nminus1 := 0
		for k := mlr[i][0]; k <= mlr[i][1]; k++ {
			if zbr[k] == 1 {
				nminus1 = nminus1 + 1
			}
			if zbr[k] == 2 {
				zbrtest[k] = i + 1
			}
			//zbr[k] = zbr[k] - 1
		}
		if nminus1 >= nmin {
			continue
		}

		for j := i + 1; j < q; j++ {
			nminus2 := 0
			for k := mlr[j][0]; k <= mlr[j][1]; k++ {
				if zbr[k] == 1 {
					nminus2 = nminus2 + 1
				}
				if zbrtest[k] == (i + 1) {
					nminus2 = nminus2 + 1
				}
				if (nminus2 + nminus1) >= nmin {
					break
				}
			}
			if nminus1+nminus2 < nmin {
				nmin = nminus1 + nminus2
			}
			if nmin == 0 {
				break
			}
		}
		if nmin == 0 {
			break
		}
		//for k := mlr[i][0]; k <= mlr[i][1]; k++ {
		//	zbr[k] = zbr[k] + 1
		//}

	}

	/*
		fmt.Println(nall)
		fmt.Println(nmin)
		fmt.Println(zbr)
		fmt.Println(mlr[1], mlr[2])
	*/

	nall = nall - nmin
	fmt.Fprint(write, nall)
	write.Flush()
}

package main

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

var (
	tree []int
	ln   int
)

type Pair struct {
	l, r int
}

type SA []int

func (s SA) Len() int {
	return len(s)
}

func (s SA) Swap(a, b int) {
	tmp := s[a]
	s[a] = s[b]
	s[b] = tmp
}
func (s SA) Less(i, j int) bool {
	if s[i] < s[j] {
		return true
	}
	return false
}
func intect(x1, y1, x2, y2 int) (int, int) {
	x := x1
	if x < x2 {
		x = x2
	}
	y := y1
	if y > y2 {
		y = y2
	}
	return x, y
}
func leftR(x1, y1, x2, y2 int) (int, int) {
	if x1 > x2 {
		return leftR(x2, y2, x1, y1)
	}
	x := x1
	y := y1
	if x2-1 < y1 {
		y = x2 - 1
	}
	return x, y
}
func rightR(x1, y1, x2, y2 int) (int, int) {
	if y1 > y2 {
		return rightR(x2, y2, x1, y1)
	}
	x := x2
	y := y2
	if y1+1 > x2 {
		x = y1 + 1
	}
	return x, y
}
func main() {
	buf := bufio.NewReader(os.Stdin)
	var m int
	fmt.Fscanf(buf, "%d %d\n", &ln, &m)
	tree = make([]int, ln+1)
	rq := make([]*Pair, m)
	leftp := make(SA, m)
	rightp := make(SA, m)
	for i := 0; i < m; i++ {
		rq[i] = &Pair{
		}
		fmt.Fscanf(buf, "%d %d\n", &rq[i].l, &rq[i].r)
		leftp[i] = rq[i].l
		rightp[i] = rq[i].r
	}
	sort.Sort(leftp)
	sort.Sort(rightp)

	ones := make([]int, ln+1)
	two := make([]int, ln+1)
	tmp := 0
	lf := 0
	lr := 0
	total := 0
	for i := 1; i <= ln; i++ {
		for lf < m && leftp[lf] <= i {
			lf++
			tmp++
		}
		for lr < m && rightp[lr] < i {
			tmp--
			lr++
		}
//		fmt.Printf("%d ", tmp)
		if tmp == 1 {
			ones[i] = 1
		} else if tmp == 2 {
			two[i] = 1
		}
		if tmp > 0{
			total++
		}
		ones[i] += ones[i-1]
		two[i] += two[i-1]
	}
//	fmt.Printf("\n")
//	fmt.Printf("ones = %v\n",ones)
//	fmt.Printf("two = %v\n",two)
	ans := 0

	for i := 0; i < m; i++ {
		for j := i+1; j < m; j++ {
			tmp = 0
			x, y := intect(rq[i].l, rq[i].r, rq[j].l, rq[j].r)
			if x <= y {
				tmp += two[y] - two[x-1]
			}
			x, y = leftR(rq[i].l, rq[i].r, rq[j].l, rq[j].r)
			if x <= y {
				tmp += ones[y] - ones[x-1]
			}
			x, y = rightR(rq[i].l, rq[i].r, rq[j].l, rq[j].r)
			if x <= y {
				tmp += ones[y] - ones[x-1]
			}
			if total-tmp > ans {
				ans = total - tmp
			}
		}
	}
	fmt.Printf("%d\n", ans)
}

/**
0 0
0 1
3
UDD
 */

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1132C(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	type pair struct{ x, y int }

	var n, m, l, r, all int
	Fscan(in, &n, &m)
	ids := make([][]int, n+1)
	for i := 0; i < m; i++ {
		Fscan(in, &l, &r)
		for p := l; p <= r; p++ {
			if len(ids[p]) < 3 {
				ids[p] = append(ids[p], i)
			}
		}
	}
	c1 := make([]int, m)
	c2 := map[pair]int{}
	for _, id := range ids {
		if id == nil {
			continue
		}
		if len(id) == 1 {
			c1[id[0]]++
		} else if len(id) == 2 {
			c2[pair{id[0], id[1]}]++
		}
		all++
	}
	min := n
	for i, c := range c1 {
		for j := i + 1; j < m; j++ {
			if c := c + c1[j] + c2[pair{i, j}]; c < min {
				min = c
			}
		}
	}
	Fprint(out, all-min)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1132C(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	type pair struct{ x, y int }

	var n, m, l, r, all int
	Fscan(in, &n, &m)
	ids := make([][]int, n+1)
	for i := 1; i <= m; i++ {
		Fscan(in, &l, &r)
		for p := l; p <= r; p++ {
			if len(ids[p]) < 3 {
				ids[p] = append(ids[p], i)
			}
		}
	}
	cnt := map[pair]int{}
	for _, is := range ids {
		if is == nil {
			continue
		}
		if len(is) == 1 {
			cnt[pair{is[0], 0}]++
		} else if len(is) == 2 {
			cnt[pair{is[0], is[1]}]++
		}
		all++
	}
	min := n
	for i := 1; i < m; i++ {
		for j := i + 1; j <= m; j++ {
			if c := cnt[pair{i, 0}] + cnt[pair{j, 0}] + cnt[pair{i, j}]; c < min {
				min = c
			}
		}
	}
	Fprint(out, all-min)
}

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

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n, q := 0, 0
	fmt.Fscanf(in, "%d %d\n", &n, &q)
	l := make([]int, q)
	r := make([]int, q)
	worker := make([][]int, n)
	for i := 0; i < n; i++ {
		worker[i] = make([]int, 0, q)
	}
	for i := 0; i < q; i++ {
		fmt.Fscanf(in, "%d %d\n", &l[i], &r[i])
		l[i]--
		r[i]--
		for j := l[i]; j <= r[i]; j++ {
			worker[j] = append(worker[j], i)
		}
	}
	unique := make([]int, q)
	pair := make([][]int, q)
	for i := 0; i < q; i++ {
		pair[i] = make([]int, q)
	}
	all := 0
	for i := 0; i < n; i++ {
		if len(worker[i]) > 0 {
			all++
		}
	}
	for i := 0; i < q; i++ {
		for j := l[i]; j <= r[i]; j++ {
			if len(worker[j]) == 1 {
				unique[i]++
			} else if len(worker[j]) == 2 {
				if worker[j][0] == i {
					pair[i][worker[j][1]]++
				} else {
					pair[i][worker[j][0]]++
				}
			}
		}
	}
	min := -1
	for i := 0; i < q; i++ {
		for j := i + 1; j < q; j++ {
			if unique[i]+unique[j]+pair[i][j] < min || min == -1 {
				min = unique[i] + unique[j] + pair[i][j]
			}
		}
	}
	fmt.Fprintf(out, "%d\n", all-min)
	return
}

func gcd(a, b int) int {
	if a < b {
		return gcd(b, a)
	}
	if a%b == 0 {
		return b
	}
	return gcd(b, a%b)
}

func solve(ang int) int {
	_ans := 180 / gcd(ang, 180)
	ans := _ans
	for ans*ang/180 > ans-2 {
		ans = ans + _ans
	}
	return ans
}


You are given a grid, consisting of $$$2$$$ rows and $$$n$$$ columns. Each cell of this grid should be colored either black or white.Two cells are considered neighbours if they have a common border and share the same color. Two cells $$$A$$$ and $$$B$$$ belong to the same component if they are neighbours, or if there is a neighbour of $$$A$$$ that belongs to the same component with $$$B$$$.Let's call some bicoloring beautiful if it has exactly $$$k$$$ components.Count the number of beautiful bicolorings. The number can be big enough, so print the answer modulo $$$998244353$$$.

The only line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 1000$$$, $$$1 \le k \le 2n$$$) — the number of columns in a grid and the number of components required.

Print a single integer — the number of beautiful bicolorings modulo $$$998244353$$$.

NoteOne of possible bicolorings in sample $$$1$$$:

package main

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

var (
	r *bufio.Scanner
	w *bufio.Writer
)

const ISDEBUG = false

func readLine() string {
	r.Scan()
	return r.Text()
}

func readInt() int {
	r.Scan()
	x, _ := strconv.Atoi(r.Text())
	return x
}

func D() {
	n, k := readInt(), readInt()

	var M int64 = 998244353

	c := make([][][]int64, n+1)
	for i := 0; i <= n; i++ {
		c[i] = make([][]int64, 2*n+1)

		for j := 0; j <= 2*n; j++ {
			c[i][j] = make([]int64, 2)
		}
	}

	c[1][1][0] = 2
	c[1][2][1] = 2

	for i := 2; i <= n; i++ {
		for j := 1; j <= 2*i; j++ {
			c[i][j][0] = (c[i-1][j][0] + (c[i-1][j][1]*2)%M + c[i-1][j-1][0]) % M
			c[i][j][1] = (c[i-1][j][1] + (c[i-1][j-1][0]*2)%M) % M
			if j >= 2 {
				c[i][j][1] = (c[i][j][1] + c[i-1][j-2][1]) % M
			}
		}
	}

	var result int64 = 0
	for i := 0; i < 2; i++ {
		result = (result + c[n][k][i]) % M
	}
	w.WriteString(fmt.Sprintf("%v\n", result))
}

func main() {
	if ISDEBUG {
		fi, err := os.Open("input.txt")
		if err != nil {
			panic(err)
		}
		fo, _ := os.OpenFile("output.txt", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
		r = bufio.NewScanner(fi)
		w = bufio.NewWriter(fo)

		defer fi.Close()
		defer fo.Close()
	} else {
		r = bufio.NewScanner(os.Stdin)
		w = bufio.NewWriter(os.Stdout)
	}
	r.Split(bufio.ScanWords)
	defer w.Flush()

	D()
}

package main

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

var (
	r *bufio.Scanner
	w *bufio.Writer
)

const ISDEBUG = false

func readLine() string {
	r.Scan()
	return r.Text()
}

func readInt() int {
	r.Scan()
	x, _ := strconv.Atoi(r.Text())
	return x
}

func D() {
	n, k := readInt(), readInt()

	M := 998244353

	c := make([][][]int, n+1)
	for i := 0; i <= n; i++ {
		c[i] = make([][]int, 2*n+1)

		for j := 0; j <= 2*n; j++ {
			c[i][j] = make([]int, 2)
		}
	}

	c[1][1][0] = 2
	c[1][2][1] = 2

	for i := 2; i <= n; i++ {
		for j := 1; j <= 2*i; j++ {
			c[i][j][0] = (c[i-1][j][0] + (c[i-1][j][1]*2)%M + c[i-1][j-1][0]) % M
			c[i][j][1] = (c[i-1][j][1] + (c[i-1][j-1][0]*2)%M) % M
			if j >= 2 {
				c[i][j][1] = (c[i][j][1] + c[i-1][j-2][1]) % M
			}
		}
	}

	result := 0
	for i := 0; i < 2; i++ {
		result = (result + c[n][k][i]) % M
	}
	w.WriteString(fmt.Sprintf("%v\n", result))
}

func main() {
	if ISDEBUG {
		fi, err := os.Open("input.txt")
		if err != nil {
			panic(err)
		}
		fo, _ := os.OpenFile("output.txt", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
		r = bufio.NewScanner(fi)
		w = bufio.NewWriter(fo)

		defer fi.Close()
		defer fo.Close()
	} else {
		r = bufio.NewScanner(os.Stdin)
		w = bufio.NewWriter(os.Stdout)
	}
	r.Split(bufio.ScanWords)
	defer w.Flush()

	D()
}

package main

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

var (
	r *bufio.Scanner
	w *bufio.Writer
)

const ISDEBUG = false

func readLine() string {
	r.Scan()
	return r.Text()
}

func readInt() int {
	r.Scan()
	x, _ := strconv.Atoi(r.Text())
	return x
}

func D() {
	n, k := readInt(), readInt()

	M := 998244353

	c := make([][][]int, n+1)
	for i := 0; i <= n; i++ {
		c[i] = make([][]int, 2*n+1)

		for j := 0; j <= 2*n; j++ {
			c[i][j] = make([]int, 2)
		}
	}

	c[1][1][0] = 2
	c[1][2][1] = 2

	for i := 2; i <= n; i++ {
		for j := 1; j <= 2*i; j++ {
			c[i][j][0] = (c[i-1][j][0] + c[i-1][j][1]*2 + c[i-1][j-1][0]) % M
			c[i][j][1] = (c[i-1][j][1] + c[i-1][j-1][0]*2) % M
			if j >= 2 {
				c[i][j][1] = (c[i][j][1] + c[i-1][j-2][1]) % M
			}
		}
	}

	result := 0
	for i := 0; i < 2; i++ {
		result = (result + c[n][k][i]) % M
	}
	w.WriteString(fmt.Sprintf("%v\n", result))
}

func main() {
	if ISDEBUG {
		fi, err := os.Open("input.txt")
		if err != nil {
			panic(err)
		}
		fo, _ := os.OpenFile("output.txt", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
		r = bufio.NewScanner(fi)
		w = bufio.NewWriter(fo)

		defer fi.Close()
		defer fo.Close()
	} else {
		r = bufio.NewScanner(os.Stdin)
		w = bufio.NewWriter(os.Stdout)
	}
	r.Split(bufio.ScanWords)
	defer w.Flush()

	D()
}


You are given an array $$$a[0 \ldots n - 1] = [a_0, a_1, \ldots, a_{n - 1}]$$$ of zeroes and ones only. Note that in this problem, unlike the others, the array indexes are numbered from zero, not from one.In one step, the array $$$a$$$ is replaced by another array of length $$$n$$$ according to the following rules:   First, a new array $$$a^{\rightarrow d}$$$ is defined as a cyclic shift of the array $$$a$$$ to the right by $$$d$$$ cells. The elements of this array can be defined as $$$a^{\rightarrow d}_i = a_{(i + n - d) \bmod n}$$$, where $$$(i + n - d) \bmod n$$$ is the remainder of integer division of $$$i + n - d$$$ by $$$n$$$.  It means that the whole array $$$a^{\rightarrow d}$$$ can be represented as a sequence $$$$$$a^{\rightarrow d} = [a_{n - d}, a_{n - d + 1}, \ldots, a_{n - 1}, a_0, a_1, \ldots, a_{n - d - 1}]$$$$$$  Then each element of the array $$$a_i$$$ is replaced by $$$a_i \,\&\, a^{\rightarrow d}_i$$$, where $$$\&$$$ is a logical "AND" operator. For example, if $$$a = [0, 0, 1, 1]$$$ and $$$d = 1$$$, then $$$a^{\rightarrow d} = [1, 0, 0, 1]$$$ and the value of $$$a$$$ after the first step will be $$$[0 \,\&\, 1, 0 \,\&\, 0, 1 \,\&\, 0, 1 \,\&\, 1]$$$, that is $$$[0, 0, 0, 1]$$$.The process ends when the array stops changing. For a given array $$$a$$$, determine whether it will consist of only zeros at the end of the process. If yes, also find the number of steps the process will take before it finishes.

The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases.  The first line of each test case description contains two integers: $$$n$$$ ($$$1 \le n \le 10^6$$$) — array size and $$$d$$$ ($$$1 \le d \le n$$$) — cyclic shift offset. The second line of the description contains $$$n$$$ space-separated integers $$$a_i$$$ ($$$0 \le a_i \le 1$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$.

Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer — the number of steps after which the array will contain only zeros for the first time. If there are still elements equal to $$$1$$$ in the array after the end of the process, print -1.

NoteIn the third sample test case the array will change as follows:   At the beginning $$$a = [1, 1, 0, 1, 0]$$$, and $$$a^{\rightarrow 2} = [1, 0, 1, 1, 0]$$$. Their element-by-element "AND" is equal to $$$$$$[1 \,\&\, 1, 1 \,\&\, 0, 0 \,\&\, 1, 1 \,\&\, 1, 0 \,\&\, 0] = [1, 0, 0, 1, 0]$$$$$$  Now $$$a = [1, 0, 0, 1, 0]$$$, then $$$a^{\rightarrow 2} = [1, 0, 1, 0, 0]$$$. Their element-by-element "AND" equals to $$$$$$[1 \,\&\, 1, 0 \,\&\, 0, 0 \,\&\, 1, 1 \,\&\, 0, 0 \,\&\, 0] = [1, 0, 0, 0, 0]$$$$$$  And finally, when $$$a = [1, 0, 0, 0, 0]$$$ we get $$$a^{\rightarrow 2} = [0, 0, 1, 0, 0]$$$. Their element-by-element "AND" equals to $$$$$$[1 \,\&\, 0, 0 \,\&\, 0, 0 \,\&\, 1, 0 \,\&\, 0, 0 \,\&\, 0] = [0, 0, 0, 0, 0]$$$$$$  Thus, the answer is $$$3$$$ steps.In the fourth sample test case, the array will not change as it shifts by $$$2$$$ to the right, so each element will be calculated as $$$0 \,\&\, 0$$$ or $$$1 \,\&\, 1$$$ thus not changing its value. So the answer is -1, the array will never contain only zeros.

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1579F(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var T, n, d, v int
o:
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &d)
		q := []int{}
		vis := make([]bool, n)
		for i := 0; i < n; i++ {
			if Fscan(in, &v); v == 0 {
				q = append(q, i)
				vis[i] = true
			}
		}
		ans := -1
		for ; len(q) > 0; ans++ {
			tmp := q
			q = nil
			for _, v := range tmp {
				if w := (v + d) % n; !vis[w] {
					q = append(q, w)
					vis[w] = true
				}
			}
		}
		for _, v := range vis {
			if !v {
				Fprintln(out, -1)
				continue o
			}
		}
		Fprintln(out, ans)
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1579F(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var T, n, d int
	var v bool
o:
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &d)
		q := []int{}
		vis := make([]bool, n)
		for i := 0; i < n; i++ {
			if Fscan(in, &v); !v {
				q = append(q, i)
				vis[i] = true
			}
		}
		ans := -1
		for ; len(q) > 0; ans++ {
			tmp := q
			q = nil
			for _, v := range tmp {
				if w := (v + d) % n; !vis[w] {
					q = append(q, w)
					vis[w] = true
				}
			}
		}
		for _, v := range vis {
			if !v {
				Fprintln(out, -1)
				continue o
			}
		}
		Fprintln(out, ans)
	}
}

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

package main

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

var n, d int

var a []int

var writer *bufio.Writer

func main() {

	read := bufio.NewReader(os.Stdin)
	writer = bufio.NewWriter(os.Stdout)

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

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

		a = make([]int, n)

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

		fmt.Println(solve())
	}
}

type pair struct {
	v int
	idx 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] }

var step []int
var seen []bool

func solve () int {
	//a^[i] = a[(i + n - d) % n]
	//i = (i + d) % n

	//1 1 0 1 0 0 1 0 0 1 0 0 1 1 0 0

	step = make([]int, n)
	seen = make([]bool, n)

	for i := 0; i < n; i++ {
		step[i] = -1
	}

	for i := n - 1; i >= 0; i-- {
		if a[i] == 1 {
			if seen[i] {
				continue
			}
			s := rec(i)

			if s == -1 {
				return -1
			}
		}
	}

	var max int

	for i := 0; i < n; i++ {
		if step[i] > max {
			max = step[i]
		}
	}

	return max
}

func rec(cur int) int {
	if seen[cur] {
		return -1
	}
	seen[cur] = true
	prevIdx := (cur - d + n) % n

	if a[prevIdx] == 1 {
		if step[prevIdx] != -1 {
			s := step[prevIdx]

			step[cur] = s + 1

			return s + 2
		}

		s := rec(prevIdx)

		if s == -1 {
			return -1
		}
		step[cur] = s

		return s + 1

	} else {
		step[cur] = 1

		return 2
	}
}


Pink Floyd are pulling a prank on Roger Waters. They know he doesn't like walls, he wants to be able to walk freely, so they are blocking him from exiting his room which can be seen as a grid.Roger Waters has a square grid of size $$$n\times n$$$ and he wants to traverse his grid from the upper left ($$$1,1$$$) corner to the lower right corner ($$$n,n$$$). Waters can move from a square to any other square adjacent by a side, as long as he is still in the grid. Also except for the cells ($$$1,1$$$) and ($$$n,n$$$) every cell has a value $$$0$$$ or $$$1$$$ in it.Before starting his traversal he will pick either a $$$0$$$ or a $$$1$$$ and will be able to only go to cells values in which are equal to the digit he chose. The starting and finishing cells ($$$1,1$$$) and ($$$n,n$$$) are exempt from this rule, he may go through them regardless of picked digit. Because of this the cell ($$$1,1$$$) takes value the letter 'S' and the cell ($$$n,n$$$) takes value the letter 'F'.For example, in the first example test case, he can go from ($$$1, 1$$$) to ($$$n, n$$$) by using the zeroes on this path: ($$$1, 1$$$), ($$$2, 1$$$), ($$$2, 2$$$), ($$$2, 3$$$), ($$$3, 3$$$), ($$$3, 4$$$), ($$$4, 4$$$)The rest of the band (Pink Floyd) wants Waters to not be able to do his traversal, so while he is not looking they will invert at most two cells in the grid (from $$$0$$$ to $$$1$$$ or vice versa). They are afraid they will not be quick enough and asked for your help in choosing the cells.  Note that you cannot invert cells $$$(1, 1)$$$ and $$$(n, n)$$$.We can show that there always exists a solution for the given constraints.Also note that Waters will pick his digit of the traversal after the band has changed his grid, so he must not be able to reach ($$$n,n$$$) no matter what digit he picks.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 50$$$). Description of the test cases follows. The first line of each test case contains one integers $$$n$$$ ($$$3 \le n \le 200$$$). The following $$$n$$$ lines of each test case contain the binary grid, square ($$$1, 1$$$) being colored in 'S' and square ($$$n, n$$$) being colored in 'F'. The sum of values of $$$n$$$ doesn't exceed $$$200$$$.

For each test case output on the first line an integer $$$c$$$ ($$$0 \le c \le 2$$$)  — the number of inverted cells. In $$$i$$$-th of the following $$$c$$$ lines, print the coordinates of the $$$i$$$-th cell you inverted. You may not invert the same cell twice.  Note that you cannot invert cells $$$(1, 1)$$$ and $$$(n, n)$$$.

NoteFor the first test case, after inverting the cell, we get the following grid:S01000011001111F

package main

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

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

	tc := readNum(reader)
	var buf bytes.Buffer
	for tc > 0 {
		tc--
		n := readNum(reader)
		G := make([]string, n)
		for i := 0; i < n; i++ {
			G[i], _ = reader.ReadString('\n')
		}
		res := solve(n, G)
		buf.WriteString(fmt.Sprintf("%d\n", len(res)))
		for i := 0; i < len(res); i++ {
			buf.WriteString(fmt.Sprintf("%d %d\n", res[i][0], res[i][1]))
		}
	}
	fmt.Print(buf.String())
}

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

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

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

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

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

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

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

	return i
}

func solve(n int, G []string) [][]int {
	if G[0][1] == G[1][0] {
		// same top-left
		if G[n-1][n-2] == G[n-2][n-1] {
			if G[0][1] != G[n-1][n-2] {
				return nil
			}
			return [][]int{{n - 2 + 1, n - 1 + 1}, {n - 1 + 1, n - 2 + 1}}
		}
		if G[n-1][n-2] == G[0][1] {
			return [][]int{{n - 1 + 1, n - 2 + 1}}
		}
		return [][]int{{n - 2 + 1, n - 1 + 1}}
	}
	if G[n-1][n-2] == G[n-2][n-1] {
		if G[0][1] == G[n-1][n-2] {
			return [][]int{{1, 2}}
		}
		return [][]int{{2, 1}}
	}

	// both different
	//01  10
	if G[0][1] == G[n-1][n-2] {
		return [][]int{{1, 2}, {n - 1, n}}
	}
	return [][]int{{1, 2}, {n, n - 1}}
}

package main

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

var _r = bufio.NewReader(os.Stdin)
var _w = bufio.NewWriter(os.Stdout)

var T, n int

func main() {
	for _, _ = fmt.Fscan(_r, &T); T > 0; T-- {
		_, _ = fmt.Fscan(_r, &n)
		pic := make([]string, n)

		for i := 0; i < n; i++ {
			_, _ = fmt.Fscan(_r, &pic[i])
		}

		s1 := string(pic[0][1]) + string(pic[1][0])
		s2 := string(pic[n-1][n-2]) + string(pic[n-2][n-1])

		if s1[0] == s1[1] && s2[0] == s2[1] && s1[0] == s2[0] {
			_, _ = fmt.Fprintln(_w, 2)
			_, _ = fmt.Fprintln(_w, 1, 2)
			_, _ = fmt.Fprintln(_w, 2, 1)
			_ = _w.Flush()
			continue
		}

		if s1[0] == s1[1] && s2[0] == s2[1] {
			_, _ = fmt.Fprintln(_w, 0)
			_ = _w.Flush()
			continue
		}

		if (s1[0] != s1[1]) && (s2[0] != s2[1]) {
			_, _ = fmt.Fprintln(_w, 2)
			if s1[0] == '0' {
				_, _ = fmt.Fprintln(_w, 1, 2)
			} else {
				_, _ = fmt.Fprintln(_w, 2, 1)
			}
			if s2[0] == '1' {
				_, _ = fmt.Fprintln(_w, n, n-1)
			} else {
				_, _ = fmt.Fprintln(_w, n-1, n)
			}
			_ = _w.Flush()
			continue
		}

		if s1[0] == s1[1] {
			_, _ = fmt.Fprintln(_w, 1)
			if s2[0] == s1[0] {
				_, _ = fmt.Fprintln(_w, n, n-1)
			} else {
				_, _ = fmt.Fprintln(_w, n-1, n)
			}
			_ = _w.Flush()
			continue
		}

		if s2[0] == s2[1] {
			_, _ = fmt.Fprintln(_w, 1)
			if s1[0] == s2[0] {
				_, _ = fmt.Fprintln(_w, 1, 2)
			} else {
				_, _ = fmt.Fprintln(_w, 2, 1)
			}
			_ = _w.Flush()
			continue
		}

	}
}

package main

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

func main() {
	defer _w.Flush()
	t := ReadInt()
	for i := 0; i < t; i++ {
		n := ReadInt()
		grid := make([][]byte, n)
		for i := 0; i < n; i++ {
			grid[i] = []byte(ReadString())
		}
		solve(grid)
	}
}

func solve(grid [][]byte) {
	n := len(grid)
	cands := [][2]int{
		{0, 1}, {1, 0}, {n - 1, n - 2}, {n - 2, n - 1},
	}
	if isAns(grid) {
		println(0)
		return
	}
	var x, y int
	for i := 0; i < 3; i++ {
		for j := i + 1; j < 4; j++ {
			x, y = cands[i][0], cands[i][1]
			grid[y][x] = invert(grid[y][x])
			x, y = cands[j][0], cands[j][1]
			grid[y][x] = invert(grid[y][x])
			if isAns(grid) {
				println(2)
				println(cands[i][1]+1, cands[i][0]+1)
				println(cands[j][1]+1, cands[j][0]+1)
				return
			}
			x, y = cands[i][0], cands[i][1]
			grid[y][x] = invert(grid[y][x])
			x, y = cands[j][0], cands[j][1]
			grid[y][x] = invert(grid[y][x])
		}
	}
	for i := 0; i < 4; i++ {
		x, y = cands[i][0], cands[i][1]
		grid[y][x] = invert(grid[y][x])
		if isAns(grid) {
			println(1)
			println(cands[i][1]+1, cands[i][0]+1)
			return
		}
		x, y = cands[i][0], cands[i][1]
		grid[y][x] = invert(grid[y][x])
	}
	println(0)
}

func invert(c byte) byte {
	if c == '0' {
		return '1'
	}
	return '0'
}

// 0でも1でも到達できないならtrue
func isAns(grid [][]byte) bool {
	n := len(grid)
	{
		visited := make([][]bool, n)
		for i := 0; i < n; i++ {
			visited[i] = make([]bool, n)
		}
		if dfs(1, 0, grid, grid[0][1], visited) {
			return false
		}
	}
	{
		visited := make([][]bool, n)
		for i := 0; i < n; i++ {
			visited[i] = make([]bool, n)
		}
		if dfs(0, 1, grid, grid[1][0], visited) {
			return false
		}
	}
	return true
}

var dx = []int{1, 0, -1, 0}
var dy = []int{0, 1, 0, -1}

// ゴールまで行けたらtrue
func dfs(x, y int, grid [][]byte, sel byte, visited [][]bool) bool {
	n := len(grid)
	visited[y][x] = true
	for i := 0; i < 4; i++ {
		nx, ny := x+dx[i], y+dy[i]
		if 0 <= nx && nx < n && 0 <= ny && ny < n {
			if nx == n-1 && ny == n-1 {
				return true
			}
			if grid[ny][nx] == sel && !visited[ny][nx] {
				if dfs(nx, ny, grid, sel, visited) {
					return true
				}
			}
		}
	}
	return false
}

var _r, _w = bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)

func print(a ...interface{})            { fmt.Fprint(_w, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(_w, f, a...) }
func println(a ...interface{})          { fmt.Fprintln(_w, a...) }
func Scan(a ...interface{}) {
	if _, err := fmt.Fscan(_r, a...); err != nil {
		panic(err)
	}
}
func ReadString() (s string) { Scan(&s); return }
func ReadInt() (i int)       { Scan(&i); return }
func ReadInts(n int) (a []int) {
	for i := 0; i < n; i++ {
		a = append(a, ReadInt())
	}
	return
}
func ReadInt64() (i int64) { Scan(&i); return }
func ReadInt64s(n int) (a []int64) {
	for i := 0; i < n; i++ {
		a = append(a, ReadInt64())
	}
	return
}

package main

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

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

	var T, n int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]string, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		ans := [][2]int{}
		if a[0][1] == a[1][0] {
			if a[n-2][n-1] == a[0][1] {
				ans = append(ans, [2]int{n - 2, n - 1})
			}
			if a[n-1][n-2] == a[0][1] {
				ans = append(ans, [2]int{n - 1, n - 2})
			}
		} else if a[n-2][n-1] == a[n-1][n-2] {
			if a[n-2][n-1] == a[0][1] {
				ans = append(ans, [2]int{0, 1})
			}
			if a[n-2][n-1] == a[1][0] {
				ans = append(ans, [2]int{1, 0})
			}
		} else {
			ans = append(ans, [2]int{0, 1})
			if a[n-2][n-1] == a[1][0] {
				ans = append(ans, [2]int{n - 2, n - 1})
			} else {
				ans = append(ans, [2]int{n - 1, n - 2})
			}
		}
		Fprintln(out, len(ans))
		for _, p := range ans {
			Fprintln(out, p[0]+1, p[1]+1)
		}
	}
}

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


You have an array of integers $$$a$$$ of size $$$n$$$. Initially, all elements of the array are equal to $$$1$$$. You can perform the following operation: choose two integers $$$i$$$ ($$$1 \le i \le n$$$) and $$$x$$$ ($$$x > 0$$$), and then increase the value of $$$a_i$$$ by $$$\left\lfloor\frac{a_i}{x}\right\rfloor$$$ (i.e. make $$$a_i = a_i + \left\lfloor\frac{a_i}{x}\right\rfloor$$$).After performing all operations, you will receive $$$c_i$$$ coins for all such $$$i$$$ that $$$a_i = b_i$$$.Your task is to determine the maximum number of coins that you can receive by performing no more than $$$k$$$ operations.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10^3; 0 \le k \le 10^6$$$) — the size of the array and the maximum number of operations, respectively. The second line contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le 10^3$$$). The third line contains $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \le c_i \le 10^6$$$). The sum of $$$n$$$ over all test cases does not exceed $$$10^3$$$.

For each test case, print one integer — the maximum number of coins that you can get by performing no more than $$$k$$$ operations.

package main

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

var n, k int

var b[]int
var c[]int64

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

	preprocess()

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

		b = make([]int, n)
		c = make([]int64, n)

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

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

		//st := time.Now().UnixNano()
		ans := solve()
		//en := time.Now().UnixNano()
//
		//fmt.Println((en - st)/1000000)

		fmt.Println(ans)
	}
}

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
var cost []int

const INF = int(1e9) + 5;

func preprocess() {
	cost = make([]int, 2001)

	for i := 0; i < 2001; i++ {
		cost[i] = INF
	}

	cost[1] = 0

	for a := 1; a <= 1e3; a++ {
		for x := 1; x <= 1e3; x++ {
			b := a + a/x
			cost[b] = min(cost[b], cost[a]+1);
		}
	}
}

func solve() int64 {

	ops := make([]int, n)

	var minCost int = 1e9

	for i := 0; i < n; i++ {
		ops[i] = cost[b[i]]
		if cost[b[i]] < minCost {
			minCost = cost[b[i]]
		}
	}

	dp := make([][]int64, 2)
	for i := 0; i < 2; i++ {
		dp[i] = make([]int64, k + 1)
	}
	for i := 0; i < 2; i++ {
		dp[i][0] = 0
	}
	for i := 0; i <= k; i++ {
		if ops[0] > i {
			dp[0][i] = 0

		} else {
			dp[0][i] = c[0]
		}
	}

	k = min(k, 12 * n)

	for i := 1; i < n; i++ {

		cur := i % 2
		var prev int
		if cur == 1 {
			prev = 0
		} else {
			prev = 1
		}

		for j := 0; j <= k; j++ {

			if ops[i] > j {
				dp[cur][j] = dp[prev][j]

			} else {
				dp[cur][j] = max(dp[prev][j], dp[prev][j - ops[i]] + c[i])
			}
		}
	}

	var max int64

	for i := 0; i <= k; i++ {
		if dp[0][i] > max {
			max = dp[0][i]
		}
		if dp[1][i] > max {
			max = dp[1][i]
		}
	}

	return max
}

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

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"
	"container/list"
	"fmt"
	"math"
	"os"
)

/*

 */

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

	b := make([]int, n+1)
	for i := 1; i < len(b); i++ {
		scanf("%d", &b[i])
	}
	scanf("\n")

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

	for i := 1; i < len(b); i++ {
		scanf("%d", &v[i])
	}
	scanf("\n")

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

	{
		bToIdcs := make(map[int][]int)
		for i := 1; i < len(b); i++ {
			bToIdcs[b[i]] = append(bToIdcs[b[i]], i)
		}
		maxB := max(b...)

		type vtx struct {
			v, d int
		}

		q := list.New()
		q.PushBack(vtx{1, 0})

		used := make(map[int]bool)

		for q.Len() > 0 && len(bToIdcs) > 0 {
			top := q.Remove(q.Front()).(vtx)

			if idcs, ok := bToIdcs[top.v]; ok {
				for _, idx := range idcs {
					w[idx] = top.d
				}
				delete(bToIdcs, top.v)
			}

			for j := 1; j <= min(top.v, maxB); j++ {
				u := top.v + top.v/j
				if _, ok := used[u]; !ok {
					q.PushBack(vtx{u, top.d + 1})
					used[u] = true
				}
				if u == top.v+1 {
					break
				}
			}
		}
	}

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

	for i := range dp {
		dp[i] = make([]int, min(k, 12*n)+1)
		if i == 0 {
			continue
		}

		for j := 0; j < len(dp[i]); j++ {
			if j >= w[i] {
				dp[i][j] = max(dp[i-1][j], v[i]+dp[i-1][j-w[i]])
			} else {
				dp[i][j] = dp[i-1][j]
			}
		}
	}

	ans := dp[len(dp)-1][len(dp[0])-1]

	fmt.Fprintln(writer, ans)
}

//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 abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

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

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

	for i := 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 findSet(parent []int, v int) int {
	if v == parent[v] {
		return v
	}

	parent[v] = findSet(parent, parent[v])

	return parent[v]
}

//goland:noinspection GoUnusedFunction
func unionSets(parent []int, v, u int) {
	v = findSet(parent, v)
	u = findSet(parent, u)

	if v != u {
		parent[v] = u
	}
}

package main

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

var (
	in     = bufio.NewReader(os.Stdin)
	dpCost []int
)

func calMaxCoin(cur, k int, dp [][]int, coins, costs []int) int {
	if cur >= len(dp) {
		return 0
	}
	if dp[cur][k] != -1 {
		return dp[cur][k]
	}

	result := 0
	if k >= costs[cur] {
		result = calMaxCoin(cur+1, k-costs[cur], dp, coins, costs) + coins[cur]
	}
	if notUse := calMaxCoin(cur+1, k, dp, coins, costs); notUse > result {
		result = notUse
	}

	dp[cur][k] = result
	return result
}

func solve() {
	var n, k int
	fmt.Fscan(in, &n)
	fmt.Fscan(in, &k)

	b := make([]int, n)
	for i := range b {
		fmt.Fscan(in, &b[i])
	}
	coins := make([]int, n)
	for i := range coins {
		fmt.Fscan(in, &coins[i])
	}

	costs := make([]int, n)
	costMax := 0
	for i, bi := range b {
		costs[i] = dpCost[bi]
		costMax += costs[i]
	}
	if costMax < k {
		k = costMax
	}

	dp := make([][]int, n)
	for i := range dp {
		dp[i] = make([]int, k+1)
		for j := range dp[i] {
			dp[i][j] = -1
		}
	}
	fmt.Println(calMaxCoin(0, k, dp, coins, costs))
}

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

func main() {
	dpCost = make([]int, 1001)
	for i := range dpCost {
		dpCost[i] = 1001
	}
	dpCost[1] = 0
	for i := 1; i < len(dpCost); i++ {
		for x := i; x >= 1; x-- {
			j := i + i/x
			if j >= len(dpCost) {
				continue
			}
			dpCost[j] = min(dpCost[j], dpCost[i]+1)
		}
	}

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

	for i := 0; i < t; i++ {
		solve()
	}
}

package main

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

var (
	in     = bufio.NewReader(os.Stdin)
	dpCost []int
)

func calCostV(cur int, dpCostV []int, v int) int {
	if dpCostV[cur] != -1 {
		return dpCostV[cur]
	}

	if cur == v {
		return 0
	}
	if cur > v {
		panic("impossible")
	}
	result := 1001
	for x := cur; x >= 1; x-- {
		next := cur + cur/x
		if next > v {
			break
		}
		candi := calCostV(next, dpCostV, v) + 1
		if candi < result {
			result = candi
		}
	}
	if result == 1001 {
		panic("impossible")
	}
	dpCostV[cur] = result
	return dpCostV[cur]
}

func getCost(v int) int {
	if dpCost[v] != -1 {
		return dpCost[v]
	}

	dpCostV := make([]int, v+1)
	for i := range dpCostV {
		dpCostV[i] = -1
	}
	dpCost[v] = calCostV(1, dpCostV, v)
	return dpCost[v]
}

func calMaxCoin(cur, k int, dp [][]int, coins, costs []int) int {
	if cur >= len(dp) {
		return 0
	}
	if dp[cur][k] != -1 {
		return dp[cur][k]
	}

	result := 0
	if k >= costs[cur] {
		result = calMaxCoin(cur+1, k-costs[cur], dp, coins, costs) + coins[cur]
	}
	if notUse := calMaxCoin(cur+1, k, dp, coins, costs); notUse > result {
		result = notUse
	}

	dp[cur][k] = result
	return result
}

func solve() {
	var n, k int
	fmt.Fscan(in, &n)
	fmt.Fscan(in, &k)

	b := make([]int, n)
	for i := range b {
		fmt.Fscan(in, &b[i])
	}
	coins := make([]int, n)
	for i := range coins {
		fmt.Fscan(in, &coins[i])
	}

	costs := make([]int, n)
	costMax := 0
	for i, bi := range b {
		costs[i] = getCost(bi)
		costMax += costs[i]
	}
	if costMax < k {
		k = costMax
	}

	dp := make([][]int, n)
	for i := range dp {
		dp[i] = make([]int, k+1)
		for j := range dp[i] {
			dp[i][j] = -1
		}
	}
	fmt.Println(calMaxCoin(0, k, dp, coins, costs))
}

func main() {
	dpCost = make([]int, 1001)
	for i := range dpCost {
		dpCost[i] = -1
	}

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

	for i := 0; i < t; i++ {
		solve()
	}
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)
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 main() {
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	var t int
	fmt.Fscan(in,&t)
	a := [1005]int{}
	a[1]=0
	for i:=2;i<=1000;i++{
		a[i]=math.MaxInt32
	}
	for i:=1;i<=1000;i++{
		for j:=1;j<=i;j++{
			nex := i+i/j
			if nex<=1000 {
				a[nex] = min(a[nex], a[i]+1)
			}
		}
	}
	for ct:=1;ct<=t;ct++{
		var n, k int
		fmt.Fscan(in,&n,&k)
		b := make([]int,1005)
		c := make([]int,1005)
		sum:=0
		for i:=1;i<=n;i++{
			fmt.Fscan(in,&b[i])
			b[i] = a[b[i]]
			sum+=b[i]
		}
		for i:=1;i<=n;i++{
			fmt.Fscan(in,&c[i])
		}
		f := [1005][12500]int{}
		f[0][0]=0
		for i:=1;i<=min(sum,k);i++{
			f[0][i]=-999999999
		}
		ans:=0
		for i:=1;i<=n;i++{
			for j:=0;j<=min(sum,k);j++{
				if j-b[i]>=0{
					f[i][j] = max(f[i][j], f[i-1][j-b[i]]+c[i])
				}
				f[i][j]=max(f[i][j],f[i-1][j])
				ans = max(ans,f[i][j])
			}
		}
		fmt.Fprintln(out,ans)
	}
}

package main

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

var (
	DistanceStepsMap []int
)

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readInt64(in *bufio.Reader) uint64 {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.ParseUint(nStr, 10, 64)
	return n
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")

	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

func readArrInt64(in *bufio.Reader) []int64 {
	numbs := readLineNumbs(in)
	arr := make([]int64, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.ParseInt(n, 10, 64)
		arr[i] = val
	}
	return arr
}

func readString(in *bufio.Reader) string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")

	return line
}

func StringToRuneSlice(s string) []rune {
	var r []rune
	for _, runeValue := range s {
		r = append(r, runeValue)
	}
	return r
}

func initMap() {
	DistanceStepsMap = make([]int, 2005)
	for i := range DistanceStepsMap {
		DistanceStepsMap[i] = math.MaxInt
	}

	DistanceStepsMap[1] = 0

	for i := 1; i <= 1000; i++ {
		for j := 1; j <= i; j++ {
			if DistanceStepsMap[i+i/j] > DistanceStepsMap[i]+1 {
				DistanceStepsMap[i+i/j] = DistanceStepsMap[i] + 1
			}
		}
	}
}

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

	return b
}

func findMinSteps(targetNumber int) int {
	return DistanceStepsMap[targetNumber]
}

func dfs(index int, opsLeft int, cache *[][]int) int {
	if index >= size {
		return 0
	}

	if (*cache)[index][opsLeft] != -1 {
		return (*cache)[index][opsLeft]
	}

	res := 0
	if DistanceStepsMap[b[index]] <= opsLeft {
		res = Max(dfs(index+1, opsLeft-DistanceStepsMap[b[index]], cache) + c[index], dfs(index+1, opsLeft, cache))
	} else {
		res = dfs(index + 1, opsLeft, cache)
	}

	(*cache)[index][opsLeft] = res

	return res
}

func solve(maxOps int) {
	maxStep := 0
	maxRes := 0
	for i := 0; i < size; i++ {
		maxStep += DistanceStepsMap[b[i]]
		maxRes += c[i]
	}

	if maxStep < maxOps {
		fmt.Println(maxRes)

		return
	}

	var cache [][]int = make([][]int, 1005)

	for i := 0; i < 1005; i++ {
		cache[i] = make([]int, maxOps + 2)

		for j := 0; j < maxOps + 2; j++ {
			cache[i][j] = -1
		}
	}

	fmt.Println(dfs(0, maxOps, &cache))
}

var (
	b    []int
	c    []int
	size int
)

func main() {
	initMap()

	in := bufio.NewReader(os.Stdin)

	tc := readInt(in)

	for i := 0; i < tc; i++ {
		f := readArrInt(in)
		size = f[0]
		maxOp := f[1]

		b = readArrInt(in)
		c = readArrInt(in)

		solve(maxOp)
	}

	//solve(1)

	//for i := 0; i < tc; i++ {
	//	n := readInt(in)
	//	_ = readArrInt(in)
	//	fmt.Println("N: ", n)
	//	fmt.Println("DONE TC: ", i+1)
	//}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1633D(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	d := [1001]int{}
	for i := 2; i < 1001; i++ { d[i] = 1e9 }
	for i := 1; i < 1000; i++ {
		for x := 1; x <= i; x++ {
			if i+i/x < 1001 {
				d[i+i/x] = min(d[i+i/x], d[i]+1)
			}
		}
	}

	var T, n, k int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k)
		m := 0
		b := make([]int, n)
		for i := range b {
			Fscan(in, &b[i])
			b[i] = d[b[i]]
			m += b[i]
		}
		m = min(m, k)
		c := make([]int, n)
		for i := range c {
			Fscan(in, &c[i])
		}
		f := make([]int, m+1)
		for i, v := range c {
			w := b[i]
			for j := m; j >= w; j-- {
				f[j] = max(f[j], f[j-w]+v)
			}
		}
		Fprintln(out, f[m])
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1633D(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	d := [1001]int{}
	for i := range d {
		d[i] = 1e9
	}
	d[1] = 0
	q := []int{1}
	for len(q) > 0 {
		v := q[0]
		q = q[1:]
		for x := v; x > 0; x-- {
			w := v + v/x
			if w > 1000 {
				break
			}
			if d[v]+1 < d[w] {
				d[w] = d[v] + 1
				q = append(q, w)
			}
		}
	}

	var T, n, k int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k)
		m := 0
		b := make([]int, n)
		for i := range b {
			Fscan(in, &b[i])
			b[i] = d[b[i]]
			m += b[i]
		}
		if m > k {
			m = k
		}
		c := make([]int, n)
		for i := range c {
			Fscan(in, &c[i])
		}
		f := make([]int, m+1)
		for i, v := range c {
			w := b[i]
			for j := m; j >= w; j-- {
				f[j] = max(f[j], f[j-w]+v)
			}
		}
		Fprintln(out, f[m])
	}
}

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

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

package main

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

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

func CF1633D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	costs := [1005]int{}
	for i := 1; i < 1005; i++ {
		costs[i] = 1e3
	}
	costs[1] = 0
	//暴力计算1->i所需的最短次数
	for i := 1; i <= 1000; i++ {
		for j := 1; j <= i; j++ {
			if i+i/j <= 1000 {
				costs[i+i/j] = min(costs[i+i/j], costs[i]+1)
			}
		}
	}

	cases := 0
	fmt.Fscan(in, &cases)
	for c := 0; c < cases; c++ {
		n, k := 0, 0
		fmt.Fscan(in, &n, &k)
		k = min(12*n, k) //一个数从1->1000最小步数最多12次，所以背包容量取min
		goods, values := make([]int, n), make([]int, n)
		for i := range goods {
			fmt.Fscan(in, &goods[i])
		}
		for i := range values {
			fmt.Fscan(in, &values[i])
		}
		//创建dp数组
		dp := make([]int, k+1)
		for i := range dp {
			dp[i] = math.MinInt32
		}
		dp[0] = 0
		ans := 0
		//0-1bag question
		for i := range goods {
			c := costs[goods[i]]
			for j := k; j >= c; j-- {
				dp[j] = max(dp[j], dp[j-c]+values[i])
				ans = max(ans, dp[j])
			}
		}
		fmt.Println(ans)
	}
}

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

func max(i, j int) int {
	if i > j {
		return i
	}
	return j
}

package main

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

func readInt(x *int) {
	if _, err := fmt.Scan(x); err != nil {
		panic(err)
	}
}
func read(x *string) {
	if _, err := fmt.Scan(x); err != nil {
		panic(err)
	}
}
func readLine() string {
	reader := bufio.NewReader(os.Stdin)
	text, _ := reader.ReadString('\n')
	return text
}
func readArray(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		readInt(&arr[i])
	}
	return arr
}
func min(vars ...int) int {
	m := vars[0]
	for _, val := range vars {
		if val < m {
			m = val
		}
	}
	return m
}
func max(vars ...int) int {
	m := vars[0]
	for _, val := range vars {
		if val > m {
			m = val
		}
	}
	return m
}

var dp [15001][1001]int

func rec(i int, n int, k int, x []int, b []int, c []int) int {
	if i == n {
		return 0
	}
	if dp[k][i] != 0 {
		return dp[k][i]
	}
	dp[k][i] = rec(i+1, n, k, x, b, c)
	if k >= x[b[i]] {
		dp[k][i] = max(dp[k][i], rec(i+1, n, k-x[b[i]], x, b, c)+c[i])
	}
	return dp[k][i]
}
func main() {
	var t int
	readInt(&t)
	x := make([]int, 1001)

	for i := 0; i < 1001; i++ {
		x[i] = 1000
	}
	x[1] = 0
	for i := 1; i <= 1000; i++ {
		for j := 1; j <= i; j++ {
			p := i + i/j
			if p <= 1000 {
				x[p] = min(x[p], x[i]+1)
			}
		}
	}
	// fmt.Println(max(x[1:]...))
	for ; t > 0; t-- {
		var n, k int
		readInt(&n)
		readInt(&k)

		b := readArray(n)
		c := readArray(n)
		sum := 0
		for _, v := range b {
			sum += x[v]
		}
		ans := 0
		// fmt.Println(sum)
		if k < sum {
			for i := 0; i <= k; i++ {
				for j := 0; j < n; j++ {
					dp[i][j] = 0
				}
			}
			ans = rec(0, n, k, x, b, c)

		} else {
			for _, v := range c {
				ans += v
			}
		}
		fmt.Println(ans)
	}

}

package main

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

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

func flush()                            { out.Flush() }
func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) {
	if !f {
		panic("gg")
	}
}

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
}

// ----------------------------- TEMPLATE END ----------------------------------

const TAM = 1010

var preCalcCost [TAM]int
var w [TAM]int
var profit [TAM]int

var dp [2][TAM*13 + 10]int

func main() {
	defer flush()

	for i := 1; i < TAM; i++ {
		preCalcCost[i] = i - 1
	}

	maxCost := 0
	for i := 1; i < TAM; i++ {
		for d := 1; d <= i; d++ {
			j := i + i/d
			if j < TAM {
				preCalcCost[j] = min(preCalcCost[j], preCalcCost[i]+1)
			}
		}
		maxCost = max(maxCost, preCalcCost[i])
	}

	// printf("maxCost = %d\n", maxCost)

	var ntc int
	scan(&ntc)
	for t := 0; t < ntc; t++ {
		var n, maxOps int
		scan(&n, &maxOps)
		for i := 0; i < n; i++ {
			scan(&w[i])
			w[i] = preCalcCost[w[i]]
		}
		for i := 0; i < n; i++ {
			scan(&profit[i])
		}

		maxOps = min(n*13, maxOps)
		for j := 0; j <= maxOps; j++ {
			dp[n&1][j] = 0
		}
		for i := n - 1; i >= 0; i-- {
			var next []int = dp[(i+1)&1][:]
			var cur []int = dp[i&1][:]
			for j := 0; j < w[i]; j++ {
				cur[j] = next[j]
			}
			for j := w[i]; j <= maxOps; j++ {
				cur[j] = max(next[j], next[j-w[i]]+profit[i])
			}
		}
		printf("%d\n", dp[0][maxOps])
	}
}

package main

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

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

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

func redirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

func main() {
	defer writer.Flush()
// 	redirectInputToFile(reader, "input.txt")

	minopsPrecalc()
	T := 1
	scan(&T)
	for t := 0; t < T; t++ {
		solve()
	}
}

var minops []int

func minopsPrecalc() {
	minops = make([]int, 1e3+1)
	for i := range minops {
		minops[i] = math.MaxInt32
	}
	minops[1] = 0
	for i := 1; i <= 1e3; i++ {
		for x := 1; x <= i; x++ {
			j := i + i/x
			if j <= 1e3 && minops[j] > minops[i]+1 {
				minops[j] = minops[i] + 1
			}
		}
	}
}

func solve() {
	var n, k int
	scan(&n, &k)

	if k > 12*n {
		k = 12 * n
	}

	b := make([]int, n)
	for i := range b {
		scan(&b[i])
		b[i] = minops[b[i]]
	}

	c := make([]int64, n)
	for i := range c {
		scan(&c[i])
	}

	dp := make([][]int64, n)
	for i := range dp {
		dp[i] = make([]int64, k+1)
	}

	for j := b[0]; j <= k; j++ {
		dp[0][j] = c[0]
	}

	for i := 1; i < n; i++ {
		for j := 0; j <= k; j++ {
			dp[i][j] = dp[i-1][j]
		}
		for j := b[i]; j <= k; j++ {
			if dp[i][j] < dp[i-1][j-b[i]]+c[i] {
				dp[i][j] = dp[i-1][j-b[i]] + c[i]
			}
		}
	}

	var ans int64 = -1
	for j := 0; j <= k; j++ {
		if ans < dp[n-1][j] {
			ans = dp[n-1][j]
		}
	}
	printf("%v\n", ans)
}

package main

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

var n, k int

var b[]int
var c[]int64

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

	preprocess()

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

		b = make([]int, n)
		c = make([]int64, n)

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

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

		//st := time.Now().UnixNano()
		ans := solve()
		//en := time.Now().UnixNano()
//
		//fmt.Println((en - st)/1000000)

		fmt.Println(ans)
	}
}

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
var cost []int

const INF = int(1e9) + 5;

func preprocess() {
	cost = make([]int, 2001)

	for i := 0; i < 2001; i++ {
		cost[i] = INF
	}

	cost[1] = 0

	for a := 1; a <= 1e3; a++ {
		for x := 1; x <= 1e3; x++ {
			b := a + a/x
			cost[b] = min(cost[b], cost[a]+1);
		}
	}
}

func solve() int64 {

	ops := make([]int, n)

	var minCost int = 1e9

	for i := 0; i < n; i++ {
		ops[i] = cost[b[i]]
		if cost[b[i]] < minCost {
			minCost = cost[b[i]]
		}
	}

	dp := make([][]int64, 2)
	for i := 0; i < 2; i++ {
		dp[i] = make([]int64, k + 1)
	}
	for i := 0; i < 2; i++ {
		dp[i][0] = 0
	}
	for i := 0; i <= k; i++ {
		if ops[0] > i {
			dp[0][i] = 0

		} else {
			dp[0][i] = c[0]
		}
	}

	k = min(k, 12 * n)

	for i := 1; i < n; i++ {

		cur := i % 2
		var prev int
		if cur == 1 {
			prev = 0
		} else {
			prev = 1
		}

		for j := cost[b[i]]; j <= k; j++ {

			if ops[i] > j {
				dp[cur][j] = dp[prev][j]

			} else {
				dp[cur][j] = max(dp[prev][j], dp[prev][j - ops[i]] + c[i])
			}
		}
	}

	var max int64

	for i := 0; i <= k; i++ {
		if dp[0][i] > max {
			max = dp[0][i]
		}
		if dp[1][i] > max {
			max = dp[1][i]
		}
	}

	return max
}

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

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"
	//"time"
)

var n, k int

var b[]int
var c[]int64

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

	preprocess()

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

		b = make([]int, n)
		c = make([]int64, n)

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

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

	//	st := time.Now().UnixNano()
		ans := solve()
	//	en := time.Now().UnixNano()
//
	//	fmt.Println((en - st)/1000000)

		fmt.Println(ans)
	}
}

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
var cost []int

const INF = int(1e9) + 5;

func preprocess() {
	cost = make([]int, 2001)

	for i := 0; i < 2001; i++ {
		cost[i] = INF
	}

	cost[1] = 0

	for a := 1; a <= 1e3; a++ {
		for x := 1; x <= 1e3; x++ {
			b := a + a/x
			cost[b] = min(cost[b], cost[a]+1);
		}
	}
}

func solve() int64 {

	ops := make([]int, n)

	var minCost int = 1e9

	for i := 0; i < n; i++ {
		ops[i] = cost[b[i]]
		if cost[b[i]] < minCost {
			minCost = cost[b[i]]
		}
	}

	dp := make([][]int64, 2)
	for i := 0; i < 2; i++ {
		dp[i] = make([]int64, k + 1)
	}
	for i := 0; i < 2; i++ {
		dp[i][0] = 0
	}
	for i := 0; i <= k; i++ {
		if ops[0] > i {
			dp[0][i] = 0

		} else {
			dp[0][i] = c[0]
		}
	}

	for i := 1; i < n; i++ {

		cur := i % 2
		var prev int
		if cur == 1 {
			prev = 0
		} else {
			prev = 1
		}

		for j := cost[b[i]]; j <= k; j++ {

			if ops[i] > j {
				dp[cur][j] = dp[prev][j]

			} else {
				dp[cur][j] = max(dp[prev][j], dp[prev][j - ops[i]] + c[i])
			}
		}
	}

	var max int64

	for i := 0; i <= k; i++ {
		if dp[0][i] > max {
			max = dp[0][i]
		}
		if dp[1][i] > max {
			max = dp[1][i]
		}
	}

	return max
}

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

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"
	"time"
)

var n, k int

var b[]int
var c[]int64

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

	preprocess()

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

		b = make([]int, n)
		c = make([]int64, n)

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

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

		st := time.Now().UnixNano()
		ans := solve()
		en := time.Now().UnixNano()

		fmt.Println((en - st)/1000000)

		fmt.Println(ans)
	}
}

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
var cost []int

const INF = int(1e9) + 5;

func preprocess() {
	cost = make([]int, 2001)

	for i := 0; i < 2001; i++ {
		cost[i] = INF
	}

	cost[1] = 0

	for a := 1; a <= 1e3; a++ {
		for x := 1; x <= 1e3; x++ {
			b := a + a/x
			cost[b] = min(cost[b], cost[a]+1);
		}
	}
}

func solve() int64 {

	ops := make([]int, n)

	var minCost int = 1e9

	for i := 0; i < n; i++ {
		ops[i] = cost[b[i]]
		if cost[b[i]] < minCost {
			minCost = cost[b[i]]
		}
	}

	dp := make([][]int64, 2)
	for i := 0; i < 2; i++ {
		dp[i] = make([]int64, k + 1)
	}
	for i := 0; i < 2; i++ {
		dp[i][0] = 0
	}
	for i := 0; i <= k; i++ {
		if ops[0] > i {
			dp[0][i] = 0

		} else {
			dp[0][i] = c[0]
		}
	}

	for i := 1; i < n; i++ {

		cur := i % 2
		var prev int
		if cur == 1 {
			prev = 0
		} else {
			prev = 1
		}

		for j := cost[b[i]]; j <= k; j++ {

			if ops[i] > j {
				dp[cur][j] = dp[prev][j]

			} else {
				dp[cur][j] = max(dp[prev][j], dp[prev][j - ops[i]] + c[i])
			}
		}
	}

	var max int64

	for i := 0; i <= k; i++ {
		if dp[0][i] > max {
			max = dp[0][i]
		}
		if dp[1][i] > max {
			max = dp[1][i]
		}
	}

	return max
}

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

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 n, k int

var b[]int
var c[]int64

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)

		b = make([]int, n)
		c = make([]int64, n)

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

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

		ans := solve()

		fmt.Println(ans)
	}
}

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 {

	ops := make([]int, n)

	for i := 0; i < len(b); i++ {
		var op int

		k := 1

		/*for ; ; {
			if k < b[i] {
				if 2*k > b[i] {
					break
				}
				k *= 2
				op++
			} else {
				break
			}
		}*/
		if k < b[i] {
			for ; ; {
				d := b[i] - k

				d1 := k / d
				r := k % d
				if r != 0 {
					d1 ++
				}

				k = k + k/d1
				op++

				if k == b[i] {
					break
				}
			}

		}
		ops[i] = op
	}

	dp := make([][]int64, n)
	for i := 0; i < n; i++ {
		dp[i] = make([]int64, k + 1)
	}
	for i := 0; i < n; i++ {
		dp[i][0] = 0
	}
	for i := 0; i <= k; i++ {
		if ops[0] > i {
			dp[0][i] = 0

		} else {
			dp[0][i] = c[0]
		}
	}

	for i := 1; i < n; i++ {

		for j := 0; j <= k; j++ {

			if ops[i] > j {
				dp[i][j] = dp[i - 1][j]

			} else {
				dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - ops[i]] + c[i])
			}
		}
	}

	var max int64

	for i := 0; i <= k; i++ {
		if dp[n-1][i] > max {
			max = dp[n- 1][i]
		}
	}

	return max
}

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

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 n, k int

var b[]int
var c[]int64

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)

		b = make([]int, n)
		c = make([]int64, n)

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

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

		ans := solve()

		fmt.Println(ans)
	}
}

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 {

	ops := make([]int, n)

	for i := 0; i < len(b); i++ {
		var op int

		k := 1

	/*	for ; ; {
			if k < b[i] {
				if 2*k > b[i] {
					break
				}
				k *= 2
				op++
			} else {
				break
			}
		}
*/
		if k < b[i] {
			for ; ; {
				d := b[i] - k

				d1 := k / d
				r := k % d
				if r != 0 {
					d1 ++
				}

				k = k + k/d1
				op++

				if k == b[i] {
					break
				}
			}

		}
		ops[i] = op
	}

	dp := make([][]int64, n)
	for i := 0; i < n; i++ {
		dp[i] = make([]int64, k + 1)
	}
	for i := 0; i < n; i++ {
		dp[i][0] = 0
	}
	for i := 0; i <= k; i++ {
		if ops[0] > i {
			dp[0][i] = 0

		} else {
			dp[0][i] = c[0]
		}
	}

	for i := 1; i < n; i++ {

		for j := 0; j <= k; j++ {

			if ops[i] > j {
				dp[i][j] = dp[i - 1][j]

			} else {
				dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - ops[i]] + c[i])
			}
		}
	}

	return dp[n - 1][k]
}

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

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 n, k int

var b[]int
var c[]int64

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)

		b = make([]int, n)
		c = make([]int64, n)

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

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

		ans := solve()

		fmt.Println(ans)
	}
}

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 {

	ops := make([]int, n)

	for i := 0; i < len(b); i++ {
		var op int

		k := 1

		for ; ; {
			if k < b[i] {
				if 2*k > b[i] {
					break
				}
				k *= 2
				op++
			} else {
				break
			}
		}

		if k < b[i] {
			for ; ; {
				d := b[i] - k

				d1 := k / d
				r := k % d
				if r != 0 {
					d1 ++
				}

				k = k + k/d1
				op++

				if k == b[i] {
					break
				}
			}

		}
		ops[i] = op
	}

	dp := make([][]int64, n)
	for i := 0; i < n; i++ {
		dp[i] = make([]int64, k + 1)
	}
	for i := 0; i < n; i++ {
		dp[i][0] = 0
	}
	for i := 0; i <= k; i++{
		if ops[0] > i {
			dp[0][i] = 0

		} else {
			dp[0][i] = c[0]
		}

	}

	for i := 1; i < n; i++ {

		for j := 0; j <= k; j++ {

			if ops[i] > j {
				dp[i][j] = dp[i - 1][j]

			} else {
				dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - ops[i]] + c[i])
			}
		}
	}

	return dp[n - 1][k]
}

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

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"
	"math"
	"os"
	"sort"
)

/*
1
4 4
1 7 5 2
2 6 5 2
======
1
6 14
11 4 6 2 8 16
43 45 9 41 15 38
*/

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

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

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

		b := make([]int, n)

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

		c := make([]int, n)

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

		type pnc struct {
			p    int
			c    int
			pToC float64
		}
		pncs := make([]pnc, n)

		for i := range pncs {
			cost := 0
			num := 1

			for 2*num < b[i] {
				num *= 2
				cost++
			}

			if num != b[i] {
				hit := false
				for x := 1; x <= num; x++ {
					if num+num/x == b[i] {
						hit = true
					}
				}

				if hit {
					cost += 1
				} else {
					cost += 2
				}
			}

			pncs[i].c = cost
			pncs[i].p = c[i]
			pncs[i].pToC = float64(pncs[i].p) / float64(pncs[i].c)
		}

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

		profitSum := 0

		for i := n - 1; i >= 0; i-- {
			if pncs[i].c <= k {
				k -= pncs[i].c
				profitSum += pncs[i].p
			}
		}

		fmt.Fprintln(writer, profitSum)
	}
}

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 min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	maximum := nums[0]
	for _, num := range nums {
		if num > maximum {
			maximum = num
		}
	}
	return maximum
}

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

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

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

	for i := 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))
}

package main

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

/*
1
4 4
1 7 5 2
2 6 5 2
======
1
6 14
11 4 6 2 8 16
43 45 9 41 15 38
*/

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

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

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

		b := make([]int, n)

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

		c := make([]int, n)

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

		type pnc struct {
			p    int
			c    int
			pToC float64
		}
		pncs := make([]pnc, n)

		for i := range pncs {
			opCnt := int(math.Log2(float64(b[i])))
			num := int(math.Pow(2, float64(opCnt)))
			if num != b[i] {
				if b[i] <= num+num/2 {
					opCnt += 1
				} else {
					opCnt += 2
				}
			}

			pncs[i].c = opCnt
			pncs[i].p = c[i]
			pncs[i].pToC = float64(pncs[i].p) / float64(pncs[i].c)
		}

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

		profitSum := 0

		for i := n - 1; i >= 0; i-- {
			if pncs[i].c <= k {
				k -= pncs[i].c
				profitSum += pncs[i].p
			}
		}

		fmt.Fprintln(writer, profitSum)
	}
}

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 min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	maximum := nums[0]
	for _, num := range nums {
		if num > maximum {
			maximum = num
		}
	}
	return maximum
}

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

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

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

	for i := 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))
}

package main

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

var (
	in     = bufio.NewReader(os.Stdin)
	dpCost []int
)

func calCostV(cur int, dpCostV []int, v int) int {
	if dpCostV[cur] != -1 {
		return dpCostV[cur]
	}

	if cur == v {
		return 0
	}
	if cur > v {
		panic("impossible")
	}
	result := 1001
	for x := cur; x >= 1; x-- {
		next := cur + cur/x
		if next > v {
			break
		}
		candi := calCostV(next, dpCostV, v) + 1
		if candi < result {
			result = candi
		}
	}
	if result == 1001 {
		panic("impossible")
	}
	dpCostV[cur] = result
	return dpCostV[cur]
}

func getCost(v int) int {
	if dpCost[v] != -1 {
		return dpCost[v]
	}

	dpCostV := make([]int, v+1)
	for i := range dpCostV {
		dpCostV[i] = -1
	}
	dpCost[v] = calCostV(1, dpCostV, v)
	return dpCost[v]
}

func calMaxCoin(cur, k int, dp [][]int, coins, costs []int) int {
	if cur >= len(dp) {
		return 0
	}
	if k <= 0 {
		return 0
	}
	if dp[cur][k] != -1 {
		return dp[cur][k]
	}

	result := 0
	if k >= costs[cur] {
		result = calMaxCoin(cur+1, k-costs[cur], dp, coins, costs) + coins[cur]
	}
	if notUse := calMaxCoin(cur+1, k, dp, coins, costs); notUse > result {
		result = notUse
	}

	dp[cur][k] = result
	return result
}

func solve() {
	var n, k int
	fmt.Fscan(in, &n)
	fmt.Fscan(in, &k)

	b := make([]int, n)
	for i := range b {
		fmt.Fscan(in, &b[i])
	}
	coins := make([]int, n)
	for i := range coins {
		fmt.Fscan(in, &coins[i])
	}

	costs := make([]int, n)
	for i, bi := range b {
		costs[i] = getCost(bi)
	}

	dp := make([][]int, n)
	for i := range dp {
		dp[i] = make([]int, k+1)
		for j := range dp[i] {
			dp[i][j] = -1
		}
	}
	fmt.Println(calMaxCoin(0, k, dp, coins, costs))
}

func main() {
	dpCost = make([]int, 1001)
	for i := range dpCost {
		dpCost[i] = -1
	}

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

	for i := 0; i < t; i++ {
		solve()
	}
}

package main

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

var (
	in     = bufio.NewReader(os.Stdin)
	dpCost []int
)

func calCostV(cur int, dpCostV []int, v int) int {
	if dpCostV[cur] != -1 {
		return dpCostV[cur]
	}

	if cur == v {
		return 0
	}
	if cur > v {
		panic("impossible")
	}
	result := 1001
	for x := cur; x >= 1; x-- {
		next := cur + cur/x
		if next > v {
			break
		}
		candi := calCostV(next, dpCostV, v) + 1
		if candi < result {
			result = candi
		}
	}
	if result == 1001 {
		panic("impossible")
	}
	dpCostV[cur] = result
	return dpCostV[cur]
}

func getCost(v int) int {
	if dpCost[v] != -1 {
		return dpCost[v]
	}

	dpCostV := make([]int, v+1)
	for i := range dpCostV {
		dpCostV[i] = -1
	}
	dpCost[v] = calCostV(1, dpCostV, v)
	return dpCost[v]
}

func calMaxCoin(cur, k int, dp [][]int, coins, costs []int) int {
	if cur >= len(dp) {
		return 0
	}
	if k <= 0 {
		return 0
	}
	if dp[cur][k] != -1 {
		return dp[cur][k]
	}

	result := 0
	if k >= costs[cur] {
		result = calMaxCoin(cur+1, k-costs[cur], dp, coins, costs) + coins[cur]
	}
	if notUse := calMaxCoin(cur+1, k, dp, coins, costs); notUse > result {
		result = notUse
	}

	dp[cur][k] = result
	return result
}

func solve() {
	var n, k int
	fmt.Fscan(in, &n)
	fmt.Fscan(in, &k)

	b := make([]int, n)
	for i := range b {
		fmt.Fscan(in, &b[i])
	}
	coins := make([]int, n)
	for i := range coins {
		fmt.Fscan(in, &coins[i])
	}

	costs := make([]int, n)
	for i, bi := range b {
		costs[i] = getCost(bi)
	}
	fmt.Println(costs)

	dp := make([][]int, n)
	for i := range dp {
		dp[i] = make([]int, k+1)
		for j := range dp[i] {
			dp[i][j] = -1
		}
	}
	fmt.Println(calMaxCoin(0, k, dp, coins, costs))
}

func main() {
	dpCost = make([]int, 1001)
	for i := range dpCost {
		dpCost[i] = -1
	}

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

	for i := 0; i < t; i++ {
		solve()
	}
}

package main

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

var in = bufio.NewReader(os.Stdin)

func calCostV(cur int, dpCostV []int, v int) int {
	if dpCostV[cur] != -1 {
		return dpCostV[cur]
	}

	if cur == v {
		return 0
	}
	if cur > v {
		panic("impossible")
	}
	result := 1001
	for x := cur; x >= 1; x-- {
		if cur%x == 0 {
			next := cur + cur/x
			if next > v {
				break
			}
			candi := calCostV(next, dpCostV, v) + 1
			if candi < result {
				result = candi
			}
		}
	}
	if result == 1001 {
		panic("impossible")
	}
	dpCostV[cur] = result
	return dpCostV[cur]
}

func getCost(v int, dpCost []int) int {
	if dpCost[v] != -1 {
		return dpCost[v]
	}

	dpCostV := make([]int, v+1)
	for i := range dpCostV {
		dpCostV[i] = -1
	}
	dpCost[v] = calCostV(1, dpCostV, v)
	return dpCost[v]
}

func calMaxCoin(cur, k int, dp [][]int, coins, costs []int) int {
	if cur >= len(dp) {
		return 0
	}
	if k <= 0 {
		return 0
	}
	if dp[cur][k] != -1 {
		return dp[cur][k]
	}

	result := 0
	if k >= costs[cur] {
		result = calMaxCoin(cur+1, k-costs[cur], dp, coins, costs) + coins[cur]
	}
	if notUse := calMaxCoin(cur+1, k, dp, coins, costs); notUse > result {
		result = notUse
	}

	dp[cur][k] = result
	return result
}

func solve() {
	dpCost := make([]int, 1001)
	for i := range dpCost {
		dpCost[i] = -1
	}

	var n, k int
	fmt.Fscan(in, &n)
	fmt.Fscan(in, &k)

	b := make([]int, n)
	for i := range b {
		fmt.Fscan(in, &b[i])
	}
	coins := make([]int, n)
	for i := range coins {
		fmt.Fscan(in, &coins[i])
	}

	costs := make([]int, n)
	for i, bi := range b {
		costs[i] = getCost(bi, dpCost)
	}

	dp := make([][]int, n)
	for i := range dp {
		dp[i] = make([]int, k+1)
		for j := range dp[i] {
			dp[i][j] = -1
		}
	}
	fmt.Println(calMaxCoin(0, k, dp, coins, costs))
}

func main() {
	var t int
	fmt.Fscan(in, &t)

	for i := 0; i < t; i++ {
		solve()
	}
}

package main

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

var in = bufio.NewReader(os.Stdin)

func CountSubStrings(x int64) int64 {
	// if x == 0, return 0
	return x * (x + 1) / 2
}

func solve() {
	var n, m int64
	fmt.Fscan(in, &n)
	fmt.Fscan(in, &m)

	zero, chunks := n-m, m+1
	base, remainder := zero/chunks, zero%chunks
	zeroCount := CountSubStrings(base)*(chunks-remainder) + CountSubStrings(base+1)*remainder
	fmt.Println(CountSubStrings(n) - zeroCount)
}

func main() {
	var t int
	fmt.Fscan(in, &t)

	for i := 0; i < t; i++ {
		solve()
	}
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)
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 main() {
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	var t int
	fmt.Fscan(in,&t)
	a := [1005]int{}
	a[1]=0
	for i:=2;i<=1000;i++{
		a[i]=math.MaxInt32
	}
	for i:=1;i<=1000;i++{
		for j:=1;j<=i;j++{
			nex := i+i/j
			if nex<=1000 {
				a[nex] = min(a[nex], a[i]+1)
			}
		}
	}
	for ct:=1;ct<=t;ct++{
		var n, k int
		fmt.Fscan(in,&n,&k)
		b := make([]int,1005)
		c := make([]int,1005)
		for i:=1;i<=n;i++{
			fmt.Fscan(in,&b[i])
			b[i] = a[b[i]]
		}
		for i:=1;i<=n;i++{
			fmt.Fscan(in,&c[i])
		}
		f := [1005][10500]int{}
		f[0][0]=0
		for i:=1;i<=min(k,10400);i++{
			f[0][i]=-999999999
		}
		ans:=0
		for i:=1;i<=n;i++{
			for j:=0;j<=min(10400,k);j++{
				if j-b[i]>=0{
					f[i][j] = max(f[i][j], f[i-1][j-b[i]]+c[i])
				}
				f[i][j]=max(f[i][j],f[i-1][j])
				ans = max(ans,f[i][j])
			}
		}
		fmt.Fprintln(out,ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)
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 main() {
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	var t int
	fmt.Fscan(in,&t)
	a := [1005]int{}
	a[1]=0
	for i:=2;i<=1000;i++{
		a[i]=math.MaxInt32
	}
	for i:=1;i<=1000;i++{
		for j:=1;j<=i;j++{
			nex := i+i/j
			if nex<=1000 {
				a[nex] = min(a[nex], a[i]+1)
			}
		}
	}
	for ct:=1;ct<=t;ct++{
		var n, k int
		fmt.Fscan(in,&n,&k)
		b := make([]int,1005)
		c := make([]int,1005)
		sum:=0
		for i:=1;i<=n;i++{
			fmt.Fscan(in,&b[i])
			b[i] = a[b[i]]
			sum+=b[i]
		}
		for i:=1;i<=n;i++{
			fmt.Fscan(in,&c[i])
		}
		f := [1005][10500]int{}
		f[1][b[1]]=c[1]
		ans:=0
		for i:=2;i<=n;i++{
			for j:=0;j<=min(10400,k);j++{
				if j-b[i]>=0{
					f[i][j] = max(f[i][j], f[i-1][j-b[i]]+c[i])
				}
				f[i][j]=max(f[i][j],f[i-1][j])
				ans = max(ans,f[i][j])
			}
		}
		fmt.Fprintln(out,ans)
	}
}

package main

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

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

func CF1633D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	costs := [1005]int{}
	for i := 1; i < 1005; i++ {
		costs[i] = 1e3
	}
	costs[1] = 0
	//暴力计算1->i所需的最短次数
	for i := 1; i <= 1000; i++ {
		for j := 1; j <= i; j++ {
			if i+i/j <= 1000 {
				costs[i+i/j] = min(costs[i+i/j], costs[i]+1)
			}
		}
	}

	cases := 0
	fmt.Fscan(in, &cases)
	for c := 0; c < cases; c++ {
		n, k := 0, 0
		fmt.Fscan(in, &n, &k)
		k = min(12*n, k) //一个数从1->1000最小步数最多12次，所以背包容量取min
		goods, values := make([]int, n), make([]int, n)
		for i := range goods {
			fmt.Fscan(in, &goods[i])
		}
		for i := range values {
			fmt.Fscan(in, &values[i])
		}
		fmt.Println(goods, values)
		//创建dp数组
		dp := make([]int, k+1)
		for i := range dp {
			dp[i] = math.MinInt32
		}
		dp[0] = 0
		ans := 0
		//0-1bag question
		for i := range goods {
			c := costs[goods[i]]
			for j := k; j >= c; j-- {
				dp[j] = max(dp[j], dp[j-c]+values[i])
				ans = max(ans, dp[j])
			}
		}
		fmt.Println(ans)
	}
}

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

func max(i, j int) int {
	if i > j {
		return i
	}
	return j
}

package main

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

func readInt(x *int) {
	if _, err := fmt.Scan(x); err != nil {
		panic(err)
	}
}
func read(x *string) {
	if _, err := fmt.Scan(x); err != nil {
		panic(err)
	}
}
func readLine() string {
	reader := bufio.NewReader(os.Stdin)
	text, _ := reader.ReadString('\n')
	return text
}
func readArray(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		readInt(&arr[i])
	}
	return arr
}
func min(vars ...int) int {
	m := vars[0]
	for _, val := range vars {
		if val < m {
			m = val
		}
	}
	return m
}
func max(vars ...int) int {
	m := vars[0]
	for _, val := range vars {
		if val > m {
			m = val
		}
	}
	return m
}

var dp [15001][1001]int

func rec(i int, n int, k int, x []int, b []int, c []int) int {
	if i == n || k <= 0 {
		return 0
	}
	if dp[k][i] != 0 {
		return dp[k][i]
	}
	dp[k][i] = rec(i+1, n, k, x, b, c)
	if k >= x[b[i]] {
		dp[k][i] = max(dp[k][i], rec(i+1, n, k-x[b[i]], x, b, c)+c[i])
	}
	return dp[k][i]
}
func main() {
	var t int
	readInt(&t)
	x := make([]int, 1001)
	x[1] = 0

	for i := 2; i <= 1000; i++ {
		x[i] = math.MaxInt
		for j := int((i + 1) / 2); j < i; j++ {
			diff := i - j
			if j%diff == 0 {
				x[i] = min(x[i], x[j]+1)
			}
		}
	}
	for ; t > 0; t-- {
		var n, k int
		readInt(&n)
		readInt(&k)

		b := readArray(n)
		c := readArray(n)
		sum := 0
		for _, v := range b {
			sum += v
		}
		ans := 0
		if k < sum {
			for i := 0; i <= k; i++ {
				for j := 0; j < n; j++ {
					dp[i][j] = 0
				}
			}
			ans = rec(0, n, k, x, b, c)
		} else {
			ans := 0
			for _, v := range c {
				ans += v
			}
		}
		fmt.Println(ans)
	}

}

package main

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

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

func flush()                            { out.Flush() }
func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) {
	if !f {
		panic("gg")
	}
}

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
}

// ----------------------------- TEMPLATE END ----------------------------------

const TAM = 1010

var preCalcCost [TAM]int
var w [TAM]int
var profit [TAM]int

var dp [2][TAM*13 + 10]int

func main() {
	defer flush()

	for i := 1; i < TAM; i++ {
		preCalcCost[i] = i - 1
	}

	maxCost := 0
	for i := 1; i < TAM; i++ {
		for d := 1; d <= i; d++ {
			j := i + i/d
			if j < TAM {
				preCalcCost[j] = min(preCalcCost[j], preCalcCost[i]+1)
			}
		}
		maxCost = max(maxCost, preCalcCost[i])
	}

	var ntc int
	scan(&ntc)
	for t := 0; t < ntc; t++ {
		var n, maxOps int
		scan(&n, &maxOps)
		for i := 0; i < n; i++ {
			scan(&w[i])
			w[i] = preCalcCost[w[i]]
		}
		for i := 0; i < n; i++ {
			scan(&profit[i])
		}

		maxOps = min(n*13, maxOps)
		for j := 0; j < maxOps; j++ {
			dp[n&1][j] = 0
		}
		for i := n - 1; i >= 0; i-- {
			var next []int = dp[(i+1)&1][:]
			var cur []int = dp[i&1][:]
			for j := 0; j < w[i]; j++ {
				cur[j] = next[j]
			}
			for j := w[i]; j <= maxOps; j++ {
				cur[j] = max(next[j], next[j-w[i]]+profit[i])
			}
		}
		printf("%d\n", dp[0][maxOps])
	}
}