Domain: Algorithms & Data Structures

You are given a tree with `n` nodes. Node `i` has an allowed initial value interval `[l_i, r_i]`.
You choose initial values `a_i` such that:
`l_i ≤ a_i ≤ r_i`

A tree is **balanced** if all node values are equal; the balanced value is that common value.

**Operation:** choose two nodes `u` and `v`. Consider the tree rooted at `u`. Increase by `1` the values of all nodes in the subtree of `v` (in that rooting). You may choose `u = v`. Values can only increase.

Find the **minimum possible** final balanced value after any number of operations.

### Input
- `n` (`1 ≤ n ≤ 2·10^5`)
- `n` lines: `l_i, r_i` (`0 ≤ l_i ≤ r_i ≤ 10^9`)
- `n-1` edges forming a tree

### Output
One integer: the minimum achievable balanced value.

Grading rubric:
criterion: Does the answer state that for an edge between parent p and child v (in a chosen rooting), choosing (u=p,v=v) increases (a_v−a_p) by 1 and choosing (u=v,v=p) decreases (a_v−a_p) by 1 (i.e., one operation changes the edge difference by exactly ±1)?
weight: 10
criterion: Does the answer state that for fixed initial values a, making a_v = a_p on an edge (p,v) requires exactly |a_v−a_p| operations affecting that edge (because each such operation changes the difference by 1)?
weight: 10
criterion: Does the answer give the final balanced value for a fixed rooting at 1 as F = a_1 + sum_{v≠1}?
weight: 10
criterion: Does the answer define the bottom-up choice of initial values as a_v = min(r_v, max(l_v, max_{child u of v}))?
weight: 10
criterion: Does the answer justify that if r_v allows a_v ≥ max_child, then choosing a_v below max_child cannot improve the objective (it only increases required increments on edges to children)?
weight: 10
criterion: Does the answer explicitly handle the clipped case: if r_v < max_child, then a_v is set to r_v and the deficit (max_child − r_v) is necessarily paid later via the term max(0, a_child − a_v)?
weight: 10
criterion: Does the answer describe computing the chosen a_v values using a postorder DFS from root 1 (children processed before parent)?
weight: 10
criterion: Does the answer describe computing the final answer by accumulating sum_{v≠1}?
weight: 10
criterion: Does the answer state an implementation complexity of O(n) time and O(n) memory and mention using 64-bit integers for the answer?
weight: 10

Answer:
### Expert Solution

Root the tree at node `1`. Let `p(v)` be the parent of `v`.

---

### A) Final balanced value for fixed initial values `a`

Consider an edge `(p, v)`.

An operation “increment subtree of `v` when rooted at `u`” can be used in two relevant ways for this edge:

- Choose `u = p`, `v = v`: increments the `v`-side component, so `(a_v - a_p)` **increases by 1**.
- Choose `u = v`, `v = p`: increments the `p`-side component (the side containing node `1`), so `(a_v - a_p)` **decreases by 1**.

So each operation changes the edge difference by exactly `±1`, hence equalizing the edge requires exactly `|a_v - a_p|` operations affecting it.

If `a_v > a_p`, to make them equal we must decrease `(a_v - a_p)` to `0`, which forces incrementing the **root side** exactly `a_v - a_p` times, increasing the final common value by that amount.
If `a_v ≤ a_p`, we can equalize by incrementing only the `v`-side, which does **not** increase the root-side final value.

Therefore the final balanced value (with root fixed at `1`) is:
`F(a) = a_1 + ∑_{v ≠ 1} max(0, a_v - a_{p(v)})`

---

### B) Optimal choice of initial values `a_v ∈ [l_v, r_v]`

Compute `a` bottom-up. For node `v`, let
`M_v = max_{u child of v} a_u`
(with `M_v = -∞` for a leaf).

If `r_v` allows `a_v ≥ M_v`, choosing `a_v < M_v` only increases the required increments on edges to children (since some child would exceed `v` more), so it cannot reduce `F(a)`. Thus we pick:
`a_v = min(r_v, max(l_v, M_v))`

If `r_v < M_v`, we are forced to set `a_v = r_v`; the unavoidable deficit is then paid later via terms `max(0, a_u - a_v)` for the too-large children.

---

### Algorithm

1. Root at `1` and run a postorder DFS to compute
   `a_v = min(r_v, max(l_v, max_{child} a_{child}))`

2. Compute
   `ans = a_1 + ∑_{v ≠ 1} max(0, a_v - a_{p(v)})`

Use 64-bit integers.

---

### Complexity

`O(n) time`, `O(n) memory`
which fits `n ≤ 2·10^5`.