Observation 1: the path minimum is the LCA value
Let $$$x=\operatorname{LCA}(u,v)$$$.
Because the values strictly increase from parent to child, every vertex below $$$x$$$ has a value larger than $$$P_x$$$. Therefore
$$$ B(u,v)=P_x=P_{\operatorname{LCA}(u,v)}. $$$So we only need to reason about LCA values.
Observation 2: score of the original tree
Let $$$T_v$$$ be the subtree of $$$v$$$ and
$$$ s_v=|T_v|. $$$Define
$$$ w_r=P_r=1 $$$and, for $$$v\ne r$$$,
$$$ w_v=P_v-P_{\operatorname{parent}(v)}. $$$For any pair $$$u,v$$$,
$$$ P_{\operatorname{LCA}(u,v)} = \sum_{x\text{ on }r\to\operatorname{LCA}(u,v)}w_x. $$$The term $$$w_x$$$ is present exactly when both endpoints belong to $$$T_x$$$.
There are $$$\binom{s_x}{2}$$$ such pairs, hence
$$$ S_0 = P_r\binom N2 + \sum_{v\ne r} (P_v-P_{\operatorname{parent}(v)}) \binom{s_v}{2}. $$$This can be computed in $$$O(N)$$$.
Observation 3: loss of one cut
Consider cutting the edge between a non-root vertex $$$v$$$ and its parent.
Define
$$$ H_v= \sum_{x\notin T_v} P_{\operatorname{LCA}(v,x)}. $$$For every fixed vertex in $$$T_v$$$, its total contribution with all vertices outside $$$T_v$$$ is exactly $$$H_v$$$.
Since there are $$$s_v$$$ vertices in $$$T_v$$$, the loss is
$$$ Y_v=s_vH_v. $$$We can compute all $$$H_v$$$ using
$$$ H_v = H_{\operatorname{parent}(v)} + (s_{\operatorname{parent}(v)}-s_v) P_{\operatorname{parent}(v)}. $$$Let
$$$ X_v=s_v. $$$Then define the line
$$$ f_v(q)=Y_v-X_vq. $$$The reason for this definition becomes clear when considering two cuts.
Observation 4: two nested cuts
Suppose $$$a$$$ is an ancestor of $$$b$$$.
The two individual losses count some pairs twice.
A pair belongs to both losses exactly when one endpoint is inside $$$T_b$$$ and the other is outside $$$T_a$$$.
Therefore the overlap is
$$$ s_bH_a. $$$Hence
$$$ L(a,b) = Y_a+Y_b-s_bH_a. $$$Since $$$X_b=s_b$$$,
$$$ L(a,b) = Y_a+Y_b-X_bH_a = Y_a+f_b(H_a). $$$So if $$$a$$$ is fixed, we need the minimum value of all descendant lines at
$$$ q=H_a. $$$This is a standard Li Chao tree query.
Observation 5: two disjoint cuts
Now suppose $$$T_a$$$ and $$$T_b$$$ are disjoint.
Let
$$$ x=\operatorname{LCA}(a,b). $$$A pair is counted in both individual losses exactly when one endpoint is in $$$T_a$$$ and the other is in $$$T_b$$$.
Every such pair has bottleneck $$$P_x$$$, and there are
$$$ s_as_b $$$such pairs.
Therefore the overlap is
$$$ P_xs_as_b. $$$Thus
$$$ L(a,b) = Y_a+Y_b-P_xs_as_b. $$$For fixed $$$a$$$,
$$$ L(a,b) = Y_a+ \left( Y_b-X_b(P_xX_a) \right). $$$Again this is exactly our line
$$$ f_b(q)=Y_b-X_bq, $$$queried at
$$$ q=P_xX_a. $$$So both possible configurations of the two cuts have become minimum line queries.
Observation 6: small-to-large merging
Process the rooted tree bottom-up.
For each processed subtree, maintain:
- the list of cut vertices whose edges are available in this subtree;
- a Li Chao tree containing their lines.
At vertex $$$x$$$, choose the largest child structure as the base.
For every other child $$$c$$$, iterate through all lines belonging to $$$c$$$.
For a line belonging to $$$a$$$, the other cut belongs to the already merged part. Since $$$a$$$ and the other cut lie in different child-subtrees of $$$x$$$, their LCA is exactly $$$x$$$.
Therefore query the base at
$$$ q=P_xX_a. $$$If the minimum line value is $$$m$$$, then the corresponding two-cut loss is
$$$ Y_a+m. $$$After querying all lines of the smaller structure, insert them into the base structure.
After all children have been merged, the base contains all cuts strictly below $$$x$$$.
If $$$x\ne r$$$, we can pair the edge above $$$x$$$ with any descendant edge. For that case the required query is
$$$ q=H_x. $$$Therefore query the base at $$$H_x$$$ and add $$$Y_x$$$.
Finally, insert the line corresponding to the edge above $$$x$$$ so that ancestors can use it.
Because we always merge the smaller structure into the larger one, every line is moved at most $$$O(\log N)$$$ times.
Each Li Chao insertion/query takes $$$O(\log N)$$$, giving
$$$ O(N\log^2N) $$$time.
Implementation detail
The query coordinate can be as large as
$$$ P_xs_a\le N^2. $$$Therefore the Li Chao domain is $$$[0,N^2+1]$$$.
We must not represent the Li Chao tree using heap-style indices inside a dense vector. A depth of $$$O(\log N)$$$ can then produce node indices of size $$$O(N^2)$$$.
Instead, use an explicit node pool:
struct LiChaoNode {
int line = -1;
int left = -1;
int right = -1;
};
Only nodes that are actually created are stored.
The tree traversal should also be iterative because the input tree may be a path of length $$$2\cdot10^5$$$.
Correctness
Every pair of cuts is classified exactly once.
If one cut is an ancestor of the other, it is considered when the ancestor vertex is processed.
Otherwise, let $$$x$$$ be the LCA of the two cut vertices. They belong to different child-subtrees of $$$x$$$, so the pair is considered when those child structures are merged at $$$x$$$.
For every considered pair, the formulas above give its exact loss by inclusion-exclusion.
Therefore the algorithm finds
$$$ M=\min_{\text{two cuts}}L(\text{cut}_1,\text{cut}_2). $$$The final answer is
$$$ \boxed{S_0-M}. $$$Complexity
There are $$$O(N)$$$ possible cut edges.
Small-to-large merging moves each line only $$$O(\log N)$$$ times, and each Li Chao insertion/query costs $$$O(\log N)$$$.
Therefore:
$$$ \boxed{O(N\log^2N)} $$$time and
$$$ \boxed{O(N\log N)} $$$memory.