Sum of Scores
Time limit2sMemory limit512 MB
Find a non-empty vertex subset connected in both given trees whose score sum is maximized, with vertex counts up to 50.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Tree, Bit manipulation
- Solved
- No attempts yet
Problem
You are given two trees and , each with vertices. In both trees the vertices are numbered 0 to , and vertex has score . Scores are integers and may be negative.
Choose a non-empty subset that satisfies both conditions below.
- Keeping only the vertices of in tree leaves a connected subgraph.
- Keeping only the vertices of in tree leaves a connected subgraph.
Among all such subsets , find the maximum value of the score sum .
Input
The first line contains the number of vertices . ()
Each of the next lines contains one edge of tree as two integers and . (, )
Each of the following lines contains one edge of tree in the same format.
The last line contains integers separated by spaces. ()
Both graphs in the input are trees.
Output
Print the maximum score sum on the first line.
Note
In the first example the score sum is largest for . Picking does not leave a connected subgraph in tree .