Maximum sum on a tree
Easy
Execution time limit is 1 second
Runtime memory usage limit is 128 megabytes
Given a tree with vertices, where the vertex numbered contains coins. Select a subset of vertices such that no two of them are adjacent (i.e., connected by an edge), and the sum of coins in the selected vertices is maximized.
Input
The first line contains the number of vertices in a tree. Each of the next lines contains two integers and , defining an edge in the tree. The last line contains non-negative integers — the number of coins in each vertex of the tree.
Output
Print the maximum possible sum of coins that can be obtained by selecting a subset of vertices in the tree with no adjacent vertices.
Examples
Input #1
Answer #1
Input #2
Answer #2
Submissions 9K
Acceptance rate 26%