You have been given ‘SKILL’ and ‘AGE’ of ‘N’ players. You want to choose the team with the highest total skill. The total skill is the sum of skills of all players in the team. However, in a team, no two players must exist such that the younger player has greater skill than the older player. In a team, two people of the same age can have different skill levels. Return the highest total skill of the team which is possible.
Example:Let’s say the age of players is [1,2,6,4] and the skill of players is [1,2,3,4]. We cannot take all players in the team as 3rd player is older than 4th player but 4th player has strictly greater skill than 3rd player. Therefore we can take anyone among them. Therefore the highest total skill of team which is possible is 7.
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case contains a single integer ‘N’ representing the number of players.
The second line of each test case contains ‘N’ single space-separated integers representing the age of players.
The third line of each test case contains ‘N’ single space-separated integers representing the skill of players.
Output Format:
For each test case, print a single integer denoting the highest total skill of the team which is possible.
The output of each test case will be printed in a separate line.
Note:
You do not need to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= N <= 100
1 <= ‘AGE[i]’ <= 10^3
1 <= ‘SKILL[i]’ <= 10^6
Time Limit: 1 sec
2
4
1 2 3 4
1 2 3 4
5
1 2 3 4 5
1 1 2 1 2
10
6
Test case 1:
We can take all the players as the player with greater age has greater skill.
Therefore the answer is 10.
Test case 2:
We can either take 3rd or 4th player as both of them cannot be part of the same team as the 3rd player is younger and has a greater skill. We will prefer 3rd player as we need maximum total skill.
Therefore the answer is 6.
2
4
1 2 6 4
1 2 3 4
4
1 2 3 4
4 3 2 1
7
4
Iterate over all possible teams.
Make an auxiliary vector/list ‘player’ which store age as its first parameter and skill as the second parameter. Sort the list with age as a priority and if age is the same then on basis of skill. Then iterate over all the possible teams recursively.
We will apply the algorithm as follows:-
O(2^N), where ‘N’ denotes the number of players.
We are iterating over all the possible teams.
O(N), where ‘N’ denotes the number of players.
We build an auxiliary vector/list to store ‘AGE’ and ‘SKILL’ of the player