#P11259. [GDKOI2023 普及组] 海星

[GDKOI2023 普及组] 海星

Problem Description

Xiao Ming wants to catch starfish on the seabed. The seabed is a tree-like structure, and the starfish are hidden inside it.

Given a tree with nn nodes, numbered 1,2,3,...,n1, 2, 3, ..., n, and the weight of node ii is denoted as pip_i.

A starfish is defined as a “flower subgraph” in the tree. Assume its center node is labeled OO, and its leaf (peripheral) nodes are labeled a1,a2,...,ata_1, a_2, ..., a_t (each leaf node must be directly connected to the center node, and the flower subgraph contains at least one center node and one leaf node). The value of this starfish is defined as pOpai|p_O - \sum p_{a_i} |.

Xiao Ming wants to know the maximum total value of starfish he can catch. (He may catch multiple starfish at the same time, but the intersection of the node sets of any two starfish must be empty.)

Additional definitions:

Flower graph: A connected graph with diameter less than or equal to 33. The node with the largest degree is the center node, and all other nodes are leaf (peripheral) nodes (so every leaf node has degree 11).

Example: The smallest flower graph is (G,V)=(1,2,(1,2))(G,V)=({1,2},{(1,2)}), which consists of only two nodes and the single edge connecting them.

Input Format

The first line contains a positive integer nn, representing the size of the tree.

The second line contains nn integers, representing pip_i.

Lines 33 to n+1n + 1 each contain two integers a,ba, b, indicating that there is an edge between node aa and node bb.

Output Format

Output one line containing one integer, representing the maximum total value of starfish Xiao Ming can catch.

5
-2 -3 4 -5 1
1 2
1 3
2 4
2 5
10
见/example/starfish/下
见/example/starfish/下

Hint

Sample Explanation

One valid plan is that Xiao Ming catches two starfish. The first starfish has center node 11 and leaf node 33, with value 66. The second starfish has center node 22 and leaf node 55, with value 44. In this case, the maximum total value is 1010.

Constraints

(1)(1) For 10%10\% of the testdata, the data is guaranteed to form a flower graph.

(2)(2) For 20%20\% of the testdata, the data is guaranteed to form a chain.

(3)(3) For 20%20\% of the testdata, it is guaranteed that the product of the weights of adjacent nodes is always negative.

(4)(4) For 20%20\% of the testdata, the data is guaranteed to form a binary tree rooted at 11.

(5)(5) For 30%30\% of the testdata, there are no special constraints.

For all testdata, 1a,bn1051 \le a, b \le n \le 10^5, 109pi108-10^9 \leq p_i \leq 10^8.

In the provided samples, data numbered ii and i+5i + 5 correspond to constraint group number ii, where i{1,2,3,4,5}i \in \{1, 2, 3, 4, 5\}.

Translated by ChatGPT 5