Metro

All submissions for this problem are available.
The city of Chefland has a metro network of $n$ stations where there are metro tunnels between certain pairs of stations. The budget was tight during construction, so the $n$ stations are connected using only $n  1$ tunnels. Every station is still reachable from every other station. Being initially constructed with such a tight budget, it is no surprise that they have fallen into disrepair over time. Chef, the mayor of Chefland, has secured $k$ dollars for renovating the metro stations. Each metro station is different and the $i^{th}$ station requires $a_i$ dollars to renovate. But, if one or more stations directly connected to the $i^{th}$ station are also being renovated, it is possible to share certain resources between the stations and the cost of renovating the $i^{th}$ station drops to $b_i$ dollars. Help Chef figure out what is the maximum number of stations he can renovate using $k$ dollars. ### Input  The first line contains $t$, the number of test cases. $t$ cases follow.  The first line of each test case contains two integers $n$ and $k$.  The next line contains $n$ integers $a_1, a_2, \dots, a_n$.  The next line contains $n$ integers $b_1, b_2, \dots, b_n$.  $n  1$ lines follow, each with a pair of integers $u$ and $v$ denoting that the $u^{th}$ and $v^{th}$ stations are connected by a tunnel. ### Output  For each testcase, output a single line containing the maximum number of stations that can be renovated. ### Constraints  $1 \leq t \leq 200$  $2 \leq n \leq 5000$  $1 \leq k \leq 10^{12}$  $1 \leq b_i < a_i \leq 10^8$  $1 \leq u, v \leq n$, $u \ne v$  Sum of $n$ over all test cases does not exceed $5000$. ### Sample Input: 3 2 5 5 10 3 3 1 2 4 9 5 10 3 4 1 5 2 2 1 2 2 3 3 4 6 15 20 20 20 20 20 20 3 1 2 10 4 3 1 2 1 3 1 4 4 5 5 6 ### Sample Output 1 3 5 ### Explanation **Case 1:** Renovating only station 1 costs $a_1 = 5$. Renovating only station 2 costs $a_2 = 10$. Renovating both 1 and 2 costs $b_1 + b_2 = 3 + 3 = 6$. Only station 1 can be renovated with 5 dollars. ![sample1](https://codechef_shared.s3.amazonaws.com/download/HYC/External_contest_i...) **Case 2:** Stations 1, 3 and 4 can be renovated for a total cost of $a_1 + b_3 + b_4 = 5 + 2 + 2 = 9$. Stations 2, 3, 4 can also be renovated with 9 dollars. It is not possible to renovate all 4 stations with 9 dollars or less. ![sample1](https://codechef_shared.s3.amazonaws.com/download/HYC/External_contest_i...) **Case 3:** Stations 1, 2, 3, 5 and 6 can be renovated at a cost of $b_1 + b_2 + b_3 + b_5 + b_6 = 3 + 1 + 2 + 4 + 3 = 13$. More than 5 stations cannot be renovated with 15 dollars or less. ![sample1](https://codechef_shared.s3.amazonaws.com/download/HYC/External_contest_i...)Author:  meooow 
Editorial  https://discuss.codechef.com/problems/CLMTRO 
Tags  cole2019, dp+trees, medium, meooow 
Date Added:  4022019 
Time Limit:  1.5 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, rust, SCALA, swift, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, kotlin, PERL6, TEXT, SCM chicken, PYP3, CLOJ, COB, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions
HELP
If you are still having problems, see a sample solution here. 