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

「RdOI R1」平衡常数(balance)

Problem 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 V′⊆VV'\subseteq V that satisfies the following conditions:

  • For all ii, ∣Vi∩V′∣≤⌊∣Vi∣2⌋|V_i \cap V'| \le \lfloor \frac{|V_i|}{2} \rfloor.

  • Maximize ∑i∈V′wi\sum _{i \in V'} w_i.

You only need to output ∑i∈V′wi\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 n−1n-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 n≤n\leq wi≤w_i\leq Special Property
1∼21\sim2 1010 10310^3
3∼53\sim 5 2×1032 \times 10^3
6∼126\sim12 10510^5
13∼1613\sim16 5×1055 \times 10^5 10910^9 v=u+1v=u+1
17∼2517\sim25

For 100%100\% of the testdata, 1≤n≤5×1051 \leq n \leq 5 \times 10^5, 0<wi≤1090 < w_i \leq 10^9, 1≤u,v≤n1 \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