Resourceful states

0

0 votes
Sparse Table, Advanced Data Structures, Centroid decomposition, Medium-Hard, Data Structures
Problem

A country has been divided into n states and each state has plenty of resources. All these states are connected with each other by using (n1) pathways. You can travel to any state from another state by using these pathways. Your task is to extract resources from states.

You are given a list of m states and you have to start your journey from any one of these states. After starting your journey, you will start extracting the resources from the states one by one while visiting them using the pathways. You are also provided with an energy level k where k is your initial energy level. This energy level is decreased by one whenever you use a pathway. If your energy level becomes zero, then you cannot extract resources anymore.

You will be provided with all the information of the country, list of the m states and the initial energy level k. Determine the maximum sum of resources that you can extract from the states during your journey. 

Note: You can not visit any state more than once during your journey. Besides, you can also select not to extract resources from any of the states. In that case, the total sum of resources will be zero.

Input format

  • First line: A single integer t that denotes the number of test cases.
  • For each t test case:
    • First line: Three integers nm, and k where n is the total number of states, m is the number of states you can start your journey from, and k is your initial energy level when you start your journey
    • Next line: An array p of n integers where the ith integer represents the number of resources in the ith state
    • Next n1 lines: Two integers u and v that indicates a pathway between the states u and v
    • Last line: n integers which are either 0 or 1. If the ith integer is 1, then you can start your journey from ith state otherwise you cannot start your journey from that state

Output format

For each of the test case, print a single integer that represents the maximum number of extracted resources.

Input Constraints

  • 1t50
  • 1n106
  • 1m,kn
  • 1u,vn
  • 109pi109
  • Sum of n over all test cases 106
Time Limit: 10
Memory Limit: 1024
Source Limit:
Explanation

You can start from state 5 and then extract from states 5,4,2 and 3. This way you will collect 15 resources in total.

Editor Image

?