#P9976. [USACO23DEC] Farmer John Actually Farms B

[USACO23DEC] Farmer John Actually Farms B

Problem Description

Farmer John is growing NN (1N21051 \leq N \leq 2\cdot 10^5) asparagus plants on his farm. However, some plants have genetic differences and grow faster than others. The initial height of plant ii is hih_i inches, and then every day, plant ii grows by aia_i inches.

FJ likes some of these plants more. He will give you an array t1,,tNt_1,\dots,t_N consisting of distinct integers; this array contains all integers from 00 to N1N-1. He wants exactly tit_i plants to be taller than plant ii. Find the minimum number of days needed to satisfy FJ’s requirement, or report that it is impossible.

Input Format

Each test contains multiple test cases.

The first line contains an integer TT, the number of test cases (1T101 \leq T \leq 10).

For each test case, the first line contains an integer NN (1N21051 \leq N \leq 2\cdot 10^5), the number of plants.

The second line contains NN integers hih_i (1hi1091 \leq h_i \leq 10^9), the initial height of plant ii.

The third line contains NN integers aia_i (1ai1091 \leq a_i \leq 10^9), the height increase per day of plant ii.

The fourth line contains NN distinct integers tit_i, the array given by FJ.

It is guaranteed that the sum of NN over all test cases does not exceed 21052\cdot 10^5.

Output Format

Output TT lines, each line containing the answer for one test case. If the requirement is impossible to satisfy, output 1-1.

Note that because the integers in this problem can be very large, you may need to use 64-bit integer types (for example, long long in C/C++).

6
1
10
1
0
2
7 3
8 10
1 0
2
3 6
10 8
0 1
2
7 3
8 9
1 0
2
7 7
8 8
0 1
2
7 3
8 8
1 0
0
3
2
5
-1
-1
2
5
7 4 1 10 12
3 4 5 2 1
2 1 0 3 4
5
4 10 12 7 1
3 1 1 4 5
2 4 3 1 0
4
7

Hint

Sample Explanation 1

In the first sample, there are 66 test cases.

In the first test case, there is only one plant, so the requirement is already satisfied on day 00.

In the second test case, the first plant needs to be shorter than the second plant. After day 11, their heights are 15,1315, 13; after day 22, their heights are both 2323; after day 33, their heights are 31,3331, 33, which is the first day that satisfies the requirement.

The third and fourth test cases are similar to the second test case.

In the fifth test case, both plants start at 77 inches and both grow by 88 inches per day, so their heights are always the same. Therefore, the condition can never be satisfied.

In the sixth test case, the initial heights do not satisfy the requirement and the growth rates are the same, so the condition can never be satisfied.

Sample Explanation 2

In the second sample, there are 22 test cases.

In the first test case, the final heights after day 44 are 19,20,21,18,1619, 20, 21, 18, 16.

In the second test case, the final heights after day 77 are 25,17,19,35,3625, 17, 19, 35, 36.

Test Point Properties

  • Test point 33 satisfies N2N \le 2.
  • Test points 454-5 satisfy N50N \le 50, and ai,hi103a_i, h_i \le 10^3.
  • Test points 686-8 satisfy N103N \le 10^3.
  • Test points 9139-13 have no additional constraints.

Translated by ChatGPT 5