#P1352. 没有上司的舞会

    ID: 349 远端评测题 1000ms 128MiB 尝试: 2 已通过: 1 难度: 5 上传者: 标签>动态规划,dp搜索树形结构福建省历届夏令营记忆化搜索

没有上司的舞会

Description

A university has nn employees, numbered 1n1 \ldots n.

There is a hierarchy among them, meaning their relationships form a tree rooted at the university president, where a parent node is the direct supervisor of its child nodes.

There will be an anniversary banquet. Inviting an employee increases the happiness index by rir_i. However, if an employee’s direct supervisor attends the banquet, that employee will refuse to attend no matter what.

Please compute which employees to invite so that the total happiness index is maximized, and find the maximum total happiness index.

Input Format

The first line contains an integer nn.

Lines 22 to (n+1)(n + 1) each contain one integer. The integer on line (i+1)(i + 1) is the happiness index rir_i of employee ii.

Lines (n+2)(n + 2) to 2n2n each contain a pair of integers l,kl, k, meaning kk is the direct supervisor of ll.

Output Format

Output a single line with one integer representing the maximum total happiness index.

7
1
1
1
1
1
1
1
1 3
2 3
6 4
7 4
4 5
3 5

5

Hint

Constraints

  • For 100%100\% of the testdata, it is guaranteed that 1n6×1031 \leq n \leq 6 \times 10^3, 128ri127-128 \leq r_i \leq 127, 1l,kn1 \leq l, k \leq n, and the given relations form a tree.

Translated by ChatGPT 5