Collect Maximum Coins

4.1

7 votes
Tree, Depth First Search, Dynamic Programming, Algorithms, Trees
Problem

There are N cities in a country, numbered 1 through N. They are connected with N1 undirected roads in such a way that one can move from one city to any other city through these roads. Each city contains a box filled with coins. The ith city contains a box with Ai coins, and it requires Bi consecutive seconds to open the box. 


Alice is initially in city 1. While standing in city i(1iN), Alice can either:

  • move to an adjacent city in 1 second, or
  • spend Bi consecutive seconds to open the box in the  ith city and collect Ai coins.

Find the maximum number of coins Alice can collect in K seconds.

Alice can visit a city any number of times but can collect the coins only once from a city.

  Input format

  • The first line of input contains two integers N denoting the number of cities and K denoting total seconds available to collect the coins.
  • The second line contains N integers A1,A2,,AN, denoting the number of coins in the cities.
  • The third line contains N integers B1,B2,,BN, denoting the number of seconds to open the boxes.

  • N1 lines follow. The ith line contains two integers Xi,Yi denoting a road between the cities Xi,Yi. It is guaranteed that the roads and cities form a tree structure.

Output format
For each query, print the maximum number of coins Alice can collect in K seconds.

Constraints

1N,K3001Ai1051Bi3001Xi,YiNXiYi

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

During the first second, Alice moves to city 3. During the 2nd second, Alice collects 2 coins from city 3. During the third second, he moves to city 4 and collects 3 coins during the fourth and fifth second. So Alice collected total 2+3=5 coins. 

Editor Image

?