Training Plans

 Nathan is preparing for the Dash marathon. He has 

training plans. The -th plan has an effectiveness of , but requires that at least

other training plans must be performed before it. A training plan cannot be repeated.

If he performs some

distinct trainings - then his training score is . If Nathan does not perform any training plan , then his score is

.

What is the highest score that Nathan can get by performing zero or more training plans, if he can perform them in any order?

Input Format

  • The first line of the input contains a single integer
- the number of test cases. The description of
  • test cases follows.
  • The first line of each test case contains
  • - the number of training plans.
  • The second line contains
  • integers where is the effectiveness of the
  • -th training plan.
  • The third line contains
  • integers where is the number of pre-requisite training plans for the
    • -th training plan.

    Output Format

    • For each test case, output a single real number - the highest score that Nathan can get by performing zero or more training plans.
    • Your answer is considered correct if its absolute or relative error does not exceed
    . Formally, let your answer be , and the jury's answer be . Your answer is accepted if and only if
    • .

    Constraints

  • Sum of
  • over all test cases does not exceed
    • .

    Sample Input 1

    3
    4
    -9 -21 -4 -43
    0 0 0 0
    5
    10 14 5 9 1
    4 1 3 0 0
    7
    -1 101 5 63 -7 -88 59
    0 1 6 2 3 4 5
    

    Sample Output 1

    0.000000
    11.500000
    54.333333
    

    Explanation

    Test case 1: It is optimal for Nathan to not perform any training plans as all the plans have negative

    value.

    Test case 2: It is optimal for Nathan to:

    • First, perform the
    -th training plan (for which
  • ).
  • Then perform the
  • -nd training plan (for which which is satisfied as he has already performed
    • training plan)

    Test case 3: It is optimal for Nathan to:

    • First, perform the
    -st training plan (for which
  • ).
  • Then perform the
  • -nd training plan (for which which is satisfied as he has already performed
  • training plan)
  • Then perform the
  • -th training plan (for which which is satisfied as he has already performed training plans)
    Previous
    Next Post »