-->

Frog Sort codechef February 2021  long challenge editorial -

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 $\left(i,j\right)$ 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},\dots ,{W}_{N}$.
• The third line contains $N$ space-separated integers ${L}_{1},{L}_{2},\dots ,{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\le T\le 2\cdot {10}^{4}$
• $2\le N\le 4$
• $1\le {W}_{i}\le N$ for each valid $i$
• $1\le {L}_{i}\le 5$ for each valid $i$
• no two frogs have the same weight

• $T=50$
• $N=2$

Subtask #2 (50 points): original constraints

Example Input

3
3
3 1 2
1 4 5
3
3 2 1
1 1 1
4
2 1 4 3
4 1 2 4


Example Output

3
6
5


Explanation

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.

Problem Statement-

Solution-

Hint - fix the minimum weight element and arrange the rest of elements.

Solution will be posted anytime soon stay connect .

Must join Telegram channel