Resourceful states
Practice
0 (0 votes)
Sparse table
Advanced data structures
Centroid decomposition
Medium Hard
Data structures
Problem
41% Success 102 Attempts 50 Points 10s Time Limit 1024MB Memory 1024 KB Max Code

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 \((n-1)\) 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 \(n\)\(m\), 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 \(i^{th}\) integer represents the number of resources in the \(i^{th}\) state
    • Next \(n-1\) 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 \(i^{th}\) integer is \(1\), then you can start your journey from \(i^{th}\) 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

  • \(1 \le t \le 50\)
  • \(1 \le n \le 10^6\)
  • \(1 \le m,k \le n\)
  • \(1 \le u,v \le n\)
  • \(-10^9 \le p_i \le 10^9\)
  • Sum of \(n\) over all test cases \(\le10^6\)

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

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
2 votes
Tags:
Data StructuresApprovedMedium-HardBinary search algorithmData Structures
Points:50
2 votes
Tags:
Data StructuresApprovedMedium-HardData Structures
Points:50
13 votes
Tags:
Binary SearchSparse tableSparse TableAdvanced Data StructuresData Structures