Shefin is playing a video game. In this game, there will be buildings in a row. building has a height of and some gold coins on top of the building. At the beginning of each round, he will land on the top of a random building. When he is on top of a building, he can collect the coins from that building. He can also jump from one building to another. He can jump from building to building if the building is not taller than the building and there is no building between these two buildings which is taller than building.
There will be a total of rounds in the game. You will be given the index of the building on which Shefin will land at the start of each round. You have to tell the maximum amount of gold coins Shefin can collect in that round.
Note that each round is independent i.e. the coins are replenished with original values whenever a new round starts.
Input will start with an integer denoting the number of testcases in a single line.
Each testcase will start with an integer , the number of buildings in the game. Then there will be space-separated integers, of them is the height of building . In the next line, there will be another space-separated integers, of them is the amount of gold coins on the top of building .
The next line will contain an integer , the number of total rounds. In each of the next lines there will be an integer indicating the index of starting building of that round. The buildings are 1-indexed.
For each round, output the maximum number of gold coins Shefin can collect in a single line.
1 6 10 12 8 12 5 8 20 10 22 11 12 11 3 4 1 6
44 20 23
In the sample input-output:
In the first round: When Shefn starts from index 4, the efficient path will be 4 -> 2 -> 6 -> 5 and thus he will collect 44 gold coins, which is maximum possible.
In the second round: Starting from index 1 , he can't go to any other building because the 2nd building is taller than the 1st building. So he can collect only 20 gold coins from the building in index 1.
In the third round: Starting from index 6, the efficient path will be 6 -> 5 and thus he will be able to collect 23 gold coins, that is maximum possible.
Login to submit