P22444 [CodeChef] FROGS
普及
题目描述

Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.

There are $N$ frogs (numbered $1$ through $N$) in a line. For each valid $i$, the $i$-th frog is initially at the position $i$, it has weight $W_{i}$, and whenever you hit its back, it jumps a distance $L_{i}$ to the right, i.e. its position increases by $L_{i}$. The weights of the frogs are pairwise distinct.

You can hit the back of each frog any number of times (possibly zero, not necessarily the same for all frogs) in any order. The frogs do not intefere with each other, so there can be any number of frogs at the same time at each position.

Your task is to sort the frogs in the increasing order of weight using the smallest possible number of hits. In other words, after all the hits are performed, then for each pair of frogs $(i, j)$ such that $W_{i} < W_{j}$, the position of the $i$-th frog should be strictly smaller than the position of the $j$-th frog. Find the smallest number of hits needed to achieve such a state.

------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
The second line contains $N$ space-separated integers $W_{1}, W_{2}, \ldots, W_{N}$.
The third line contains $N$ space-separated integers $L_{1}, L_{2}, \ldots, L_{N}$.

------ Output ------
For each test case, print a single line containing one integer ― the smallest number of times you need to hit the backs of the frogs.

------ Constraints ------
$1 ≤ T ≤ 2 \cdot 10^{4}$
$2 ≤ N ≤ 4$
$1 ≤ W_{i} ≤ N$ for each valid $i$
$1 ≤ L_{i} ≤ 5$ for each valid $i$
no two frogs have the same weight

------ Subtasks ------
Subtask #1 (50 points):
$T = 50$
$N = 2$

Subtask #2 (50 points): original constraints

----- Sample Input 1 ------
3

3

3 1 2

1 4 5

3

3 2 1

1 1 1

4

2 1 4 3

4 1 2 4
----- Sample Output 1 ------
3

6

5
----- explanation 1 ------
Example case 1: We can hit the back of the first frog three times.

Example case 2: We can hit the back of the first frog four times, then hit the back of the second frog two times.

输入格式

见题面

输出格式

见题面

样例输入
3 3 3 1 2 1 4 5 3 3 2 1 1 1 1 4 2 1 4 3 4 1 2 4
样例输出
3 6 5