All Tracks Data Structures Advanced Data Structures Problem

Resourceful states

Advanced Data Structures, Centroid Decomposition, Data Structures, Medium-Hard, Sparse Table


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\)
10 4 3
5 -2 9 -1 9 2 2 3 -5 3
1 2
1 7
2 3
2 4
7 8
7 9
4 5
4 6
5 10
1 0 0 0 1 0 1 0 1 0

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.

Time Limit: 10.0 sec(s) for each input file.
Memory Limit: 1024 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: Bash, C, C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Julia, Kotlin, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Swift, Swift-4.1, TypeScript, Visual Basic


Initializing Code Editor...
Your Rating:


This Problem was Asked in


Challenge Name

HourStorm #6

View All Notifications