#P8258. Maximum Weighted Independent Set Merging
Maximum Weighted Independent Set Merging
Maximum Weighted Independent Set Merging
In this problem, you are given a tree of n
nodes (numbered from 1 to n) where each node i
initially holds a maximum‐weighted independent set (MWIS) problem with a positive integer difficulty di
. For every node 2 ≤ i ≤ n, there is a bidirectional communication channel (an edge) between node i
and node ci
(with 1 ≤ ci < i). Thus the channels form a tree.
You are allowed to perform the following operation any number of times until exactly one node holds a non‐zero problem (the other nodes’ problems become 0):
- Choose a node
x
that currently holds a problem (its difficulty is not 0). - Let N(x) be the set of nodes that are directly connected to
x
(i.e. for which there is an edge with x). In the operation, you combine the problems hosted on node x and on every node in N(x) that still holds a problem. The combined new problem will be assigned to node x and its difficulty becomes
$$ new_d(x) = \Big( d(x)+\sum_{y\in N(x)\;\text{with}\;d(y)>0} d(y) \Big)- d(x). $$
In other words, the new difficulty is the sum of difficulties on node x and all its directly connected nodes that have a problem, minus the original difficulty of node x. - Then, for every neighbor
y
(in N(x)) that had its problem included, its difficulty becomes 0.
The network structure (the tree) never changes; the communication channels are fixed even if a node’s current difficulty is 0. However, a node with 0 difficulty is considered to not be hosting a problem and thus does not contribute when its neighbor is chosen for an operation.
Your goal is to perform a sequence of operations so that exactly one node ends up with a nonzero difficulty (i.e. one problem) and the difficulties on all the other nodes become 0. Because of the inherent loss in every merge (node x loses its own original difficulty when merging), you want the final remaining problem’s difficulty to be as large as possible.
It can be shown that an optimal strategy yields a final difficulty equal to:
$$ \text{answer} = \begin{cases} d_1, & \text{if } n=1,\\ \Big( \sum_{i=1}^{n}d_i \Big) - \sum_{i=2}^{n}\min(d_i,d_{c_i}) , & \text{if } n\ge 2. \end{cases} $$
Here, for each node i (2 ≤ i ≤ n), ci
is the index of a node that is directly connected to i, and min(di, dci)
is taken with the original difficulties of node i and its connected node ci
.
You are to compute this maximum achievable final difficulty.
inputFormat
The input is given as follows:
n d1 d2 ... dn c2 c3 ... cn
Here, n
(1 ≤ n ≤ 2×105) is the number of nodes. The second line contains n positive integers (di
) representing the difficulty of the problem on node i. The third line contains n-1 integers; for each i from 2 to n, ci
(with 1 ≤ ci < i) indicates that there is a bidirectional communication channel between node i and node ci
.
outputFormat
Output a single integer – the maximum possible final difficulty that can be achieved.
sample
2
1 100
1
100