You are given two arrays of size \(N\). Array 1 is \(val\) denoting values, and Array 2 is \(h\) denoting heights. At every position of the array, we can either pick the value \(val_i\) or increase the current height by \(h_i\). You are allowed to perform exactly 1 move at each position of array.
You are provided with \(M\) indexes \(I_i\). For each of these indexes, you are given with \({min\_height}_i\), which denotes the minimum height required to reach that index. Our height must be minimum of \({min\_height}_i\) before reaching the position \(i\) . If our height is less than \({min\_height}_i\) at index \(I_i\), we can not jump to index \(I_i\).
Note: All the indices \(I_i\) are distinct.
Your aim is to maximise the sum of \(val_i\) picked, at the end of the array, subject to given constraints. You need to print -1, incase there is no way of reaching the end of the array.
Input
The first line of input contains a number \(T\) that denotes the total number of test cases.
The first line for each test case consists of a number \(N\).
The next line contains \(N\) space separated integers corresponding to \(val_i\).
The next line contains \(N\) space separated integers corresponding to \(h_i\).
The next line contains a single number \(M\).
The next line contains \(M\) space separated integers corresponding to \(I_i\).
The next line contains \(M\) space separated integers corresponding to \({min\_height}_i,\).
Output
Output a single integer corresponding to maximum possible sum of \(val_i\) picked, at the end of the array, subject to given constraints.
Constraints
Initially height=0, sum=0.
At index 0, we dont have any constraint on height. We move up by 4 at this step. Therefore, height=4 and sum=0.
At index 1, we should have height of atleast 3. As we are at height 4, this constraint is satisfied. At this stage, we pick the value. Therefore, height=4 and sum=4.
At index 2, we should have height of atleast 2. As we are at height 4, this constraint is satisfied. At this stage, we pick the value. Therefore, height=4 and sum=7.
At index 3, we dont have any constraint on height. At this stage, we pick the value. Therefore, height=4 and sum=9.
Therefore, the maximum possible sum of values picked is 9.
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor
Login to unlock the editorial
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor