Tree Advertising
Time limit1sMemory limit1024 MB
Root a tree at city 1 and choose a set of edges within budget so that the total population of cities whose path to the root contains a chosen edge is maximized.
- Level
Medium7 of 10
- Topics
- Tree, Dynamic programming, DFS
- Solved
- No attempts yet
Problem
Azerbaijan has cities, numbered through , connected by roads so that every city can be reached from every other city along a sequence of roads. The IOI will soon be held in the capital Baku (city ), and everyone in the country will drive from their home city to the capital to watch the competition.
You want to use this occasion to advertise your new, very clever competitive programming judge by putting up posters on many trees along various roads. If you put up posters along a road, everyone who travels along that road at some point during their trip from their home city to the capital will see the poster.
You have judged that it adds nothing if a person sees your posters more than once during their trip. Your judge is so impressive that everyone wants to use it after seeing the poster a single time! Each road has a cost for putting up posters on all trees along it, each city has a population, and you have a limited budget. If you put up posters optimally, what is the largest number of people who will see at least one poster during their trip to the capital?
Input
The first line contains two integers: the number of cities () and your budget in kronor ().
The second line contains the numbers (). is the number of people who live in city .
The following lines describe all the roads in Azerbaijan. The -th of these lines contains the integers () and (), meaning that the -th road runs between cities and and costs kronor to put up posters along.
It is guaranteed that all cities can be reached from each other using these roads.
Output
Print a single number: the largest number of people who can see your posters, if you place them optimally.
Hint
In the first example, it is optimal to put up one poster on the road between city and city , and one between city and city . This costs (which fits within the budget of ), and makes the people in cities , , , and see the advertisement, for a total of people.