You are given two trees, each with N vertices. In both trees the vertices are numbered 0 through N−1, and the same number refers to the same vertex.
Vertex i has score Si.
Choose a subset of {0,1,…,N−1} that satisfies both of these conditions.
- Keeping only the chosen vertices in the first tree leaves a connected subgraph.
- Keeping only the chosen vertices in the second tree leaves a connected subgraph.
Write a program that finds the largest sum of scores over all such subsets. The empty subset is allowed and its sum is 0, so the answer is never negative.