Chase
Time limit4sMemory limit512 MB
Jerry walks a simple path in a tree, dropping up to v breadcrumbs that zero out neighbor pigeon counts; maximize the pigeons Tom later meets minus the pigeons Jerry met.
- Level
Hard8 of 10
- Topics
- Tree, Dynamic programming, DFS
- Solved
- No attempts yet
Problem
Tom the cat is chasing Jerry the mouse again. Jerry wants an advantage, so he runs into crowds of pigeons where Tom has a harder time following him. He has just arrived at the central park of Ljubljana. The park has statues, numbered to , and passages that do not cross each other and connect the statues so that every statue can be reached from every other statue. Around statue there are pigeons.
Jerry has breadcrumbs in his pocket. If he drops a breadcrumb at the statue he is standing at, the pigeons of every statue that a passage connects directly to it immediately fly to this statue to eat the crumb. As a result the number of pigeons around this statue and around its neighbours changes.
Everything happens in this order. First Jerry arrives at statue and meets the pigeons that are there. Then he drops the breadcrumb. Then he leaves the statue. The pigeons of the neighbouring statues move to statue before Jerry arrives at the next statue, so they do not count toward the number of pigeons he meets.
Jerry may enter the park at any statue and run along passages, but he may never use the same passage twice, and he leaves the park wherever he wants. After Jerry exits the park, Tom enters and walks exactly the same route. By dropping at most breadcrumbs, Jerry wants to maximise the difference between the number of pigeons Tom meets on the route and the number of pigeons he meets himself. Only the pigeons that are at a statue right before Jerry arrives there count toward his own total.
Input
The first line contains the number of statues and the number of breadcrumbs . The second line contains integers to , separated by spaces. Each of the next lines contains two integers and , which mean that a passage connects statue and statue .
Output
Print one integer, the maximum difference between the number of pigeons Tom meets and the number of pigeons Jerry meets.
Constraints
Note
In the first example one optimal route is the following. Jerry enters the park at statue 6 and meets 5 pigeons there. He drops a breadcrumb, so becomes 27 and . Next he runs to statue 7 and meets 0 pigeons. He drops the second breadcrumb, so becomes 41 and . He exits the park. He met pigeons. Tom follows the same route and meets pigeons. The difference is .