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


package main

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

func die(v ...interface{}) {
	_, _, line, _ := runtime.Caller(1)
	fmt.Printf("die line %d: %v\n", line, v)
	os.Exit(-1)
}

func check(b bool) {
	if !b {
		_, _, line, _ := runtime.Caller(1)
		fmt.Printf("check failed line %d\n", line)
		os.Exit(-1)
	}
}

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

type IntHeap []int

func (h IntHeap) Len() int            { return len(h) }
func (h IntHeap) Less(i, j int) bool  { return h[i] < h[j] }
func (h IntHeap) Swap(i, j int)       { h[i], h[j] = h[j], h[i] }
func (h *IntHeap) Push(x interface{}) { *h = append(*h, x.(int)) }
func (h *IntHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

const (
	INIT = -1
)

var (
	n    int
	m    int
	adj  []int
	doom int
	d    []int
)

func at(i, j int) int {
	return i*m + j
}

// Tricks:
//   -ive distance for the current s
//   use path instead of p[u] to backtrack
//   subtract depth at cycle from total depth
func dist(s int) int {
	if d[s] != 0 {
		return d[s]
	}

	path := &IntHeap{}
	u := s
	depth := -1
	for u != doom && d[u] == 0 {
		d[u] = depth
		depth--
		path.Push(u)

		u = adj[u]
	}

	l := 1
	if u != doom {
		if d[u] < 0 { // cycle
			cycle := d[u] - depth // total depth - depth at start of cycle
			for i := 0; i < cycle; i++ {
				v := path.Pop().(int)
				d[v] = cycle
			}
		}
		l += d[u] // either start of cycle or other s path
	}

	for path.Len() > 0 {
		v := path.Pop().(int)
		d[v] = l
		l++
	}

	return d[s]
}

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

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

	for ; tc > 0; tc-- {

		fmt.Fscan(r, &n, &m)
		adj = make([]int, n*m+1)
		doom = n * m

		for i := 0; i < n; i++ {
			var s string
			fmt.Fscan(r, &s)

			for j := 0; j < m; j++ {
				ni := i
				nj := j
				switch s[j] {
				case 'L':
					nj--
				case 'R':
					nj++
				case 'U':
					ni--
				case 'D':
					ni++
				}
				if ni < 0 || ni >= n || nj < 0 || nj >= m {
					adj[at(i, j)] = doom
				} else {
					adj[at(i, j)] = at(ni, nj)
				}
			}
		}

		d = make([]int, n*m+1)

		best := 0
		ans := INIT
		for u := 0; u < n*m; u++ {
			rv := dist(u)
			if best < rv {
				best = rv
				ans = u
			}
		}

		fmt.Printf("%d %d %d\n", ans/m+1, ans%m+1, best)

	}
}

package main

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

func die(v ...interface{}) {
	_, _, line, _ := runtime.Caller(1)
	fmt.Printf("die line %d: %v\n", line, v)
	os.Exit(-1)
}

func check(b bool) {
	if !b {
		_, _, line, _ := runtime.Caller(1)
		fmt.Printf("check failed line %d\n", line)
		os.Exit(-1)
	}
}

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

type IntHeap []int

func (h IntHeap) Len() int            { return len(h) }
func (h IntHeap) Less(i, j int) bool  { return h[i] < h[j] }
func (h IntHeap) Swap(i, j int)       { h[i], h[j] = h[j], h[i] }
func (h *IntHeap) Push(x interface{}) { *h = append(*h, x.(int)) }
func (h *IntHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

const (
	INIT = -1
)

var (
	n    int
	m    int
	adj  []int
	doom int
	d    []int
)

func at(i, j int) int {
	return i*m + j
}

// Tricks:
//   -ive distance for the current s
//   use path instead of p[u] to backtrack
//   subtract depth at cycle from total depth
func dist(s int) int {
	if d[s] != 0 {
		return d[s]
	}

	path := IntHeap{}
	u := s
	depth := -1
	for u != doom && d[u] == 0 {
		d[u] = depth
		depth--
		path.Push(u)

		u = adj[u]
	}

	l := 1
	if u != doom {
		if d[u] < 0 { // cycle
			cycle := d[u] - depth // total depth - depth at start of cycle
			for i := 0; i < cycle; i++ {
				v := path.Pop().(int)
				d[v] = cycle
			}
		}
		l += d[u] // either start of cycle or other s path
	}

	for len(path) > 0 {
		v := path.Pop().(int)
		d[v] = l
		l++
	}

	return d[s]
}

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

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

	for ; tc > 0; tc-- {

		fmt.Fscan(r, &n, &m)
		adj = make([]int, n*m+1)
		doom = n * m

		for i := 0; i < n; i++ {
			var s string
			fmt.Fscan(r, &s)

			for j := 0; j < m; j++ {
				ni := i
				nj := j
				switch s[j] {
				case 'L':
					nj--
				case 'R':
					nj++
				case 'U':
					ni--
				case 'D':
					ni++
				}
				if ni < 0 || ni >= n || nj < 0 || nj >= m {
					adj[at(i, j)] = doom
				} else {
					adj[at(i, j)] = at(ni, nj)
				}
			}
		}

		d = make([]int, n*m+1)

		best := 0
		ans := INIT
		for u := 0; u < n*m; u++ {
			rv := dist(u)
			if best < rv {
				best = rv
				ans = u
			}
		}

		fmt.Printf("%d %d %d\n", ans/m+1, ans%m+1, best)

	}
}

package main

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

func die(v ...interface{}) {
	_, _, line, _ := runtime.Caller(1)
	fmt.Printf("die line %d: %v\n", line, v)
	os.Exit(-1)
}

func check(b bool) {
	if !b {
		_, _, line, _ := runtime.Caller(1)
		fmt.Printf("check failed line %d\n", line)
		os.Exit(-1)
	}
}

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

type IntHeap []int

func (h IntHeap) Len() int            { return len(h) }
func (h IntHeap) Less(i, j int) bool  { return h[i] < h[j] }
func (h IntHeap) Swap(i, j int)       { h[i], h[j] = h[j], h[i] }
func (h *IntHeap) Push(x interface{}) { *h = append(*h, x.(int)) }
func (h *IntHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

type vi []int

func (h *vi) pop() int {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

const (
	INIT = -1
)

var (
	n    int
	m    int
	adj  []int
	doom int
	d    []int
)

func at(i, j int) int {
	return i*m + j
}

// Tricks:
//   -ive distance for the current s
//   use path instead of p[u] to backtrack
//   subtract depth at cycle from total depth
func dist(s int) int {
	if d[s] != 0 {
		return d[s]
	}

	path := vi{}
	u := s
	depth := -1
	for u != doom && d[u] == 0 {
		d[u] = depth
		depth--
		path = append(path, u)

		u = adj[u]
	}

	l := 1
	if u != doom {
		if d[u] < 0 { // cycle
			cycle := d[u] - depth // total depth - depth at start of cycle
			for i := 0; i < cycle; i++ {
				v := path.pop()
				d[v] = cycle
			}
		}
		l += d[u] // either start of cycle or other s path
	}

	for len(path) > 0 {
		v := path.pop()
		d[v] = l
		l++
	}

	return d[s]
}

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

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

	for ; tc > 0; tc-- {

		fmt.Fscan(r, &n, &m)
		adj = make([]int, n*m+1)
		doom = n * m

		for i := 0; i < n; i++ {
			var s string
			fmt.Fscan(r, &s)

			for j := 0; j < m; j++ {
				ni := i
				nj := j
				switch s[j] {
				case 'L':
					nj--
				case 'R':
					nj++
				case 'U':
					ni--
				case 'D':
					ni++
				}
				if ni < 0 || ni >= n || nj < 0 || nj >= m {
					adj[at(i, j)] = doom
				} else {
					adj[at(i, j)] = at(ni, nj)
				}
			}
		}

		d = make([]int, n*m+1)

		best := 0
		ans := INIT
		for u := 0; u < n*m; u++ {
			rv := dist(u)
			if best < rv {
				best = rv
				ans = u
			}
		}

		fmt.Printf("%d %d %d\n", ans/m+1, ans%m+1, best)

	}
}


package main

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

func Init() {
}

func Solve(io *FastIO) {
    N := io.NextInt()
    S := io.NextLong()
    A := io.NextLongArrayOffset(N, 1)

    L := 1
    R := N
    good := 0
    price := int64(0)
    for L <= R {
        M := (L + R) >> 1
        costs := MakeCosts(A, M)
        if cost, ok := CanPurchase(costs, M, S); ok {
            good = M
            price = cost
            L = M + 1
        } else {
            R = M - 1
        }
    }
    io.Println(good, price)
}

func MakeCosts(A []int64, K int) []int64 {
    costs := make([]int64, len(A))
    for i := 1; i < len(A); i++ {
        costs[i] = A[i] + (int64(K) * int64(i))
    }
    return costs
}

func CanPurchase(costs []int64, K int, S int64) (int64, bool) {
    sort.Sort(SortInt64(costs[1:]))
    sum := int64(0)
    for i := 1; i <= K; i++ {
        sum += costs[i]
    }
    return sum, sum <= S
}

type SortInt64 []int64

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

func (s SortInt64) Swap(i, j int) {
    s[i], s[j] = s[j], s[i]
}

func (s SortInt64) Less(i, j int) bool {
    return s[i] < s[j]
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

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

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}

package main

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

func solve() {
	n := readInt()
	s := readInt()
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = readInt()
	}

	lo := 1
	hi := n
	ans := 0
	T := 0
	for lo <= hi {
		k := (lo + hi) / 2
		t := isOk(k, a, s)
		if t > 0 {
			lo = k + 1
			if ans < k {
				ans = k
				T = t
			}
		} else {
			hi = k - 1
		}
	}
	fmt.Println(ans, T)
}

func isOk(k int, a []int, s int) int {
	b := make([]int, 0)
	maxSum := int64(s)
	maxK := int64(k)
	for i := 0; i < len(a); i++ {
		v := int64(a[i]) + maxK*int64(i+1)
		if v <= maxSum {
			b = append(b, int(v))
		}
	}
	if len(b) < k {
		return 0
	}
	sort.Ints(b)
	sum := 0
	for i := 0; i < k; i++ {
		sum += b[i]
		if sum > s {
			return 0
		}
	}
	return sum
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

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

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

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

package main

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

func solve() {
	n := readInt()
	s := readInt()
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = readInt()
	}

	lo := 0
	hi := n
	ans := 0
	T := 0
	for lo <= hi {
		k := (lo + hi) / 2
		t := isOk(k, a, s)
		if t > 0 {
			lo = k + 1
			ans = k
			T = t
		} else {
			hi = k - 1
		}
	}
	fmt.Println(ans, T)
}

func isOk(k int, a []int, s int) int {
	b := make([]int, 0)
	for i := 0; i < len(a); i++ {
		v := int64(a[i]) + int64(k)*int64(i+1)
		if v <= int64(s) {
			b = append(b, int(v))
		}
	}
	if len(b) < k {
		return 0
	}
	sort.Ints(b)
	sum := 0
	for i := 0; i < k; i++ {
		sum += b[i]
		if sum > s {
			return 0
		}
	}
	return sum
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

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

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

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

package main

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

func solve() {
	n := readInt()
	s := readInt()
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = readInt()
	}

	lo := 0
	hi := n
	ans := 0
	T := 0
	for lo <= hi {
		k := (lo + hi) / 2
		t := isOk(k, a, s)
		if t > 0 {
			lo = k + 1
			ans = k
			T = t
		} else {
			hi = k - 1
		}
	}
	fmt.Println(ans, T)
}

func isOk(k int, a []int, s int) int {
	b := make([]int, 0)
	for i := 0; i < len(a); i++ {
		v := int64(a[i]) + int64(k)*int64(i+1)
		if v <= int64(s) {
			b = append(b, int(v))
		}
	}
	if len(b) < k {
		return 0
	}
	sort.Ints(b)
	sum := 0
	for i := 0; i < k; i++ {
		sum += b[i]
	}
	return sum
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

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

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

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

package main

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

func solve() {
	n := readInt()
	s := readInt()
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = readInt()
	}

	lo := 0
	hi := n
	ans := 0
	T := 0
	for lo <= hi {
		k := (lo + hi) / 2
		t := isOk(k, a, s)
		if t > 0 {
			lo = k + 1
			if ans < k {
				ans = k
				T = t
			}
		} else {
			hi = k - 1
		}
	}
	fmt.Println(ans, T)
}

func isOk(k int, a []int, s int) int {
	b := make([]int, 0)
	maxSum := int64(s)
	maxK := int64(k)
	for i := 0; i < len(a); i++ {
		v := int64(a[i]) + maxK*int64(i+1)
		if v <= maxSum {
			b = append(b, int(v))
		}
	}
	if len(b) < k {
		return 0
	}
	sort.Ints(b)
	sum := 0
	for i := 0; i < k; i++ {
		sum += b[i]
		if sum > s {
			return 0
		}
	}
	return sum
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

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

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

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

package main

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

func Init() {
}

func Solve(io *FastIO) {
    N := io.NextInt()
    S := io.NextLong()
    A := io.NextIntArrayOffset(N, 1)

    sort.Ints(A[1 : N + 1])

    baseSum := int64(0)
    prevCost := int64(0)
    for i := 1; i <= N; i++ {
        baseSum += int64(A[i])
        additional := (int64(i) * int64(i) * int64(i + 1)) >> 1
        nextCost := baseSum + additional
        if nextCost > S {
            io.Println(i - 1, prevCost)
            return
        }
        prevCost = nextCost
    }
    io.Println(N, prevCost)
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

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

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}


package main

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

func main() {
	sc := NewScanner()
	N := sc.NextInt()
	for i := 0; i < N; i++ {
		s, a, b, c := sc.NextInt(), sc.NextInt(), sc.NextInt(), sc.NextInt()
		k := int64(s / c)
		fmt.Println(k + int64(int64(int64(k)/int64(a))*int64(b)))
	}

}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

import (
    "fmt"
)

func main() {
    var count int
    fmt.Scanf("%d\n", &count)
    for i := 0; i < count; i++ {
        var m, c1, c2, price, res int64
        fmt.Scanf("%d %d %d %d\n", &m, &c1, &c2, &price)
        res = m / price
        res += (res / c1) * c2
        fmt.Println(res)
    }
}

package main

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

func ReadNumber(in *bufio.Reader) int64 {
	line, _ := in.ReadString('\n')
	num, _ := strconv.ParseInt(strings.TrimSpace(line[:len(line)-1]), 10, 64)
	return num
}

func ReadArray(in *bufio.Reader) []int64 {
	line, _ := in.ReadString('\n')

	strs := strings.Split(strings.TrimSpace(line[:len(line)-1]), " ")
	nums := make([]int64, len(strs))
	for i, str := range strs {
		nums[i], _ = strconv.ParseInt(str, 10, 64)
	}
	return nums
}

func main() {
	in := bufio.NewReader(os.Stdin)
	t := int(ReadNumber(in))
	for i := 0; i < t; i++ {
		nums := ReadArray(in)
		a := nums[0] / nums[3]
		b := a / nums[1]
		c := b * nums[2]
		fmt.Println(a + c)
	}
}

package main

import (
    "fmt"
    "os"
    )

func main() {

    var n,s,l,d int64
    var i,T int16
    var ans int64

    fmt.Scanf("%d\n", &T)

    for i=0; i< T; i++ {
        fmt.Scanf("%d%d%d%d\n", &n, &s, &l, &d)
        ans = n/d
        ans = ans + ((ans/s)*l)
        fmt.Println(ans)
    }
    os.Exit(0)
}

package main

import "fmt"

func main() {
	var t, s, a, b, c int64
	fmt.Scan(&t)
	for ; t > 0; t-- {
		fmt.Scan(&s, &a, &b, &c)
		fmt.Println(s/c + (s / c / a * b))
	}
}

package main

import "fmt"

func main() {

	var t int
	fmt.Scan(&t)

	for i := 0; i < t; i++ {
		var s, a, b, c int64
		fmt.Scan(&s, &a, &b, &c)

		var total int64
		total = (s / c)
		total = total + (total/a)*b

		fmt.Println(total)
	}
}

package main

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

var scanner *bufio.Scanner

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

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

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

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

	//writer.WriteString(fmt.Sprintf("%s", Problem705A()))
	Problem1065A()
}

func Problem1065A() {
	t := getI()
	ans := make([]int64,t)
	for i := 0; i < t; i++ {
		money := getI64()
		limitForBonus := getI64()
		bonusChoco := getI64()
		priceChoco := getI64()

		primaryBuy := money / priceChoco
		optBonus := primaryBuy / limitForBonus
		totalBonus := optBonus * bonusChoco

		ans[i] = primaryBuy + totalBonus
	}
	for dig := range ans {
		fmt.Println(ans[dig])
	}
}

package main

import (
	"fmt"
)

func main() {
	var t, s, a, b, c int64
	fmt.Scan(&t)
	for i := int64(0); i < t; i++ {
		fmt.Scan(&s, &a, &b, &c)
		pac := s / (a * c)
		left := s % (a * c)
		fmt.Println(pac*(a+b) + left/c)
	}
}

package main

import (
	"fmt"
)

func main() {
	var t int
	fmt.Scan(&t)
	var r []string
	for i:=0;i<t;i++ {
		var s,a,b,c int64
		fmt.Scan(&s,&a,&b,&c)
		s /=c
		r = append(r,fmt.Sprint((s/a)*(a+b)+(s%a)))
	}
	for _,l := range(r) {
		fmt.Println(l)
	}
}

package main

import "fmt"

func main() {
	var t, s, a, b, c int
	fmt.Scan(&t)
	for ; t > 0; t-- {
		fmt.Scan(&s, &a, &b, &c)
		fmt.Println(s/c + (s / c / a * b))
	}
}

package main

import "fmt"

func main() {

	var t int
	fmt.Scan(&t)

	for i := 0; i < t; i++ {
		var s, a, b, c int
		fmt.Scan(&s, &a, &b, &c)

		total := (s / c)
		total = total + (total/a)*b

		fmt.Println(total)
	}
}

package main

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

func main() {
	sc := NewScanner()
	N := sc.NextInt()
	for i := 0; i < N; i++ {
		s, a, b, c := sc.NextInt(), sc.NextInt(), sc.NextInt(), sc.NextInt()
		k := s / c
		fmt.Println(k + (k/a)*b)
	}

}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

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

var scanner *bufio.Scanner

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

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

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

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

	//writer.WriteString(fmt.Sprintf("%s", Problem705A()))
	Problem1065A()
}

func Problem1065A() {
	t := getI()
	for i := 0; i < t; i++ {
		s := getI()
		a := getI()
		b := getI()
		c := getI()

		bonus := s/a * b
		buy := (s/a * c)
		left := (s - buy) * c

		fmt.Println((buy + left + bonus))
	}
}

package main

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

var scanner *bufio.Scanner

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

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

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

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

	//writer.WriteString(fmt.Sprintf("%s", Problem705A()))
	Problem1065A()
}

func Problem1065A() {
	t := getI()
	ans := make([]int64,t)
	for i := 0; i < t; i++ {
		s := getI64()
		a := getI64()
		b := getI64()
		c := getI64()

		bonus := s/a * b
		buy := (s/a * c)
		left := (s - buy) * c

		ans[i] = buy + left + bonus
	}
	for dig := range ans {
		fmt.Println(ans[dig])
	}
}

package main

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

var scanner *bufio.Scanner

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

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

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

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

	//writer.WriteString(fmt.Sprintf("%s", Problem705A()))
	Problem1065A()
}

func Problem1065A() {
	t := getI()
	ans := make([]int64,t)
	for i := 0; i < t; i++ {
		money := getI64()
		limitForBonus := getI64()
		bonusChoco := getI64()
		priceChoco := getI64()

		primaryBuy := money / priceChoco
		optBonus := primaryBuy / limitForBonus
		totalBonus := optBonus * bonusChoco

		fmt.Println("primaryBuy totalBonus ", primaryBuy, totalBonus)
		ans[i] = primaryBuy + totalBonus
	}
	for dig := range ans {
		fmt.Println(ans[dig])
	}
}

package main

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

var scanner *bufio.Scanner

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

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

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

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

	//writer.WriteString(fmt.Sprintf("%s", Problem705A()))
	Problem1065A()
}

func Problem1065A() {
	t := getI()
	ans := make([]int,t)
	for i := 0; i < t; i++ {
		s := getI()
		a := getI()
		b := getI()
		c := getI()

		bonus := s/a * b
		buy := (s/a * c)
		left := (s - buy) * c

		ans[i] = buy + left + bonus
	}
	for dig := range ans {
		fmt.Println(ans[dig])
	}
}


package main

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

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

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

func getLine() string {
	s, _ := reader.ReadString('\n')
	if strings.HasSuffix(s, "\r\n") {
		s = s[:len(s)-2]
	} else if strings.HasSuffix(s, "\n") {
		s = s[:len(s)-1]
	}
	return s
}
func getLine2Int() (n int) {
	scanf("%d\n", &n)
	return
}
func getLine2Int2() (a, b int) {
	scanf("%d %d\n", &a, &b)
	return
}
func getLine2Ints(n int) []int {
	ans := make([]int, n)
	for i := 0; i < n; i++ {
		scanf("%d ", &ans[i])
	}
	scanf("%d\n", &ans[n-1])
	return ans
}

func pcheck(i bool) {
	if i {
		printf("YES\n")
	} else {
		printf("NO\n")
	}
}

var debug = true

func see(a interface{}) {
	if debug {
		printf("DEBUG:%v\n", a)
	}
}

func main() {
	defer writer.Flush()

	var n, a, b int
	scanf("%d %d %d\n", &n, &a, &b)
	s := getLine()
	if s[a-1] == s[b-1] {
		printf("0\n")
	} else {
		printf("1\n")
	}
}

package main

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

func main() {
	var n, a, b int
	if _, err := fmt.Scanf("%d %d %d\n", &n, &a, &b); err != nil {
		return
	}

	re := regexp.MustCompile(`\r?\n`)

	reader := bufio.NewReader(os.Stdin)
	if s, err := reader.ReadString('\n'); err != nil {
		if err != io.EOF {
			return
		}
	} else {
		s = re.ReplaceAllString(s, "")
		if a == b || s[a-1] == s[b-1] {
			fmt.Println(0)
		} else {
			fmt.Println(1)
		}
	}
}

package main

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

func main() {
	sc := NewScanner()
	_, a, b := sc.NextInt(), sc.NextInt(), sc.NextInt()

	S := sc.NextLine()

	a--
	b--

	if S[a] == S[b] {
		fmt.Println(0)
		return
	}

	fmt.Println(1)

}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r:rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf) + 1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf) + 1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main
import (
    "fmt"
)

func main(){
    var n, start, finish int
    var airports string
    fmt.Scan(&n, &start, &finish, &airports)
    if airports[start - 1] == airports[finish - 1] {
        fmt.Println(0)
    } else {
        fmt.Println(1)
    }
}

package main

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

func main() {
	n, a, b := read3i()
	str := readline()
	if len(str) < n {
		str = readline()
	}
	a--
	b--
	if str[a] == str[b] {
		fmt.Fprintln(writer, 0)
	} else {
		fmt.Fprintln(writer, 1)
	}
	writer.Flush()
}

func getPrimes(n int) []int {
	isPrime := make([]bool, n)
	for i := 2; i < len(isPrime); i++ {
		isPrime[i] = true
	}
	root := int(math.Sqrt(float64(n)))
	for i := 2; i <= root; i++ {
		if isPrime[i] {
			for j := i * i; j < n; j += i {
				isPrime[j] = false
			}
		}
	}
	primes := make([]int, 0)
	for i, v := range isPrime {
		if v {
			primes = append(primes, i)
		}
	}
	return primes
}

var (
	reader = bufio.NewReaderSize(os.Stdin, 1<<13)
	writer = bufio.NewWriterSize(os.Stdout, 1<<13)
)

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

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

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

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

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

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

func readi() int {
	return int(readi64())
}

func read2i() (int, int) {
	return readi(), readi()
}

func read3i() (int, int, int) {
	return readi(), readi(), readi()
}

func readis(n int) []int {
	res := make([]int, n)
	for i := range res {
		res[i] = readi()
	}
	return res
}

func readi64() int64 {
	b, err := reader.ReadByte()
	for !isValid(b, err) {
		b, err = reader.ReadByte()
	}
	sign, res := int64(1), int64(0)
	if b == '-' {
		sign *= -1
		b, err = reader.ReadByte()
	}
	for isValid(b, err) {
		res = res*10 + int64(b-'0')
		b, err = reader.ReadByte()
	}
	return res * sign
}

func readline() string {
	res := new(bytes.Buffer)
	for {
		line, isPrefix, err := reader.ReadLine()
		if err != nil {
			panic(err)
		}
		res.Write(line)
		if !isPrefix {
			break
		}
	}
	return res.String()
}

func isValid(b byte, err error) bool {
	return err == nil && (('0' <= b && b <= '9') || b == '-')
}

package main

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

func solve() {
    _, a, b := readInt(), readInt()-1, readInt()-1
    s := readString()
    ans := 0
    if s[a] != s[b] {
		ans = 1
    }
    fmt.Println(ans)
}

// Helpers

var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)

func main() {
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// Math

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

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

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

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

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

// Sort

type Ints64 []int64

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

/* Sort tempalte
func (a ) Len() int           { return len(a) }
func (a ) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ) Less(i, j int) bool { return  }
*/

package main

import (
	"fmt"
)

func main() {
	var n, a, b int64
	var s string
	fmt.Scan(&n, &a, &b, &s)

	r := 1
	if s[a-1] == s[b-1]{
		r = 0
	}

	fmt.Println(r)
}

package main

import(
"fmt"
)

func main() {
    var n,a,b int
    fmt.Scanf("%d %d %d",&n,&a,&b)
    var s string
    fmt.Scan(&s)
    eq := s[a-1] == s[b-1]
    if eq {
        fmt.Println(0)
    } else {
        fmt.Println(1)
    }
}

package main

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

func solve() {
    _, a, b := readInt(), readInt()-1, readInt()-1
    s := readString()
    ans := 0
    if s[a] != s[b] {
        cur := abs(a-b)
        for i := range s {
            if s[i] != s[b] {
                cur = min(cur, abs(i-b))
            } else {
                cur = min(cur, abs(i-a))
            }
        }
        ans = min(cur, 2)
    }
    fmt.Println(ans)
}

// Helpers

var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)

func main() {
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// Math

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

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

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

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

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

// Sort

type Ints64 []int64

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

/* Sort tempalte
func (a ) Len() int           { return len(a) }
func (a ) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ) Less(i, j int) bool { return  }
*/

package main

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

func solve() {
    _, a, b := readInt(), readInt()-1, readInt()-1
    s := readString()
    ans := 0
    if s[a] != s[b] {
        cur := abs(a-b)
        for i := range s {
            if s[i] != s[b] {
                cur = min(cur, abs(i-b))
            }
        }
        ans = cur
    }
    fmt.Println(ans)
}

// Helpers

var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)

func main() {
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// Math

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

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

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

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

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

// Sort

type Ints64 []int64

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

/* Sort tempalte
func (a ) Len() int           { return len(a) }
func (a ) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ) Less(i, j int) bool { return  }
*/

package main

import (
    "fmt"
    "math"
)

func abs(a , b int) int {
    return int(math.Abs(float64(a-b)))
}

func main() {
    var n,a,b int
    fmt.Scanf("%d %d %d",&n,&a,&b)
    var s string
    fmt.Scan(&s)
    //company 1
    dA , dB := 1000000, 10000000
    for i:= 1; i <= n; i++ {
        if s[i-1] == '1' {
            if abs(i,a) < dA {
                dA = abs(i,a)
            }
            if abs(i,b) < dB {
                dB = abs(i,b)
            }
        }
    }
    dis := dA + dB
    dA , dB = 1000000, 10000000
    for i:= 1; i <= n; i++ {
        if s[i-1] == '0' {
            if abs(i,a) < dA {
                dA = abs(i,a)
            }
            if abs(i,b) < dB {
                dB = abs(i,b)
            }
        }
    }
    if dA + dB < dis {
        dis = dA + dB
    }
    fmt.Println(dis)
}

package main

import(
"fmt"
)

func main() {
    var n,a,b int
    fmt.Scanf("%d %d %d",&n,&a,&b)
    var s string
    fmt.Scan(&s)
    allEq := true
    for i:=1; i < n; i++ {
       if s[i] != s[i-1] {
            allEq = false
            break
        }
    }
    if allEq {
        fmt.Println(0)
    } else {
        fmt.Println(1)
    }
}