CF BUDDY
← Problems·

1873F · Money Trees

1300 · binary search, greedy, math

Problem: Luca is in front of a row of nn trees. The ii-th tree has aia_i fruit and height hih_i.

He wants to choose a contiguous subarray of the array [hl,hl+1,,hr][h_l, h_{l+1}, \dots, h_r] such that for each ii (li<rl \leq i < r), hih_i is divisible^{\dagger} by hi+1h_{i+1}. He will collect all the fruit from each of the trees in the subarray (that is, he will collect al+al+1++ara_l + a_{l+1} + \dots + a_r fruits). However, if he collects more than kk fruits in total, he will get caught.

What is the maximum length of a subarray Luca can choose so he doesn't get caught?

^{\dagger} xx is divisible by yy if the ratio xy\frac{x}{y} is an integer.

Input Format: The first line contains a single integer tt (1t10001 \leq t \leq 1000) — the number of test cases.

The first of each test case line contains two space-separated integers nn and kk (1n21051 \leq n \leq 2 \cdot 10^5; 1k1091 \leq k \leq 10^9) — the number of trees and the maximum amount of fruits Luca can collect without getting caught.

The second line of each test case contains nn space-separated integers aia_i (1ai1041 \leq a_i \leq 10^4) — the number of fruits in the ii-th tree.

The third line of each test case contains nn space-separated integers hih_i (1hi1091 \leq h_i \leq 10^9) — the height of the ii-th tree.

The sum of nn over all test cases does not exceed 21052 \cdot 10^5.

Output Format: For each test case output a single integer, the length of the maximum length contiguous subarray satisfying the conditions, or 00 if there is no such subarray.

Note: In the first test case, Luca can select the subarray with l=1l=1 and r=3r=3.

In the second test case, Luca can select the subarray with l=3l=3 and r=4r=4.

In the third test case, Luca can select the subarray with l=2l=2 and r=2r=2.

Sample Cases

Case 1

Input

5
5 12
3 2 4 1 8
4 4 2 4 1
4 8
5 4 1 2
6 2 3 1
3 12
7 9 10
2 2 4
1 10
11
1
7 10
2 6 3 1 5 10 6
72 24 24 12 4 4 2

Output

3
2
1
0
3

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback