Road Repair
Time limit1sMemory limit256 MB
Find a tree path with total cost at most C that maximizes total benefit.
- Level
Hard8 of 10
- Topics
- Tree, Divide and conquer, Dynamic programming
- Solved
- No attempts yet
Problem
A city has a road network in which every road connects two districts. The network is a tree, so between any two districts there is exactly one path. A path here is the sequence of roads you travel along to get from one district to the other.
Many cars use the roads every day, so the roads wear out badly. The city transport department repairs them on a regular schedule. Every road has a cost and a benefit . Repairing costs and yields the social benefit .
Suppose you repair every road on a path. The cost of the path is the sum of the costs of its roads, and the benefit of the path is the sum of their benefits. Given an upper bound on cost, find a path whose cost is at most and whose benefit is as large as possible.
Input
Your program reads from standard input. The input holds test cases, and the first line gives .
The first line of each test case has , the number of districts in the network (). The districts are numbered through , and the network always has exactly roads. Each of the next lines has four integers , , and , meaning that the road joining districts and has cost and benefit (, ). The last line gives the cost bound ().
Output
Your program writes to standard output. Print one line for each test case. The line holds a single integer, the maximum benefit a path can obtain while its cost is at most . If no such path exists, print .