The Combat of Ages.

0

0 votes
Problem

Problem Statement

There is a war going on between N heroes. Each hero has some strength a[i] (1 ≤  i  ≤ N). You are also given an array b[i] which indicates the age of ith hero.  Moreover, each hero has some coins associated with them, initially, all of them have coins equal to their strength. As the older ones are more experienced, younger ones can’t beat them even if their strength is greater than the older ones. A hero can kill his opponent if strength of opponent is not greater than hero's strength and age of hero is greater than the opponent.

For example :- In a battle between 2 heroes X and Y, having strengths a[x] and a[y]  respectively and their ages are b[x] and b[y] respectively, hero X can kill hero Y if a[x]  a[y] and b[x] > b[y] and vice versa. However, in any other case, it will be considered as a draw.

Whenever a hero kills the other, he will take all the coins which his opponent had.

For example:- if hero X having 5 coins kills the hero Y having 10 coins then hero will be eliminated and X takes all the coins he had. So, the total number of coins that X has after the battle is 15.

You have to find out the maximum number of coins any of the heroes can have without violating any of the above conditions.

Note:- A battle between 2 heroes doesn’t change the initial strength of the heroes (it will remain constant throughout the war). The only thing which can change after the battle is the number of coins.

Input:-

The first line contains a single integer t (1 ≤ t ≤ 100) -- the number of test cases.
The first line of each test case contains an integer N (1 ≤ N ≤ 105) -- the number of heroes.
The second line of each test case contains N integers a1,a2,….. an (1 ≤  ai  ≤ 109) -- the strength of ith hero.
The third line of each test case contains N distinct integers b1,b2,…..bn(1  ≤  b≤  N)-- the age of ith hero.


Output:-


For each test case, print an integer that indicates the maximum number of coins a hero can have.

Time Limit: 5
Memory Limit: 256
Source Limit:
Explanation

In the first test case,

The strengths of the heroes are in the order: [4,5,27,8,2,7] and their ages are [3,6,4,2,5,1] 

Hero 1 can’t kill anyone.
Hero 2 can kill hero 1 or 5.
Hero 3 can kill hero 1 or 4 or 6.

Hero 4 can kill only hero 6.
Hero 5 can’t kill anyone.
Hero 6 can’t kill anyone.

Therefore, maximum coins:-

Hero 1 can have = 4 (his initial coins).
Hero 2 can have = 9 (5 coins(initially) + 4 coins(by killing hero 1)).
Hero 3 can have = 40 (25 coins(initially) +  15 coins(by killing hero 4 after hero 4 killed hero 6)).
Hero 4 can have = 15( 8 coins(initially) + 7 coins(by killing hero 6).
Hero 5 can have = 2 (his initial coins).
Hero 6 can have = 7 (his initial coins).
Therefore, the answer is 40 coins which can be achieved by hero 3 if hero 4 kills hero 6 first and takes his coins and after that hero 3 kills hero 4 and takes all of his coins.

Editor Image

?