#P8625. [蓝桥杯 2015 省 B] 生命之树
[蓝桥杯 2015 省 B] 生命之树
Description
In the forest, God created the Tree of Life.
He labeled each node of the tree (a leaf is also a node) with an integer, which represents the harmony value of that node.
God wants to choose a set of nodes (the empty set is allowed) such that for any two nodes in , there exists a sequence of nodes where every node in the sequence is an element of , and every pair of adjacent nodes in the sequence is connected by an edge.
Under this condition, God wants to maximize the sum of the integers corresponding to the nodes in .
This maximum sum is the score God gives to the Tree of Life.
With atm's efforts, he already knows the integer on each node of the tree. However, since atm is not good at calculations, he does not know how to compute the score efficiently. He needs you to write a program to compute the score of a tree.
Input Format
The first line contains an integer , indicating that the tree has nodes.
The second line contains integers, in order, representing the score of each node.
The next lines each contain integers , indicating that there is an edge between and . Since this is a tree, there are no cycles.
Output Format
Output one number in a single line, representing the score God gives to this tree.
5
1 -2 -3 4 5
4 2
3 1
1 2
2 5
8
Hint
For of the testdata, .
For of the testdata, , and the absolute value of each node's score does not exceed .
Time limit: 3 seconds. Memory limit: 256 MB.
Lanqiao Cup 2015 Provincial Contest B Group, Problem J.
Translated by ChatGPT 5
京公网安备 11011102002149号