#P7130. 「RdOI R1」平衡常数(balance)

「RdOI R1」平衡常数(balance)

Description

Given a rooted weighted tree G=(V,E)G=(V,E) with root 11, the weight of node ii is denoted by wiw_i. Let the set of nodes in the subtree rooted at ii be ViV_i. Find a node set VVV'\subseteq V that satisfies the following conditions:

  • For all ii, ViVVi2|V_i \cap V'| \le \lfloor \frac{|V_i|}{2} \rfloor.

  • Maximize iVwi\sum _{i \in V'} w_i.

You only need to output iVwi\sum _{i\in V'} w_i, i.e., the sum of the weights of the selected nodes.

Input Format

The first line contains a positive integer nn.

The second line contains nn positive integers wiw_i.

The next n1n-1 lines each contain two integers u,vu,v, representing the two endpoints of an edge.

Output Format

Output a single line containing the maximum total sum you computed.

3
1 2 3
1 2
1 3
1

Hint

Constraints

Test Point ID nn\leq wiw_i\leq Special Property
121\sim2 1010 10310^3
353\sim 5 2×1032 \times 10^3
6126\sim12 10510^5
131613\sim16 5×1055 \times 10^5 10910^9 v=u+1v=u+1
172517\sim25

For 100%100\% of the testdata, 1n5×1051 \leq n \leq 5 \times 10^5, 0<wi1090 < w_i \leq 10^9, 1u,vn1 \leq u,v \leq n.


Notes / Hints

  • Idea From: LCuter.

File Input/Output (simulation, not needed when submitting code)

  • File name: balance.cpp.
  • Input file name: balance.in.
  • Output file name: balance.out.

Translated by ChatGPT 5