Domain: Algorithms & Data Structures

You are given an undirected tree with `n` vertices numbered `1` to `n`. Vertex `i` has an integer value `a_i`.

You may perform the following operation any number of times:

- Choose a vertex with current degree `≤ 1` and delete it (and its incident edge if it exists).

You may delete all vertices.

After deletions, perform **compression**:

- While there exists a vertex of degree exactly `2`, delete it and connect its two neighbors by an edge.

Repeat until no degree-2 vertices remain. The final compressed tree is unique (order-independent).

Compute the maximum possible **sum of values of vertices remaining after compression**.

### Input
- `n` (`2 ≤ n ≤ 5·10^5`)
- `a_1, ..., a_n` (`-10^9 ≤ a_i ≤ 10^9`)
- `n-1` undirected edges `u v` forming a tree

### Output
One integer: the maximum achievable sum.

Grading rubric:
criterion: Does the answer explicitly state: in the chosen remaining subtree S (before compression), a vertex contributes a_v to the final sum iff deg_S(v) ≠ 2 (and contributes 0 iff deg_S(v)=2 because it is removed during compression)?
weight: 20
criterion: Does the answer formally define dp1[v] as the maximum obtainable final sum using only vertices in subtree(v) under the condition that the edge (parent(v),v) is kept in the remaining subtree S?
weight: 10
criterion: Does the answer state the dp1[v] candidate for keeping 0 child edges as a_v (degree in S equals 1, so v is not compressed)?
weight: 10
criterion: Does the answer state the dp1[v] candidate for keeping exactly 1 child edge as max_u dp1[u] and explicitly exclude adding a_v (degree in S equals 2, so v is compressed)?
weight: 10
criterion: Does the answer state the dp1[v] candidate for keeping ≥2 child edges as a_v + bestSum_{≥2}?
weight: 10
criterion: Does the answer define the component (no-parent-edge) candidate for keeping 0 child edges as a_v (degree 0, v remains)?
weight: 10
criterion: Does the answer define the component candidate for keeping exactly 1 child edge as a_v + best1 (degree 1, v remains)?
weight: 10
criterion: Does the answer define the component candidate for keeping exactly 2 child edges as best2 and explicitly exclude adding a_v (degree 2, v is compressed)?
weight: 10
criterion: Does the answer state the final output is max(0, maximum over all vertices of the component candidates) to allow deleting all vertices?
weight: 10

Answer:
## Expert Solution

Root the tree at node `1`. Any sequence of leaf deletions leaves a connected subtree `S` (possibly empty).

#### Key characterization (compression effect)
Compression repeatedly deletes every vertex of degree exactly `2`. It never deletes vertices of degree `0`, `1`, or `≥ 3`. Hence:

**A vertex `v` contributes `a_v` to the final sum iff `deg_S(v) ≠ 2`;**
**if `deg_S(v) = 2` then `v` is removed and contributes `0`.**

---

## DP definition
Let `parent(v)` and `children(v)` be w.r.t. the rooting.

`dp1[v] = maximum final (post-compression) sum using only subtree(v)`
**under the condition that** edge `(parent(v), v)` is kept in `S`.

Then `v` has a parent-edge, so:
`deg_S(v) = 1 + #{kept child edges (v, u)}`

For each child `u`, either keep `(v, u)` (gain `dp1[u]`) or delete that child side (gain `0`).
Let `b_u = dp1[u]` for children `u`.

---

## Deterministic helper values from `{b_u}`
- `best1 = max(b_u)` (requires `≥ 1` child)
- `best2 =` sum of the two largest `b_u` (requires `≥ 2` children)
- `bestSum_{≥k}` for `k ∈ {2, 3}`: maximum sum over any subset of children of size `≥ k`

Compute `bestSum_{≥k}` deterministically:
Let `P = ∑_{u: b_u > 0} b_u`, and `p = #{u: b_u > 0}`.
- If `p ≥ k`: `bestSum_{≥k} = P`
- Else: `bestSum_{≥k} = P +` (sum of the `k - p` largest values among the non-positive `b_u`).

This is optimal: take all positive contributions, then add the least harmful remaining values until reaching size `k`.

---

## Transition for `dp1[v]`
Let `k` be the number of kept child edges.

1) `k = 0`: `deg_S(v) = 1 ⇒ v` is **not compressed**, so include `a_v`:
   `cand0 = a_v`

2) `k = 1`: `deg_S(v) = 2 ⇒ v` **is compressed**, so **exclude `a_v`**:
   `cand1 = best1`

3) `k ≥ 2`: `deg_S(v) ≥ 3 ⇒ v` is **not compressed**, so include `a_v`:
   `candGe2 = a_v + bestSum_{≥2}`

Therefore:
`dp1[v] = max(cand0, cand1 (if exists), candGe2 (if exists))`

---

## Component candidates (no parent edge)
A remaining subtree `S` can be entirely inside `subtree(v)` without keeping the parent edge. Then `deg_S(v)` equals the number of kept child edges, giving four explicit cases:

- keep 0 children: `deg = 0 ⇒ v` remains
  `val0 = a_v`
- keep 1 child: `deg = 1 ⇒ v` remains
  `val1 = a_v + best1`
- keep 2 children: `deg = 2 ⇒ v` is compressed (**exclude `a_v`**)
  `val2 = best2`
- keep `≥ 3` children: `deg ≥ 3 ⇒ v` remains
  `valGe3 = a_v + bestSum_{≥3}`

Let `ans` be the maximum of these component candidates over all vertices `v`.

Finally, deleting all vertices is allowed, so the output is:
`max(0, ans)`

Compute `dp1[v]` in postorder and update `ans` at each vertex. Use 64-bit integers.